BMP bitmap file structure and VC operation Zhu Yonghui

xiaoxiao2021-03-06  87

BMP bitmap file structure and VC operation

Zhu Yonghui

---- 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. --- First, BMP file structure ---- 1. BMP file composition ---- BMP file consists of file header, 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 starting position. ---- The structure is defined as follows: TypedEf struct tagbitmapfileheader

{

Word bftype; // bitmap file type, must be BM

DWORD BFSIZE; // Bitmap file size, byte

Word bfreserved1; // bitmap file reserved word, must be 0

Word bFReServed2; // bitmap file reserved word, must be 0

DWORD BFOFFBITS; // Bit map data starting position to relatively

// The offset of the file header is indicated, in bytes

} 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 is occupied by the number of bytes

Long biwidth; // Bit map width, in pixels

Long biheight; // bit map height, in pixels

Word biplanes; // The level of target device 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 BICOMPRESSION; // bitmap compression type, must be 0 (not compressed),

// 1 (Bi_RLE8 compression type) or 2 (Bi_RLE4 compression type)

DWORD BisizeImage; // Bit map size, byte

Long Bixpelspermeter; // bitmap level resolution, number of pixels per meter

Long biypelspermeter; // bit map vertical resolution, number of pixels per rice

DWORD biclrused; // number of color tables in the color table actually used by the bitmap

DWORD BICLRIMPORTANT; // Bitmap display Direct 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 {

Byte rgbblue; // Blue brightness (value range is 0-255)

Byte rgbgreen; // green brightness (value range is 0-255)

BYTE RGBRED; / / red brightness (value range is 0-255)

BYTE RGBRESERVED; / / Retain, 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, there is no color entry. The bitmap information head and color table form bitmap information, the BitmapInfo structure is defined as follows:

Typedef struct tagbitmapinfo {

BitmapInfoHeader Bmiheader; // bitmap information head

RGBQUAD BMICOLORS [1]; // Color table

} BitmapInfo;

---- 5. Bitmap 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. A number of bytes of a pixel value of a bitmap: When BiBitCount = 1, 8 pixels account for 1 byte;

When BIBITCOUNT = 4, 2 pixels account for 1 byte;

When BiBITCount = 8, 1 pixel occupies 1 byte;

When BIBITCOUNT = 24, 1 pixel accounts for 3 bytes;

Windows specifies that the number of bytes occupying a scan line must be a multiple of 4 (i.e., in units), insufficient by 0 fill, one scan line accounts for the number of bytes: DataSizePerLine = (BiWidth * Bibitcount 31 ) / 8; // A number of bytes of scan lines DataSizePerLine = DatasizePerLine / 4 * 4; // The number of bytes must be the size of the 4 multipragraph data (no compression): DataSize = DataSizePerline * Biheight ; ---- II, BMP bitmap general display method ---- 1. Apply for memory space for storage bitmap file ---- GlobalAlloc (GHND, FileLength); ---- 2. Bitmap file read Apply for memory space ---- LoadFileTMemory (MpBitsSrc, mfileName); ---- 3. In onpaint, you can create a display bitmap with CREATEDIBITMAP () with createCompAPLEDC () Create a compatible DC, --- Select the display bitmap with SelectBitMap (). ---- 4. Functions such as Bitblt or StretchBLT display bitmap ---- 5. Delete the bitmap created with deleteObject () ---- The shortcomings of the above method are: 1) Slow display speed; 2) memory Occupation; 3) The bitmap is large when the display is large when the display is displayed, and can be solved by installing font smoothing software); 4) On the device of the low color bits (such as 256 display mode) display high color bits ( Such as true colorful graphics distortion is serious. ---- Third, the BMP bit map Scale display ---- use the DrawDib video function to display the bitmap, the memory takes up less, fast speed, and the graphic processing is dilated. 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. Open the video function DrawDibopen (), generally placed in the constructor ---- 2. Apply for memory space for storage bitmap file ---- GlobalAlloc (GHND, FileLength; ---- 3. Bitmap file reading in the application memory space ---- LoadFileTMemory (MPBITSRC, MFileName); ---- 4. In OnPaint, use DrawDibRealize (), DrawDibdraw () display bit Figure ---- 5. Close the video function DrawDibclose (), generally placed in the destructor ---- The above method is: 1) Fast display speed; 2) Small memory occupation; 3) Scaling time graphics Small distortion, 4) Distortion when displaying a high color bit number on a low color bit number; 5) Make simple animation by directly 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 Handle M_HBITSRC; // Bitmap file handle (memory)

LPSTR M_PBITSRC; // Bitmap file address (memory)

BitmapInfoHeader * mpbitmapinfo; // bitmap information head

---- 2. Add to open video functions in the CVIEWBIMAP class constructor ---- m_hdrawdib = DrawDibopen (); ---- 3. Add Close video function if (m_hdrawdib! = Null) in the CVIEWBIMAP classification function

