| Herd Software Development
|=
DaVinci Graphics Library
|==
DaVinci Documentation Home Search Order


Leonardo: The DIBSelectColors function

Produces a "template" image with an optimal palette. The palette can then be applied to an image reduced in color depth using DitherTo8.

HDIB API DIBSelectColors(
HDIB hDIB,
int DesiredColors);

hDIB HDIB Handle of the DIB to be analyzed.

DesiredColors int Desired number of colors (0..255; 236 is recommended as the upper limit).

Returns

The return value is the handle of a one-pixel DIB with an optimized 8 bit palette. The DIB is intended for use only as a "palette holder" image, and is generally used as the lpPal parameter in DitherTo8. can be used where the parameter refers lpPal to the DIB with the optimized color palette.

Remarks

DitherTo8 uses a simple color selection algorithm based on the use of the most frequently used colors in the source image. This does not always produce the best quality output. DIBSelectColors uses the "median cut" algorithm which very often produces a more pleasing image. This algorithm requires significantly more CPU time than direct use of DitherTo8's internal algorithm.

If the video driver is set for 256 color display, then only one optimized palette can be displayed system-wide. All other images onscreen at that moment will have to use this palette.

Example

HDIB                hDIBSelectPalette = DIBSelectColors(Data->hDIB, 236);
LPBITMAPINFOHEADER  lpbmiSelectPalette;

if (hDIBSelectPalette)
{
   lpbmiSelectPalette = (LPBITMAPINFOHEADER) GlobalLock(hDIBSelectPalette);

   hNewDIB = DitherTo8((LPBITMAPINFOHEADER) GlobalLock(Data->hDIB), 0, 0, lpbmiSelectPalette);

   GlobalUnlock(hDIBSelectPalette);
   GlobalFree(hDIBSelectPalette);
}