net.sourceforge.jiu.color.quantization
Class OctreeColorQuantizer
- RGBIndex, RGBQuantizer
public class OctreeColorQuantizer
Performs the octree color quantization algorithm for a given RGB truecolor image.
The quality is usually somewhat inferior to the results of
MedianCutQuantizer
.
Note that you can improve the quality by applying a dithering algorithm.
See
ErrorDiffusionDithering
.
Usage example
This reduces some RGB24Image image to a 16 color paletted image:
MemoryRGB24Image image = ...; // initialize
OctreeColorQuantizer ocq = new OctreeColorQuantizer();
ocq.setInputImage(image);
ocq.setPaletteSize(16);
ocq.process();
PixelImage quantizedImage = ocq.getOutputImage();
Credits
private int | assignPaletteIndexValues(OctreeNode node, int index) - If node is a leaf node, this method assigns palette index values
and determines the representative color, otherwise it simply
recursively calls itself for all child nodes.
|
Palette | createPalette() - Return a Palette object with the list of colors to be used in the quantization
process.
|
void | init() - Creates an octree and prepares this quantizer so that colors can be mapped to
palette index values.
|
private int | initOctree()
|
private void | initPalette(OctreeNode node, Palette palette)
|
int | map(int[] origRgb, int[] quantizedRgb) - Maps an RGB color
origRgb to one of the colors in the
color map; that color will be written to quantizedRgb
and its palette index will be returned.
|
private void | mapImage()
|
void | process() - Initializes an octree, reduces it have as many leaves (or less) as
the desired palette size and maps the original image to the newly-created
palette.
|
private void | pruneOctree() - Reduces the octree until it has as many leaves (or less) than specified
by the
paletteSize argument in the constructor
OctreeColorQuantizer(int) .
|
void | setPaletteSize(int newPaletteSize)
|
DEFAULT_PALETTE_SIZE
public static final int DEFAULT_PALETTE_SIZE
The default number of colors in the palette.
Will be used when no other value is specified via
setPaletteSize(int)
.
blueValues
private int[] blueValues
greenValues
private int[] greenValues
paletteSize
private int paletteSize
redValues
private int[] redValues
assignPaletteIndexValues
private int assignPaletteIndexValues(OctreeNode node,
int index)
If node is a leaf node, this method assigns palette index values
and determines the representative color, otherwise it simply
recursively calls itself for all child nodes.
The updated index value is returned.
It is increased whenever a leaf is assigned that index value.
node
- the node of the octree that will itself (and its children) be processedindex
- the current index in the palette index assignment procedure
- updated index value; may have been increased while node or its child(ren) -
were assigned index values
createPalette
public Palette createPalette()
Return a Palette object with the list of colors to be used in the quantization
process.
That palette may be fixed or created specifically for a given input image.
- createPalette in interface RGBQuantizer
- Palette object for destination image
init
public void init()
throws MissingParameterException,
WrongParameterException
Creates an octree and prepares this quantizer so that colors can be mapped to
palette index values.
If you use
process()
you must not call this method.
On the other hand, if you want to do the mapping yourself - maybe if you
want to do mapping and dithering interchangeably - call this method first,
then do the mapping yourself.
initOctree
private int initOctree()
map
public int map(int[] origRgb,
int[] quantizedRgb)
Maps an RGB color origRgb
to one of the colors in the
color map; that color will be written to quantizedRgb
and its palette index will be returned.
- map in interface RGBQuantizer
origRgb
- the color to be mapped to the best-possible counterpart in the
palette; the array is indexed by the constants from RGBIndex
quantizedRgb
- the resulting color from the palette will be written
to this array; it is also indexed by the constants from RGBIndex
- index of the found color in the palette
mapImage
private void mapImage()
pruneOctree
private void pruneOctree()
Reduces the octree until it has as many leaves (or less) than specified
by the paletteSize
argument in the constructor
OctreeColorQuantizer(int)
.
setPaletteSize
public void setPaletteSize(int newPaletteSize)