Wince Program Design (3rd edition)-2.3 bitmap

Source: Internet
Author: User
Tags transparent color

Bitmap

A bitmap is a graphical object used to create, draw, manipulate, and receive images in a device environment. From the small winodws icon on the [start button] to the [close] button on the title bar, bitmap is everywhere in windows. Bitmap can be seen as an image composed of pixel arrays, which can be drawn on the screen. Like all images, bitmap has its own height and width. It also provides methods to determine the color of the bitmap. The bitmap is also a bit (BITs) array that describes each pixel in the bitmap.

Traditionally, bitmaps in windows are divided into two types: Device-related bitmaps (DDBS) and device-independent bitmaps (dibs ). DDBS is a bitmap closely related to the characteristics of a specific DC, which is not easily drawn on a DC with different features. Dibs, on the contrary, has nothing to do with a specific device, so it needs to carry enough information to facilitate accurate plotting on any device.

Windwos ce contains many bitmap functions that can be used in other Windows versions. The difference is that only Windows CE supports a new four-color format and different dibs manipulation methods.

Device-related bitmap
You can use the createbitmap function to create device-related bitmaps. The function prototype is as follows:
Hbitmap createbitmap (INT nwidth, int nheight, uint cplanes, uint cbitsperpel, const void * lpvbits );
Nwidth and nheight indicate the size of the bitmap. Cplanes is a historical product in which the display uses different hardware planes to implement each color in pixels. For Windows CE, this parameter must be 1. Cbitspperpel indicates the number of digits used by each pixel. The number of colors is the power of cbitspperpel. In Windows CE, 1, 2, 4, 8, 16, and 24 are allowed. As I said, the 4-color bitmap is unique to Windows CE and is not supported by other Windows platforms.

The last parameter is a pointer to the bitmap bit. In Windows CE, bits are always arranged in compressed pixel format. That is, each pixel is stored in bytes into a series of BITs, And the next pixel is immediately followed by the previous one. The first pixel of the bit array is the pixel in the upper left corner of the bitmap. The pixels are arranged along the top line of the bitmap, followed by the second line, and so on. Each line of Bitmap must be in double-byte (4-byte) alignment. To align the next row, use 0 at the end of the row. Figure 2-5 demonstrates this arrangement. The figure shows a 126*64 pixel bitmap, each of which uses 8 bits.
 
Figure 2-5 (Omitted) the byte layout in the bitmap

The createcompatiblebitmap function has the following prototype:
Hbitmap createcompatiblebitmap (HDC, int nwidth, int nheight );
You can use this function to create a bitmap that is compatible with the input device environment. Therefore, if the device environment is a four-color DC, The created bitmap is also a four-color bitmap. This function is convenient when you want to manipulate images on the screen, because it is easy to create a blank image that is compatible with the direct color of the screen.

Device-independent bitmap
The basic difference between a device-independent bitmap and a device-related bitmap is that the image stored as dibs has its own color information. Since Windows 3.0, which uses BMP as the extension, almost every bitmap file contains the information required to create DIB in windows.

In early windows, it was essential for programmers to write programs to manually read DIB files and convert data into bitmaps. Now, this tedious task can be completed through the shloaddibitmap function specific to Windows CE. The function prototype is as follows:
Hbitmap shloaddibitmap (lpctstr szfilename );
This function loads the bitmap directly from the bitmap file and provides the bitmap handle. In Windows XP, you can use the LoadImage function with the lr_loadfromfile parameter flag to perform the same processing. However, LoadImage in Windows CE does not support this flag.

DiB Fragment
Although bitmap files are easily loaded in Windows CE, sometimes you have to read the screen image, manipulate the image, and redraw the image to the screen. This is one of the better aspects of dibs than DDBS. Although BIT data of the device-related bitmap can be obtained, the format of the buffer zone is directly dependent on the screen format. By using DIB, or more accurately, using DIB fragments, your program can read bitmap into a buffer in a predefined format without worrying about the format of the display device.

Although many Dib creation functions have been added since Windows 3.0, Windows CE only supports some Dib functions in XP. Createdibsection is the first of these functions:
Hbitmap createdibsection (HDC, const bitmapinfo * pbmi, uint iusage, void * ppvbits,
Handle hsection, DWORD dwoffset );
Because they were added to the Win32 API quite late, dib fragments may be fresh for programmers. The Dib fragment is used to improve the performance of applications that directly manipulate bitmap on winodows nt. In short, the dib segment allows programmers to select DIB in a device environment when directly accessing bitmap data. To achieve this, the dib segment combines a buffer zone with the memory DC, which also contains the BIT data of the DC. Because the image is mapped to a DC, you can use other graphical functions to modify the image. At the same time, the original BIT data in Dib format in DC can be directly manipulated. It is good to improve the performance of NT, but for window ce programmers, it is the most valuable to simplify the usage and operation of Bitmap content.

