Class ResampleOp
- All Implemented Interfaces:
BufferedImageOp
BufferedImage
to a new width and height, using
high performance and high quality algorithms.
Several different interpolation algorithms may be specifed in the
constructor, either using the
filter type constants, or one of the
RendereingHints
.
For fastest results, use FILTER_POINT
or FILTER_BOX
.
In most cases, FILTER_TRIANGLE
will produce acceptable results, while
being relatively fast.
For higher quality output, use more sophisticated interpolation algorithms,
like FILTER_MITCHELL
or FILTER_LANCZOS
.
Example:
BufferedImage image; //... ResampleOp resampler = new ResampleOp(100, 100, ResampleOp.FILTER_TRIANGLE); BufferedImage thumbnail = resampler.filter(image, null);
If your input image is very large, it's possible to first resample using the
very fast FILTER_POINT
algorithm, then resample to the wanted size,
using a higher quality algorithm:
BufferedImage verylLarge; //... int w = 300; int h = 200; BufferedImage temp = new ResampleOp(w * 2, h * 2, FILTER_POINT).filter(verylLarge, null); BufferedImage scaled = new ResampleOp(w, h).filter(temp, null);
For maximum performance, this class will use native code, through
JMagick, when available.
Otherwise, the class will silently fall back to pure Java mode.
Native code may be disabled globally, by setting the system property
com.twelvemonkeys.image.accel
to false
.
To allow debug of the native code, set the system property
com.twelvemonkeys.image.magick.debug
to true
.
This BufferedImageOp
is based on C example code found in
Graphics Gems III,
Filtered Image Rescaling, by Dale Schumacher (with additional improvments by
Ray Gardener).
Additional changes are inspired by
ImageMagick and
Marco Schmidt's Java Imaging Utilities
(which are also adaptions of the same original code from Graphics Gems III).
For a description of the various interpolation algorithms, see General Filtered Image Rescaling in Graphics Gems III, Academic Press, 1994.
- Version:
- $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/image/ResampleOp.java#1 $
- Author:
- Harald Kuhr, last modified by $Author: haku $
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Blackman interpolation..static final int
Blackman-Bessel interpolation.static final int
Blackman-Sinc interpolation.static final int
Box interpolation.static final int
Catrom interpolation.static final int
Cubic interpolation.static final int
Gaussian interpolation.static final int
Hamming interpolation.static final int
Hanning interpolation.static final int
Hermite interpolation.static final int
Lanczos interpolation.static final int
Mitchell interpolation.static final int
Point interpolation (also known as "nearest neighbour").static final int
Quadratic interpolation.static final int
Triangle interpolation (also known as "linear" or "bilinear").static final int
Undefined interpolation, filter method will use default filter.static final RenderingHints.Key
RenderingHints.Key specifying resampling interpolation algorithm.static final Object
static final Object
static final Object
static final Object
static final Object
static final Object
static final Object
static final Object
static final Object
static final Object
static final Object
static final Object
static final Object
static final Object
static final Object
-
Constructor Summary
ConstructorsConstructorDescriptionResampleOp
(int width, int height) Creates aResampleOp
that will resample input images to the given width and height, using the default interpolation filter.ResampleOp
(int width, int height, int filterType) Creates aResampleOp
that will resample input images to the given width and height, using the given interpolation filter.ResampleOp
(int width, int height, RenderingHints hints) Creates aResampleOp
that will resample input images to the given width and height, using the interpolation filter specified by the given hints. -
Method Summary
Modifier and TypeMethodDescriptionfinal BufferedImage
createCompatibleDestImage
(BufferedImage pInput, ColorModel pModel) final BufferedImage
filter
(BufferedImage input, BufferedImage output) Re-samples (scales) the image to the size, and using the algorithm specified in the constructor.getBounds2D
(BufferedImage src) int
Returns the current filter type constant.getPoint2D
(Point2D srcPt, Point2D dstPt)
-
Field Details
-
FILTER_UNDEFINED
public static final int FILTER_UNDEFINEDUndefined interpolation, filter method will use default filter.- See Also:
-
FILTER_POINT
public static final int FILTER_POINTPoint interpolation (also known as "nearest neighbour"). Very fast, but low quality (similar toRenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR
andImage.SCALE_REPLICATE
).- See Also:
-
FILTER_BOX
public static final int FILTER_BOXBox interpolation. Fast, but low quality.- See Also:
-
FILTER_TRIANGLE
public static final int FILTER_TRIANGLETriangle interpolation (also known as "linear" or "bilinear"). Quite fast, with acceptable quality (similar toRenderingHints.VALUE_INTERPOLATION_BILINEAR
andImage.SCALE_AREA_AVERAGING
).- See Also:
-
FILTER_HERMITE
public static final int FILTER_HERMITEHermite interpolation.- See Also:
-
FILTER_HANNING
public static final int FILTER_HANNINGHanning interpolation.- See Also:
-
FILTER_HAMMING
public static final int FILTER_HAMMINGHamming interpolation.- See Also:
-
FILTER_BLACKMAN
public static final int FILTER_BLACKMANBlackman interpolation..- See Also:
-
FILTER_GAUSSIAN
public static final int FILTER_GAUSSIANGaussian interpolation.- See Also:
-
FILTER_QUADRATIC
public static final int FILTER_QUADRATICQuadratic interpolation.- See Also:
-
FILTER_CUBIC
public static final int FILTER_CUBICCubic interpolation.- See Also:
-
FILTER_CATROM
public static final int FILTER_CATROMCatrom interpolation.- See Also:
-
FILTER_MITCHELL
public static final int FILTER_MITCHELLMitchell interpolation. High quality.- See Also:
-
FILTER_LANCZOS
public static final int FILTER_LANCZOSLanczos interpolation. High quality.- See Also:
-
FILTER_BLACKMAN_BESSEL
public static final int FILTER_BLACKMAN_BESSELBlackman-Bessel interpolation. High quality.- See Also:
-
FILTER_BLACKMAN_SINC
public static final int FILTER_BLACKMAN_SINCBlackman-Sinc interpolation. High quality.- See Also:
-
KEY_RESAMPLE_INTERPOLATION
RenderingHints.Key specifying resampling interpolation algorithm. -
VALUE_INTERPOLATION_POINT
- See Also:
-
VALUE_INTERPOLATION_BOX
- See Also:
-
VALUE_INTERPOLATION_TRIANGLE
- See Also:
-
VALUE_INTERPOLATION_HERMITE
- See Also:
-
VALUE_INTERPOLATION_HANNING
- See Also:
-
VALUE_INTERPOLATION_HAMMING
- See Also:
-
VALUE_INTERPOLATION_BLACKMAN
- See Also:
-
VALUE_INTERPOLATION_GAUSSIAN
- See Also:
-
VALUE_INTERPOLATION_QUADRATIC
- See Also:
-
VALUE_INTERPOLATION_CUBIC
- See Also:
-
VALUE_INTERPOLATION_CATROM
- See Also:
-
VALUE_INTERPOLATION_MITCHELL
- See Also:
-
VALUE_INTERPOLATION_LANCZOS
- See Also:
-
VALUE_INTERPOLATION_BLACKMAN_BESSEL
- See Also:
-
VALUE_INTERPOLATION_BLACKMAN_SINC
- See Also:
-
-
Constructor Details
-
ResampleOp
public ResampleOp(int width, int height) Creates aResampleOp
that will resample input images to the given width and height, using the default interpolation filter.- Parameters:
width
- width of the re-sampled imageheight
- height of the re-sampled image
-
ResampleOp
Creates aResampleOp
that will resample input images to the given width and height, using the interpolation filter specified by the given hints.If using
RenderingHints
, the hints are mapped as follows:KEY_RESAMPLE_INTERPOLATION
takes precedence over any standardjava.awt
hints, and dictates interpolation directly, seeRenderingHints
constants.KEY_INTERPOLATION
takes precedence over other hints.RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR
specifiesFILTER_POINT
RenderingHints.VALUE_INTERPOLATION_BILINEAR
specifiesFILTER_TRIANGLE
RenderingHints.VALUE_INTERPOLATION_BICUBIC
specifiesFILTER_QUADRATIC
KEY_RENDERING
orKEY_COLOR_RENDERING
RenderingHints.VALUE_RENDER_SPEED
specifiesFILTER_POINT
RenderingHints.VALUE_RENDER_QUALITY
specifiesFILTER_MITCHELL
Other hints have no effect on this filter.
- Parameters:
width
- width of the re-sampled imageheight
- height of the re-sampled imagehints
- rendering hints, affecting interpolation algorithm- See Also:
-
ResampleOp
public ResampleOp(int width, int height, int filterType) Creates aResampleOp
that will resample input images to the given width and height, using the given interpolation filter.- Parameters:
width
- width of the re-sampled imageheight
- height of the re-sampled imagefilterType
- interpolation filter algorithm- See Also:
-
-
Method Details
-
filter
Re-samples (scales) the image to the size, and using the algorithm specified in the constructor.- Specified by:
filter
in interfaceBufferedImageOp
- Parameters:
input
- TheBufferedImage
to be filteredoutput
- TheBufferedImage
in which to store the resampled image- Returns:
- The re-sampled
BufferedImage
. - Throws:
NullPointerException
- ifinput
isnull
IllegalArgumentException
- ifinput == output
.- See Also:
-
getFilterType
public int getFilterType()Returns the current filter type constant.- Returns:
- the current filter type constant.
- See Also:
-
createCompatibleDestImage
- Specified by:
createCompatibleDestImage
in interfaceBufferedImageOp
-
getRenderingHints
- Specified by:
getRenderingHints
in interfaceBufferedImageOp
-
getBounds2D
- Specified by:
getBounds2D
in interfaceBufferedImageOp
-
getPoint2D
- Specified by:
getPoint2D
in interfaceBufferedImageOp
-