📄 affinetransformop.java
字号:
/* * @(#)AffineTransformOp.java 1.60 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.awt.image;import java.awt.geom.AffineTransform;import java.awt.geom.NoninvertibleTransformException;import java.awt.geom.Rectangle2D;import java.awt.geom.Point2D;import java.awt.AlphaComposite;import java.awt.GraphicsEnvironment;import java.awt.Rectangle;import java.awt.RenderingHints;import java.awt.Transparency;import sun.awt.image.ImagingLib;/** * This class uses an affine transform to perform a linear mapping from * 2D coordinates in the source image or <CODE>Raster</CODE> to 2D coordinates * in the destination image or <CODE>Raster</CODE>. * The type of interpolation that is used is specified through a constructor, * either by a <CODE>RenderingHints</CODE> object or by one of the integer * interpolation types defined in this class. * <p> * If a <CODE>RenderingHints</CODE> object is specified in the constructor, the * interpolation hint and the rendering quality hint are used to set * the interpolation type for this operation. The color rendering hint * and the dithering hint can be used when color conversion is required. * <p> * Note that the following constraints have to be met: * <ul> * <li>The source and destination must be different. * <li>For <CODE>Raster</CODE> objects, the number of bands in the source must * be equal to the number of bands in the destination. * </ul> * @see AffineTransform * @see BufferedImageFilter * @see java.awt.RenderingHints#KEY_INTERPOLATION * @see java.awt.RenderingHints#KEY_RENDERING * @see java.awt.RenderingHints#KEY_COLOR_RENDERING * @see java.awt.RenderingHints#KEY_DITHERING * @version 16 Apr 1998 */public class AffineTransformOp implements BufferedImageOp, RasterOp { private AffineTransform xform; RenderingHints hints; /** * Nearest-neighbor interpolation type. */ public static final int TYPE_NEAREST_NEIGHBOR = 1; /** * Bilinear interpolation type. */ public static final int TYPE_BILINEAR = 2; /** * Bicubic interpolation type. */ private static final int TYPE_BICUBIC = 3; int interpolationType = TYPE_NEAREST_NEIGHBOR; /** * Constructs an <CODE>AffineTransformOp</CODE> given an affine transform. * The interpolation type is determined from the * <CODE>RenderingHints</CODE> object. If the interpolation hint is * defined, it will be used. Otherwise, if the rendering quality hint is * defined, the interpolation type is determined from its value. If no * hints are specified (<CODE>hints</CODE> is null), * the interpolation type is {@link #TYPE_NEAREST_NEIGHBOR * TYPE_NEAREST_NEIGHBOR}. * * @param xform The <CODE>AffineTransform</CODE> to use for the * operation. * * @param hints The <CODE>RenderingHints</CODE> object used to specify * the interpolation type for the operation. * * @throws ImagingOpException if the transform is non-invertible. * @see java.awt.RenderingHints#KEY_INTERPOLATION * @see java.awt.RenderingHints#KEY_RENDERING */ public AffineTransformOp(AffineTransform xform, RenderingHints hints){ validateTransform(xform); this.xform = (AffineTransform) xform.clone(); this.hints = hints; if (hints != null) { Object value = hints.get(hints.KEY_INTERPOLATION); if (value == null) { value = hints.get(hints.KEY_RENDERING); if (value == hints.VALUE_RENDER_SPEED) { interpolationType = TYPE_NEAREST_NEIGHBOR; } else if (value == hints.VALUE_RENDER_QUALITY) { interpolationType = TYPE_BILINEAR; } } else if (value == hints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR) { interpolationType = TYPE_NEAREST_NEIGHBOR; } else if (value == hints.VALUE_INTERPOLATION_BILINEAR) { interpolationType = TYPE_BILINEAR; } else if (value == hints.VALUE_INTERPOLATION_BICUBIC) { interpolationType = TYPE_BICUBIC; } } else { interpolationType = TYPE_NEAREST_NEIGHBOR; } } /** * Constructs an <CODE>AffineTransformOp</CODE> given an affine transform * and the interpolation type. * * @param xform The <CODE>AffineTransform</CODE> to use for the operation. * @param interpolationType One of the integer * interpolation type constants defined by this class: * {@link #TYPE_NEAREST_NEIGHBOR TYPE_NEAREST_NEIGHBOR}, * {@link #TYPE_BILINEAR TYPE_BILINEAR}. * @throws ImagingOpException if the transform is non-invertible. */ public AffineTransformOp(AffineTransform xform, int interpolationType) { validateTransform(xform); this.xform = (AffineTransform)xform.clone(); switch(interpolationType) { case TYPE_NEAREST_NEIGHBOR: case TYPE_BILINEAR: case TYPE_BICUBIC: break; default: throw new IllegalArgumentException("Unknown interpolation type: "+ interpolationType); } this.interpolationType = interpolationType; } /** * Returns the interpolation type used by this op. * @return the interpolation type. * @see #TYPE_NEAREST_NEIGHBOR * @see #TYPE_BILINEAR */ public final int getInterpolationType() { return interpolationType; } /** * Transforms the source <CODE>BufferedImage</CODE> and stores the results * in the destination <CODE>BufferedImage</CODE>. * If the color models for the two images do not match, a color * conversion into the destination color model is performed. * If the destination image is null, * a <CODE>BufferedImage</CODE> is created with the source * <CODE>ColorModel</CODE>. * <p> * The coordinates of the rectangle returned by * <code>getBounds2D(BufferedImage)</code> * are not necessarily the same as the coordinates of the * <code>BufferedImage</code> returned by this method. If the * upper-left corner coordinates of the rectangle are * negative then this part of the rectangle is not drawn. If the * upper-left corner coordinates of the rectangle are positive * then the filtered image is drawn at that position in the * destination <code>BufferedImage</code>. * <p> * An <CODE>IllegalArgumentException</CODE> is thrown if the source is * the same as the destination. * * @param src The <CODE>BufferedImage</CODE> to transform. * @param dst The <CODE>BufferedImage</CODE> in which to store the results * of the transformation. * * @return The filtered <CODE>BufferedImage</CODE>. * @throws IllegalArgumentException if <code>src</code> and * <code>dst</code> are the same * @throws ImagingOpException if the image cannot be transformed * because of a data-processing error that might be * caused by an invalid image format, tile format, or * image-processing operation, or any other unsupported * operation. */ public final BufferedImage filter(BufferedImage src, BufferedImage dst) { if (src == null) { throw new NullPointerException("src image is null"); } if (src == dst) { throw new IllegalArgumentException("src image cannot be the "+ "same as the dst image"); } boolean needToConvert = false; ColorModel srcCM = src.getColorModel(); ColorModel dstCM; BufferedImage origDst = dst; if (dst == null) { dst = createCompatibleDestImage(src, null); dstCM = srcCM; origDst = dst; } else { dstCM = dst.getColorModel(); if (srcCM.getColorSpace().getType() != dstCM.getColorSpace().getType()) { int type = xform.getType(); boolean needTrans = ((type& (xform.TYPE_MASK_ROTATION| xform.TYPE_GENERAL_TRANSFORM)) != 0); if (! needTrans && type != xform.TYPE_TRANSLATION && type != xform.TYPE_IDENTITY) { double[] mtx = new double[4]; xform.getMatrix(mtx); // Check out the matrix. A non-integral scale will force ARGB // since the edge conditions can't be guaranteed. needTrans = (mtx[0] != (int)mtx[0] || mtx[3] != (int)mtx[3]); } if (needTrans && srcCM.getTransparency() == Transparency.OPAQUE) { // Need to convert first ColorConvertOp ccop = new ColorConvertOp(hints); BufferedImage tmpSrc = null; int sw = src.getWidth(); int sh = src.getHeight(); if (dstCM.getTransparency() == Transparency.OPAQUE) { tmpSrc = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_ARGB); } else { WritableRaster r = dstCM.createCompatibleWritableRaster(sw, sh); tmpSrc = new BufferedImage(dstCM, r, dstCM.isAlphaPremultiplied(), null); } src = ccop.filter(src, tmpSrc); } else { needToConvert = true; dst = createCompatibleDestImage(src, null); } } } if (interpolationType != TYPE_NEAREST_NEIGHBOR && dst.getColorModel() instanceof IndexColorModel) { dst = new BufferedImage(dst.getWidth(), dst.getHeight(), BufferedImage.TYPE_INT_ARGB); } if (ImagingLib.filter(this, src, dst) == null) { throw new ImagingOpException ("Unable to transform src image"); } if (needToConvert) { ColorConvertOp ccop = new ColorConvertOp(hints); ccop.filter(dst, origDst); } else if (origDst != dst) { java.awt.Graphics2D g = origDst.createGraphics();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -