0

In pset4's resize.c, everytime I run the program, it gives me "unsupported file format" as result as it should if the infile is not 24-bit uncompressed bmp 4.0 but, I have used copied and pasted the code from copy.c where a image passes the test but through my code it doesn't. Here's my code:

  #include <stdio.h>
  #include <stdlib.h>

  #include "bmp.h"

int main(int argc, char *argv[])
{
    if(argc != 4)
    {
        fprintf(stderr, "Usage: ./resize n infile outfile\n");
        return 1;
    }

int n = atoi(argv[1]);

if(n < 0 || n > 100)
{
    fprintf(stderr, "The resizing factor 'n' shouldn't be greater than 100 or negative.\n");
    return 2;
}

char *infile = argv[2];
char *outfile = argv[3];

FILE *in = fopen(infile, "r");
if(in == NULL)
{
    fprintf(stderr, "Could not open %s\n", infile);
    return 3;
}
FILE *out = fopen(outfile, "w");
if(out == NULL)
{
    fprintf(stderr, "Could not open %s\n", outfile);
    return 4;
}

BITMAPINFOHEADER bi, bx;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, in);
bx = bi;
BITMAPFILEHEADER bf, by;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, in);
by = bf;

if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || 
    bi.biBitCount != 24 || bi.biCompression != 0)
{
    fclose(out);
    fclose(in);
    fprintf(stderr, "Unsupported file format.\n");
    return 4;
}

bx.biHeight = n * bi.biHeight;
bx.biWidth = n * bi.biWidth;

int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;    
int newp = (4 - (bx.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

bx.biSizeImage = (bx.biWidth * sizeof(RGBTRIPLE) + newp) * abs(bx.biHeight);
by.bfSize = bx.biSizeImage + sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);

fwrite(&bx, sizeof(BITMAPINFOHEADER), 1, out);
fwrite(&by, sizeof(BITMAPFILEHEADER), 1, out);

for(int i = 0, a = bx.biHeight; i < a; i++)
{
    for(int j = 0, b = bx.biWidth; j < b; j++)
    {
        RGBTRIPLE triple;
        fread(&triple, sizeof(RGBTRIPLE), 1, in);
        for(int k = 0; k < n; k++)
        {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, out);
        }
    }

    fseek(in, padding, SEEK_CUR);

    for(int l = 0; l < newp; l++)
    {
        fputc(0x00, out);
    }
}

fclose(in);
fclose(out);

return 0;
}

I removed the code for checking the infile and yet, after the program compiles, I get a new image file as I should and when I open the new file, it says "Invalid or Unsupported image format". Can't understand why this is the case.

2

bmp starts with BITMAPFILEHEADER, then BITMAPINFOHEADER. You read in reverse order.

4
  • Yes. That's the reason for the first problem but again, after the program runs and when I try to open the resized image, it says "Invalid or Unsupported file format". Why is this the case? – Tejesh Atr Jul 9 '17 at 9:53
  • so now you read headers correctly, pass unsupported format check, but image doesn't open? – obesman Jul 9 '17 at 12:02
  • It does. Thank you for your time :) – Tejesh Atr Jul 10 '17 at 12:40
  • I have the same problem and my code is almost similar to than in the question. I got the resized image file and when I come to open it, nothing appears and a message tells me "Unsupported file format" shows up!! @TejeshAtr How did you solve it? – S. Sabry Oct 6 '19 at 12:25

You must log in to answer this question.

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