Data Structure of Bitmap files

Source: Internet
Author: User
This post reprinted address http://bbs.pfan.cn/post-323046.html original person: a river of Autumn Water recently in the research of the structure of the bitmap, see a good article, do not feel exclusive. Thank you for your contribution! Data Structure of Bitmap files

I. File Composition
A bmp file consists of four parts: the file header, the bitmap information header, the color palette, and the data area (the true color bitmap has no color palette and consists of three parts). The structure is as follows (in all the tables below, the unit of offset and length is byte, 10 hexadecimal outside the brackets, and 16 hexadecimal inside the brackets ):

Table 1: Overall structure of a BMP file
--------------------------------------------------------
Offset length description
--------------------------------------------------------
1. BMP file header 000 (000) 14
2. BMP header 014 (00e) 40
3. color palette 054 (036) 8-1024 8 = monochrome, 1024 = 256 colors
4. bitmap data, also known as bitmap
--------------------------------------------------------
Note:
1. Part 1-3 in the Table above is collectively referred to as the dib file header, and Part 2 and Part 3 are also referred to as bitmap information.
2. For a monochrome, 16-color, and 256-color bitmap, the color palette stores the RGB values of the actual color, while the bitmap lattice stores the index values corresponding to the color.
3. There is no color palette for the true color bitmap. The actual color RGB values are stored in the bitmap lattice.

The following describes the structure of each part.

(I) The structure of the BMP file header is shown in table 2.

Table 2: BMP file header Structure
--------------------------------------------------------------
Description of offset Length
--------------------------------------------------------------
000 (000) 2 ASCII code of the file type mongod "BM"
002 (002) 4 file size in bytes
006 (006) 4 reserved 00 00 00 00 must be 0
010 (00a) 4 bitmap lattice offset
--------------------------------------------------------------
Description: bitmap dot matrix offset = the sum of 1-3 part lengths described in table 1

(Ii) The structure of the BMP Information header is shown in table 3.

Table 3: BMP header structure:
------------------------------------------------------------------------------
Description of the Offset length (hexadecimal)
------------------------------------------------------------------------------
014 (00e) 4 BMP header structure length 28 00 00 00 00 (10 hexadecimal = 40)
018 (012) 4 Image Width
022 (016) 4 Image Height
026 (01a) 2-digit panel number 01 00 (required = 1)
028 (01c) 2 the number of digits occupied by each pixel 04 00 (6 possible values: 1/4/8/10/18/20)
030 (01e) 4-pixel data compression type 00 00 00 00 (0 indicates not compressed)
034 (022) 4 bitmap lattice Length
038 (026) 4 horizontal device resolution
042 (02a) 4 device vertical resolution
046 (02e) 4 valid colors 00 00 00 00 (actually used color index in the palette)
)
050 (032) 4 Number of important color indexes 00 00 00 00 (0 indicates that all colors are important)
------------------------------------------------------------------------------
Note:
1. The length data in the table above follows the principle of "low front, high behind. In addition, where the number of bytes is 2, the data type is integer, and the number of bytes is 4, and the data type is long.
2. The bitmap lattice Length indicates the image size, rather than the size of the entire bitmap file.
3. the number of bits in each pixel refers to the number of BITs (1 byte = 8 bits) in the in-position graph of the pixel. 01 indicates a monochrome Bitmap (black/white Bitmap) and 04 indicates a 16-color bitmap, 08 indicates 256 colors, 10 indicates 16-bit enhanced colors, 18 indicates 24-bit true colors, and 20 indicates 32-bit true colors.
4. The unit of the horizontal and vertical resolutions of the device is pixel/meter. I really don't know why Microsoft has set the "meter" as the unit. No wonder these two parameters of most BMP images are equal to 0.

(Iii) The structure of the color palette is shown in Table 4-Table 6

