I did the debugging using GDB. And it gets through pretty smoothily yet when I open the output file it says " invalid/ unsupported file format". NOTE : obi = bi ( where obi is the bmpinfoheader for the output file.) wasn't working in cs50 ide. So I had to do it manully for each structure element. Thank you for your time. /** this is the program to resize the * size of the bit map image */ #include #include
#include "bmp.h"
int main(int argc, char* argv[])
{
// ensure proper usage
if (argc != 4)
{
printf("Usage: ./copy infile outfile\n");
return 1;
}
// remember filenames
int factor = atoi(argv[1]);
char* infile = argv[2];
char* outfile = argv[3];
// open input file
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
printf("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, obf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi, obi;
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 4;
}
obi = bi;
obf = bf;
int biHeight = bi.biHeight > 0 ? bi.biHeight: - bi.biHeight;
obi.biSizeImage = (bi.biWidth + biHeight) * factor * 3 + ((bi.biWidth * factor * 3) % 4) * biHeight;
// write outfile's BITMAPFILEHEADER
obi.biWidth = (bi.biWidth * factor);
obi.biHeight = bi.biHeight * factor;
obf.bfSize = obi.biSizeImage + 54;
fwrite(&obf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&obi, sizeof(BITMAPINFOHEADER), 1, outptr);
// determine padding for scanlines
int obipadding = (4 - (obi.biWidth * 3) % 4);
// iterate over infile's scanlines
int bipadding = 4 - ((bi.biWidth * 3) % 4);
for (int i = 0; i < biHeight; i++)
{
// iterate over pixels in scanline
for (int n = 0; n < factor; n++)
{
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 to the temperory file
for (int m = 0; m < factor; m++)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
for (int d = 0; d < obipadding; d++)
{
fprintf(outptr, "%c", 0x00);
}
fseek(inptr, - bi.biWidth, SEEK_CUR);
}
fseek(inptr, bi.biWidth + bipadding, SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}