com.davisor.graphics
Class OctreeColorReducer

java.lang.Object
  extended bycom.davisor.graphics.ColorReducer
      extended bycom.davisor.graphics.DynamicColorReducer
          extended bycom.davisor.graphics.OctreeColorReducer
All Implemented Interfaces:
java.awt.image.BufferedImageOp

public class OctreeColorReducer
extends DynamicColorReducer

OctreeColorReducer implements a fast high-quality color palette size reducers. As the name suggests, this class uses a well known octree color quantization algorithm. The algorithm and it's many variations have been widely published in basic computer graphics literature.

This particular implementation uses various speed optimizations that produce slightly less optimal color quantizations, but significantly faster than a stricly optimal solution would. The current implementation works also only with RGB color models with 8 bits per component, and with BITMASK transparency. In particular, colors with the alpha -value above a 0x80 threshold are considered fully opaque, other fully transparent.

This implementation supports also color component quantization, by allowing a color bit mask to be applied on colors before they are inserted into the tree. Please see addColor(int) for more information.

Since:
JDK1.2

Nested Class Summary
protected  class OctreeColorReducer.ColorNode
          ColorNode represents one OctreeColorReducer.ColorTree color node.
protected  class OctreeColorReducer.ColorTree
          ColorTree implements a color octree that orginizes colors into a hierarchy of similar colors.
 
Field Summary
protected  OctreeColorReducer.ColorTree M_tree
          Current color tree.
 
Fields inherited from class com.davisor.graphics.ColorReducer
COLORMASK_12BITS, COLORMASK_18BITS, COLORMASK_24BITS, COLORMASK_6BITS, M_colorMask
 
Constructor Summary
OctreeColorReducer(int maxColors)
          Creates a new color reducer.
OctreeColorReducer(int maxColors, int colorMask)
          Creates a new color reducer with given color mask.
OctreeColorReducer(int maxColors, int colorMask, java.awt.image.RenderedImage image)
          Creates a new color reducer with given color mask, then applies the colors in given image to it.
OctreeColorReducer(int maxColors, java.awt.image.RenderedImage image)
          Creates a new color reducer, then applies the colors in given image to it.
 
Method Summary
 void addColor(int argb)
          Adds a ARGB color into the reducer source color set.
 int getColor(int argb)
          Gets the replacement color value from the current color tree for the given color.
 int getColorCount()
          Gets the number of different colors, including the fully transparent color, if defined.
 int[] getColorsFromTree()
          Simple helper method to get access to the tree colors
 int getIndex(int argb)
          Gets the replacement color index from the current color tree for the given color.
 java.awt.image.IndexColorModel getPalette()
          Gets current reduced color palette.
 
Methods inherited from class com.davisor.graphics.DynamicColorReducer
addColors
 
Methods inherited from class com.davisor.graphics.ColorReducer
createCompatibleDestImage, filter, getBounds2D, getColor, getColorBits, getColorMask, getColorMask, getColorMask, getPoint2D, getRenderingHints, setAlphaBits, setAlphaMask, setBlueBits, setBlueMask, setColorBits, setColorMask, setGreenBits, setGreenMask, setRedBits, setRedMask
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

M_tree

protected OctreeColorReducer.ColorTree M_tree
Current color tree.

Constructor Detail

OctreeColorReducer

public OctreeColorReducer(int maxColors)
Creates a new color reducer.

Parameters:
maxColors - the number of colors this reducer reduces colors to

OctreeColorReducer

public OctreeColorReducer(int maxColors,
                          int colorMask)
Creates a new color reducer with given color mask.

Parameters:
maxColors - the number of colors this reducer reduces colors to
colorMask - color quantization mask
See Also:
ColorReducer.setColorMask(int)

OctreeColorReducer

public OctreeColorReducer(int maxColors,
                          java.awt.image.RenderedImage image)
Creates a new color reducer, then applies the colors in given image to it.

Parameters:
maxColors - the number of colors this reducer reduces colors to
image - image to scan colors from
See Also:
DynamicColorReducer.addColors(java.awt.image.RenderedImage), ColorReducer.setColorMask(int)

OctreeColorReducer

public OctreeColorReducer(int maxColors,
                          int colorMask,
                          java.awt.image.RenderedImage image)
Creates a new color reducer with given color mask, then applies the colors in given image to it.

Parameters:
maxColors - the number of colors this reducer reduces colors to
colorMask - color quantization mask
image - image to scan colors from
See Also:
DynamicColorReducer.addColors(java.awt.image.RenderedImage), ColorReducer.setColorMask(int)
Method Detail

getColor

public int getColor(int argb)
Gets the replacement color value from the current color tree for the given color.

Color palettes provide typically only limited set of transparent colors, as typical color palette contains only fully opaque colors, and only one fully transparent color, if even that. The transparency component of the returned palette color may therefore be significantly different that of the source color.

Specified by:
getColor in class ColorReducer
Parameters:
argb - standard sRGB color value (with alpha)
Returns:
replacement sRGB color value (with alpha)
See Also:
addColor(int), getIndex(int), OctreeColorReducer.ColorTree.getColor(int)

getIndex

public int getIndex(int argb)
Gets the replacement color index from the current color tree for the given color. The returned index will refer to the latest color palette generated by the getPalette() method.

Color palettes provide typically only limited set of transparent colors, as typical color palette contains only fully opaque colors, and only one fully transparent color, if even that. The transparency component of the returned palette color may therefore be significantly different that of the source color.

Specified by:
getIndex in class ColorReducer
Parameters:
argb - standard sRGB color value (with alpha)
Returns:
replacement color index
Throws:
java.lang.IllegalStateException - if the color tree has not been assosiated with any color palette
See Also:
addColor(int), getColor(int), OctreeColorReducer.ColorTree.getIndex(int)

getColorsFromTree

public int[] getColorsFromTree()
Simple helper method to get access to the tree colors


getPalette

public java.awt.image.IndexColorModel getPalette()
Gets current reduced color palette.

Specified by:
getPalette in class ColorReducer

addColor

public void addColor(int argb)
Adds a ARGB color into the reducer source color set. The given source color will be first filtered against current color mask, after which the resulting quantized color is added into current color octal tree. Changing the color mask while colors are being added into the tree will not directly affect the tree or colors already added into it. Instead, new colors will just be filtered against the new mask.

It must also be noted that color masking is applied only once, just before a source color is being added into the color tree. The actual octal tree color tree algorithm will then work with full sRGB colorspace, with full 8 bits color components. The resulting reduced color tree may therefore have colors that contain color component bits masked out from the source colors.

Color masking adds another level of color quantization on top of the reduction provided by the Octree algorithm. Color component level masking is usefull in particular when colors are prepared for LCD displays that support less than 256 color component intensity levels.

Specified by:
addColor in class DynamicColorReducer
Parameters:
argb - standard 32 bit ARGB color value to add
See Also:
getColor(int), getIndex(int), ColorReducer.setColorMask(int), OctreeColorReducer.ColorTree.addColor(int)

getColorCount

public int getColorCount()
Gets the number of different colors, including the fully transparent color, if defined. The number may be smaller than the given maximum number of colors, but never greater.



Copyright © 2001-2004 Davisor Oy. All Rights Reserved.