org.geotools.image
Class ImageWorker

Object
  extended by ImageWorker

public class ImageWorker
extends Object

Helper methods for applying JAI operations on an image. The image is specified at creation time. Sucessive operations can be applied by invoking the methods defined in this class, and the final image can be obtained by invoking getRenderedImage() at the end of the process.

If an exception is thrown during a method invocation, then this ImageWorker is left in an undetermined state and should not be used anymore.

Since:
2.3
Author:
Simone Giannecchini, Bryce Nordgren, Martin Desruisseaux
Module:
modules/library/coverage (gt-coverage.jar)

Nested Class Summary
static class ImageWorker.PNGImageWriteParam
          Workaround class for compressing PNG using the default PNGImageEncoder shipped with the JDK.
 
Field Summary
protected  RenderedImage image
          The image being built.
static Hints.Key TILING_ALLOWED
          If FALSE, image operators are not allowed to produce tiled images.
 
Constructor Summary
ImageWorker()
          Creates a new uninitialized builder for an image read.
ImageWorker(File input)
          Creates a new builder for an image read from the specified file.
ImageWorker(RenderedImage image)
          Creates a new builder for the specified image.
 
Method Summary
 ImageWorker addBand(RenderedImage image, boolean before)
          Perform a BandMerge operation between the underlying image and the provided one.
 ImageWorker addImage(RenderedImage renderedImage)
          Takes two rendered or renderable source images, and adds every pair of pixels, one from each source image of the corresponding position and band.
 ImageWorker addTransparencyToIndexColorModel(RenderedImage alphaChannel, boolean errorDiffusion)
          Adds transparency to a preexisting image whose color model is index color model.
 ImageWorker addTransparencyToIndexColorModel(RenderedImage alphaChannel, boolean translucent, int transparent, boolean errorDiffusion)
          Adds transparency to a preexisting image whose color model is index color model.
 ImageWorker bandMerge(int writeband)
          Add the bands to the Component Color Model
 ImageWorker binarize()
          Binarizes the image.
 ImageWorker binarize(double threshold)
          Binarizes the image.
 ImageWorker binarize(int value0, int value1)
          Binarizes the image (if not already done) and replace all 0 values by value0 and all 1 values by value1.
 ImageWorker forceBitmaskIndexColorModel()
          Reduces the color model to index color model with opaque or bitmask transparency.
 ImageWorker forceBitmaskIndexColorModel(int transparent, boolean errorDiffusion)
          Reduces the color model to index color model with opaque or bitmask transparency.
 ImageWorker forceColorSpaceGRAYScale()
          Forces the image color model to the GRAYScale color space.
 ImageWorker forceColorSpaceIHS()
          Forces the image color model to the IHS color space.
 ImageWorker forceColorSpaceRGB()
          Forces the image color model to the RGB color space.
 ImageWorker forceComponentColorModel()
          Reformats the color model to a component color model preserving transparency.
 ImageWorker forceComponentColorModel(boolean checkTransparent)
          Reformats the color model to a component color model preserving transparency.
 ImageWorker forceIndexColorModel(boolean error)
          Reduces the color model to index color model.
 ImageWorker forceIndexColorModelForGIF(boolean errorDiffusion)
          Converts the image to a GIF-compliant image.
 ImageWorker format(int dataType)
          Formats the underlying image to the provided data type.
 BufferedImage getBufferedImage()
          Returns the current image as a buffered image.
 ROI getImageAsROI()
          Returns a Region Of Interest built from the current image.
 double[] getMaximums()
          Returns the maximal values found in every image bands.
 double[] getMinimums()
          Returns the minimal values found in every image bands.
 int getNumBands()
          Returns the number of bands in the image.
 PlanarImage getPlanarImage()
          Returns the rendered image as a planar image.
 RenderedImage getRenderedImage()
          Returns the current image.
 RenderedOp getRenderedOperation()
          Returns the rendered image as a rendered operation.
 Object getRenderingHint(RenderingHints.Key key)
          Returns the rendering hint for the specified key, or null if none.
 RenderingHints getRenderingHints()
          Returns the rendering hints for an image to be computed by this class.
 ROI getROI()
          Returns the region of interest currently set, or null if none.
 int getTransparentPixel()
          Returns the transparent pixel value, or -1 if none.
 ImageWorker intensity()
          Creates an image which represents approximatively the intensity of image.
 ImageWorker invert()
          Inverts the pixel values of the image.
 boolean isBinary()
          Returns true if the image is binary.
 boolean isBytes()
          Returns true if the image stores its pixel values in 8 bits.
 boolean isColorSpaceGRAYScale()
          Returns true if the image uses a GrayScale color space.
 boolean isColorSpaceRGB()
          Returns true if the image uses a RGB color space.
 boolean isIndexed()
          Returns true if the image uses an index color model.
 boolean isTranslucent()
          Returns true if the image is translucent.
 void load(String source, int imageChoice, boolean readMetadata)
          Loads an image using the provided file name and the current hints, which are used to control caching and layout.
