I've been trying 'resize' for a while now, and I don't understand what's going wrong. On input of small.bmp, the first output line is correct, but the following lines are all different colours (the pixels of small.bmp are green if not white). I think the padding calculations are correct, it's most likely something to do with my vertical scaling.
I created new header files for the output by copying the values, and changing the: biWidth, biHeight and bfSize dependent on the scaling factor n. The code should horizontally scale, add the new padding, then vertically scale for each line of the original input.
Is it something to do with pointers? Are both my sets of header files pointing to the same place?
Does fputc move the pointer in outptr?
Also, if I try to resize smiley.bmp, I get an unreadable output file. Why should I get different results from two different images?
//New header files for resized image
//Change the bitmap width and height
BITMAPINFOHEADER nbi = bi;
nbi.biWidth = n*(bi.biWidth);
nbi.biHeight = n*(bi.biHeight);
// determine padding for scanlines, required for new
int newpadding = (4 - (nbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
BITMAPFILEHEADER nbf = bf;
nbf.bfSize = ((nbi.biWidth * sizeof(RGBTRIPLE) + newpadding) * abs(nbi.biHeight)) + 54;
// write outfile's BITMAPFILEHEADER
fwrite(&nbf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&nbi, sizeof(BITMAPINFOHEADER), 1, outptr);
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; //old padding
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
for (int l = 0; l < n; l++) //vertical scaling
{
// 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);
// write RGB triple to outfile
for (int m = 0; m < n; m++) //horizontal scaling
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
// skip over padding, if any, skips to the next line.
fseek(inptr, padding, SEEK_CUR);
// then add it back (to demonstrate how)
for (int k = 0; k < newpadding; k++)
{
fputc(0x00, outptr);
}
fseek(outptr, newpadding, SEEK_CUR); //set output pointer to the next line.
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}