BMP format Parsing

Source: Internet
Author: User
Tags bmp image

I've been working on image processing lately, so much.

Introduction to BMP format

A. Format composition

1: Bitmap header file data structure, it contains BMP image file type, display content and other information;

2: Bitmap information data structure, it contains BMP image width, height, compression method, as well as the definition of color information;

3: Palette, this part is optional, some bitmaps need a palette, some bitmaps, such as a true color graph (24-bit BMP) do not need a palette;

4: Bitmap data, this part of the content according to the BMP bitmap use different number of bits, in the 24-bit graph is used directly RGB, while the other less than 24 bits of the color index in the palette value.

B. Corresponding data structure

BMP file Header (14 bytes): BMP file Header data structure contains information such as the type of BMP file, the file size, and the starting position of the bitmap.

BMP image File Header

int bftype;//bitmap file type with a value of bm,1, 2-bit

int bfsize;//file size, 3-6 bytes

int bfReserved1 =0;//reserved word 7-8, must be 0

int bfReserved2 =0;//reserved word 9-10, must be 0

int bfoffbits;//offset of 11-14 bytes from the beginning of the file to the bitmap data

(lena512.bmp)

0x42 0x4d means BM

0x00040436 represents bytes, and so on

Bitmap Information Header (40 bytes): BMP bitmap Information header data is used to describe the size of the bitmap and other information.

BMP Bitmap Information Header

int bisize;//Figure Information Header length 15-18

int biwidth;//bitmap width, in pixels 19-22

int biheight;//bitmap height, in pixels 23-26

int biplanes = The number of bits of the 1;//bitmap, which is always 1 27-28

int bibitcount;//bits per pixel, supports 1, 4, 8, 16, 24. 29-30

int bicompression;

Compression Description: There are 0 (not compressed), 1 (RLE 8, 8-bit RLE compression), 2 (RLE 4, 4-bit RLE compression, 3 (bitfields, bit field storage) 31-34

The size of the bitmap data represented by the int bisizeimage;//bytes = = Number of bytes per row x Bitmap height 35-38

int bixpelspermeter; Bitmap horizontal resolution, 39-42 pixels per metre

int biypelspermeter; Bitmap vertical resolution, 43-46 pixels per metre

int biclrused; The number of colors in the color table that the bitmap is actually using is set to 0, which means that all palette entries are used 47-50

int biclrimportant; The number of important colors in the bitmap display is 0, which means it is important. 51-54

(lena521.bmp)

C. Color table (color palette)

A color table is used to describe a color in a bitmap, which has several table entries, each of which is a RGBQUAD type structure that defines a color. The RGBQUAD structure is defined as follows:

Class Rgbquad {

The order of the colors is also blue green red + reserved

int rgbblue; Blue Brightness (value range is 0-255)

int rgbgreen; Green Brightness (value range is 0-255)

int rgbred; Red Brightness (value range is 0-255)

int rgbreserved=0; Reserved, must be 0

}

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 is no color table entry.

(lena521.bmp)

8 -bit biBitCount represents the palette because it is a grayscale image, so the RGB values are the same

(test. bmp)

1 -bit biBitCount represents a palette with no rgbreserved Entries

D. Bitmap data

The bitmap data records each pixel value of the bitmap, which is left to right within the scan line, and the scan line is from bottom to top. The number of bytes that a bitmap's pixel value occupies:

When bibitcount=1, 8 pixels of 1 bytes;

When bibitcount=4, 2 pixels of 1 bytes;

When bibitcount=8, 1 pixels of 1 bytes;

When bibitcount=24, 1 pixels accounted for 3 bytes, in order of b,g,r respectively;

Windows specifies that the number of bytes that a scan row occupies must be

Multiples of 4 (that is, in long), insufficient to fill with 0,

biSizeImage = (((((Bi.biwidth * bi.bibitcount) + & ~31)/8) * bi.biheight;

(Important: When programming, otherwise prone to errors in the image)

1bit represents the processing code of the image:

for (int j=0;j<this.biheight;j++) {

for (int i=0;i<this.biwidth;) {

int rowNum = (this.biwidth/32+1);

byte[] Rowpix = new byte[rownum];//one row of data read at a time

Bis.read (Rowpix,0,rownum);

for (int k=0;k<rownum;k++) {

int[] bit = Getbooleanarray ((byte) rowpix[k]);

int num = 0;

while (num<8 && i<this.biwidth) {

Colorindex[j][i] = Bit[num];

num++;

i++;

}

}

}

Special NOTE: BMP Read pixel point is from the bottom left corner to read, check for a long time ...

BMP format Parsing

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.