0N/A/*
2362N/A * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt.image;
0N/A
0N/Aimport java.awt.geom.AffineTransform;
0N/Aimport java.awt.geom.NoninvertibleTransformException;
0N/Aimport java.awt.geom.Rectangle2D;
0N/Aimport java.awt.geom.Point2D;
0N/Aimport java.awt.AlphaComposite;
0N/Aimport java.awt.GraphicsEnvironment;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.awt.RenderingHints;
0N/Aimport java.awt.Transparency;
0N/Aimport sun.awt.image.ImagingLib;
0N/A
0N/A/**
0N/A * This class uses an affine transform to perform a linear mapping from
0N/A * 2D coordinates in the source image or <CODE>Raster</CODE> to 2D coordinates
0N/A * in the destination image or <CODE>Raster</CODE>.
0N/A * The type of interpolation that is used is specified through a constructor,
0N/A * either by a <CODE>RenderingHints</CODE> object or by one of the integer
0N/A * interpolation types defined in this class.
0N/A * <p>
0N/A * If a <CODE>RenderingHints</CODE> object is specified in the constructor, the
0N/A * interpolation hint and the rendering quality hint are used to set
0N/A * the interpolation type for this operation. The color rendering hint
0N/A * and the dithering hint can be used when color conversion is required.
0N/A * <p>
0N/A * Note that the following constraints have to be met:
0N/A * <ul>
0N/A * <li>The source and destination must be different.
0N/A * <li>For <CODE>Raster</CODE> objects, the number of bands in the source must
0N/A * be equal to the number of bands in the destination.
0N/A * </ul>
0N/A * @see AffineTransform
0N/A * @see BufferedImageFilter
0N/A * @see java.awt.RenderingHints#KEY_INTERPOLATION
0N/A * @see java.awt.RenderingHints#KEY_RENDERING
0N/A * @see java.awt.RenderingHints#KEY_COLOR_RENDERING
0N/A * @see java.awt.RenderingHints#KEY_DITHERING
0N/A */
0N/Apublic class AffineTransformOp implements BufferedImageOp, RasterOp {
0N/A private AffineTransform xform;
0N/A RenderingHints hints;
0N/A
0N/A /**
0N/A * Nearest-neighbor interpolation type.
0N/A */
0N/A public static final int TYPE_NEAREST_NEIGHBOR = 1;
0N/A
0N/A /**
0N/A * Bilinear interpolation type.
0N/A */
0N/A public static final int TYPE_BILINEAR = 2;
0N/A
0N/A /**
0N/A * Bicubic interpolation type.
0N/A */
0N/A public static final int TYPE_BICUBIC = 3;
0N/A
0N/A int interpolationType = TYPE_NEAREST_NEIGHBOR;
0N/A
0N/A /**
0N/A * Constructs an <CODE>AffineTransformOp</CODE> given an affine transform.
0N/A * The interpolation type is determined from the
0N/A * <CODE>RenderingHints</CODE> object. If the interpolation hint is
0N/A * defined, it will be used. Otherwise, if the rendering quality hint is
0N/A * defined, the interpolation type is determined from its value. If no
0N/A * hints are specified (<CODE>hints</CODE> is null),
0N/A * the interpolation type is {@link #TYPE_NEAREST_NEIGHBOR
0N/A * TYPE_NEAREST_NEIGHBOR}.
0N/A *
0N/A * @param xform The <CODE>AffineTransform</CODE> to use for the
0N/A * operation.
0N/A *
0N/A * @param hints The <CODE>RenderingHints</CODE> object used to specify
0N/A * the interpolation type for the operation.
0N/A *
0N/A * @throws ImagingOpException if the transform is non-invertible.
0N/A * @see java.awt.RenderingHints#KEY_INTERPOLATION
0N/A * @see java.awt.RenderingHints#KEY_RENDERING
0N/A */
0N/A public AffineTransformOp(AffineTransform xform, RenderingHints hints){
0N/A validateTransform(xform);
0N/A this.xform = (AffineTransform) xform.clone();
0N/A this.hints = hints;
0N/A
0N/A if (hints != null) {
0N/A Object value = hints.get(hints.KEY_INTERPOLATION);
0N/A if (value == null) {
0N/A value = hints.get(hints.KEY_RENDERING);
0N/A if (value == hints.VALUE_RENDER_SPEED) {
0N/A interpolationType = TYPE_NEAREST_NEIGHBOR;
0N/A }
0N/A else if (value == hints.VALUE_RENDER_QUALITY) {
0N/A interpolationType = TYPE_BILINEAR;
0N/A }
0N/A }
0N/A else if (value == hints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR) {
0N/A interpolationType = TYPE_NEAREST_NEIGHBOR;
0N/A }
0N/A else if (value == hints.VALUE_INTERPOLATION_BILINEAR) {
0N/A interpolationType = TYPE_BILINEAR;
0N/A }
0N/A else if (value == hints.VALUE_INTERPOLATION_BICUBIC) {
0N/A interpolationType = TYPE_BICUBIC;
0N/A }
0N/A }
0N/A else {
0N/A interpolationType = TYPE_NEAREST_NEIGHBOR;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Constructs an <CODE>AffineTransformOp</CODE> given an affine transform
0N/A * and the interpolation type.
0N/A *
0N/A * @param xform The <CODE>AffineTransform</CODE> to use for the operation.
0N/A * @param interpolationType One of the integer
0N/A * interpolation type constants defined by this class:
0N/A * {@link #TYPE_NEAREST_NEIGHBOR TYPE_NEAREST_NEIGHBOR},
0N/A * {@link #TYPE_BILINEAR TYPE_BILINEAR},
0N/A * {@link #TYPE_BICUBIC TYPE_BICUBIC}.
0N/A * @throws ImagingOpException if the transform is non-invertible.
0N/A */
0N/A public AffineTransformOp(AffineTransform xform, int interpolationType) {
0N/A validateTransform(xform);
0N/A this.xform = (AffineTransform)xform.clone();
0N/A switch(interpolationType) {
0N/A case TYPE_NEAREST_NEIGHBOR:
0N/A case TYPE_BILINEAR:
0N/A case TYPE_BICUBIC:
0N/A break;
0N/A default:
0N/A throw new IllegalArgumentException("Unknown interpolation type: "+
0N/A interpolationType);
0N/A }
0N/A this.interpolationType = interpolationType;
0N/A }
0N/A
0N/A /**
0N/A * Returns the interpolation type used by this op.
0N/A * @return the interpolation type.
0N/A * @see #TYPE_NEAREST_NEIGHBOR
0N/A * @see #TYPE_BILINEAR
0N/A * @see #TYPE_BICUBIC
0N/A */
0N/A public final int getInterpolationType() {
0N/A return interpolationType;
0N/A }
0N/A
0N/A /**
0N/A * Transforms the source <CODE>BufferedImage</CODE> and stores the results
0N/A * in the destination <CODE>BufferedImage</CODE>.
0N/A * If the color models for the two images do not match, a color
0N/A * conversion into the destination color model is performed.
0N/A * If the destination image is null,
0N/A * a <CODE>BufferedImage</CODE> is created with the source
0N/A * <CODE>ColorModel</CODE>.
0N/A * <p>
0N/A * The coordinates of the rectangle returned by
0N/A * <code>getBounds2D(BufferedImage)</code>
0N/A * are not necessarily the same as the coordinates of the
0N/A * <code>BufferedImage</code> returned by this method. If the
0N/A * upper-left corner coordinates of the rectangle are
0N/A * negative then this part of the rectangle is not drawn. If the
0N/A * upper-left corner coordinates of the rectangle are positive
0N/A * then the filtered image is drawn at that position in the
0N/A * destination <code>BufferedImage</code>.
0N/A * <p>
0N/A * An <CODE>IllegalArgumentException</CODE> is thrown if the source is
0N/A * the same as the destination.
0N/A *
0N/A * @param src The <CODE>BufferedImage</CODE> to transform.
0N/A * @param dst The <CODE>BufferedImage</CODE> in which to store the results
0N/A * of the transformation.
0N/A *
0N/A * @return The filtered <CODE>BufferedImage</CODE>.
0N/A * @throws IllegalArgumentException if <code>src</code> and
0N/A * <code>dst</code> are the same
0N/A * @throws ImagingOpException if the image cannot be transformed
0N/A * because of a data-processing error that might be
0N/A * caused by an invalid image format, tile format, or
0N/A * image-processing operation, or any other unsupported
0N/A * operation.
0N/A */
0N/A public final BufferedImage filter(BufferedImage src, BufferedImage dst) {
0N/A
0N/A if (src == null) {
0N/A throw new NullPointerException("src image is null");
0N/A }
0N/A if (src == dst) {
0N/A throw new IllegalArgumentException("src image cannot be the "+
0N/A "same as the dst image");
0N/A }
0N/A
0N/A boolean needToConvert = false;
0N/A ColorModel srcCM = src.getColorModel();
0N/A ColorModel dstCM;
0N/A BufferedImage origDst = dst;
0N/A
0N/A if (dst == null) {
0N/A dst = createCompatibleDestImage(src, null);
0N/A dstCM = srcCM;
0N/A origDst = dst;
0N/A }
0N/A else {
0N/A dstCM = dst.getColorModel();
0N/A if (srcCM.getColorSpace().getType() !=
0N/A dstCM.getColorSpace().getType())
0N/A {
0N/A int type = xform.getType();
0N/A boolean needTrans = ((type&
0N/A (xform.TYPE_MASK_ROTATION|
0N/A xform.TYPE_GENERAL_TRANSFORM))
0N/A != 0);
0N/A if (! needTrans && type != xform.TYPE_TRANSLATION && type != xform.TYPE_IDENTITY)
0N/A {
0N/A double[] mtx = new double[4];
0N/A xform.getMatrix(mtx);
0N/A // Check out the matrix. A non-integral scale will force ARGB
0N/A // since the edge conditions can't be guaranteed.
0N/A needTrans = (mtx[0] != (int)mtx[0] || mtx[3] != (int)mtx[3]);
0N/A }
0N/A
0N/A if (needTrans &&
0N/A srcCM.getTransparency() == Transparency.OPAQUE)
0N/A {
0N/A // Need to convert first
0N/A ColorConvertOp ccop = new ColorConvertOp(hints);
0N/A BufferedImage tmpSrc = null;
0N/A int sw = src.getWidth();
0N/A int sh = src.getHeight();
0N/A if (dstCM.getTransparency() == Transparency.OPAQUE) {
0N/A tmpSrc = new BufferedImage(sw, sh,
0N/A BufferedImage.TYPE_INT_ARGB);
0N/A }
0N/A else {
0N/A WritableRaster r =
0N/A dstCM.createCompatibleWritableRaster(sw, sh);
0N/A tmpSrc = new BufferedImage(dstCM, r,
0N/A dstCM.isAlphaPremultiplied(),
0N/A null);
0N/A }
0N/A src = ccop.filter(src, tmpSrc);
0N/A }
0N/A else {
0N/A needToConvert = true;
0N/A dst = createCompatibleDestImage(src, null);
0N/A }
0N/A }
0N/A
0N/A }
0N/A
0N/A if (interpolationType != TYPE_NEAREST_NEIGHBOR &&
0N/A dst.getColorModel() instanceof IndexColorModel) {
0N/A dst = new BufferedImage(dst.getWidth(), dst.getHeight(),
0N/A BufferedImage.TYPE_INT_ARGB);
0N/A }
0N/A if (ImagingLib.filter(this, src, dst) == null) {
0N/A throw new ImagingOpException ("Unable to transform src image");
0N/A }
0N/A
0N/A if (needToConvert) {
0N/A ColorConvertOp ccop = new ColorConvertOp(hints);
0N/A ccop.filter(dst, origDst);
0N/A }
0N/A else if (origDst != dst) {
0N/A java.awt.Graphics2D g = origDst.createGraphics();
0N/A try {
0N/A g.setComposite(AlphaComposite.Src);
0N/A g.drawImage(dst, 0, 0, null);
0N/A } finally {
0N/A g.dispose();
0N/A }
0N/A }
0N/A
0N/A return origDst;
0N/A }
0N/A
0N/A /**
0N/A * Transforms the source <CODE>Raster</CODE> and stores the results in
0N/A * the destination <CODE>Raster</CODE>. This operation performs the
0N/A * transform band by band.
0N/A * <p>
0N/A * If the destination <CODE>Raster</CODE> is null, a new
0N/A * <CODE>Raster</CODE> is created.
0N/A * An <CODE>IllegalArgumentException</CODE> may be thrown if the source is
0N/A * the same as the destination or if the number of bands in
0N/A * the source is not equal to the number of bands in the
0N/A * destination.
0N/A * <p>
0N/A * The coordinates of the rectangle returned by
0N/A * <code>getBounds2D(Raster)</code>
0N/A * are not necessarily the same as the coordinates of the
0N/A * <code>WritableRaster</code> returned by this method. If the
0N/A * upper-left corner coordinates of rectangle are negative then
0N/A * this part of the rectangle is not drawn. If the coordinates
0N/A * of the rectangle are positive then the filtered image is drawn at
0N/A * that position in the destination <code>Raster</code>.
0N/A * <p>
0N/A * @param src The <CODE>Raster</CODE> to transform.
0N/A * @param dst The <CODE>Raster</CODE> in which to store the results of the
0N/A * transformation.
0N/A *
0N/A * @return The transformed <CODE>Raster</CODE>.
0N/A *
0N/A * @throws ImagingOpException if the raster cannot be transformed
0N/A * because of a data-processing error that might be
0N/A * caused by an invalid image format, tile format, or
0N/A * image-processing operation, or any other unsupported
0N/A * operation.
0N/A */
0N/A public final WritableRaster filter(Raster src, WritableRaster dst) {
0N/A if (src == null) {
0N/A throw new NullPointerException("src image is null");
0N/A }
0N/A if (dst == null) {
0N/A dst = createCompatibleDestRaster(src);
0N/A }
0N/A if (src == dst) {
0N/A throw new IllegalArgumentException("src image cannot be the "+
0N/A "same as the dst image");
0N/A }
0N/A if (src.getNumBands() != dst.getNumBands()) {
0N/A throw new IllegalArgumentException("Number of src bands ("+
0N/A src.getNumBands()+
0N/A ") does not match number of "+
0N/A " dst bands ("+
0N/A dst.getNumBands()+")");
0N/A }
0N/A
0N/A if (ImagingLib.filter(this, src, dst) == null) {
0N/A throw new ImagingOpException ("Unable to transform src image");
0N/A }
0N/A return dst;
0N/A }
0N/A
0N/A /**
0N/A * Returns the bounding box of the transformed destination. The
0N/A * rectangle returned is the actual bounding box of the
0N/A * transformed points. The coordinates of the upper-left corner
0N/A * of the returned rectangle might not be (0,&nbsp;0).
0N/A *
0N/A * @param src The <CODE>BufferedImage</CODE> to be transformed.
0N/A *
0N/A * @return The <CODE>Rectangle2D</CODE> representing the destination's
0N/A * bounding box.
0N/A */
0N/A public final Rectangle2D getBounds2D (BufferedImage src) {
0N/A return getBounds2D(src.getRaster());
0N/A }
0N/A
0N/A /**
0N/A * Returns the bounding box of the transformed destination. The
0N/A * rectangle returned will be the actual bounding box of the
0N/A * transformed points. The coordinates of the upper-left corner
0N/A * of the returned rectangle might not be (0,&nbsp;0).
0N/A *
0N/A * @param src The <CODE>Raster</CODE> to be transformed.
0N/A *
0N/A * @return The <CODE>Rectangle2D</CODE> representing the destination's
0N/A * bounding box.
0N/A */
0N/A public final Rectangle2D getBounds2D (Raster src) {
0N/A int w = src.getWidth();
0N/A int h = src.getHeight();
0N/A
0N/A // Get the bounding box of the src and transform the corners
0N/A float[] pts = {0, 0, w, 0, w, h, 0, h};
0N/A xform.transform(pts, 0, pts, 0, 4);
0N/A
0N/A // Get the min, max of the dst
0N/A float fmaxX = pts[0];
0N/A float fmaxY = pts[1];
0N/A float fminX = pts[0];
0N/A float fminY = pts[1];
0N/A for (int i=2; i < 8; i+=2) {
0N/A if (pts[i] > fmaxX) {
0N/A fmaxX = pts[i];
0N/A }
0N/A else if (pts[i] < fminX) {
0N/A fminX = pts[i];
0N/A }
0N/A if (pts[i+1] > fmaxY) {
0N/A fmaxY = pts[i+1];
0N/A }
0N/A else if (pts[i+1] < fminY) {
0N/A fminY = pts[i+1];
0N/A }
0N/A }
0N/A
0N/A return new Rectangle2D.Float(fminX, fminY, fmaxX-fminX, fmaxY-fminY);
0N/A }
0N/A
0N/A /**
0N/A * Creates a zeroed destination image with the correct size and number of
0N/A * bands. A <CODE>RasterFormatException</CODE> may be thrown if the
0N/A * transformed width or height is equal to 0.
0N/A * <p>
0N/A * If <CODE>destCM</CODE> is null,
0N/A * an appropriate <CODE>ColorModel</CODE> is used; this
0N/A * <CODE>ColorModel</CODE> may have
0N/A * an alpha channel even if the source <CODE>ColorModel</CODE> is opaque.
0N/A *
0N/A * @param src The <CODE>BufferedImage</CODE> to be transformed.
0N/A * @param destCM <CODE>ColorModel</CODE> of the destination. If null,
0N/A * an appropriate <CODE>ColorModel</CODE> is used.
0N/A *
0N/A * @return The zeroed destination image.
0N/A */
0N/A public BufferedImage createCompatibleDestImage (BufferedImage src,
0N/A ColorModel destCM) {
0N/A BufferedImage image;
0N/A Rectangle r = getBounds2D(src).getBounds();
0N/A
0N/A // If r.x (or r.y) is < 0, then we want to only create an image
0N/A // that is in the positive range.
0N/A // If r.x (or r.y) is > 0, then we need to create an image that
0N/A // includes the translation.
0N/A int w = r.x + r.width;
0N/A int h = r.y + r.height;
0N/A if (w <= 0) {
0N/A throw new RasterFormatException("Transformed width ("+w+
0N/A ") is less than or equal to 0.");
0N/A }
0N/A if (h <= 0) {
0N/A throw new RasterFormatException("Transformed height ("+h+
0N/A ") is less than or equal to 0.");
0N/A }
0N/A
0N/A if (destCM == null) {
0N/A ColorModel cm = src.getColorModel();
0N/A if (interpolationType != TYPE_NEAREST_NEIGHBOR &&
0N/A (cm instanceof IndexColorModel ||
0N/A cm.getTransparency() == Transparency.OPAQUE))
0N/A {
0N/A image = new BufferedImage(w, h,
0N/A BufferedImage.TYPE_INT_ARGB);
0N/A }
0N/A else {
0N/A image = new BufferedImage(cm,
0N/A src.getRaster().createCompatibleWritableRaster(w,h),
0N/A cm.isAlphaPremultiplied(), null);
0N/A }
0N/A }
0N/A else {
0N/A image = new BufferedImage(destCM,
0N/A destCM.createCompatibleWritableRaster(w,h),
0N/A destCM.isAlphaPremultiplied(), null);
0N/A }
0N/A
0N/A return image;
0N/A }
0N/A
0N/A /**
0N/A * Creates a zeroed destination <CODE>Raster</CODE> with the correct size
0N/A * and number of bands. A <CODE>RasterFormatException</CODE> may be thrown
0N/A * if the transformed width or height is equal to 0.
0N/A *
0N/A * @param src The <CODE>Raster</CODE> to be transformed.
0N/A *
0N/A * @return The zeroed destination <CODE>Raster</CODE>.
0N/A */
0N/A public WritableRaster createCompatibleDestRaster (Raster src) {
0N/A Rectangle2D r = getBounds2D(src);
0N/A
0N/A return src.createCompatibleWritableRaster((int)r.getX(),
0N/A (int)r.getY(),
0N/A (int)r.getWidth(),
0N/A (int)r.getHeight());
0N/A }
0N/A
0N/A /**
0N/A * Returns the location of the corresponding destination point given a
0N/A * point in the source. If <CODE>dstPt</CODE> is specified, it
0N/A * is used to hold the return value.
0N/A *
0N/A * @param srcPt The <code>Point2D</code> that represents the source
0N/A * point.
0N/A * @param dstPt The <CODE>Point2D</CODE> in which to store the result.
0N/A *
0N/A * @return The <CODE>Point2D</CODE> in the destination that corresponds to
0N/A * the specified point in the source.
0N/A */
0N/A public final Point2D getPoint2D (Point2D srcPt, Point2D dstPt) {
0N/A return xform.transform (srcPt, dstPt);
0N/A }
0N/A
0N/A /**
0N/A * Returns the affine transform used by this transform operation.
0N/A *
0N/A * @return The <CODE>AffineTransform</CODE> associated with this op.
0N/A */
0N/A public final AffineTransform getTransform() {
0N/A return (AffineTransform) xform.clone();
0N/A }
0N/A
0N/A /**
0N/A * Returns the rendering hints used by this transform operation.
0N/A *
0N/A * @return The <CODE>RenderingHints</CODE> object associated with this op.
0N/A */
0N/A public final RenderingHints getRenderingHints() {
0N/A if (hints == null) {
0N/A Object val;
0N/A switch(interpolationType) {
0N/A case TYPE_NEAREST_NEIGHBOR:
0N/A val = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
0N/A break;
0N/A case TYPE_BILINEAR:
0N/A val = RenderingHints.VALUE_INTERPOLATION_BILINEAR;
0N/A break;
0N/A case TYPE_BICUBIC:
0N/A val = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
0N/A break;
0N/A default:
0N/A // Should never get here
0N/A throw new InternalError("Unknown interpolation type "+
0N/A interpolationType);
0N/A
0N/A }
0N/A hints = new RenderingHints(RenderingHints.KEY_INTERPOLATION, val);
0N/A }
0N/A
0N/A return hints;
0N/A }
0N/A
0N/A // We need to be able to invert the transform if we want to
0N/A // transform the image. If the determinant of the matrix is 0,
0N/A // then we can't invert the transform.
0N/A void validateTransform(AffineTransform xform) {
0N/A if (Math.abs(xform.getDeterminant()) <= Double.MIN_VALUE) {
0N/A throw new ImagingOpException("Unable to invert transform "+xform);
0N/A }
0N/A }
0N/A}