{

DrawDibclose (M_HDRAWDIB);

m_hdrawdib = NULL;

}

---- 4. Add graphicDraw () voidcviewbitmap :: onpaint () in CVIEWBIMAP class graphic display function onpaint () voidcviewbitmap :: onpaint ()

{

CPAINTDC DC (this); // Device Context for Painting

GraphicDraw ();

}

Voidcviewbitmap :: GraphicDraw (void)

{

CClientDC DC (this); // device context for Painting

BitmapfileHeader * PbitmapfileHeader;

Ulong bfoffbits = 0;

Cpoint wid;

// Graphic file name is valid (= 0 bmp)

IF (MbitmapFileType

// Graphic file name is valid (= 0 bmp)

// Prepare to display true color level map

PbitmapfileHeader = (BitmapfileHeader *) MPBITSSRC;

Bfoffbits = PbitmapfileHeader-> bfoffbits;

// Display bitmap using a normal function

IF (m_hdrawdib == null || mdispmethod == 0)

{

Hbitmap hbitmap = :: createDibitmap (dc.m_hdc, mpbitmapinfo, cbm_init, mpbitssrc bfoffbits,

(Lpbitmapinfo) MPBitmapInfo, DIB_RGB_COLORS;

// establish a bitmap

HDC HMEMDC = :: createcompatibledc (dc.m_hdc); // Establish memory

Hbitmap hbitmapold = selectbitmap (hmemdc, hbitmap); // Select object

// Member CRECT MDISPR is used to indicate the size of the graphic display area.

// Member CPOINT MPOS is used to indicate graphical display start position coordinates.

IF (mpos.x> (mpbitmapinfo-> biwidth - mdispr.width ())))

Mpos.x = mpbitmapinfo-> biwidth - mdispr.width ();

IF (mpos.y> (mpbitmapinfo-> biheight- mdispr.Height ())))

Mpos.y = mpbitmapinfo-> biheight- mdispr.Height ();

IF (Mpos.x <0) mpos.x = 0;

IF (mpos.y <0) mpos.y = 0;

IF (MFullViewTog == 0)

{

// Show true color set

:: Bitblt (dc.m_hdc, 0,0, mdispr.width (), mdispr.Height (), HMEMDC, Mpos.x, Mpos.y, srccopy;

}

Else

{

:: Stretchblt (dc.m_hdc, 0, 0, mdispr.width (), mdispr.Height (), HMEMDC, 0, 0, MPbitmapInfo-> BiWidth, MPbitmapInfo-

> Biheight, Srccopy;

}

// End display true color level map

:: DeleteObject (SelectObject (HMEMDC, HbitMapold);

// Delete bitmap

}

Else

{

// Use a video function to display a bitmap

IF (mpos.x> (mpbitmapinfo-> biwidth - mdispr.width ())))

Mpos.x = mpbitmapinfo-> biwidth - mdispr.width (); if (mpos.y> (mpbitmapinfo-> biheight- mdispr.Height ())))))

Mpos.y = mpbitmapinfo-> biheight- mdispr.Height ();

IF (Mpos.x <0) mpos.x = 0;

IF (mpos.y <0) mpos.y = 0;

// Show true color set

DrawDibRealize (m_hdrawdib, dc.getsafehdc (), true);

IF (MFullViewTog == 0)

{

Wid.x = mdispr.width ();

Wid.y = mdispr.height ();

// 1: 1 When displayed, it cannot be larger than the size of the graphic.

IF (wid.x> mpbitmapinfo-> biwidth)

Wid.x = mpbitmapinfo-> biwidth;

IF (wid.y> mpbitmapinfo-> biheight)

Wid.y = mpbitmapinfo-> biheight;

DrawDibdraw (m_hdrawdib, dc.getsafehdc (), 0, 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,

(LPBITSRC BFOFFBITS), 0, 0, Mpbitmapinfo-> BiWidth, MpbitmapInfo-> Biheight, DDF_BACKGROUNDPAL);

}

}

Return;

}

---- V. Use the CViewBIMAP class to display BMP bitmap ---- 1. Creating a new 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 files in the project file in Visual C 5.0..-> Add to Project-> Files. ---- 3. Press CTRL_W in Visual C 5.0 to enter the MFC ClassWizard, select Class Name to CMAINFRAME, ObjectIDS: ID_VIEW_BITMAP, Messages Select Command, then click the Add FUCCTION button, then enter the function name ONVIEWBIMA P. After adding ONVIEWBIMAP, click onviewbimap entry in Member Functions: Click the Edit Code button to edit the program code. The code is as follows: void cmainframe :: OnViewbitmap ()

{

// Todo: add your command handler code hender ostMap = 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.

转载请注明原文地址:https://www.9cbs.com/read-122197.html

New Post(0)