There is some serious flaw in the header file code of my program since all the variables that was required to be modified is showing error while running the peek program (as shown in in the screenshot below). Either my formulas are wrong or I'm not writing the modified elements correctly into the outfile.
Here is my sub-code:
// create variable for the new header files
BITMAPFILEHEADER new_bf;
BITMAPINFOHEADER new_bi;
// determine padding for scanlines
int old_padding = (4 - (old_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int new_padding = (4 - ((old_bi.biWidth * n) * sizeof(RGBTRIPLE)) % 4) % 4;
// write outfile's BITMAPFILEHEADER
fwrite(&old_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&old_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// New element of BITMAPFILEHEADER
new_bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (old_bi.biWidth * n * sizeof(RGBTRIPLE) + new_padding) * abs(old_bi.biHeight) * n;
// New elements of BITMAPINFOHEADER
new_bi.biHeight = old_bi.biHeight * n;
new_bi.biWidth = old_bi.biWidth * n;
new_bi.biSizeImage = (old_bi.biWidth * n * sizeof(RGBTRIPLE) + new_padding) * abs(old_bi.biHeight) * n;
// Do I have to use the fwrite program again to write these modified variable into outfile? If so then how do I use it?
Then the for-loop starts, which of course still a work in progress (the vertical resizing has not yet been dealt with)
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(old_bi.biHeight); i < biHeight; i++)
{
//fseek(inptr, old_bi.biWidth * sizeof(RGBTRIPLE), SEEK_CUR);
// iterate over pixels in scanline
for (int j = 0; j < old_bi.biWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// resize width n times
for (int k = 0; k < n; k++)
{
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// skip over padding, if any
fseek(inptr, old_padding, SEEK_CUR);
// then add it back (to demonstrate how)
for (int m = 0; m < new_padding; m++)
{
fputc(0x00, outptr);
}
}
The output result is that I'm getting a completely green square of the same size (irrespective of the value of n) when I'm applying the code on small.bmp.
Can you please point out the error I'm making?
Thanks in advance.
Here is the complete code for reference:
https://gist.github.com/abor123/022b246babcac9ae3414339929e4c637