DIB (Device-Independent Bitmap, ie equipment is unacceptable) small knowledge
Introduction
There are two types of bitmaps, namely: Device-related bitmap (DDB), and devices have no bitmap (DIB). DDB bitmaps are very common in early Windows systems (previously before Windows 3.0), in fact it is also unique. However, with the progress of the display manufacturing technology, some inherent problems of the DDB bitmap are beginning to emerge, and the bitblt () function is based on the DDB bitmap. For example, it is not able to store (or acquisition) to create the resolution of the original device of this picture, so that the application cannot quickly determine if the client's display device is suitable for displaying this picture. In order to solve this problem, Microsoft creates a DIB bitmap format.
The BMP file is a universal file format for Windows saved images, accounting for important status in digital image processing. The image data saved in the BMP file is a DIB (Device-Independent Bitmap, ie equipment-independent bitmap), DIB is a standard Windows bitmap format, which comes with color information, so the palette management is very easy.
BMP file structure
The DIB bitmap contains the following colors and sizes:
* Color format of the original device (ie, the device created by the image).
* The resolution of the original device.
* Palette of the original device
* A bit array, three values from red, green, and blue (RGB) represent a pixel.
* A array compression flag is used to indicate the compression scheme of the data (if needed).
The above information is saved in the BitmapInfo structure, which is composed of BitmapInfoHeader structure and two or more RGBQUAD structures. The members of the BitMapInfoheader structure contains the size of the image, the color format of the original device, and information such as data compression scheme. The RGBQUAD structure identifies the color data used in pixels.
The composition of the bitmap file structure name symbol bitmap file header (Bitmap-file header)
BitmapfileHeader
BMFH
Bitmap-Information Header
BitmapInfoHeader
BMIH
Color Table
RGBQUAD
Acolors []
Image data array bytes
Byte
Abitmapbits []
Bitmap file formal structure
Name size content image file header 0000H
File identification
2 bytes
Two-byte content is used to identify the type of bitmap: 'BM': Windows 3.1x, 95, NT, ... 'BA': OS / 2 Bitmap Array 'Ci': OS / 2 Color Icon 'CP': OS / 2 Color Pointer 'IC': OS / 2 icon 'PT': OS / 2 Pointer Note: Because the OS / 2 system is not popular, you only need to judge the first identification "BM" when programming.
0002H
FILE SIZE
1 DWORD
The size of the entire file represented by bytes
0006H
RESERVED
1 DWORD
Reserved, must be set to 0
000ah
Bitmap Data Offset
1 DWORD
Offset from the data between the file to the start of the bitmap data (Bitmap Data)
Bitmap information head
000EH
Bitmap header size1 dword
The length of the bitmap info header is used to describe the color, compression method, and the like of the bitmap. The following length is represented: 28H - Windows 3.1x, 95, NT, ... 0CH - OS / 2 1.x F0H - OS / 2 2.X Note: In the Windows 95, 98, 2000 and other operating systems, bitmap information head The length is not necessarily 28h because Microsoft has developed a new BMP file format, where the information head structure changes are relatively large, and the length is long. Therefore, it is best not to use the constant 28h directly, but should read this value from a specific file. This ensures compatibility of the program.
0012H
Width
1 DWORD
The width of the bitmap, in pixels
0016h
HEIGHT
1 DWORD
The height of the bitmap, in pixels
001ah
Planes
1 word
The number of bitmaps (Note: This value will always be 1)
001ch
Bits Per Pixel
1 word
Bits 1 - Monochrome bitmap (actually there can be black and white by default) 4 - 16 colors in the default, you can define these two colors) 4 - 16 colors Figure 8 - 256 colors Bitchart 16 - 16bit High Color Bit Figure 24 - 24bit Real Color Bit Figure 32 - 32Bit Enhanced Truth Color Bit Map
001EH
Compression
1 DWORD
Compression Description: 0 - Do not compress (expressed using Bi_RGB) 1 - RLE 8 - Use the 8-bit RLE compression mode (represented by bi_rle8) 2 - RLE 4 - Use a BI_RLE4 represented by bi_rle4) 3 - bitfields-bit field Storage mode (represented by bi_bitfields)
0022h
Bitmap Data Size
1 DWORD
The size of bitmap data represented by byte numbers. This number must be a multiple of 4
0026h
HRESolution
1 DWORD
Horizontal resolution represented by pixels / m
002ah
Vresolution
1 DWORD
Vertical resolution represented by pixels / m
002EH
Colors
1 DWORD
The number of colors used by bitmaps. If 8-bit / pixels are expressed as 100 h or 256.
0032h
Important Colors
1 DWORD
Specify the number of important colors. When the value of this domain is equal to the number of colors (or equal to 0), it means that all colors is as important.
Palette data
Different according to the version of the BMP version
Palette
N * 4 byte
Palette specification. For each entry in the palette, these four bytes describe the value of the RGB with the following method:
1 byte for blue components
1 byte for green components
1 byte for red components
1 byte is used for fillers (set to 0)
Image data
Different depending on the BMP version and the dimension of the palette
Bitmap Data
XXX Bytes
The size of this domain depends on the compression method and the size of the image, and the bit depth of the image, which contains all bitmap data bytes, which may be the index number of the color palette, or the actual RGB value, this will Determine according to the position depth in the image information header.
Bitmap file program structure
BitmapfileHeader
Bitmap file header
(Only for BMP files)
BFTYPE = "BM" (0x4D42)
BFSIZE
BFRESERVED1
BFRESERVED2
Bfoffbits
BitmapInfoHeader
Bitmap information head
Bisize
BiWidth
Biheight
BIPLANCE
Bibitcount
Bicompression
BisizeImage
Bixpelspermeter
Biypelspermeter
Biclrused
BICLRIMPORTANT
Palette
Palette
Monochrome DIB has 2 entry
16-color DIB has 16 entry or fewer
256 color DIB has 256 entry or fewer
Real color DIB no palette
DIB Pixels
DIB image data
Pixel is arranged in the order of each column per sum
The number of bytes per line must be 4 inexposers
Bitmap reading
Write the general process of writing Bitmap:
1: State BitmapFileHeader and clear the structure:
BitmapfileHeader Bfh;
MEMSET (& BFH, 0, SIZEOF (BFH));
2: Initialize this structure:
BFN.BFTYPE = 'MB'; // bitmap
/ / Describe the size of the file, CBBuffer is the size of bitmap data.
Bfn.bfsize = sizeof (bfn) Cbbuffer Sizeof (BitmapInfoHead);
/ / Describe the offset of bitmap file data across the entire bitmap file, that is, where data starts
Bfn.bfoffbits = sizeof (bitmapinirter) sizeof (BitmapfileHead);
3: Write the BFN into the file
4: State BitmapInfoHeader and clear the structure:
BitmapInfoheader Bih;
MEMSET (& BiH, 0, Sizeof (BIH));
5: Initialize this structure:
Bih.bisize = sizeof (bih);
Bih.biWidth = BIWIDTH; // Bit map of the width
Bih.biheight = Biheight; // bit map height
BIH.BIPLANES = BIPLANES; // bitmap part number
BIH.BIBITCOUNT = BIBITCOUNT; // bitmap
6: Write BIH into the file
7: Finally, write data.
Read bitmap: (pseudo code)
IF Open Bitmap File
Read Two bytes (TYPE) And if Different Than 0x4d42 Stop
Ignore Eight Bytes
Read FourTes (Start of Image Data)
Ignore Four Bytes
Read FourTes (Width of Bitmap)
Read FourTes (Height of Bitmap)
Ignore Two Bytes
Read Two Bytes (Bit Count of Bitmap) And if Different Than 24 Stop
Read FourTes (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 For)
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