BMP FILE
BMP檔案是針對Windows 3.0或之後的DIB( Device Independent Bitmap)所制定的標準。 作者來介紹這個標準的格式欄位(Format Field)。基本格式包含四個部份: 轉載至:Basic File Format
|
Name |
Size |
Description |
||
|
Header |
14
bytes |
Windows
Structure: BITMAPFILEHEADER |
||
|
|
Signature |
2
bytes |
'BM' |
|
|
FileSize |
4
bytes |
File
size in bytes |
||
|
reserved |
4
bytes |
unused
(=0) |
||
|
DataOffset |
4
bytes |
File
offset to Raster Data |
||
|
InfoHeader |
40
bytes |
Windows
Structure: BITMAPINFOHEADER |
||
|
|
Size |
4
bytes |
Size
of InfoHeader =40 |
|
|
Width |
4
bytes |
Bitmap
Width |
||
|
Height |
4
bytes |
Bitmap
Height |
||
|
Planes |
2
bytes |
Number
of Planes (=1) |
||
|
BitCount |
2
bytes |
Bits
per Pixel |
||
|
Compression |
4
bytes |
Type
of Compression |
||
|
ImageSize |
4
bytes
|
(compressed)
Size of Image |
||
|
XpixelsPerM |
4
bytes |
horizontal
resolution: Pixels/meter |
||
|
YpixelsPerM |
4
bytes |
vertical
resolution: Pixels/meter |
||
|
ColorsUsed |
4
bytes |
Number
of actually used colors |
||
|
ColorsImportant |
4
bytes |
Number
of important colors |
||
|
ColorTable |
4
* NumColors bytes |
present
only if Info.BitsPerPixel <= 8 |
||
|
|
|
Blue |
1
byte |
Blue
intensity |
|
Green |
1
byte |
Green
intensity |
||
|
Red |
1
byte |
Red
intensity |
||
|
reserved |
1
byte |
unused
(=0) |
||
|
repeated
NumColors times |
||||
|
Raster
Data |
Info.ImageSize
bytes |
The
pixel data |
||
註:以上紅色部份,原本的資料有誤,作者加以更正。 以上是一個標準格式,提供查詢,以下提供一張圖,及它實際的16進制值。
Note:這張圖寬為8 pixels,高為6 pixels(第一列為黑色;第二、三列為藍色;第四、五、六列為紅色) 儲存格式為16色。
42 4D 8E 00 00 00 00 00 00 00 76 00 00 00 28 00 00 00 08 00 00 00 06 00 00 00 01 00 04 00 00 00 00 00 18 00 00 00 C4 0E 00 00 C4 0E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 00 00 80 00 00 00 80 80 00 80 00 00 00 80 00 80 00 80 80 00 00 80 80 80 00 C0 C0 C0 00 00 00 FF 00 00 FF 00 00 00 FF FF 00 FF 00 00 00 FF 00 FF 00 FF FF 00 00 FF FF FF 00 99 99 99 99 99 99 99 99 99 99 99 99 CC CC CC CC CC CC CC CC 00 00 00 00
事前的準備完畢之後,開始介紹標準欄位,介紹完畢之後,會以一個C語言的範例程式來讀取BMP檔。
1.Header : 14 bytes
•Signature(簽名,2 bytes): 42 4D ==>'BM'
•FileSize(檔案大小,4 bytes)):8E 00 00 00 ==> 0X0000008E ==> 142 bytes
•Reserved(4 bytes):00 00 00 00 ==> 沒有使用
•DataOffset(資料偏移,4 bytes):76 00 00 00 ==> 0x00000076 ==>118
DataOffset意指Raster Data的Offset是多少,不清楚的話,在之後會在介紹。
2.InfoHeader : 40 bytes
•Size(4 bytes):28 00 00 00 ==> 0x00000028 ==> 40 bytes
InfoHeader的大小是多少,在Window是40,在OS/2是64
•Width(圖片寬度,4 bytes):08 00 00 00 ==> 0x00000008 ==> 8 pixels
•Height(圖片高度,4 bytes):06 00 00 00 ==> 0x00000006 ==> 6 pixels
•Planes(plane的數目,2 bytes):01 00 ==> 0x0001 ==> 1
•BitCount(每個pixel幾個bits,2 bytes) : 04 00 ==> 0x0004 ==> 4
每4 bits表示一個pixel。另外4 bits可以有16種數值表示,NumColors即為16
•Compression(壓縮,4 bytes):00 00 00 00 ==> 0x00000000 ==> 0
•ImageSize(圖片大小):18 00 00 00 ==> 0x00000018 ==> 24 bytes ==> Raster Data的大小
•XpixelsPerM(Pixels/meter,水平解析度,4 bytes):CE 0E 00 00 ==> 0X00000ECE
•YpixelsPerM(Pixels/meter,垂直解析度,4 bytes):CE 0E 00 00 ==> 0X00000ECE
•ColorsUsed(使用的Color數目,4 bytes):00 00 00 00 ==> 0
•ColorsImportant(4 bytes): 00 00 00 00 ==> 0 ( 0 ==> all )
3.ColorTable(又稱為ColorMap): 重覆 NumColors(16)次 •Red (Red intensity,1 byte) •Green (Green intensity,1 byte) •Blue (Blue intensity,1 byte) •reserved (unused,1 byte)
16 MS-Windows standard colors table
| Red intensity | Green intensity | Blue intensity | color |
name |
|
| 0 | 00 | 00 | 00 | black | |
| 1 | 80 | 00 | 00 | maroon | |
| 2 | 00 | 80 | 00 | green | |
| 3 | 80 | 80 | 00 | olive | |
| 4 | 00 | 00 | 80 | navy | |
| 5 | 80 | 00 | 80 | purple | |
| 6 | 00 | 80 | 80 | teal | |
| 7 | 80 | 80 | 80 | gray | |
| 8 | C0 | C0 | C0 | silver | |
| 9 | FF | 00 | 00 | red | |
| A | 00 | FF | 00 | lime | |
| B | FF | FF | 00 | yellow | |
| C | 00 | 00 | FF | blue | |
| D | FF | 00 | FF | fuchsia | |
| E | 00 | FF | FF | cyan | |
| F | FF | FF | FF | white |
4.Raster Data(點線數據;大陸翻成"柵格數據") 以下資料節錄至:PDF 印 刷 數 據 交 換 標 準
Raster data 點 線 數 據
一 行 密 排 的 小 點 組 成 線 條 , 一 連 串 的 密 排 線
條 組 成 整 個 頁 面 ;
點 線 通 常 從 左 上 角 開 始 逐 漸 移 向 右 下 角 。
組 成 圖 像 的 個 別 元 素 ( 即 每 一 點 , 亦 即 像 素 )
用 數 值 代 表 , 顯 示 色 量 。
利用所得的數值(Raster data)去映射(Mapping)一個table,得到各個色量。
Raster Data → Color Table (Color Map) → R, G, B intensity
本文章所提供的圖片,共6pixels高,第一列為黑色;第二、三列為藍色;第四、五、六列為紅色
但儲存的方式是由下而上(bottom-up),由左而右(left-right),所得到的數值是:
99 99 99 99 99 99 99 99 99 99 99 99 CC CC CC CC CC CC CC CC 00 00 00 00 每個pixels佔4bits。 從第六、五、四、...二、一列儲起。9對應到上面的表格即為紅色,C:藍色,0:黑色。 接下來以一個程式來讀取bmp檔。
#include<stdio.h>
#include<stdlib.h>
typedef long INT32;
typedef unsigned short int INT16;
typedef unsigned char U_CHAR;
#define UCH(x) ((int) (x))
#define GET_2B(array,offset) ((INT16) UCH(array[offset]) + \
(((INT16) UCH(array[offset+1])) << 8))
#define GET_4B(array,offset) ((INT32) UCH(array[offset]) + \
(((INT32) UCH(array[offset+1])) << 8) + \
(((INT32) UCH(array[offset+2])) << 16) + \
(((INT32) UCH(array[offset+3])) << 24))
#define FREAD(file,buf,sizeofbuf) \
((size_t) fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
int main()
{
FILE *input_file = 0 ;
U_CHAR bmpfileheader[14] = { 0 } ;
U_CHAR bmpinfoheader[40] = { 0 } ;
INT32 FileSize = 0 ;
INT32 bfOffBits =0 ;
INT32 headerSize =0 ;
INT32 biWidth = 0 ;
INT32 biHeight = 0 ;
INT16 biPlanes = 0 ;
INT16 BitCount = 0 ;
INT32 biCompression = 0 ;
INT32 biImageSize = 0;
INT32 biXPelsPerMeter = 0 ,biYPelsPerMeter = 0 ;
INT32 biClrUsed = 0 ;
INT32 biClrImp = 0 ;
/* 假設以上提供的檔案放置在C:\example.bmp */
if( ( input_file = fopen("C:\\example.bmp","rb") ) == NULL ){
fprintf(stderr,"File can't open.\n");
exit(0);
}
FREAD(input_file,bmpfileheader,14);
FREAD(input_file,bmpinfoheader,40);
if (GET_2B(bmpfileheader,0) == 0x4D42) /* 'BM' */
fprintf(stdout,"BMP file.\n");
else{
fprintf(stdout,"Not bmp file.\n");
exit(0);
}
FileSize = GET_4B(bmpfileheader,2);
bfOffBits = GET_4B(bmpfileheader,10);
headerSize = GET_4B(bmpinfoheader,0);
biWidth = GET_4B(bmpinfoheader,4);
biHeight = GET_4B(bmpinfoheader,8);
biPlanes = GET_2B(bmpinfoheader,12);
BitCount = GET_2B(bmpinfoheader,14);
biCompression = GET_4B(bmpinfoheader,16);
biImageSize = GET_4B(bmpinfoheader,20);
biXPelsPerMeter = GET_4B(bmpinfoheader,24);
biYPelsPerMeter = GET_4B(bmpinfoheader,28);
biClrUsed = GET_4B(bmpinfoheader,32);
biClrImp = GET_4B(bmpinfoheader,36);
printf("FileSize = %ld \n"
"DataOffset = %ld \n"
"HeaderSize = %ld \n"
"Width = %ld \n"
"Height = %ld \n"
"Planes = %d \n"
"BitCount = %d \n"
"Compression = %ld \n"
"ImageSize = %ld \n"
"XpixelsPerM = %ld \n"
"YpixelsPerM = %ld \n"
"ColorsUsed = %ld \n"
"ColorsImportant = %ld \n",FileSize,bfOffBits,headerSize,biWidth,biHeight,biPlanes,
BitCount,biCompression,biImageSize,biXPelsPerMeter,biYPelsPerMeter,biClrUsed,biClrImp);
fclose (input_file);
return 0;
}
相關文章: The .bmp file format DaubNET File Formats Collection BMP
Written By James On 2004/02/29