BMP bitmap file structure and VC operation

zhaozj2021-02-11  204

---- 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 BM

DWORD BFSIZE; // Bitmap file size, byte

Wordbfreserved1; // bitmap file reserved word, must be 0

Wordbfreserved2; // 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

Longbiwidth; // Bit map width, in pixels

Longbiheight; // 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

Longbixpelspermeter; // bitmap level resolution, number of pixels per meter

Longbiypelspermeter; // bitmap vertical resolution, number of pixels per meter

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 {

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

Bytergbgreen; // Green brightness (value range is 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, 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. 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 pixel occupies 1 byte;

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

Windows specifies that the number of bytes occupied by a scan line must be a multiple of 4 (ie, in line), insufficiently filling,

The number of bytes accounted for a scan line: DataSizePerLine = (BiWidth * BibitCount 31) / 8;

// A scan line occupying the number of bytes DatasizePerLine = DataSizePerLine / 4 * 4; // The number of bytes must be a multiple of 4

Bitmap data size (no compression): DataSize = DataSizePerline * Biheight;

---- II, 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 () display bitmap in onpaint functions.

---- 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 MPBITSSRC; // bitmap file address (memory)

BitMapInfoHeader * mpbitmapinfo; // Bitmap information head ---- 2. Add a video function to 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 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;

}

---- Five, use the CVIEWBIMAP class to display 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 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 here

CVIEWBITMAP * 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.

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

New Post(0)