static void main(String[] args)
          Loads the image from the specified file, and display it in a window.
 ImageWorker makeColorTransparent(Color transparentColor)
          Replaces all occurences of the given color (usually opaque) by a fully transparent color.
 ImageWorker mask(RenderedImage mask, boolean maskValue, int newValue)
          Applies the specified mask over the current image.
 ImageWorker maskComponentColorModelByte(Color transparentColor)
          Deprecated. This method will be private (and maybe replaced) in a future version. Use makeColorTransparent(java.awt.Color) instead. Current implementation invokes a lot of JAI operations: "BandSelect" --> "Lookup" --> "BandCombine" --> "Extrema" --> "Binarize" --> "Format" --> "BandSelect" (one more time) --> "Multiply" --> "BandMerge". I would expect more speed and memory efficiency by writing our own JAI operation (PointOp subclass) doing that in one step. It would also be more deterministic (our "binarize" method depends on statistics on pixel values) and avoid unwanted side-effect like turning black color (RGB = 0,0,0) to transparent one. It would also be easier to maintain I believe.
 ImageWorker maskIndexColorModelByte(Color transparentColor)
          Deprecated. Use makeColorTransparent(java.awt.Color) instead. This method will be private in a future version. The Byte suffix in the method name will be removed since this method works for type USHORT as well.
 ImageWorker multiplyConst(double[] inValues)
          Takes one rendered or renderable image and an array of double constants, and multiplies every pixel of the same band of the source by the constant from the corresponding array entry.
 ImageWorker removeRenderingHint(RenderingHints.Key key)
          Removes a rendering hint.
 ImageWorker rescaleToBytes()
          Rescales the image such that it uses 8 bits.
 ImageWorker retainBands(int numBands)
          Retains inconditionnaly the first numBands of image.
 ImageWorker retainBands(int[] bands)
          Retains inconditionnaly certain bands of image.
 ImageWorker retainFirstBand()
          Retains inconditionnaly the first band of image.
 ImageWorker retainLastBand()
          Retains inconditionnaly the last band of image.
 ImageWorker setImage(RenderedImage image)
          Prepare this builder for the specified image.
 ImageWorker setRenderingHint(RenderingHints.Key key, Object value)
          Sets a rendering hint tile to use for all images to be computed by this class.
 ImageWorker setRenderingHints(RenderingHints hints)
          Set a map of rendering hints to use for all images to be computed by this class.
 ImageWorker setROI(ROI roi)
          Set the region of interest (ROI).
 ImageWorker show()
          Shows the current image in a window together with the operation chain as a tree.
 ImageWorker tile()
          If the was not already tiled, tile it.
 ImageWorker tileCacheEnabled(boolean status)
          If false, disables the tile cache.
 ImageWorker write(File output)
          Writes the image to the specified file.
 ImageWorker writeGIF(Object destination, String compression, float compressionRate)
          Writes outs the image contained into this ImageWorker as a GIF using the provided destination, compression and compression rate.
 void writeJPEG(Object destination, String compression, float compressionRate, boolean nativeAcc)
          Writes outs the image contained into this ImageWorker as a JPEG using the provided destination , compression and compression rate.
 void writePNG(Object destination, String compression, float compressionRate, boolean nativeAcc, boolean paletted)
          Writes outs the image contained into this ImageWorker as a PNG using the provided destination, compression and compression rate.
 ImageWorker xorConst(int[] values)
          Takes one rendered or renderable image and an array of integer constants, and performs a bit-wise logical "xor" between every pixel in the same band of the source and the constant from the corresponding array entry.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TILING_ALLOWED

