I'm struggling with resize.c for quite some time. I've come to the point where it perfectly resizes sometimes (for example, small.bmp with factor 5) but sometimes it doesnt(for example, small.bmp with factor 2 or 3 or 4). Check 50 gives me this:
:) 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
It seems to me that I have no problems with the headers, because when I compare the headers in my output with the ones in the staff implementation's output, they're identical. My guess is that I have some problems with padding calculation or writing. Here's some of my code:
//create header for the new bmp
BITMAPFILEHEADER bfNew;
bfNew = bfOrig;
BITMAPINFOHEADER biNew;
biNew = biOrig;
//make neccessary changes to the header parameters
biNew.biWidth = biOrig.biWidth * n;
biNew.biHeight = biOrig.biHeight * n;
//calculate padding of original and new file
int paddingOrig = (4 - (biOrig.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int paddingNew = (4 - (biNew.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//with the padding calculated, now I can calculate new image size
biNew.biSizeImage = (biNew.biWidth * sizeof(RGBTRIPLE) + paddingNew) * abs(biNew.biHeight);
bfNew.bfSize = biNew.biSizeImage + 54;
//write the headers in new file
fwrite(&bfNew, sizeof(BITMAPFILEHEADER), 1, output);
fwrite(&biNew, sizeof(BITMAPINFOHEADER), 1, output);
int i, j, k, v, h;
for (i = 0; i < abs(biOrig.biHeight); i++)
{
//print out the row n times (resize verticaly)
for (h = 0; h < n; h++)
{
//move the file pointer in input one row back, so I can print out the same row again
if (h != 0)
{
fseek(input, -((biOrig.biWidth * sizeof(RGBTRIPLE)) + paddingNew), SEEK_CUR);
}
//print out every pixel n times (resize horizontaly)
for (j = 0; j < biOrig.biWidth; j++)
{
RGBTRIPLE triple;
fread(&triple, sizeof(RGBTRIPLE), 1, input);
for (v = 0; v < n; v++)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, output);
}
}
//skip the padding in input file
fseek(input, paddingOrig, SEEK_CUR);
//write the padding in output file
for (k = 0; k < paddingNew; k++)
{
fputc(0x00, output);
}
}
}