Introduction to BMP Bitmap format in Windows

Source: Internet
Author: User
Tags bmp image

BMP picture, is the abbreviation of bitmap (bitmap), it is the basic format of Windows display picture. Under Windows, image files (including video playback) in any format are converted to bitmaps to be displayed. Image files in various formats are also generated using different compression algorithms based on the bitmap format.

The bitmap file is divided into the following 3 parts:

block name  

Size (yte)

bitmap file header

bitmapfileheader

14

Bitmap information header

bitmapinfoheader

40

rgb color array

byte*

(1) Bitmap file header Bitmapfileheader

The structure is defined as follows:

typedef struct TAGBITMAPFILEHEADER

{

UINT Bftype;
DWORD bfsize;
UINT bfReserved1;
UINT BfReserved2;
DWORD bfoffbits;

} Bitmapfileheader;

which

bftype

bfsize

Describes the size of the bitmap file, in bytes

bfreserved1

reserved, must be set to 0

bfreserved2

reserved, must be set to 0

Bfoffbits

This parameter is very useful because the length of the bitmap information header and palette varies depending on the situation, so you can use this offset value to quickly read the in-place data from the file.

(2) Bitmap information header Bitmapinfoheader

The structure is defined 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;

which

Bisize

Describes the number of words that are required for the BITMAPINFOHEADER structure , not bytes .

Biwidth

Indicates the width of the image, in pixels.

Biheight

Indicates the height of the image, in pixels. Note: This value, in addition to describing the height of the image, has another use to indicate whether the image is a backward bitmap or a forward bitmap. If the value is a positive number, the image is inverted, and if the value is a negative number, the image is positive. Most BMP files are inverted bitmaps, meaning that the height value is a positive number .

biplanes

Describes the number of bits for the target device, and its value will always be set to 1.

biBitCount

Indicates the number of bits/pixels, with values of 1, 4, 8, 16, 24, or 32. Most of the images we use are 24-bit and 32-bit.

Bicompression

Describe the type of image data compression, and we only discuss types that are not compressed: Bi_rgb.

biSizeImage

Indicates the size of the image, in bytes. when in BI_RGB format, it can be set to 0.

Bixpelspermeter

Describes the horizontal resolution, expressed in pixels/meters.

Biypelspermeter

Describes the vertical resolution, expressed in pixels/meters.

biClrUsed

Describes the number of color indexes in the color table that the bitmap is actually using ( set to 0, which means that all palette items are used ).

Biclrimportant

Indicates that the number of color indexes that have a significant effect on the image display, if 0, is important .

(3) RGB color channel

Under windows, the storage format for the RGB color matrix is BGR. In other words, for a 24-bit RGB bitmap, the data format is: .... For 32-bit RGB bitmaps, the data format is: .... Here, the alpha channel is also known as a transparent channel, which is a transparent property of that pixel, with a value between 0 (fully transparent)------255 (opaque). For 24-bit images, the entire image is opaque because there is no alpha channel . The color data area records the corresponding color values for each pixel. The way they are recorded differs depending on the color pattern.

