1. BMP file Composition
A bmp file consists of four parts: File Header, bitmap information header, color information, and graphic data.
2. BMP File Header
The data structure of the BMP file header contains information such as the type, size, and start position of the BMP file.
Its structure is defined as follows:
Typedef struct tagbitmapfileheader
{
Wordbftype; // The type of the bitmap file, which must be BM.
DWORD bfsize; // the size of the bitmap file, in bytes.
Wordbfreserved1; // reserved word for bitmap files, which must be 0
Wordbfreserved2; // reserved word for bitmap files, which must be 0
DWORD bfoffbits; // the starting position of the bitmap data, relative to the bitmap
// The offset of the file header, in bytes
} Bitmapfileheader;
3. Bitmap header
The BMP Bitmap header is used to describe the size and other information of the bitmap.
Typedef struct tagbitmapinfoheader {
DWORD bisize; // number of bytes occupied by the structure
Longbiwidth; // The width of the bitmap, in pixels.
Longbiheight; // The height of the bitmap, in pixels
Word biplanes; // The level of the target device, which must be 1
Word bibitcount // The number of digits required for each pixel, which must be 1 (two-color ),
// 4 (16 colors), 8 (256 colors), or 24 (true color)
DWORD bicompression; // bitmap compression type, which must be 0 (not compressed ),
// 1 (bi_rle8 compression type) or 2 (bi_rle4 compression type)
DWORD bisizeimage; // bitmap size, in bytes
Longbixpelspermeter; // horizontal bitmap resolution, number of workers per meter
Longbiypelspermeter; // bitmap vertical resolution, number of workers per meter
DWORD biclrused; // number of colors in the color table used by the bitmap
DWORD biclrimportant; // number of important colors in the bitmap display process
} Bitmapinfoheader;
4. Color Table
A color table is used to describe the color in a bitmap. It has several table items. Each table item is an rgbquad-type structure and defines a color. The rgbquad structure is defined as follows:
Typedef struct tagrgbquad {
Bytergbblue; // blue brightness (value range: 0-255)
Bytergbgreen; // The brightness of the green color (value range: 0-255)
Bytergbred; // The Red brightness (value range: 0-255)
Bytergbreserved; // reserved, must be 0
} Rgbquad;
The number of rgbquad structure data in the color table is determined by bibitcount:
When bibitcount is 1, 4, and 8, there are 2, 16, and 256 table items respectively;
When bibitcount = 24, there is no color table item.
The bitmap information header and the color table form bitmap information. The bitmapinfo structure is defined as follows:
Typedef struct tagbitmapinfo {
Bitmapinfoheader bmiheader; // Bitmap header
Rgbquad bmicolors [1]; // color table
} Bitmapinfo;
5. bitmap data
The bitmap data records each pixel value of the bitmap. The record sequence is from left to right in the scan row, and the rows are from bottom to top. The number of bytes occupied by a pixel value of a bitmap:
When bibitcount = 1, 8 pixels constitute 1 byte;
When bibitcount = 4, 2 pixels constitute 1 byte;
When bibitcount = 8, 1 pixel occupies 1 byte;
When bibitcount = 24, one pixel occupies three bytes;
Windows requires that the number of bytes occupied by a scan row must be
A multiple of 4 (in the unit of long). The insufficient values are filled with 0,
Calculation of the number of bytes occupied by a scanned row:
Datasizeperline = (biwidth * bibitcount + 31)/8;
// The number of bytes that a scan row occupies
Datasizeperline = datasizeperline/4*4; // The number of bytes must be a multiple of 4
Bitmap data size (without compression ):
Datasize = datasizeperline * biheight;
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1516523