public static final Hints.Key TILING_ALLOWED
If FALSE, image operators are not allowed to produce tiled images. The default is TRUE. The FALSE value is sometime useful for exporting images to some formats that doesn't support tiling (e.g. GIF).

See Also:
setRenderingHint(java.awt.RenderingHints.Key, java.lang.Object)

image

protected RenderedImage image
The image being built.

Constructor Detail

ImageWorker

public ImageWorker()
Creates a new uninitialized builder for an image read.

See Also:
load(java.lang.String, int, boolean)

ImageWorker

public ImageWorker(File input)
            throws IOException
Creates a new builder for an image read from the specified file.

Parameters:
input - The file to read.
Throws:
IOException - if the file can't be read.

ImageWorker

public ImageWorker(RenderedImage image)
Creates a new builder for the specified image. The images to be computed (if any) will save their tiles in the default tile cache.

Parameters:
image - The source image.
Method Detail

setImage

public final ImageWorker setImage(RenderedImage image)
Prepare this builder for the specified image. The images to be computed (if any) will save their tiles in the default tile cache.

Parameters:
image - The source image.

load

public final void load(String source,
                       int imageChoice,
                       boolean readMetadata)
Loads an image using the provided file name and the current hints, which are used to control caching and layout.

Parameters:
source - Filename of the source image to read.
imageChoice - Image index in multipage images.
readMatadata - If true, metadata will be read.

getRenderedImage

public final RenderedImage getRenderedImage()
Returns the current image.

Returns:
The rendered image.
See Also:
getBufferedImage(), getPlanarImage(), getRenderedOperation(), getImageAsROI()

getBufferedImage

public final BufferedImage getBufferedImage()
Returns the current image as a buffered image.

Returns:
The buffered image.
Since:
2.5
See Also:
getRenderedImage(), getPlanarImage(), getRenderedOperation(), getImageAsROI()

getPlanarImage

public final PlanarImage getPlanarImage()
Returns the rendered image as a planar image.

Returns:
The planar image.
See Also:
getRenderedImage(), getRenderedOperation(), getImageAsROI()

getRenderedOperation

public final RenderedOp getRenderedOperation()
Returns the rendered image as a rendered operation.

Returns:
The rendered operation.
See Also:
getRenderedImage(), getPlanarImage(), getImageAsROI()

getImageAsROI

public final ROI getImageAsROI()
Returns a Region Of Interest built from the current image. If the image is multi-bands, then this method first computes an estimation of its intensity. Next, this method binarize the image and constructs a ROI from the result.

Returns:
The image as a region of interest.
See Also:
getRenderedImage(), getPlanarImage(), getRenderedOperation()

getROI

public final ROI getROI()
Returns the region of interest currently set, or null if none. The default value is null.

Returns:
The current region of interest.
See Also:
getMinimums(), getMaximums()

setROI

public final ImageWorker setROI(ROI roi)
Set the region of interest (ROI). A null set the ROI to the whole image. The ROI is used by statistical methods like getMinimums() and getMaximums().

Parameters:
roi - The new region of interest.
Returns:
This ImageWorker
See Also:
getMinimums(), getMaximums()

getRenderingHint

public final Object getRenderingHint(RenderingHints.Key key)
Returns the rendering hint for the specified key, or null if none.


setRenderingHint

public final ImageWorker setRenderingHint(RenderingHints.Key key,
                                          Object value)
Sets a rendering hint tile to use for all images to be computed by this class. This method applies only to the next images to be computed; images already computed before this method call (if any) will not be affected.

Some common examples:

Returns:
This ImageWorker

