src/java.desktop/share/classes/java/awt/image/RescaleOp.java
author tvaleev
Thu, 04 Oct 2018 12:40:55 -0700
changeset 52248 2e330da7cbf4
parent 47509 7d0f05e7c7f5
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
/*
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47509
diff changeset
     2
 * Copyright (c) 1997, 2018, 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.color.ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.geom.Rectangle2D;
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
    30
import java.awt.AlphaComposite;
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
    31
import java.awt.Graphics2D;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.geom.Point2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.RenderingHints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.awt.image.ImagingLib;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class performs a pixel-by-pixel rescaling of the data in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * source image by multiplying the sample values for each pixel by a scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * factor and then adding an offset. The scaled sample values are clipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * to the minimum/maximum representable in the destination image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The pseudo code for the rescaling operation is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *for each pixel from Source object {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *    for each band/component of the pixel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *        dstElement = (srcElement*scaleFactor) + offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * For Rasters, rescaling operates on bands.  The number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * sets of scaling constants may be one, in which case the same constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * are applied to all bands, or it must equal the number of Source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Raster bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * For BufferedImages, rescaling operates on color and alpha components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * The number of sets of scaling constants may be one, in which case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * same constants are applied to all color (but not alpha) components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Otherwise, the  number of sets of scaling constants may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * equal the number of Source color components, in which case no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * rescaling of the alpha component (if present) is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * If neither of these cases apply, the number of sets of scaling constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * must equal the number of Source color components plus alpha components,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * in which case all color and alpha components are rescaled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * BufferedImage sources with premultiplied alpha data are treated in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * manner as non-premultiplied images for purposes of rescaling.  That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * the rescaling is done per band on the raw data of the BufferedImage source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * without regard to whether the data is premultiplied.  If a color conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * is required to the destination ColorModel, the premultiplied state of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * both source and destination will be taken into account for this step.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Images with an IndexColorModel cannot be rescaled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * If a RenderingHints object is specified in the constructor, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * color rendering hint and the dithering hint may be used when color
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * conversion is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * Note that in-place operation is allowed (i.e. the source and destination can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * be the same object).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @see java.awt.RenderingHints#KEY_COLOR_RENDERING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @see java.awt.RenderingHints#KEY_DITHERING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
public class RescaleOp implements BufferedImageOp, RasterOp {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    float[] scaleFactors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    float[] offsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    RenderingHints hints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private int srcNbits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private int dstNbits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Constructs a new RescaleOp with the desired scale factors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * and offsets.  The length of the scaleFactor and offset arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * must meet the restrictions stated in the class comments above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * The RenderingHints argument may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param scaleFactors the specified scale factors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param offsets the specified offsets
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   102
     * @param hints the specified {@code RenderingHints}, or
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   103
     *        {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public RescaleOp (float[] scaleFactors, float[] offsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                      RenderingHints hints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        length = scaleFactors.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (length > offsets.length) length = offsets.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.scaleFactors = new float[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.offsets      = new float[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        for (int i=0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            this.scaleFactors[i] = scaleFactors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            this.offsets[i]      = offsets[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.hints = hints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Constructs a new RescaleOp with the desired scale factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * and offset.  The scaleFactor and offset will be applied to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * all bands in a source Raster and to all color (but not alpha)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * components in a BufferedImage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * The RenderingHints argument may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param scaleFactor the specified scale factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param offset the specified offset
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   127
     * @param hints the specified {@code RenderingHints}, or
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   128
     *        {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public RescaleOp (float scaleFactor, float offset, RenderingHints hints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        length = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.scaleFactors = new float[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.offsets      = new float[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        this.scaleFactors[0] = scaleFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        this.offsets[0]       = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.hints = hints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Returns the scale factors in the given array. The array is also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * returned for convenience.  If scaleFactors is null, a new array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * will be allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param scaleFactors the array to contain the scale factors of
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   144
     *        this {@code RescaleOp}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   145
     * @return the scale factors of this {@code RescaleOp}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47509
diff changeset
   147
    public final float[] getScaleFactors (float[] scaleFactors) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (scaleFactors == null) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 5506
diff changeset
   149
            return this.scaleFactors.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        System.arraycopy (this.scaleFactors, 0, scaleFactors, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                          Math.min(this.scaleFactors.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                   scaleFactors.length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return scaleFactors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Returns the offsets in the given array. The array is also returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * for convenience.  If offsets is null, a new array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * will be allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param offsets the array to contain the offsets of
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   162
     *        this {@code RescaleOp}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   163
     * @return the offsets of this {@code RescaleOp}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47509
diff changeset
   165
    public final float[] getOffsets(float[] offsets) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (offsets == null) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 5506
diff changeset
   167
            return this.offsets.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        System.arraycopy (this.offsets, 0, offsets, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                          Math.min(this.offsets.length, offsets.length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return offsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Returns the number of scaling factors and offsets used in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * RescaleOp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return the number of scaling factors and offsets of this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   179
     *         {@code RescaleOp}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 32280
diff changeset
   181
    public final int getNumFactors() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Creates a ByteLookupTable to implement the rescale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * The table may have either a SHORT or BYTE input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param nElems    Number of elements the table is to have.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *                  This will generally be 256 for byte and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *                  65536 for short.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47509
diff changeset
   193
    private ByteLookupTable createByteLut(float[] scale,
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47509
diff changeset
   194
                                          float[] off,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                          int   nBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                          int   nElems) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   198
        byte[][]        lutData = new byte[nBands][nElems];
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   199
        int band;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   201
        for (band=0; band<scale.length; band++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            float  bandScale   = scale[band];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            float  bandOff     = off[band];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            byte[] bandLutData = lutData[band];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            for (int i=0; i<nElems; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                int val = (int)(i*bandScale + bandOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                if ((val & 0xffffff00) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    if (val < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        val = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                bandLutData[i] = (byte)val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   218
        int maxToCopy = (nBands == 4 && scale.length == 4) ? 4 : 3;
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   219
        while (band < lutData.length && band < maxToCopy) {
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   220
           System.arraycopy(lutData[band-1], 0, lutData[band], 0, nElems);
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   221
           band++;
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   222
        }
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   223
        if (nBands == 4 && band < nBands) {
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   224
           byte[] bandLutData = lutData[band];
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   225
           for (int i=0; i<nElems; i++) {
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   226
              bandLutData[i] = (byte)i;
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   227
           }
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   228
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return new ByteLookupTable(0, lutData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Creates a ShortLookupTable to implement the rescale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * The table may have either a SHORT or BYTE input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param nElems    Number of elements the table is to have.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *                  This will generally be 256 for byte and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *                  65536 for short.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47509
diff changeset
   240
    private ShortLookupTable createShortLut(float[] scale,
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47509
diff changeset
   241
                                            float[] off,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                            int   nBands,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                            int   nElems) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   245
        short[][]        lutData = new short[nBands][nElems];
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   246
        int band = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   248
        for (band=0; band<scale.length; band++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            float   bandScale   = scale[band];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            float   bandOff     = off[band];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            short[] bandLutData = lutData[band];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            for (int i=0; i<nElems; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                int val = (int)(i*bandScale + bandOff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                if ((val & 0xffff0000) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    if (val < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                        val = 65535;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                bandLutData[i] = (short)val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   264
        int maxToCopy = (nBands == 4 && scale.length == 4) ? 4 : 3;
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   265
        while (band < lutData.length && band < maxToCopy) {
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   266
           System.arraycopy(lutData[band-1], 0, lutData[band], 0, nElems);
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   267
           band++;
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   268
        }
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   269
        if (nBands == 4 && band < nBands) {
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   270
           short[] bandLutData = lutData[band];
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   271
           for (int i=0; i<nElems; i++) {
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   272
              bandLutData[i] = (short)i;
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   273
           }
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   274
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return new ShortLookupTable(0, lutData);
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
     * Determines if the rescale can be performed as a lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * The dst must be a byte or short type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * The src must be less than 16 bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * All source band sizes must be the same and all dst band sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * must be the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private boolean canUseLookup(Raster src, Raster dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        // Check that the src datatype is either a BYTE or SHORT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        int datatype = src.getDataBuffer().getDataType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if(datatype != DataBuffer.TYPE_BYTE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
           datatype != DataBuffer.TYPE_USHORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // Check dst sample sizes. All must be 8 or 16 bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        SampleModel dstSM = dst.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        dstNbits = dstSM.getSampleSize(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (!(dstNbits == 8 || dstNbits == 16)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        for (int i=1; i<src.getNumBands(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            int bandSize = dstSM.getSampleSize(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if (bandSize != dstNbits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        // Check src sample sizes. All must be the same size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        SampleModel srcSM = src.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        srcNbits = srcSM.getSampleSize(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (srcNbits > 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        for (int i=1; i<src.getNumBands(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            int bandSize = srcSM.getSampleSize(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (bandSize != srcNbits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   329
      if (dstSM instanceof ComponentSampleModel) {
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   330
           ComponentSampleModel dsm = (ComponentSampleModel)dstSM;
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   331
           if (dsm.getPixelStride() != dst.getNumBands()) {
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   332
               return false;
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   333
           }
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   334
        }
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   335
        if (srcSM instanceof ComponentSampleModel) {
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   336
           ComponentSampleModel csm = (ComponentSampleModel)srcSM;
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   337
           if (csm.getPixelStride() != src.getNumBands()) {
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   338
               return false;
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   339
           }
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   340
        }
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   341
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Rescales the source BufferedImage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * If the color model in the source image is not the same as that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * in the destination image, the pixels will be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * in the destination.  If the destination image is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * a BufferedImage will be created with the source ColorModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * An IllegalArgumentException may be thrown if the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * scaling factors/offsets in this object does not meet the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * restrictions stated in the class comments above, or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * source image has an IndexColorModel.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   355
     * @param src the {@code BufferedImage} to be filtered
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param dst the destination for the filtering operation
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   357
     *            or {@code null}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   358
     * @return the filtered {@code BufferedImage}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   359
     * @throws IllegalArgumentException if the {@code ColorModel}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   360
     *         of {@code src} is an {@code IndexColorModel},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *         or if the number of scaling factors and offsets in this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   362
     *         {@code RescaleOp} do not meet the requirements
47509
7d0f05e7c7f5 8180501: RescaleOp.filter does not document IllegalArgumentException if sizes differ.
prr
parents: 47216
diff changeset
   363
     *         stated in the class comments, or if the source and
7d0f05e7c7f5 8180501: RescaleOp.filter does not document IllegalArgumentException if sizes differ.
prr
parents: 47216
diff changeset
   364
     *         destination images differ in size.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public final BufferedImage filter (BufferedImage src, BufferedImage dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        ColorModel srcCM = src.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        ColorModel dstCM;
32280
2a6b0db67f5e 8080287: The image of BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_ARGB_PRE is blank
psadhukhan
parents: 25859
diff changeset
   369
        int numSrcColorComp = srcCM.getNumColorComponents();
2a6b0db67f5e 8080287: The image of BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_ARGB_PRE is blank
psadhukhan
parents: 25859
diff changeset
   370
        int scaleConst = length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (srcCM instanceof IndexColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                IllegalArgumentException("Rescaling cannot be "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                         "performed on an indexed image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
32280
2a6b0db67f5e 8080287: The image of BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_ARGB_PRE is blank
psadhukhan
parents: 25859
diff changeset
   377
        if (scaleConst != 1 && scaleConst != numSrcColorComp &&
2a6b0db67f5e 8080287: The image of BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_ARGB_PRE is blank
psadhukhan
parents: 25859
diff changeset
   378
            scaleConst != srcCM.getNumComponents())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            throw new IllegalArgumentException("Number of scaling constants "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                                               "does not equal the number of"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                               " of color or color/alpha "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                               " components");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        boolean needToConvert = false;
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   387
        boolean needToDraw = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        // Include alpha
32280
2a6b0db67f5e 8080287: The image of BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_ARGB_PRE is blank
psadhukhan
parents: 25859
diff changeset
   390
        if (scaleConst > numSrcColorComp && srcCM.hasAlpha()) {
2a6b0db67f5e 8080287: The image of BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_ARGB_PRE is blank
psadhukhan
parents: 25859
diff changeset
   391
            scaleConst = numSrcColorComp+1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        int width = src.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        int height = src.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
32280
2a6b0db67f5e 8080287: The image of BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_ARGB_PRE is blank
psadhukhan
parents: 25859
diff changeset
   397
        BufferedImage origDst = dst;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (dst == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            dst = createCompatibleDestImage(src, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            dstCM = srcCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (width != dst.getWidth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    IllegalArgumentException("Src width ("+width+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                             ") not equal to dst width ("+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                                             dst.getWidth()+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (height != dst.getHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    IllegalArgumentException("Src height ("+height+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                                             ") not equal to dst height ("+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                                             dst.getHeight()+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            dstCM = dst.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            if(srcCM.getColorSpace().getType() !=
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   418
                 dstCM.getColorSpace().getType()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                needToConvert = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                dst = createCompatibleDestImage(src, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        // Try to use a native BI rescale operation first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (ImagingLib.filter(this, src, dst) == null) {
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   429
            if (src.getRaster().getNumBands() !=
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   430
                dst.getRaster().getNumBands()) {
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   431
                needToDraw = true;
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   432
                dst = createCompatibleDestImage(src, null);
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   433
            }
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   434
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            // Native BI rescale failed - convert to rasters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            WritableRaster srcRaster = src.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            WritableRaster dstRaster = dst.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            // Call the raster filter method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            //
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   444
            filterRasterImpl(srcRaster, dstRaster, scaleConst, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   447
        if (needToDraw) {
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   448
             Graphics2D g = origDst.createGraphics();
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   449
             g.setComposite(AlphaComposite.Src);
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   450
             g.drawImage(dst, 0, 0, width, height, null);
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   451
             g.dispose();
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   452
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (needToConvert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            // ColorModels are not the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            ColorConvertOp ccop = new ColorConvertOp(hints);
32280
2a6b0db67f5e 8080287: The image of BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_ARGB_PRE is blank
psadhukhan
parents: 25859
diff changeset
   456
            dst = ccop.filter(dst, origDst);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
32280
2a6b0db67f5e 8080287: The image of BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_ARGB_PRE is blank
psadhukhan
parents: 25859
diff changeset
   458
        return dst;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * Rescales the pixel data in the source Raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * If the destination Raster is null, a new Raster will be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * The source and destination must have the same number of bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Otherwise, an IllegalArgumentException is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Note that the number of scaling factors/offsets in this object must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * meet the restrictions stated in the class comments above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Otherwise, an IllegalArgumentException is thrown.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   469
     * @param src the {@code Raster} to be filtered
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @param dst the destination for the filtering operation
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   471
     *            or {@code null}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   472
     * @return the filtered {@code WritableRaster}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   473
     * @throws IllegalArgumentException if {@code src} and
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   474
     *         {@code dst} do not have the same number of bands,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *         or if the number of scaling factors and offsets in this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   476
     *         {@code RescaleOp} do not meet the requirements
47509
7d0f05e7c7f5 8180501: RescaleOp.filter does not document IllegalArgumentException if sizes differ.
prr
parents: 47216
diff changeset
   477
     *         stated in the class comments, or if the source and
7d0f05e7c7f5 8180501: RescaleOp.filter does not document IllegalArgumentException if sizes differ.
prr
parents: 47216
diff changeset
   478
     *         destination rasters differ in size.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    public final WritableRaster filter (Raster src, WritableRaster dst)  {
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   481
        return filterRasterImpl(src, dst, length, true);
32280
2a6b0db67f5e 8080287: The image of BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_ARGB_PRE is blank
psadhukhan
parents: 25859
diff changeset
   482
    }
2a6b0db67f5e 8080287: The image of BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_ARGB_PRE is blank
psadhukhan
parents: 25859
diff changeset
   483
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   484
    private WritableRaster filterRasterImpl(Raster src, WritableRaster dst,
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   485
                                            int scaleConst, boolean sCheck) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        int numBands = src.getNumBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        int width  = src.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        int height = src.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        int[] srcPix = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        int step = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        int tidx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        // Create a new destination Raster, if needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (dst == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            dst = createCompatibleDestRaster(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        else if (height != dst.getHeight() || width != dst.getWidth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
               IllegalArgumentException("Width or height of Rasters do not "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                                        "match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        else if (numBands != dst.getNumBands()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            // Make sure that the number of bands are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            throw new IllegalArgumentException("Number of bands in src "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                            + numBands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                            + " does not equal number of bands in dest "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                            + dst.getNumBands());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
32280
2a6b0db67f5e 8080287: The image of BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_ARGB_PRE is blank
psadhukhan
parents: 25859
diff changeset
   509
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        // Make sure that the arrays match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        // Make sure that the low/high/constant arrays match
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   512
        if (sCheck && scaleConst != 1 && scaleConst != src.getNumBands()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            throw new IllegalArgumentException("Number of scaling constants "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                                               "does not equal the number of"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                                               " of bands in the src raster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        // Try for a native raster rescale first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        if (ImagingLib.filter(this, src, dst) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            return dst;
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
        // Native raster rescale failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        // Try to see if a lookup operation can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        if (canUseLookup(src, dst)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            int srcNgray = (1 << srcNbits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            int dstNgray = (1 << dstNbits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if (dstNgray == 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                ByteLookupTable lut = createByteLut(scaleFactors, offsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                                                    numBands, srcNgray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                LookupOp op = new LookupOp(lut, hints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                op.filter(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                ShortLookupTable lut = createShortLut(scaleFactors, offsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                                                      numBands, srcNgray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                LookupOp op = new LookupOp(lut, hints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                op.filter(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            // Fall back to the slow code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            //
32280
2a6b0db67f5e 8080287: The image of BufferedImage.TYPE_INT_ARGB and BufferedImage.TYPE_INT_ARGB_PRE is blank
psadhukhan
parents: 25859
diff changeset
   548
            if (scaleConst > 1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                step = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            int sminX = src.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            int sY = src.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            int dminX = dst.getMinX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            int dY = dst.getMinY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            int sX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            int dX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            //  Determine bits per band to determine maxval for clamps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            //  The min is assumed to be zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            //  REMIND: This must change if we ever support signed data types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            int nbits;
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47509
diff changeset
   565
            int[] dstMax = new int[numBands];
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47509
diff changeset
   566
            int[] dstMask = new int[numBands];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            SampleModel dstSM = dst.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            for (int z=0; z<numBands; z++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                nbits = dstSM.getSampleSize(z);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                dstMax[z] = (1 << nbits) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                dstMask[z] = ~(dstMax[z]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            int val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            for (int y=0; y < height; y++, sY++, dY++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                dX = dminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                sX = sminX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                for (int x = 0; x < width; x++, sX++, dX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    // Get data for all bands at this x,y position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    srcPix = src.getPixel(sX, sY, srcPix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    tidx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    for (int z=0; z<numBands; z++, tidx += step) {
45349
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   583
                        if ((scaleConst == 1 || scaleConst == 3) &&
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   584
                            (z == 3) && (numBands == 4)) {
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   585
                           val = srcPix[z];
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   586
                        } else {
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   587
                            val = (int)(srcPix[z]*scaleFactors[tidx]
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   588
                                              + offsets[tidx]);
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   589
b8f80bc16972 8177393: Result of RescaleOp for 4BYTE_ABGR images may be 25% black
prr
parents: 35667
diff changeset
   590
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                        // Clamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        if ((val & dstMask[z]) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                            if (val < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                                val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                                val = dstMax[z];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                        srcPix[z] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    // Put it back for all bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    dst.setPixel(dX, dY, srcPix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * Returns the bounding box of the rescaled destination image.  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * this is not a geometric operation, the bounding box does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    public final Rectangle2D getBounds2D (BufferedImage src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         return getBounds2D(src.getRaster());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * Returns the bounding box of the rescaled destination Raster.  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * this is not a geometric operation, the bounding box does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * change.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   624
     * @param src the rescaled destination {@code Raster}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   625
     * @return the bounds of the specified {@code Raster}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    public final Rectangle2D getBounds2D (Raster src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        return src.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * Creates a zeroed destination image with the correct size and number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @param src       Source image for the filter operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @param destCM    ColorModel of the destination.  If null, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *                  ColorModel of the source will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @return the zeroed-destination image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    public BufferedImage createCompatibleDestImage (BufferedImage src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                                                    ColorModel destCM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        BufferedImage image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        if (destCM == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            ColorModel cm = src.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            image = new BufferedImage(cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                      src.getRaster().createCompatibleWritableRaster(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                                      cm.isAlphaPremultiplied(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                                      null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            int w = src.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            int h = src.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            image = new BufferedImage (destCM,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                                   destCM.createCompatibleWritableRaster(w, h),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                                   destCM.isAlphaPremultiplied(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   661
     * Creates a zeroed-destination {@code Raster} with the correct
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * size and number of bands, given this source.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   663
     * @param src       the source {@code Raster}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   664
     * @return the zeroed-destination {@code Raster}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    public WritableRaster createCompatibleDestRaster (Raster src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        return src.createCompatibleWritableRaster(src.getWidth(), src.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * Returns the location of the destination point given a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * point in the source.  If dstPt is non-null, it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * be used to hold the return value.  Since this is not a geometric
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * operation, the srcPt will equal the dstPt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @param srcPt a point in the source image
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   676
     * @param dstPt the destination point or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @return the location of the destination point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    public final Point2D getPoint2D (Point2D srcPt, Point2D dstPt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        if (dstPt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            dstPt = new Point2D.Float();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        dstPt.setLocation(srcPt.getX(), srcPt.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        return dstPt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * Returns the rendering hints for this op.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   689
     * @return the rendering hints of this {@code RescaleOp}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    public final RenderingHints getRenderingHints() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        return hints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
}