About the Image Storage format (introduction of BMP format) _ Related skills

Source: Internet
Author: User
Tags bmp image reserved

Bmp

BMP (full name bitmap) is a standard image file format in the window operating system

Can be divided into two categories: device-related bitmaps (DDB) and device-independent bitmaps (DIB), which are very widely used.

It uses bit-mapped storage format, in addition to image depth optional, no other compression, therefore, BMP files occupy a large space. BMP file image depth can be selected Lbit, 4bit, 8bit and 24bit. BMP files when storing data, the image is scanned in the order from left to right, from bottom to top. Because the BMP file format is a standard for exchanging diagram-related data in a Windows environment, graphical image software running in a Windows environment supports BMP image formats.

Composition

A typical BMP image file consists of four parts:

1: Bitmap header file data structure, it contains BMP image file type, display content and other information;
2: Bitmap information data structure, it contains a BMP image of the wide, high, compression methods, as well as the definition of color and other information;
3: Palette, this part is optional, some bitmaps need palettes, some bitmaps, such as True Color map (24-bit BMP) do not need a palette;
4: Bitmap data, this part of the content according to the BMP bitmap used by the number of different digits, in the 24-bit diagram directly using RGB, while the other less than 24-bit using the color index of the palette.

The corresponding data structure

1:bmp file composition
BMP file consists of file header, bitmap information header, color information and graphic data four parts.

The

2:bmp header (14 bytes)
BMP file header data structure contains information such as the type of BMP file, file size, and bitmap start position.
Its structure is defined as follows:
typedef struct TAGBITMAPFILEHEADER
{
WORD bftype;//bitmap file type, must be BM (1-2 bytes)
DWORD BF Size; The size of the bitmap file, in bytes (3-6 bytes)
Word bfReserved1;//bitmap file reserved word, must be 0 (7-8 bytes)
Word bfReserved2//bitmap file reserved word, must be 0 (9-10 bytes)
DWORD Bfoffbits; The starting position of the bitmap data, expressed as an offset from the bitmap (11-14-byte)
//header, in bytes
Bitmapfileheader


3: Bitmap information Header (40 bytes)
BMP Bitmap Information header data is used to describe information such as the size of bitmaps.
typedef struct tagbitmapinfoheader{
DWORD bisize//This structure occupies a number of bytes (15-18 bytes)
LONG biwidth;//bitmap width, in pixels (1 9-22 bytes
LONG biheight;//bitmap height, in pixels (23-26 bytes)
Word biplanes;//target device level, must be 1 (27-28 bytes)
WORD bibitcount; The number of digits required per pixel, must be 1 (two-color), (29-30 bytes)
//4 (16 colors), 8 (256 color) or 24 (true color)
DWORD bicompression;//bitmap compression type, must be 0 (not compressed), (31-34 bytes
//1 (bi_rle8 compression type) or 2 (bi_rle4 compression type)
DWORD bisizeimage;//bitmap size, in bytes (35-38 bytes)
LONG bixpelspermeter; /Bitmap Horizontal resolution, number of pixels per meter (39-42 bytes)
LONG biypelspermeter//Bitmap vertical resolution, per pixel (43-46 bytes)
DWORD biclrused;//Bitmap actual color in the table used (47 -50 bytes)
DWORD biclrimportant;//Bitmap Display the number of important colors (51-54 bytes)
} bitmapinfoheader
 

4: Color Table
The color table is used to describe the color in a bitmap, it has several table entries, each table item is a RGBQUAD type structure, and defines a color. The RGBQUAD structure is defined as follows:
typedef struct TAGRGBQUAD {
The brightness of BYTE rgbblue;//Blue (value range is 0-255)
BYTE Rgbgreen; Green Brightness (value range is 0-255)
BYTE rgbred; Red Brightness (value range is 0-255)
BYTE rgbreserved;//reserved, must be 0
} Rgbquad;

The number of Rgbquad structure data in the color table is bibitcount to determine:
When bibitcount=1,4,8, there are 2,16,256 table items respectively;
When bibitcount=24, there are no color table entries.
The bitmap information header and color table Form the bitmap information, and the BITMAPINFO structure is defined as follows:
typedef struct TAGBITMAPINFO {
Bitmapinfoheader Bmiheader; Bitmap Information Header
Rgbquad Bmicolors[1]; Color table
} bitmapinfo;

5: Bitmap Data
Bitmap data records each pixel value of a bitmap, which is in the scanned row from left to right, and between scan rows from bottom to top. The number of bytes per pixel of a bitmap:
When bibitcount=1, 8 pixels account for 1 bytes;
When bibitcount=4, 2 pixels account for 1 bytes;
When bibitcount=8, 1 pixels account for 1 bytes;
When bibitcount=24, 1 pixels account for 3 bytes;
Windows stipulates that the number of bytes in a scan row must be
Multiples of 4 (that is, in long), with insufficient padding of 0,
biSizeImage = (((bi.biwidth * bi.bibitcount) + to) & ~31)/8 * bi.biheight;
Examples of specific data:
such as the beginning of a BMP file:
424D 4690 0000 0000 0000 4600 0000 2800 0000 8000 0000 9000 0000 0100*1000 0300 0000 0090 0000 A00F 0000 A00F 0000 0000 00 0000 0000*00f8 E007 1f00 0000*02f1 84f1 04f1 84f1 84f1 06f2 84f1 06f2 04f2 86f2 06f2 86f2 86f2 ...

File section

Image file Header
1) 1-2: (The numbers here represent "word", that is, two bytes, same below) image file header. 0x4d42= ' BM ' represents a BMP format supported by Windows. (Note: Check ASCII table B 0x42,m0x4d,bftype is two bytes, B is low byte, M is high byte so bftype=0x4d42, not 0x424d, but note)
2) 3-6: The entire file size. 4690 0000, for 00009046h=36934.
3) 7-8: Reserved, must be set to 0.
4) 9-10: Reserved, must be set to 0.
5) 11-14: Offset (14+40+4* (2^bibitcount) from the beginning of the file to the bitmap data. 4600 0000, for 00000046h=70, the top file header is 35 characters = 70 bytes.
Bitmap Information Header
6) 15-18: Bit chart information header length.
7) 19-22: bitmap width, in pixels. 8000 0000, for 00000080h=128.
8) 23-26: bitmap height, in pixels. 9000 0000, for 00000090h=144.
9) 27-28: The number of bits in the bitmap, which is always 1. 0100, for 0001h=1.
10) 29-30: The number of digits per pixel. There are 1 (monochrome), 4 (16 colors), 8 (256 colors), (64K color, high color), (16M color, True Color), (4096M-color, enhanced true color). 1000 for 0010h=16.
11) 31-34: Compression Note: There are 0 (not compressed), 1 (RLE 8, 8-bit RLE compression), 2 (RLE 4, 4-bit RLE compression, 3 (bitfields, bit-field storage). RLE simply uses pixels and pixel values to compress. T408 is a bit-domain storage method, with two bytes representing one pixel, and a bit field assigned to R5B6G5. The figure 0300 0000 is 00000003h=3.
12) 35-38: The size of the bitmap data in bytes, which must be a multiple of 4, equal to (multiples of the smallest 4 of the ≥ bitmap width) x bitmap height x per pixel number. 0090 0000 for 00009000h=80x90x2h=36864.
13) 39-42: Horizontal resolution expressed in pixels/meters. a00f 0000 is 0000 0fa0h=4000.
14) 43-46: The vertical resolution expressed in pixels/meters. a00f 0000 is 0000 0fa0h=4000.
15) 47-50: The number of color indices used by the bitmap. When set to 0, all palette entries are used.
16) 51-54: The number of color indices that have an important effect on the image display. If it's 0, that's important.
Color board
17) (55+0) to (50-1+2^bibitcount): Color Board specification. For each table item in the palette, use the following method to describe the value of RGB:
1 bytes for blue components
1 bytes for green components
1 bytes for red component
1 bytes for padding (set to 0)
Color boards are not used for 24-bit true color images because the RGB values in the bitmap represent the color of each pixel.
For example, color plate for 00f8 0000 E007 0000 1f00 0000 0000 0000, of which:
00f8 is f800h = 1111 1000 0000 0000 (binary), which is a mask of blue components.
E007 is 07e0h = 0000 0111 1110 0000 (binary), a mask of green components.
The 1f00 is 001Fh = 0000 0000 0001 [1]1111 (binary), which is a mask of red components.
0000 total set to 0.

