Reading Bitmap (bitmap) Implementation and its key points

Source: Internet
Author: User

The format of the bitmap is as follows:

1. File header information block

0000-0001: File identification, for the letter ASCII code "BM".

0002-0005: File size.

0006-0009: Reserved, each byte is filled with "00".

000A-000D: Records the starting position of the image data area. Each byte of information meaning is: The file header information block size, image description information block size, image color table size, reserved (for 01).

2. Image description Information block

000E-0011: The size of the image description information block, usually 28H.

0012-0015: Image width.

0016-0019: Image height.

001A-001B: Total number of images plane (constant 1).

001C-001D: Records the number of bits of a pixel, a very important value, and the number of colors in the image is determined by that value.

001E-0021: Data compression method (value bit 0: not compressed; 1:8-bit compression; 2:4-bit compression).

0022-0025: The size of the image area data.

0026-0029: How many pixels per metre of the level, in the device-independent bitmap (. DIB), each byte is filled with 00H.

002a-002d: The vertical number of pixels per metre, in the device-independent bitmap (. DIB), each byte is filled with 00H.

002E-0031: The number of colors used in this image, as a value of 0, means that all colors are as important.

3. Color table

The size of the color table depends on the color pattern used: 2-color image is 8 bytes, 16-color image bit 64 bytes, 256-color image is 1024 bytes. Where every 4 bytes represents a color, with B (blue), G (green), R (red), alpha (32-bit bitmap transparency values, not typically required). That is, the first 4 bytes represents the color of the color number 0, next represents the color number 1 color, and so on.

4. Image Data Area

The next bit of the color table is the image data area of the bitmap file, which records the color number of each pixel corresponding to the color pattern, and its record is also the same as 2-color image each point occupies 1 bits, 16-color image each point accounted for 4 bits, 256-color image each point accounted for 8 bits. 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:

The 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 bit 16 color, in each byte allocation 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. In order to display the convenience, in addition to the true color, the other color mode in each of the number of rows of bytes to use the data "00" to be filled with 4 of the integer times. 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.

The code implements a bitmap width, a height of 4 integer multiples, and a color bit depth of 24 bits.

Header file Definition:

#pragmaPack (1)typedef unsignedCharBYTE; typedef unsigned ShortWORD; typedef unsignedintDWORD; typedefLongLONG; typedefstructBw_bitmapfileheader {WORD fileType;        DWORD fileSize;        WORD fileReserved1;        WORD FileReserved2;    DWORD OffSet;    } Bitmapfileheader; typedefstructBw_bitmapfinfoheader {DWORD headsize;        LONG width;        LONG height;        WORD plant;        WORD Bitcount;        DWORD compression;        DWORD Sizeimage;        LONG Xpelpermeter;        LONG Ypelpermeter;        DWORD clrused;    DWORD clrimportant;    }bitmapinfohead; typedefstructBw_rgbquad {BYTE rgbblue;        BYTE Rgbgreen;        BYTE rgbred;    BYTE rgbreserved;    }rgbquad; typedefstructBw_pixel {BYTE red;        BYTE Green;    BYTE Blue;    } PIXEL; classBw_bitmap { Public:        BOOLReadbmp (Char*) ;        Bitmapfileheader Bitmapfileheader;        Bitmapinfohead Bitmapinfohead; Rgbquad*Rgbquad; PIXEL*Pixeldata; };

Specific CPP files:

BOOLBw_bitmap::readbmp (Char*fileName) {FILE*FileR, Filew; FileR= fopen (FileName,"RB") ; if(FileR! =NULL) {            //bw_bitmap* BITMAP = new Bw_bitmap;Fread (&bitmapfileheader,1,sizeof(Bitmapfileheader), FileR); if(0x4d42!=bitmapfileheader.filetype) {fclose (FileR); returnNULL; } fread (&bitmapinfohead,1,sizeof(Bitmapinfohead), FileR); Rgbquad=Newrgbquad[bitmapinfohead.clrused];  for(intIcount =0; Icount < bitmapinfohead.clrused; ++icount) {Fread (Char*) & (Rgbquad[icount].rgbblue),1,sizeof(BYTE), FileR); Fread ((Char*) & (Rgbquad[icount].rgbgreen),1,sizeof(BYTE), FileR); Fread ((Char*) & (rgbquad[icount].rgbred),1,sizeof(BYTE), FileR); //fread (char *) & (bitmap->rgbquad[icount].rgbreserved), 1,sizeof (BYTE), FileR);             }            intwidth =Bitmapinfohead.width; intHeight =Bitmapinfohead.height; Pixeldata=NewPixel[width * Height *sizeof(PIXEL)]; //Initializes a pixel array of the original picture//fseek (Fpi,54,seek_set); //to read the pixel data of a pictureFread (Pixeldata,sizeof(PIXEL) *Width,height,filer);              Fclose (FileR); return true ; }          Else          {              //cout<< "File Open error!"  <<endl;             return false ; }      }}

When writing the code, be aware that the header file header uses #pragma pack (1), which tells the compiler to use Boundary 1 alignment (that is, unaligned).

If the #pragma pack (1) is not used, the test results are as follows: sizeof (Bitmapfileheader) has a value of 16 instead of 14. Indicates that the compiler used 4 for the boundary alignment.

If you are in a Linux environment, use __ATTRIBUTE__ ((packed)) to achieve the same effect.

Summary: In the implementation of the data format strict requirements for the function, you should be aware of the compiler's optimization caused by the trouble.

Reading Bitmap (bitmap) Implementation and its key points

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.