Class IndexImage

java.lang.Object
com.twelvemonkeys.image.IndexImage

class IndexImage extends 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 and DITHER_DEFAULT.

Color selection speed/accuracy can be controlled using the hints COLOR_SELECTION_FAST, COLOR_SELECTION_QUALITY and COLOR_SELECTION_DEFAULT.

Transparency support can be controlled using the hints TRANSPARENCY_OPAQUE, TRANSPARENCY_BITMASK and TRANSPARENCY_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:
  • Field Details

    • DITHER_MASK

      protected static final int DITHER_MASK
      Dither mask
      See Also:
    • DITHER_DEFAULT

      public static final int DITHER_DEFAULT
      Java default dither
      See Also:
    • DITHER_NONE

      public static final int DITHER_NONE
      No dither
      See Also:
    • DITHER_DIFFUSION

      public static final int DITHER_DIFFUSION
      Error diffusion dither
      See Also:
    • DITHER_DIFFUSION_ALTSCANS

      public static final int DITHER_DIFFUSION_ALTSCANS
      Error diffusion dither with alternating scans
      See Also:
    • COLOR_SELECTION_MASK

      protected static final int COLOR_SELECTION_MASK
      Color Selection mask
      See Also:
    • COLOR_SELECTION_DEFAULT

      public static final int COLOR_SELECTION_DEFAULT
      Default color selection
      See Also:
    • COLOR_SELECTION_FAST

      public static final int COLOR_SELECTION_FAST
      Prioritize speed
      See Also:
    • COLOR_SELECTION_QUALITY

      public static final int COLOR_SELECTION_QUALITY
      Prioritize quality
      See Also:
    • TRANSPARENCY_MASK

      protected static final int TRANSPARENCY_MASK
      Transparency mask
      See Also:
    • TRANSPARENCY_DEFAULT

      public static final int TRANSPARENCY_DEFAULT
      Default transparency (none)
      See Also:
    • TRANSPARENCY_OPAQUE

      public static final int TRANSPARENCY_OPAQUE
      Discard any alpha information
      See Also:
    • TRANSPARENCY_BITMASK

      public static final int TRANSPARENCY_BITMASK
      Convert alpha to bitmask
      See Also:
    • TRANSPARENCY_TRANSLUCENT

      protected static final int TRANSPARENCY_TRANSLUCENT
      Keep original alpha (not supported yet)
      See Also:
  • Constructor Details

    • IndexImage

      private IndexImage()
      You cannot create this
  • Method Details

    • getIndexColorModel

      @Deprecated public static IndexColorModel getIndexColorModel(Image pImage, int pNumberOfColors, boolean pFast)
      Deprecated.
      Use getIndexColorModel(Image,int,int) instead! This version will be removed in a later version of the API.
      Parameters:
      pImage - the image to get IndexColorModel from
      pNumberOfColors - the number of colors for the IndexColorModel
      pFast - true if fast
      Returns:
      an IndexColorModel
      See Also:
    • getIndexColorModel

      public static IndexColorModel getIndexColorModel(Image pImage, int pNumberOfColors, int pHints) throws ImageConversionException
      Gets an IndexColorModel from the given image. If the image has an IndexColorModel, this will be returned. Otherwise, an IndexColorModel is created, using an adaptive palette.
      Parameters:
      pImage - the image to get IndexColorModel from
      pNumberOfColors - the number of colors for the IndexColorModel
      pHints - one of COLOR_SELECTION_FAST, COLOR_SELECTION_QUALITY or COLOR_SELECTION_DEFAULT.
      Returns:
      The IndexColorModel from the given image, or a newly created IndexColorModel using an adaptive palette.
      Throws:
      ImageConversionException - if an exception occurred during color model extraction.
    • createIndexColorModel

      private static IndexColorModel createIndexColorModel(BufferedImage pImage, int pNumberOfColors, int pHints)
      Creates an IndexColorModel from the given image, using an adaptive palette.
      Parameters:
      pImage - the image to get IndexColorModel from
      pNumberOfColors - the number of colors for the IndexColorModel
      pHints - use fast mode if possible (might give slightly lower quality)
      Returns:
      a new IndexColorModel created from the given image
    • getIndexedImage

      public static BufferedImage getIndexedImage(BufferedImage pImage)
      Converts the input image (must be TYPE_INT_RGB or TYPE_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 an IndexColorModel.
      See Also:
    • isFast

      private static boolean isFast(int pHints)
      Tests if the hint COLOR_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 hint TRANSPARENCY_BITMASK or TRANSPARENCY_TRANSLUCENT is set.
      Parameters:
      pHints - hints
      Returns:
      true if the hint TRANSPARENCY_BITMASK or TRANSPARENCY_TRANSLUCENT is set.
    • getIndexedImage

      public static BufferedImage getIndexedImage(BufferedImage pImage, Image pPalette, Color pMatte, int pHints) throws ImageConversionException
      Converts the input image (must be TYPE_INT_RGB or TYPE_INT_ARGB) to an indexed image. If the palette image uses an IndexColorModel, this will be used. Otherwise, generating an adaptive palette (8 bit) from the given palette image. Dithering, transparency and color selection is controlled with the pHintsparameter.

      The image returned is a new image, the input image is not modified.

      Parameters:
      pImage - the BufferedImage to index
      pPalette - the Image to read color information from
      pMatte - the background color, used where the original image was transparent
      pHints - hints that control output quality and speed.
      Returns:
      the indexed BufferedImage. The image will be of type BufferedImage.TYPE_BYTE_INDEXED or BufferedImage.TYPE_BYTE_BINARY, and use an IndexColorModel.
      Throws:
      ImageConversionException - if an exception occurred during color model extraction.
      See Also:
    • getIndexedImage

      public static BufferedImage getIndexedImage(BufferedImage pImage, int pNumberOfColors, Color pMatte, int pHints)
      Converts the input image (must be TYPE_INT_RGB or TYPE_INT_ARGB) to an indexed image. Generating an adaptive palette with the given number of colors. Dithering, transparency and color selection is controlled with the pHints parameter.

      The image returned is a new image, the input image is not modified.

      Parameters:
      pImage - the BufferedImage to index
      pNumberOfColors - the number of colors for the image
      pMatte - the background color, used where the original image was transparent
      pHints - hints that control output quality and speed.
      Returns:
      the indexed BufferedImage. The image will be of type BufferedImage.TYPE_BYTE_INDEXED or BufferedImage.TYPE_BYTE_BINARY, and use an IndexColorModel.
      See Also:
    • getIndexedImage

      public static BufferedImage getIndexedImage(BufferedImage pImage, IndexColorModel pColors, Color pMatte, int pHints)
      Converts the input image (must be TYPE_INT_RGB or TYPE_INT_ARGB) to an indexed image. Using the supplied IndexColorModel's palette. Dithering, transparency and color selection is controlled with the pHints parameter.

      The image returned is a new image, the input image is not modified.

      Parameters:
      pImage - the BufferedImage to index
      pColors - an IndexColorModel containing the color information
      pMatte - 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 or BufferedImage.TYPE_BYTE_BINARY, and use an IndexColorModel.
      See Also:
    • getIndexedImage

      public static BufferedImage getIndexedImage(BufferedImage pImage, int pNumberOfColors, int pHints)
      Converts the input image (must be TYPE_INT_RGB or TYPE_INT_ARGB) to an indexed image. Generating an adaptive palette with the given number of colors. Dithering, transparency and color selection is controlled with the pHintsparameter.

      The image returned is a new image, the input image is not modified.

      Parameters:
      pImage - the BufferedImage to index
      pNumberOfColors - the number of colors for the image
      pHints - hints that control output quality and speed.
      Returns:
      the indexed BufferedImage. The image will be of type BufferedImage.TYPE_BYTE_INDEXED or BufferedImage.TYPE_BYTE_BINARY, and use an IndexColorModel.
      See Also:
    • getIndexedImage

      public static BufferedImage getIndexedImage(BufferedImage pImage, IndexColorModel pColors, int pHints)
      Converts the input image (must be TYPE_INT_RGB or TYPE_INT_ARGB) to an indexed image. Using the supplied IndexColorModel's palette. Dithering, transparency and color selection is controlled with the pHintsparameter.

      The image returned is a new image, the input image is not modified.

      Parameters:
      pImage - the BufferedImage to index
      pColors - an IndexColorModel containing the color information
      pHints - RenderingHints that control output quality and speed.
      Returns:
      the indexed BufferedImage. The image will be of type BufferedImage.TYPE_BYTE_INDEXED or BufferedImage.TYPE_BYTE_BINARY, and use an IndexColorModel.
      See Also:
    • getIndexedImage

      public static BufferedImage getIndexedImage(BufferedImage pImage, Image pPalette, int pHints)
      Converts the input image (must be TYPE_INT_RGB or TYPE_INT_ARGB) to an indexed image. If the palette image uses an IndexColorModel, this will be used. Otherwise, generating an adaptive palette (8 bit) from the given palette image. Dithering, transparency and color selection is controlled with the pHintsparameter.

      The image returned is a new image, the input image is not modified.

      Parameters:
      pImage - the BufferedImage to index
      pPalette - the Image to read color information from
      pHints - hints that control output quality and speed.
      Returns:
      the indexed BufferedImage. The image will be of type BufferedImage.TYPE_BYTE_INDEXED or BufferedImage.TYPE_BYTE_BINARY, and use an IndexColorModel.
      See Also:
    • createSolid

      private static BufferedImage createSolid(BufferedImage pOriginal, Color pBackground)
      Creates a copy of the given image, with a solid background
      Parameters:
      pOriginal - the original image
      pBackground - the background color
      Returns:
      a new BufferedImage
    • applyAlpha

      private static void applyAlpha(BufferedImage pImage, 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 to
      pAlpha - the image containing the alpha
    • main

      public static void main(String[] pArgs)