Table 4: Structure of the color palette
------------------------
Color length description
------------------------
Blue (B) 1 blue component
Green (g) 1 green component
Red (R) 1 red weight
Reserved 1 unused, = 0
------------------------
Note:
1. The monochrome bitmap palette contains two color values: Black and white. The bitmap lattice area consists of two index values: 1 and 0. The 16-color bitmap palette contains 16 color values. The bitmap lattice area consists of 0--15 index values. The 256-color bitmap palette stores 256 color values, and the bitmap lattice area stores the 0--255 index values.
2. In the color palette, each color consists of three components: blue, green, and red, plus a reserved byte. Therefore, each color occupies 4 bytes.

Table 5: color palette data of three bitmap types
-----------------------------------
Icon type offset Length
-----------------------------------
Monochrome 054 (036) 8 (4 × 2)
16 colors 054 (036) 64 (4 × 16)
256 colors 054 (036) 1024 (4 × 256)
-----------------------------------

Table 6: Common palette index values and corresponding color values of VB
----------------------------
Color index value color value
----------------------------
Monochrome bitmap:
Black 0 00 00 00 00
White 1 FF 00

16-color bitmap:
Black 0 00 00 00 00
Dark red 1 00 00 80 00
Dark green 2 00 80 00 00
Dark yellow 3 00 80 80 00
Deep Blue 4 80 00 00 00
Deep Purple 5 80 00 80 00
Shen Qing 6 80 80 00 00
Dark gray 7 80 80 80 00
Light gray 8 C0 C0 C0 00
Red 9 00 00 ff 00
Green a 00 ff 00 00
Yellow B 00 FF 00
Blue C ff 00 00 00
Purple D ff 00 ff 00
Cyan e ff 00 00
White f ff 00
----------------------------
Note:
1. Each color value occupies 4 bytes, And the last byte is not used.
2. the "number of bytes occupied by each pixel" in Table 3 refers to the number of bytes occupied by a pixel in the in-place graph. If it is equal to 1, one pixel occupies one bit, and one byte can represent eight pixels, we call it a monochrome Bitmap (black and white Bitmap). If it is 4, it indicates that one pixel occupies 4 bits, and one byte can represent 2 pixels. We call it a 16-color bitmap. If it is 8, A pixel occupies 8 bits, and a byte represents only one pixel, which is called a 256-color bitmap.
3. How can this problem be solved? For example, if a byte value in the bitmap area of a 16-color bitmap is 81, the two pixels corresponding to the byte are displayed. The pixel index value on the left is 8, the corresponding color is light gray, and the pixel index value on the right is 1, the corresponding color is dark red.

(Iv) bitmap lattice structure

1. uncompress bitmap lattice data is stored from bottom to top by scanning line, while in each scanning line, it is stored from left to right, that is, the first pixel value of the image, in the lower left corner of the bitmap array, the last pixel value is in the upper right corner of the bitmap array.
2. Use the bitmap of the color palette. The Data byte height in the bitmap area indicates the pixel to the left. Low Position indicates the right pixel.
3. In the bitmap lattice area, the length of each scanned line must end at the 32-bit boundary. That is to say, the length of each scanned line must be 4 bytes apart; otherwise, it must be all 0 bytes. For example, if a scanned line contains three bytes, you must add one blank byte to four bytes. If there are six bytes, then, we must add two blank bytes to 8 bytes.
4. The relationship between the bitmap width and the scanned line length is shown in Table 7-table 10 (image width unit: pixels, scanned line length unit: bytes)

Table 7: Relationship between the width of a monochrome bitmap and the scanning line length
----------------------
Image Width scanned line length
----------------------
1-32 4
33-64 8
65--96 12
97--120 16
......
----------------------

Table 8: Relationship between 16-color bitmap width and Scan Line Length
----------------------
Image Width scanned line length
----------------------
1-8 4
9-16 8
17--24 12
25--32 16
33--40 20
41--48 24
49--56 28
57--64 32
65--72 36
71--80 40
......
----------------------

Table 9: Relationship between 256-color bitmap width and Scan Line Length
----------------------
Image Width scanned line length
----------------------
1-4 4
5-8 8
9-12 12
13-16 16
17--20 20
21-24
25--28 28
29--32 32
33--36 36
37--40 40
41--44 44
45--48 48
......
----------------------

