10. Analysis of bitmap format

Speaking of pictures, bitmap ( Bitmap ) is of course the simplest, it is the basic format for Windows to display pictures, and its file extension is *.BMP . Under Windows , any kind of picture files (including video playback) must be converted into bitmaps before they can be displayed. Picture files in various formats are also generated by different compression algorithms based on the bitmap format. ( Appropriate amount of images are used in Flash , which are stored in the same color area).
1. Let's take a look at the format of the bitmap file ( *.BMP ).
Bitmap files are mainly divided into the following three parts:
block name
Corresponding to the Windows structure definition
Large and small ( Byte )
file header
BITMAPFILEHEADER
14
bitmap header
BITMAPINFOHEADER
40
RGB color array
BYTE*
Determined by the image length and width
1.    File information header BITMAPFILEHEADER
The structure is defined as follows:
typedef struct tagBITMAPFILEHEADER { /* bmfh */
UINT bfType;  
DWORD bfSize; 
UINT bfReserved1; 
UINT bfReserved2; 
DWORD bfOffBits;
} BITMAPFILEHEADER;
in:
bfType
Specifies the type of file, the value must be 0x4D42, which is the character 'BM'.
bfSize
Indicates the size of the bitmap file, in bytes
bfReserved1
Reserved, must be set to 0
bfReserved2
Reserved, must be set to 0
bfOffBits
Specifies the byte offset from the beginning of the file header to the actual image data. This parameter is very useful, because the length of the bitmap header and palette will vary depending on the situation, so you can use this offset value to quickly read bit data from the file.
2. Bitmap information header BITMAPINFOHEADER
The structure is defined as follows:
typedef struct tagBITMAPINFOHEADER { /* bmih */
DWORD biSize; 
LONG biWidth; 
LONG biHeight; 
WORD biPlanes; 
WORD biBitCount; 
DWORD biCompression; 
DWORD biSizeImage; 
LONG biXPelsPerMeter; 
LONG biYPelsPerMeter; 
DWORD biClrUsed; 
DWORD biClrImportant;
} BITMAPINFOHEADER;
in:
biSize
Specifies the number of words required for the BITMAPINFOHEADER structure.
biWidth
Specifies the width of the image, in pixels.
biHeight
说明图象的高度,以象素为单位。注:这个值除了用于描述图像的高度之外,它还有另一个用处,就是指明该图像是倒向的位图,还是正向的位图。如果该值是一个正数,说明图像是倒向的,如果该值是一个负数,则说明图像是正向的。大多数的BMP文件都是倒向的位图,也就是时,高度值是一个正数。
biPlanes
为目标设备说明位面数,其值将总是被设为1。
biBitCount
说明比特数/象素,其值为1、4、8、16、24、或32。但是由于我们平时用到的图像绝大部分是24位和32位的,所以我们讨论这两类图像。
biCompression
说明图象数据压缩的类型,同样我们只讨论没有压缩的类型:BI_RGB
biSizeImage
说明图象的大小,以字节为单位。当用BI_RGB格式时,可设置为0
biXPelsPerMeter
说明水平分辨率,用象素/米表示。
biYPelsPerMeter
说明垂直分辨率,用象素/米表示。
biClrUsed
说明位图实际使用的彩色表中的颜色索引数(设为0的话,则说明使用所有调色板项)。
biClrImportant
说明对图象显示有重要影响的颜色索引的数目,如果是0,表示都重要。
3、RGB颜色阵列
有关RGB三色空间我想大家都很熟悉,这里我想说的是在Windows下,RGB颜色阵列存储的格式其实BGR。也就是说,对于24位的RGB位图像素数据格式是:
蓝色B
绿色G
红色R
对于32位的RGB位图像素数据格式是:
蓝色B
绿色G
红色R
透明通道A
透明通道也称Alpha通道,该值是该像素点的透明属性,取值在0(全透明)到255(不透明)之间。对于24位的图像来说,因为没有Alpha通道,故整个图像都不透明。
二、搞清了文件格式,下一步我们要实现加载。
            加载文件的目的是要得到图片属性,以及RGB数据,然后可以将其绘制在DC(GDI),或是生成纹理对象(3D:OpenGL/Direct3D)。这两种用途在数据处理上有点区别,我们主要按前一种用法讲,在和3D有不同的地方,我们再提出来。
