c - 调整大小程序适用于某些人而不适用于其他人
问题描述
所以这个问题应该有 3 个参数(因子、输入文件和输出文件)。该因子是一个从 1 到 100 的正整数。然后假设程序会调整 infile 图像的大小。如果因子为 1:生成相同的图像。如果因子为 2:生成两倍大的图像。等等。输出图像应写入输出文件。
目前,我的程序对某些图像成功地做到了这一点,并且仅在某些比例因子上。
当我通过课程的 IDE 检查程序运行这个问题时,我收到的结果是:
:) resize.c 和 bmp.h 存在。
:) resize.c 编译。
:) 当 n 为 1 时不会调整 small.bmp 的
大小 :( 当 n 为 2 时正确调整 small.bmp的大小
像素数据的 34 字节不匹配。预期为 0xff,而不是 0x00
:( 当 n 为 3字节 48 的像素数据不匹配时,正确调整 small.bmp 的大小。预期为 0xff,而不是 0x00
:( 当 n 为 4字节 62 的像素数据不匹配时,正确调整 small.bmp 的大小。预期为 0xff,而不是 0x00
:( 当 n 为 5字节 80 的像素数据不匹配时,正确调整 small.bmp 的大小。预期为 0xff,而不是 0x00
:) 当 n 为 2 时正确
调整 large.bmp 的大小 :) 当 n 为 2 时正确调整笑脸.bmp
// Copies a BMP file and resizes it
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: ./resize factor infile outfile\n");
return 1;
}
// Check argument 1 to see if integer within acceptable range
int factor = atoi(argv[1]);
if (factor <= 0 || factor > 100)
{
fprintf(stderr, "Must be a positive integer greater than 0 and equal or less than 100\n");
return 1;
}
// remember filenames
char *infile = argv[2];
char *outfile = argv[3];
// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
// open output file
FILE *outptr = fopen(outfile, "w");
if (outptr == NULL)
{
fclose(inptr);
fprintf(stderr, "Could not create %s.\n", outfile);
return 3;
}
// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf;
BITMAPFILEHEADER bf_New;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
bf_New = bf;
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
BITMAPINFOHEADER bi_New;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
bi_New = bi;
// ensure infile is (likely) a 24-bit uncompressed BMP 4.0
if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
bi.biBitCount != 24 || bi.biCompression != 0)
{
fclose(outptr);
fclose(inptr);
fprintf(stderr, "Unsupported file format.\n");
return 4;
}
// set new height and width of BMP
bi_New.biHeight = bi.biHeight * factor;
bi_New.biWidth = bi.biWidth * factor;
// calculate padding for old file and new file
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int padding_New = (4 - (bi_New.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// set the file size for the new file
bi_New.biSizeImage = (bi_New.biWidth * sizeof(RGBTRIPLE) + padding_New) * abs(bi_New.biHeight);
bf_New.bfSize = bi_New.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bf_New, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi_New, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
// itterate factor times
for (int k = 0; k < factor; k++)
{
// iterate over pixels in scanline
for (int j = 0; j < bi.biWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// iterate over horizontal pixels
for (int l = 0; l < factor; l++)
{
// write RGB triple to outfile iterate the same pixel by factor times
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// skip over padding, if any
fseek(inptr, padding, SEEK_CUR);
// add new padding
for (int m = 0; m < padding_New; m++)
{
fputc(0x00, outptr);
}
// seek back to the beginning of row in input file, but not after iteration of printing
if (k + 1 < factor )
{
fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE)), SEEK_CUR);
}
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}
解决方案
我找到了我希望出错的地方..在保存第一行像素后,你寻找填充宽度到新位置..然后寻找三元组的宽度*大小,不包括填充..我想你可以像这样的单独循环..对于因子-1次你回溯,对于你打印的没有填充的内容..最后一次你用填充向前搜索..这样你就准备好开始新行了......
推荐阅读
- promela - 使用破坏消息的机制修改上述 Promela 模型
- php - 从包含多个案例的查询中获取数据
- google-sheets - Countif 单元格和右侧单元格
- ruby-on-rails - 通知 ruby on rails
- android - android.support.constraint.ConstraintLayout 不能转换为 android.widget.TextView
- c# - 下载文件并通过 FolderbrowserDialog 选择其位置
- java - 区分全名、公司名称和职务
- vue.js - Vue(x) 在使用 Vue.set(...) 时不是反应式的,但在使用 Object.assign 时是反应式的,为什么?
- angular - 订阅商店切片 - ngrx
- javafx - 在 Raspberry Pi 3 上使用 JavaFX 11 播放 mp3