The most important parameter in this function is the pointer to the bitmapinfo structure. This structure describes the layout and color composition of device-independent bitmap. It contains a bitmapinfoheader structure and an rgbquad array that represents the bitmap palette.

The bitmapinfoheader is defined as follows:
Typedef struct tagbitmapinfoheader {
DWORD bisize;
Long biwidth;
Long biheight;
Word biplanes;
Word bibitcount;
DWORD bicompression;
DWORD bisizeimage;
Long bixpelspermeter;
Long biypelspermeter;
DWORD biclrused;
DWORD biclrimportant;
} Bitmapinfoheader;
As you can see, this structure contains much more information than the parameters passed to createbitmap. The first field is the size of the structure and must be filled by the caller. It is used to distinguish the bitmapcoreinfoheader structure inherited by the OS/2 manager. Biwidth, biheight, biplanes, and bibitcount are similar to parameters with the same name in createbitmap. However, the positive and negative signs of biheight specify the arrangement of bit arrays. If biheight is a positive number, the bits are arranged in the ascending and bottom formats, which is the same as createbitmap. If biheight is a negative number, the bit array is arranged in the bottom-up format, that is, the bottom row of the bitmap is defined at the top of the bit array. Like createbitmap, biplanes must be set to 1.

Bicompression indicates the compression method used by bit arrays. In Windows CE, the supported labels are bi_rgb, indicating that the buffer zone is not compressed; bi_bitfields, indicating that the pixel format is defined in the first three entries of the color table. Bisizeimage indicates the size of a bit array. However, when the bi_rgb flag is used, bisizeimage can be set to 0, indicating that the size of the array is calculated using the size (dimensions) and number of pixels provided in the bitmapinfoheader structure.

Bixpelspermeter and biypelspermeter provide accurate image size information. However, for createbibsection, these parameters can be set to 0. Biclrused indicates the number of colors in the actually used color palette. In a 256-color image, there are 256 entries in the color palette, but you may only need about 100 different colors. This field helps the palette Manager-Windows manage Color Matching Parts-match the color in the system palette with the color required by the bitmap. Biclrimportant further points out the desired color. For bitmaps of more colors, these two fields are set to 0, indicating that all colors are used and all colors are important.

As mentioned above, the bitmapinfoheader structure is followed by the rgbquad structure array. The structure is defined as follows:
Typedef struct tagrgbquad {/* rgbq */
Byte rgbblue;
Byte rgbgreen;
Byte rgbred;
Byte rgbreserved;
} Rgbquad

This structure allows 256 levels of color (SHADE) in red, blue, and green ). Although this structure can be used to create almost any color, the colors actually rendered on the device are limited by the colors that the device can display.

In general, the rgbquad structure array describes the dib palette. The color palette is the color list in the bitmap. If a bitmap has a palette, each entry of the bitmap array contains a palette index of each pixel color instead of a color. Although it is unnecessary for a monochrome bitmap, the color palette is very important when you draw a color bitmap on a color device. For example, although each pixel in a 256-color bitmap is one byte, this Byte points to a 24-bit value representing a red, green, and blue color. Therefore, although the 256 color bitmap can only contain 256 different colors, the 24-bit color palette is used for color painting, therefore, each of these colors can use one of the thousands, 6, and millions of colors. To facilitate 32-bit use, each palette entry containing only 24-bit color information is expanded to 32-bit width, which is also the source of rgbquad name. (Note: quad has four meanings)

Only two of the four remaining parameters of createdibsection are used for Windows CE. Iusage indicates how colors in the palette are drawn. If this parameter is dib_rgb_colors, it indicates that the BIT data in the bitmap contains all the RGB color information of each pixel. dib_pal_colors indicates that the bitmap pixel contains the index of the selected palette in the DC. Ppvbits is a pointer to the BIT data that forms a bitmap image. The last two parameters, hsection and dwoffset, are not supported by Windows CE and must be set to 0. In other versions of Windows, they allow the use of memory ing files for outgoing data. Because Windows CE does not support memory ing files, they cannot be supported by createdibsection.

