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