com.davisor.graphics
Class ColorReducer

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

public abstract class ColorReducer
extends java.lang.Object
implements java.awt.image.BufferedImageOp

ColorReducer implements an abstract base class for color reducers that approximate larger color sets with smaller ones. Color reduction is frequently needed for example when RGB image are converted to palette images that support only limited sized palettes. For example, the ageing GIF(tm) image format supports only 256 colors in its palette.

ColorReducer offers a common interface for different color quantization or reduction methods. It also provides a generic color reduction filter interface that uses the services implemented by spesific sub-classes, and a set of tools to help to filter out selected color component bits in order to further quantize the colors in a reduced color set.

Color masks are simple 24 bit bit patterns that seleted color reducer operations will use to filter out color bits from given or generated color values. This may be used in particular to reduce the number of color shades the color reducer has to work with in the first place. All operations that apply the color mask are expected to explicitly document this in their own method documentation. Finally, please note, that color masks apply on color alpha components as well on the RGB components. In particular, a failure to set color mask alpha bits properly will result to loss of color transparency information.

Since:
JDK1.2

Field Summary
static int COLORMASK_12BITS
          alpha + 12 bit color quantization mask (4 bits/component).
static int COLORMASK_18BITS
          alpha + 18 bit color quantization mask (6 bits/component).
static int COLORMASK_24BITS
          alpha + 24 bit color quantization mask (8 bits/component).
static int COLORMASK_6BITS
          alpha + 6 bit color quantization mask (2 bits/component).
protected  int M_colorMask
          Color quantization bitmask.
 
Constructor Summary
ColorReducer()
          Creates a new color reducer with a default 24 bit pass-through color quantization mask.
ColorReducer(int colorMask)
          Creates a new color reducer with given color quantization mask.
 
Method Summary
 java.awt.image.BufferedImage createCompatibleDestImage(java.awt.image.BufferedImage src, java.awt.image.ColorModel destCM)
          Creates a zeroed destination image with the correct size and number of bands.
 java.awt.image.BufferedImage filter(java.awt.image.BufferedImage src, java.awt.image.BufferedImage dest)
          Reduces the colors of given source image, and copies the result into given destination image.
 java.awt.geom.Rectangle2D getBounds2D(java.awt.image.BufferedImage src)
          Returns the bounding box of the filtered destination image.
abstract  int getColor(int argb)
          Gets the replacement color value from the current palette for the given color.
 int getColor(int argb, boolean quantize)
          Gets an optionally quantized replacement color value from the current palette for the given color.
protected static int getColorBits(int bits)
          Gets a 8 -bit bitmask containing given number of most significant '1' bits.
 int getColorMask()
          Gets current color quantization mask.
static int getColorMask(int bits)
          Computes a color quantization mask, with equal number of bits for each color component.
static int getColorMask(int redBits, int greenBits, int blueBits)
          Computes a color quantization mask.
abstract  int getIndex(int argb)
          Gets the replacement color index in current palette for the given color.
abstract  java.awt.image.IndexColorModel getPalette()
          Gets current reduced color palette.
 java.awt.geom.Point2D getPoint2D(java.awt.geom.Point2D srcPoint, java.awt.geom.Point2D dstPoint)
          Returns the location of the destination point given a point in the source image.
 java.awt.RenderingHints getRenderingHints()
          Returns the rendering hints for this BufferdImageOp.
 void setAlphaBits(int bits)
          Sets alpha color quantization bits.
 void setAlphaMask(int mask)
          Sets alpha component quantization mask.
 void setBlueBits(int bits)
          Sets blue color quantization bits.
 void setBlueMask(int mask)
          Sets blue component quantization mask.
 void setColorBits(int bits)
          Sets color quantization bits for all color components, but not for the alpha component.
 void setColorMask(int colorMask)
          Sets color quantization mask.
 void setGreenBits(int bits)
          Sets green color quantization bits.
 void setGreenMask(int mask)
          Sets green component quantization mask.
 void setRedBits(int bits)
          Sets red color quantization bits.
 void setRedMask(int mask)
          Sets red component quantization mask.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COLORMASK_24BITS

