Solved

Changing palette in GDI+

Posted on 2003-10-29
7
1,084 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:Keijo
  • 3
  • 2
7 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
Comment Utility
This problem is solved in VC++, see the following links:

http://www.microsoft.com/msj/1097/wicked1097.aspx
http://www.codeproject.com/bitmap/cquantizer.asp

Maybe somebody implemented this algorithm in .NET. If not, you need to use this code or translate it to C#.
I used CQuantizer code few years ago to display true-color bitmaps in 256-colors display, it gives a good results.
0
 

Author Comment

by:Keijo
Comment Utility
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
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
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.
0
 

Author Comment

by:Keijo
Comment Utility
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.
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now