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);
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 $
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static class(package private) static class(package private) static class(package private) static class(package private) static class(package private) static class(package private) static class(package private) static class(package private) static class(package private) static class(package private) static class(package private) static class(package private) static interface(package private) static classRendereingHints.Key implementation, works only with Value values.(package private) static class(package private) static class(package private) static class(package private) static class(package private) static class(package private) static final classRenderingHints value implementation, works with Key keys. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final doubleprivate static final doublestatic 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.private final intprivate final intstatic final RenderingHints.KeyRenderingHints.Key specifying resampling interpolation algorithm.private static final doubleprivate static final doubleprivate static final doubleprivate static final doubleprivate static final doubleprivate static final doubleprivate static final doublestatic 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 Objectprivate final int -
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 TypeMethodDescriptionprivate static doublebessel(double t) (package private) static doublebesselOrderOne(double t) private static doubleblackman(double t) private ResampleOp.ContributorListcalcXContrib(double xscale, double fwidth, int srcwidth, ResampleOp.InterpolationFilter pFilter, int i) final BufferedImagecreateCompatibleDestImage(BufferedImage pInput, ColorModel pModel) private static ResampleOp.InterpolationFiltercreateFilter(int pFilterType) private static BufferedImagefastResample(BufferedImage input, BufferedImage output, int width, int height, int type) 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.private static intgetFilterType(RenderingHints pHints) Gets the filter type specified by the given hints.getPoint2D(Point2D srcPt, Point2D dstPt) private static doublej1(double t) private static doublep1(double t) private static doubleq1(double t) private BufferedImageresample(BufferedImage pSource, BufferedImage pDest, ResampleOp.InterpolationFilter pFilter) (package private) static intround(double d) private static doublesinc(double x) private static intvalidateFilterType(int pFilterType)
-
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:
-
width
private final int width -
height
private final int height -
filterType
private final int filterType -
B
private static final double B- See Also:
-
C
private static final double C- See Also:
-
P0
private static final double P0- See Also:
-
P2
private static final double P2- See Also:
-
P3
private static final double P3- See Also:
-
Q0
private static final double Q0- See Also:
-
Q1
private static final double Q1- See Also:
-
Q2
private static final double Q2- See Also:
-
Q3
private static final double Q3- 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
-
validateFilterType
private static int validateFilterType(int pFilterType) -
getFilterType
Gets the filter type specified by the given hints.- Parameters:
pHints- rendering hints- Returns:
- a filter type constant
-
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:
-
fastResample
private static BufferedImage fastResample(BufferedImage input, BufferedImage output, int width, int height, int type) -
getFilterType
public int getFilterType()Returns the current filter type constant.- Returns:
- the current filter type constant.
- See Also:
-
createFilter
-
createCompatibleDestImage
- Specified by:
createCompatibleDestImagein interfaceBufferedImageOp
-
getRenderingHints
- Specified by:
getRenderingHintsin interfaceBufferedImageOp
-
getBounds2D
- Specified by:
getBounds2Din interfaceBufferedImageOp
-
getPoint2D
- Specified by:
getPoint2Din interfaceBufferedImageOp
-
sinc
private static double sinc(double x) -
j1
private static double j1(double t) -
p1
private static double p1(double t) -
q1
private static double q1(double t) -
besselOrderOne
static double besselOrderOne(double t) -
bessel
private static double bessel(double t) -
blackman
private static double blackman(double t) -
round
static int round(double d) -
calcXContrib
private ResampleOp.ContributorList calcXContrib(double xscale, double fwidth, int srcwidth, ResampleOp.InterpolationFilter pFilter, int i) -
resample
private BufferedImage resample(BufferedImage pSource, BufferedImage pDest, ResampleOp.InterpolationFilter pFilter)
-