APIdia Launch Partner
Top Description Fields Constructors Methods
java.awt.image

public Class RescaleOp

extends Object
implements BufferedImageOp, RasterOp
Class Inheritance
All Implemented Interfaces
java.awt.image.RasterOp, java.awt.image.BufferedImageOp
Imports
java.awt.geom.Rectangle2D, .Point2D, java.awt.AlphaComposite, .Graphics2D, .RenderingHints, sun.awt.image.ImagingLib

This class performs a pixel-by-pixel rescaling of the data in the source image by multiplying the sample values for each pixel by a scale factor and then adding an offset. The scaled sample values are clipped to the minimum/maximum representable in the destination image.

The pseudo code for the rescaling operation is as follows:

for each pixel from Source object {
    for each band/component of the pixel {
        dstElement = (srcElement*scaleFactor) + offset
    }
}
 

For Rasters, rescaling operates on bands. The number of sets of scaling constants may be one, in which case the same constants are applied to all bands, or it must equal the number of Source Raster bands.

For BufferedImages, rescaling operates on color and alpha components. The number of sets of scaling constants may be one, in which case the same constants are applied to all color (but not alpha) components. Otherwise, the number of sets of scaling constants may equal the number of Source color components, in which case no rescaling of the alpha component (if present) is performed. If neither of these cases apply, the number of sets of scaling constants must equal the number of Source color components plus alpha components, in which case all color and alpha components are rescaled.

BufferedImage sources with premultiplied alpha data are treated in the same manner as non-premultiplied images for purposes of rescaling. That is, the rescaling is done per band on the raw data of the BufferedImage source without regard to whether the data is premultiplied. If a color conversion is required to the destination ColorModel, the premultiplied state of both source and destination will be taken into account for this step.

Images with an IndexColorModel cannot be rescaled.

If a RenderingHints object is specified in the constructor, the color rendering hint and the dithering hint may be used when color conversion is required.

Note that in-place operation is allowed (i.e. the source and destination can be the same object).

See Also
java.awt.RenderingHints#KEY_COLOR_RENDERING, java.awt.RenderingHints#KEY_DITHERING

Field Summary

Modifier and TypeField and Description
private int
pack-priv RenderingHints
pack-priv int
pack-priv float[]
pack-priv float[]
private int

Constructor Summary

AccessConstructor and Description
public
RescaleOp(float[]
the specified scale factors
scaleFactors
,
float[]
the specified offsets
offsets
,
RenderingHints
the specified RenderingHints, or null
hints
)

Constructs a new RescaleOp with the desired scale factors and offsets.

public
RescaleOp(float
the specified scale factor
scaleFactor
,
float
the specified offset
offset
,
RenderingHints
the specified RenderingHints, or null
hints
)

Constructs a new RescaleOp with the desired scale factor and offset.

Method Summary

Modifier and TypeMethod and Description
private boolean
canUseLookup(Raster src, Raster dst)

Determines if the rescale can be performed as a lookup.

private ByteLookupTable
createByteLut(float[] scale, float[] off, int nBands, int
Number of elements the table is to have. This will generally be 256 for byte and 65536 for short.
nElems
)

Creates a ByteLookupTable to implement the rescale.

public BufferedImage

Returns:

the zeroed-destination image.
createCompatibleDestImage
(BufferedImage
Source image for the filter operation.
src
,
ColorModel
ColorModel of the destination. If null, the ColorModel of the source will be used.
destCM
)

Implements java.awt.image.BufferedImageOp.createCompatibleDestImage.

Creates a zeroed destination image with the correct size and number of bands.

public WritableRaster

Returns:

the zeroed-destination Raster.
createCompatibleDestRaster
(Raster
the source Raster
src
)

Implements java.awt.image.RasterOp.createCompatibleDestRaster.

Creates a zeroed-destination Raster with the correct size and number of bands, given this source.

private ShortLookupTable
createShortLut(float[] scale, float[] off, int nBands, int
Number of elements the table is to have. This will generally be 256 for byte and 65536 for short.
nElems
)

Creates a ShortLookupTable to implement the rescale.

public final BufferedImage

Returns:

the filtered BufferedImage.
filter
(BufferedImage
the BufferedImage to be filtered
src
,
BufferedImage
the destination for the filtering operation or null
dst
)

Implements java.awt.image.BufferedImageOp.filter.

Rescales the source BufferedImage.

public final WritableRaster

Returns:

the filtered WritableRaster.
filter
(Raster
the Raster to be filtered
src
,
WritableRaster
the destination for the filtering operation or null
dst
)

