Class ResampleOp
- java.lang.Object
-
- com.twelvemonkeys.image.ResampleOp
-
- All Implemented Interfaces:
java.awt.image.BufferedImageOp
public class ResampleOp extends java.lang.Object implements java.awt.image.BufferedImageOpResamples (scales) aBufferedImageto 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 theRendereingHints.For fastest results, use
FILTER_POINTorFILTER_BOX. In most cases,FILTER_TRIANGLEwill produce acceptable results, while being relatively fast. For higher quality output, use more sophisticated interpolation algorithms, likeFILTER_MITCHELLorFILTER_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_POINTalgorithm, 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
BufferedImageOpis 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:
ResampleOp(int,int,int),ResampleOp(int,int,java.awt.RenderingHints),BufferedImage,RenderingHints,AffineTransformOp
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classResampleOp.BlackmanBesselFilter(package private) static classResampleOp.BlackmanSincFilter(package private) static classResampleOp.BlacmanFilter(package private) static classResampleOp.BoxFilter(package private) static classResampleOp.CatromFilter(package private) static classResampleOp.Contributor(package private) static classResampleOp.ContributorList(package private) static classResampleOp.CubicFilter(package private) static classResampleOp.GaussianFilter(package private) static classResampleOp.HammingFilter(package private) static classResampleOp.HanningFilter(package private) static classResampleOp.HermiteFilter(package private) static interfaceResampleOp.InterpolationFilter(package private) static classResampleOp.KeyRendereingHints.Key implementation, works only with Value values.(package private) static classResampleOp.LanczosFilter(package private) static classResampleOp.MitchellFilter(package private) static classResampleOp.PointFilter(package private) static classResampleOp.QuadraticFilter(package private) static classResampleOp.TriangleFilter(package private) static classResampleOp.ValueRenderingHints value implementation, works with Key keys.
-
Field Summary
Fields Modifier and Type Field Description private static doubleBprivate static doubleCstatic intFILTER_BLACKMANBlackman interpolation..static intFILTER_BLACKMAN_BESSELBlackman-Bessel interpolation.static intFILTER_BLACKMAN_SINCBlackman-Sinc interpolation.static intFILTER_BOXBox interpolation.static intFILTER_CATROMCatrom interpolation.static intFILTER_CUBICCubic interpolation.static intFILTER_GAUSSIANGaussian interpolation.static intFILTER_HAMMINGHamming interpolation.static intFILTER_HANNINGHanning interpolation.static intFILTER_HERMITEHermite interpolation.static intFILTER_LANCZOSLanczos interpolation.static intFILTER_MITCHELLMitchell interpolation.static intFILTER_POINTPoint interpolation (also known as "nearest neighbour").static intFILTER_QUADRATICQuadratic interpolation.static intFILTER_TRIANGLETriangle interpolation (also known as "linear" or "bilinear").static intFILTER_UNDEFINEDUndefined interpolation, filter method will use default filter.private intfilterTypeprivate intheightstatic java.awt.RenderingHints.KeyKEY_RESAMPLE_INTERPOLATIONRenderingHints.Key specifying resampling interpolation algorithm.private static doubleP0private static doubleP2private static doubleP3private static doubleQ0private static doubleQ1private static doubleQ2private static doubleQ3static java.lang.ObjectVALUE_INTERPOLATION_BLACKMANstatic java.lang.ObjectVALUE_INTERPOLATION_BLACKMAN_BESSELstatic java.lang.ObjectVALUE_INTERPOLATION_BLACKMAN_SINCstatic java.lang.ObjectVALUE_INTERPOLATION_BOXstatic java.lang.ObjectVALUE_INTERPOLATION_CATROMstatic java.lang.ObjectVALUE_INTERPOLATION_CUBICstatic java.lang.ObjectVALUE_INTERPOLATION_GAUSSIANstatic java.lang.ObjectVALUE_INTERPOLATION_HAMMINGstatic java.lang.ObjectVALUE_INTERPOLATION_HANNINGstatic java.lang.ObjectVALUE_INTERPOLATION_HERMITEstatic java.lang.ObjectVALUE_INTERPOLATION_LANCZOSstatic java.lang.ObjectVALUE_INTERPOLATION_MITCHELLstatic java.lang.ObjectVALUE_INTERPOLATION_POINTstatic java.lang.ObjectVALUE_INTERPOLATION_QUADRATICstatic java.lang.ObjectVALUE_INTERPOLATION_TRIANGLEprivate intwidth
-
Constructor Summary
Constructors Constructor Description ResampleOp(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, java.awt.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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private 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)java.awt.image.BufferedImagecreateCompatibleDestImage(java.awt.image.BufferedImage pInput, java.awt.image.ColorModel pModel)private static ResampleOp.InterpolationFiltercreateFilter(int pFilterType)private static java.awt.image.BufferedImagefastResample(java.awt.image.BufferedImage input, java.awt.image.BufferedImage output, int width, int height, int type)java.awt.image.BufferedImagefilter(java.awt.image.BufferedImage input, java.awt.image.BufferedImage output)Re-samples (scales) the image to the size, and using the algorithm specified in the constructor.java.awt.geom.Rectangle2DgetBounds2D(java.awt.image.BufferedImage src)intgetFilterType()Returns the current filter type constant.private static intgetFilterType(java.awt.RenderingHints pHints)Gets the filter type specified by the given hints.java.awt.geom.Point2DgetPoint2D(java.awt.geom.Point2D srcPt, java.awt.geom.Point2D dstPt)java.awt.RenderingHintsgetRenderingHints()private static doublej1(double t)private static doublep1(double t)private static doubleq1(double t)private java.awt.image.BufferedImageresample(java.awt.image.BufferedImage pSource, java.awt.image.BufferedImage pDest, ResampleOp.InterpolationFilter pFilter)(package private) static intround(double d)private static doublesinc(double x)private static intvalidateFilterType(int pFilterType)
-
-
-
Field Detail
-
FILTER_UNDEFINED
public static final int FILTER_UNDEFINED
Undefined interpolation, filter method will use default filter.- See Also:
- Constant Field Values
-
FILTER_POINT
public static final int FILTER_POINT
Point interpolation (also known as "nearest neighbour"). Very fast, but low quality (similar toRenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBORandImage.SCALE_REPLICATE).- See Also:
- Constant Field Values
-
FILTER_BOX
public static final int FILTER_BOX
Box interpolation. Fast, but low quality.- See Also:
- Constant Field Values
-
FILTER_TRIANGLE
public static final int FILTER_TRIANGLE
Triangle interpolation (also known as "linear" or "bilinear"). Quite fast, with acceptable quality (similar toRenderingHints.VALUE_INTERPOLATION_BILINEARandImage.SCALE_AREA_AVERAGING).- See Also:
- Constant Field Values
-
FILTER_HERMITE
public static final int FILTER_HERMITE
Hermite interpolation.- See Also:
- Constant Field Values
-
FILTER_HANNING
public static final int FILTER_HANNING
Hanning interpolation.- See Also:
- Constant Field Values
-
FILTER_HAMMING
public static final int FILTER_HAMMING
Hamming interpolation.- See Also:
- Constant Field Values
-
FILTER_BLACKMAN
public static final int FILTER_BLACKMAN
Blackman interpolation..- See Also:
- Constant Field Values
-
FILTER_GAUSSIAN
public static final int FILTER_GAUSSIAN
Gaussian interpolation.- See Also:
- Constant Field Values
-
FILTER_QUADRATIC
public static final int FILTER_QUADRATIC
Quadratic interpolation.- See Also:
- Constant Field Values
-
FILTER_CUBIC
public static final int FILTER_CUBIC
Cubic interpolation.- See Also:
- Constant Field Values
-
FILTER_CATROM
public static final int FILTER_CATROM
Catrom interpolation.- See Also:
- Constant Field Values
-
FILTER_MITCHELL
public static final int FILTER_MITCHELL
Mitchell interpolation. High quality.- See Also:
- Constant Field Values
-
FILTER_LANCZOS
public static final int FILTER_LANCZOS
Lanczos interpolation. High quality.- See Also:
- Constant Field Values
-
FILTER_BLACKMAN_BESSEL
public static final int FILTER_BLACKMAN_BESSEL
Blackman-Bessel interpolation. High quality.- See Also:
- Constant Field Values
-
FILTER_BLACKMAN_SINC
public static final int FILTER_BLACKMAN_SINC
Blackman-Sinc interpolation. High quality.- See Also:
- Constant Field Values
-
KEY_RESAMPLE_INTERPOLATION
public static final java.awt.RenderingHints.Key KEY_RESAMPLE_INTERPOLATION
RenderingHints.Key specifying resampling interpolation algorithm.
-
VALUE_INTERPOLATION_POINT
public static final java.lang.Object VALUE_INTERPOLATION_POINT
- See Also:
FILTER_POINT
-
VALUE_INTERPOLATION_BOX
public static final java.lang.Object VALUE_INTERPOLATION_BOX
- See Also:
FILTER_BOX
-
VALUE_INTERPOLATION_TRIANGLE
public static final java.lang.Object VALUE_INTERPOLATION_TRIANGLE
- See Also:
FILTER_TRIANGLE
-
VALUE_INTERPOLATION_HERMITE
public static final java.lang.Object VALUE_INTERPOLATION_HERMITE
- See Also:
FILTER_HERMITE
-
VALUE_INTERPOLATION_HANNING
public static final java.lang.Object VALUE_INTERPOLATION_HANNING
- See Also:
FILTER_HANNING
-
VALUE_INTERPOLATION_HAMMING
public static final java.lang.Object VALUE_INTERPOLATION_HAMMING
- See Also:
FILTER_HAMMING
-
VALUE_INTERPOLATION_BLACKMAN
public static final java.lang.Object VALUE_INTERPOLATION_BLACKMAN
- See Also:
FILTER_BLACKMAN
-
VALUE_INTERPOLATION_GAUSSIAN
public static final java.lang.Object VALUE_INTERPOLATION_GAUSSIAN
- See Also:
FILTER_GAUSSIAN
-
VALUE_INTERPOLATION_QUADRATIC
public static final java.lang.Object VALUE_INTERPOLATION_QUADRATIC
- See Also:
FILTER_QUADRATIC
-
VALUE_INTERPOLATION_CUBIC
public static final java.lang.Object VALUE_INTERPOLATION_CUBIC
- See Also:
FILTER_CUBIC
-
VALUE_INTERPOLATION_CATROM
public static final java.lang.Object VALUE_INTERPOLATION_CATROM
- See Also:
FILTER_CATROM
-
VALUE_INTERPOLATION_MITCHELL
public static final java.lang.Object VALUE_INTERPOLATION_MITCHELL
- See Also:
FILTER_MITCHELL
-
VALUE_INTERPOLATION_LANCZOS
public static final java.lang.Object VALUE_INTERPOLATION_LANCZOS
- See Also:
FILTER_LANCZOS
-
VALUE_INTERPOLATION_BLACKMAN_BESSEL
public static final java.lang.Object VALUE_INTERPOLATION_BLACKMAN_BESSEL
- See Also:
FILTER_BLACKMAN_BESSEL
-
VALUE_INTERPOLATION_BLACKMAN_SINC
public static final java.lang.Object VALUE_INTERPOLATION_BLACKMAN_SINC
- See Also:
FILTER_BLACKMAN_SINC
-
width
private final int width
-
height
private final int height
-
filterType
private final int filterType
-
B
private static final double B
- See Also:
- Constant Field Values
-
C
private static final double C
- See Also:
- Constant Field Values
-
P0
private static final double P0
- See Also:
- Constant Field Values
-
P2
private static final double P2
- See Also:
- Constant Field Values
-
P3
private static final double P3
- See Also:
- Constant Field Values
-
Q0
private static final double Q0
- See Also:
- Constant Field Values
-
Q1
private static final double Q1
- See Also:
- Constant Field Values
-
Q2
private static final double Q2
- See Also:
- Constant Field Values
-
Q3
private static final double Q3
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
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
public ResampleOp(int width, int height, java.awt.RenderingHints hints)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:
KEY_RESAMPLE_INTERPOLATION,RenderingHints.KEY_INTERPOLATION,RenderingHints.KEY_RENDERING,RenderingHints.KEY_COLOR_RENDERING
-
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:
- filter type constants
-
-
Method Detail
-
validateFilterType
private static int validateFilterType(int pFilterType)
-
getFilterType
private static int getFilterType(java.awt.RenderingHints pHints)
Gets the filter type specified by the given hints.- Parameters:
pHints- rendering hints- Returns:
- a filter type constant
-
filter
public final java.awt.image.BufferedImage filter(java.awt.image.BufferedImage input, java.awt.image.BufferedImage output)Re-samples (scales) the image to the size, and using the algorithm specified in the constructor.- Specified by:
filterin interfacejava.awt.image.BufferedImageOp- Parameters:
input- TheBufferedImageto be filteredoutput- TheBufferedImagein which to store the resampled image- Returns:
- The re-sampled
BufferedImage. - Throws:
java.lang.NullPointerException- ifinputisnulljava.lang.IllegalArgumentException- ifinput == output.- See Also:
ResampleOp(int,int,int)
-
fastResample
private static java.awt.image.BufferedImage fastResample(java.awt.image.BufferedImage input, java.awt.image.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:
- filter type constants
-
createFilter
private static ResampleOp.InterpolationFilter createFilter(int pFilterType)
-
createCompatibleDestImage
public final java.awt.image.BufferedImage createCompatibleDestImage(java.awt.image.BufferedImage pInput, java.awt.image.ColorModel pModel)- Specified by:
createCompatibleDestImagein interfacejava.awt.image.BufferedImageOp
-
getRenderingHints
public java.awt.RenderingHints getRenderingHints()
- Specified by:
getRenderingHintsin interfacejava.awt.image.BufferedImageOp
-
getBounds2D
public java.awt.geom.Rectangle2D getBounds2D(java.awt.image.BufferedImage src)
- Specified by:
getBounds2Din interfacejava.awt.image.BufferedImageOp
-
getPoint2D
public java.awt.geom.Point2D getPoint2D(java.awt.geom.Point2D srcPt, java.awt.geom.Point2D dstPt)- Specified by:
getPoint2Din interfacejava.awt.image.BufferedImageOp
-
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 java.awt.image.BufferedImage resample(java.awt.image.BufferedImage pSource, java.awt.image.BufferedImage pDest, ResampleOp.InterpolationFilter pFilter)
-
-