setRenderingHints

public final ImageWorker setRenderingHints(RenderingHints hints)
Set a map of rendering hints to use for all images to be computed by this class. This method applies only to the next images to be computed; images already computed before this method call (if any) will not be affected.

If hints is null we won't modify this list.

Returns:
This ImageWorker
See Also:
#setRenderingHint(RenderingHints)

removeRenderingHint

public final ImageWorker removeRenderingHint(RenderingHints.Key key)
Removes a rendering hint. Note that invoking this method is not the same than invoking setRenderingHint(key, null). This is especially true for the tile cache hint:

Returns:
This ImageWorker

getRenderingHints

public final RenderingHints getRenderingHints()
Returns the rendering hints for an image to be computed by this class. The default implementation returns the following hints:

Returns:
The rendering hints to use for image computation (never null).

tileCacheEnabled

public final ImageWorker tileCacheEnabled(boolean status)
If false, disables the tile cache. Invoking this method with value true cancel the last invocation with value false. If this method was invoking many time with value false, then this method must be invoked the same amount of time with the value true for reenabling the cache.

Note: This method name doesn't contain the usual set prefix because it doesn't really set a flag. Instead it increments or decrements a counter.

Returns:
This ImageWorker

getNumBands

public final int getNumBands()
Returns the number of bands in the image.

See Also:
retainBands(int), retainFirstBand(), SampleModel.getNumBands()

getTransparentPixel

public final int getTransparentPixel()
Returns the transparent pixel value, or -1 if none.


getMinimums

public final double[] getMinimums()
Returns the minimal values found in every image bands. If a region of interest is defined, then the statistics will be computed only over that region.

See Also:
getMaximums(), setROI(javax.media.jai.ROI)

getMaximums

public final double[] getMaximums()
Returns the maximal values found in every image bands. If a region of interest is defined, then the statistics will be computed only over that region.

See Also:
getMinimums(), setROI(javax.media.jai.ROI)

isBytes

public final boolean isBytes()
Returns true if the image stores its pixel values in 8 bits.

See Also:
rescaleToBytes()

isBinary

public final boolean isBinary()
Returns true if the image is binary. Such image usually contains only two values: 0 and 1.

See Also:
binarize(), binarize(double), binarize(int,int)

isIndexed

public final boolean isIndexed()
Returns true if the image uses an index color model.

See Also:
forceIndexColorModel(boolean), forceBitmaskIndexColorModel(), forceIndexColorModelForGIF(boolean)

isColorSpaceRGB

public final boolean isColorSpaceRGB()
Returns true if the image uses a RGB color space. Note that a RGB color space doesn't mean that pixel values are directly stored as RGB components. The image may be indexed as well.

See Also:
forceColorSpaceRGB()

isColorSpaceGRAYScale

public final boolean isColorSpaceGRAYScale()
Returns true if the image uses a GrayScale color space. Note that a GrayScale color space doesn't mean that pixel values are directly stored as GrayScale component. The image may be indexed as well.

See Also:
forceColorSpaceGRAYScale()

isTranslucent

public final boolean isTranslucent()
Returns true if the image is translucent.

See Also:
forceBitmaskIndexColorModel()

rescaleToBytes

public final ImageWorker rescaleToBytes()
Rescales the image such that it uses 8 bits. If the image already uses 8 bits, then this method does nothing. Otherwise this method computes the minimum and maximum values for each band, rescale them in the range [0 .. 255] and force the resulting image to TYPE_BYTE.

Returns:
This ImageWorker
See Also:
isBytes(), RescaleDescriptor

forceIndexColorModel

public final ImageWorker forceIndexColorModel(boolean error)
Reduces the color model to index color model. If the current image already uses an index color model, then this method do nothing. Otherwise, the current implementation performs a ditering on the original color model. Note that this operation loose the alpha channel.

This for the moment should work only with opaque images, with non opaque images we just remove the alpha band in order to build an IndexColorModel. This is one because in general it could be very difficult to decide the final transparency for each pixel given the complexity if the algorithms for obtaining an IndexColorModel.

