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.