Implements java.awt.image.RasterOp.filter.

Rescales the pixel data in the source Raster.

private WritableRaster
filterRasterImpl(Raster src, WritableRaster dst, int scaleConst, boolean sCheck)

public final Rectangle2D
getBounds2D(BufferedImage src)

Implements java.awt.image.BufferedImageOp.getBounds2D.

Returns the bounding box of the rescaled destination image.

public final Rectangle2D

Returns:

the bounds of the specified Raster.
getBounds2D
(Raster
the rescaled destination Raster
src
)

Implements java.awt.image.RasterOp.getBounds2D.

Returns the bounding box of the rescaled destination Raster.

public final int

Returns:

the number of scaling factors and offsets of this RescaleOp.
getNumFactors
()

Returns the number of scaling factors and offsets used in this RescaleOp.

public final float[]

Returns:

the offsets of this RescaleOp.
getOffsets
(float[]
the array to contain the offsets of this RescaleOp
offsets
)

Returns the offsets in the given array.

public final Point2D

Returns:

the location of the destination point.
getPoint2D
(Point2D
a point in the source image
srcPt
,
Point2D
the destination point or null
dstPt
)

Implements java.awt.image.BufferedImageOp.getPoint2D, java.awt.image.RasterOp.getPoint2D.

Returns the location of the destination point given a point in the source.

public final RenderingHints

Returns:

the rendering hints of this RescaleOp.
getRenderingHints
()

Implements java.awt.image.BufferedImageOp.getRenderingHints, java.awt.image.RasterOp.getRenderingHints.

Returns the rendering hints for this op.

public final float[]

Returns:

the scale factors of this RescaleOp.
getScaleFactors
(float[]
the array to contain the scale factors of this RescaleOp
scaleFactors
)

Returns the scale factors in the given array.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dstNbitsback to summary
private int dstNbits
hintsback to summary
pack-priv RenderingHints hints
lengthback to summary
pack-priv int length
offsetsback to summary
pack-priv float[] offsets
scaleFactorsback to summary
pack-priv float[] scaleFactors
srcNbitsback to summary
private int srcNbits

Constructor Detail

RescaleOpback to summary
public RescaleOp(float[] scaleFactors, float[] offsets, RenderingHints hints)

Constructs a new RescaleOp with the desired scale factors and offsets. The length of the scaleFactor and offset arrays must meet the restrictions stated in the class comments above. The RenderingHints argument may be null.

Parameters
scaleFactors:float[]

the specified scale factors

offsets:float[]

the specified offsets

hints:RenderingHints

the specified RenderingHints, or null

RescaleOpback to summary
public RescaleOp(float scaleFactor, float offset, RenderingHints hints)

Constructs a new RescaleOp with the desired scale factor and offset. The scaleFactor and offset will be applied to all bands in a source Raster and to all color (but not alpha) components in a BufferedImage. The RenderingHints argument may be null.

Parameters
scaleFactor:float

the specified scale factor

offset:float

the specified offset

hints:RenderingHints

the specified RenderingHints, or null

Method Detail

canUseLookupback to summary
private boolean canUseLookup(Raster src, Raster dst)

Determines if the rescale can be performed as a lookup. The dst must be a byte or short type. The src must be less than 16 bits. All source band sizes must be the same and all dst band sizes must be the same.

createByteLutback to summary
private ByteLookupTable createByteLut(float[] scale, float[] off, int nBands, int nElems)

Creates a ByteLookupTable to implement the rescale. The table may have either a SHORT or BYTE input.

Parameters
nElems:int

Number of elements the table is to have. This will generally be 256 for byte and 65536 for short.

createCompatibleDestImageback to summary
public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel destCM)

Implements java.awt.image.BufferedImageOp.createCompatibleDestImage.

Creates a zeroed destination image with the correct size and number of bands.

Parameters
src:BufferedImage

Source image for the filter operation.

destCM:ColorModel

ColorModel of the destination. If null, the ColorModel of the source will be used.

Returns:BufferedImage

the zeroed-destination image.

createCompatibleDestRasterback to summary
public WritableRaster createCompatibleDestRaster(Raster src)

Implements java.awt.image.RasterOp.createCompatibleDestRaster.

Creates a zeroed-destination Raster with the correct size and number of bands, given this source.

Parameters
src:Raster

the source Raster

Returns:WritableRaster

the zeroed-destination Raster.