If an IndexColorModel with a single transparency index is enough for you, we advise you to take a look at forceIndexColorModelForGIF(boolean) methdo.

See Also:
isIndexed(), forceBitmaskIndexColorModel(), forceIndexColorModelForGIF(boolean), OrderedDitherDescriptor

forceBitmaskIndexColorModel

public final ImageWorker forceBitmaskIndexColorModel()
Reduces the color model to index color model with opaque or bitmask transparency. If the current image already uses a suitable color model, then this method do nothing.

Returns:
this ImageWorker.
See Also:
isIndexed(), isTranslucent(), forceIndexColorModel(boolean), forceIndexColorModelForGIF(boolean)

forceBitmaskIndexColorModel

public final ImageWorker forceBitmaskIndexColorModel(int transparent,
                                                     boolean errorDiffusion)
Reduces the color model to index color model with opaque or bitmask transparency. If the current image already uses a suitable color model, then this method do nothing.

Parameters:
transparent - A pixel value to define as the transparent pixel. *
errorDiffusion - Tells if I should use ErrorDiffusionDescriptor or OrderedDitherDescriptor JAi operations. errorDiffusion
Returns:
this ImageWorker.
See Also:
isIndexed(), isTranslucent(), forceIndexColorModel(boolean), forceIndexColorModelForGIF(boolean)

forceIndexColorModelForGIF

public final ImageWorker forceIndexColorModelForGIF(boolean errorDiffusion)
Converts the image to a GIF-compliant image. This method has been created in order to convert the input image to a form that is compatible with the GIF model. It first remove the information about transparency since the error diffusion and the error dither operations are unable to process images with more than 3 bands. Afterwards the image is processed with an error diffusion operator in order to reduce the number of bands from 3 to 1 and the number of color to 216. A suitable layout is used for the final image via the rendering hints in order to take into account the different layout model for the final image.

Tip: For optimizing writing GIF, we need to create the image untiled. This can be done by invoking setRenderingHint(TILING_ALLOWED, Boolean.FALSE) first.

Parameters:
errorDiffusion - Tells if I should use ErrorDiffusionDescriptor or OrderedDitherDescriptor JAi operations.
Returns:
this ImageWorker.
See Also:
isIndexed(), forceIndexColorModel(boolean), forceBitmaskIndexColorModel()

forceComponentColorModel

public final ImageWorker forceComponentColorModel()
Reformats the color model to a component color model preserving transparency. This is used especially in order to go from PackedColorModel to ComponentColorModel, which seems to be well accepted from PNGEncoder and TIFFEncoder.

This code is adapted from jai-interests mailing list archive.

Returns:
this ImageWorker.
See Also:
FormatDescriptor

forceComponentColorModel

public final ImageWorker forceComponentColorModel(boolean checkTransparent)
Reformats the color model to a component color model preserving transparency. This is used especially in order to go from PackedColorModel to ComponentColorModel, which seems to be well accepted from PNGEncoder and TIFFEncoder.

This code is adapted from jai-interests mailing list archive.

Parameters:
checkTransparent - tells this method to not consider fully transparent pixels when optimizing grayscale palettes.
Returns:
this ImageWorker.
See Also:
FormatDescriptor

forceColorSpaceRGB

public final ImageWorker forceColorSpaceRGB()
Forces the image color model to the RGB color space. If the current color space is already of RGB type, then this method does nothing. This operation may loose the alpha channel.

Returns:
this ImageWorker.
See Also:
isColorSpaceRGB(), ColorConvertDescriptor

forceColorSpaceIHS

public final ImageWorker forceColorSpaceIHS()
Forces the image color model to the IHS color space. If the current color space is already of IHS type, then this method does nothing. This operation may loose the alpha channel.

Returns:
this ImageWorker.
See Also:
ColorConvertDescriptor

bandMerge

public final ImageWorker bandMerge(int writeband)
Add the bands to the Component Color Model

Parameters:
writeband - number of bands after the bandmerge.
Returns:
this ImageWorker.

addBand

public final ImageWorker addBand(RenderedImage image,
                                 boolean before)
