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