Apologies that I cannot be more specific here, I've been through my code with the debugger multiple times and have reviewed other examples online but I'm stuck on PSET3 resize. I think I'm saving correctly to an array, writing it to the outfile and using the fseek function correctly but my output larger.bmp file is not correct. Any help would be greatly appreciated, very frustrated right now. My code below:
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// checking for three command lines only
if (argc != 4)
{
printf("./resize n infile outfile\n");
return 1;
}
// checking that the second command line is a number
for(int i = 0, n = strlen(argv[1]); i < n; i++)
{
if (isalpha(argv[1][i]))
{
printf("./resize n infile outfile\n");
return 1;
}
}
// creating the variables
int n = atoi(argv[1]);
char *infile = argv[2];
char *outfile = argv[3];
// checking the second command line is between 0 and 100
if (n > 100 || n < 0)
{
printf("./resize n infile outfile\n");
return 1;
}
// 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);
printf("Could not create %s.\n", outfile);
return 3;
}
// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf, bfr;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
bfr = bf;
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi, bir;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
bir = bi;
// multiply height and width by n
bir.biWidth = bi.biWidth * n;
bir.biHeight = bi.biHeight * n;
// 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);
printf("Unsupported file format.\n");
return 4;
}
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int outpadding = (4 - (bir.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// total size of file and image
bir.biSizeImage = ((sizeof(RGBTRIPLE) * bir.biWidth) + outpadding) * abs(bir.biHeight);
bfr.bfSize = bir.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bfr, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bir, sizeof(BITMAPINFOHEADER), 1, outptr);
// initiate array
RGBTRIPLE *scanlinearray = malloc(sizeof(RGBTRIPLE) * bir.biWidth);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
// iterate over pixels in scanline
for (int k = 0; k < bi.biWidth; k++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple to array n times
for (int l = 0; l < n; l++)
{
scanlinearray[l] = triple;
}
}
// skip over padding
fseek(inptr, padding, SEEK_CUR);
// for n times
for (int j = 0; j < n; j++)
{
// write array to outfile
fwrite(scanlinearray, sizeof(RGBTRIPLE), bir.biWidth, outptr);
// add padding
for (int m = 0; m < outpadding; m++)
{
fputc(0x00, outptr);
}
}
}
// free array
free(scanlinearray);
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}
Thanks!