Perform a BandMerge operation between the underlying image and the provided one.

Parameters:
image - to merge with the underlying one.
before - true if we want to use first the provided image, false otherwise.
Returns:
this ImageWorker.

forceColorSpaceGRAYScale

public final ImageWorker forceColorSpaceGRAYScale()
Forces the image color model to the GRAYScale color space. If the current color space is already of type, then this method does nothing.

Returns:
this ImageWorker.
See Also:
isColorSpaceGRAYScale(), ColorConvertDescriptor

intensity

public final ImageWorker intensity()
Creates an image which represents approximatively the intensity of image. The result is always a single-banded image. If the image uses an IHS color space, then this method just retain the first band without any further processing. Otherwise, this method performs a simple band combine operation on the image in order to come up with a simple estimation of the intensity of the image based on the average value of the color components. It is worthwhile to note that the alpha band is stripped from the image.

Returns:
this ImageWorker.
See Also:
BandCombineDescriptor

retainFirstBand

public final ImageWorker retainFirstBand()
Retains inconditionnaly the first band of image. All other bands (if any) are discarted without any further processing.

Returns:
this ImageWorker.
See Also:
getNumBands(), retainBands(int), BandSelectDescriptor

retainLastBand

public final ImageWorker retainLastBand()
Retains inconditionnaly the last band of image. All other bands (if any) are discarted without any further processing.

Returns:
this ImageWorker.
See Also:
getNumBands(), retainBands(int), BandSelectDescriptor

retainBands

public final ImageWorker retainBands(int numBands)
Retains inconditionnaly the first numBands of image. All other bands (if any) are discarted without any further processing. This method does nothing if the current image does not have a greater amount of bands than numBands.

Parameters:
numBands - the number of bands to retain.
Returns:
this ImageWorker.
See Also:
getNumBands(), retainFirstBand(), BandSelectDescriptor

retainBands

public final ImageWorker retainBands(int[] bands)
Retains inconditionnaly certain bands of image. All other bands (if any) are discarded without any further processing.

Parameters:
bands - the bands to retain.
Returns:
this ImageWorker.
See Also:
getNumBands(), retainFirstBand(), BandSelectDescriptor

format

public final ImageWorker format(int dataType)
Formats the underlying image to the provided data type.

Parameters:
dataType - to be used for this FormatDescriptor operation.
Returns:
this ImageWorker

binarize

public final ImageWorker binarize()
Binarizes the image. If the image is multi-bands, then this method first computes an estimation of its intensity. Then, the threshold value is set halfway between the minimal and maximal values found in the image.

Returns:
this ImageWorker.
See Also:
isBinary(), binarize(double), binarize(int,int), BinarizeDescriptor

binarize

public final ImageWorker binarize(double threshold)
Binarizes the image. If the image is already binarized, then this method does nothing.

Parameters:
threshold - The threshold value.
Returns:
this ImageWorker.
See Also:
isBinary(), binarize(), binarize(int,int), BinarizeDescriptor

binarize

public final ImageWorker binarize(int value0,
                                  int value1)
Binarizes the image (if not already done) and replace all 0 values by value0 and all 1 values by value1. If the image should be binarized using a custom threshold value (instead of the automatic one), invoke binarize(double) explicitly before this method.

Returns:
this ImageWorker.
See Also:
isBinary(), binarize(), binarize(double), BinarizeDescriptor, LookupDescriptor

makeColorTransparent

public final ImageWorker makeColorTransparent(Color transparentColor)
                                       throws IllegalStateException
Replaces all occurences of the given color (usually opaque) by a fully transparent color. Currents implementation supports image backed by any IndexColorModel, or by ComponentColorModel with TYPE_BYTE. More types may be added in future GeoTools versions.

Parameters:
transparentColor - The color to make transparent.
Returns:
this image worker.
Throws:
IllegalStateException - if the current image has an unsupported color model.

maskIndexColorModelByte

@Deprecated
public final ImageWorker maskIndexColorModelByte(Color transparentColor)
Deprecated. Use makeColorTransparent(java.awt.Color) instead. This method will be private in a future version. The Byte suffix in the method name will be removed since this method works for type USHORT as well.

