I am trying to figure out an algorithm that would convert an image - 8 or 32 bit - to a limited palette of 3 or 4 custom colors (not grayscale).
Is this possible ?
I am trying to use C++ (but I could get it from other languages)
It seems that Qt already has a palette - though I don't understand its usage, it seems to force a display on one of its own standard palettes, I did not see any way of customizing it.
I would appreciate some algorithm examples or suggestions.
You can convert an image to use a fixed number of colors, by passing in an array of QRgb values with convertToFormat
:
http://qt-project.org/doc/qt-5/qimage.html#convertToFormat-2
There's no dithering in that case...it just picks the nearest color to use. So it's not forcing your hand about the palette, but you have to decide what colors to use. There are a lot of "color palette from image" generators on the Internet which you might look at if you want to pick the N "best" colors to use for an image, although it's going to be a heuristic.
And if you want dithering, you might start from sources like this case from Meego:
https://github.com/openwebos/qt/blob/master/src/plugins/graphicssystems/meego/dithering.cpp
Frequently people with tasks like this will use ImageMagick: