2

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.

enter image description here

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

1
  • Could you explain how you fixed this? My code has the same peek output...
    – Jason_V
    Commented Apr 9, 2017 at 23:58

1 Answer 1

0

Do you think it could have something to do with writing the headers out before you calculate the new values?

5
  • I thought that I was copying from the infile to outfile the values of headers in my above sub-code using the fwrite function (line 10 and 13 in the above sub-code). My logic was to first copy the entire header from the infile and then change the values of the 4 elements. Am I not doing that in my code?
    – ABor
    Commented May 6, 2016 at 6:07
  • That's exactly what you are doing - simply copying the headers from the infile to the outfile. The problem is that you need to update those 4 header values before you write the headers out. Updating them after the write only changes them in memory, not in the output file.
    – Cliff B
    Commented May 6, 2016 at 7:11
  • So I should write two more fwrites (for bi and bf) after the formulas (at the end of the 1st sub code above) that would include the modified values of those 4 headers? I was thinking of including them but couldn't come up with what should I write for buffer pointer (1st argument of fwriite) to actually change the values of modified headers in the outfile.
    – ABor
    Commented May 6, 2016 at 7:41
  • No. Why would you write two sets of headers to the output file????? You read the headers from the input file into memory, modify the values in the header structure, and then write out the modified two headers!!!
    – Cliff B
    Commented May 6, 2016 at 8:07
  • I think I got the header file correct this time, at least that is what peek is showing. It turns out that a new variable (new_bf etc.) was not at all required, at least while reading and writing the header file. Thank you for your support.
    – ABor
    Commented May 6, 2016 at 8:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .