First look at the header file format of BMP:
Note: The above multi-byte type, for example, int32 is stored after the first high level and low level, such as the BIT file size: 0002-0005, int32 type, the obtained integer is correct and must be combined in the order of 5th, 4, 3, and 2 bytes.
With the header file information, we can find the bitmap data. Bitmap data is processed in a group of 4 bytes (32-bit, corresponding to 32-bit CPU optimization.
Example 1. Let's first look at the simple internal storage method (0: Black, 1: White) with only two colors of black and white ):
Original BMP image (one square indicates one pixel)
Binary Code of the BMP Image
The red box indicates the bitmap data information. Four bytes are used as a group (7*5 pixels). The bitmap data is 5 bytes, and the figure height is 5 pixels. Therefore, each row contains 1 byte, they are:
7E 00 00 00; 0111 1110 0000 0000 0000 0000
Fe 00 00 00; 1111 1110 0000 0000 0000 0000 0000
7E 00 00 00; 0111 1110 0000 0000 0000 0000
Fe 00 00 00; 1111 1110 0000 0000 0000 0000 0000
54 00 00 00; 0101 0100 0000 0000 0000 0000 0000
By comparing the original BMP image, you will find that it is stored horizontally and vertically, first down and then on. Because there are only 7 horizontal pixels, 4 bytes (32 bits) use only the first 7 bits, and then 0.
Example 2. view the 24-Bit Bitmap, which is composed of three RGB primary colors. Each color is represented by one byte.
24-bit BMP source Image
24-bit BMP Image Binary Code
The red box indicates the bitmap data information. Four bytes are used as a group (2*3 pixels). The bitmap data contains 24 bytes. The height of the image is 3 pixels. Therefore, each row contains 8 bytes, they are:
FF 00 ff 00 00 00; FF 00 ff 00 00 00 00 indicates: white green
00 ff 00 00 00 ff 00 00; 00 ff 00 00 00 ff 00 00 indicates: green red
00 00 00 ff 00 00 00 00; 00 00 00 ff 00 00 00 00 means: black and blue
Because 24 bits use RGB primary colors, each primary color uses one byte.
After observation, it is found that the color is in BGR sequence, which may be related to the order of word storage. The color is first high and then low.