I have been stuck on the resize pset for a few days now. I have looked at all questions on here and reddit but just can't figure it out. When I do the peek, the headers seem consistent with the staff implementation. I have gone line by line using GDB but still don't see where the problems are. I have come to a bit of a dead end, I have made so many changes to my code to try and get it working I don't know what to try next. It is now showing as compiling but nothing is else is working as per Check50. If anyone could be point me in the general direction of the parts of the code that I should be focusing on I would greatly appreciate. Apologies if my query is similar to others on the forum but I don't know enough to spot it!
int main(int argc, char* argv[])
{
// takes n and converts to int
int n = atoi(argv[1]);
// ensure proper usage
if (argc != 4 || n < 1 || n > 100)
{
printf("Usage: ./resize n(must be in the range 1 - 100) 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)
{
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;
}
//Create header files for output file
BITMAPFILEHEADER bf_out = bf;
BITMAPINFOHEADER bi_out = bi;
//Update the output file headers
bi_out.biWidth= bi.biWidth * n;
bi_out.biHeight= bi.biHeight * n;
// determine padding for old and new scanlines
int old_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int new_padding = (4 - (bi_out.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// calculates new size of image
bi_out.biSizeImage = (bi_out.biWidth + new_padding) * abs(bi_out.biHeight) * sizeof(RGBTRIPLE);
bf_out.bfSize = bi_out.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bf_out, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi_out, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanline
for (int i = 0, h = abs(bi.biHeight); i < h; i++)
{
// do this factor number of times
for (int m = 0; m < n; m++)
{
//iterate through the pixels
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 outfile n times
for (int z = 0; z < n; z++)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// skip over padding, if any
fseek(inptr, old_padding, SEEK_CUR);
// add in new padding if required
for (int k = 0; k < new_padding; k++)
{
fputc(0x00, outptr);
}
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}