0

When I run my code and try to open the image that it created, I receive the following error: "Invalid or unsupported image format."

Can someone please help me understand why this is happening? I initialized the new header types, bf2 and bi2 to the original headers, and I think I wrote the file and info headers in the correct order.

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

// declare new variable types
BITMAPFILEHEADER bf2 = bf;
BITMAPINFOHEADER bi2 = bi;

// declare new variables
bi2.biWidth = bi.biWidth * n;
bi2.biHeight = bi.biHeight * n;
int out_padding = (4 - (bi2.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi2.biSizeImage = ((sizeof(RGBTRIPLE) * bi2.biWidth) + out_padding) * abs(bi2.biHeight);
bf2.bfSize = bi2.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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

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

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    // iterate over pixels in scanline
    for (int j = 0; j < bi.biWidth; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

        // read RGB triple from infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

        // write RGB triple n times to enlarge outfile
        fwrite(&triple, sizeof(RGBTRIPLE), n, outptr);
    }

    // skip over infile's padding, if any
    fseek(inptr, in_padding, SEEK_CUR);

    // write outfile's padding
    for (int k = 0; k < out_padding; k++)
    {
        fputc(0x00, outptr);
    }
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
0

I'm going to assume that you haven't added any code yet to scale the actual image data yet.

I ran your code and it seems to be correctly handling the headers. Perhaps you're executing a different version of executable?

1
  • Hey Cliff, I apologize but I don't understand what you mean when you say that I haven't added code to scale the image data. Isn't that what happens within the for loops? Also, what do you mean by a different version of executable?
    – dmz
    Jul 21 '17 at 19:23

You must log in to answer this question.

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