Handling Palette Changes
Whenever you paint an image to a device of 256 colors or less, the current system palette is used. This topic describes how to ensure that the current system palette is the one you want to use.
First, consider that LEADTOOLS gives you the following two choices for painting to a device of 256 colors or less:
A fixed palette (LEAD's fixed palette or the Netscape palette). The fixed palette is the same for any image. It is good to use when more than one image is displayed at the same time and you want all of the images to look good. A fixed palette is always used when painting a high-color or true-color image. The default is the LEAD fixed palette, but you can specify the Netscape palette for 256-color devices.
The bitmap's own palette. This is the best available representation of the image's colors. It may be an optimized palette or a fixed palette, depending on how the image was created.
If you are using the high-level LBitmapWindow (or derived) class, you can use the LBitmapWindow::HandlePalette function to make handling palette change issues easy. This will allow the class object to do all the required processing for you.
Generally, the following order of execution ensures that the current system palette is the one you want to use:
1. |
Set the display mode to turn on or turn off the fixed palette when painting all images. |
The following code turns on the LEAD fixed palette:
LBitmapSettings::SetDisplayMode (DISPLAYMODE_FIXEDPALETTE, DISPLAYMODE_FIXEDPALETTE);
The following code turns off the LEAD fixed palette:
LBitmapSettings::SetDisplayMode (DISPLAYMODE_FIXEDPALETTE, 0);
2. |
Create the paint palette. The LBitmapBase::CreatePaintPalette function returns a handle to the palette that you want to use. If the fixed palette is turned on, this is always the handle to the fixed palette. Otherwise it is the best available palette for the specified image. The following code shows the variable declarations and the function call. (The scope of the variables is application dependent; they are shown all together here for simplicity.) |
HWND hWnd; /* The current window, usually specified in a function parameter */
LBitmapBase LeadBitmap; /* Bitmap for the loaded image. */
HPALETTE hpalPaint; /* Paint palette handle. */
HDC hdc; /* Device context */
hdc = GetDC (hWnd);
hpalPaint = LeadBitamp.CreatePaintPalette(hdc);
3. |
Select and realize the system palette, using the palette handle returned by the LBitmapBase::CreatePaintPalette function. The following shows the required function calls (with none of the normal housekeeping): |
SelectPalette (hdc, hpalPaint, FALSE);
RealizePalette (hdc);
4. |
Call one of the LEADTOOLS painting functions. |
LeadBitmap.Paint()->SetDC(hdc);
LeadBitmap.Paint()->PaintDC ();
Normally the execution order described above is handled in the code for the windows palette and painting messages: WM_PALETTECHANGED, WM_QUERYNEWPALETTE, and WM_PAINT. The DEMO example program shows how these can be implemented.
However, in some cases, such as implementing a paint-while-load feature, it is more efficient to create, select, and realize the palette without going through the message processing.