-->

Correct stride formula for BITMAP

2019-05-27 08:05发布

问题:

Calculating Surface Stride

In an uncompressed bitmap, the stride is the number of bytes needed to go from the start of one row of pixels to the start of the next row.

The above is from BITMAPINFOHEADER structure and makes absolute sense.

The same site gives the following formula to calculate stride though:

For uncompressed RGB formats, the minimum stride is always the image width in bytes, rounded up to the nearest DWORD. You can use the following formula to calculate the stride:

stride = ((((biWidth * biBitCount) + 31) & ~31) >> 3)

Assume image with width 600, height 800, and 1bpp

I expect the stride to be 600/8 = 75... But the formula above gives me 76 !

I was using (w + 7) / 8 and getting the expected 75...

Still seeing the formula above coming from Microsoft fills me with doubts - is that formula correct ?

回答1:

75 is not rounded up to the nearest DWORD. DWORDs are 4 bytes each. 76 is the next highest multiple of 4.

The formula is correct (it rounds up to the next DWORD in bits before dividing to get the final byte count). You appear to be rounding merely to the nearest byte, which isn't the same thing.