1、加载文件头
            //Load the file header
            BITMAPFILEHEADER header;
            memset(&header, 0, sizeof(header));
            inf.read((char*)&header, sizeof(header));
            if(header.bfType != 0x4D42)
                        return false;
            这个很简单,没有什么好说的。
            2、加载位图信息头
            //Load the image information header
            BITMAPINFOHEADER infoheader;
            memset(&infoheader, 0, sizeof(infoheader));
            inf.read((char*)&infoheader, sizeof(infoheader));
            m_iImageWidth = infoheader.biWidth;
            m_iImageHeight = infoheader.biHeight;
            m_iBitsPerPixel = infoheader.biBitCount;
            这里我们得到了3各重要的图形属性:宽,高,以及每个像素颜色所占用的位数。
3、行对齐
由于Windows在进行行扫描的时候最小的单位为4个字节,所以当
图片宽 X 每个像素的字节数 = 4的整数倍
时要在每行的后面补上缺少的字节,以0填充(一般来说当图像宽度为2的幂时不需要对齐)。位图文件里的数据在写入的时候已经进行了行对齐,也就是说加载的时候不需要再做行对齐。但是这样一来图片数据的长度就不是:宽 X  X 每个像素的字节数  了,我们需要通过下面的方法计算正确的数据长度:
//Calculate the image data size
int iLineByteCnt = (((m_iImageWidth*m_iBitsPerPixel) + 31) >> 5) << 2;
m_iImageDataSize = iLineByteCnt * m_iImageHeight;
4、加载图片数据
对于24位和32位的位图文件,位图数据的偏移量为sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER),也就是说现在我们可以直接读取图像数据了。
            if(m_pImageData) delete []m_pImageData;
            m_pImageData = new unsigned char[m_iImageDataSize];
            inf.read((char*)m_pImageData, m_iImageDataSize);
如果你足够细心,就会发现内存m_pImageData里的数据的确是BGR格式,可以用个纯蓝色或者是纯红色的图片测试一下。
5、绘制
好了,数据和属性我们都有了,现在就可以拿来随便用了,就和吃馒头一样,爱粘白糖粘白糖,爱粘红糖粘红糖。下面是我的GDI绘制代码,仅作参考。
void CImage::DrawImage(HDC hdc, int iLeft, int iTop, int iWidth, int iHeight)
{
            if(!hdc || m_pImageData == NULL)
                        return;
            BITMAPINFO bmi;
            memset(&bmi, 0, sizeof(bmi));
            bmi.bmiHeader.biSize = sizeof(BITMAPINFO);
            bmi.bmiHeader.biWidth = m_iImageWidth;
            bmi.bmiHeader.biHeight = m_iImageHeight;
            bmi.bmiHeader.biPlanes = 1;
            bmi.bmiHeader.biBitCount = m_iBitsPerPixel;
            bmi.bmiHeader.biCompression = BI_RGB;
            bmi.bmiHeader.biSizeImage = m_iImageDataSize;
            StretchDIBits(hdc, iLeft, iTop, iWidth, iHeight,
                                                0, 0, m_iImageWidth, m_iImageHeight,
                                                m_pImageData, &bmi, DIB_RGB_COLORS, SRCCOPY);
}
6、3D(OpenGL)的不同之处
如果你是想用刚才我们得到的数据生成纹理对象,那么你还要请出下面的问题。
首先,用来生成纹理的数据不需要对齐,也就是说不能在每行的后面加上对齐的字节。当然在OpenGL里要求纹理图片的尺寸为2的幂,所以这个问题实际上不存在;
其次,我们得到的图形数据格式是BGR(BGRA),所以在生成纹理的时候,需指定格式为GL_BGR_EXT(GL_BGRA_EXT);否则需要做BGR->RGB(BGRA->RGBA)的转化。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324497016&siteId=291194637