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:

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: 
    L_SetDisplayMode (DISPLAYMODE_FIXEDPALETTE, DISPLAYMODE_FIXEDPALETTE); 
     
    // The following code turns off the LEAD fixed palette: 
    L_SetDisplayMode (DISPLAYMODE_FIXEDPALETTE, 0); 

  2. Create the paint palette. The L_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 */   
    BITMAPHANDLE LeadBitmap;   /* Bitmap handle for the loaded image. */   
    HPALETTE hpalPaint;        /* Paint palette handle. */   
    HDC hdc; /* Device context */ 
    hdc = GetDC (hWnd);   
    hpalPaint = L_CreatePaintPalette (hdc, &LeadBitmap); 

  3. Select and realize the system palette, using the palette handle returned by the L_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. The following example uses RECT variables that are defined elsewhere, as described in the comments:

    L_PaintDC (hdc, 
        &LeadBitmap, 
        &rLeadSource,   /* Source rectangle */ 
        NULL,           /* Default source clip area */ 
        &rLeadDest,     /* Destination rectangle */ 
        &ps.rcPaint,    /* Destination clipping set by WM_PAINT */ 
        SRCCOPY);       /* Normal Paint */ 

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 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. For an example, refer to the FILEREADCALLBACK function.

Please note that if you are painting a 16-bit grayscale image to a 24-bit device, you must call L_CreatePaintPalette before you call L_PaintDC.

LEADTOOLS provides two functions for duplicating palettes. The L_DupPalette function duplicates a specified Windows palette. The L_DupBitmapPalette function duplicates the Windows palette of a specified bitmap.

Help Version 20.0.2020.4.3
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Raster Imaging C API Help