BMP bitmap file format detailed and programming suggestions

Source: Internet
Author: User

bmp File Source stream length, although the JPG, PNG and other format image files, it is a bit of soil, but after all, BMP file format is relatively simple, easy to understand, as for the BMP many bitmap format can not blame Microsoft, mainly early on who did not expect the picture technology will develop so fast, And every time the upgrade is compatible, it can only be so (a bit cumbersome but not complicated). Days to write this article so as to stay in the document and programming enthusiasts reference. BMP bitmap File structure mainly consists of: BMP file Header, bitmap information header, color table and graphics data four parts, for 24-bit, 32-bit there is no color table field, low-level map has a color index table. First, BMP file header structure BMP file header data structure contains information such as BMP file type flag, file size and the starting position of bitmap data. Its structure is defined as follows: TypeDef strUCt Tagbitmapfileheader{word Bftype;dword bfsize; WORD bfReserved1; //reserved word, must be 0WORD BfReserved2;//reserved word, must be 0DWORD bfoffbits;} bitmapfileheader;bftype--is a bitmap file type, must be BM, converted to hexadecimal code is 0x4d42 (small end of the convention, about big and small end of the concept refer to the later days of the article) the size of the bfsize--bitmap file, in bytes, Represents the size of the entire BMP file, including 2 header segments and bitmap data areas, calculated as follows:sizeof(Bitmapfileheader) +sizeof(Bitmapinfoheader) + (biwidth* bibitcount+ to)/8*Biheight may be a lot of netizens to the second half of the formula is very confused, why calculate the size of the data area, this is because Windows also specifies that a scan line in the image file of the number of bytes must be a multiple of 4, the insufficient portion to be filled with 0, So the size of the data area cannot be used easily (Biwidth*bibitcount*biheight)/8 calculates the total number of bytes, instead of the number of bits per line, plus 31 in the numerator (because 4 bytes is filled with 32 bits) is to prevent a scan line more than one or less to do the carry (+ to) Rounding (/8) operation. bfoffbits--the starting position of the bitmap data, that is, the starting position of the data area, where the base point is the offset of the bitmap data area relative to the file header, in bytes. BMP bitmap Information Header structure BMP bitmap Information header structure is mainly used to explain the size of the bitmap, compression type and other important information. Its structure is as follows: TypeDefstructTagbitmapinfoheader{dword bisize;//The number of bytes in this structure, in fact the structure occupies 40 bytes, but every time windows still need you to addLONG Biwidth;//the width of the bitmap, in pixelsLONG Biheight;//the height of the bitmap, in pixelsWORD biplanes;//the number of planes of the target device, the contract must be 1WORD biBitCount//The number of bits required per pixel must be one of 1 (two-color), 4 (16-color), 8 (256-color), 24 (True color), or 32 (32-bit true color)DWORD bicompression;//bitmap compression type, must be either 0 (uncompressed), 1 (bi_rle8 compression type), or 2 (bi_rle4 compression type)DWORD biSizeImage;//the size of the bitmap, in bytes, for Bi_rgb must be set to 0, and for compressed files, refer to MSDNLONG Bixpelspermeter;//Horizontal resolution, number of pixels per metre, generally not cared for, set to 0LONG Biypelspermeter;//vertical resolution, number of pixels per metre, generally not cared for, set to 0DWORD biclrused;//The number of colors in the color table that the bitmap actually uses, usually without concern, set to 0DWORD biclrimportant;//The number of important colors in the process of bitmap display, generally do not care, set to 0} Bitmapinfoheader; The structure occupies 40 bytes. Third, the BMP color palette structure, or called color table this item is not mentioned in many articles on the network, or only refers to the concept of color palette, mainly because now using the colour table of BMP (24 true color of the pictures below the sake of very few), here, the color table represents the BMP file of the DIB (device-independent bitmap) size and color information. The structure is defined as follows: TypeDefstruct_bitmapcoreinfo {bitmapcoreheader bmciheader;//See the structure definition belowRgbtriple bmcicolors[1];//See the structure definition below} bitmapcoreinfo; Bitmapcoreheader is defined as follows: TypeDefstructTagbitmapcoreheader {//BMCHDWORD bcsize;//indicates the number of bytes occupied by this structureWORD Bcwidth;//width of the bitmapWORD Bcheight;//height of bitmapWORD Bcplanes;//device plane number, set to 1WORD Bcbitcount;//The number of bits per pixel, such as 1, 4, 8, 24, actually the last 24 seems useless, of course, you can use the program convention. the structure of the bitmapcoreheaderrgbtriple is defined as follows: TypeDefstructTagrgbtriple {//RGBTBYTE Rgbtblue;//BlueBYTE Rgbtgreen;//GreenBYTE rgbtred;//Redrgbtriple Four, BMP bitmap data area to the map data area, it must be related to the concept of color table, color table is used to describe the color of the bitmap, according to the pixel partition of the bitmap, each pixel with a rgbquad structure to represent a color. The RGBQUAD structure is defined as follows: TypeDefstructTagrgbquad {BYTE rgbblue;//BlueBYTE Rgbgreen;//GreenBYTE rgbred;//RedBYTE rgbreserved;//reserved} Rgbquad; According to the understanding of the day, the structure is only useful in the process of processing, feel free to explain all you can, as long as not the RGB color reverse. The bitmap information header and color table make up the bitmap information area, and the BITMAPINFO structure is defined as follows: TypeDefstructTagbitmapinfo {bitmapinfoheader bmiheader;//Bitmap Information HeaderRgbquad bmicolors[1];//Color Table} bitmapinfo; The Rgbquad item inside the structure is the real color table, and the size is determined according to the actual situation. The structure of the color table is also simple, which is to use RGB three tones and the final bitmap color information. As for how many bytes of Rgbquad data is actually used, it needs to be determined by bibitcount, so it is not necessary to use byte in the above structure to represent a certain four bytes. The day margin is described as follows:1, when bibitcount=1 o'clock, 8 pixels accounted for 1 bytes, each pixel intelligence with a bit to represent, color can only have two kinds of 1 or 0, that is, two colors, the specific color needs to look up the color table. 2, when bibitcount=4, 2 pixels accounted for 1 bytes, each pixel accounted for half a byte (4 bit), possibly color has 2^4=16 kinds, namely 16 color, the specific color needs to look up the color table. 3, when bibitcount=8, 1 pixels is 1 bytes, possibly color has 2^8=256 kinds, namely 256 color, the specific color needs to look up the color table. 4, when bibitcount=16, 1 pixels is 2 bytes, possibly color has 2^ -=65536 kinds, namely 64K, the specific color needs to look up the color table. 5, when bibitcount=24, 1 pixels is 3 bytes, possibly color has 2^ -=16777216, so many colors if it is to write a color table to occupy at least 16M, the whole universe certainly disagree, so simply take each byte to represent a color, exactly three bytes rgb, "very 6+1". 6, when bibitcount=32 o'clock, 1 pixels accounted for 4 bytes, which is not mentioned in many articles on the Internet, only about 24 bits, 32 True color is the original rgbreserved use, as a transparency mark. The understanding method with 24 bits, plus reserved reserved transparency, the specific color of the day edge is not out. Programming recommendations (for data processing):1, BMP file read extract to a two-dimensional array, for the use of C + +, VC + +friends, there are ready-made DIB derived classes can refer to Google a bit. 2, for the normal reading of BMP files, drawing there are a lot of ready-made system functions to call, day edge no longer elaborate, such as CreateDIBitmap, CreateCompatibleDC, BitBlt, StretchBlt and so on. 2, BMP file write operation only need to follow the above BMP file format, the BMP file header, information table and bitmap data area in order to write BMP file.
Http://www.metsky.com/archives/198.html
what is the specific difference between JPG format and BMP format? Data storage format is not the same, specific look at their information it ! jpg or BMP, is a format saved to the file, not the real computer processing format. The computer handles the DIB, the device-independent bitmap, regardless of the format, when the file is opened, it should be converted to a DIB to handle. In fact, BMP format is directly preserved in the DIB format grayscale, binary, filtering processing is for the DIB format, is in memory processing, not on the disk directly on the file processing. Bmp,jpg just save the format is different, of course, JPG has compression, BMP uncompressed, and finally save the time certainly will not be identical.
Converting a 24-bit bitmap to a 8-bit bitmap

BMP bitmap file format detailed and programming suggestions

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.