How to process BMP bitmap in Linux

Source: Internet
Author: User
Microsoft Windows
The paintbrush tool in 3.0 provides users with powerful graphic rendering and editing functions, such as rotating, scaling, and splicing. [1] these functions can be used to easily generate various complex color images. the generated image is stored on the disk in the format of a bitmap file. this file is used as a resource in Mircosoft
With the support of Windows 3.0, the image can be reproduced on any device as needed [2].

In actual work, I often encounter the following problem: Can I remove Windows support and use these images in other systems after I use the paint brush tool to generate the desired image? The key to solving this problem is to understand the record format of the bitmap file. This problem is easy to solve as long as you understand the record format of the bitmap file. For this purpose, the text will introduce Mircosoft in detail.
The record format of the Windows 3.0 bitmap file.

A Microsoft Windows 3.0 bitmap file (with the. BMP extension) consists of the following three parts:

* Bitmapfileheader Data Structure

* Bitmapinfo Data Structure

* Bitmap Array

1. Structure of the bitmap file header

The data structure of the bitmap file header contains information such as the type, size, and print format of the bitmap file. It is defined in windows. h:

Typedef struct tagbitmapfileheader

{

Word bftype;/* the type of the bitmap file, which must be BM .*/

DWORD bfsize;/* the size of the bitmap file, in bytes .*/

Word bfreserved1;/* Reserved Words for bitmap files, which must be 0 .*/

Word bfreserved2;/* Reserved Words for bitmap files, which must be 0 .*/

DWORD bfoffbits;/* start position of the bitmap array, relative to the bitmap file */

/* Header offset, in bytes .*/

} Bitmapfileheader;

Ii. Structure of Bitmap Information

The bitmap data structure contains the size, color, and other information of the bitmap file. It is defined in windows. h:
Typedef stuc tagbitmapinfo

{

Bitmapinfoheader bmiheader;

Rgbquad bmicolor [];

} Bitmapinfo;

1. The bmiheader is a data structure of the bimmapinfoheader type, used to describe the size of the bitmap.
Definition of bitmapinfoheader:

Typedef struct tagbitmapinfoheader

{

DWORD bisize;/* the length of the bmiheader, in bytes .*/

DWORD biwidth;/* the bitmap width, in pixels .*/

DWORD bihight;/* the height of the bitmap, in pixels .*/

Word biplanes;/* target device level, which must be 1 .*/

Word bibitcount;/* The number of digits required for each pixel, which must be 1 (monochrome ).*/

/* 4 (16 colors), 8 (256 colors), or 24 (2 ^ 24 colors .*/

DWORD bicompress;/* the bitmap compression type, which must be 0 (not compressed ).*/

/* 1 (bi_rle8 compression type) or 2 (bi_rle4 compression type .*/

DWORD bisizeimage;/* the bitmap size, in bytes .*/

Dowrd bixpeispermeter;
/* Horizontal resolution of the bitmap target device, in the unit of prime number of each meter .*/

DWORD biypeispermeter;
/* Horizontal resolution of the bitmap target device, in the unit of prime number of each meter .*/

DWORD bicirused;
/* The number of addresses in the color table used by the bitmap. For details, see [3]. */

DWORD bicirimprotant;
/* Number of addresses that are considered to be important colors in the bitmap display process. For details, see [3] */

} Bitmapinfoheader;

2.
Bimcolor [] is a color table used to describe the color in the bitmap. it has several table items, each of which is an rgbquad structure and defines a color. rgbquad definition:

Typedef tagrgbquad

{

Byte rgbblue;

Byte rgbgreen;

Byte rgbred;

Byte rgbreserved;

} Rgbquad;

In the color defined by rgbquad, the blue brightness is determined by rgbblue, the green brightness is determined by rgbgreen, the red brightness is determined by rgbred, And the rgbrserved must be 0.

For example, if a table item is, 00, FF, 00, the color is pure red.

Bimcolor [] The number of table items is determined by bmbitcount:

When bmbitcount is 1, 4, 8,
Bimcolor [] has 2, 16, and 256 Items respectively. If the pixel value of a point is N, the color of the pixel is the color defined by bimcolor.

When bmbitcount = 24,
The table item of bimcolor [] is null. each three bytes of the bitmap array represents one pixel. These three bytes directly define the relative brightness of blue, green, and red in the pixel color, thus eliminating the need for the bimcolor [] color table.

