Here's the code I finally have. but the issue is the cs50 check says :
:) resize.c and bmp.h exist
:) resize.c compiles
:( doesn't resize 1x1-pixel BMP when n is 1
:( resizes 1x1-pixel BMP to 2x2 correctly when n is 2
:( resizes 1x1-pixel BMP to 3x3 correctly when n is 3
:( resizes 1x1-pixel BMP to 4x4 correctly when n is 4
:( resizes 1x1-pixel BMP to 5x5 correctly when n is 5
:( resizes 2x2-pixel BMP to 4x4 correctly when n is 2
and when I try to just go head resize the image from small.bmp to large.bmp, the system says "invalid unsupported image or format". SO FRUSTRATED......I have been working on this resize.c for days!!! I read all over the internet and people say it was because of the issue of header, but I just cannot figure out!! please help me and tell me what happened.
/**
* copy.c
*
* Computer Science 50
* Problem Set 4
*
* Resize a BMP piece by piece, just because.
*/
#include <stdio.h>
#include <stdlib.h>
#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];
if ((factor < 1) || (factor > 100))
{
printf( "factor can only be the number in range 1~100\n");
return 4;
}
// 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;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
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;
}
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
bi.biWidth *= factor;
bi.biHeight *= factor;
int old_width = bi.biWidth;
int old_height = bi.biHeight;
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
int new_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// determine the padding the new bmp
int padding = (4 - (old_width * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = (sizeof(RGBTRIPLE) * bi.biWidth + new_padding) * bi.biHeight;
bi.biSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// iterate over infile's scanlines
for (int i = 0; i < old_height; i++)
{
// iterate over pixels in scanline
for (int k = 0; k < factor; k++)
{
for (int j = 0; j < old_width; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
for (int q = 0; q < factor; q++)
{
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
fseek(inptr, padding, SEEK_CUR);
for(int m = 0; m < new_padding; m++)
{
fputc(0x00, outptr);
}
if( k + 1 < factor)
{
fseek(inptr, 54 + (old_width * sizeof(RGBTRIPLE) + padding * i), SEEK_SET);
}
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}