Package com.twelvemonkeys.image
Class DiffusionDither
- java.lang.Object
-
- com.twelvemonkeys.image.DiffusionDither
-
- All Implemented Interfaces:
java.awt.image.BufferedImageOp,java.awt.image.RasterOp
public class DiffusionDither extends java.lang.Object implements java.awt.image.BufferedImageOp, java.awt.image.RasterOpThisBufferedImageOp/RasterOpimplements basic Floyd-Steinberg error-diffusion algorithm for dithering.The weights used are 7/16, 3/16, 5/16 and 1/16, distributed like this:
Floyd-Steinberg error-diffusion weights x 7/16 3/16 5/16 1/16 See Computer Graphics (Foley et al.) for more information.
- Version:
- $Id: DiffusionDither.java#1 $
-
-
Field Summary
Fields Modifier and Type Field Description private booleanalternateScansprivate static intFS_SCALEprotected java.awt.image.IndexColorModelindexColorModelprivate static java.util.RandomRANDOM
-
Constructor Summary
Constructors Constructor Description DiffusionDither()Creates aDiffusionDither, with no fixedIndexColorModel.DiffusionDither(java.awt.image.IndexColorModel pICM)Creates aDiffusionDither, using the givenIndexColorModelfor dithering into.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.awt.image.BufferedImagecreateCompatibleDestImage(java.awt.image.BufferedImage pSource, java.awt.image.ColorModel pDestCM)Creates a compatibleBufferedImageto dither into.java.awt.image.WritableRastercreateCompatibleDestRaster(java.awt.image.Raster pSrc)Creates a compatibleRasterto dither into.java.awt.image.WritableRastercreateCompatibleDestRaster(java.awt.image.Raster pSrc, java.awt.image.IndexColorModel pIndexColorModel)Creates a compatibleRasterto dither into.private java.awt.image.IndexColorModelcreateIndexColorModel(java.awt.image.Raster pSource)java.awt.image.BufferedImagefilter(java.awt.image.BufferedImage pSource, java.awt.image.BufferedImage pDest)Performs a single-input/single-output dither operation, applying basic Floyd-Steinberg error-diffusion to the image.java.awt.image.WritableRasterfilter(java.awt.image.Raster pSource, java.awt.image.WritableRaster pDest)Performs a single-input/single-output dither operation, applying basic Floyd-Steinberg error-diffusion to the image.java.awt.image.WritableRasterfilter(java.awt.image.Raster pSource, java.awt.image.WritableRaster pDest, java.awt.image.IndexColorModel pColorModel)Performs a single-input/single-output dither operation, applying basic Floyd-Steinberg error-diffusion to the image.java.awt.geom.Rectangle2DgetBounds2D(java.awt.image.BufferedImage pSrc)Returns the bounding box of the filtered destination image.java.awt.geom.Rectangle2DgetBounds2D(java.awt.image.Raster pSrc)Returns the bounding box of the filtered destination Raster.private java.awt.image.IndexColorModelgetICM(java.awt.image.BufferedImage pSource)private java.awt.image.IndexColorModelgetICM(java.awt.image.Raster pSource)java.awt.geom.Point2DgetPoint2D(java.awt.geom.Point2D pSrcPt, java.awt.geom.Point2D pDstPt)Returns the location of the destination point given a point in the source.java.awt.RenderingHintsgetRenderingHints()Returns the rendering mHints for this op.voidsetAlternateScans(boolean pUse)Sets the scan mode.private static inttoIntARGB(int[] pRGB)Converts a int triplet to int ARGB.private static int[]toRGBArray(int pARGB, int[] pBuffer)Converts an int ARGB to int triplet.
-
-
-
Field Detail
-
FS_SCALE
private static final int FS_SCALE
- See Also:
- Constant Field Values
-
RANDOM
private static final java.util.Random RANDOM
-
indexColorModel
protected final java.awt.image.IndexColorModel indexColorModel
-
alternateScans
private boolean alternateScans
-
-
Constructor Detail
-
DiffusionDither
public DiffusionDither(java.awt.image.IndexColorModel pICM)
Creates aDiffusionDither, using the givenIndexColorModelfor dithering into.- Parameters:
pICM- an IndexColorModel.
-
DiffusionDither
public DiffusionDither()
Creates aDiffusionDither, with no fixedIndexColorModel. The color model will be generated for each filtering, unless the destination image already has anIndexColorModel.
-
-
Method Detail
-
setAlternateScans
public void setAlternateScans(boolean pUse)
Sets the scan mode. If the parameter is true, error distribution for every even line will be left-to-right, while odd lines will be right-to-left. The default istrue.- Parameters:
pUse-trueif scan mode should be alternating left/right
-
createCompatibleDestImage
public final java.awt.image.BufferedImage createCompatibleDestImage(java.awt.image.BufferedImage pSource, java.awt.image.ColorModel pDestCM)Creates a compatibleBufferedImageto dither into. OnlyIndexColorModelallowed.- Specified by:
createCompatibleDestImagein interfacejava.awt.image.BufferedImageOp- Returns:
- a compatible
BufferedImage - Throws:
ImageFilterException- ifpDestCMis notnullor an instance ofIndexColorModel.
-
createCompatibleDestRaster
public final java.awt.image.WritableRaster createCompatibleDestRaster(java.awt.image.Raster pSrc)
Creates a compatibleRasterto dither into. OnlyIndexColorModelallowed.- Specified by:
createCompatibleDestRasterin interfacejava.awt.image.RasterOp- Parameters:
pSrc- the source raster- Returns:
- a
WritableRaster
-
createCompatibleDestRaster
public final java.awt.image.WritableRaster createCompatibleDestRaster(java.awt.image.Raster pSrc, java.awt.image.IndexColorModel pIndexColorModel)Creates a compatibleRasterto dither into.- Parameters:
pSrc- the source raster.pIndexColorModel- the index color model used to create aRaster.- Returns:
- a
WritableRaster
-
getBounds2D
public final java.awt.geom.Rectangle2D getBounds2D(java.awt.image.BufferedImage pSrc)
Returns the bounding box of the filtered destination image. Since this is not a geometric operation, the bounding box does not change.- Specified by:
getBounds2Din interfacejava.awt.image.BufferedImageOp- Parameters:
pSrc- theBufferedImageto be filtered- Returns:
- the bounds of the filtered definition image.
-
getBounds2D
public final java.awt.geom.Rectangle2D getBounds2D(java.awt.image.Raster pSrc)
Returns the bounding box of the filtered destination Raster. Since this is not a geometric operation, the bounding box does not change.- Specified by:
getBounds2Din interfacejava.awt.image.RasterOp- Parameters:
pSrc- theRasterto be filtered- Returns:
- the bounds of the filtered definition
Raster.
-
getPoint2D
public final java.awt.geom.Point2D getPoint2D(java.awt.geom.Point2D pSrcPt, java.awt.geom.Point2D pDstPt)Returns the location of the destination point given a point in the source. IfdstPtis notnull, it will be used to hold the return value. Since this is not a geometric operation, thesrcPtwill equal thedstPt.- Specified by:
getPoint2Din interfacejava.awt.image.BufferedImageOp- Specified by:
getPoint2Din interfacejava.awt.image.RasterOp- Parameters:
pSrcPt- aPoint2Dthat represents a point in the source imagepDstPt- aPoint2Dthat represents the location in the destination- Returns:
- the
Point2Din the destination that corresponds to the specified point in the source.
-
getRenderingHints
public final java.awt.RenderingHints getRenderingHints()
Returns the rendering mHints for this op.- Specified by:
getRenderingHintsin interfacejava.awt.image.BufferedImageOp- Specified by:
getRenderingHintsin interfacejava.awt.image.RasterOp- Returns:
- the
RenderingHintsobject associated with this op.
-
toRGBArray
private static int[] toRGBArray(int pARGB, int[] pBuffer)Converts an int ARGB to int triplet.
-
toIntARGB
private static int toIntARGB(int[] pRGB)
Converts a int triplet to int ARGB.
-
filter
public final java.awt.image.BufferedImage filter(java.awt.image.BufferedImage pSource, java.awt.image.BufferedImage pDest)Performs a single-input/single-output dither operation, applying basic Floyd-Steinberg error-diffusion to the image.- Specified by:
filterin interfacejava.awt.image.BufferedImageOp- Parameters:
pSource- the source imagepDest- the destination image- Returns:
- the destination image, or a new image, if
pDestwasnull.
-
filter
public final java.awt.image.WritableRaster filter(java.awt.image.Raster pSource, java.awt.image.WritableRaster pDest)Performs a single-input/single-output dither operation, applying basic Floyd-Steinberg error-diffusion to the image.- Specified by:
filterin interfacejava.awt.image.RasterOp- Parameters:
pSource- the source raster, assumed to be in sRGBpDest- the destination raster, may benull- Returns:
- the destination raster, or a new raster, if
pDestwasnull.
-
getICM
private java.awt.image.IndexColorModel getICM(java.awt.image.BufferedImage pSource)
-
getICM
private java.awt.image.IndexColorModel getICM(java.awt.image.Raster pSource)
-
createIndexColorModel
private java.awt.image.IndexColorModel createIndexColorModel(java.awt.image.Raster pSource)
-
filter
public final java.awt.image.WritableRaster filter(java.awt.image.Raster pSource, java.awt.image.WritableRaster pDest, java.awt.image.IndexColorModel pColorModel)Performs a single-input/single-output dither operation, applying basic Floyd-Steinberg error-diffusion to the image.- Parameters:
pSource- the source raster, assumed to be in sRGBpDest- the destination raster, may benullpColorModel- the indexed color model to use- Returns:
- the destination raster, or a new raster, if
pDestwasnull.
-
-