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!