![]() |
The open imaging DSP library
|
Convert images from RGB to indexed color mode.
The palettize and depalettize operations bring the ability to reduce the number of colors in the image down to a compact palette.
First the image is imported from a buffer or another source.
The palette need to be allocated. In some cases, stack space will be too small for the palette buffer and need to be made a global array variable.
Here 3 is for 3 byte per pixel (RGB24) and 4 is because of the PALETTE4
format is selected.
Then the color palette can be generated from the content of the image. The number 1000
refers to the number of pixels sampled from the image used to generate the palette.
The algorithm used is k-mean. It can be run multiple times to run multiple k-mean cycles and improve the palette to better match the image. However, the palette can remain the same across several frames and in such case, a single call can be done per frame to continuously adjusting the color palette to the new frames.
Now that a palette is generated, it is possible to use it to encode the input image:
For getting RGB data back, it is possible to run the opposite operation with the same palette:
See Supported operations for the list of all palettization format. The conversion is always between RGB24 and one of the palette size.