Table 10: Relationship between 24-bit true color bitmap width and Scan Line Length
----------------------
Image Width scanned line length
----------------------
1 4
2 8
3 12
4 12
5 16
6 20
7 24
8 24
9 28
10 32
11 36
12 36
13 40
14 44
15 48
16 48
17 52
18 56
19 60
20 60
21 64
22 68
23 72
24 72
......
----------------------
Note:
1. The VB code for calculating the length of a scanned line of a bitmap is summarized from the above four tables as follows:
① Monochrome bitmap scanned line length = 4 x (Image Width \ 32 + ABS (Image Width mod 32)> 0 ))
② 16-color bitmap each scanned line length = 4 × (Image Width \ 8 + ABS (Image Width mod 8)> 0 ))
③ 256 color bitmap scanned line length = 4 x (Image Width \ 4 + ABS (Image Width mod 4)> 0 ))
④ 24-bit true color bitmap each scanned line length = 4× (Image Width × 3 \ 4 + ABS (Image Width × 3 mod 4)> 0 ))
2. Knowing the scanning line length and Image Height, we can easily calculate the size of the map file. For example, for a 16 × 16 monochrome bitmap, we can see in Table 7 that the scanned line length is 4, then the bitmap lattice size = height × scanned line length = 16 × 4 = 64. We can see in table 5, the size of the color palette is 8. You can see that the size of the BMP file header is 14, and the size of the BMP Information header is 40. Therefore, the size of the bitmap file is 64 + 8 + 14 + 40 = 126 bytes.

 

Ii. Three specific examples

(I) Create an 8x8 24-bit true color Bitmap Using the system's built-in drawing program. The image is square, the border is red (& h0000ff), and the center is light gray (& hc4c4c4 ), all the data is shown in table 11.
Let's first predict the file size: we can see in Table 10 that the scanned line length is 24, then the image data length is 8 × 24 = 192. Table 1 shows that the total length of the file header and information header is 54, the file size is 54 + 192 = 246 bytes. Let's verify it according to table 11.

Table 11: 8x8 Real-color bitmap data
--------------------------------------------------------
Offset data
--------------------------------------------------------
00: 42 4D F6 00 00 00 00 00 00 36 00 00 00 28 00
10: 00 00 08 00 00 08 00 00 00 01 00 18 00 00 00 00
20: 00 00 C0 00 00 00 00 00 00 00 00 00 00 00 00 00
30: 00 00 00 00 00 00 ff 00 00 ff 00 00 00 ff 00
40: 00 ff 00 00 ff 00 00 ff 00 00 ff 00 00 ff 00 00
50: FF C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4
60: C4 C4 C4 00 00 ff 00 FF C4 C4 C4 C4 C4 C4
70: C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 00 00 ff 00 00
80: FF C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4
90: C4 C4 C4 00 00 ff 00 FF C4 C4 C4 C4 C4 C4
A0: C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 00 00 ff 00 00
B0: FF C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4
C0: C4 C4 C4 00 00 ff 00 FF C4 C4 C4 C4 C4 C4
D0: C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 C4 00 00 ff 00 00
E0: ff 00 00 ff 00 00 ff 00 00 ff 00 00 ff 00 FF
F0: 00 00 ff 00 FF
--------------------------------------------------------
Analysis:

1. BMP File Header (14 bytes)
00-01: ASCII code with a value of 42 4d and "BM"
02-05: The value = F6 00 00 00, and the file size = 246 bytes
06-09: Reserved
0a-0d: value = 36 00 00 00, bitmap dot matrix offset = 54

2. BMP header (40 bytes)
0e-11: value = 28 00 00 00, BMP header size = 40
12-15: value = 08 00 00, image width = 8
16-19: value = 08 00 00, Image Height = 8
1A-1B: value = 01 00, number of bit panels = 1
1c-1d: value = 18 00, number of digits per pixel = 24 (24-bit true color)
1e-21: value = 00 00 00 00, compression type = 0 (no compression)
22-25: value = C0 00 00 00, Image Data Length = 192
26-29: horizontal device resolution = 0
2a-2d: Device vertical resolution = 0
2e-31: the number of color indexes actually used in the color palette = 0
32-35: number of important color indexes = 0

