Link to home
Start Free TrialLog in
Avatar of Keijo
Keijo

asked on

Changing palette in GDI+

How can I change a 16- or 24-bit palette down to a fixed 8-bit color palette so that I can pre-define the colors in the target palette? That is, I have a set of 255 colors and no other colors can be used. The transformation must use some kind of advanced rendering instead of a straight next possible color transformation.

Keijo
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Keijo
Keijo

ASKER

Thanks for your answer Alex,

There's still one problem with the CQuantizer, as it generates the optimal palette itself. As I mentioned, I'm stuck with a pre-defined palette that can not be changed. I took a quick look at the source of CQuantizer.ProcessImage and at least I couldn't figure out how to force the palette.

Keijo
CQuantizer generates optimal color palette from image.
Reading your question again I see that you need very simple algorithm: convert image to some color palette. For each pixel in the image you need to find the closest match in the palette, like GetNearestPaletteIndex API does. This is simple task: compare pixel value with each palette entry. If values are equal, use this entry. If not, continue search and select the closest match.
Avatar of Keijo

ASKER

I'm actually developing for a platform that supports only 256 fixed colors, and when other colors are encountered they are automatically converted to the closest match. The problem with this is that the images tend to turn out pretty crappy because this way, so an algorithm that does some more advanced rendering is definitely needed.
Unfortunately, there is no such algorithm. Finding the best match for each pixel is the best possible way. The problem is that current palette is not good for the image. The solution is to find optimal palette for each image, and this is what CQuantizer class does.
Consider the color palette which contains entries (0, 0, 0),  (1,1,1) ... (255,255,255) - gray-scale palette and image with only one color - (255,0,0). Using this palette you cannot draw this simple image correctly.
Old PC graphic cards supported only 256 colors, but programmers could select color palette. Each program could create it's own color palette and activate it when it is in foreground. CQuantizer was written in these old times. If your platform supports 256 colors allowing to change current palette dynamically, use CQuantizer algorithm. If your platform supports only 256 fixed colors, there is nothing to do.