最近学习雷神的博客, RGB24转换成BMP位图, 发现一些问题与心得, 记录下来
1.位图知识:(会列出两篇比较好的博文, 这里简单总结一下)
位图结构:
位图文件头(14字节)
位图信息头(40字节)
彩色表(RGB24真彩不存在彩色表)
位图数据(实际上就是RGB数据)
【注意1】:位图文件头与位图信息头一共是54字节, 雷神在博文里写的结构体不正确, 正确的结构体如下:
贴图中的位图文件头为12字节, 因为有两个字节的BM用字符数组表示了, 详见最后的完整代码
/*12Bytes*/
typedef struct /**** BMP file header structure ****/
{
unsigned int bfSize; /* Size of file */
unsigned short bfReserved1; /* Reserved */
unsigned short bfReserved2; /* ... */
unsigned int bfOffBits; /* Offset to bitmap data */
}BITMAPFILEHEADER;
/*40Bytes*/
typedef struct /**** BMP file info structure ****/
{
unsigned int biSize; /* Size of info header */
int biWidth; /* Width of image */
int biHeight; /* Height of image */
unsigned short biPlanes; /* Number of color planes */
unsigned short biBitCount; /* Number of bits per pixel */
unsigned int biCompression; /* Type of compression to use */
unsigned int biSizeImage; /* Size of image data */
int biXPelsPerMeter; /* X pixels per meter */
int biYPelsPerMeter; /* Y pixels per meter */
unsigned int biClrUsed; /* Number of colors used */
unsigned int biClrImportant; /* Number of important colors */
}BITMAPINFOHEADER;
【注意2】:BMP的存储方式是小端存储, 即低字节放低地址, 高字节放高地址
关于大小端存储, 我一直有误区, 我错误理解为"地位放在低地址, 高位放在高地址",这是不对的, 应该是将整个低字节的内容都存储到低地址去
例如内存中存储0x12345678的小端