3. Bitmap (192 bytes)
36-4d: the last row of the bitmap data. Because it is a red line, the color value of each pixel is 00 ff.
4e-65: the data in the last and last rows of the bitmap. The first and last two pixels of the row are red, and the middle six pixels are light gray. Therefore, the values of 4e-50 bytes and 63-65 bytes are 00 ff, the 51-62 bytes are C4.
66-dd: the data from the second row to the sixth row of the bitmap. Each row occupies 24 bytes.
DE-F5: The data of the first line of the bitmap, see the analysis of the last line

(2) create a 16-color bitmap with the built-in system drawing program. The image is square, the border is red (& h0000ff), and the middle is yellow (& h00ffff ), all the data is shown in table 12.
Let's first predict the file size: we can see in Table 8 that the scanned line length is 8, then the image data length is 16 × 8 = 128. Table 1 shows that the sum of the file header length and information header length is 54, table 5 shows that the color palette length is 64, and the file size is 64 + 54 + 128 = 246 bytes. Let's verify it according to table 12.

Table 12: 16*16 16 color bitmap data
--------------------------------------------------------
Offset data
--------------------------------------------------------
00: 42 4D F6 00 00 00 00 00 00 76 00 00 00 28 00
10: 00 00 10 00 00 00 10 00 00 00 01 00 04 00 00 00
20: 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00
30: 00 00 00 00 00 00 00 00 00 00 80 00 80
40: 00 00 00 80 00 80 00 00 00 00 80 00 80 80
50: 00 00 80 80 00 C0 C0 C0 00 00 00 ff 00 FF
60: 00 00 00 FF 00 ff 00 00 00 ff 00 ff 00 FF
70: 00 00 FF 00 99 99 99 99 99 9B bb
80: BB B9 9B BB B9 9B bb
90: BB B9 9B BB B9 9B bb
A0: BB B9 9B BB B9 9B bb
B0: BB B9 9B BB B9 9B bb
C0: BB B9 9B BB B9 9B bb
D0: BB B9 9B BB B9 9B bb
E0: BB B9 9B BB B9 99
F0: 99 99 99 99 99
--------------------------------------------------------
Analysis:

1. BMP File Header (14 bytes)
00-01: ASCII code with a value of 42 4d and "BM"
02-05: The value = F6 00 00 00, and the file size = 246 bytes
06-09: Reserved
0a-0d: value = 76 00 00 00, bitmap dot matrix offset = 121

2. BMP header (40 bytes)
0e-11: value = 28 00 00 00, BMP header size = 40
12-15: value = 10 00 00, image width = 16
16-19: value = 10 00 00, Image Height = 16
1A-1B: value = 01 00, number of bit panels = 1
1c-1d: value = 04 00, number of digits per pixel = 4 (16 colors)
1e-21: value = 00 00 00 00, compression type = 0 (no compression)
22-25: value = 80 00 00, Image Data Length = 128
26-29: horizontal device resolution = 0
2a-2d: Device vertical resolution = 0
2e-31: the number of color indexes actually used in the color palette = 0 (Full Use)
32-35: number of important color indexes = 0 (all important)

3. color palette (64 bytes)
36-75: see table 6 for analysis.

4. Bitmap (128 bytes)
-7d: value = 99 99 99 99 99 99 99, the last row of the bitmap data, because it is a red line, so the color index value of each pixel is 9
7e-85: value = 9B BB B9, the data in the last and last rows of the bitmap, the first and last two pixels of the row are red, so the index value is 9, 14 pixels in the middle are yellow, so the index value is B
86-ed: the data from the second row to the second row of the bitmap. Each row occupies 8 bytes. The analysis is the same as above.
EE-F5: The data of the first line of the bitmap, see the analysis of the last line

 