3. Structure of the bitmap Array

The bitmap Array records each pixel value of the bitmap. when a bitmap file is generated, Windows scans the bitmap line by line from the lower left corner of the bitmap (that is, from left to right to bottom) and records the bitmap pixel values one by one. the bytes of these recorded pixel values form a bitmap array.

Bitmap Arrays can be compressed or uncompressed.

1. Non-compressed format

In a non-compressed format, the pixel value of each bitmap point corresponds to several bits of the bitmap array. The size of the bitmap array is determined by the bitmap width, the height and bitbitmap color count are determined.

(1) Relationship between bitmap row scan and bitmap Array

If the pixel value of A scanned row needs to be n Bytes, then:

Bitmap array 0 ~ N-1 bytes record the pixel value of the first row of the bitmap. n ~ of the bitmap Array ~ The 2n-1 bytes record the pixel value of the second row of the bitmap. Likewise, m-1) x n ~ of the bitmap Array ~ M * n-1 bytes record the pixel value of the M-th scanned row of the bitmap. The size of the bitmap array is N * bihight.

When (biwith * bitbitcount) mod32 = 0:

N = (biwith * bibitcount)/8

When (biwith * bitbitcount) mod32! = 0:

N = (biwith * bibitcount)/8 + 4

In the above formula, the reason for the addition of 4 rather than 1 is to make the pixel value of A scan row occupy a multiple of the number of bytes of the bitmap array (Windows requires that it end at the long boundary ), fill the missing bits with 0.

(2) Relationship between the bitmap pixel value and the bitmap array (in the M-scan behavior example)

Set the n Bytes of the pixel value of the M-th scanned row to A0, A1, A2,..., respectively. Then:

When bitbitcount = 1: The D7 bits of A0 record the 1st pixel value of the M-th scanned row of the bitmap, the D6 bit records the 1st pixel value of the M-th scanned row of the bitmap ,...,
D0 records the 8th pixel value of the M-th scanned row of the bitmap,
The D7 of A1 records the 9th pixel values of the M-th scanned row of the bitmap, And the D6 records the 10th pixel values of the M-th scanned row of the bitmap ,...

When bitbitcount = 4: A0's D7-D4 bit records the 1st pixels of the M-th row of the bitmap, the D3-D0 bit records the 2nd pixels of the M-th scanned row of the bitmap,
The D7-D4 bit of A1 records the 3rd pixel value of the M-th scanned row of the bitmap ,...

When bitbitcount = 8: The D7-D4 bit of A0 records the 1st pixels of the M-th scanned row of the bitmap, a1 records the 2nd pixel value of the M-th scanned row of the bitmap ,...

When bitbitcount = 24: A0, A1, and A2 bits record the 1st pixel values of the M-th row of the bitmap, A3, A4, a5 records the 2nd pixel value of the M-th scanned row of the bitmap ,...

The relationship between the pixel values of other bitmap scan rows and the bitmap array is similar.

2. compression format

Windows supports bi_rle8 and bi_rle4 compressed bitmap storage formats, reducing the disk space occupied by the bitmap array.

(1) bi_rle8 compression format

When bicompression = 1, bitmap files use this compression encoding format. the compression encoding is based on two bytes. the first byte specifies the number of consecutive pixels repeatedly drawn by the color specified by two bytes.

For example, compression encoding 05
04 indicates that five pixels are displayed consecutively from the current position. The values of these five pixels are 04.

When the first byte is zero, the second byte has a special meaning:

0: End of row

1: End of the image

2:
Escape the following two bytes. Use these two bytes to indicate the horizontal and vertical displacement of the next pixel starting from the current position.

N (0x03 <n <xff): escape the next n Bytes. The next n pixels are drawn by the color specified by the n Bytes. note: In actual encoding, you must ensure that the number of subsequent bytes is a multiple of 4. fill in the missing bits with 0.

For example, the compression code 00 indicates that the new scan line is started, and the compression code 00
01 indicates the end of the compressed bitmap array. The compression encoding is 00 02 05.
01 indicates to move 5 pixels to the right from the current position, move one row down, and then draw the next pixel. The compression code is 00 03.
05 06 07
00 indicates that three pixels are consecutively drawn from the current position. The colors of the three pixels are, 06, and 07 respectively. The last 00 is to ensure that the number of escaped bytes is a multiple of 4.

