public class OctreeColorQuantizer extends ImageToImageOperation implements RGBIndex, RGBQuantizer
MedianCutQuantizer
.
Note that you can improve the quality by applying a dithering algorithm.
See ErrorDiffusionDithering
.
MemoryRGB24Image image = ...; // initialize OctreeColorQuantizer ocq = new OctreeColorQuantizer(); ocq.setInputImage(image); ocq.setPaletteSize(16); ocq.process(); PixelImage quantizedImage = ocq.getOutputImage();
Modifier and Type | Field and Description |
---|---|
private int[] |
blueValues |
static int |
DEFAULT_PALETTE_SIZE
The default number of colors in the palette.
|
private int[] |
greenValues |
private Palette |
palette |
private int |
paletteSize |
private int[] |
redValues |
private OctreeNode |
root |
INDEX_BLUE, INDEX_GREEN, INDEX_RED
Constructor and Description |
---|
OctreeColorQuantizer() |
Modifier and Type | Method and Description |
---|---|
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) |
canInputAndOutputBeEqual, ensureImagesHaveSameResolution, ensureInputImageIsAvailable, ensureOutputImageResolution, getInputImage, getOutputImage, setCanInputAndOutputBeEqual, setInputImage, setOutputImage
addProgressListener, addProgressListeners, getAbort, removeProgressListener, setAbort, setProgress, setProgress
public static final int DEFAULT_PALETTE_SIZE
setPaletteSize(int)
.private int paletteSize
private OctreeNode root
private Palette palette
private int[] redValues
private int[] greenValues
private int[] blueValues
private int assignPaletteIndexValues(OctreeNode node, int index)
node
- the node of the octree that will itself (and its children) be processedindex
- the current index in the palette index assignment procedurepublic Palette createPalette()
RGBQuantizer
createPalette
in interface RGBQuantizer
public void init() throws MissingParameterException, WrongParameterException
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.MissingParameterException
- if parameters like the input image are missingWrongParameterException
- if parameters exist but are of the wrong typeprivate int initOctree()
private void initPalette(OctreeNode node, Palette palette)
public int map(int[] origRgb, int[] quantizedRgb)
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
private void mapImage()
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.)private void pruneOctree()
paletteSize
argument in the constructor
#OctreeColorQuantizer(int)
.public void setPaletteSize(int newPaletteSize)