The bmp images looks pretty much alike with the staff's bmp when there is no padding. But when I use a factor of 3, the image shows random colors, even when xxd padding seems to be ok.
I also checked the headers with peek and they're not the problem. I can't find what's wrong with my program, any ideas?
BITMAPFILEHEADER bfOut = bf;
BITMAPINFOHEADER biOut = bi;
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int newPadding = ((4 - (bi.biWidth * n) % 4) % 4) * sizeof(RGBTRIPLE);
//change values and write outfile's BITMAPFILEHEADER
biOut.biWidth = n * bi.biWidth;
biOut.biHeight = n * bi.biHeight;
bfOut.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + ((newPadding + (bi.biWidth * n)) * (abs(n * bi.biHeight)) * (sizeof(RGBTRIPLE)));
fwrite(&bfOut, sizeof(BITMAPFILEHEADER), 1, outptr);
//change values and write for outfile's BITMAPINFOHEADER
biOut.biSizeImage = ((newPadding + (bi.biWidth * n)) * (abs(n * bi.biHeight)) * (sizeof(RGBTRIPLE)));
fwrite(&biOut, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
//for n times
for (int z = 0; z < n; z++)
{
// 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 n times
for (int t = 0; t < n; t++)
{
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 < newPadding; k++)
{
fputc(0x00, outptr);
}
fseek(inptr, -((bi.biWidth * sizeof(RGBTRIPLE)) + padding), SEEK_CUR);
}
fseek(inptr, (bi.biWidth * sizeof(RGBTRIPLE)) + padding, SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}