1. BMP file composition
The BMP file consists of a file header, bitmap information head, color information, and graphical data.
2. BMP file header
The BMP file header data contains information such as the type, file size and bitmap start positions of the BMP file.
Its structure is defined as follows:
Typedef struct tagbitmapfileheader
{
WordbfType; // Bitmap file type, must be BM
DWORD BFSIZE; // Bitmap file size, byte
Wordbfreserved1; // bitmap file reserved word, must be 0
Wordbfreserved2; // bitmap file reserved word, must be 0
DWORD BFOFFBITS; // Bit map data starting position to relatively
// The offset of the file header is indicated, in bytes
} BitmapfileHeader;
3. Bitmap information head
BMP bitmap information header data is used to explain the size of the bitmap.
Typedef struct tagbitmapinfoheader {
DWORD BISIZE; / / This structure is occupied by the number of bytes
Longbiwidth; // Bit map width, in pixels
Longbiheight; // bit map height, in pixels
Word biplanes; // The level of target device must be 1
Word Bibitcount // The number of bits required for each pixel must be 1 (two-color),
// 4 (16 colors), 8 (256 colors) or 24 (true color)
DWORD BICOMPRESSION; // bitmap compression type, must be 0 (not compressed),
// 1 (Bi_RLE8 compression type) or 2 (Bi_RLE4 compression type)
DWORD BisizeImage; // Bit map size, byte
Longbixpelspermeter; // bitmap level resolution, number of pixels per meter
Longbiypelspermeter; // bitmap vertical resolution, number of pixels per meter
DWORD biclrused; // number of color tables in the color table actually used by the bitmap
DWORD BICLRIMPORTANT; // Bitmap display Direct number
} BitmapInfoHeader;
4. Color table
The color table is used to explain the color in the bitmap, which has several entries, each of which is a RGBQUAD type structure, defining a color. The RGBQUAD structure is defined as follows:
Typedef struct tagrgbquad {
BytergbBlue; // Blue brightness (value range is 0-255)
Bytergbgreen; // Green brightness (value range is 0-255)
Bytergbred; // red brightness (value range is 0-255)
Bytergbreserved; // Reserved, must be 0
} RGBQUAD;
The number of RGBQUAD structure data in color table has BIBITCOUNT to determine:
When BiBitCount = 1, 4, 8, there are 2,16,256 items, respectively;
When BiBitCount = 24, there is no color entry.
The bitmap information head and color table form bitmap information, the BitmapInfo structure is defined as follows:
Typedef struct tagbitmapinfo {
BitmapInfoHeader Bmiheader; // bitmap information head
RGBQUAD BMICOLORS [1]; // Color table
} BitmapInfo;
5. Bitmap data
The bitmap data records each pixel value of the bitmap, the recording order is from left to right, and the scan line is from bottom to top. The number of bytes of a pixel value of the bitmap:
When BIBITCOUNT = 1, 8 pixels account for 1 byte;
When BIBITCOUNT = 4, 2 pixels account for 1 byte;
When BiBITCount = 8, 1 pixel occupies 1 byte;
When BiBitCount = 24, one pixel accounts for 3 bytes; Windows specifies the number of bytes occupied by a scan line.
4 times (ie, in line), not enough 0 fill,
A scan line accounts for the number of bytes:
DataSizeperline = (BiWidth * BibitCount 31) / 8;
// A number of bytes occupied by a scan line
DataSizeperline = DataSizePerLine / 4 * 4; // The number of bytes must be a multiple of 4
The size of bitmap data (no compression):
DataSize = DataSizePerline * Biheight;