BMP image format description

Source: Internet
Author: User
Tags bmp image

Http://freeze001.blog.163.com/blog/static/3288535200911724942399/

 

BMP is an image file format unrelated to hardware devices. It is also a standard bitmap format that we often see in Windows on PCs. It is widely used. It uses a bit ing storage format. Apart from image depth options, it does not use any compression. Therefore, BMP files occupy a large amount of space. Its biggest benefit is being "accepted" by most software, which can be called a common format.

BMP was a popular image format in the past. Currently, BMP (window Bitmap) images are mainly used as wallpapers when windows are run on PCs. BMP can provide lossless compression, which is called RLE (program length encoding). It is an excellent option when creating a wallpaper image file. Window sometimes encounters an identification error when searching for wallpaper images saved as RLE Compressed files ., Therefore, it is best to disable the RLE compression function first.

A bmp file consists of four parts: File Header, bitmap information header, color information, and graphic data.
1. 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.
Typedef struct tagbitmapfileheader {
Word bftype; // The type of the bitmap file, which must be BM.
DWORD bfsize; // the size of the bitmap file, in bytes.
Word bfreserved1; // reserved word of the bitmap file, which must be 0
Word bfreserved2; // reserved word of the bitmap file, which must be 0
DWORD bfoffbits; // the starting position of the bitmap data, expressed in bytes as the offset relative to the bitmap file header.
} Bitmapfileheader;
2. Bitmap information header: BMP bitmap information header data is used to describe bitmap size and other information.
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. It must be one of 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
Long bixpelspermeter; // horizontal bitmap resolution, number of workers per meter
Long biypelspermeter; // bitmap vertical resolution, number of bytes 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;
3. Color Table: The color table is used to describe the color in the bitmap. It has several table items. Each table item is an rgbquad-type structure and defines a color.
Typedef struct tagrgbquad {
Byte rgbblue; // blue brightness (value range: 0-255)
Byte rgbgreen; // The Green brightness (value range: 0-255)
Byte rgbred; // red brightness (value range: 0-255)
Byte rgbreserved; // 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;
4. bitmap data: the bitmap data records each pixel value of the bitmap. The record sequence is from left to right in the scanning row, and the scanning 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), and the number of insufficient bytes must be filled with 0,
Calculation of the number of bytes occupied by a scanned row:
Datasizeperline = (biwidth * bibitcount + 31)/8; // number of bytes occupied by a scanned row
Datasizeperline = datasizeperline/4*4; // The number of bytes must be a multiple of 4
Bitmap data size (without compression ):
Datasize = datasizeperline * biheight;
Ii. BMP File Analysis
1. tool software: Hex workshop or ultraedit
2. Analysis: First of all, please note that all values are stored in the "high position, low position" principle, such as 12345678h in the memory is 7856 3412 ). The hexadecimal data of a graph is analyzed as an example. In the analysis, in order to simplify the description, a word (two bytes, for example, mongod is a word) is used as the serial number, and "H" indicates a hexadecimal number.

Mongod 4690 0000 0000 0000 4600 0000 2800
0000 8000 0000 9000 0000 0100 1000
0000 0090 0000 a00f 0000 a00f 0000 0000
0000 0000 0000 00f8 0000 e007 0000 1f00
0000 0000 0000 02f1 84f1 04f1 84f1 84f1
06f2 84f1 06f2 04f2 86f2 06f2 86f2 86f2
1: image file header. Required DH = 'bm ', which indicates the BMP format supported by windows.
2-3: The entire file size. 4690 0000, 00009046 H = 36934.
4-5: Reserved. It must be set to 0.
6-7: The offset between graph data from the file. 4600 0000, 00000046 H = 70, the above file header is 35 characters = 70 bytes.
8-9: the length of the Bitmap header.
10-11: The bitmap width, in pixels. 8000 0000, 00000080 H = 128.
12-13: the height of the bitmap, in pixels. 9000 0000, 00000090 H = 144.
14: Number of bitwise sides of a bitmap. The value is always 1. 0100, for 0001 H = 1.
15: The number of digits in each pixel. Available in 1 (monochrome), 4 (16 color), 8 (256 color), 16 (64 K color, high color), 24 (16 M color, true color), 32 (40 96 m color, enhanced real color ). T408 supports the 16-bit format. 1000 is 0010 H = 16.

16-17: compression Description: 0 (not compressed), 1 (RLE 8, 8-bit RLE compression), 2 (RLE 4, 4-bit RLE compression), 3 (bitfields, bit domain storage ). Rle is simply compressed using the pixel number + pixel value. T408 uses a bit field storage mode, which uses two bytes to represent a pixel and the bit field is allocated as r5b6g5. In the figure, 0300 0000 is 00000003 H = 3.

18-19: the size of the bitmap data expressed by the number of cells. The number must be a multiple of 4, and the value is equal to the bitmap width × bitmap height × number of digits per pixel. 0090 0000 is 00009000 H = 80x90x2 h = 36864.

20-21: horizontal resolution in pixels/meters. A00f 0000 is 0000 0fa0h = 4000.
22-23: the vertical resolution in pixels/meters. A00f 0000 is 0000 0fa0h = 4000.
2: The number of color indexes used by the bitmap. If it is set to 0, all color palette items are used.
26-27: Number of color indexes that have an important impact on image display. If it is 0, it means all important.
28-35: color palette specification. For each table item in the color palette, use the following method to describe the RGB value:
1 byte for the blue component
1 byte for green component
1 byte for red weight
1 byte for padding (set to 0)
The color table is not used for 24-bit true color images, because the RGB value in the bitmap represents the color of each pixel. However, a color table is required for a 16-bit r5g6b5 domain color image. The preceding figure is not correct with the above explanation. The following explanation shall prevail.

The color palette in the figure is 00f8 0000 e007 0000 1f00 0000 0000 0000, of which:
00fb 0000 is fb00h = 1111100000000000 (Binary) and is the mask of the red component.
E007 0000 is 07e0h = 0000011111100000 (Binary) and is the mask of the green component.
1f00 0000 is 0000000000011111 (Binary), which is the mask of the red component.
0000 0000 is always set to 0.
Perform the "and" Operation on the mask and pixel value and then shift the value to obtain the component values of different colors. Looking at the mask, we can see that, in fact, in the two bytes of each pixel value, 16 bits are 5, 6, and 5 BITs respectively R, G, and B. After the component value is taken out, multiply the R, G, and B values by 8, 4, and 8, respectively, the first component can be filled into a byte, and then the three bytes are combined by RGB, put it into the memory (also in reverse order), you can convert it to the 24-bit standard BMP format.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.