(3) create a 16 × 16 monochrome bitmap with the built-in Plotting Program. The image is square, the border is black (& h000000), and the middle is white (& hffffff ), all the data is shown in table 13.
Let's first predict the file size: Table 7 shows that the scanned line length is 4, and the image data length is 16 × 4 = 64. Table 1 shows that the sum of the file header length and information header length is 54, table 5 shows that the color palette length is 8, and the file size is 8 + 54 + 64 = 126 bytes. Let's verify it according to table 13.

Table 13: monochrome bitmap data of 16 × 16
--------------------------------------------------------
Offset data
--------------------------------------------------------
00: 42 4D 7E 00 00 00 00 00 3E 00 00 28 00
10: 00 00 10 00 00 00 10 00 00 00 01 00 00 00 00
20: 00 00 40 00 00 00 00 00 00 00 00 00 00 00 00
30: 00 00 00 00 00 00 00 00 FF 00 00 00 00 00
40: 00 00 7f Fe 00 00 7f Fe 00 00 7f Fe 00 7f fe
50: 00 00 7f Fe 00 00 7f Fe 00 00 7f Fe 00 7f fe
60: 00 00 7f Fe 00 00 7f Fe 00 00 7f Fe 00 7f fe
70: 00 00 7f Fe 00 00 7f Fe 00 00 00 00 00
--------------------------------------------------------
Analysis:

1. BMP File Header (14 bytes)
00-01: ASCII code with a value of 42 4d and "BM"
02-05: value = 7E 00 00, file size = 126 bytes
06-09: Reserved
0a-0d: value = 3E 00 00 00, bitmap dot matrix offset = 62

2. BMP header (40 bytes)
0e-11: value = 28 00 00 00, BMP header size = 40
12-15: value = 10 00 00, image width = 16
16-19: value = 10 00 00, Image Height = 16
1A-1B: value = 01 00, number of bit panels = 1
1c-1d: value = 01 00, number of digits per pixel = 1 (monochrome)
1e-21: value = 00 00 00 00, compression type = 0 (no compression)
22-25: value = 40 00 00, Image Data Length = 64
26-29: horizontal device resolution = 0
2a-2d: Device vertical resolution = 0
2e-31: the number of color indexes actually used in the color palette = 0 (Full Use)
32-35: number of important color indexes = 0 (all important)

3. color palette (8 bytes)
36-3d: value = 00 00 00 00 FF 00, the first four words are black RGB values, and the last four bytes are white RGB values

4. Bitmap (64 bytes)
3e-41: value = 0. the last row of the bitmap data. Because it is a black line, the color index value of each pixel is 0.
42-45: The value = 7f Fe 00, the data in the last row of the bitmap. The valid value is the first two bytes, and the last two bytes are the supplementary bytes. Convert 7f Fe into binary values: 0111,1111, 1111,1110. The first and last two pixels of the row are black. Therefore, the index value is 0 and the middle 14 pixels are white. Therefore, the index value is 1.
46-79: the data from the second row to the second row of the bitmap, each row occupies 4 bytes. The analysis is the same as above.
7a-7d: The data of the first row of the bitmap. For more information, see the analysis of the last row.

3. Small experiment: change one byte and change the monochrome bitmap from black to white to red and white.
Use the hex editor to open any monochrome bitmap, change the & H38 byte value to FF, save it, and use the graph tool to open it. How is the color changed?
In fact, we can change the 6 bytes in the palette to any value!
We can see that 16-color and 256-color Bitmap (as long as there is a color palette) can be changed accordingly.
In addition, we can also use this method for confidentiality: If you have a color palette pattern bitmap and the image above is all your bank passwords, what should you do to encrypt the image? You can try to change all colors in the color palette to the same color value, so that others will see a solid color image, and when you look at the password, you only need to restore all colors in the palette (you can write a piece of code to complete the conversion ). Of course, if you perform an exclusive or operation on every byte in the bitmap part, it will become a secret!

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.