Getdibcolortable and setdibcolortable are two functions used to manage the dib palette. Their prototype is as follows:
Uint getdibcolortable (HDC, uint ustartindex, uint centries, rgbquad * pcolors );
And
Uint setdibcolortable (HDC, uint ustartindex, uint centries, rgbquad * pcolors );
For these two functions, ustartindex indicates the first entry to the palette to be set or queried. Centries indicates how many palette entries will change. The pointer to the rgbquad array is a color array used to set (for setdibcolortable) or to query (for getdibcolortable.

Draw bitmap
It is good to create and load bitmaps, but if the bitmaps you create cannot be drawn on the screen, there is no big use. Creating a bitmap may not be as simple as you think. Before a bitmap is drawn to a screen DC, you must first select a DC for the bitmap and then copy it to the screen device environment. Although this process may seem a bit confusing, there is a reasonable reason.

The process of selecting a bitmap to a device environment is similar to that of selecting a logical font to a device environment. Let's turn the ideal into reality. Just as windows needs to find the most likely matching font for the requested font, the bitmap selection process must also find the available color on the matching device for the color required by the bitmap. The bitmap can be drawn to the screen only after this process is completed. To help complete this step, Windows provides a dual DC-memory device environment.

To create a memory device environment, you can use the createcompatibledc function:
HDC createcompatibledc (HDC );
This function creates a memory DC compatible with the current screen DC. Once created, you can use the SelectObject function that you used to select the logical font to select the source bitmap to the memory DC. Finally, use bitblt or stretchblt to copy the bitmap from the memory DC to the screen DC.

The main force of Bitmap functions is
Bool bitblt (HDC hdcdest, int nxdest, int nydest, int nwidth, int nheight, HDC hdcsrc, int nxsrc, int nysrc, DWORD dwrop );

The bitblt function is pronounced as "bit bits". It is an interesting function that operates in the device environment, rather than in the memory. It is sometimes a very special function. The first parameter is the handle of the target device environment on which the bitmap will be copied. The following four Parameters specify the position and size of the destination rectangle where the bitmap is located. The following three Parameters specify the handle of the Source Device environment and the position in the DC in the upper left corner of the source image.

The last parameter dwdrop specifies how the image is copied from the source device environment to the target device environment. The drop-down code specifies how the source bitmap and the current target device combine to generate the final image. The drop-down code is srcopy, indicating a simple copy of the source image. The drop-down code is srcpaint, which indicates that the source image is operated or operated between the source image and the target image. The ROP code is SRCINVERT, which indicates copying a logical inversion image, which is essentially a negative source image. Some drop-down codes also use the selected brush as a factor for calculating the result image. There are a lot of drop-down code, so it is impossible to cover all of them here. To get a full list, please refer to the Windows CE programming documentation.

The following code snippet summarizes how to draw a bitmap:

// Create a DC that matches the device.
Hdcmem = createcompatibledc (HDC );

// Select the bitmap into the compatible device context.
Holdsel = SelectObject (hdcmem, hbitmap );

// Get the bitmap dimensions from the bitmap.
GetObject (hbitmap, sizeof (Bitmap), & BMP );

// Copy the bitmap image from the memory DC to the screen DC.
Bitblt (HDC, rect. Left, rect. Top, BMP. bmwidth, BMP. bmheight,
Hdcmem, 0, 0, srccopy );

// Restore original bitmap selection and destroy the memory DC.
SelectObject (hdcmem, holdsel );
Deletedc (hdcmem );

Create a memory device environment and select the DC for the drawn bitmap. Because you may not store the size of the bitmap to be drawn, you can call GetObject to obtain it. GetObject returns information about the graphic object. In this example, it is a bitmap. This useful function can be used to query information about fonts and other image objects. Next, use bitbits to copy the bitmap to the DC Screen. To clean up the memory, You Need To unselect the bitmap from the memory device environment and delete the memory DC using deletedc. Do not confuse deletedc with releasedc. releasedc releases a display DC. Deletedc should only be used with createcompatibledc, and releasedc should only be used with getdc or getwindowsdc.

In addition to copying bitmap, you can also use stretchblt to stretch or compress the bitmap. The function is prototype as follows:
Bool values (HDC hdcdest, int values, int nyorigindest, int nwidthdest, int values, HDC hdcsrc, int nxoriginsrc, int nyoriginsrc, int nwidthsrc, int nheightsrc, DWORD dwrop );
The parameters in stretchblt are basically the same as those in bitblt, but one exception is that you can specify the width and height of the source image. Likewise, the drop-down code here specifies how the source and destination are combined to generate the final image.

Windows CE has another bitmap function transparentimage. The prototype is as follows:
Bool transparentimage (HDC hdcdest, long dstx, long dsty, long dstcx,
Long dstcy, handle HSRC, long srcx, long srcy,
Long srccx, long srccy, colorref transparentcolor );

This function is similar to stretchblt, but there are two important exceptions. First, you can specify the transparent color in the bitmap. When the bitmap is copied to the target, the transparent pixels in the bitmap are not copied. The second difference is that the HSRC parameter is either a device environment or a bitmap handle. This parameter allows you to ignore the requirement that the source image must be selected into a device environment before drawing an image on the screen. The transparentimage function is basically the same as the transparentblt function in Windows 2000, except that transparetblt cannot directly use bitmap as the source image.

Like other versions of Windows, Windows CE also supports two other bits functions: patblt and maskblt. The patblt function combines the selected image painter with the current image in the Target DC to generate the result image. I will talk about image painter later in this chapter. The maskblt function is similar to bitblt, but contains a mask image, which is used to draw only a part of the source image to the Target DC.

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.