BMP图像四字节对齐的问题
2013-03-01 00:00
260 查看
1、内存分配单位是32位的,即4字节;
2、位图中每行象素的数据是连续的,而下一行不能和上一行共一个分配单元(4字节),所以每行象素的数据长度必须是4字节的倍数;
3、代码说明如下:
int WidthBytes( int nBits, int nWidth )
{//nBits为色彩位数, nWidth为每行象素个数
int nWidthBytes;//每行象素的数据长度
nWidthBytes = nWidth;
if( nBits == 1 ) nWidthBytes = ( nWidth + 7 ) / 8;
else if( nBits == 4 ) nWidthBytes = ( nWidth + 1 ) / 2; /*竟然没有8位的。256色不是很常见吗?!*/
else if( nBits == 16 ) nWidthBytes = nWidth * 2;
else if( nBits == 24 ) nWidthBytes = nWidth * 3;//24位真彩色
else if( nBits == 32 ) nWidthBytes = nWidth * 4;//32位真彩色
//*******四字节对齐*******
while( ( nWidthBytes & 3 ) != 0 ) nWidthBytes++;
//*******四字节对齐*******
return( nWidthBytes );
}
--------------------------------------------------------------------------------------------------------------------------
我的理解:
1.bmp为4字节的方式,因此在buf中,不论存取还是显示,都是以4字节为单位的。
2.接下来的问题就是怎么确定一行到底要怎么对齐。
首先,int 的除法。结果还是int,会舍掉小数点。所以。我们加上3字节。再除以4。就可以防止字节数变少
eg:Width=1(位图的宽度为1像素) BitCount=24(24位的像素位数。1个像素24位即3字节)
Width*BitCount/8=3得出占用的字节数是3.
(3+3)/4 这是求出“基数”,为1. 如果没有加上3的话,商为0,明显不符合题意。
1*4得出LineBytes=4.
同理,如果是以位为单位,就是加上31. Width*BitCount+31
then: (Width*BitCount+31)/32 *4
2、位图中每行象素的数据是连续的,而下一行不能和上一行共一个分配单元(4字节),所以每行象素的数据长度必须是4字节的倍数;
3、代码说明如下:
int WidthBytes( int nBits, int nWidth )
{//nBits为色彩位数, nWidth为每行象素个数
int nWidthBytes;//每行象素的数据长度
nWidthBytes = nWidth;
if( nBits == 1 ) nWidthBytes = ( nWidth + 7 ) / 8;
else if( nBits == 4 ) nWidthBytes = ( nWidth + 1 ) / 2; /*竟然没有8位的。256色不是很常见吗?!*/
else if( nBits == 16 ) nWidthBytes = nWidth * 2;
else if( nBits == 24 ) nWidthBytes = nWidth * 3;//24位真彩色
else if( nBits == 32 ) nWidthBytes = nWidth * 4;//32位真彩色
//*******四字节对齐*******
while( ( nWidthBytes & 3 ) != 0 ) nWidthBytes++;
//*******四字节对齐*******
return( nWidthBytes );
}
--------------------------------------------------------------------------------------------------------------------------
我的理解:
1.bmp为4字节的方式,因此在buf中,不论存取还是显示,都是以4字节为单位的。
2.接下来的问题就是怎么确定一行到底要怎么对齐。
首先,int 的除法。结果还是int,会舍掉小数点。所以。我们加上3字节。再除以4。就可以防止字节数变少
eg:Width=1(位图的宽度为1像素) BitCount=24(24位的像素位数。1个像素24位即3字节)
Width*BitCount/8=3得出占用的字节数是3.
(3+3)/4 这是求出“基数”,为1. 如果没有加上3的话,商为0,明显不符合题意。
1*4得出LineBytes=4.
同理,如果是以位为单位,就是加上31. Width*BitCount+31
then: (Width*BitCount+31)/32 *4
相关文章推荐
- bmp图像的字节对齐问题
- BMP图四字节对齐的问题
- 纠结的图像字节对齐问题
- 字节对齐在24位bmp图像读取中的使用
- LCD驱动以及BMP图四字节对齐的问题
- OpenCV图像数据字节对齐问题
- C++读写BMP文件头部时的字节对齐问题
- 字节内存对齐问题
- struct 中字节对齐问题
- 内存字节对齐问题!
- C语言字节对齐问题详解
- 结构体字节对齐问题
- <C/C++>C语言字节对齐问题总结
- C语言中一个字节对齐问题的分析
- 很奇怪的字节对齐问题
- C语言编程中的字节对齐问题
- DSP中字节/字对齐的问题
- UCOS-II之字节对齐问题 sprintf
- OpenGL保存bmp图片(字节对齐与RGB分量转换)
- VC中结构体内存分配问题透析(“字节对齐”访问数据)