My code seems to work perfectly, and the headers match those of the staff solution. However, it fails check50, with the message "Image has more bytes than expected" showing up for almost all criteria.
(this shows up:
:) resize.c and bmp.h exist.
:) resize.c compiles.
:) doesn't resize small.bmp when n is 1
:( resizes small.bmp correctly when n is 2
Image has more bytes than expected.
:( resizes small.bmp correctly when n is 3
Image has more bytes than expected.
:( resizes small.bmp correctly when n is 4
Image has more bytes than expected.
:( resizes small.bmp correctly when n is 5
Image has more bytes than expected.
:( resizes large.bmp correctly when n is 2
Image has more bytes than expected.
:( resizes smiley.bmp correctly when n is 2
Image has more bytes than expected.
:( resizes smiley.bmp correctly when n is 3
Image has more bytes than expected.)
I attached my code. Thanks!
int main(int argc, char *argv[]) { // ensure proper usage if (argc != 4) { fprintf(stderr, "Usage: ./resize n infile outfile\n"); return 1; }
// remember filenames
char *infile = argv[2];
char *outfile = argv[3];
char *q = argv[1];
int n = atoi(q);
if (n < 1 || n >= 100)
{
return 1;
}
// 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;
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;
}
int old_width = bi.biWidth;
bi.biWidth *= n;
bi.biHeight *= n;
int padding = (4 - (old_width * sizeof(RGBTRIPLE)) % 4) % 4;
int paddinga = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = (((sizeof(RGBTRIPLE) * bi.biWidth) + paddinga) * abs(bi.biHeight));
bf.bfSize = bf.bfOffBits + bi.biSizeImage;
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
int rowcount = 0;
while (rowcount < n)
{
// iterate over pixels in scanline
for (int j = 0; j < old_width; j++)
{
int pixelcount = 0;
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
while (pixelcount < n)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
pixelcount++;
}
}
for (int k = 0; k < paddinga; k++)
{
fputc(0x00, outptr);
}
if (rowcount < (n - 1))
{
fseek(inptr, -(old_width * (int)sizeof(RGBTRIPLE)), SEEK_CUR);
}
rowcount++;
}
fseek(inptr, padding, SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}