BMP bitmap file structure and smooth zoom
Xi'an Jiaotong University Fluid Machinery Institute Zhang Yiyun
---- Use ordinary ways to display the BMP bitmap, accounting for large memory, slow speed, when graphic is shortened, distortion, showing a graphic graphic of high color bits on a low color bit number. Distortion. This article uses a video function to display BMP bitmaps, which can eliminate the above shortcomings.
---- One, BMP file structure
---- 1. BMP file composition
---- BMP file consists of file head, bitmap information head, color information, and graphics data.
---- 2. BMP file header
---- BMP file header data structure contains information such as the type of BMP file, file size, and bitmap start position.
---- The structure is defined as follows:
TypeDef struct tagbitmapfileHeader {WordBFTYPE; // bitmap file type, must be the size of BMDWord bfsize; // bitmap file, in byte is unit WordBfreserved1; // bitmap file reserved word, must be 0wordbfreserved2; // bitmap The file reserved word must be 0Dword bfoffbits; // bitmap data of the starting position of the bitmap data is represented by the offset relative to the bitmap // file header} BitmapFileHeader;
---- 3. Bitmap information head
----
BMP bitmap information header data is used to explain the size of the bitmap. Typedef struct tagbitmapinfoheader {dWord bisize; // This structure occupies the number of bytes longbiWidth; // bit map, in pixels, longbiheight; // bitmap height, in pixels Word biplanes; // target device Level, must be 1 Word BibitCount // The number of bits required for each pixel must be 1 (two-color), // 4 (16 colors), 8 (256 colors) or 24 (true color) DWORD BICOMPIRESSION; / / Bittap compression type, must be 0 (no compression), // 1 (bi_rle8 compression type) or 2 (Bi_RLE4 compression type) DWORD BisizeImage; // bitmap size, in byte as lineter; // Bitmap horizontal resolution, pixel number longbiypelspermeter; // bit map vertical resolution, pixel DWORD BICLRUSED; // Bitgram actually used color table DWORD BICLRIMPORTANT; // Bitmap display Important color number} BitmapInfoHeader;
---- 4. Color table
---- Color table is used to explain the color in the bitmap, which has several entries, each entry is a RGBQUAD type structure, defining a color. The RGBQUAD structure is defined as follows:
Typedef struct tagrgbquad {bytergbblue; // Blue brightness (value range is 0-255) BytergBGreen; // Green brightness (value range 0-255) Bytergbred; // Red brightness (value range is 0-255) BytergBreserved; // Reserved, must be 0} RGBQUAD; the number of RGBQUAD structure data in color table has bibitcount to determine: When BiBitCount = 1, 4, 8, there are 2,16,256 items, respectively; when BiBitcount = 24 No color entries. The bitmap information head and color table constitute bitmap information, the BitmapInfo structure is defined as follows: typedef struct tagbitmapinfo {bitmapinfoHeader bmiheader; // bitmap information head RGBQUAD BMICOLORS [1]; // color table} BitmapInfo; ---- 5. Bit Figure data
---- bitmap data records each pixel value of the bitmap, the recording order is from left to right in the scan line, and the scan line is from bottom to. The number of bytes of a pixel value of the bitmap:
When BIBITCOUNT = 1, 8 pixels account for 1 byte; when BiBitCount = 4, 2 pixels account for 1 byte; when BiBITCount = 8, 1 pixels account for 1 byte; when BiBitCount = 24 1 pixels account for 3 bytes; Windows specifies that the number of bytes occupied by a scan line must be a multiple of 4 (ie, in line as a unit), and the number of bytes accounted for one scan line is calculated. Method: DataSizePerLine = (BiWidth * BibitCount 31) / 8; // A scan line occupying the number of bytes DatasizePerLine = DataSizePerLine / 4 * 4; // The number of bytes must be the size of the multipragraph data (no Compression In the case of data: DataSize = DataSizePerline * Biheight;
----
Second, BMP bitmap general display method
---- 1. Apply for memory space for storage bitmap file
---- GlobalLoc (GHND, FileLength);
---- 2. Bitmap file read in the application memory space
---- LoadFileTomeMory (MPBITSRC, MFileName);
---- 3. In the onpaint function, create a display bitmap
---- Create a display bitmap with createDibitMap, create a compatible DC with CreateCompatibleDC (),
---- Select the display bitmap with SelectBitMap ().
---- 4. Display bitmaps with functions such as Bitblt or StretchBLT
---- 5. Delete the created bitmap with deleteObject ()
---- The shortcomings of the above method are: 1) Slow display speed; 2) Memory occupation is large; On the device (such as 256 display mode) shows the graphic distortion of the high color bits (such as true color) graphics.
---- Third, BMP bit map scale display
---- Use the DrawDib video function to display bitmap, with less memory, fast speed, and DITHERING processing. Duel processing is a graphics algorithm that can be used to display color images on a device that supports less color than the image. The BMP bitmap display method is as follows:
---- 1. Turn on the video function DrawDibopen (), generally placed in the constructor
---- 2. Apply for memory space for storage bitmap file
---- GlobalLoc (GHND, FileLength);
---- 3. Bitmap file read into the application memory space
---- LoadFileTomeMory (MPBITSRC, MFileName);
---- 4. Use DrawDibRealize (), DrawDibDraw () in ONPAINT, DrawDibdraw () display bitmap ---- 5. Close the video function DrawDibClose (), generally placed in the destructor
---- The advantages of the above method are: 1) Fast display speed; 2) Demolition is less; Small distortion; 5) You can make simple animations by direct processing bitmap data.
---- Four, CVIEWBIMAP class programming points
---- 1. Add video functions such as video functions in the CViewBIMAP class
HDrawDib m_hdrawdib; // video function handlemhbitssrc; // bitmap file handle (memory) LPSTR MPBITSRC; // bitmap file address (memory) BitmapInfoHeader * mpbitmapinfo; // bitmap information header
---- 2. Add to open the video function in the CVIEWBIMAP class constructor
---- m_hdrawdib = drawdibopen ();
---- 3. Add a shutdown video function in the CVIEWBIMAP classification function
IF (m_hdrawdib! = null) {drawdibclose (m_hdrawdib); m_hdrawdib = NULL;}
---- 4. Add graphicDraw () to the CVIEWBIMAP graphic display function onpaint ()
voidCViewBitmap :: OnPaint () {CPaintDC dc (this); // device context for paintingGraphicDraw ();} voidCViewBitmap :: GraphicDraw (void) {CClientDC dc (this); // device context for paintingBITMAPFILEHEADER * pBitmapFileHeader; ULONG bfoffBits = 0 ; Cpoint wid; // graphic file name is valid (= 0 bmp) if (MbitmapFileType
Mpbitmapinfo-> biheight- mdispr.height (); if (mpos.x <0) mpos.x = 0; if (mpos.y <0) mpos.y = 0; // Display true color bitmap DrawDibRealize (m_hdrawdib, Dc.getsafeHDC (), true); if (mFullViewTog == 0) {wid.x = mdispr.width (); wid.y = mdispr.Height (); // 1: 1 When display, it is not larger than graphical size if (Wid.x> mpBitmapInfo-> biWidth) Wid.x = mpBitmapInfo-> biWidth; if (Wid.y> mpBitmapInfo-> biHeight) Wid.y = mpBitmapInfo-> biHeight; DrawDibDraw (m_hDrawDib, dc.GetSafeHdc (), 0 , Wid.x, Wid.y, MpbitmapInfo, (LPBITSRC BFOffbits), Mpos.x, Mpos.y, Wid.x, Wid.y, DDF_BACKGROUNDPAL);} else {DrawDibdraw (M_HDRAWDIB, DC. GetSafeHdc (), 0, 0, mDispR.Width (), mDispR.Height (), mpBitmapInfo, (LPVOID) (mpBitsSrc bfoffBits), 0, 0, mpBitmapInfo-> biWidth, mpBitmapInfo-> biHeight, DDF_BACKGROUNDPAL);}} Return;} ----
5. Use the CVIEWBIMAP class to display the BMP bitmap
---- 1. Newly built a name MyMap project file in Visual C 5.0, type MFC AppWizard [EXE]. After the compilation run, click ResourceView in Workspace (if closed, open with alt_0), click on the symbol on the left side of MENU to expand the MENU entry, double-click the IDR_MAINFRAME entry, enter the menu resource editing, in '"View (V)" drop-down The "Viewbitmap" entry is added to the end of the menu (English version for the View drop-down menu), which ID_View_bitmap is added.
---- 2. Click downlift menu in Visual C 5.0 to add Bitmap0.h and bitmap0.cpp to the project file.
---- 3. Press CTRL_W in Visual C 5.0 to enter the MFC ClassWizard, select Class Name CMAINFRAME, ObjectIDS: ID_VIEW_BITMAP, Messages Select Command, then click the Add Futen button, then enter the function name ONVIEWMAP. After adding ONVIEWBIMAP, click onviewbimap entry in Member Functions: Click the Edit Code button to edit the program code. code show as below:
void CMainFrame :: OnViewBitmap () {// TODO: Add your command handler code hereCViewBitmap * pViewBitmap = NULL; pViewBitmap = new CViewBitmap ( "BITMAP.BMP", this); pViewBitmap-> ShowWindow (TRUE);}
---- Add #include "bitmap0.h" on the head of the program, then compile operation. ---- 4. Find a big big color BMP bitmap, copy it into bitmap.bmp.
---- 5. When running, click the drop-down menu "View (V) -> Viewbitmap" (English version for view-> Viewbitmap) to display the BitMap.BMP bitmap.
---- 6. CVIEWBIMAP class function description
---- 1. With horizontal and vertical scroll bars in the client area. When the bitmap size is greater than the display client area, the scroll bar can be used; the scroll bar is invalid when the bitmap is small and smaller than the display customer area or full screen display.
---- 2. There is a status bar at the bottom of the customer area. The first grid in the status bar is bitmap information, the second grid is a bitmap display method, which can be used using a normal function or using a video function. Click on the mouse in the second area to be connected between the two. The third grid is a bitmap display ratio, which can be 1; 1 display or full screen display. Clicking on the mouse in the third grid area can be connected between the two. When the full screen is displayed, if the bitmap is smaller than the client area, the bitmap is enlarged; if the bitmap is larger than the client area, the bitmap is reduced.
---- 3. Support file drag and drop function. This bitmap can be displayed from the resource manager to the client area.
---- Program debugging After passing, you can find a large color bitmap or adjust the client area than the bitmap, in full screen display mode, compare the difference between using normal functions and using the video function. It can be seen that both the bitmap is different, but when the bit map is short, the two differences are distinct; the bitmap is disturbed when using the video function, and the display speed is fast.
---- It is also possible to switch the screen display from the true color display mode to 256 color display mode from the control panel, and then compare the difference between the same true color bitmap using the normal function and using the video function. You can now experience the superiority of the video function.
---- When the full screen is displayed, the XY direction ratio of the bitmap is different. If you want to keep the same scale, you can properly adjust it in the display program, readers can complete themselves.