I was having issues with my headers because I chose to create a separate header file for my new header changes. However, my code was not passing the check50. Upon researching and questioning, I found out that bfSize could be written using bfOffBits.

bii = bi;

bff = bf; 

int oldWidth = bi.biWidth;
int oldHeight = abs(bi.biHeight);

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

bii.biWidth = bii.biWidth * number;
bii.biHeight = bii.biHeight * number;

int newPadding = (4 - (bii.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

bii.biSizeImage = ((sizeof(RGBTRIPLE) * bii.biWidth) + newPadding) * abs(bii.biHeight);
bff.bfSize = bff.bfOffBits + bii.biSizeImage;

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

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

What I don't understand is why

 bff.bfSize = bff.bfOffBits + bii.biSizeImage;

is working? What is bfOffBits and how does it calculate the entire size of the file, including the headers? I understand that biSizeImage gives the size of the bmp and padding, but how does bfOffBits give the size of BITMAPFILEHEADER and BITMAPINFOHEADER?




The offset, in bytes, from the beginning of the BITMAPFILEHEADER structure to the bitmap bits.

So it's the same as sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER), as we don't have other headers in our file. And those two plus the bitmap make the file.

  • So would you have nay idea why check50 kept denying my code when I had written that bff.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bii.biSizeImage ?
    – Naomi
    Apr 30 '18 at 17:17
  • Those should be equivalent in our case, as we have no other data in there.
    – Blauelf
    May 1 '18 at 19:03

You must log in to answer this question.

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