Bitmap (1)

Source: Internet
Author: User
Tags color representation

Nowadays, Windows has become the operating system used by most users. An important factor for its success over DOS is its visual and beautiful interface. How does one display images in windows? This involves bitmap ).

We know that a normal display screen consists of many points, which we call pixel. Scan is used for display: each time the electron gun scans a row from left to right, coloring each pixel, and then scanning several rows from top to bottom, it swept the screen. To prevent flickering, repeat the above process dozens of times per second. For example, we often say that the screen resolution is 640 × 480, And the refresh frequency is 70Hz, which means that each line needs to scan 640 pixels, a total of 480 rows, and the screen is repeatedly scanned 70 times per second.

We call this monitor a bit image device. A bitmap is a two-dimensional Pixel matrix, and a bitmap is an image that is displayed and stored using the bitmap method.

Let's talk about the three-element RGB concept.

We know that all colors in nature can be composed of red, green, blue (R, G, B. Some colors contain more red components, such as dark red; some contain less red components, such as light red. A total of 255 levels can be divided into 0 to 256 for the amount of Red ingredients, 0 indicates that the red ingredients are not included, and 255 indicates that the red ingredients contain 100%. Similarly, green and blue are also divided into 256 levels. This classification concept is called quantification.

In this way, according to the different combinations of red, green, and blue, we can represent 256x256x256, about 16 million colors. So many colors are rich enough for our human eyes.

Table 1.1
RGB of common colors
Combination Value

Color

R

G

B

Red

255

0

0

Blue

0

255

0

Green

0

0

255

Yellow

255

255

0

Purple

255

0

255

Qing

0

255

255

White

255

255

255

Black

0

0

0

Gray

128

128

128

As you may have understood, when each pixel in a graph has a different RGB value, the color will be colorful. This is true, but there are still some differences in practice.

Let's take a look at the example below.

There is a color map with a length and width of 200 pixels and 16 colors. Each pixel is represented by three components: R, G, and B. Because each component has 256 levels, it must be expressed in 8 bits, that is, one byte. Therefore, each pixel needs 3 bytes. The entire image should be 200x200x3, about KB, not a small number! If we use the following method, we can save a lot of effort.

Because it is a 16-color graph, that is to say, this graph has only 16 colors at most, we can use a table: each row in the table records the R, G, and B values of a color. In this way, when we represent the color of a pixel, we only need to point out the number of rows in which the color is indexed in the table. For example, if the table's 0th behavior is, 0, 0 (red), when a pixel is red, you only need to indicate 0.

Let's calculate it again: 16 states can be expressed in 4 bits, so a pixel must be half a byte. The entire image should be 200x200x0.5, about 20 K bytes, plus 3x16 = 48 bytes in the preceding table. the total number of bytes occupied is about 1/6 of the previous one, saving a lot of time?

This R, G, and B table is what we often call the palette. Another name is the color search table LUT (look
Up Table), it seems more accurate. The color palette technology is used in Windows bitmap. In fact, not only Windows bitmap, but also many image file formats such as PCX, Tif, and GIF are used. Therefore, it is very useful to grasp the concept of the color palette.

There is a picture in which the number of colors is as high as 256 × 256 × 256, that is to say, it contains all the colors in the R, G, and B Color Representation Methods We mentioned above, this is called a true color chart (true
Color ). A true color chart does not mean that a picture contains all colors, but that it has the ability to display all colors, that is, it can contain at most all colors. When representing a true color chart, each pixel is directly represented by three sub-Bytes: R, G, and B, instead of the color palette technology. The reason is obvious: if you use a color palette, it means that a pixel also needs to use 24 bits, because each color index needs to use 24 bits (because there are 224 colors in total, that is, the palette has 224 rows ), it is the same as the number of bytes expressed by using the R, G, and B components. It is not only inexpensive, but also a large palette of 256 × 256 × 256 × 3 bytes. Therefore, a true color chart is represented by three components: R, G, and B. It is also called a 24-bit color chart.

Ii. BMP file format
This section introduces the concept of a location map and a color palette. Next, let's look at the format of the Windows location file (.bmp. The BMP file is roughly divided into four parts.
The first part is bitmapfileheader, a structure, which is defined as follows:
Typedef struct tagbitmapfileheader {
Word bftype;
DWORD bfsize;
Word bfreserved1;
Word bfreserved2;
DWORD bfoffbits;
} Bitmapfileheader;
The length of this structure is fixed and 14 bytes (word is a 16-bit integer without a symbol, DWORD is a 32-bit integer without a symbol). The fields are described as follows:
Bftype
Specifies the file type, which must be 0x0000d, that is, the string "BM". Also, the first two bytes of all. BMP files are "BM"
Bfsize
Specify the file size, including the 14 bytes
Bfreserved1, bfreserved2
Reserved Words, no need to consider
Bfoffbits
It is the number of offset bytes from the file header to the actual bitmap data, that is, the sum of the length of the first three parts in figure 3.
The second part is bitmapinfoheader, which is also a structure. Its definition is as follows:
Typedef struct tagbitmapinfoheader {
DWORD bisize;
Long biwidth;
Long biheight;
Word biplanes;
Word bibitcount;
DWORD bicompression;
DWORD bisizeimage;
Long bixpelspermeter;
Long biypelspermeter;
DWORD biclrused;
DWORD biclrimportant;
} Bitmapinfoheader;
The length of this structure is fixed and 40 bytes (word is a 16-bit integer without symbols, DWORD is a 32-bit integer without symbols, and long is a 32-bit integer ), the fields are described as follows:
Bisize
Specify the length of this structure, which is 40
Biwidth
Specify the image width in pixels.
Biheight
Specifies the Image Height, in pixels.
Biplanes
It must be 1.
Bibitcount
The sequence format supports 32-bit colors, which will not be discussed here ).

