For PSET5 Resize.c, I have tried implementing the following with n being the resize factor inputted by the user. The code compiles but returns an image of an conglumeration of colours (or a corrupted image).... Not sure why even after hours of debugging w GDB as well as looking thr previous posts on stackexchange ... Can anyone point out my mistake please??
UPDATED:
int main(int argc, char* argv[])
{
// ensure proper usage
if (argc != 4)
{
printf("Usage: ./resize n infile outfile\n");
return 1;
}
// remember filenames and resize factor, n.
int n = atoi(argv[1]);
char* infile = argv[2];
char* outfile = argv[3];
if (n < 1)
{
printf("n should be a positive integer greater than 0\n");
return 5;
}
// 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);
//Store previous values (for calculating padding later) as tmp.
LONG tmpbiWidth = bi.biWidth;
LONG tmpbiHeight = bi.biHeight;
// determine padding for scanlines for old (input) and new (output)
int paddingNEW = (4 - (n*tmpbiWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int paddingOLD = (4 - (tmpbiWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//Alter bi for output.
bi.biWidth = n*bi.biWidth;
bi.biHeight = n*bi.biHeight;
bi.biSizeImage = 3 * abs(bi.biHeight) * bi.biWidth + (paddingNEW * abs(bi.biHeight));
bf.bfSize = bi.biSizeImage + 54;
// 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;
}
// 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 (or rows)
for (int i = 0, biHeight = abs(tmpbiHeight); i < biHeight; i++)
{
fseek(inptr,-bi.biWidth*n - paddingNEW,SEEK_CUR);
//Resize vertically - copying each row of edited original copy n times.
for (int k = 0; k < n; k++)
{
// iterate over pixels in scanline
for (int j = 0; j < tmpbiWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple n times to outfile
for (int p = 0; p < n; p++)
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
// skip over padding, if any when reading input (actually this isn't really necessary in this case)
fseek(inptr, paddingOLD, SEEK_CUR);
// then add new number of padding
for (int k = 0; k < paddingNEW; k++)
{
fputc(0x00, outptr);
}
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}