For an image backed by an IndexColorModel, replaces all occurences of the given color (usually opaque) by a fully transparent color.

Parameters:
transparentColor - The color to make transparent.
Returns:
this image worker.

maskComponentColorModelByte

@Deprecated
public final ImageWorker maskComponentColorModelByte(Color transparentColor)
Deprecated. This method will be private (and maybe replaced) in a future version. Use makeColorTransparent(java.awt.Color) instead. Current implementation invokes a lot of JAI operations: "BandSelect" --> "Lookup" --> "BandCombine" --> "Extrema" --> "Binarize" --> "Format" --> "BandSelect" (one more time) --> "Multiply" --> "BandMerge". I would expect more speed and memory efficiency by writing our own JAI operation (PointOp subclass) doing that in one step. It would also be more deterministic (our "binarize" method depends on statistics on pixel values) and avoid unwanted side-effect like turning black color (RGB = 0,0,0) to transparent one. It would also be easier to maintain I believe.

For an image backed by an ComponentColorModel, replaces all occurences of the given color (usually opaque) by a fully transparent color.

Parameters:
transparentColor - The color to make transparent.
Returns:
this image worker.

invert

public final ImageWorker invert()
Inverts the pixel values of the image.

See Also:
InvertDescriptor

mask

public final ImageWorker mask(RenderedImage mask,
                              boolean maskValue,
                              int newValue)
Applies the specified mask over the current image. The mask should be binarized - if it is not, this method will do it itself. Then, for every pixels in the mask with value equals to maskValue, the corresponding pixel in the image will be set to the specified newValue.

Note: current implementation force the color model to an indexed one. Future versions may avoid this change.

Parameters:
mask - The mask to apply, as a binarized image.
maskValue - The mask value to search for (false for 0 or true for 1).
newValue - The new value for every pixels in image corresponding to maskValue in the mask.
Returns:
this ImageWorker.

addImage

public final ImageWorker addImage(RenderedImage renderedImage)
Takes two rendered or renderable source images, and adds every pair of pixels, one from each source image of the corresponding position and band. See JAI AddDescriptor for details.

Parameters:
renderedImage - the RenderedImage to be added to this ImageWorker.
Returns:
this ImageWorker.
See Also:
AddDescriptor

multiplyConst

public final ImageWorker multiplyConst(double[] inValues)
Takes one rendered or renderable image and an array of double constants, and multiplies every pixel of the same band of the source by the constant from the corresponding array entry. See JAI MultiplyConstDescriptor for details.

Parameters:
inValues - The constants to be multiplied.
Returns:
this ImageWorker.
See Also:
MultiplyConstDescriptor

xorConst

public final ImageWorker xorConst(int[] values)
Takes one rendered or renderable image and an array of integer constants, and performs a bit-wise logical "xor" between every pixel in the same band of the source and the constant from the corresponding array entry. See JAI XorConstDescriptor for details.

See Also:
XorConstDescriptor

addTransparencyToIndexColorModel

public ImageWorker addTransparencyToIndexColorModel(RenderedImage alphaChannel,
                                                    boolean errorDiffusion)
Adds transparency to a preexisting image whose color model is index color model. For all pixels with the value false in the specified transparency mask, the corresponding pixel in the image is set to the transparent pixel value. All other pixels are left unchanged.

Parameters:
alphaChannel - The mask to apply as a binarized image.
errorDiffusion - Tells if I should use ErrorDiffusionDescriptor or OrderedDitherDescriptor JAi operations.
Returns:
this ImageWorker.
See Also:
isTranslucent(), forceBitmaskIndexColorModel()

addTransparencyToIndexColorModel

public final ImageWorker addTransparencyToIndexColorModel(RenderedImage alphaChannel,
                                                          boolean translucent,
                                                          int transparent,
                                                          boolean errorDiffusion)