createShortLutback to summary
private ShortLookupTable createShortLut(float[] scale, float[] off, int nBands, int nElems)

Creates a ShortLookupTable to implement the rescale. The table may have either a SHORT or BYTE input.

Parameters
nElems:int

Number of elements the table is to have. This will generally be 256 for byte and 65536 for short.

filterback to summary
public final BufferedImage filter(BufferedImage src, BufferedImage dst)

Implements java.awt.image.BufferedImageOp.filter.

Rescales the source BufferedImage. If the color model in the source image is not the same as that in the destination image, the pixels will be converted in the destination. If the destination image is null, a BufferedImage will be created with the source ColorModel. An IllegalArgumentException may be thrown if the number of scaling factors/offsets in this object does not meet the restrictions stated in the class comments above, or if the source image has an IndexColorModel.

Parameters
src:BufferedImage

the BufferedImage to be filtered

dst:BufferedImage

the destination for the filtering operation or null

Returns:BufferedImage

the filtered BufferedImage.

Exceptions
IllegalArgumentException:
if the ColorModel of src is an IndexColorModel, or if the number of scaling factors and offsets in this RescaleOp do not meet the requirements stated in the class comments, or if the source and destination images differ in size.
filterback to summary
public final WritableRaster filter(Raster src, WritableRaster dst)

Implements java.awt.image.RasterOp.filter.

Rescales the pixel data in the source Raster. If the destination Raster is null, a new Raster will be created. The source and destination must have the same number of bands. Otherwise, an IllegalArgumentException is thrown. Note that the number of scaling factors/offsets in this object must meet the restrictions stated in the class comments above. Otherwise, an IllegalArgumentException is thrown.

Parameters
src:Raster

the Raster to be filtered

dst:WritableRaster

the destination for the filtering operation or null

Returns:WritableRaster

the filtered WritableRaster.

Exceptions
IllegalArgumentException:
if src and dst do not have the same number of bands, or if the number of scaling factors and offsets in this RescaleOp do not meet the requirements stated in the class comments, or if the source and destination rasters differ in size.
filterRasterImplback to summary
private WritableRaster filterRasterImpl(Raster src, WritableRaster dst, int scaleConst, boolean sCheck)
getBounds2Dback to summary
public final Rectangle2D getBounds2D(BufferedImage src)

Implements java.awt.image.BufferedImageOp.getBounds2D.

Returns the bounding box of the rescaled destination image. Since this is not a geometric operation, the bounding box does not change.

getBounds2Dback to summary
public final Rectangle2D getBounds2D(Raster src)

Implements java.awt.image.RasterOp.getBounds2D.

Returns the bounding box of the rescaled destination Raster. Since this is not a geometric operation, the bounding box does not change.

Parameters
src:Raster

the rescaled destination Raster

Returns:Rectangle2D

the bounds of the specified Raster.

getNumFactorsback to summary
public final int getNumFactors()

Returns the number of scaling factors and offsets used in this RescaleOp.

Returns:int

the number of scaling factors and offsets of this RescaleOp.

getOffsetsback to summary
public final float[] getOffsets(float[] offsets)

Returns the offsets in the given array. The array is also returned for convenience. If offsets is null, a new array will be allocated.

Parameters
offsets:float[]

the array to contain the offsets of this RescaleOp

Returns:float[]

the offsets of this RescaleOp.

getPoint2Dback to summary
public final Point2D getPoint2D(Point2D srcPt, Point2D dstPt)

Implements java.awt.image.BufferedImageOp.getPoint2D, java.awt.image.RasterOp.getPoint2D.

Returns the location of the destination point given a point in the source. If dstPt is non-null, it will be used to hold the return value. Since this is not a geometric operation, the srcPt will equal the dstPt.

Parameters
srcPt:Point2D

a point in the source image

dstPt:Point2D

the destination point or null

Returns:Point2D

the location of the destination point.

getRenderingHintsback to summary
public final RenderingHints getRenderingHints()

Implements java.awt.image.BufferedImageOp.getRenderingHints, java.awt.image.RasterOp.getRenderingHints.

Returns the rendering hints for this op.

Returns:RenderingHints

the rendering hints of this RescaleOp.

getScaleFactorsback to summary
public final float[] getScaleFactors(float[] scaleFactors)

Returns the scale factors in the given array. The array is also returned for convenience. If scaleFactors is null, a new array will be allocated.

Parameters
scaleFactors:float[]

the array to contain the scale factors of this RescaleOp

Returns:float[]

the scale factors of this RescaleOp.