0

I do not quite understand how this reuse of variables is incorrect:

// save initial values
    int initial_Height = bi.biHeight;
    int initial_Width = bi.biWidth;
    int initial_padding = (4 - (initial_Width * sizeof(RGBTRIPLE)) % 4) % 4;

    // update BITMAPFILEHEADER BITMAPINFOHEADER properties
    int new_Height = initial_Height * n;
    int new_Width = initial_Width * n;
    int new_padding = (4 - (new_Width * sizeof(RGBTRIPLE)) % 4) % 4;

    bi.biSizeImage = ((sizeof(RGBTRIPLE) * new_Width) + new_padding) * abs(new_Height);
    bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

But this is correct:

// save initial values
    int initial_Height = bi.biHeight;
    int initial_Width = bi.biWidth;
    int initial_padding = (4 - (initial_Width * sizeof(RGBTRIPLE)) % 4) % 4;

    // update BITMAPFILEHEADER BITMAPINFOHEADER properties
    bi.biHeight *= n;
    bi.biWidth *= n;
    int new_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + new_padding) * abs(bi.biHeight);
    bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

And, this seems to be correct:

...
int new_Height = bi.biHeight *= n;
int new_Width = bi.biWidth *= n;
...

Can someone explain why bi.biHeight and bi.biWidth can not be directly assigned to variables and reused like so int var = bi.biHeight int new_var = var * 2?

0

The problem is that you write bi, and in the first version, it still contains the old image dimensions, and an image viewer will rely on that information. So first one is not fully wrong, it's just missing the assignments to bi.biHeight and bi.biWidth (if bi is the header you write).

2
  • So, int new_Height = initial_Height * n; where int initial_Height = bi.biHeight; is not the same as int new_Height = bi.biHeight * n;?
    – alljamin
    Sep 19 '19 at 0:32
  • It is the same, assuming bi.biHeight to be the input image height. I don't question the final value of new_Height. The difference is in the final value of bi.biHeight, which gets written to the output file.
    – Blauelf
    Sep 19 '19 at 6:04

You must log in to answer this question.

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