正确的BITMAP步幅公式 [英] Correct stride formula for BITMAP

查看:67
本文介绍了正确的BITMAP步幅公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计算表面步幅

在未压缩的位图中,跨度为从一行像素的起点开始需要的字节数到下一行的开头.

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.

以上内容来自BITMAPINFOHEADER结构,并具有绝对意义.

The above is from BITMAPINFOHEADER structure and makes absolute sense.

同一站点提供了以下公式来计算步幅:

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

对于未压缩的RGB格式,最小步幅始终是图像宽度(以字节为单位),四舍五入到最接近的DWORD.您可以使用以下公式可计算步幅:

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:

步幅=(((((biWidth * biBitCount)+ 31)&〜31)>> 3)

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

假设图像的宽度为600,高度为800和1bpp

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

我希望步幅为600/8 = 75 ...但是上面的公式给了我76!

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

我正在使用(w + 7)/8 并获得了预期的75 ...

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 ?

推荐答案

75不四舍五入到最接近的DWORD.DWORD均为4个字节.76是4的第二高倍.

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

该公式是正确的(在除以得到最终字节数之前,它会四舍五入到位的下一个DWORD).您似乎只是舍入到最接近的字节,这是不一样的.

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.

这篇关于正确的BITMAP步幅公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