Package com.twelvemonkeys.image
Class DiffusionDither
java.lang.Object
com.twelvemonkeys.image.DiffusionDither
- All Implemented Interfaces:
BufferedImageOp
,RasterOp
This
BufferedImageOp/RasterOp
implements basic
Floyd-Steinberg error-diffusion algorithm for dithering.
The weights used are 7/16, 3/16, 5/16 and 1/16, distributed like this:
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
FieldsModifier and TypeFieldDescriptionprivate boolean
private static final int
protected final IndexColorModel
private static final Random
-
Constructor Summary
ConstructorsConstructorDescriptionCreates aDiffusionDither
, with no fixedIndexColorModel
.Creates aDiffusionDither
, using the givenIndexColorModel
for dithering into. -
Method Summary
Modifier and TypeMethodDescriptionfinal BufferedImage
createCompatibleDestImage
(BufferedImage pSource, ColorModel pDestCM) Creates a compatibleBufferedImage
to dither into.final WritableRaster
Creates a compatibleRaster
to dither into.final WritableRaster
createCompatibleDestRaster
(Raster pSrc, IndexColorModel pIndexColorModel) Creates a compatibleRaster
to dither into.private IndexColorModel
createIndexColorModel
(Raster pSource) final BufferedImage
filter
(BufferedImage pSource, BufferedImage pDest) Performs a single-input/single-output dither operation, applying basic Floyd-Steinberg error-diffusion to the image.final WritableRaster
filter
(Raster pSource, WritableRaster pDest) Performs a single-input/single-output dither operation, applying basic Floyd-Steinberg error-diffusion to the image.final WritableRaster
filter
(Raster pSource, WritableRaster pDest, IndexColorModel pColorModel) Performs a single-input/single-output dither operation, applying basic Floyd-Steinberg error-diffusion to the image.final Rectangle2D
getBounds2D
(BufferedImage pSrc) Returns the bounding box of the filtered destination image.final Rectangle2D
getBounds2D
(Raster pSrc) Returns the bounding box of the filtered destination Raster.private IndexColorModel
getICM
(BufferedImage pSource) private IndexColorModel
final Point2D
getPoint2D
(Point2D pSrcPt, Point2D pDstPt) Returns the location of the destination point given a point in the source.final RenderingHints
Returns the rendering mHints for this op.void
setAlternateScans
(boolean pUse) Sets the scan mode.private static int
toIntARGB
(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 Details
-
FS_SCALE
private static final int FS_SCALE- See Also:
-
RANDOM
-
indexColorModel
-
alternateScans
private boolean alternateScans
-
-
Constructor Details
-
DiffusionDither
Creates aDiffusionDither
, using the givenIndexColorModel
for 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 Details
-
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
-true
if scan mode should be alternating left/right
-
createCompatibleDestImage
Creates a compatibleBufferedImage
to dither into. OnlyIndexColorModel
allowed.- Specified by:
createCompatibleDestImage
in interfaceBufferedImageOp
- Returns:
- a compatible
BufferedImage
- Throws:
ImageFilterException
- ifpDestCM
is notnull
or an instance ofIndexColorModel
.
-
createCompatibleDestRaster
Creates a compatibleRaster
to dither into. OnlyIndexColorModel
allowed.- Specified by:
createCompatibleDestRaster
in interfaceRasterOp
- Parameters:
pSrc
- the source raster- Returns:
- a
WritableRaster
-
createCompatibleDestRaster
public final WritableRaster createCompatibleDestRaster(Raster pSrc, IndexColorModel pIndexColorModel) Creates a compatibleRaster
to dither into.- Parameters:
pSrc
- the source raster.pIndexColorModel
- the index color model used to create aRaster
.- Returns:
- a
WritableRaster
-
getBounds2D
Returns the bounding box of the filtered destination image. Since this is not a geometric operation, the bounding box does not change.- Specified by:
getBounds2D
in interfaceBufferedImageOp
- Parameters:
pSrc
- theBufferedImage
to be filtered- Returns:
- the bounds of the filtered definition image.
-
getBounds2D
Returns the bounding box of the filtered destination Raster. Since this is not a geometric operation, the bounding box does not change.- Specified by:
getBounds2D
in interfaceRasterOp
- Parameters:
pSrc
- theRaster
to be filtered- Returns:
- the bounds of the filtered definition
Raster
.
-
getPoint2D
Returns the location of the destination point given a point in the source. IfdstPt
is notnull
, it will be used to hold the return value. Since this is not a geometric operation, thesrcPt
will equal thedstPt
.- Specified by:
getPoint2D
in interfaceBufferedImageOp
- Specified by:
getPoint2D
in interfaceRasterOp
- Parameters:
pSrcPt
- aPoint2D
that represents a point in the source imagepDstPt
- aPoint2D
that represents the location in the destination- Returns:
- the
Point2D
in the destination that corresponds to the specified point in the source.
-
getRenderingHints
Returns the rendering mHints for this op.- Specified by:
getRenderingHints
in interfaceBufferedImageOp
- Specified by:
getRenderingHints
in interfaceRasterOp
- Returns:
- the
RenderingHints
object 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
Performs a single-input/single-output dither operation, applying basic Floyd-Steinberg error-diffusion to the image.- Specified by:
filter
in interfaceBufferedImageOp
- Parameters:
pSource
- the source imagepDest
- the destination image- Returns:
- the destination image, or a new image, if
pDest
wasnull
.
-
filter
Performs a single-input/single-output dither operation, applying basic Floyd-Steinberg error-diffusion to the image. -
getICM
-
getICM
-
createIndexColorModel
-
filter
public final WritableRaster filter(Raster pSource, WritableRaster pDest, 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 benull
pColorModel
- the indexed color model to use- Returns:
- the destination raster, or a new raster, if
pDest
wasnull
.
-