I've gone through my code, and I've progressed now to the point that the outfile finally scaled correctly, but now the colors are off for some reason, and I have no idea why (Infile and Outfile BMPs shown below my code). I'm not even sure how different colored pixels can be written to the outfile, if I am simply reading and writing pixels from one file to another. Any hints? Here is my code:
/**
* Copies a BMP piece by piece, just because.
*/
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// convert argv[1] to integer
int n = atoi(argv[1]);
// ensure proper usage
if(argc !=4)
{
fprintf(stderr,"Usage: ./resize n small.bmp large.bmp");
return 1;
}
// ensure n <= 100
if(n > 100)
{
fprintf(stderr, "n must be <= 100.");
return 1;
}
// remember filenames
char *infile = argv[2];
char *outfile = argv[3];
// 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);
// redefine some stuff after resizing
bi.biWidth *= n;
bi.biHeight *= n;
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth)+((4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4))*abs(bi.biHeight);
bf.bfSize *= (n*n)*sizeof(RGBTRIPLE)+(4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// 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;
}
int inPadding = (4 - ((bi.biWidth/n) * sizeof(RGBTRIPLE)) % 4) % 4;
int outPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 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
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight/n; i++)
{
// vertically scale "n" times
for (int y=0;y<n;y++)
{
// iterate over pixels in scanline
for (int j = 0; j < bi.biWidth/n; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple to outfile (horizontal scaling)
for(int k = 0; k<n; k++)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// write outfile padding
for (int k = 0; k < outPadding; k++)
{
fputc(0x00, outptr);
}
// return infile cursor to start of current scanline
fseek(inptr, i*((bi.biWidth)/n) + (i)*(inPadding), SEEK_SET);
}
// move on to next scanline
fseek(inptr, ((bi.biWidth)/n) + (inPadding), SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}