因此,该问题应该使用3个参数(factor,infile和outfile)。该因子是1到100之间的正整数。然后假定程序调整了文件内图像的大小。如果因子为1:则产生相同的图像。如果因子为2:则生成两倍大的图像。等等。输出图像应写入输出文件。
当前,我的程序仅对某些比例因子成功地对某些图像执行了此操作。 当我通过课程的IDE检查程序针对该问题运行它时,收到的结果是:
:) resize.c和bmp.h存在。
:) resize.c编译。
:)当n为1时不会调整small.bmp的大小
:(当n为2时会正确调整small.bmp的大小
像素数据的字节34不匹配。预期为0xff,而不是0x00
:(当n为3时会正确调整small.bmp的大小
像素数据的字节48不匹配。预期为0xff,而不是0x00
:(当n为4时正确调整small.bmp的大小
像素数据的字节62不匹配。预期为0xff,而不是0x00
:(当n为5时会正确调整small.bmp的大小
像素数据的字节80不匹配。预期为0xff,而不是0x00
:)在n为2时正确地调整large.bmp的大小
:)在n为2时正确调整smiley.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 aceptable range
int factor = atoi(argv[1]);
if (factor <= 0 || factor > 100)
{
fprintf(stderr, "Must be a positive integer greater than 0 and eqaul 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 itterate 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;
}
答案 0 :(得分:1)
我找到了我希望出现错误的地方。.保存第一行像素后,u将填充宽度查找到新位置。.然后查找三倍的后退宽度*大小,其中不包括填充.. i认为您可以像这样将循环分开..您回头搜索了-1倍,打印了没有填充的内容..最后一次您使用了填充..这样您就可以开始新行了...