Adds transparency to a preexisting image whose color model is index color model. First, this method creates a new index color model with the specified transparent pixel, if needed (this method may skip this step if the specified pixel is already transparent. Then for all pixels with the value false in the specified transparency mask, the corresponding pixel in the image is set to that transparent value. All other pixels are left unchanged.

Parameters:
alphaChannel - The mask to apply as a binarized image.
translucent - true if translucent images are allowed, or false if the resulting images must be a bitmask.
transparent - The value for transparent pixels, to be given to every pixels in the image corresponding to false in the mask. The special value -1 maps to the last pixel value allowed for the indexed color model.
errorDiffusion - Tells if I should use ErrorDiffusionDescriptor or OrderedDitherDescriptor JAi operations.
Returns:
this ImageWorker.

tile

public final ImageWorker tile()
If the was not already tiled, tile it. Note that no tiling will be done if 'getRenderingHints()' failed to suggest a tile size. This method is for internal use by write(java.io.File) methods only.

Returns:
this ImageWorker.

write

public final ImageWorker write(File output)
                        throws IOException
Writes the image to the specified file. This method differs from ImageIO#write(String,File) in a number of ways:

Returns:
this ImageWorker.
Throws:
IOException

writePNG

public final void writePNG(Object destination,
                           String compression,
                           float compressionRate,
                           boolean nativeAcc,
                           boolean paletted)
                    throws IOException
Writes outs the image contained into this ImageWorker as a PNG using the provided destination, compression and compression rate.

The destination object can be anything providing that we have an ImageOutputStreamSpi that recognizes it.

Parameters:
destination - where to write the internal image as a PNG.
compression - algorithm.
compressionRate - percentage of compression.
nativeAcc - should we use native acceleration.
paletted - should we write the png as 8 bits?
Throws:
IOException - In case an error occurs during the search for an ImageOutputStream or during the eoncding process.

writeGIF

public final ImageWorker writeGIF(Object destination,
                                  String compression,
                                  float compressionRate)
                           throws IOException
Writes outs the image contained into this ImageWorker as a GIF using the provided destination, compression and compression rate.

It is worth to point out that the only compressions algorithm availaible with the jdk GIFImageWriter is "LZW" while the compression rates have to be confined between 0 and 1. AN acceptable values is usally 0.75f.

The destination object can be anything providing that we have an ImageOutputStreamSpi that recognizes it.

Parameters:
destination - where to write the internal image as a gif.
compression - The name of compression algorithm.
compressionRate - percentage of compression, as a number between 0 and 1.
Returns:
this ImageWorker.
Throws:
IOException - In case an error occurs during the search for an ImageOutputStream or during the eoncding process.
See Also:
forceIndexColorModelForGIF(boolean)

writeJPEG

public final void writeJPEG(Object destination,
                            String compression,
                            float compressionRate,
                            boolean nativeAcc)
                     throws IOException
Writes outs the image contained into this ImageWorker as a JPEG using the provided destination , compression and compression rate.

The destination object can be anything providing that we have an ImageOutputStreamSpi that recognizes it.

Parameters:
destination - where to write the internal image as a JPEG.
compression - algorithm.
compressionRate - percentage of compression.
nativeAcc - should we use native acceleration.
Throws:
IOException - In case an error occurs during the search for an ImageOutputStream or during the eoncding process.

show

public final ImageWorker show()
                       throws HeadlessException
Shows the current image in a window together with the operation chain as a tree. This method is provided mostly for debugging purpose. This method requires the gt2-widgets-swing.jar file in the classpath.

Returns:
this ImageWorker.
Throws:
HeadlessException - if gt2-widgets-swing.jar is not on the classpath, or if AWT can't create the window components.
See Also:
org.geotools.gui.swing.image.OperationTreeBrowser#show(RenderedImage)

main

public static void main(String[] args)
Loads the image from the specified file, and display it in a window. This method is mostly as a convenient way to test operation chains. This method can be invoked from the command line. If an optional -operation argument is provided, the Java method (one of the image operations provided in this class) immediately following it is executed. Example:
 java org.geotools.image.ImageWorker -operation binarize <var><filename></var>
 



Copyright © 1996-2009 Geotools. All Rights Reserved.