public static final int COLORMASK_24BITS
alpha + 24 bit color quantization mask (8 bits/component).

See Also:
Constant Field Values

COLORMASK_18BITS

public static final int COLORMASK_18BITS
alpha + 18 bit color quantization mask (6 bits/component).

See Also:
Constant Field Values

COLORMASK_12BITS

public static final int COLORMASK_12BITS
alpha + 12 bit color quantization mask (4 bits/component).

See Also:
Constant Field Values

COLORMASK_6BITS

public static final int COLORMASK_6BITS
alpha + 6 bit color quantization mask (2 bits/component).

See Also:
Constant Field Values

M_colorMask

protected int M_colorMask
Color quantization bitmask.

Constructor Detail

ColorReducer

public ColorReducer()
Creates a new color reducer with a default 24 bit pass-through color quantization mask. This mask filters no color bits out.

See Also:
setColorMask(int)

ColorReducer

public ColorReducer(int colorMask)
Creates a new color reducer with given color quantization mask.

Parameters:
colorMask - color quantization mask
See Also:
setColorMask(int)
Method Detail

getColor

public abstract int getColor(int argb)
Gets the replacement color value from the current palette 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.

Parameters:
argb - standard sRGB color value (with alpha)
Returns:
replacement sRGB color value (with alpha)
See Also:
getColor(int,boolean), getPalette()

getIndex

public abstract int getIndex(int argb)
Gets the replacement color index in current palette 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.

Parameters:
argb - standard sRGB color value (with alpha)
Returns:
replacement color index
See Also:
getColor(int), getPalette()

getPalette

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


getColorBits

protected static int getColorBits(int bits)
Gets a 8 -bit bitmask containing given number of most significant '1' bits. These bitmasks are used to filter out least significatn bit values from color component values.
bitsmask# of intensity levels
0000000001
1100000002
2110000004
3111000008
41111000016
51111100032
61111110064
711111110128
811111111256

Parameters:
bits - number of significant color bits (0-8)
Returns:
8 -bit color component mask
See Also:
setBlueMask(int), setGreenMask(int), setRedMask(int)

getColor

public int getColor(int argb,
                    boolean quantize)
Gets an optionally quantized replacement color value from the current palette for the given color.

This method seeks first a proper replacement color for given source color with getColor(int) method. If so requested, the replacement color is then filtered against current color mask before it is returned.

Parameters:
argb - standard sRGB color value (with alpha)
quantize - mask the result color against current color mask or not
Returns:
optionally quantized replacement sRGB color value (with alpha)
See Also:
getColor(int), setColorMask(int)

getColorMask

public int getColorMask()
Gets current color quantization mask. The 24 bit mask will contain 8 bits masks for each color component: alpha, red, green, and blue.

Returns:
current color quantization mask (0xAARRGGBB)

getColorMask

public static int getColorMask(int bits)
Computes a color quantization mask, with equal number of bits for each color component.

Parameters:
bits - number of significant color bits (0-8)
Returns:
a color quantization mask
See Also:
getColorMask(int,int,int)

getColorMask

public static int getColorMask(int redBits,
                               int greenBits,
                               int blueBits)
Computes a color quantization mask.

Parameters:
redBits - number of significant red color bits (0-8)
greenBits - number of significant green color bits (0-8)
blueBits - number of significant blue color bits (0-8)
Returns:
a color quantization mask
See Also:
getColorMask(int)

setAlphaBits

public void setAlphaBits(int bits)
Sets alpha color quantization bits.

Parameters:
bits - number of alpha bits (0-8)
See Also:
getColorBits(int), setAlphaMask(int)

setAlphaMask

public void setAlphaMask(int mask)
Sets alpha component quantization mask.

Parameters:
mask - alpha component quantization mask, residing in the 8 least significant bits of given value
See Also:
getColorBits(int), setAlphaBits(int)

setBlueBits

public void setBlueBits(int bits)
Sets blue color quantization bits.

Parameters:
bits - number of blue bits (0-8)
See Also:
getColorBits(int), setBlueMask(int)

