我想知道是否有人可以指出我在这个问题上的正确方向(没有答案,只是提示问题是什么)。该问题的想法是根据作为命令参数提供的整数来调整BMP图像的大小。如果数字为2,则文件大小将是像素的两倍。
我测试了我创建的代码以更新标头,并且当我运行它时文件的大小是正确的。但是,在尝试调整像素大小后,文件的大小显着增加或在某些情况下减少到大约50个字节。不确定是什么错误。
这是代码:
// resizes a BMP file
#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 integer (1-100) infile outfile\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;
}
int n= atoi(argv[1]);
// 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;
}
// modify bitmapinfoheader for height an width
bi.biWidth = bi.biWidth*n;
bi.biHeight = bi.biHeight*n;
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// modify bisizeimage
bi.biSizeImage = (((bi.biWidth*(sizeof(RGBTRIPLE)))+padding)*abs(bi.biHeight));
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// modify bfsize before writing it
bf.bfSize = (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(bi.biSizeImage));
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
// temporary storage for a triple
RGBTRIPLE triple;
// temporary storage for scanline
RGBTRIPLE scanline[bi.biWidth];
//array position counter
int arraypos = 0;
// iterate over pixels in scanline
for (int j = 0; j < bi.biWidth; j++)
{
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
//write to array n times
for (int l = 0; l < n; arraypos++, l++)
{
scanline[arraypos] = triple;
}
}
for (int m = 0; m < n; m++)
{
// write array to outfile
fwrite(&scanline, sizeof(scanline), 1, outptr);
// Add padding)
for (int k = 0; k < padding; k++)
{
fputc(0x00, outptr);
}
}
// skip over padding, if any
fseek(inptr, padding, SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0; }
答案 0 :(得分:0)
bf.bfSize = (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
sizeof(bi.biSizeImage));
sizeof(bi.biSizeImage)
只是sizeof(int)
。这应该是文件大小,因此您需要bi.biSizeImage
本身。
另一个问题是您更改宽度和高度,但不保存旧的宽度和高度。如果新的宽度和高度较大,则您点击文件末尾。
更改如下,还添加fread
int width_save = bi.biWidth;
int height_save = bi.biHeight;
bi.biWidth *= n;
bi.biHeight *= n;
//calculate width in bytes
int wb_in = ((width_save * 24 + 31) / 32) * 4;
int wb_out = ((bi.biWidth * 24 + 31) / 32) * 4;
bi.biSizeImage = wb_out * abs(bi.biHeight);
bf.bfSize = 54 + bi.biSizeImage;
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
RGBTRIPLE *line_in = malloc(wb_in);
RGBTRIPLE *line_out = malloc(wb_out);
for(int i = 0; i < abs(height_save); i++)
{
fread(line_in, 1, wb_in, inptr);
for(int a = 0; a < width_save; a++)
for (int b = 0; b < n; b++)
line_out[a * n + b] = line_in[a];
for(int a = 0; a < n; a++)
fwrite(line_out, 1, wb_out, outptr);
}