public class MedianCutQuantizer extends ImageToImageOperation implements RGBIndex, RGBQuantizer
RGB24Image
.
MedianCutQuantizer quantizer = new MedianCutQuantizer(); quantizer.setInputImage(image); quantizer.setPaletteSize(256); quantizer.process(); PixelImage quantizedImage = quantizer.getOutputImage();If you want to combine Median Cut quantization with error diffusion dithering to improve the visual quality of the output, try the
ErrorDiffusionDithering
class.
However, note that noise is introduced into the image with dithering methods so
that the resulting image may not be suitable for automatic processing.
MedianCutContourRemoval
,
ErrorDiffusionDithering
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_METHOD_REPR_COLOR
The default method to determine the representative color
from a list of colors.
|
private boolean |
doNotMap |
private RGBColorList |
list |
private int |
maxValue |
private int |
method |
static int |
METHOD_REPR_COLOR_AVERAGE
Constant value for a method of determining the representative color
for a set of colors by computing the average of all samples for each
of the three components red, green and blue.
|
static int |
METHOD_REPR_COLOR_MEDIAN
Constant value for a method of determining the representative color
for a set of colors by picking the median value of all samples for each
of the three components red, green and blue.
|
static int |
METHOD_REPR_COLOR_WEIGHTED_AVERAGE
Constant value for a method of determining the representative color
for a set of colors by computing the weighted average of all samples for each
of the three components red, green and blue.
|
private boolean |
outputTruecolor |
private int |
paletteSize |
private MedianCutNode |
root |
INDEX_BLUE, INDEX_GREEN, INDEX_RED
Constructor and Description |
---|
MedianCutQuantizer()
Creates a MedianCutQuantizer object and
initializes its fields to default values.
|
Modifier and Type | Method and Description |
---|---|
private void |
addNodes(MedianCutNode[] nodeList,
MedianCutNode node) |
private RGBColorList |
createColorList(RGB24Image image) |
MedianCutNode[] |
createLeafList()
Creates a linear list of leaf nodes.
|
Palette |
createPalette()
Creates a palette with the representative colors of all leaf nodes.
|
void |
findAllRepresentativeColors() |
private MedianCutNode |
findLeafToBeSplit(MedianCutNode node)
Traverses tree given by argument node and returns leaf with largest distribution
of samples for any of its three components.
|
MedianCutNode |
findNearestNeighbor(int[] rgb)
For a given RGB value, searches the node in the internal node tree whose
representative color is closest to this color.
|
int |
findNearestNeighbor(MedianCutNode[] nodes,
int red,
int green,
int blue)
For each node in the argument array computes the distance between the
representative color of that node and the color given by the three
argument samples.
|
void |
findPalette() |
private int[] |
findRepresentativeColor(int index1,
int index2)
Computes a representative color for a set of colors in the color list.
|
private void |
findRepresentativeColors(MedianCutNode node)
Calls findRepresentativeColor with node if node is a leaf.
|
int |
getMethodToDetermineRepresentativeColors()
Returns the method (to be) used to determine the representative
color for the list of colors of a node.
|
int |
getPaletteSize()
Returns the number of colors in the destination image.
|
boolean |
getTruecolorOutput()
Returns if this operation is supposed to generate truecolor or
paletted output.
|
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.
|
void |
mapImage(RGB24Image in,
Paletted8Image out) |
void |
mapImage(RGB24Image in,
RGB24Image out) |
void |
process()
This method does the actual work of the operation.
|
void |
setAllPaletteIndexValues() |
void |
setMapping(boolean doMap)
Defines whether process will map the input image to an output image.
|
void |
setMethodToDetermineRepresentativeColors(int newMethod)
Sets the method to determine the representative color
for a list of colors.
|
private int |
setPaletteIndexValues(MedianCutNode node,
int index)
Recursively visits node and its descendants, assigning ascending
palette index values to leaves via MedianCutNode.setPaletteIndex(int).
|
void |
setPaletteSize(int newPaletteSize)
Sets the number of colors that this operations is supposed to reduce
the original image to.
|
void |
setTruecolorOutput(boolean useTruecolor)
Lets the user specify if the output image is to be truecolor
(argument useTruecolor is
true ) or paletted
(argument useTruecolor is false ). |
void |
splitNode(MedianCutNode node) |
canInputAndOutputBeEqual, ensureImagesHaveSameResolution, ensureInputImageIsAvailable, ensureOutputImageResolution, getInputImage, getOutputImage, setCanInputAndOutputBeEqual, setInputImage, setOutputImage
addProgressListener, addProgressListeners, getAbort, removeProgressListener, setAbort, setProgress, setProgress
public static final int METHOD_REPR_COLOR_AVERAGE
public static final int METHOD_REPR_COLOR_WEIGHTED_AVERAGE
public static final int METHOD_REPR_COLOR_MEDIAN
public static final int DEFAULT_METHOD_REPR_COLOR
setMethodToDetermineRepresentativeColors(int)
.private boolean doNotMap
private RGBColorList list
private int maxValue
private int method
private boolean outputTruecolor
private int paletteSize
private MedianCutNode root
public MedianCutQuantizer()
private void addNodes(MedianCutNode[] nodeList, MedianCutNode node)
private RGBColorList createColorList(RGB24Image image) throws OperationFailedException
OperationFailedException
public MedianCutNode[] createLeafList()
findPalette()
was successfully run before.public Palette createPalette()
findPalette()
was successfully run before.createPalette
in interface RGBQuantizer
private MedianCutNode findLeafToBeSplit(MedianCutNode node)
public MedianCutNode findNearestNeighbor(int[] rgb)
rgb
- the color for which a match is searched; the array must have at least
three entries; RGBIndex
constants are used to address the samplespublic int findNearestNeighbor(MedianCutNode[] nodes, int red, int green, int blue)
public void findPalette()
public void findAllRepresentativeColors()
private int[] findRepresentativeColor(int index1, int index2)
RGBIndex
.
The method of determining the color (the REPR_xxx constants from this class)
has been given to the constructor.private void findRepresentativeColors(MedianCutNode node)
public int getMethodToDetermineRepresentativeColors()
DEFAULT_METHOD_REPR_COLOR
.public int getPaletteSize()
public boolean getTruecolorOutput()
setTruecolorOutput(boolean)
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 void mapImage(RGB24Image in, RGB24Image out)
public void mapImage(RGB24Image in, Paletted8Image out)
public void process() throws MissingParameterException, OperationFailedException, WrongParameterException
Operation
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.)OperationFailedException
public void setAllPaletteIndexValues()
public void setMapping(boolean doMap)
public void setMethodToDetermineRepresentativeColors(int newMethod)
newMethod
- the new method, one of the METHOD_xyz constants in this classprivate int setPaletteIndexValues(MedianCutNode node, int index)
public void setPaletteSize(int newPaletteSize)
newPaletteSize
- the number of colorsjava.lang.IllegalArgumentException
- if the argument is smaller than 1 or larger than 256getPaletteSize()
public void setTruecolorOutput(boolean useTruecolor)
true
) or paletted
(argument useTruecolor is false
).
If the color type is to be changed afterwards, use PromoteToRgb24
to convert from paletted to truecolor.
Reducing a truecolor image that uses only 256 or less colors to
a paletted image can be done with AutoDetectColorType.useTruecolor
- public void splitNode(MedianCutNode node)