Now it's a little bit better since:
:) resize.c and bmp.h exist
:) resize.c compiles
:( doesn't resize 1x1-pixel BMP when n is 1
:( resizes 1x1-pixel BMP to 2x2 correctly when n is 2
:( resizes 1x1-pixel BMP to 3x3 correctly when n is 3
:) resizes 1x1-pixel BMP to 4x4 correctly when n is 4
:( resizes 1x1-pixel BMP to 5x5 correctly when n is 5
:) resizes 2x2-pixel BMP to 4x4 correctly when n is 2
But still have no clue what the problem is.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bmp.h"
int main(int argc, char* argv[])
{
// ensure proper usage
int factor = atoi(argv[1]);
if (argc != 4 || factor < 1 || factor > 100)
{
printf("Usage: './copy n infile outfile' where n is an integer that is not less than 1 but not more than 100\n");
return 1;
}
// 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 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;
}
// Copy of original data
int originalbiWidth = bi.biWidth;
int originalbiHeight = bi.biHeight;
// replace some data in header
bi.biWidth = bi.biWidth * factor;
bi.biHeight = bi.biHeight * factor;
// determine original padding for scanlines
int inpadding = (4 - (originalbiWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// determine new padding for scanlines
int outpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// replace some data in header
bi.biSizeImage = (bi.biWidth + outpadding) * abs(bi.biHeight) * sizeof(RGBTRIPLE);
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// 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, l = abs(originalbiHeight); i < l; i++)
{
// write rows 'factor' times to outfile
for (int m = 0; m < factor; m++)
{
if (m != 0)
{
// return the cursor to the beginning of the scanline
fseek(inptr, - (originalbiWidth * sizeof(RGBTRIPLE) + inpadding), SEEK_CUR);
}
// iterate over pixels in scanline
for (int j = 0; j < originalbiWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple 'factor' times to outfile
for (int n = 0; n < factor; n++)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// skip over padding, if any
fseek(inptr, inpadding, SEEK_CUR);
// then add it back (to demonstrate how)
for (int k = 0; k < outpadding; k++)
{
fputc(0x00, outptr);
}
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}