Bitmap, palette, and region

Source: Internet
Author: User
I. paletteOn a computer that uses a 256-color graphics card, each program allows a total of 256 colors. However, apart from the 20 static colors reserved by the operating system, the other 236 colors allow each application to decide on its own. So we have a palette. Applications running on such computers must select the main colors they need into the color palette of the video card for better display. To use the color palette, we first need to know the type of the computer graphics card to determine whether to use or whether to use the color palette. To obtain this information, call CDC: getdevicecaps (rastercaps) & rc_palette. If the expression is non-zero, the color palette is allowed. To use the palette, we first need to name a cpalette object and call the createpalette member function to create a palette. The parameter of this member function is a logpalette structure. The structure is as follows:

Typedef struct taglogpalette {
Word palversion; // 0x300
Word palnumentries; // Number of color palette
Paletteentry palpalentry [1]; // each color information of the palette
} Logpalette;

We can see that this structure only contains information about the color of the configuration palette. This is because MFC does not know how many colors are required, but we must expand this structure to accommodate all colors, the method is as follows: struct
{
Logpalette LP;
Paletteentry PE [2];
} LPL;
Logpalette * PLP = (logpalette *) & LPL; in this way, information of three colors can be accommodated. The structure of the paletteentry is as follows: typedef struct tagpaletteentry {
Byte pered;
Byte pegreen;
Byte peblue;
Byte peflags;
} Paletteentry; peflags in this structure are generally set to 0. After creating a palette object, we can select the cpalette object to the CDC and call CDC: selectpalette (cpalette *, bool) the member function, instead of the SelectObject member function of a common GDI object, calls the CDC: realizepalette () function to implement the color palette. In the program that uses the color palette, we cannot use the RGB macro but use the palettergb or paletteindex macro. The former uses the similar color in the color palette to implement the RGB color, the latter uses the palette index value to determine the color. In the program that uses the color palette, we need the Framework Window to respond to the wm_querypallete and wm_palettechange messages. The former indicates the settings of the color palette, and the latter indicates the changes of the system color palette due to changes in the front-end window. Refresh the View window: bool cmainframe: onquerynewpalette ()
{
M_wndview.invalidate (); Return cframewnd: onquerynewpalette ();
} Void cmainframe: onpalettechanged (cwnd * pfocuswnd)
{
Cframewnd: onpalettechanged (pfocuswnd); If (pfocuswnd! = This)
M_wndview.invalidate ();
} There is an animation not implemented by erasure and re-painting, but by changing the color of the corresponding position in the color palette. To implement this animation, we first need to call the cpalette: getpaletteentries function to read the color of the specified range in the palette to the paletteentry array, and then use the animatepalette function to load the new color information into the palette. The prototype of the two functions is as follows: uint getpaletteentries (
Uint nstartindex, // start position
Uint nnumentries, // Number of reads
Lppaletteentry lppalettecolors // paletteentry Array
) Const; void animatepalette (
Uint nstartindex,
Uint nnumentries,
Lppaletteentry lppalettecolors
); The following is a related example: paletteentry PE [3]; // This program assumes that the palette contains three colors
M_palette.getpaletteentries (, PE); // read the color of the third (indexed as 2) in the palette to the first element of the PE array.
M_palette.getpaletteentries (, & PE [1]); // read the two elements starting from the first element in the palette to the position where the PE array starts from the second element.
M_palette.animatepalette (, PE); // Replace the color in the color palette with the color in the PE Array
Invalidate (); 2. Bitmap can be divided into device-related bitmaps (DDB) and device-independent bitmaps (DIB ). The difference between the two bitmaps is whether the display and storage of bitmaps are related to the display devices. Information in Bitmap-related bitmaps is related to the current display hardware. When a file is transplanted to another computer, it cannot be correctly displayed; the information stored in a device-independent bitmap is irrelevant to the current display hardware. Therefore, you can keep the information on two different computers and ensure that the display is correct. To use bitmap in MFC, you need to use the cbitmap class. This class fully encapsulates all DDB content. To use Dib, you need to use it with cbitmap and API functions. When using DDB, we first need to use the createcompatiblebitmap (CDC, int nwidth, int nheight) member function of the cbitmap class to construct a bitmap compatible with the CDC pointing to the display device, then we use a CDC object called the memory device description table to select the bitmap into the CDC through the SelectObject member function. Here we can call the GDI function to plot the bitmap. When we need to display the bitmap in the memory to the display or printer, we can use bitblt or stretchblt function for bitblock transfer. Of course, when the bitmap used is a file in the resource, we only need to call cbitmap: loadbitmap (ID) for the dib type bitmap, we need to call the following to load the bitmap from the file :: loadImage API function. The last parameter of this function must contain lr_createdibsection, so that the loaded bitmap contains part of dibsection information. After this function is called, an hbitmap handle is returned. We only need to mount this handle to the cbitmap object. We need to obtain the width, height, and color number of DIB bitmap. We only need to call the cbitmap: GetObject function. This function requires a dibsection data structure as the parameter. In this data structure, the dsbm. bmwidth and dsbm. bmheight fields indicate the width and height of the bitmap. If the dsbmhi. biclrused field is not zero, this field indicates the number of colors contained in the bitmap. Otherwise, expression 1 <dsbmhi. bibitcount indicates the number of colors contained in the bitmap. To display a bitmap on an 8-bit display device, we need a color palette. The number of colors obtained from the dib file plays a key role. If the bitmap contains more than 256 colors, then we can directly call the cpalette: createhalftonepalette () member function to create a color palette. Otherwise, we will read the data in the dib color table to create a color palette. You can call the: getdibcolortable API function to read the color table in the dib file. This function requires an array of rgbquad data structures, which will store all the data in the color table. The following is a function for reading from DIB to creating a palette: bool cloadbmp 1doc: onopendocument (lpctstr lpszpathname)
{
If (! Cdocument: onopendocument (lpszpathname ))
Return false; hbitmap = (hbitmap): LoadImage (null, lpszpathname, image_bitmap, 0, 0, lr_loadfromfile | lr_createdibsection );
If (hbitmap = NULL)
{
Cstring string;
String. Format (_ T ("% s is not a Dib file"), lpszpathname );
Afxmessagebox (string );
Return false;
} M_bitmap.attach (hbitmap );
 
Cclientdc DC (null );
If (DC. getdevicecaps (rastercaps) & rc_palette) = 0)
Return true;
 
