public class ArbitraryPaletteQuantizer extends ImageToImageOperation implements RGBIndex, RGBQuantizer
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 (L1) instead of Euclidean distance (L2). This saves a square root operation per distance computation.
There are more sophisticated nearest neighbor algorithms available, left for future extensions.
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();
Modifier and Type | Field and Description |
---|---|
private int[] |
BLUE |
private int[] |
GREEN |
private int |
numEntries |
private Palette |
palette |
private int[] |
RED |
INDEX_BLUE, INDEX_GREEN, INDEX_RED
Constructor and Description |
---|
ArbitraryPaletteQuantizer(Palette palette)
Creates a quantizer that will be able to map pixels (or a complete image)
to the argument palette.
|
Modifier and Type | Method and Description |
---|---|
Palette |
createPalette()
Returns a copy of the palette that was given to the
constructor of this class.
|
int |
map(int[] origRgb,
int[] quantizedRgb)
This method maps a triplet of intensity values to its quantized counterpart
and returns the palette index of that quantized color.
|
int |
map(int red,
int green,
int blue)
Finds the best match for the argument color in the palette and returns
its index.
|
void |
process()
Maps the input image to an output image, using the palette given to the constructor.
|
private void |
process(RGB24Image in,
Paletted8Image out) |
canInputAndOutputBeEqual, ensureImagesHaveSameResolution, ensureInputImageIsAvailable, ensureOutputImageResolution, getInputImage, getOutputImage, setCanInputAndOutputBeEqual, setInputImage, setOutputImage
addProgressListener, addProgressListeners, getAbort, removeProgressListener, setAbort, setProgress, setProgress
private final int[] RED
private final int[] GREEN
private final int[] BLUE
private Palette palette
private int numEntries
public ArbitraryPaletteQuantizer(Palette palette)
public Palette createPalette()
createPalette
in interface RGBQuantizer
public int map(int[] origRgb, int[] quantizedRgb)
RGBQuantizer
map
in interface RGBQuantizer
origRgb
- the three samples red, green and blue for which a good match is searched in the palettequantizedRgb
- will hold the three samples found to be closest to origRgb after the call to this methodpublic int map(int red, int green, int blue)
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 quantizedprivate void process(RGB24Image in, Paletted8Image out)
public void process() throws MissingParameterException, WrongParameterException
process
in class Operation
MissingParameterException
- if any mandatory parameter was not given to the operationWrongParameterException
- if at least one of the input parameters was
not initialized appropriately (values out of the valid interval, etc.)