CS50 pset4调整尺寸不太舒适。在我写入文件之前,标题似乎已正确更新

CS50 pset4调整尺寸不太舒适。在我写入文件之前,标题似乎已正确更新,c,debugging,image-resizing,bmp,cs50,C,Debugging,Image Resizing,Bmp,Cs50,我想知道是否有人能为我指出这个问题的正确方向(没有答案,只是暗示问题是什么)。问题的想法是根据作为命令参数提供的整数调整BMP图像的大小。如果数字为2,则文件大小将是像素的两倍 我测试了为更新标题而创建的代码,运行时文件的大小是正确的。但是,在尝试调整像素大小后,文件的大小会显著增加,有时会减少到50字节左右。不确定是什么错误 代码如下: // resizes a BMP file #include <stdio.h> #include <stdlib.h> #incl

我想知道是否有人能为我指出这个问题的正确方向(没有答案,只是暗示问题是什么)。问题的想法是根据作为命令参数提供的整数调整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; }
//调整BMP文件的大小
#包括
#包括
#包括“bmp.h”
int main(int argc,char*argv[]){
//确保正确使用
如果(argc!=4)
{
fprintf(stderr,“用法:调整整数大小(1-100)填充输出文件\n”);
返回1;
}
//记住文件名
char*infle=argv[2];
char*outfile=argv[3];
//打开输入文件
文件*inptr=fopen(填入“r”);
如果(inptr==NULL)
{
fprintf(stderr,“无法打开%s。\n”,填充);
返回2;
}
//打开输出文件
文件*outptr=fopen(输出文件,“w”);
if(outptr==NULL)
{
fclose(inptr);
fprintf(stderr,“无法创建%s.\n”,输出文件);
返回3;
}
int n=atoi(argv[1]);
//读取内嵌的BITMAPFILEHEADER
位图文件头;
fread(&bf,sizeof(BITMAPFILEHEADER),1,inptr);
//读取内嵌的BitMapInfo标头
BitMapInfo头bi;
fread(&bi,sizeof(BitMapInfo标头),1,inptr);
//确保infle(可能)是24位未压缩的BMP 4.0
如果(bf.bfType!=0x4d42 | | bf.BFOFFFITS!=54 | | bi.biSize!=40||
bi.biBitCount!=24 | | bi.biCompression!=0)
{
fclose(outptr);
fclose(inptr);
fprintf(stderr,“不支持的文件格式。\n”);
返回4;
}
//修改高度和宽度的BitMapInfo标头
bi.biWidth=bi.biWidth*n;
bi.biHeight=bi.biHeight*n;
//确定扫描线的填充
int padding=(4-(bi.biWidth*sizeof(RGBTRIPLE))%4)%4;
//修改bisizeimage
bi.BIIZEMAGE=((bi.biWidth*(sizeof(RGBTRIPLE)))+填充)*abs(bi.biHeight));
//写入输出文件的BITMAPFILEHEADER
fwrite(&bf,sizeof(BITMAPFILEHEADER),1,outptr);
//在写入之前修改bfsize
bf.bfSize=(sizeof(BITMAPFILEHEADER)+sizeof(bitmapinfo头)+sizeof(bi.biSizeImage));
//写入输出文件的BitMapInfo标头
fwrite(&bi,sizeof(BitMapInfo标头),1,outptr);
//迭代infle的扫描线
对于(inti=0,biHeight=abs(bi.biHeight);i
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);
}
int-width\u save=bi.biWidth;
int height_save=bi.biHeight;
bi.biWidth*=n;
bi.bi高度*=n;
//以字节为单位计算宽度
int wb_in=((宽度保存*24+31)/32)*4;
int wb_out=((bi.biWidth*24+31)/32)*4;
bi.BISIZEMAGE=wb_out*abs(bi.biHeight);
bf.bfSize=54+bi.bisizemage;
fwrite(&bf,sizeof(BITMAPFILEHEADER),1,outptr);
fwrite(&bi,sizeof(BitMapInfo标头),1,outptr);
RGBTRIPLE*线_in=malloc(wb_in);
RGBTRIPLE*line_out=malloc(wb_out);
对于(int i=0;i
欢迎使用堆栈溢出!寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现所需的最短代码(请参阅)。没有明确问题陈述的问题对其他读者没有用处,也很可能得不到回答。有关更多帮助,请查看“”,您应该在调试器中运行程序并检查使用的值。提示:位置
bi.biWidth=bi.biWidth*n是您问题的一部分。谢谢您的解答!老实说,你回答的某些部分让我困惑,但一开始就有了解决我的代码的线索。我正在迭代像素,并用新值填充填充填充。
sizeof(BITMAPFILEHEADER)
始终为14,
sizeof(BITMAPINFOHEADER)
始终为40。总数是54,我忘记解释了。我使用了一个计算“字节宽度”的公式,它确保这个值始终是4的倍数。你对我用了类似的方法
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);
}