0

Every time I try to resize a picture I get an error saying "Invalid or Unsupported Image Format" and I'm not sure what I've mistyped in my code. I've looked at it, I've googled, and a week ago I even asked a question about the error but my issue hasn't been resolved. I've tried to change my code but nothing has worked. I'm not sure what to do at this point.

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

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

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi, bi2;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
bi2 = bi;

// 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;
}
bi2.biWidth = bi.biWidth * n;
bi2.biHeight = bi.biHeight * n;
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int padding2 =  (4 - (bi2.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi2.biSizeImage = (((sizeof(RGBTRIPLE)*(bi2.biWidth))+padding2)*abs(bi2.biHeight));
bi2.biSize = bi2.biSizeImage + sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);

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

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

Please help and provide reasoning for why my code isn't working!

1

Along with using 1 and not n to write headers (no matter how large the file is scaled up, the headers are only written out once each), some of the header fields are wrong.

Many of the header fields have very similar names. Look very closely at the fields that you are changing and verify that they are the correct fields to be changed!

Then, use the peek and xxd tools to look at the raw data to compare your output to the output from the staff sample files. Start at n=1 and, once it's right, move on to n=2.

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

0

Use 1, not n in the fwrites. You want to write one bitmap file header and one bitmap info header.

2
  • Thank you. However, I have changed my fwrites and my image is still invalid and unsupported. Are there any other issues with my code? – Tenacity Mar 19 '18 at 18:07
  • Value of n is what you expect? If it is, use ~cs50/pset4/peek on both your file and one resized by the staff solution ~cs50/pset4/resize. If the headers match, it's the bitmap data that doesn't. – Blauelf Mar 20 '18 at 10:12

You must log in to answer this question.

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