Hello everyone. I hope you are well.
I am having a bit of a tough time with the resize portion of problem set 4 and I feel I am super close. When I run my program (./resize 4 small.bmp large.bmp), my green square stretches out as per the attached image. Would anyone here be able to give me some pointers in the right direction please?
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char* argv[])
{
// ensure proper usage
if (argc != 4)
{
printf("Usage: ./copy infile outfile\n");
return 0;
}
// remember filenames
int resizeFactor = atoi(argv[1]);
char* infile = argv[2];
char* outfile = argv[3];
if (resizeFactor < 0 || resizeFactor > 100)
{
return 0;
}
// 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);
printf("biSizeImage before: %i\n", bi.biSizeImage);
printf("file size before: %i\n", bf.bfSize);
bi.biWidth *= resizeFactor;
bi.biHeight *= resizeFactor;
printf("biWidth: %i\n", bi.biWidth);
printf("biHeight: %i\n", bi.biHeight);
// determine padding for scanlines
// int overshoot = (bi.biWidth * sizeof(RGBTRIPLE)) % 4;
// int padding = overshoot > 0 ? 4 - overshoot: 0;
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = (bi.biWidth * abs(bi.biHeight)) + padding;
printf("\npadding: %i\n", padding);
printf("biSizeImage after: %i\n", bi.biSizeImage);
printf("file size after: %i\n", bf.bfSize);
// 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);
/* plan to iterate through the pixels:
* read the file in
* for each row of pixels
* copy each pixel n times over
* add any padding if required
*/
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
for (int l = 0; l < resizeFactor; l++)
{
// 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);
// write RGB triple to outfile
for (int h = 0; h < bi.biWidth; h++)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// if we need to add padding, then add it
if (padding != 0)
{
for (int k = 0; k < padding; k++)
{
fputc(0x00, outptr);
}
}
// otherwise skip the padding
else{
fseek(inptr, padding, SEEK_CUR);
}
fseek(outptr, -(bi.biWidth + padding), SEEK_CUR);
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}