Bibitcount=1
Indicates that the bitmap has a maximum of two colors,black and white by default, you can also define these two colors yourself. The image information Header palette will have two palette entries, called index 0 and Index 1. Each of the image data arrays represents a pixel. If a bit is 0, the RGB value of index 0 is used when it is displayed, and if the bit is 1, the RGB value of index 1 is used.
Bibitcount=4
Indicates that the bitmap has a maximum of 16 colors.each pixel is represented by 4 bits, and these 4 bits are used as table entries in the color table to find the color of the pixel.。 For example, if the first byte in a bitmap is 0x1f, it represents two pixels, the first pixel color is found in the 2nd table item in the color table, and the second pixel color is found in the 16th table item in the color table. In this case, there are 16 RGB entries by default in the palette. Corresponds to index 0 to index 15.
Bibitcount=8
Indicates that the bitmap has a maximum of 256 colors.each pixel is represented by 8 bits, and these 8 bits are used as table entries in the color table to find the color of the pixel.。 For example, if the first byte in a bitmap is 0x1f, the pixel color is found in the 32nd table item in the color table. At this point, by default, there will be 256 RGB entries in the palette, corresponding to index 0 to index 255.
Bibitcount=16
Indicates that the bitmap has a maximum of 65536 colors.each pigment is represented by a 16-bit (2-byte). This format is called high-color, or enhanced 16-bit color, or 64K color。 It's more complicated, and when the value of the Bicompression member is BI_RGB, it doesn't have a palette.in 16 bits, the lowest 5 bits represent the blue component, the middle 5 bits represent the green component, the high 5 bits represent the red component, the total is 15 bits, the highest one is reserved and set to 0。This format is also known as 555 16-bit bitmaps。
If the value of the Bicompression member is Bi_bitfields, then the situation is complicated, first, the position of the original palette is occupied by three DWORD variables, called Red, green, and blue masks. Used to describe the position of the red, green, and blue components in 16 bits.In Windows95 (or 98), the system can accept two formats of bit fields: 555 and 565, in 555 format, red, green, and blue masks are: 0x7c00, 0x03e0, 0x001f, and in 565 format, they are: 0xf800, 0X07E0, 0x001f. After you have read a pixel, you can use the mask "and" on the pixel value separately to extract the desired color component (and, of course, the appropriate left and right moves). In NT systems, there is no format limit, except that there can be no overlap between masks. (Note: This format of the image is more cumbersome to use, but because its display effect is close to the true color, and the image data is much smaller than the true color image, so, it is more used in game software).
Bibitcount=24
Indicates that the bitmap has a maximum of 2 24-time Square, about 16.7 million colors.This bitmap has no palette (bmicolors member size 0), in the in-place array, each 3 bytes represents a pixel, corresponding to the color R, G, B。
Bibitcount=32
Indicates that the bitmap has a maximum of 2 32 seed colors. The structure of this bitmap is very similar to a 16-bit bitmap structure, and when the value of the Bicompression member is BI_RGB, it does not have a palette,32 bits have 24 bits for storing RGB values in the order: highest bit-reserved, red 8 bit, green 8-bit, blue 8-bit。 This format is also a 888 32-bit bitmap.
If the value of the Bicompression member is bi_bitfields, the position of the original palette is occupied by three DWORD variables, which are red, green, and blue masks, respectively, to describe the position of the red, green, and blue components in 32 bits.In Windows95 (or 98), the system accepts only 888 formats, which means that the value of three masks will only be: 0xFF0000, 0xff00, 0xFF。 In the NT system, you just have to be careful to make the masks do not overlap. (Note: This image format is more structured because it is DWORD aligned, so you can compile-level code optimization (simple) in memory for image processing).

BMP image Data area at the time of storage, 2-color image each point accounted for 1 bits (8 bits 1 bytes), 16-color image each point accounted for 4 bits (half bytes), 256-color image each point accounted for 8 bits (1 bytes); True color images each point accounted for 24 bits (3 bytes). As a result, the size of the entire data area varies. In terms of its law, the following formula can be calculated: image Data Information size = (Image width * Image Height * Record pixel number of bits)/8.
However, the size of the uncompressed image information area. In addition to true-color mode, the rest is greater than or equal to the size of the data information. What is this for? There are two reasons:
1.bmp file records a row of images in bytes. Therefore, there is no data bit information in one byte that represents the point in a different two rows. That is, the display mode is 16 colors, when each byte is assigned two point information, if the image width bit odd, then the last pixel of the information will be exclusive of one byte, the second 4 bits of this byte will not make sense. The next byte begins to record the next line of information.
2. for ease of display, in addition to True color, the number of rows in each of the other color patterns will be filled with the data "00" to 4 integer times, 4-byte alignment. If the display mode is 16 colors, when the image width is 19 o'clock, each row is stored with a complement of 4-(19/2+1)%4=2 bytes (plus 1 is because there is a pixel in it to monopolize one byte). If the display mode is 256 colors, each row is supplemented with 4-19%4=1 bytes when the image is a width of 19 o'clock.

When the BMP image data area is read, the offset of the bitmap data is sizeof (bitmapfileheader) + sizeof (Bitmapinfoheader).

because Windows has a minimum of 4 bytes in a row scan, when the picture is wide × the number of bytes per pixel! = 4 integer times to fill the missing bytes after each line with 0 padding (generally not justified when the image width is a power of 2). The data in the bitmap file is aligned in line when it is written, which means that no line alignment is required when loading. However, the length of the image data is not: Width x Height x number of bytes per pixel, we need to calculate the correct data length by the following method

Calculate the image data size

Widthstep, the actual number of bytes per row

int ilinebytecnt = (((m_iimagewidth*m_ibitsperpixel) + +) >> 5) << 2;

M_iimagedatasize = ilinebytecnt * m_iimageheight;

Introduction to BMP Bitmap format in Windows

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.