A color quantizer that maps an
RGBImage
to any given palette.
This operation is restricted to RGB24Image and palettes with up to 256 colors.
It picks the color from the palette which is closest to the
color to be quantized (with the minimum distance).
This is a rather naive implementation which, for any given color
to be quantized, computes the distance between it and each color
in the palette (read: this operation is rather slow with a large palette and input image).
It uses
Manhattan distance (L
1)
instead of
Euclidean distance (L
2).
This saves a square root operation per distance computation.
There are more sophisticated
nearest
neighbor algorithms available, left for future extensions.
Usage example
This example maps an RGB truecolor image to some palette
we create.
RGB24Image image = ...; // initialize this
// create some Palette object that you want to map the image to
Palette palette = new Palette(3); // create palette with three entries
palette.put(0, 33, 00, 244); // set first color
palette.put(1, 0, 240, 193); // set second color
palette.put(2, 245, 126, 136); // set third color
ArbitraryPaletteQuantizer quantizer = new ArbitraryPaletteQuantizer(palette);
quantizer.setInputImage(image);
quantizer.process();
PixelImage quantizedImage = quantizer.getOutputImage();
createPalette
public Palette createPalette()
Returns a copy of the palette that was given to the
constructor of this class.
- createPalette in interface RGBQuantizer
- new copy of the palette this quantizer works on
map
public int map(int red,
int green,
int blue)
Finds the best match for the argument color in the palette and returns
its index.
Similar to
map(int[],int[])
, the quantized color is not required
and thus a few assignemnts are saved by this method.
red
- red sample of the pixel to be quantizedgreen
- green sample of the pixel to be quantizedblue
- blue sample of the pixel to be quantized
- index of the color in the palette that is closest to the argument color
map
public int map(int[] origRgb,
int[] quantizedRgb)
- map in interface RGBQuantizer