I'm trying to do pset 5 and can not seem to resize my image, I tried many different way to formulate the image size but somehow its not working, I was wondering if anyone can spot what I am doing wrong? much appreciated for anyone's help, here is the 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 1;
}
// resizing the varible
int resize = atoi(argv[1]);
// checking invalid resize values
if (resize < 0 || resize > 100)
{
printf("Resize must be between 1 and 100 \n");
return 2;
}
// 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 3;
}
// open output file
FILE* outptr = fopen(outfile, "w");
if (outptr == NULL)
{
fclose(inptr);
fprintf(stderr, "Could not create %s.\n", outfile);
return 4;
}
// 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;
}
// making varibles for bitmap headers for resizefile
BITMAPFILEHEADER resize_bf;
BITMAPINFOHEADER resize_bi;
resize_bf = bf;
resize_bi = bi;
// updating by the size factor
resize_bi.biWidth = bi.biWidth * resize;
resize_bi.biHeight = bi.biHeight * resize;
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// determine the padding for resizing it with its factor
int outPadding = (4 - (resize_bi.biWidth *sizeof(RGBTRIPLE)) % 4) % 4;
// THIS FORMULATION I NEED TO FIGURE OUT CORRECTLY // calculating file and image size from formulating the following
resize_bi.biSizeImage = (resize_bi.biWidth * sizeof(RGBTRIPLE) + outPadding) * abs(resize_bi.biHeight); // resize_bf.bfSize = bf.bfSize - bi.biSizeImage + resize_bi.biSizeImage; resize_bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + resize_bi.biSizeImage;
// 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
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
// write each line for resize times
for (int s = 0; s < resize; s++)
{
// iterate over pixels in scanline
for (int j = 0; j < bi.biWidth; j++)
{
// temporary storage for width
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write the tripled size to the outfile
for (int t = 0; t < resize; t++)
{
// write RGB triple to outfile for horizontal update
fwrite(&triple,sizeof(RGBTRIPLE), 1, outptr);
}
}
// skip over padding, if any
fseek(inptr, padding, SEEK_CUR);
// then add it back (to demonstrate how)
for (int k = 0; k < padding; k++)
{
fputc(0x00, outptr);
}
// more skipping for resize padding if its odd number of bytes
fseek(inptr, -(bi.biWidth * 3 + padding),SEEK_CUR);
}
// same as above but for other direction
fseek(inptr, bi.biWidth * 3 + padding, SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}