Package com.day.image

Class DitherOp

  • All Implemented Interfaces:
    java.awt.image.BufferedImageOp

    public class DitherOp
    extends AbstractBufferedImageOp
    The DitherOp provides the dithering capability for reducing colors of an image to any number of colors. The real number of colors at the end of color reduction depends on the color profile of the image under reduction.

    Currently the following two dithering algorithm's are supported :

    1. Simple reduction by reducing the bit width of the color components
    2. Reduction by reducing the bit width and doing some rudimentary error correction on neighbouring pixels.
    Since:
    coati
    See Also:
    Implementation of Image Dithering/Color Reduction
    • Constructor Summary

      Constructors 
      Constructor Description
      DitherOp​(int nCol, java.awt.Color transparency, java.awt.Color bgcolor, DitherOp.DitherAlgorithm algorithm, java.awt.RenderingHints hints)
      Creates a new DitherOp instance to reduce colors of an image to the given number of colors.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static java.awt.image.BufferedImage convertToIndexed​(java.awt.image.BufferedImage src, int nCol, java.awt.Color transparency, java.awt.Color bgcolor, java.awt.RenderingHints hints)
      Converts the source image to an image with IndexColorModel and with a maximal number of colors.
      java.awt.image.BufferedImage filter​(java.awt.image.BufferedImage src, java.awt.image.BufferedImage dst)
      Performs the operation on a BufferedImage.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DITHER_NONE

        public static final DitherOp.DitherAlgorithm DITHER_NONE
        Indicate the use of the simple bit width color reduction algorithm with no further dithering effects.
      • DITHER_SIMPLE_ERROR_CORRECTION

        public static final DitherOp.DitherAlgorithm DITHER_SIMPLE_ERROR_CORRECTION
        Indicate the use of the simple bit width color reduction algorithm plus using the simple error correction algorithm.
    • Constructor Detail

      • DitherOp

        public DitherOp​(int nCol,
                        java.awt.Color transparency,
                        java.awt.Color bgcolor,
                        DitherOp.DitherAlgorithm algorithm,
                        java.awt.RenderingHints hints)
        Creates a new DitherOp instance to reduce colors of an image to the given number of colors.
        Parameters:
        nCol - The maximum number of colors to reduce the image to. This must be higher than 2.
        transparency - The Color of the color to be considered as transparent. This will be one of the nCol colors if not null.
        bgcolor - The Color of the color to be considered as the background. This will be one of the nCol colors if not null.
        algorithm - The dithering algorithm to be used.
        hints - The RenderingHints for the filter operation. This parameter may be null and is not currently used.
        Throws:
        java.lang.NullPointerException - if algorithm is null.
        java.lang.IllegalArgumentException - if nCol is less than 2.
    • Method Detail

      • convertToIndexed

        public static java.awt.image.BufferedImage convertToIndexed​(java.awt.image.BufferedImage src,
                                                                    int nCol,
                                                                    java.awt.Color transparency,
                                                                    java.awt.Color bgcolor,
                                                                    java.awt.RenderingHints hints)
        Converts the source image to an image with IndexColorModel and with a maximal number of colors. If the source image already has an IndexColorModel and the map size of the that color model instance (src.getColorModel().getMapSize()) is less than or equal to the number of colors desired, the source image is returned. Else a new image is returned.
        Parameters:
        src - The source image to convert to the IndexColorModel.
        nCol - The maximum number of colors to reduce the image to. This must be higher than 2.
        transparency - The Color of the color to be considered as transparent. This will be one of the nCol colors if not null.
        bgcolor - The Color of the color to be considered as the background. This will be one of the nCol colors if not null.
        hints - The RenderingHints for the filter operation. This parameter may be null and is not currently used.
        Returns:
        An image with the IndexColorModel color model. If the source image already has the IndexColorModel and the number of colors are less than or equal to nCol the source image is returned else a new image according to the parameters is returned.
        Throws:
        java.lang.NullPointerException - if the source image is null.
        java.lang.NullPointerException - if algorithm is null.
        java.lang.IllegalArgumentException - if nCol is less than 2.
      • filter

        public java.awt.image.BufferedImage filter​(java.awt.image.BufferedImage src,
                                                   java.awt.image.BufferedImage dst)
        Performs the operation on a BufferedImage. This implementation only cares to make the images compatible and calls the doFilter(BufferedImage, BufferedImage) to do the actual filtering operation.

        If the color models for the two images do not match, a color conversion into the destination color model will be performed. If the destination image is null, a BufferedImage with an appropriate ColorModel will be created.

        Note: The dest image might be clipped if it is not big enough to take the complete resized image.

        This method is overwritten to make sure the pixel data is premultiplied with the alpha value to take the real alpha value into account.

        Specified by:
        filter in interface java.awt.image.BufferedImageOp
        Overrides:
        filter in class AbstractBufferedImageOp
        Parameters:
        src - The src image to be resized.
        dst - The dest image into which to place the resized image. This may be null in which case a new image with the correct size will be created.
        Returns:
        The newly created image (if dest was null) or dest into which the resized src image has been drawn.
        Throws:
        java.lang.IllegalArgumentException - if the dest image is the same as the src image.
        java.lang.NullPointerException - if the src image is null.