1

My resize.c runs just fine in the terminal, but the newly-created 'resized' image is broken: no image is displayed, and the message 'bogus header data' is shown. I changed the header files like so in my code:

// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf, bf_resize;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi, bi_resize;
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;
}

// change the header information
bi_resize.biWidth = bi.biWidth * factor;
bi_resize.biHeight = bi.biHeight * factor;

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

// determine size data
bi_resize.biSizeImage = (bi_resize.biWidth * sizeof(RGBTRIPLE) +   padding_resize) * abs(bi_resize.biHeight);
bf_resize.bfSize = 54 + bi_resize.biSizeImage;

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

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

I saw a different question here with the same problem but I didn't get how the person fixed theirs, plus the issue with my code might be different. I will very much appreciate help :)

1

You are correctly reading the BITMAPFILEHEADER and BITMAPINFOHEADER from the file. You are also correctly creating the new ones for the output file, and you correctly change the values you have to change. But you don't copy all the other values from the *HEADERs you read. That's why you get the "bogus header data" message.

1
  • 1
    Thank you, got it now! Aug 24 '15 at 13:35

You must log in to answer this question.

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