src/java.desktop/share/classes/java/awt/image/AffineTransformOp.java
author tvaleev
Thu, 04 Oct 2018 12:40:55 -0700
changeset 52248 2e330da7cbf4
parent 47216 71c04702a3d5
permissions -rw-r--r--
8211300: Convert C-style array declarations in JDK client code Reviewed-by: prr, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 16734
diff changeset
     2
 * Copyright (c) 1997, 2014, 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
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    42
 * 2D coordinates in the source image or {@code Raster} to 2D coordinates
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    43
 * in the destination image or {@code Raster}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * The type of interpolation that is used is specified through a constructor,
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    45
 * either by a {@code RenderingHints} object or by one of the integer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * interpolation types defined in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    48
 * If a {@code RenderingHints} object is specified in the constructor, the
2
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.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    56
 * <li>For {@code Raster} objects, the number of bands in the source must
2
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
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    88
     * Constructs an {@code AffineTransformOp} given an affine transform.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * The interpolation type is determined from the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    90
     * {@code RenderingHints} object.  If the interpolation hint is
2
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
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    93
     * hints are specified ({@code hints} is null),
2
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
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    97
     * @param xform The {@code AffineTransform} to use for the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   100
     * @param hints The {@code RenderingHints} object used to specify
2
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) {
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 16734
diff changeset
   113
            Object value = hints.get(RenderingHints.KEY_INTERPOLATION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (value == null) {
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 16734
diff changeset
   115
                value = hints.get(RenderingHints.KEY_RENDERING);
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 16734
diff changeset
   116
                if (value == RenderingHints.VALUE_RENDER_SPEED) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    interpolationType = TYPE_NEAREST_NEIGHBOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 16734
diff changeset
   119
                else if (value == RenderingHints.VALUE_RENDER_QUALITY) {
2
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
            }
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 16734
diff changeset
   123
            else if (value == RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                interpolationType = TYPE_NEAREST_NEIGHBOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 16734
diff changeset
   126
            else if (value == RenderingHints.VALUE_INTERPOLATION_BILINEAR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                interpolationType = TYPE_BILINEAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 16734
diff changeset
   129
            else if (value == RenderingHints.VALUE_INTERPOLATION_BICUBIC) {
2
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
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   139
     * Constructs an {@code AffineTransformOp} given an affine transform
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * and the interpolation type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   142
     * @param xform The {@code AffineTransform} to use for the operation.
2
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
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   177
     * Transforms the source {@code BufferedImage} and stores the results
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   178
     * in the destination {@code BufferedImage}.
2
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,
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   182
     * a {@code BufferedImage} is created with the source
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   183
     * {@code ColorModel}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * The coordinates of the rectangle returned by
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   186
     * {@code getBounds2D(BufferedImage)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * are not necessarily the same as the coordinates of the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   188
     * {@code BufferedImage} returned by this method.  If the
2
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
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   193
     * destination {@code BufferedImage}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   195
     * An {@code IllegalArgumentException} is thrown if the source is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * the same as the destination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   198
     * @param src The {@code BufferedImage} to transform.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   199
     * @param dst The {@code BufferedImage} in which to store the results
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * of the transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   202
     * @return The filtered {@code BufferedImage}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   203
     * @throws IllegalArgumentException if {@code src} and
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   204
     *         {@code dst} are the same
2
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&
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 16734
diff changeset
   238
                                      (AffineTransform.TYPE_MASK_ROTATION|
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 16734
diff changeset
   239
                                       AffineTransform.TYPE_GENERAL_TRANSFORM))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                     != 0);
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 16734
diff changeset
   241
                if (! needTrans &&
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 16734
diff changeset
   242
                    type != AffineTransform.TYPE_TRANSLATION &&
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 16734
diff changeset
   243
                    type != AffineTransform.TYPE_IDENTITY)
2
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
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   308
     * Transforms the source {@code Raster} and stores the results in
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   309
     * the destination {@code Raster}.  This operation performs the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * transform band by band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   312
     * If the destination {@code Raster} is null, a new
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   313
     * {@code Raster} is created.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   314
     * An {@code IllegalArgumentException} may be thrown if the source is
2
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
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   320
     * {@code getBounds2D(Raster)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * are not necessarily the same as the coordinates of the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   322
     * {@code WritableRaster} returned by this method.  If the
2
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
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   326
     * that position in the destination {@code Raster}.
25770
d132697706ea 8037511: Tidy warnings cleanup for java.awt - 2d part
avstepan
parents: 22567
diff changeset
   327
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   328
     * @param src The {@code Raster} to transform.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   329
     * @param dst The {@code Raster} in which to store the results of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   332
     * @return The transformed {@code Raster}.
2
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
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   371
     * @param src The {@code BufferedImage} to be transformed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   373
     * @return The {@code Rectangle2D} representing the destination's
2
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
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   386
     * @param src The {@code Raster} to be transformed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   388
     * @return The {@code Rectangle2D} representing the destination's
2
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
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   424
     * bands.  A {@code RasterFormatException} may be thrown if the
2
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>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   427
     * If {@code destCM} is null,
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   428
     * an appropriate {@code ColorModel} is used; this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   429
     * {@code ColorModel} may have
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   430
     * an alpha channel even if the source {@code ColorModel} is opaque.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   432
     * @param src  The {@code BufferedImage} to be transformed.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   433
     * @param destCM  {@code ColorModel} of the destination.  If null,
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   434
     * an appropriate {@code ColorModel} is used.
2
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
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   483
     * Creates a zeroed destination {@code Raster} with the correct size
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   484
     * and number of bands.  A {@code RasterFormatException} may be thrown
2
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
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   487
     * @param src The {@code Raster} to be transformed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   489
     * @return The zeroed destination {@code Raster}.
2
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
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   502
     * point in the source.  If {@code dstPt} is specified, it
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * is used to hold the return value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   505
     * @param srcPt The {@code Point2D} that represents the source
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *              point.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   507
     * @param dstPt The {@code Point2D} in which to store the result.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   509
     * @return The {@code Point2D} in the destination that corresponds to
2
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
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   519
     * @return The {@code AffineTransform} associated with this op.
2
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
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   528
     * @return The {@code RenderingHints} object associated with this op.
2
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
}