Implement BMP bitmap file structure and smooth scaling using VC

Source: Internet
Author: User
The BMP bitmap is displayed in a common way, which occupies a large memory and is slow. When the image is reduced, the distortion is severe, and the image with a high color digit is displayed on a device with a low color digit. This article uses the video function to display BMP bitmap, which can eliminate the above disadvantages.
[Blocked ads]
1. BMP file structure

1. BMP file Composition
A bmp file consists of four parts: File Header, bitmap information header, color information, and graphic data.

2. BMP File Header
The data structure of the BMP file header contains information such as the type, size, and start position of the BMP file.

Its structure is defined as follows:

Typedef struct tagbitmapfileheader
{
Wordbftype; // The type of the bitmap file, which must be BM.
DWORD bfsize; // the size of the bitmap file, in bytes.
Wordbfreserved1; // reserved word for bitmap files, which must be 0
Wordbfreserved2; // reserved word for bitmap files, which must be 0
DWORD bfoffbits; // the starting position of the bitmap data, relative to the bitmap
// The offset of the file header, in bytes
} Bitmapfileheader;

3. Bitmap header
The BMP Bitmap header is used to describe the size and other information of the bitmap.

Typedef struct tagbitmapinfoheader {
DWORD bisize; // number of bytes occupied by the structure
Longbiwidth; // The width of the bitmap, in pixels.
Longbiheight; // The height of the bitmap, in pixels
Word biplanes; // The level of the target device, which must be 1
Word bibitcount // The number of digits required for each pixel, which must be 1 (two-color ),
// 4 (16 colors), 8 (256 colors), or 24 (true color)
DWORD bicompression; // bitmap compression type, which must be 0 (not compressed ),
// 1 (bi_rle8 compression type) or 2 (bi_rle4 compression type)
DWORD bisizeimage; // bitmap size, in bytes
Longbixpelspermeter; // horizontal bitmap resolution, number of workers per meter
Longbiypelspermeter; // bitmap vertical resolution, number of workers per meter
DWORD biclrused; // number of colors in the color table used by the bitmap
DWORD biclrimportant; // number of important colors in the bitmap display process
} Bitmapinfoheader;

4. Color Table
A color table is used to describe the color in a bitmap. It has several table items. Each table item is an rgbquad-type structure and defines a color. The rgbquad structure is defined as follows:

Typedef struct tagrgbquad {
Bytergbblue; // blue brightness (value range: 0-255)
Bytergbgreen; // The brightness of the green color (value range: 0-255)
Bytergbred; // The Red brightness (value range: 0-255)
Bytergbreserved; // reserved, must be 0
} Rgbquad;
The number of rgbquad structure data in the color table is determined by bibitcount:
When bibitcount is 1, 4, and 8, there are 2, 16, and 256 table items respectively;
When bibitcount = 24, there is no color table item.
The bitmap information header and the color table form bitmap information. The bitmapinfo structure is defined as follows:
Typedef struct tagbitmapinfo {
Bitmapinfoheader bmiheader; // Bitmap header
Rgbquad bmicolors [1]; // color table
} Bitmapinfo;

5. bitmap data
The bitmap data records each pixel value of the bitmap. The record sequence is from left to right in the scan row, and the rows are from bottom to top. The number of bytes occupied by a pixel value of a bitmap:
When bibitcount = 1, 8 pixels constitute 1 byte;
When bibitcount = 4, 2 pixels constitute 1 byte;
When bibitcount = 8, 1 pixel occupies 1 byte;
When bibitcount = 24, one pixel occupies three bytes;

Windows requires that the number of bytes for a row to be scanned must be a multiple of 4 (in the unit of long). If the number of bytes for a row to be scanned is insufficient, the number of bytes for the row to be scanned is calculated as follows:

Datasizeperline = (biwidth * bibitcount + 31)/8;
// The number of bytes that a scan row occupies
Datasizeperline = datasizeperline/4*4; // The number of bytes must be a multiple of 4
Bitmap data size (without compression ):
Datasize = datasizeperline * biheight;

Ii. BMP bitmap display method
1. Apply for memory space to store bitmap files
Globalalloc (ghnd, filelength );

2. Reading bitmap files into the applied memory space
Loadfiletomemory (mpbitssrc, mfilename );

3. Create a display bitmap in functions such as onpaint
Use createdibitmap () to create a display bitmap, and use createcompatibledc () to create compatible DC,
Use selectbitmap () to select a display bitmap.

4. Use bitblt or stretchblt and other functions to display bitmap

5. Use deleteobject () to delete the created bitmap

The disadvantages of the above methods are: 1) Slow display speed; 2) High memory usage; 3) large image distortion when the bitmap is scaled down (the font smoothing software can be installed ); 4) A high-color image (such as true color) is severely distorted on a low-color device (such as a 256 display mode.

Iii. BMP bitmap zoom display
The drawdib video function is used to display bitmaps. The memory usage is small and the speed is fast. In addition, the graphics can be deplayed (dithering. Desalination is a type of image.AlgorithmIt can be used to display color images on a device that supports less colors than the image. The BMP bitmap is displayed as follows:

1. Open the video function drawdibopen (), which is generally placed in the constructor.

2. Apply for memory space to store bitmap files

Globalalloc (ghnd, filelength );

3. Reading bitmap files into the applied memory space

Loadfiletomemory (mpbitssrc, mfilename );

4. Use drawdibrealize () and drawdibdraw () to display bitmap in functions such as onpaint.

5. Disable the video function drawdibclose (), which is generally placed in the destructor.

The advantages of the above methods are: 1) fast display speed; 2) low memory usage; 3) Low image distortion during zoom display; 4) low Distortion when displaying high-color graphics on devices with low-color digits; 5) simple animation can be made by directly processing bitmap data.

