I have multiple errors when using check50 and I am not sure why. I realize that multiple people already addressed similar error as mine, but I do not understand the explanations. I would appreciate if someone would be able to directly point to where I need to add/delete/edit parts of my code. Help is very appreciative, and as always, thank you.
Code:
// Copies a BMP file
// resizes a BMP via input of "n"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: ./resize n infile outfile\n");
return 1;
}
// recieve resize factor, "n"
int n = atoi(argv[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;
}
// changing parameters to n times
BITMAPFILEHEADER nbf = bf;
BITMAPINFOHEADER nbi = bi;
nbi.biWidth *= n;
nbi.biHeight *= n;
// determine padding for scanlines
int npadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
nbi.biSizeImage = (nbi.biWidth * sizeof(RGBTRIPLE) + npadding) * abs(nbi.biHeight);
nbf.bfSize = nbi.biSizeImage + nbf.bfOffBits;
// write outfile's BITMAPFILEHEADER
fwrite(&nbf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&nbi, sizeof(BITMAPINFOHEADER), 1, outptr);
//determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) %4;
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
RGBTRIPLE line[nbi.biWidth];
int lineIterator = 0;
// iterate over pixels in scanline
for (int j = 0; j < bi.biWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr); // [R G padding]
// before writing to outfile we need to modify the pixels
// write RGB triple to outfile ... n*w
for (int x = 0; x < n; x++)
line[lineIterator] = triple;
}
// skip over padding, if any
fseek(inptr, padding, SEEK_CUR);
// then add it back (to demonstrate how)
for (int k = 0; k < n; k++)
{
fwrite(line, sizeof(RGBTRIPLE), nbi.biWidth, outptr);
// then add it back (to demonstrate how)
for (int x = 0; x < npadding; x++)
{
fputc(0x00, outptr);
}
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}
Errors:
:) resize.c and bmp.h exist.
:) resize.c compiles.
:( doesn't resize small.bmp when n is 1
Byte 5 of pixel data doesn't match. Expected 0xff, not 0x00
:( resizes small.bmp correctly when n is 2
Header field bfSize doesn't match. Expected 0xae, not 0xb4
:( resizes small.bmp correctly when n is 3
Header field bfSize doesn't match. Expected 0x132, not 0x144
:( resizes small.bmp correctly when n is 4
Header field bfSize doesn't match. Expected 0x1e6, not 0x20a
:( resizes small.bmp correctly when n is 5
Byte 4 of pixel data doesn't match. Expected 0x00, not 0x41
:( resizes large.bmp correctly when n is 2
Byte 4 of pixel data doesn't match. Expected 0x00, not 0x24
:( resizes smiley.bmp correctly when n is 2
Byte 4 of pixel data doesn't match. Expected 0xff, not 0x75
:( resizes smiley.bmp correctly when n is 3
Byte 4 of pixel data doesn't match. Expected 0xff, not 0xf7