Solved

Changing palette in GDI+

Posted on 2003-10-29
7
1,087 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
ID: 9640635
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
ID: 9641198
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
ID: 9641264
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
ID: 9648557
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
ID: 9648740
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 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