(2) bi_rle4 compression format

When bicompression is 4, bitmap files are compressed later. the compression format of bi_rle4 is similar to that of bi_rle8. The difference between Dimension 1 and Dimension 1 is that one byte of bi_rle4 contains two pixels of color.

For continuous display, the first pixel is drawn in the color specified by the four-byte height, and the second pixel is drawn in the color specified by the byte low level, the third pixel is drawn in the color specified by the byte four-digit height ,..., until all pixels are drawn.

Example: Compression Code 06
67 indicates that five pixels are consecutively drawn from the current position. The colors of the five pixels are 6, 7, 6, and 6, respectively.

Compression Code 00 04 45 67 00 indicates that four pixels are consecutively drawn from the current position,
The color of the Four pixels is 4, 5, 6, and 7 respectively. The last 00 is to ensure that the number of escaped bytes is a multiple of 4.

Iv. instance analysis

In order to clearly describe the format of the bitmap file, the following analyzes the bitmap file that stores a simple area (as shown in the figure. the background color is blue, the color of the above line is yellow, and the color of the following line is red. the bitmap is stored as a 16-color image [1] and the file name is demo. BMP. use DEBUG to analyze the content of the bitmap file as follows:

186c: 0100 42 4D 3E 01 00 00 00 00-00 00 76 00 00 00 28 00

186c: 0110 00 00 00 26 00 00 00 0a 00-00 00 01 00 04 00 00 00

186c: 0120 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 00

186c: 0130 00 00 00 00 00 00 00-00 00 00 00 80 00 00 80

186c: 0140 00 00 00 80 00 80 00-00 00 80 00 80 00 80 80

186c: 0150 00 00 80 80 00 40 40-40 00 00 00 ff 00 00 FF

186c: 0160 00 00 00 FF 00 ff 00-00 00 ff 00 ff 00 ff 00 FF
FF

186c: 0170 00 00 FF 00 CC CC-CC CC

186c: 0180 CC CC-CC 00 CC

186c: 0190 CC CC-CC CC 99

186c: 01a0 99 99 99 99 99 99-99 99 99 99 99 99 99 99 99 CC

186c: 01b0 CC 00 CC CC-CC CC

186c: 01c0 CC 00 CC CC-CC CC

186c: 01d0 CC CC-CC 00 cc cb bb bb

186c: 01e0 BB BB-BB bb bc cc 00 CC

186c: 01f0 CC CC-CC CC

186c: 0200 CC 00 CC CC-CC CC

186c: 0210 CC 00 CC CC-CC CC

186c: 0220 CC CC-CC 00 CC

186c: 0230 CC CC-CC 00 CC 00 00

(0, 0)

┌ ── ─ ┐

│ (3, 4) (yellow) (35, 4) │

│ _____________________ │

│ () (Red) () │

│ _____________________ │

└ ── ─ ┘

(38,10)

Attached Image

Where:

0100-010d is the data structure of bitmapfileheader, which describes the following information: A total of 318b bitmap files
(Bfsize = 0x013e), the bitmap array starts from 0176 (bfoffbits = 0x76 ).

010e-0135 is the data structure of bitmapfileheader, which describes the following information: bitmapfileheader is a total of 28B (bisize = 0x28), the bitmap size is 38X10 (biwith00x26,
Bihight = 0x0a, in pixels)

Each four bits of the bitmap array represent a pixel. The bitmap has 16 colors,
Bmicolor [] has 16 table items (bitbitcount = 4), and bitmap adopts non-Compressed Storage (bitcompression = 0 );

0136-0175 is the bmicolor [] color table. Each four bytes define a color. A total of 64 bits are defined and 16 colors are defined. the ninth table item (015a-015d) defines red, 11th table items (0162-0165) defines yellow, and 12th table items (0166-0169) defines blue.

0176-023d is a bitmap array. Each scan row of a bitmap has 38 pixels, corresponding to 20 bytes in the bitmap array. The value of 20th bytes is 0 to ensure that the number of bytes is a multiple of 4. the yellow line is scanned in the 6th rows of the bitmap. This line corresponds to the 01da-01ed byte of the bitmap array, and the red line is scanned in the 3rd rows of the bitmap array. This line corresponds to the 0192-01b1 byte of the bitmap array. There are 10 rows.

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.