0

The code does what it's suppose to do visually. It resizes the image but it appears that there is an error with the bfSize.

I have no idea where the mistake is and have tried everything I can think of. Is there something I forgot to update or does anyone know where the error is coming from?

This is the error message:

Results for cs50/problems/2019/x/resize/less generated by check50 v3.0.9
:) resize.c and bmp.h exist.
:) resize.c compiles.
:) doesn't resize small.bmp when n is 1
:( resizes small.bmp correctly when n is 2
expected 0xae, not 0x5a in header field bfSize
:( resizes small.bmp correctly when n is 3
expected 0x132, not 0x5a in header field bfSize
:( resizes small.bmp correctly when n is 4
expected 0x1e6, not 0x5a in header field bfSize
:( resizes small.bmp correctly when n is 5
expected 0x306, not 0x5a in header field bfSize
:( resizes large.bmp correctly when n is 2
expected 0x6f6, not 0x1e6 in header field bfSize
:( resizes smiley.bmp correctly when n is 2
expected 0x336, not 0xf6 in header field bfSize
:( resizes smiley.bmp correctly when n is 3
expected 0x6f6, not 0xf6 in header field bfSize

relevant code [EDIT: unrelated code removed for Honor Code Compliance. ]

// 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;
}

//update BITMAPINFOHEADER
BITMAPINFOHEADER outbi = bi;
outbi.biHeight = bi.biHeight * n;
outbi.biWidth = bi.biWidth * n;

//update BITMAPFILEHEADER
BITMAPFILEHEADER outbf = bf;

// write outfile's BITMAPFILEHEADER
fwrite(&outbf, sizeof(BITMAPFILEHEADER), 1, outptr);

// write outfile's BITMAPINFOHEADER
fwrite(&outbi, sizeof(BITMAPINFOHEADER), 1, outptr);

// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

// determine padding for scanlines (updated)
int outpadding = (4 - (outbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
outbi.biSizeImage = ((sizeof(RGBTRIPLE) * outbi.biWidth) + outpadding) * abs(outbi.biHeight);

//outbf.bfSize = outbi.biSizeImage + sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
outbf.bfSize = outbi.biSizeImage + 54;

...
}
1

Wouldn't it be more useful to write out the new headers after recalculating all the header values? ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .