net.sourceforge.jiu.color.quantization

Class ArbitraryPaletteQuantizer

Implemented Interfaces:
RGBIndex, RGBQuantizer

public class ArbitraryPaletteQuantizer
extends ImageToImageOperation
implements RGBIndex, RGBQuantizer

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 (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.

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();
 
Author:
Marco Schmidt
Since:
0.5.0

Field Summary

private int[]
BLUE
private int[]
GREEN
private int[]
RED
private int
numEntries
private Palette
palette

Fields inherited from class net.sourceforge.jiu.ops.ImageToImageOperation

canInAndOutBeEqual, inputImage, outputImage

Fields inherited from class net.sourceforge.jiu.ops.Operation

abort, progressListeners

Fields inherited from interface net.sourceforge.jiu.data.RGBIndex

INDEX_BLUE, INDEX_GREEN, INDEX_RED

Constructor Summary

ArbitraryPaletteQuantizer(Palette palette)
Creates a quantizer that will be able to map pixels (or a complete image) to the argument palette.

Method Summary

Palette
createPalette()
Returns a copy of the palette that was given to the constructor of this class.
int
map(int red, int green, int blue)
Finds the best match for the argument color in the palette and returns its index.
int
map(int[] origRgb, int[] quantizedRgb)
void
process()
Maps the input image to an output image, using the palette given to the constructor.
private void
process(RGB24Image in, Paletted8Image out)

Methods inherited from class net.sourceforge.jiu.ops.ImageToImageOperation

canInputAndOutputBeEqual, ensureImagesHaveSameResolution, ensureInputImageIsAvailable, ensureOutputImageResolution, getInputImage, getOutputImage, setCanInputAndOutputBeEqual, setInputImage, setOutputImage

Methods inherited from class net.sourceforge.jiu.ops.Operation

addProgressListener, addProgressListeners, getAbort, process, removeProgressListener, setAbort, setProgress, setProgress

Field Details

BLUE

private final int[] BLUE

GREEN

private final int[] GREEN

RED

private final int[] RED

numEntries

private int numEntries

palette

private Palette palette

Constructor Details

ArbitraryPaletteQuantizer

public ArbitraryPaletteQuantizer(Palette palette)
Creates a quantizer that will be able to map pixels (or a complete image) to the argument palette.

Method Details

createPalette

public Palette createPalette()
Returns a copy of the palette that was given to the constructor of this class.
Specified by:
createPalette in interface RGBQuantizer
Returns:
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.
Parameters:
red - red sample of the pixel to be quantized
green - green sample of the pixel to be quantized
blue - blue sample of the pixel to be quantized
Returns:
index of the color in the palette that is closest to the argument color

map

public int map(int[] origRgb,
               int[] quantizedRgb)
Specified by:
map in interface RGBQuantizer

process

public void process()
            throws MissingParameterException,
                   WrongParameterException
Maps the input image to an output image, using the palette given to the constructor.
Overrides:
process in interface Operation

process

private void process(RGB24Image in,
                     Paletted8Image out)