Dib (device-independent Bitmap) device-independent bitmap

Source: Internet
Author: User

Bitmap has two types: Device-related Bitmap (DDB) and device-independent Bitmap (DIB ). DDB bitmap is common in earlier Windows systems (before Windows 3.0). In fact, it is unique. However, with the development of the display manufacturing technology and the diversity of display devices, some inherent problems of DDB bitmap began to emerge. functions such as bitblt () are based on DDB bitmap. For example, it cannot store (or obtain) the resolution of the original device that creates the image, so that the application cannot quickly determine whether the display device of the client is suitable for displaying the image. To solve this problem, Microsoft has created a DIB bitmap format.
 
BMP files are a common file format for storing images in windows and play an important role in digital image processing. The image data stored in the BMP file is a Dib (device-independent bitmap, that is, device-independent Bitmap). DiB is a standard Windows bitmap format, and it comes with color information, therefore, it is very easy to manage the palette.
 
BMP file structure
 
The DIB bitmap contains the following color and size information:
* The color format of the original device (that is, the device that creates an image.
* Resolution of the original device.
* Palette of the original device
* A single-digit Group, represented by Red, green, and blue (RGB) values.
* An array compression flag is used to indicate the data compression scheme (if needed ).
The above information is stored in the bitmapinfo structure, which consists of the bitmapinfoheader structure and two or more rgbquad structures. The members in the bitmapinfoheader structure indicate the image size, the color format of the original device, and the data compression scheme. The rgbquad structure identifies the color data used by pixels.
 
Name and symbol of the structure of the bitmap file
Bitmap-File Header (Bitmap-file header) bitmapfileheaderbmfh
Bitmap-Information header (Bitmap-Information header) bitmapinfoheaderbmih
Color Table rgbquadacolors []
Byteabitmapbits []

 
Formal Structure of Bitmap files
Content of the name of the Offset Field
 
 
 
Image files
The two-byte content of the header 0000h file is used to identify the bitmap type:
'Bm ': Windows 3.1x, 95, NT ,...
'Ba': OS/2 bitmap Array
'Cies': OS/2 color icon
'Cp': OS/2 color pointer
'Ic ': OS/2 icon
'Pt': OS/2 pointer
Note: Because OS/2 systems are not widely used, you only need to determine the first "BM" in programming.
0002 hfile size1 DWORD size of the entire file in bytes
0006hreserved1 DWORD reserved, must be set to 0
000 ahbitmap data offset1 DWORD offset between data (bitmap data) starting from when the file is in place
The length of the bitmap info header, used to describe the color and compression method of the bitmap. The following length:
28 h-Windows 3.1x, 95, NT ,...
0ch-OS/2 1.x
F0h-OS/2 2.x
Note: in Windows 95, 98, 2000, and other operating systems, the length of the Bitmap header is not necessarily 28 h, because Microsoft has developed a new BMP file format, the structure of the information header changes greatly and the length is extended. Therefore, it is best not to directly use the constant 28 h, but to read the value from a specific file. This ensures program compatibility.
Width of the 0012hwidth1 DWORD bitmap, in pixels
Height of the 0016hheight1 DWORD bitmap, in pixels
001ahplanes1 bit number of word Bitmap (Note: This value will always be 1)
001 chbits per pixel1 word number of digits per pixel
1-monochrome Bitmap (in fact, there are two colors, black and white by default. You can define these two colors by yourself)
4-16 color bitmap
8-256-color bitmap
16-16 bit high-color bitmap
24-24bit true color bitmap
32-32bit enhanced true color bitmap
001ehcompression1 DWORD compression description:
0-no compression (in bi_rgb format)
1-RLE 8-use the 8-bit RLE compression method (represented by bi_rle8)
2-RLE 4-use the 4-bit RLE compression method (represented by bi_rle4)
3-bitfields-bit domain storage method (represented by bi_bitfields)
0022 hbitmap data size1 DWORD the size of the bitmap data expressed by the number of cells. The value must be a multiple of 4.
0026hhresolution1 DWORD horizontal resolution in pixels/meters
002ahvresolution1 DWORD vertical resolution in pixels/meters
The number of colors used by the DWORD bitmap of 002ehcolors1. For example, 8-bit/pixel represents 256 h or.
0032 himportant colors1 DWORD specifies the number of important colors. When the value of this field is equal to the number of colors (or equal to 0), it indicates that all colors are equally important.
The palette data varies with the paletten * 4 byte palette specification based on the BMP version. For each table item in the color palette, use the following methods to describe the values of RGB:
1 byte for the blue component
1 byte for green component
1 byte for red weight
1 byte for padding (set to 0)

The size of Bitmap dataxxx bytes depends on the compression method, image size, and image bit depth. It contains all bitmap data bytes, the data may be the index number of the color palette or the actual RGB value, which is determined based on the bit depth value in the Image Information header.

 
 
Program Structure of Bitmap files
Bitmapfileheader
Bitmap File Header
(Only for BMP files)
Bftype = "BM" (0x4d42)
Bfsize
Bfreserved1
Bfreserved2
Bfoffbits
Bitmapinfoheader
Bitmap header
Bisize
Biwidth
Biheight
Biplance
Bibitcount
Bicompression
Bisizeimage
Bixpelspermeter
Biypelspermeter
Biclrused
Biclrimportant
Palette
Color palette
The monochrome Dib has two table items.
16-color Dib has 16 table items or fewer
256-color Dib has 256 table items or fewer
True Color Dib no color palette
DiB pixels
DIB image data
Pixels are arranged in the order of each column in each row.
The number of bytes in each row must be an integer multiple of 4.
 
Bitmap read/write
 
The general process of writing bitmap:
1: declare bitmapfileheader and clear the structure:
Bitmapfileheader BFH;
Memset (& BFH, 0, sizeof (BFH ));
2: Initialize the structure:
Bfn. bftype = 'mb'; // bitmap
// Specifies the file size. cbbuffer is the size of bitmap data.
Bfn. bfsize = sizeof (bfn) + cbbuffer + sizeof (bitmapinfoheader );
// Describe the offset of the bitmap file data in the entire bitmap file, that is, where the data starts
Bfn. bfoffbits = sizeof (bitmapinforheader) + sizeof (bitmapfileheader );
3: Write bfn to a file
4: declare bitmapinfoheader and clear the structure:
Bitmapinfoheader BiH;
Memset (& BiH, 0, sizeof (BiH ));
5: Initialize the structure:
BiH. bisize = sizeof (BiH );
BiH. biwidth = biwidth; // The width of the bitmap.
BiH. biheight = biheight; // The height of the bitmap.
BiH. biplanes = biplanes; // bitmap bit number
BiH. bibitcount = bibitcount; // bitmap color depth
6. Write BiH to a file
7: Write Data at last.
 
Read bitmap: (pseudo code)
If open bitmap file
Read two bytes (type) and if different than 0x4d42 stop
Ignore eight bytes
Read four bytes (start of image data)
Ignore four bytes
Read four bytes (width of Bitmap)
Read four bytes (height of Bitmap)
Ignore two bytes
Read two bytes (bit count of Bitmap) and if different than 24 stop
Read four bytes (compression of Bitmap) and if different than bi_rgb stop
Move to start of image data
Allocate memory for image data (3 (one byte for red, other
Green other for blue) * imagewidth * imageheight)
Read (3 * imagewidth * imageheight) bytes from file to buffer
Swap the red and blue components of Buffer
If imageheight is negative
Flip the buffer lines
End if
Close file

 

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.