I've been struggling with Resize on Pset4 for awhile now and can't quite seem to figure out what I am doing wrong. I am trying to implement the Recopy method with the following psuedocode:
for each row for n-1 times write pixels, padding to outfile send infile cursor back write pixels, padding to outfile skip over infile padding
I've attached my code below, does anyone have any guidance?
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: ./copy infile outfile\n");
return 1;
}
// remember filenames
char *infile = argv[2];
char *outfile = argv[3];
int n = atoi(argv[1]);
printf("%i\n", n);
// 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;
}
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// create header for output file
BITMAPFILEHEADER bfo;
bfo = bf;
BITMAPINFOHEADER bio;
bio = bi;
bio.biWidth = bi.biWidth * n;
bio.biHeight = bi.biHeight * n;
int resized_padding = (4 - (bio.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bio.biSizeImage = (bio.biWidth * abs(bio.biHeight) * 3) + resized_padding*abs(bio.biHeight); // in bytes
bfo.bfSize = bio.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); // the size of the picture in bytes
// 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++)
{
// repeat n-1 times to resize image
for (int x = 0; x < n-1; x++)
{
for (int j = 0; j < bio.biWidth; j++)
{
printf("%i\n", bio.biWidth);
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple to outfile for the amount of times needed
for (int y = 0; y < n-1; y++)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// write padding to outfile
for (int k = 0; k < resized_padding; k++)
{
fputc(0x00, outptr);
}
//send infile cursor back
fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE)), SEEK_CUR);
}
// write pixels and padding to outfile
for (int j = 0; j < bio.biWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple to outfile for the amount of times needed
for (int z = 0; z < n-1; z ++)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
for (int k = 0; k < resized_padding; k++)
{
fputc(0x00, outptr);
}
// skip over padding
fseek(inptr, padding, SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}