Bmp format analysis

The bitmap file (Bitmap-File, BMP) format is an image file storage format adopted by Windows, and all image processing software running in the Windows environment supports this format. The BMP bitmap file format before Windows 3.0 is related to the display device, so it is called the device-dependent bitmap (device-dependent bitmap, DDB) file format. The BMP bitmap file format after Windows 3.0 has nothing to do with the display device, so this BMP bitmap file format is called device-independent bitmap (device-independent bitmap, DIB) format, the purpose is to enable Windows to display in any type The BMP bitmap file is displayed on the device.
      A bitmap file can be seen as consisting of four parts: a bitmap-file header, a bitmap-information header, a color table, and a byte array that defines the bitmap. BMP files can be opened in text opening mode.
(1) File header information block
0000-0001: file identification, which is the letter ASCII code "BM", 42 4D. Or compare with 19778.
0002-0005: The size of the entire file, in bytes. Low byte first.
0006-0009: reserved, fill in each byte with "00".
000A-000D: Record the starting position of the image data area. The offset from the beginning of the file to the bitmap data. 
(2) Image description information block
000E-0011: The size of the image description information block, usually 28H.
0012-0015: Image width. In pixels. Low byte first.
0016-0019: Image height. In pixels. Low byte first.
001A-001B: The total number of planes of the image (constantly 1).
001C-001D: The number of digits of recording pixels, a very important value, and the number of colors of the image is determined by this value.
1-Monochrome bitmap, 4-16
color bitmap,
8-256 color bitmap,
F-16-bit bitmap,
10-16-bit RGB565 bitmap,
18-24bit (true color) bitmap, 20-32
-bit bitmap.
001E-0021: Data compression method (value bit 0: no compression; 1: 8-bit compression; 2: 4-bit compression; 3: Bitfields compression).
0022-0025: The size of the image area data. Unit byte, the number must be a multiple of 4.
0026-0029: How many pixels are there per meter in the level, in the device-independent bitmap (.DIB), each byte is filled with 00H.
002A-002D: How many pixels are there per meter vertically, in the device-independent bitmap (.DIB), each byte is filled with 00H.
002E-0031: The number of colors used in this image.
0032-0035: Specify the number of important colors. When the value of this field is equal to the number of colors (or equal to 0), it means that all colors are equally important.
     As above, the information header of the entire bitmap has a total of 54 bytes, and each position has a specific meaning.
(3) Color table (palette)
      The size of the color table depends on the color mode used, in which every 4 bytes represents a color, and it is B (blue), G (green), R (red), alpha (the transparency value of a 32-bit bitmap) , Generally not needed). The size of the color table depends on the color mode used: 2-color image is 8 bytes; 16-color image is 64 bytes; 256-color image is 1024 bytes. Among them, every 4 bytes represents a color, and B (blue), G (green), R (red), alpha (the transparency value of a 32-bit bitmap, generally not required). That is, the first 4 bytes represent the color of color number 1, and the next represents the color of color number 2, and so on.   
      For 24-bit true color images, the color table is not used (also including 16-bit and 32-bit bitmaps), because the RGB value in the bitmap represents the color of each pixel; and for those using indexed colors , You need a larger palette.
(4) The
       color table of the image data area The next is the image data area of ​​the bitmap file. In this part, the color index number corresponding to each pixel is recorded. The recording method also depends on the color mode. 1 bit (8 bits is 1 byte); 16-color image occupies 4 bits per dot (nibble); 256-color image occupies 8 bits per dot (1 byte); true color image occupies 24 bits per dot (3 words) Section). Therefore, the size of the entire data area will also change accordingly. In terms of the law, the following calculation formula can be obtained: image data information size = (image width * image height * number of recorded pixels)/8. The scan line is stored from bottom to top, that is, the first byte in the array represents the pixel at the lower left corner of the bitmap, and the last byte represents the pixel at the upper right corner of the bitmap.
       However, the size of the uncompressed image information area. Except for the true color mode, the rest are greater than or equal to the size of the data information. Why is this? There are two reasons:
The BMP file records a line of images in bytes. Therefore, there is no point represented by the data bit information in one byte in two different rows. In other words, set the display mode to 16 colors. When two points of information are allocated to each byte, if the width of the image is odd, then the information of the last pixel will occupy one byte, and the last 4 bits of this byte Will not make sense. The next byte will start recording the information of the next line. 
(5) Case analysis

     The following 4x4 pixel bitmap

After UE opens into a hexadecimal file, the display is as follows:

We can verify each part of the information analyzed above by looking up the corresponding data bits. This makes it clearer.
The data format of 24-bit true color is B (8 bits) G (8 bits) R (8 bits), which is caused by the low byte first. 
Structure (6) bitmap operations commonly used in 
      the bitmap header file structure:
typedef struct tagBITMAPFILEHEADER {      Short bfType;      int bfSize;      Short bfReserved1;      Short bfReserved2;      int bfOffBits; } the BITMAPFILEHEADER, * PBITMAPFILEHEADER;       bitmap information structure: typedef struct tagBITMAPINFOHEADER {       int biSize;       int biWidth;       int biHeight;       short biPlanes;       short biBitCount;       int biCompression;       int biSizeImage;       int biXPelsPerMeter;       int biYPelsPerMeter;

















      int biClrUsed;
      int biClrImportant;
}BITMAPINFOHEADER,*PBITMAPINFOHEADER;
 (7) Scan order
BMP bitmap scan order is from the bottom left corner of the picture from left to right, from bottom to top to scan

Reference original text: http://blog.csdn.net/jsjjms/archive/2007/04/18/1568615.aspx

Guess you like

Origin blog.csdn.net/kh815/article/details/87985673