Research on the data structure of Icon files
--------------------
I would like to consult the data format of the true color icon and bitmap
Research on the data structure of Icon files
In the past few days, I have studied the data structure of a single logo by writing an icon editing program.
To share with you. (Note: The icons mentioned below are in the color palette mode.
Ming)
I. Starting from the principle of icon display
Each icon is composed of two separate bitmaps. If the icon is to mask the background color, the first
A Bitmap consists of a black background (the corresponding color bits are all 0) and a color icon pattern.
It is called an XOR bitmap because it is combined by an exclusive or operation (XOR). The second bitmap is composed of a white background (
The bitmap is composed of a black icon (the color bit is 0 ).
Combined with Operation (and), it is called and bitmap. Therefore, the icon is displayed in two steps:
1. The current screen display and the bitmap are combined with the and operation;
2. The current screen display and XOR bitmap are combined through XOR operations.
As you know, the result of 1 and any value and operation will maintain the original value, while the result of 0 and any value and operation is
Is 0, so in step 1, the white (1) and the screen display in the and bitmap are blocked by the original color after the and operation, while the black
Color (0) blocks the original color. After step 1 is completed, a black icon is displayed. In subsequent steps
In 2, because 0 and any value are different or the result will be the original value, therefore, after XOR bitmap and screen display are different or operated,
The bitmap and the black part in the screen are blocked by the corresponding color part. After step 2 ends, an irregular shape
The icon appears on the screen. This is how the icon is displayed.
2. Icon graphics
An icon is actually a bitmap format. It is a standard bitmap format. For Windows
It is enough to obtain information in the Information header. It explains how to process the data that appears later. For the color palette Module
The data package contains the color palette and the pixel color index. If the color is true, the subsequent data is directly pixel.
The RGB color value of the vertex.
Bitmap files are composed of file headers, BMP Information headers, color palette, data areas (also called bitmap lattice), and other parts.
. After understanding this situation, we can simply understand the structure of the previously described icons as bitmap information. In this way,
We may not need to really draw a picture at all, but only need to fill in the key data program.
According to the requirements of the icon format, a bitmap with a size not greater than 255x255 pixels can be written directly,
Encapsulated as a standard icon (the actual bitmap width and height remain unchanged, so you can make an icon up to 255x255
).
3. Icon Data Structure
The data structure of the icon file can be divided into six parts. The data structure is as follows:
-----------------------------------------------------------------------
Length (unit: bytes) of the Shun sign
-----------------------------------------------------------------------
1 file header 6
2. How many images are there in Image Information Block 16?
3. BMP header 40
4 XOR bitmap color palette 16 color = 64,256 color = 1024 true color icon from then on the pixel RGB Value
5 XOR bitmaps are stored as palette index values.
6 and bitmap
-----------------------------------------------------------------------
Note:
1. Part 3-6 in the preceding table are collectively referred to as image data blocks. Part 5 and Part 6 are also referred to as image data areas (also called bitmap lattice)
2. No matter what type of icon, the length of 1-3 bytes is fixed.
3. Pay attention to the description of image information blocks in Part 1. What does "How many images are there" mean? Originally
Icon files may store several images (Windows Icons can have up to 8 images), but each image has
Their own color palette, XOR bitmap, and their image sizes may be different. Three graphs in 16 colors
For example, the data structure is as follows:
-----------------------------------------------------------------
Description of the length of the Sn
-----------------------------------------------------------------
1 file header 6
2. Image Information block of image 1 16
3. Image Information block of Image 2 16
4. Image Information block of Image 3 16
5. The image data block of image 1 is not fixed. The BMP header and palette are fixed to 40 and 64 bytes.
6. The image data block of Image 2 is not the same as that of Image 2.
7. The image data block of Image 3 is not the same as that of Image 3.
------------------------------------------------------------------
The following example details the icons with one image, 16-color, and 16-16 specifications (10-digit before offset brackets, including
Number is in hexadecimal notation ):
----------------------------------------------------------------------------
Offset byte Number Description typical value (hexadecimal)
----------------------------------------------------------------------------
(1) File Header 6 bytes)
000 (000) 2 reserved bytes 00 00
002 (002) 2 Resource Type 01 00 (01 is the icon, 02 is the cursor)
004 (004) 2 image count 01 00
(2. 16 bytes of image information block)
006 (006) 1 icon width 10
007 (007) 1 icon height 10
008 (008) 1 color count 10 (02 = monochrome, 00 ≥ 256 colors)
009 (009) 1 unused 00
010 (00a) 4 reserved 00 00 00
014 (00e) 4 image data block length 28 01 00 00 (10 hexadecimal = 296)
018 (012) 4. offset of the image data block to the file header: 16 00 00 00 (10 hexadecimal = 22)
(3) header 40 bytes of BMP Information)
022 (016) 4 BMP header structure length 28 00 00 00 (10 hexadecimal = 40)
026 (01a) 4 Image Width 10 00 00 00
030 (01e) 4 Image Height (XOR Image Height + and Image Height) 20 00 00 00
034 (022) 2-digit panel count 01 00
036 (024) 2 the number of digits occupied by each pixel 04 00
038 (026) 4-pixel data compression type 00 00 00 00 (indicating not compressed)
042 (02a) 4 Image Data Length C0 00 00 00 00 (10 hexadecimal = 192)
046 (02e) 16 unused 16 00
(4. The XOR bitmap color palette is 64 bytes)
062 (03e) 1 blue component
063 (03f) 1 green component
064 (040) 1 red weight
065 (041) 1 unused 00
......
(V. XOR bitmap data)
126 (07e) 128 XOR bitmap
(6. monochrome and bitmap data)
254 (0fe) 64 and bitmap
-----------------------------------------------------------------------------
Note:
1. The data about the length in the Table above follows the principle of "the low position is in front and the high position is in the back. For example, the number of images whose offset is 00e
The data block length is 28 01 00 00. During computation, convert them into 00 00 01 28, which is actually 16
128 is the 10-digit 296.
2. The "number of bits per pixel" with an offset of 0 24 indicates the number of bits per pixel in the XOR Bitmap (1 byte = 8 bits)
, 04 indicates that each pixel occupies 4 bits. In other words, each byte in the XOR bitmap can represent 2 pixels. For example
01 00, it indicates a monochrome (black and white Bitmap), 04 00 is 16 color, 08 00 is 256 color, 18 00 is 24 real color.
3. the XOR bitmap corresponds to two pixels (16 colors) or one pixel (256 colors) per byte. Its arrangement is backward (the last line
Pixels are at the beginning, and so on). In the row, pixels are in the order from left to right, And the byte height indicates the pixel to the left. And Bit
Each byte of the image corresponds to 8 pixels (each corresponding to 1 pixel), and the arrangement is the same as that of the XOR bitmap.
4. The true color icon does not have a color palette. Instead, the pixel RGB value is directly stored at the end of Part 2 of the BMP header, which is 24 bits.
Each three bytes of the true color icon corresponds to one pixel value, and each byte corresponds to the R, G, and B values of the pixel value respectively. Image
The prime arrangement pattern is the same as the icon in the color palette mode.
The following is the color palette of various icons, the length and total length of the image data, and the offset is in the brackets:
Unit: bytes
------------------------------------------------------
Icon type palette XOR bitmap and total length of the bitmap
------------------------------------------------------
16 colors 16x16 pixels (62) 64 (126) 128 (254) 64 318
16 colors 24x24 pixels (62) 64 (126) 288 (414) 96 510
16 colors 32x32 pixels (62) 64 (126) 512 (638) 128 766
16 colors 48x48 pixels (62) 64 (126) 1152 (1278) 384 1662
256 colors 16x16 pixels (62) 1024 (1086) 256 (1342) 64 1406
256 colors 24x24 pixels (62) 1024 (1086) 576 (1662) 96 1758
256 colors 32x32 pixels (62) 1024 (1086) 1024 (2110) 128 2238
256 colors 48x48 pixels (62) 1024 (1086) 2304 (3390) 384 3774
------------------------------------------------------
Note:
1. the XOR bitmap corresponds to two pixels (16 colors) or one pixel (256 colors) per byte)
2. The and bitmap is a group of 4 bytes (the and bitmap of 48 × 48 icons is a group of 8 bytes ).
Should be 1 pixel. Note:
① 16 × 16 icons are not used in the last two bytes of each group, so they are actually two bytes (16 bits) corresponding to a row of 16 pixels.
② 24 × 24 icons are not used in the last byte of each group, so they are actually 3 bytes (24 bits) corresponding to 24 pixels in a row.
③ 32 × 32 icons each group of 4 bytes 32 bits correspond to a row of 32 pixels
④ The last two bytes of the 48 × 48 icon in each group are not used. Therefore, it is actually 6 bytes (48 bits) that correspond to 48 pixels in a row.
3. Why are there unnecessary bytes in the and bitmap? Isn't that a waste? It turns out that Windows has a rule that
A scanned line (a row of data in a bitmap is called a scanned line) must end at a 32-bit boundary. That is to say,
The length of a scanned line (calculated by bit) must be 32 characters in length or 4 bytes in length. That is to say, for example
If there are only eight digits, then we can add 24 digits in the blank space. If there are only 48 digits, we can add 16 digits in the blank space.
4. To make the background transparent, the XOR bitmap background must be black (corresponding color bit = 0), the pattern is color, and the and bit
The image background must be white (corresponding color bit = 1), and the pattern must be black (corresponding color bit = 0)
5. The color information stored in the XOR bitmap is not the color value, but the index value corresponding to the color palette, starting from 0.
Id.
4. Icon color
The color of an icon in the palette mode is determined by the color palette, which can be customized. For example, 16 colors
Icon. The color palette can only contain 16 colors. The following uses the 16 colors of QBASIC as the color palette, XOR bit
Compare the 16-color index value of the color palette with the qbcolor color value:
----------------------------------------------------
Color index value the value in the qBASIC palette (the last byte is not used)
----------------------------------------------------
Black 0 0 00 00 00 00
Dark red 1 4 00 00 80 00
Dark green 2 2 00 80 00 00
Dark yellow 3 6 00 80 80 00
Deep Blue 4 1 80 00 00
Deep Purple 5 5 80 00 80 00
Shen Qing 6 3 80 80 00 00
Dark gray 7 8 80 80 00
Light gray 8 7 C0 C0 C0 00
Red 9 C (12) 00 00 ff 00
Green A (10) A (10) 00 ff 00 00
Yellow B (11) E (14) 00 ff 00
Blue C (12) 9 ff 00 00 00
Purple D (13) D (13) ff 00 ff 00
Cyan E (14) B (11) FF 00 00
White F (15) f (15) FF 00
----------------------------------------------------
How can this problem be solved? For example, if the value of a byte in the XOR bitmap is 81
Should be 2 pixels, the left pixel is light gray and the right pixel is dark red.
V. A specific example
There is a 16-color 16*16 pixel icon, which is a large red "Mouth" shape, all its data is as follows:
------------------------------------------------------
Offset 00 01 02 03 04 05 06 07 08 09 0a 0b 0C 0d 0e 0f
------------------------------------------------------
000 00 00 01 00 01 00 10 10 10 00 00 00 00 28 01
010 00 00 16 00 00 00 28 00 00 00 10 00 00 00 20 00
020 00 00 01 00 04 00 00 00 00 C0 00 00 00 00 00
030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
040 00 00 00 80 00 80 00 00 00 00 80 80 00
050 00 00 80 00 80 00 80 00 00 00 80 80 00 C0 C0
060 C0 00 00 00 ff 00 00 ff 00 00 00 ff 00 ff 00 ff 00
070 00 00 ff 00 ff 00 ff 00 ff 00 00 FF 00 99
080 99 99 99 99 90 00 00 00 00 00 09 90 00
090 00 00 00 00 09 90 00 00 00 00 00 09 90 00
0a0 00 00 00 00 09 90 00 00 00 00 00 09 90 00
0b0 00 00 00 00 09 90 00 00 00 00 00 09 90 00
0c0 00 00 00 00 09 90 00 00 00 00 00 09 90 00
0d0 00 00 00 00 09 90 00 00 00 00 00 09 90 00
0e0 00 00 00 00 09 90 00 00 00 00 00 09 90 00
0f0 00 00 00 00 09 99 99 99 99 99 99 00 00
100 00 00 7f Fe 00 00 7f Fe 00 00 7f Fe 00 7f fe
110 00 00 7f Fe 00 00 7f Fe 00 00 7f Fe 00 7f fe
120 00 00 7f Fe 00 00 7f Fe 00 00 7f Fe 00 7f fe
130 00 00 7f Fe 00 00 7f Fe 00 00 00 00 00
------------------------------------------------------
The following sections describe the bitmap data:
1. the last row of XOR bitmap data is in the 8 bytes 07e-085. Because it is a red line, the index value is 9.
2. The data in the last row of the XOR bitmap is in the 8 bytes of 086-08d. The first and last two pixels of the row are in red, and the remaining 14 are in red.
The pixel is the background color, so the color index values of the 086 bytes corresponding to the pixel are 9 and 0, respectively, and the color index values of the 08d bytes correspond to the pixel
The color index values are 0 and 9 respectively, because the background color is to be shielded, except Red, the others are black, black in
The index value in the palette is 0. The data of other rows is analyzed accordingly.
3. the last row of the and bitmap is in the 0fe-101 byte. The last two bytes are useless. The first two bytes are 00,
To shield the background color, the bitmap and image must be 0 (black), regardless of the original color of the image,
Therefore, this line is a red line, and the corresponding color bits are all 0.
4. The data of the last and second rows of the bitmap is in the 4 bytes of the 102-105. The first two bytes correspond to 16 pixels of the row.
The value is 7f fe. First look at 7f, its binary value is 01111111, because the first pixel of this row is red, so the corresponding highest
The color is 0 (black), followed by the 7 pixels are the background color, and in the and bitmap, to shield the background color, then the background color
The corresponding bits should be white and white in a monochrome, so the hexadecimal value represented by these eight pixels is 7f.
Let's look at Fe. Its binary value is 11111110. It corresponds to the last 8 pixels of the row, and the last 1 pixel of the row is red,
So the corresponding pixel is 0 (black), and other pixels are the background color, so the corresponding bit is 1 (white ).
5. You can analyze the data of other rows by referring to the above method.