Bicompression
Specifies whether the bitmap is compressed. Valid values include bi_rgb, bi_rle8, bi_rle4, and bi_bitfields (all constants defined by windows ). It should be noted that the Windows bitmap can adopt the rle4 and rle8 compression formats, but not much is used. We will discuss only the first case of no compression, that is, the case where bicompression is bi_rgb.

Bisizeimage
The number of bytes occupied by the actual bitmap data can also be calculated from the following formula:
Bisizeimage = biwidth' * biheight
Note that the biwidth' in the above formula must be an integer multiple of 4 (so it is not biwidth, but biwidth', indicating the integer multiple closest to 4, which is greater than or equal to biwidth. For example, if biwidth = 240, biwidth' = 240; If biwidth = 241, biwidth' = 244) If bicompression is bi_rgb, the item may be zero.

Bixpelspermeter
Specify the horizontal resolution of the target device. The unit is the number of pixels per meter. For details about the definition of resolution, see the print section.
Biypelspermeter
Specifies the vertical resolution of the target device, in the same unit as above.
Biclrused
Specify the number of colors actually used by the image. If the value is zero, the number of colors used is 2 to the power of ititcount.
Biclrimportant
Specify the number of important colors in the image. If the value is zero, all colors are considered important.
The third part is the palette. Of course, this is for bitmap files that require the palette. Some bitmaps, such as true color charts, have been mentioned earlier, do not need a color palette. bitmapinfoheader is directly followed by bitmap data. The color palette is actually an array with a total of biclrused elements (if the value is zero, there will be 2 bibitcount to the power of the element ). The type of each element in the array is an rgbquad structure, which occupies 4 bytes. Its definition is as follows:

Typedef struct tagrgbquad {
Byte rgbblue; // The Blue weight of the color.
Byte rgbgreen; // The green weight of the color.
Byte rgbred; // The Red weight of the color.
Byte rgbreserved; // reserved value
} Rgbquad;
The fourth part is the actual image data. For bitmap that uses the color palette, the image data is the index value of the pixel appearance in the color palette. For a true color chart, the image data is the actual R, G, and B values. The following describes two-color, 16-color, 256-color bitmap, and true-color bitmap.

(1) For a two-color bitmap, the color of the pixel can be expressed with one digit (generally 0 indicates black, and 1 indicates white), so one byte can represent 8 pixels.
(2) For a 16-color bitmap, four digits can represent the color of one pixel, so one byte can represent two pixels.
(3) For a 256-color bitmap, one byte can represent exactly one pixel.

(4) For a high-color bitmap, two bytes represent 1 pixel, indicating 565 (RGB), 4444 (rgba) 555 (RGB, the highest bit is retained ), for storage, a proper shift operation is required. Five BITs indicate the colors that 8 bits can represent, which may cause distortion. However, such distortion has little impact on human vision, therefore, games, application software, and graphics processing are widely used. Although they are complicated to use, this is equivalent to an approximate value of 26/256 (the 8th power of 2) (R) it is about 10 percent of the red, it also means that about 10 percent of the red can also use 3/32 (2 to the 5 power) (R ).

(5) For a true color chart, three bytes can represent 1 pixel.
Pay attention to the following two points:
1. The number of bytes in each row must be an integral multiple of 4. If not, you need to complete it. This is already mentioned in the previous section of bisizeimage.
2. In general, the data in the. BMP file is from bottom to top, left to right. That is to say, what is first read from the file is the first pixel on the left of the bottom line of the image, then the second pixel on the left... Next is the first pixel on the left and the second pixel on the left... And so on. The last result is the rightmost pixel of the top row.

3. Bitmap each pixel occupies a different number of digits than the color number of the bitmap.

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.