setBlueMask

public void setBlueMask(int mask)
Sets blue component quantization mask.

Parameters:
mask - blue component quantization mask, residing in the 8 least significant bits of given value
See Also:
getColorBits(int), setBlueBits(int)

setColorBits

public void setColorBits(int bits)
Sets color quantization bits for all color components, but not for the alpha component.

Parameters:
bits - number of color component bits (0-8)
See Also:
getColorBits(int), setBlueMask(int), setGreenMask(int), setRedMask(int)

setColorMask

public void setColorMask(int colorMask)
Sets color quantization mask. Color quantization mask gives a bit pattern against which standard sRGB colors may be masked against before adding them to or fetching them from a color reducer. Different color reducer operations will use this mask to filter given or generated color values as documented by each respective method.

The 24 bit mask is expected to contain 8 bits masks for each color component: alpha, red, green, and blue. For more information about color masks and quantization, please see class documentation.

Parameters:
colorMask - color quantization mask (0xAARRGGBB)
See Also:
getColor(int,boolean), getColorMask(), setColorBits(int)

setGreenBits

public void setGreenBits(int bits)
Sets green color quantization bits.

Parameters:
bits - number of green bits (0-8)
See Also:
getColorBits(int), setGreenMask(int)

setGreenMask

public void setGreenMask(int mask)
Sets green component quantization mask.

Parameters:
mask - green component quantization mask, residing in the 8 least significant bits of given value
See Also:
getColorBits(int), setGreenBits(int)

setRedBits

public void setRedBits(int bits)
Sets red color quantization bits.

Parameters:
bits - number of red bits (0-8)
See Also:
getColorBits(int), setRedMask(int)

setRedMask

public void setRedMask(int mask)
Sets red component quantization mask.

Parameters:
mask - red component quantization mask, residing in the 8 least significant bits of given value
See Also:
getColorBits(int), setRedBits(int)

createCompatibleDestImage

public java.awt.image.BufferedImage createCompatibleDestImage(java.awt.image.BufferedImage src,
                                                              java.awt.image.ColorModel destCM)
Creates a zeroed destination image with the correct size and number of bands. In particular, the returned image size will be equal to given source image, and have only one band, containing indexes to current IndexColorModel palette.

Specified by:
createCompatibleDestImage in interface java.awt.image.BufferedImageOp
Parameters:
src - the source image
destCM - ColorModel of the destination (ignored)
Returns:
a BufferedImage that is compatible with src
See Also:
getPalette()

filter

public java.awt.image.BufferedImage filter(java.awt.image.BufferedImage src,
                                           java.awt.image.BufferedImage dest)
Reduces the colors of given source image, and copies the result into given destination image. If no destination image is given, a new destination image with a suitable reduced index color model is created.

Specified by:
filter in interface java.awt.image.BufferedImageOp
Parameters:
src - the image to be filtered
dest - the imaget in which to store the results (may be null)
Returns:
the filtered image

getBounds2D

public java.awt.geom.Rectangle2D getBounds2D(java.awt.image.BufferedImage src)
Returns the bounding box of the filtered destination image.

Color reduction does not alter image bounds, so therefore the bounds from given source image are returned.

Specified by:
getBounds2D in interface java.awt.image.BufferedImageOp
Parameters:
src - the source image

getPoint2D

public java.awt.geom.Point2D getPoint2D(java.awt.geom.Point2D srcPoint,
                                        java.awt.geom.Point2D dstPoint)
Returns the location of the destination point given a point in the source image. If dstPt is non-null, it will be used to hold the return value.

Color reduction does not alter the geometry of the source image, so therefore th source point is always returned.

Specified by:
getPoint2D in interface java.awt.image.BufferedImageOp
Parameters:
srcPoint - the source Point2D
dstPoint - the destination Point2D
Returns:
the location of the destination point

getRenderingHints

public java.awt.RenderingHints getRenderingHints()
Returns the rendering hints for this BufferdImageOp. Returns null if no hints have been set.

Specified by:
getRenderingHints in interface java.awt.image.BufferedImageOp
Returns:
always null


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