我目前正在参加CS50课程,而且我完全坚持调整大小问题(不太舒服)。分配是创建一个带有输入.bmp文件的程序,将其缩放n次并将其写入输出.bmp文件。但我的代码只是扭曲了输入图像,并没有改变大小。 这是为测试提供的bmp(3x3像素,只是放大了这里):https://imgur.com/1v55tjz 当我试图调整它时,它只是转向这个(仍然是3x3):https://imgur.com/JAKQMfY
这是调整大小的代码:
b
这是bmp.h文件:
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: ./copy infile outfile\n");
return 1;
}
// remember filenames
int n = atoi(argv[1]);
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;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
// 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;
}
// create file- and infoheader for output file
BITMAPFILEHEADER outBF = bf;
BITMAPINFOHEADER outBI = bi;
//Scale width and height by n
outBI.biWidth *= n;
outBI.biHeight *= n;
//Calculate padding for input and output file
int outPadding = (4 - (outBI.biWidth * sizeof(RGBTRIPLE) % 4) % 4);
int inPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//Calculate biSizeImage and bfSize for output file
outBI.biSizeImage = ((sizeof(RGBTRIPLE) * outBI.biWidth) + outPadding * abs(outBI.biHeight));
outBF.bfSize = outBI.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
//Instantiate two counters to keep track of whether we have reached the n - 1 times of the recopy method and to be able to fseek to the next line
int countIterations = 1;
int countPositionInFile = 0;
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
countIterations = 1;
for(int j = 0; j < n; j++)
{
// iterate over pixels in scanline
for (int k = 0; k < bi.biWidth; k++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// resize horizontally by writing every pixel in row n times
for(int l = 0; l < n; l++)
{
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// skip over padding, if any
fseek(inptr, inPadding, SEEK_CUR);
//Add padding for this line
for (int l = 0; l < outPadding; l++)
{
fputc(0x00, outptr);
}
if(countIterations == n)
{
countPositionInFile++;
}
int newPosition = (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (countPositionInFile * (bi.biWidth + inPadding)));
// return the "cursor" to the start of the file
fseek(inptr, newPosition, SEEK_SET);
countIterations++;
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}
我希望有人有这项任务的经验,可以提供帮助,我现在感觉很困难,但也许我只是盯着它看了太久。 提前谢谢!
答案 0 :(得分:1)
应使用二进制标志打开文件:
FILE *inptr = fopen(infile, "rb");
FILE *outptr = fopen(outfile, "wb");
您忽略了outBI
和outBF
,您正在将旧标头写入新文件中:
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);//old header
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);//old header
这里使用两种不同的方法来计算填充:
int outPadding = (4 - (outBI.biWidth * sizeof(RGBTRIPLE) % 4) % 4);
int inPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
^ ^
除了偶然之外,它们不可能都是正确的。在这种情况下,我们使用24位图,您可以将填充计算为
int outPadding = outBI.biWidth % 4;
int inPadding = bi.biWidth % 4;
这将与您使用fseek(inptr, inPadding, SEEK_CUR);
和for(...) fputc(0x00, outptr);
我无法弄清楚这段代码:
int newPosition = (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
+ (countPositionInFile * (bi.biWidth + inPadding)));<==remove
fseek(inptr, newPosition, SEEK_SET);<==remove
那应该是(bi.biWidth * 3)
或者您可以删除它并使用ftell
在开始阅读该行之前保存位置,然后使用fseek
返回该位置。< / p>
位图高度应该从下到上读取,但在这种情况下,它没有什么区别。