The color component value can be obtained by the "and" operation of the mask and the pixel value and then the shift operation. Look at the mask. In fact, in two bytes of 16 bits per pixel value, 5, 6, and 5 bits, from highest to lowest, are the R, G, and B component values. Take out the value of the R, G, b value multiplied by 8, 4, 8 can be filled with the first component of a byte, and then the three bytes by RGB combination, put into memory (also to reverse order), you can convert to 24-bit standard BMP format.

Image Data Array
18) 55 (No color palette)-bfsize: represents one pixel per two byte. The first byte in the array represents the pixel in the lower-left corner of the bitmap, and the last byte represents the pixel in the upper-right corner of the bitmap.

Storage algorithms
BMP files are usually uncompressed, so they are usually much larger than the compressed image file format of the same image. For example, a 800x600 24-bit occupies almost 1.4MB of space. Therefore, they are usually not suitable for transmission on the Internet or other medium with low or limited capacity. Depending on the color depth, a pixel on the image can be represented by one or more bytes, which is determined by N/8 (n is bit depth and 1 bytes contains 8 data bits). Pixel-based ASCII values, such as the picture browser, calculate the color of pixels, and then read the corresponding values from the palette. For more detailed information, see the section on bitmap files below. N-bit 2n color bitmap Approximate byte number can be calculated with the following formula: BMP file Size is approximately equal to 54+4*2 N-th square + (w*h*n)/8, where the height and width are pixels. Note that 54 of the above formula is the file header of the bitmap file, which is the size of the color palette. Also note that this is an approximation, for n-bit bitmap images, although there may be up to 2n in color, a particular image may not use all of these colors. Because the color palette defines only the colors used by the image, the actual color palette will be less than. If you want to know how these values are obtained, refer to the section in the file format below. Because the storage algorithm itself determines the factors, according to the different image parameters calculated size and the actual size of the file will be a few small differences.

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.