This code works as expected visually, however when going through check50, it returns an error (resizing with a factor of 2 for example)
"Header field bfSize doesn't match. Expected 0xae, not 0x126"
When going through debug50, bfSize was calculated correctly, and I have already written the changes in before resizing the individual pixels. I have no real lead as to where in the code are the extra bytes being added in somehow.
// Upcales a BMP file according to a scaling factor
// includes removed for visual purposes
int main(int argc, char *argv[])
{
// remember the scaling factor
int scale = atoi(argv[1]);
// ensure proper usage
if (argc != 4 || scale > 100 || scale < 0)
{
fprintf(stderr, "Usage: resize n infile outfile\n");
return 1;
}
// remember filenames
char *infile = argv[2];
char *outfile = argv[3];
// 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;
}
// remember infile's padding for scanlines
int inPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// update image width and height according to scaling factor
bi.biWidth *= scale;
bi.biHeight *= scale;
// determine outfile's padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// update image size
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);
// update filesize
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// 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++)
{
// array to keep track of the pixels per scanline
RGBTRIPLE pixels[bi.biWidth];
// iterate over pixels in scanline
for (int j = 0; j < bi.biWidth; j += scale)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write to array n times
for (int k = 0; k < scale; k++)
{
pixels[j + k] = triple;
}
}
for (int l = 0; l < scale; l++)
{
// write array to outfile
fwrite(pixels, sizeof(pixels), 1, outptr);
// write padding to outfile
for (int m = 0; m < padding; m++)
{
fputc(0x00, outptr);
}
}
// skip over padding, if any
fseek(inptr, inPadding, SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}