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 class
RendereingHints.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 class
RenderingHints value implementation, works with Key keys. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final double
private static final double
static 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.private final int
private final int
static final RenderingHints.Key
RenderingHints.Key specifying resampling interpolation algorithm.private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
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
private final int
-
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 TypeMethodDescriptionprivate static double
bessel
(double t) (package private) static double
besselOrderOne
(double t) private static double
blackman
(double t) private ResampleOp.ContributorList
calcXContrib
(double xscale, double fwidth, int srcwidth, ResampleOp.InterpolationFilter pFilter, int i) final BufferedImage
createCompatibleDestImage
(BufferedImage pInput, ColorModel pModel) private static ResampleOp.InterpolationFilter
createFilter
(int pFilterType) private static BufferedImage
fastResample
(BufferedImage input, BufferedImage output, int width, int height, int type) 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.private static int
getFilterType
(RenderingHints pHints) Gets the filter type specified by the given hints.getPoint2D
(Point2D srcPt, Point2D dstPt) private static double
j1
(double t) private static double
p1
(double t) private static double
q1
(double t) private BufferedImage
resample
(BufferedImage pSource, BufferedImage pDest, ResampleOp.InterpolationFilter pFilter) (package private) static int
round
(double d) private static double
sinc
(double x) private static int
validateFilterType
(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_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:
-
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 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
-
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:
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:
-
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:
createCompatibleDestImage
in interfaceBufferedImageOp
-
getRenderingHints
- Specified by:
getRenderingHints
in interfaceBufferedImageOp
-
getBounds2D
- Specified by:
getBounds2D
in interfaceBufferedImageOp
-
getPoint2D
- Specified by:
getPoint2D
in 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)
-