jdk/src/share/classes/java/awt/image/AffineTransformOp.java
author prr
Thu, 10 Apr 2008 16:28:45 -0700
changeset 539 7952521a4ad3
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6662775: Move imaging and color classes from closed to open Reviewed-by: tdv, campbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.geom.NoninvertibleTransformException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.geom.Point2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.AlphaComposite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.GraphicsEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.RenderingHints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.Transparency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.awt.image.ImagingLib;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This class uses an affine transform to perform a linear mapping from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * 2D coordinates in the source image or <CODE>Raster</CODE> to 2D coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * in the destination image or <CODE>Raster</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The type of interpolation that is used is specified through a constructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * either by a <CODE>RenderingHints</CODE> object or by one of the integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * interpolation types defined in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * If a <CODE>RenderingHints</CODE> object is specified in the constructor, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * interpolation hint and the rendering quality hint are used to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * the interpolation type for this operation.  The color rendering hint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * and the dithering hint can be used when color conversion is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Note that the following constraints have to be met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <li>The source and destination must be different.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <li>For <CODE>Raster</CODE> objects, the number of bands in the source must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * be equal to the number of bands in the destination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see AffineTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see BufferedImageFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see java.awt.RenderingHints#KEY_INTERPOLATION
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @see java.awt.RenderingHints#KEY_RENDERING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @see java.awt.RenderingHints#KEY_COLOR_RENDERING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @see java.awt.RenderingHints#KEY_DITHERING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public class AffineTransformOp implements BufferedImageOp, RasterOp {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private AffineTransform xform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    RenderingHints hints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Nearest-neighbor interpolation type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static final int TYPE_NEAREST_NEIGHBOR = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Bilinear interpolation type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static final int TYPE_BILINEAR = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Bicubic interpolation type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static final int TYPE_BICUBIC = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    int interpolationType = TYPE_NEAREST_NEIGHBOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Constructs an <CODE>AffineTransformOp</CODE> given an affine transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * The interpolation type is determined from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <CODE>RenderingHints</CODE> object.  If the interpolation hint is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * defined, it will be used. Otherwise, if the rendering quality hint is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * defined, the interpolation type is determined from its value.  If no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * hints are specified (<CODE>hints</CODE> is null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * the interpolation type is {@link #TYPE_NEAREST_NEIGHBOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * TYPE_NEAREST_NEIGHBOR}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param xform The <CODE>AffineTransform</CODE> to use for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param hints The <CODE>RenderingHints</CODE> object used to specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * the interpolation type for the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @throws ImagingOpException if the transform is non-invertible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @see java.awt.RenderingHints#KEY_INTERPOLATION
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @see java.awt.RenderingHints#KEY_RENDERING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public AffineTransformOp(AffineTransform xform, RenderingHints hints){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        validateTransform(xform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.xform = (AffineTransform) xform.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.hints = hints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (hints != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            Object value = hints.get(hints.KEY_INTERPOLATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                value = hints.get(hints.KEY_RENDERING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if (value == hints.VALUE_RENDER_SPEED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    interpolationType = TYPE_NEAREST_NEIGHBOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                else if (value == hints.VALUE_RENDER_QUALITY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    interpolationType = TYPE_BILINEAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            else if (value == hints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                interpolationType = TYPE_NEAREST_NEIGHBOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            else if (value == hints.VALUE_INTERPOLATION_BILINEAR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                interpolationType = TYPE_BILINEAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            else if (value == hints.VALUE_INTERPOLATION_BICUBIC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                interpolationType = TYPE_BICUBIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            interpolationType = TYPE_NEAREST_NEIGHBOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Constructs an <CODE>AffineTransformOp</CODE> given an affine transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * and the interpolation type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param xform The <CODE>AffineTransform</CODE> to use for the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param interpolationType One of the integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * interpolation type constants defined by this class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * {@link #TYPE_NEAREST_NEIGHBOR TYPE_NEAREST_NEIGHBOR},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * {@link #TYPE_BILINEAR TYPE_BILINEAR},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * {@link #TYPE_BICUBIC TYPE_BICUBIC}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @throws ImagingOpException if the transform is non-invertible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public AffineTransformOp(AffineTransform xform, int interpolationType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        validateTransform(xform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this.xform = (AffineTransform)xform.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        switch(interpolationType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            case TYPE_NEAREST_NEIGHBOR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            case TYPE_BILINEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            case TYPE_BICUBIC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new IllegalArgumentException("Unknown interpolation type: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                               interpolationType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        this.interpolationType = interpolationType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns the interpolation type used by this op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @return the interpolation type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @see #TYPE_NEAREST_NEIGHBOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @see #TYPE_BILINEAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @see #TYPE_BICUBIC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public final int getInterpolationType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return interpolationType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Transforms the source <CODE>BufferedImage</CODE> and stores the results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * in the destination <CODE>BufferedImage</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * If the color models for the two images do not match, a color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * conversion into the destination color model is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * If the destination image is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * a <CODE>BufferedImage</CODE> is created with the source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * <CODE>ColorModel</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * The coordinates of the rectangle returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <code>getBounds2D(BufferedImage)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * are not necessarily the same as the coordinates of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <code>BufferedImage</code> returned by this method.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * upper-left corner coordinates of the rectangle are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * negative then this part of the rectangle is not drawn.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * upper-left corner coordinates of the  rectangle are positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * then the filtered image is drawn at that position in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * destination <code>BufferedImage</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * An <CODE>IllegalArgumentException</CODE> is thrown if the source is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * the same as the destination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param src The <CODE>BufferedImage</CODE> to transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param dst The <CODE>BufferedImage</CODE> in which to store the results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * of the transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @return The filtered <CODE>BufferedImage</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @throws IllegalArgumentException if <code>src</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *         <code>dst</code> are the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @throws ImagingOpException if the image cannot be transformed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *         because of a data-processing error that might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *         caused by an invalid image format, tile format, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *         image-processing operation, or any other unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *         operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public final BufferedImage filter(BufferedImage src, BufferedImage dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (src == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            throw new NullPointerException("src image is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (src == dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throw new IllegalArgumentException("src image cannot be the "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                               "same as the dst image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        boolean needToConvert = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        ColorModel srcCM = src.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        ColorModel dstCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        BufferedImage origDst = dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (dst == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            dst = createCompatibleDestImage(src, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            dstCM = srcCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            origDst = dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            dstCM = dst.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (srcCM.getColorSpace().getType() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                dstCM.getColorSpace().getType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                int type = xform.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                boolean needTrans = ((type&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                      (xform.TYPE_MASK_ROTATION|
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                       xform.TYPE_GENERAL_TRANSFORM))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                     != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                if (! needTrans && type != xform.TYPE_TRANSLATION && type != xform.TYPE_IDENTITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    double[] mtx = new double[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    xform.getMatrix(mtx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    // Check out the matrix.  A non-integral scale will force ARGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    // since the edge conditions can't be guaranteed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    needTrans = (mtx[0] != (int)mtx[0] || mtx[3] != (int)mtx[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                if (needTrans &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    srcCM.getTransparency() == Transparency.OPAQUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    // Need to convert first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    ColorConvertOp ccop = new ColorConvertOp(hints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    BufferedImage tmpSrc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    int sw = src.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    int sh = src.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    if (dstCM.getTransparency() == Transparency.OPAQUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                        tmpSrc = new BufferedImage(sw, sh,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                                  BufferedImage.TYPE_INT_ARGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        WritableRaster r =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                            dstCM.createCompatibleWritableRaster(sw, sh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        tmpSrc = new BufferedImage(dstCM, r,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                                  dstCM.isAlphaPremultiplied(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                                  null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    src = ccop.filter(src, tmpSrc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    needToConvert = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    dst = createCompatibleDestImage(src, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (interpolationType != TYPE_NEAREST_NEIGHBOR &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            dst.getColorModel() instanceof IndexColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            dst = new BufferedImage(dst.getWidth(), dst.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                    BufferedImage.TYPE_INT_ARGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (ImagingLib.filter(this, src, dst) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            throw new ImagingOpException ("Unable to transform src image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (needToConvert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            ColorConvertOp ccop = new ColorConvertOp(hints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            ccop.filter(dst, origDst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        else if (origDst != dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            java.awt.Graphics2D g = origDst.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                g.setComposite(AlphaComposite.Src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                g.drawImage(dst, 0, 0, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return origDst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Transforms the source <CODE>Raster</CODE> and stores the results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * the destination <CODE>Raster</CODE>.  This operation performs the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * transform band by band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * If the destination <CODE>Raster</CODE> is null, a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <CODE>Raster</CODE> is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * An <CODE>IllegalArgumentException</CODE> may be thrown if the source is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * the same as the destination or if the number of bands in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * the source is not equal to the number of bands in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * destination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * The coordinates of the rectangle returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <code>getBounds2D(Raster)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * are not necessarily the same as the coordinates of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <code>WritableRaster</code> returned by this method.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * upper-left corner coordinates of rectangle are negative then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * this part of the rectangle is not drawn.  If the coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * of the rectangle are positive then the filtered image is drawn at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * that position in the destination <code>Raster</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param src The <CODE>Raster</CODE> to transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @param dst The <CODE>Raster</CODE> in which to store the results of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @return The transformed <CODE>Raster</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @throws ImagingOpException if the raster cannot be transformed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *         because of a data-processing error that might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *         caused by an invalid image format, tile format, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *         image-processing operation, or any other unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *         operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public final WritableRaster filter(Raster src, WritableRaster dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (src == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            throw new NullPointerException("src image is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (dst == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            dst = createCompatibleDestRaster(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (src == dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw new IllegalArgumentException("src image cannot be the "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                               "same as the dst image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (src.getNumBands() != dst.getNumBands()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            throw new IllegalArgumentException("Number of src bands ("+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                               src.getNumBands()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                               ") does not match number of "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                               " dst bands ("+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                               dst.getNumBands()+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (ImagingLib.filter(this, src, dst) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            throw new ImagingOpException ("Unable to transform src image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Returns the bounding box of the transformed destination.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * rectangle returned is the actual bounding box of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * transformed points.  The coordinates of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * of the returned rectangle might not be (0,&nbsp;0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param src The <CODE>BufferedImage</CODE> to be transformed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @return The <CODE>Rectangle2D</CODE> representing the destination's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * bounding box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public final Rectangle2D getBounds2D (BufferedImage src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return getBounds2D(src.getRaster());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Returns the bounding box of the transformed destination.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * rectangle returned will be the actual bounding box of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * transformed points.  The coordinates of the upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * of the returned rectangle might not be (0,&nbsp;0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @param src The <CODE>Raster</CODE> to be transformed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @return The <CODE>Rectangle2D</CODE> representing the destination's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * bounding box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public final Rectangle2D getBounds2D (Raster src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        int w = src.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        int h = src.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        // Get the bounding box of the src and transform the corners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        float[] pts = {0, 0, w, 0, w, h, 0, h};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        xform.transform(pts, 0, pts, 0, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        // Get the min, max of the dst
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        float fmaxX = pts[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        float fmaxY = pts[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        float fminX = pts[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        float fminY = pts[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        for (int i=2; i < 8; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (pts[i] > fmaxX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                fmaxX = pts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            else if (pts[i] < fminX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                fminX = pts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            if (pts[i+1] > fmaxY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                fmaxY = pts[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            else if (pts[i+1] < fminY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                fminY = pts[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return new Rectangle2D.Float(fminX, fminY, fmaxX-fminX, fmaxY-fminY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Creates a zeroed destination image with the correct size and number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * bands.  A <CODE>RasterFormatException</CODE> may be thrown if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * transformed width or height is equal to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * If <CODE>destCM</CODE> is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * an appropriate <CODE>ColorModel</CODE> is used; this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * <CODE>ColorModel</CODE> may have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * an alpha channel even if the source <CODE>ColorModel</CODE> is opaque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @param src  The <CODE>BufferedImage</CODE> to be transformed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @param destCM  <CODE>ColorModel</CODE> of the destination.  If null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * an appropriate <CODE>ColorModel</CODE> is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @return The zeroed destination image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public BufferedImage createCompatibleDestImage (BufferedImage src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                                                    ColorModel destCM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        BufferedImage image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        Rectangle r = getBounds2D(src).getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        // If r.x (or r.y) is < 0, then we want to only create an image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        // that is in the positive range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        // If r.x (or r.y) is > 0, then we need to create an image that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        // includes the translation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        int w = r.x + r.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        int h = r.y + r.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (w <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            throw new RasterFormatException("Transformed width ("+w+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                                            ") is less than or equal to 0.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (h <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            throw new RasterFormatException("Transformed height ("+h+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                                            ") is less than or equal to 0.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (destCM == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            ColorModel cm = src.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            if (interpolationType != TYPE_NEAREST_NEIGHBOR &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                (cm instanceof IndexColorModel ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                 cm.getTransparency() == Transparency.OPAQUE))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                image = new BufferedImage(w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                                          BufferedImage.TYPE_INT_ARGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                image = new BufferedImage(cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                          src.getRaster().createCompatibleWritableRaster(w,h),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                          cm.isAlphaPremultiplied(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            image = new BufferedImage(destCM,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                    destCM.createCompatibleWritableRaster(w,h),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                                    destCM.isAlphaPremultiplied(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * Creates a zeroed destination <CODE>Raster</CODE> with the correct size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * and number of bands.  A <CODE>RasterFormatException</CODE> may be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * if the transformed width or height is equal to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @param src The <CODE>Raster</CODE> to be transformed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @return The zeroed destination <CODE>Raster</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    public WritableRaster createCompatibleDestRaster (Raster src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        Rectangle2D r = getBounds2D(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        return src.createCompatibleWritableRaster((int)r.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                                  (int)r.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                                  (int)r.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                                                  (int)r.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Returns the location of the corresponding destination point given a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * point in the source.  If <CODE>dstPt</CODE> is specified, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * is used to hold the return value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @param srcPt The <code>Point2D</code> that represents the source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *              point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @param dstPt The <CODE>Point2D</CODE> in which to store the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @return The <CODE>Point2D</CODE> in the destination that corresponds to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * the specified point in the source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    public final Point2D getPoint2D (Point2D srcPt, Point2D dstPt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        return xform.transform (srcPt, dstPt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * Returns the affine transform used by this transform operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @return The <CODE>AffineTransform</CODE> associated with this op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    public final AffineTransform getTransform() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return (AffineTransform) xform.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Returns the rendering hints used by this transform operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @return The <CODE>RenderingHints</CODE> object associated with this op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    public final RenderingHints getRenderingHints() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        if (hints == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            Object val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            switch(interpolationType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            case TYPE_NEAREST_NEIGHBOR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                val = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            case TYPE_BILINEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                val = RenderingHints.VALUE_INTERPOLATION_BILINEAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            case TYPE_BICUBIC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                val = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                // Should never get here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                throw new InternalError("Unknown interpolation type "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                                         interpolationType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            hints = new RenderingHints(RenderingHints.KEY_INTERPOLATION, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        return hints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    // We need to be able to invert the transform if we want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    // transform the image.  If the determinant of the matrix is 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    // then we can't invert the transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    void validateTransform(AffineTransform xform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (Math.abs(xform.getDeterminant()) <= Double.MIN_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            throw new ImagingOpException("Unable to invert transform "+xform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
}