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 intBlackman interpolation..static final intBlackman-Bessel interpolation.static final intBlackman-Sinc interpolation.static final intBox interpolation.static final intCatrom interpolation.static final intCubic interpolation.static final intGaussian interpolation.static final intHamming interpolation.static final intHanning interpolation.static final intHermite interpolation.static final intLanczos interpolation.static final intMitchell interpolation.static final intPoint interpolation (also known as "nearest neighbour").static final intQuadratic interpolation.static final intTriangle interpolation (also known as "linear" or "bilinear").static final intUndefined interpolation, filter method will use default filter.static final RenderingHints.KeyRenderingHints.Key specifying resampling interpolation algorithm.static final Objectstatic final Objectstatic final Objectstatic final Objectstatic final Objectstatic final Objectstatic final Objectstatic final Objectstatic final Objectstatic final Objectstatic final Objectstatic final Objectstatic final Objectstatic final Objectstatic final Object -
Constructor Summary
ConstructorsConstructorDescriptionResampleOp(int width, int height) Creates aResampleOpthat will resample input images to the given width and height, using the default interpolation filter.ResampleOp(int width, int height, int filterType) Creates aResampleOpthat will resample input images to the given width and height, using the given interpolation filter.ResampleOp(int width, int height, RenderingHints hints) Creates aResampleOpthat will resample input images to the given width and height, using the interpolation filter specified by the given hints. -
Method Summary
Modifier and TypeMethodDescriptionfinal BufferedImagecreateCompatibleDestImage(BufferedImage pInput, ColorModel pModel) final BufferedImagefilter(BufferedImage input, BufferedImage output) Re-samples (scales) the image to the size, and using the algorithm specified in the constructor.getBounds2D(BufferedImage src) intReturns 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_NEIGHBORandImage.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_BILINEARandImage.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 aResampleOpthat 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 aResampleOpthat 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_INTERPOLATIONtakes precedence over any standardjava.awthints, and dictates interpolation directly, seeRenderingHintsconstants.KEY_INTERPOLATIONtakes precedence over other hints.RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBORspecifiesFILTER_POINTRenderingHints.VALUE_INTERPOLATION_BILINEARspecifiesFILTER_TRIANGLERenderingHints.VALUE_INTERPOLATION_BICUBICspecifiesFILTER_QUADRATIC
KEY_RENDERINGorKEY_COLOR_RENDERINGRenderingHints.VALUE_RENDER_SPEEDspecifiesFILTER_POINTRenderingHints.VALUE_RENDER_QUALITYspecifiesFILTER_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 aResampleOpthat 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:
filterin interfaceBufferedImageOp- Parameters:
input- TheBufferedImageto be filteredoutput- TheBufferedImagein which to store the resampled image- Returns:
- The re-sampled
BufferedImage. - Throws:
NullPointerException- ifinputisnullIllegalArgumentException- 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:
createCompatibleDestImagein interfaceBufferedImageOp
-
getRenderingHints
- Specified by:
getRenderingHintsin interfaceBufferedImageOp
-
getBounds2D
- Specified by:
getBounds2Din interfaceBufferedImageOp
-
getPoint2D
- Specified by:
getPoint2Din interfaceBufferedImageOp
-