Class IndexImage
- java.lang.Object
-
- com.twelvemonkeys.image.IndexImage
-
class IndexImage extends java.lang.Object
This class implements an adaptive palette generator to reduce images to a variable number of colors. It can also render images into fixed color pallettes.Support for the default JVM (ordered/pattern) dither, Floyd-Steinberg like error-diffusion and no dither, controlled by the hints
DITHER_DIFFUSION
,DITHER_NONE
andDITHER_DEFAULT
.Color selection speed/accuracy can be controlled using the hints
COLOR_SELECTION_FAST
,COLOR_SELECTION_QUALITY
andCOLOR_SELECTION_DEFAULT
.Transparency support can be controlled using the hints
TRANSPARENCY_OPAQUE
,TRANSPARENCY_BITMASK
andTRANSPARENCY_TRANSLUCENT
.
This product includes software developed by the Apache Software Foundation. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation. For more information on the Apache Software Foundation, please see http://www.apache.org/
- Version:
- $Id: IndexImage.java#1 $
- See Also:
DiffusionDither
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
IndexImage.Counter
Used to track a color and the number of pixels of that colorsprivate static class
IndexImage.Cube
Used to define a cube of the color space.
-
Field Summary
Fields Modifier and Type Field Description static int
COLOR_SELECTION_DEFAULT
Default color selectionstatic int
COLOR_SELECTION_FAST
Prioritize speedprotected static int
COLOR_SELECTION_MASK
Color Selection maskstatic int
COLOR_SELECTION_QUALITY
Prioritize qualitystatic int
DITHER_DEFAULT
Java default ditherstatic int
DITHER_DIFFUSION
Error diffusion ditherstatic int
DITHER_DIFFUSION_ALTSCANS
Error diffusion dither with alternating scansprotected static int
DITHER_MASK
Dither maskstatic int
DITHER_NONE
No ditherstatic int
TRANSPARENCY_BITMASK
Convert alpha to bitmaskstatic int
TRANSPARENCY_DEFAULT
Default transparency (none)protected static int
TRANSPARENCY_MASK
Transparency maskstatic int
TRANSPARENCY_OPAQUE
Discard any alpha informationprotected static int
TRANSPARENCY_TRANSLUCENT
Keep original alpha (not supported yet)
-
Constructor Summary
Constructors Modifier Constructor Description private
IndexImage()
You cannot create this
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private static void
applyAlpha(java.awt.image.BufferedImage pImage, java.awt.image.BufferedImage pAlpha)
Applies the alpha-component of the alpha image to the given image.private static java.awt.image.IndexColorModel
createIndexColorModel(java.awt.image.BufferedImage pImage, int pNumberOfColors, int pHints)
Creates anIndexColorModel
from the given image, using an adaptive palette.private static java.awt.image.BufferedImage
createSolid(java.awt.image.BufferedImage pOriginal, java.awt.Color pBackground)
Creates a copy of the given image, with a solid backgroundstatic java.awt.image.IndexColorModel
getIndexColorModel(java.awt.Image pImage, int pNumberOfColors, boolean pFast)
Deprecated.UsegetIndexColorModel(Image,int,int)
instead! This version will be removed in a later version of the API.static java.awt.image.IndexColorModel
getIndexColorModel(java.awt.Image pImage, int pNumberOfColors, int pHints)
Gets anIndexColorModel
from the given image.static java.awt.image.BufferedImage
getIndexedImage(java.awt.image.BufferedImage pImage)
Converts the input image (must beTYPE_INT_RGB
orTYPE_INT_ARGB
) to an indexed image.static java.awt.image.BufferedImage
getIndexedImage(java.awt.image.BufferedImage pImage, int pNumberOfColors, int pHints)
Converts the input image (must beTYPE_INT_RGB
orTYPE_INT_ARGB
) to an indexed image.static java.awt.image.BufferedImage
getIndexedImage(java.awt.image.BufferedImage pImage, int pNumberOfColors, java.awt.Color pMatte, int pHints)
Converts the input image (must beTYPE_INT_RGB
orTYPE_INT_ARGB
) to an indexed image.static java.awt.image.BufferedImage
getIndexedImage(java.awt.image.BufferedImage pImage, java.awt.image.IndexColorModel pColors, int pHints)
Converts the input image (must beTYPE_INT_RGB
orTYPE_INT_ARGB
) to an indexed image.static java.awt.image.BufferedImage
getIndexedImage(java.awt.image.BufferedImage pImage, java.awt.image.IndexColorModel pColors, java.awt.Color pMatte, int pHints)
Converts the input image (must beTYPE_INT_RGB
orTYPE_INT_ARGB
) to an indexed image.static java.awt.image.BufferedImage
getIndexedImage(java.awt.image.BufferedImage pImage, java.awt.Image pPalette, int pHints)
Converts the input image (must beTYPE_INT_RGB
orTYPE_INT_ARGB
) to an indexed image.static java.awt.image.BufferedImage
getIndexedImage(java.awt.image.BufferedImage pImage, java.awt.Image pPalette, java.awt.Color pMatte, int pHints)
Converts the input image (must beTYPE_INT_RGB
orTYPE_INT_ARGB
) to an indexed image.private static boolean
isFast(int pHints)
Tests if the hintCOLOR_SELECTION_QUALITY
is not set.(package private) static boolean
isTransparent(int pHints)
Tests if the hintTRANSPARENCY_BITMASK
orTRANSPARENCY_TRANSLUCENT
is set.static void
main(java.lang.String[] pArgs)
-
-
-
Field Detail
-
DITHER_MASK
protected static final int DITHER_MASK
Dither mask- See Also:
- Constant Field Values
-
DITHER_DEFAULT
public static final int DITHER_DEFAULT
Java default dither- See Also:
- Constant Field Values
-
DITHER_NONE
public static final int DITHER_NONE
No dither- See Also:
- Constant Field Values
-
DITHER_DIFFUSION
public static final int DITHER_DIFFUSION
Error diffusion dither- See Also:
- Constant Field Values
-
DITHER_DIFFUSION_ALTSCANS
public static final int DITHER_DIFFUSION_ALTSCANS
Error diffusion dither with alternating scans- See Also:
- Constant Field Values
-
COLOR_SELECTION_MASK
protected static final int COLOR_SELECTION_MASK
Color Selection mask- See Also:
- Constant Field Values
-
COLOR_SELECTION_DEFAULT
public static final int COLOR_SELECTION_DEFAULT
Default color selection- See Also:
- Constant Field Values
-
COLOR_SELECTION_FAST
public static final int COLOR_SELECTION_FAST
Prioritize speed- See Also:
- Constant Field Values
-
COLOR_SELECTION_QUALITY
public static final int COLOR_SELECTION_QUALITY
Prioritize quality- See Also:
- Constant Field Values
-
TRANSPARENCY_MASK
protected static final int TRANSPARENCY_MASK
Transparency mask- See Also:
- Constant Field Values
-
TRANSPARENCY_DEFAULT
public static final int TRANSPARENCY_DEFAULT
Default transparency (none)- See Also:
- Constant Field Values
-
TRANSPARENCY_OPAQUE
public static final int TRANSPARENCY_OPAQUE
Discard any alpha information- See Also:
- Constant Field Values
-
TRANSPARENCY_BITMASK
public static final int TRANSPARENCY_BITMASK
Convert alpha to bitmask- See Also:
- Constant Field Values
-
TRANSPARENCY_TRANSLUCENT
protected static final int TRANSPARENCY_TRANSLUCENT
Keep original alpha (not supported yet)- See Also:
- Constant Field Values
-
-
Method Detail
-
getIndexColorModel
@Deprecated public static java.awt.image.IndexColorModel getIndexColorModel(java.awt.Image pImage, int pNumberOfColors, boolean pFast)
Deprecated.UsegetIndexColorModel(Image,int,int)
instead! This version will be removed in a later version of the API.- Parameters:
pImage
- the image to getIndexColorModel
frompNumberOfColors
- the number of colors for theIndexColorModel
pFast
-true
if fast- Returns:
- an
IndexColorModel
- See Also:
getIndexColorModel(Image,int,int)
-
getIndexColorModel
public static java.awt.image.IndexColorModel getIndexColorModel(java.awt.Image pImage, int pNumberOfColors, int pHints) throws ImageConversionException
Gets anIndexColorModel
from the given image. If the image has anIndexColorModel
, this will be returned. Otherwise, anIndexColorModel
is created, using an adaptive palette.- Parameters:
pImage
- the image to getIndexColorModel
frompNumberOfColors
- the number of colors for theIndexColorModel
pHints
- one ofCOLOR_SELECTION_FAST
,COLOR_SELECTION_QUALITY
orCOLOR_SELECTION_DEFAULT
.- Returns:
- The
IndexColorModel
from the given image, or a newly createdIndexColorModel
using an adaptive palette. - Throws:
ImageConversionException
- if an exception occurred during color model extraction.
-
createIndexColorModel
private static java.awt.image.IndexColorModel createIndexColorModel(java.awt.image.BufferedImage pImage, int pNumberOfColors, int pHints)
Creates anIndexColorModel
from the given image, using an adaptive palette.- Parameters:
pImage
- the image to getIndexColorModel
frompNumberOfColors
- the number of colors for theIndexColorModel
pHints
- use fast mode if possible (might give slightly lower quality)- Returns:
- a new
IndexColorModel
created from the given image
-
getIndexedImage
public static java.awt.image.BufferedImage getIndexedImage(java.awt.image.BufferedImage pImage)
Converts the input image (must beTYPE_INT_RGB
orTYPE_INT_ARGB
) to an indexed image. Generating an adaptive palette (8 bit) from the color data in the image, and uses default dither.The image returned is a new image, the input image is not modified.
- Parameters:
pImage
- the BufferedImage to index and get color information from.- Returns:
- the indexed BufferedImage. The image will be of type
BufferedImage.TYPE_BYTE_INDEXED
, and use anIndexColorModel
. - See Also:
BufferedImage.TYPE_BYTE_INDEXED
,IndexColorModel
-
isFast
private static boolean isFast(int pHints)
Tests if the hintCOLOR_SELECTION_QUALITY
is not set.- Parameters:
pHints
- hints- Returns:
- true if the hint
COLOR_SELECTION_QUALITY
is not set.
-
isTransparent
static boolean isTransparent(int pHints)
Tests if the hintTRANSPARENCY_BITMASK
orTRANSPARENCY_TRANSLUCENT
is set.- Parameters:
pHints
- hints- Returns:
- true if the hint
TRANSPARENCY_BITMASK
orTRANSPARENCY_TRANSLUCENT
is set.
-
getIndexedImage
public static java.awt.image.BufferedImage getIndexedImage(java.awt.image.BufferedImage pImage, java.awt.Image pPalette, java.awt.Color pMatte, int pHints) throws ImageConversionException
Converts the input image (must beTYPE_INT_RGB
orTYPE_INT_ARGB
) to an indexed image. If the palette image uses anIndexColorModel
, this will be used. Otherwise, generating an adaptive palette (8 bit) from the given palette image. Dithering, transparency and color selection is controlled with thepHints
parameter.The image returned is a new image, the input image is not modified.
- Parameters:
pImage
- the BufferedImage to indexpPalette
- the Image to read color information frompMatte
- the background color, used where the original image was transparentpHints
- hints that control output quality and speed.- Returns:
- the indexed BufferedImage. The image will be of type
BufferedImage.TYPE_BYTE_INDEXED
orBufferedImage.TYPE_BYTE_BINARY
, and use anIndexColorModel
. - Throws:
ImageConversionException
- if an exception occurred during color model extraction.- See Also:
DITHER_DIFFUSION
,DITHER_NONE
,COLOR_SELECTION_FAST
,COLOR_SELECTION_QUALITY
,TRANSPARENCY_OPAQUE
,TRANSPARENCY_BITMASK
,BufferedImage.TYPE_BYTE_INDEXED
,BufferedImage.TYPE_BYTE_BINARY
,IndexColorModel
-
getIndexedImage
public static java.awt.image.BufferedImage getIndexedImage(java.awt.image.BufferedImage pImage, int pNumberOfColors, java.awt.Color pMatte, int pHints)
Converts the input image (must beTYPE_INT_RGB
orTYPE_INT_ARGB
) to an indexed image. Generating an adaptive palette with the given number of colors. Dithering, transparency and color selection is controlled with thepHints
parameter.The image returned is a new image, the input image is not modified.
- Parameters:
pImage
- the BufferedImage to indexpNumberOfColors
- the number of colors for the imagepMatte
- the background color, used where the original image was transparentpHints
- hints that control output quality and speed.- Returns:
- the indexed BufferedImage. The image will be of type
BufferedImage.TYPE_BYTE_INDEXED
orBufferedImage.TYPE_BYTE_BINARY
, and use anIndexColorModel
. - See Also:
DITHER_DIFFUSION
,DITHER_NONE
,COLOR_SELECTION_FAST
,COLOR_SELECTION_QUALITY
,TRANSPARENCY_OPAQUE
,TRANSPARENCY_BITMASK
,BufferedImage.TYPE_BYTE_INDEXED
,BufferedImage.TYPE_BYTE_BINARY
,IndexColorModel
-
getIndexedImage
public static java.awt.image.BufferedImage getIndexedImage(java.awt.image.BufferedImage pImage, java.awt.image.IndexColorModel pColors, java.awt.Color pMatte, int pHints)
Converts the input image (must beTYPE_INT_RGB
orTYPE_INT_ARGB
) to an indexed image. Using the suppliedIndexColorModel
's palette. Dithering, transparency and color selection is controlled with thepHints
parameter.The image returned is a new image, the input image is not modified.
- Parameters:
pImage
- the BufferedImage to indexpColors
- anIndexColorModel
containing the color informationpMatte
- the background color, used where the original image was transparent. Also note that any transparent antialias will be rendered against this color.pHints
- RenderingHints that control output quality and speed.- Returns:
- the indexed BufferedImage. The image will be of type
BufferedImage.TYPE_BYTE_INDEXED
orBufferedImage.TYPE_BYTE_BINARY
, and use anIndexColorModel
. - See Also:
DITHER_DIFFUSION
,DITHER_NONE
,COLOR_SELECTION_FAST
,COLOR_SELECTION_QUALITY
,TRANSPARENCY_OPAQUE
,TRANSPARENCY_BITMASK
,BufferedImage.TYPE_BYTE_INDEXED
,BufferedImage.TYPE_BYTE_BINARY
,IndexColorModel
-
getIndexedImage
public static java.awt.image.BufferedImage getIndexedImage(java.awt.image.BufferedImage pImage, int pNumberOfColors, int pHints)
Converts the input image (must beTYPE_INT_RGB
orTYPE_INT_ARGB
) to an indexed image. Generating an adaptive palette with the given number of colors. Dithering, transparency and color selection is controlled with thepHints
parameter.The image returned is a new image, the input image is not modified.
- Parameters:
pImage
- the BufferedImage to indexpNumberOfColors
- the number of colors for the imagepHints
- hints that control output quality and speed.- Returns:
- the indexed BufferedImage. The image will be of type
BufferedImage.TYPE_BYTE_INDEXED
orBufferedImage.TYPE_BYTE_BINARY
, and use anIndexColorModel
. - See Also:
DITHER_DIFFUSION
,DITHER_NONE
,COLOR_SELECTION_FAST
,COLOR_SELECTION_QUALITY
,TRANSPARENCY_OPAQUE
,TRANSPARENCY_BITMASK
,BufferedImage.TYPE_BYTE_INDEXED
,BufferedImage.TYPE_BYTE_BINARY
,IndexColorModel
-
getIndexedImage
public static java.awt.image.BufferedImage getIndexedImage(java.awt.image.BufferedImage pImage, java.awt.image.IndexColorModel pColors, int pHints)
Converts the input image (must beTYPE_INT_RGB
orTYPE_INT_ARGB
) to an indexed image. Using the suppliedIndexColorModel
's palette. Dithering, transparency and color selection is controlled with thepHints
parameter.The image returned is a new image, the input image is not modified.
- Parameters:
pImage
- the BufferedImage to indexpColors
- anIndexColorModel
containing the color informationpHints
- RenderingHints that control output quality and speed.- Returns:
- the indexed BufferedImage. The image will be of type
BufferedImage.TYPE_BYTE_INDEXED
orBufferedImage.TYPE_BYTE_BINARY
, and use anIndexColorModel
. - See Also:
DITHER_DIFFUSION
,DITHER_NONE
,COLOR_SELECTION_FAST
,COLOR_SELECTION_QUALITY
,TRANSPARENCY_OPAQUE
,TRANSPARENCY_BITMASK
,BufferedImage.TYPE_BYTE_INDEXED
,BufferedImage.TYPE_BYTE_BINARY
,IndexColorModel
-
getIndexedImage
public static java.awt.image.BufferedImage getIndexedImage(java.awt.image.BufferedImage pImage, java.awt.Image pPalette, int pHints)
Converts the input image (must beTYPE_INT_RGB
orTYPE_INT_ARGB
) to an indexed image. If the palette image uses anIndexColorModel
, this will be used. Otherwise, generating an adaptive palette (8 bit) from the given palette image. Dithering, transparency and color selection is controlled with thepHints
parameter.The image returned is a new image, the input image is not modified.
- Parameters:
pImage
- the BufferedImage to indexpPalette
- the Image to read color information frompHints
- hints that control output quality and speed.- Returns:
- the indexed BufferedImage. The image will be of type
BufferedImage.TYPE_BYTE_INDEXED
orBufferedImage.TYPE_BYTE_BINARY
, and use anIndexColorModel
. - See Also:
DITHER_DIFFUSION
,DITHER_NONE
,COLOR_SELECTION_FAST
,COLOR_SELECTION_QUALITY
,TRANSPARENCY_OPAQUE
,TRANSPARENCY_BITMASK
,BufferedImage.TYPE_BYTE_INDEXED
,BufferedImage.TYPE_BYTE_BINARY
,IndexColorModel
-
createSolid
private static java.awt.image.BufferedImage createSolid(java.awt.image.BufferedImage pOriginal, java.awt.Color pBackground)
Creates a copy of the given image, with a solid background- Parameters:
pOriginal
- the original imagepBackground
- the background color- Returns:
- a new
BufferedImage
-
applyAlpha
private static void applyAlpha(java.awt.image.BufferedImage pImage, java.awt.image.BufferedImage pAlpha)
Applies the alpha-component of the alpha image to the given image. The given image is modified in place.- Parameters:
pImage
- the image to apply alpha topAlpha
- the image containing the alpha
-
main
public static void main(java.lang.String[] pArgs)
-
-