Correct stride formula for BITMAP

房东的猫 提交于 2019-12-07 22:18:35

问题


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.



来源:https://stackoverflow.com/questions/28049838/correct-stride-formula-for-bitmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!