This pset has been super hard for me. I am trying to resize horizontally before I tackle vertical and I am able to compile and run the program but when I go to open the output file I get the error message "Invalid or Unsupported Image Format."
I've backed out of my changes from copy.c and the problem seems to be when I change the fwrite from &bf to &bfresize and &bi to &biresize. I see several others have had the same problem but my code already includes one of the recommended fixes in one of them and doesn't include what I think was problematic in the others. I've read through them all and I still don't see it.
I was able to get the image to open yesterday and I can't remember what I changed since then that might be causing this problem. I've moved some things around, did I accidentally delete a line of code? I'm sure it's something stupid but I've looked at it over and over and I just don't see it. Help would be much appreciated.
// Copies a BMP file
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: resize factor small large\n");
return 1;
}
// remember filenames
char *small = argv[2];
char *larger = argv[3];
int n = atoi(argv[1]);
// open input file
FILE *inptr = fopen(small, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", small);
return 2;
}
// open output file
FILE *outptr = fopen(larger, "w");
if (outptr == NULL)
{
fclose(inptr);
fprintf(stderr, "Could not create %s.\n", larger);
return 3;
}
//Ensure argv[1] is a number between 1 and 100
if (n < 0 || n > 100)
{
fprintf(stderr, "Choose a resize factor between 1 and 100.\n");
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;
}
BITMAPFILEHEADER bfresize = bf;
BITMAPINFOHEADER biresize = bi;
biresize.biWidth = bi.biWidth * n;
biresize.biHeight = bi.biHeight * n;
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int paddingresize = (4 - (biresize.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
biresize.biSizeImage = ((sizeof(RGBTRIPLE) * biresize.biWidth) + paddingresize) * abs(biresize.biHeight);
bfresize.bfSize = biresize.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bfresize, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&biresize, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
// 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);
for (int l = 0; l < n; l++)
// write RGB triple to outfile
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 < paddingresize; k++)
{
fputc(0x00, outptr);
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}