Dibsection Ds;
M_bitmap.getobject (sizeof (dibsection), & Ds );
Int ncolor;
If (Ds. dsbmih. biclrused! = 0)
Ncolor = Ds. dsbmih. biclrused;
Else
Ncolor = 1 <Ds. dsbmih. bibitcount;
If (ncolor> 256)
M_palette.createhalftonepalette (& DC );
Else
{
Rgbquad * prgb = new rgbquad [ncolor];
CDC memdc;
Memdc. createcompatibledc (& DC );
Cbitmap * oldbitmap = (cbitmap *) memdc. SelectObject (m_bitmap );
: Getdibcolortable (HDC) memdc, 0, ncolor, prgb );
Memdc. SelectObject (oldbitmap );
Int nsize = sizeof (logpalette) + (sizeof (paletteentry) * (ncolor-1 ));
Logpalette * PLP = (logpalette *) New byte [nsize];
PLP-> palversion = 0x300;
PLP-> palnumentries = ncolor;
For (INT I = 0; I <ncolor; I ++)
{
PLP-> palpalentry [I]. peflags = 0;
PLP-> palpalentry [I]. pered = prgb [I]. rgbred;
PLP-> palpalentry [I]. pegreen = prgb [I]. rgbgreen;
PLP-> palpalentry [I]. peblue = prgb [I]. rgbblue;
}
M_palette.createpalette (PLP );
Delete [] prgb;
Delete [] PLP;
} Return true;
} Iii. RegionsA zone is a part of the customer zone that is allowed to be drawn. When a zone is set in DC, all the drawings outside the zone will be dropped. The method for creating a region is to first define a crgn object and call createrectrgn and createellipticrgn to create a rectangle and an elliptical region, or use the createfrompath member function to create a region based on DC in beginpath () and endpath () to create a region. After creating a region, we need to call the CDC: selectcliprgn member function to select the region to the device description table for the region to take effect. Currently, And, or, and diff operations can be performed on multiple regions to create more complex regions. To implement this operation, call the crgn: combinergn (crgn, crgn, uint) member function. Here, two crgn objects in the parameter are the source object of the operation, the object that calls the member function is the target object, which must have been created before calling the operation, and the last uint parameter is a constant that represents the operation type.

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.