📄 chap01.htm
字号:
宋体;"Times New Roman"'>三个分量表示,它又叫做</span><span
lang=EN-US>24</span><span style='font-family:宋体;
"Times New Roman"'>位色图。</span></p>
<h2><a name="_Toc486331865"></a><a
name="_Toc486332865"> <span
lang=EN-US>1.2</span> <span lang=EN-US> </span></a><a
name="_Toc486338974"></a><a name="_Toc454810839"></a><a name="_Toc454856613"><span><span>bmp</span></span></a><span><span><span style='font-family:黑体;'>文件格式</span></span></span></h2>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>介绍完位图和调色板的概念,下面就让我们来看一看</span><span lang=EN-US>Windows</span><span
style='font-family:宋体;"Times New Roman"'>的位图文件</span><span lang=EN-US>(.bmp</span><span
style='font-family:宋体;"Times New Roman"'>文件</span><span lang=EN-US>)</span><span style='font-family:
宋体;"Times New Roman"'>的格式是什么样子的。</span></p>
<p style='line-height:18.0pt'><span
lang=EN-US>bmp</span><span style='font-family:宋体;
"Times New Roman"'>文件大体上分成四个部分,如图</span><span lang=EN-US>1.3</span><span
style='font-family:宋体;"Times New Roman"'>所示。</span></p>
<div align=center>
<table border=1 cellspacing=0 cellpadding=0 width="80%">
<tr>
<td width=455 valign=top class="Normal">
<p align=center style='text-align:center;line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>位图文件头</span><span lang=EN-US>BITMAPFILEHEADER</span></p>
</td>
</tr>
<tr>
<td width=455 valign=top class="Normal">
<p align=center style='text-align:center;line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>位图信息头</span><span lang=EN-US>BITMAPINFOHEADER</span></p>
</td>
</tr>
<tr>
<td width=455 valign=top class="Normal">
<p align=center style='text-align:center;line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>调色板</span><span lang=EN-US>Palette</span></p>
</td>
</tr>
<tr>
<td width=455 valign=top class="Normal">
<p align=center style='text-align:center;line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>实际的位图数据</span><span lang=EN-US>ImageDate</span></p>
</td>
</tr>
</table>
</div>
<p align=center style='text-align:center;line-height:18.0pt'><b><span
style='font-family:宋体;"Times New Roman"'>图</span>1.3 Windows</b><b><span
style='font-family:宋体;"Times New Roman"'>位图文件结构示意图</span><span lang=EN-US></span></b></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>第一部分为位图文件头</span><b><span lang=EN-US>BITMAPFILEHEADER</span></b><span
style='font-family:宋体;"Times New Roman"'>,是一个结构,其定义如下:</span></p>
<p style='line-height:18.0pt'><span lang=EN-US>typedef struct tagBITMAPFILEHEADER
{</span></p>
<p style='line-height:18.0pt'>WORD
bfType; </p>
<p style='line-height:18.0pt'>DWORD bfSize; </p>
<p style='line-height:18.0pt'>WORD
bfReserved1; </p>
<p style='line-height:18.0pt'>WORD
bfReserved2; </p>
<p style='line-height:18.0pt'>DWORD bfOffBits; </p>
<p style='line-height:18.0pt'><span lang=EN-US>} BITMAPFILEHEADER; </span><span lang=EN-US style='font-size:9.0pt;'></span></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>这个结构的长度是固定的,为</span><span lang=EN-US>14</span><span
style='font-family:宋体;"Times New Roman"'>个字节</span><span lang=EN-US>(WORD</span><span
style='font-family:宋体;"Times New Roman"'>为无符号</span><span lang=EN-US>16</span><span style='font-family:
宋体;"Times New Roman"'>位整数,</span><span
lang=EN-US>DWORD</span><span style='font-family:宋体;
"Times New Roman"'>为无符号</span><span lang=EN-US>32</span><span
style='font-family:宋体;"Times New Roman"'>位整数</span><span lang=EN-US>)</span><span style='font-family:
宋体;"Times New Roman"'>,各个域的说明如下:</span></p>
<p style='line-height:18.0pt'><b><span
lang=EN-US>bfType</span></b></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>指定文件类型,必须是</span><span lang=EN-US>0x424D</span><span
style='font-family:宋体;"Times New Roman"'>,即字符串“</span><span lang=EN-US>BM</span><span
style='font-family:宋体;"Times New Roman"'>”,也就是说所有</span><span lang=EN-US>.bmp</span><span
style='font-family:宋体;"Times New Roman"'>文件的头两个字节都是“</span><span lang=EN-US>BM</span><span
style='font-family:宋体;"Times New Roman"'>”。</span></p>
<p style='line-height:18.0pt'><b><span
lang=EN-US>bfSize</span></b></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>指定文件大小,包括这</span><span lang=EN-US>14</span><span
style='font-family:宋体;"Times New Roman"'>个字节。</span></p>
<p style='line-height:18.0pt'><b><span
lang=EN-US>bfReserved1</span></b><b><span style='font-family:宋体;"Times New Roman";"Times New Roman"'>,</span>bfReserved2
</b></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>为保留字,不用考虑</span></p>
<p style='line-height:18.0pt'><b><span
lang=EN-US>bfOffBits</span></b></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>为从文件头到实际的位图数据的偏移字节数,即图</span><span lang=EN-US>1.3</span><span
style='font-family:宋体;"Times New Roman"'>中前三个部分的长度之和。</span></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>第二部分为位图信息头</span><b><span lang=EN-US>BITMAPINFOHEADER</span></b><span
style='font-family:宋体;"Times New Roman"'>,也是一个结构,其定义如下:</span></p>
<p style='line-height:18.0pt'><span
lang=EN-US>typedef struct tagBITMAPINFOHEADER{</span></p>
<p style='line-height:18.0pt'>DWORD biSize; </p>
<p style='line-height:18.0pt'>LONG
biWidth; </p>
<p style='line-height:18.0pt'>LONG
biHeight; </p>
<p style='line-height:18.0pt'>WORD
biPlanes; </p>
<p style='line-height:18.0pt'>WORD
biBitCount </p>
<p style='line-height:18.0pt'>DWORD biCompression; </p>
<p style='line-height:18.0pt'>DWORD biSizeImage; </p>
<p style='line-height:18.0pt'>LONG
biXPelsPerMeter; </p>
<p style='line-height:18.0pt'>LONG
biYPelsPerMeter; </p>
<p style='line-height:18.0pt'>DWORD biClrUsed; </p>
<p style='line-height:18.0pt'>DWORD biClrImportant; </p>
<p style='line-height:18.0pt'><span
lang=EN-US>} BITMAPINFOHEADER; </span></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>这个结构的长度是固定的,为</span><span lang=EN-US>40</span><span
style='font-family:宋体;"Times New Roman"'>个字节</span><span lang=EN-US>(LONG</span><span
style='font-family:宋体;"Times New Roman"'>为</span><span lang=EN-US>32</span><span style='font-family:
宋体;"Times New Roman"'>位整数</span><span
lang=EN-US>)</span><span style='font-family:宋体;
"Times New Roman"'>,各个域的说明如下:</span></p>
<p style='line-height:18.0pt'><b><span
lang=EN-US>biSize</span></b></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>指定这个结构的长度,为</span><span lang=EN-US>40</span><span
style='font-family:宋体;"Times New Roman"'>。</span></p>
<p style='line-height:18.0pt'><b><span
lang=EN-US>biWidth</span></b></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>指定图象的宽度,单位是象素。</span></p>
<p style='line-height:18.0pt'><b><span
lang=EN-US>biHeight</span></b></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>指定图象的高度,单位是象素。</span></p>
<p style='line-height:18.0pt'><b><span
lang=EN-US>biPlanes</span></b></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>必须是</span><span lang=EN-US>1</span><span style='font-family:
宋体;"Times New Roman"'>,不用考虑。</span></p>
<p style='line-height:18.0pt'><b><span
lang=EN-US>biBitCount </span></b></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>指定表示颜色时要用到的位数,常用的值为</span><span lang=EN-US>1(</span><span
style='font-family:宋体;"Times New Roman"'>黑白二色图</span><span lang=EN-US>), 4(16</span><span
style='font-family:宋体;"Times New Roman"'>色图</span><span lang=EN-US>), 8(256</span><span
style='font-family:宋体;"Times New Roman"'>色</span><span lang=EN-US>), 24(</span><span
style='font-family:宋体;"Times New Roman"'>真彩色图</span><span lang=EN-US>)(</span><span style='font-family:
宋体;"Times New Roman"'>新的</span><span
lang=EN-US>.bmp</span><span style='font-family:宋体;
"Times New Roman"'>格式支持</span><span lang=EN-US>32</span><span
style='font-family:宋体;"Times New Roman"'>位色,这里就不做讨论了</span><span lang=EN-US>)</span><span
style='font-family:宋体;"Times New Roman"'>。</span></p>
<p style='line-height:18.0pt'><b><span
lang=EN-US>biCompression</span></b></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>指定位图是否压缩,有效的值为</span><span lang=EN-US>BI_RGB</span><span
style='font-family:宋体;"Times New Roman"'>,</span><span lang=EN-US>BI_RLE8</span><span
style='font-family:宋体;"Times New Roman"'>,</span><span lang=EN-US>BI_RLE4</span><span
style='font-family:宋体;"Times New Roman"'>,</span><span lang=EN-US>BI_BITFIELDS(</span><span
style='font-family:宋体;"Times New Roman"'>都是一些</span><span lang=EN-US>Windows</span><span
style='font-family:宋体;"Times New Roman"'>定义好的常量</span><span lang=EN-US>)</span><span
style='font-family:宋体;"Times New Roman"'>。要说明的是,</span><span lang=EN-US>Windows</span><span
style='font-family:宋体;"Times New Roman"'>位图可以采用</span><span lang=EN-US>RLE4</span><span
style='font-family:宋体;"Times New Roman"'>,和</span><span lang=EN-US>RLE8</span><span style='font-family:
宋体;"Times New Roman"'>的压缩格式,但用的不多。我们今后所讨论的只有第一种不压缩的情况,即</span><span
lang=EN-US>biCompression</span><span style='font-family:宋体;"Times New Roman";"Times New Roman"'>为</span><span
lang=EN-US>BI_RGB</span><span style='font-family:宋体;
"Times New Roman"'>的情况。</span></p>
<p style='line-height:18.0pt'><b><span
lang=EN-US>biSizeImage</span></b></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>指定实际的位图数据占用的字节数,其实也可以从以下的公式中计算出来:</span></p>
<p class=a style='line-height:18.0pt'><span lang=EN-US>biSizeImage=biWidth’
× biHeight</span></p>
<p style='line-height:18.0pt'><span
style='font-family:宋体;"Times New Roman"'>要注意的是:上述公式中的</span><span lang=EN-US>biWidth’</span><span
style='font-family:宋体;"Times New Roman"'>必须是</span><span lang=EN-US>4</span><span style='font-family:
宋体;"Times New Roman"'>的整倍数</span><span
lang=EN-US>(</span><span style='font-family:宋体;
"Times New Roman"'>所以不是</span><span lang=EN-US>biWidth</span><span
style='font-family:宋体;"Times New Roman"'>,而是</span><span lang=EN-US>biWidth’</span><span
style='font-family:宋体;"Times New Roman"'>,表示大于或等于</span><span lang=EN-US>biWidth</span><span
style='font-family:宋体;"Times New Roman"'>的,最接近</span><span lang=EN-US>4</span><span style='font-family:
宋体;"Times New Roman"'>的整倍数。举个例子,如果</span><span
lang=EN-US>biWidth=240</span><span style='font-family:宋体;"Times New Roman";"Times New Roman"'>,则</span><span
lang=EN-US>biWidth’=240</span><span style='font-family:宋体;"Times New Roman";"Times New Roman"'>;如果</span><span
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -