Class IndexImage
- java.lang.Object
-
- com.twelvemonkeys.image.IndexImage
-
class IndexImage extends java.lang.ObjectThis 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_NONEandDITHER_DEFAULT.Color selection speed/accuracy can be controlled using the hints
COLOR_SELECTION_FAST,COLOR_SELECTION_QUALITYandCOLOR_SELECTION_DEFAULT.Transparency support can be controlled using the hints
TRANSPARENCY_OPAQUE,TRANSPARENCY_BITMASKandTRANSPARENCY_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 classIndexImage.CounterUsed to track a color and the number of pixels of that colorsprivate static classIndexImage.CubeUsed to define a cube of the color space.
-
Field Summary
Fields Modifier and Type Field Description static intCOLOR_SELECTION_DEFAULTDefault color selectionstatic intCOLOR_SELECTION_FASTPrioritize speedprotected static intCOLOR_SELECTION_MASKColor Selection maskstatic intCOLOR_SELECTION_QUALITYPrioritize qualitystatic intDITHER_DEFAULTJava default ditherstatic intDITHER_DIFFUSIONError diffusion ditherstatic intDITHER_DIFFUSION_ALTSCANSError diffusion dither with alternating scansprotected static intDITHER_MASKDither maskstatic intDITHER_NONENo ditherstatic intTRANSPARENCY_BITMASKConvert alpha to bitmaskstatic intTRANSPARENCY_DEFAULTDefault transparency (none)protected static intTRANSPARENCY_MASKTransparency maskstatic intTRANSPARENCY_OPAQUEDiscard any alpha informationprotected static intTRANSPARENCY_TRANSLUCENTKeep original alpha (not supported yet)
-
Constructor Summary
Constructors Modifier Constructor Description privateIndexImage()You cannot create this
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private static voidapplyAlpha(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.IndexColorModelcreateIndexColorModel(java.awt.image.BufferedImage pImage, int pNumberOfColors, int pHints)Creates anIndexColorModelfrom the given image, using an adaptive palette.private static java.awt.image.BufferedImagecreateSolid(java.awt.image.BufferedImage pOriginal, java.awt.Color pBackground)Creates a copy of the given image, with a solid backgroundstatic java.awt.image.IndexColorModelgetIndexColorModel(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.IndexColorModelgetIndexColorModel(java.awt.Image pImage, int pNumberOfColors, int pHints)Gets anIndexColorModelfrom the given image.static java.awt.image.BufferedImagegetIndexedImage(java.awt.image.BufferedImage pImage)Converts the input image (must beTYPE_INT_RGBorTYPE_INT_ARGB) to an indexed image.static java.awt.image.BufferedImagegetIndexedImage(java.awt.image.BufferedImage pImage, int pNumberOfColors, int pHints)Converts the input image (must beTYPE_INT_RGBorTYPE_INT_ARGB) to an indexed image.static java.awt.image.BufferedImagegetIndexedImage(java.awt.image.BufferedImage pImage, int pNumberOfColors, java.awt.Color pMatte, int pHints)Converts the input image (must beTYPE_INT_RGBorTYPE_INT_ARGB) to an indexed image.static java.awt.image.BufferedImagegetIndexedImage(java.awt.image.BufferedImage pImage, java.awt.image.IndexColorModel pColors, int pHints)Converts the input image (must beTYPE_INT_RGBorTYPE_INT_ARGB) to an indexed image.static java.awt.image.BufferedImagegetIndexedImage(java.awt.image.BufferedImage pImage, java.awt.image.IndexColorModel pColors, java.awt.Color pMatte, int pHints)Converts the input image (must beTYPE_INT_RGBorTYPE_INT_ARGB) to an indexed image.static java.awt.image.BufferedImagegetIndexedImage(java.awt.image.BufferedImage pImage, java.awt.Image pPalette, int pHints)Converts the input image (must beTYPE_INT_RGBorTYPE_INT_ARGB) to an indexed image.static java.awt.image.BufferedImagegetIndexedImage(java.awt.image.BufferedImage pImage, java.awt.Image pPalette, java.awt.Color pMatte, int pHints)Converts the input image (must beTYPE_INT_RGBorTYPE_INT_ARGB) to an indexed image.private static booleanisFast(int pHints)Tests if the hintCOLOR_SELECTION_QUALITYis not set.(package private) static booleanisTransparent(int pHints)Tests if the hintTRANSPARENCY_BITMASKorTRANSPARENCY_TRANSLUCENTis set.static voidmain(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 getIndexColorModelfrompNumberOfColors- the number of colors for theIndexColorModelpFast-trueif 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 ImageConversionExceptionGets anIndexColorModelfrom the given image. If the image has anIndexColorModel, this will be returned. Otherwise, anIndexColorModelis created, using an adaptive palette.- Parameters:
pImage- the image to getIndexColorModelfrompNumberOfColors- the number of colors for theIndexColorModelpHints- one ofCOLOR_SELECTION_FAST,COLOR_SELECTION_QUALITYorCOLOR_SELECTION_DEFAULT.- Returns:
- The
IndexColorModelfrom the given image, or a newly createdIndexColorModelusing 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 anIndexColorModelfrom the given image, using an adaptive palette.- Parameters:
pImage- the image to getIndexColorModelfrompNumberOfColors- the number of colors for theIndexColorModelpHints- use fast mode if possible (might give slightly lower quality)- Returns:
- a new
IndexColorModelcreated from the given image
-
getIndexedImage
public static java.awt.image.BufferedImage getIndexedImage(java.awt.image.BufferedImage pImage)
Converts the input image (must beTYPE_INT_RGBorTYPE_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_QUALITYis not set.- Parameters:
pHints- hints- Returns:
- true if the hint
COLOR_SELECTION_QUALITYis not set.
-
isTransparent
static boolean isTransparent(int pHints)
Tests if the hintTRANSPARENCY_BITMASKorTRANSPARENCY_TRANSLUCENTis set.- Parameters:
pHints- hints- Returns:
- true if the hint
TRANSPARENCY_BITMASKorTRANSPARENCY_TRANSLUCENTis 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 ImageConversionExceptionConverts the input image (must beTYPE_INT_RGBorTYPE_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 thepHintsparameter.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_INDEXEDorBufferedImage.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_RGBorTYPE_INT_ARGB) to an indexed image. Generating an adaptive palette with the given number of colors. Dithering, transparency and color selection is controlled with thepHintsparameter.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_INDEXEDorBufferedImage.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_RGBorTYPE_INT_ARGB) to an indexed image. Using the suppliedIndexColorModel's palette. Dithering, transparency and color selection is controlled with thepHintsparameter.The image returned is a new image, the input image is not modified.
- Parameters:
pImage- the BufferedImage to indexpColors- anIndexColorModelcontaining 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_INDEXEDorBufferedImage.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_RGBorTYPE_INT_ARGB) to an indexed image. Generating an adaptive palette with the given number of colors. Dithering, transparency and color selection is controlled with thepHintsparameter.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_INDEXEDorBufferedImage.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_RGBorTYPE_INT_ARGB) to an indexed image. Using the suppliedIndexColorModel's palette. Dithering, transparency and color selection is controlled with thepHintsparameter.The image returned is a new image, the input image is not modified.
- Parameters:
pImage- the BufferedImage to indexpColors- anIndexColorModelcontaining the color informationpHints- RenderingHints that control output quality and speed.- Returns:
- the indexed BufferedImage. The image will be of type
BufferedImage.TYPE_BYTE_INDEXEDorBufferedImage.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_RGBorTYPE_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 thepHintsparameter.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_INDEXEDorBufferedImage.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)
-
-