Iv. Key Points of cviewbimap Programming
1. Add video functions and other Members to the cviewbimap class.

Hdrawdib m_hdrawdib; // video Function
Handlemhbitssrc; // bitmap file handle (memory)
Lpstr mpbitssrc; // bitmap file address (memory)
Bitmapinfoheader * mpbitmapinfo; // Bitmap header

2. Add the video function to the cviewbimap class constructor.

M_hdrawdib = drawdibopen ();

3. Add the function to disable the video in the cviewbimap class destructor.

If (m_hdrawdib! = NULL)
{
Drawdibclose (m_hdrawdib );
M_hdrawdib = NULL;
}

4. Add graphicdraw () to the image display function onpaint of the cviewbimap class ()

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;

// The image file name is valid (= 0 BMP)
If (mbitmapfiletype <id_bitmap_bmp) return;

// The image file name is valid (= 0 BMP)
// Prepare to display the true color bitmap
Pbitmapfileheader = (bitmapfileheader *) mpbitssrc;
Bfoffbits = pbitmapfileheader-> bfoffbits;

// Use a common function to display a bitmap

If (m_hdrawdib = NULL | mdispmethod = 0)
{< br> hbitmap =: createdibitmap (DC. m_hdc,
mpbitmapinfo, cbm_init, mpbitssrc + bfoffbits,
(lpbitmapinfo) mpbitmapinfo, dib_rgb_colors);
// create a bitmap
HDC hmemdc = :: createcompatibledc (DC. m_hdc); // Create memory
hbitmap hbitmapold = selectbitmap (hmemdc, hbitmap); // select an object
// The member crect mdispr is used to indicate the size of the graphic display area.
// The member cpoint MPOs is used to indicate the starting position coordinate of the graphic display.
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)
{
// Display the true color bitmap
: 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 );
}
// Display the true color bitmap
: Deleteobject (SelectObject (hmemdc, hbitmapold ));
// Delete a bitmap
} Else {

// Use the video function to display 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;

// Display the true color bitmap
Drawdibrealize (m_hdrawdib, DC. getsafehdc (), true );

If (mfullviewtog = 0)
{
WID. x = mdispr. Width ();
WID. Y = mdispr. Height ();
// When is displayed, the image size cannot be greater
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, (lpvoid) (mpbitssrc + 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 BMP bitmaps
1. Create a new project file named mymap in Visual C ++ 5.0 with the type of MFC Appwizard [EXE]. After compilation and running are passed, click resourceview In workspace (such as closed, open with alt_0), click the + symbol on the left of menu to expand the menu entry, and double-click the idr_mainframe entry to go to the menu resource editing page, add the "viewbitmap" entry at the end of the "View (v)" drop-down menu (view drop-down menu in English Version). Its ID is id_view_bitmap.

2. in Visual C ++ 5.0, click the drop-down menu project> Add to project> files... 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 the class name cmainframe, objectids: id_view_bitmap, select command for messages, click Add fucction, and enter the function name onviewbimap. After onviewbimap is added, click onviewbimap entry in member functions: and click Edit code.ProgramCode. 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" in the program header, and then compile and run the program.

4. Find a larger real-color BMP bitmap and copy it to bitmap. BMP.

5. During running, click View (V)-> viewbitmap (view in English) to display bitmap. BMP bitmap.

Vi. cviewbimap Functions
1. A horizontal and vertical scroll bars are included in the customer area. You can use a scroll bar when the bitmap size is greater than the client area. When the bitmap size is smaller than the client area or full screen display, the scroll bar is invalid.

2. There is a status bar at the bottom of the customer area. The first in the status bar is the bitmap information, and the second is the bitmap display method, which can be a common function or a video function. Click the mouse in the second area to switch between the two. The third is the bitmap display ratio, which can be 1; 1 or full screen. In the third frame, click the mouse to switch between the two. In full screen display, if the bitmap is smaller than the customer area, the bitmap is enlarged. If the bitmap is larger than the customer area, the bitmap is reduced.

3. Supports file drag and drop. You can drag a bitmap file from the Resource Manager to the customer area to display the bitmap.

After the program passes debugging, you can find a large real-color bitmap or adjust the customer area to be smaller than the bitmap. In the full-screen display mode, the difference between using common functions and using video functions is compared. It can be seen that there is little difference between the two when the bitmap is enlarged, but there is a significant difference between the two when the bitmap is reduced. When the video function is used, the bitmap distortion is small and the display speed is fast.

You can also switch the screen display mode from the true color display mode to the 256 color display mode from the control panel, and then compare the differences between using common functions and using video functions to display the same true color bitmap. Now we can see the advantages of using video functions.

During full screen display, the XY ratio of the bitmap is different. To maintain the same proportion, you can adjust it in the display program.

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.