jdk/src/share/classes/java/awt/image/ConvolveOp.java
author erikj
Thu, 07 Jun 2012 18:05:09 -0700
changeset 12813 c10ab96dcf41
parent 5506 202f599c92aa
child 16734 da1901d79073
permissions -rw-r--r--
7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI Reviewed-by: ohair, ohrstrom, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2000, 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.ICC_Profile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.RenderingHints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.geom.Point2D;
12813
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    33
import javax.tools.annotation.GenerateNativeHeader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.awt.image.ImagingLib;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class implements a convolution from the source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * to the destination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Convolution using a convolution kernel is a spatial operation that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * computes the output pixel from an input pixel by multiplying the kernel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * with the surround of the input pixel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This allows the output pixel to be affected by the immediate neighborhood
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * in a way that can be mathematically specified with a kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This class operates with BufferedImage data in which color components are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * premultiplied with the alpha component.  If the Source BufferedImage has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * an alpha component, and the color components are not premultiplied with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the alpha component, then the data are premultiplied before being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * convolved.  If the Destination has color components which are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * premultiplied, then alpha is divided out before storing into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Destination (if alpha is 0, the color components are set to 0).  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Destination has no alpha component, then the resulting alpha is discarded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * after first dividing it out of the color components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Rasters are treated as having no alpha channel.  If the above treatment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * of the alpha channel in BufferedImages is not desired, it may be avoided
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * by getting the Raster of a source BufferedImage and using the filter method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * of this class which works with Rasters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * If a RenderingHints object is specified in the constructor, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * color rendering hint and the dithering hint may be used when color
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * conversion is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Note that the Source and the Destination may not be the same object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @see Kernel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @see java.awt.RenderingHints#KEY_COLOR_RENDERING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @see java.awt.RenderingHints#KEY_DITHERING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
12813
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    69
/* No native methods here, but the constants are needed in the supporting JNI code */
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    70
@GenerateNativeHeader
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
public class ConvolveOp implements BufferedImageOp, RasterOp {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    Kernel kernel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    int edgeHint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    RenderingHints hints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Edge condition constants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Pixels at the edge of the destination image are set to zero.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * is the default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static final int EDGE_ZERO_FILL = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Pixels at the edge of the source image are copied to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * the corresponding pixels in the destination without modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static final int EDGE_NO_OP     = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Constructs a ConvolveOp given a Kernel, an edge condition, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * RenderingHints object (which may be null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param kernel the specified <code>Kernel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param edgeCondition the specified edge condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param hints the specified <code>RenderingHints</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @see Kernel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @see #EDGE_NO_OP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @see #EDGE_ZERO_FILL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @see java.awt.RenderingHints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public ConvolveOp(Kernel kernel, int edgeCondition, RenderingHints hints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.kernel   = kernel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.edgeHint = edgeCondition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.hints    = hints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Constructs a ConvolveOp given a Kernel.  The edge condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * will be EDGE_ZERO_FILL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param kernel the specified <code>Kernel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @see Kernel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @see #EDGE_ZERO_FILL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public ConvolveOp(Kernel kernel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this.kernel   = kernel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this.edgeHint = EDGE_ZERO_FILL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Returns the edge condition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @return the edge condition of this <code>ConvolveOp</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @see #EDGE_NO_OP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @see #EDGE_ZERO_FILL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public int getEdgeCondition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return edgeHint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Returns the Kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @return the <code>Kernel</code> of this <code>ConvolveOp</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public final Kernel getKernel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return (Kernel) kernel.clone();
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
     * Performs a convolution on BufferedImages.  Each component of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * source image will be convolved (including the alpha component, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * present).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * If the color model in the source image is not the same as that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * in the destination image, the pixels will be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * in the destination.  If the destination image is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * a BufferedImage will be created with the source ColorModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * The IllegalArgumentException may be thrown if the source is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * same as the destination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param src the source <code>BufferedImage</code> to filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param dst the destination <code>BufferedImage</code> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *        filtered <code>src</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @return the filtered <code>BufferedImage</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @throws NullPointerException if <code>src</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @throws IllegalArgumentException if <code>src</code> equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *         <code>dst</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @throws ImagingOpException if <code>src</code> cannot be filtered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public final BufferedImage filter (BufferedImage src, BufferedImage dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (src == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new NullPointerException("src image is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (src == dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new IllegalArgumentException("src image cannot be the "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                               "same as the dst image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        boolean needToConvert = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        ColorModel srcCM = src.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        ColorModel dstCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        BufferedImage origDst = dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // Can't convolve an IndexColorModel.  Need to expand it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (srcCM instanceof IndexColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            IndexColorModel icm = (IndexColorModel) srcCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            src = icm.convertToIntDiscrete(src.getRaster(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            srcCM = src.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (dst == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            dst = createCompatibleDestImage(src, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            dstCM = srcCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            origDst = dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            dstCM = dst.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (srcCM.getColorSpace().getType() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                dstCM.getColorSpace().getType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                needToConvert = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                dst = createCompatibleDestImage(src, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                dstCM = dst.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            else if (dstCM instanceof IndexColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                dst = createCompatibleDestImage(src, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                dstCM = dst.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (ImagingLib.filter(this, src, dst) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new ImagingOpException ("Unable to convolve src image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (needToConvert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            ColorConvertOp ccop = new ColorConvertOp(hints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            ccop.filter(dst, origDst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        else if (origDst != dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            java.awt.Graphics2D g = origDst.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                g.drawImage(dst, 0, 0, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return origDst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Performs a convolution on Rasters.  Each band of the source Raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * will be convolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * The source and destination must have the same number of bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * If the destination Raster is null, a new Raster will be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * The IllegalArgumentException may be thrown if the source is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * the same as the destination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param src the source <code>Raster</code> to filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param dst the destination <code>WritableRaster</code> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *        filtered <code>src</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @return the filtered <code>WritableRaster</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @throws NullPointerException if <code>src</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @throws ImagingOpException if <code>src</code> and <code>dst</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *         do not have the same number of bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @throws ImagingOpException if <code>src</code> cannot be filtered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @throws IllegalArgumentException if <code>src</code> equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *         <code>dst</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public final WritableRaster filter (Raster src, WritableRaster dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (dst == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            dst = createCompatibleDestRaster(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        else if (src == dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new IllegalArgumentException("src image cannot be the "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                               "same as the dst image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        else if (src.getNumBands() != dst.getNumBands()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            throw new ImagingOpException("Different number of bands in src "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                         " and dst Rasters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (ImagingLib.filter(this, src, dst) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new ImagingOpException ("Unable to convolve src image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Creates a zeroed destination image with the correct size and number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * of bands.  If destCM is null, an appropriate ColorModel will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param src       Source image for the filter operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param destCM    ColorModel of the destination.  Can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @return a destination <code>BufferedImage</code> with the correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *         size and number of bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public BufferedImage createCompatibleDestImage(BufferedImage src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                                   ColorModel destCM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        BufferedImage image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        int w = src.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        int h = src.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        WritableRaster wr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (destCM == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            destCM = src.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            // Not much support for ICM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (destCM instanceof IndexColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                destCM = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                /* Create destination image as similar to the source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                 *  as it possible...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                wr = src.getData().createCompatibleWritableRaster(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (wr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            /* This is the case when destination color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
             * was explicitly specified (and it may be not compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
             * with source raster structure) or source is indexed image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
             * We should use destination color model to create compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
             * destination raster here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            wr = destCM.createCompatibleWritableRaster(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        image = new BufferedImage (destCM, wr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                   destCM.isAlphaPremultiplied(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return image;
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
     * Creates a zeroed destination Raster with the correct size and number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * of bands, given this source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public WritableRaster createCompatibleDestRaster(Raster src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return src.createCompatibleWritableRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Returns the bounding box of the filtered destination image.  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * this is not a geometric operation, the bounding box does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public final Rectangle2D getBounds2D(BufferedImage src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return getBounds2D(src.getRaster());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Returns the bounding box of the filtered destination Raster.  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * this is not a geometric operation, the bounding box does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public final Rectangle2D getBounds2D(Raster src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return src.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Returns the location of the destination point given a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * point in the source.  If dstPt is non-null, it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * be used to hold the return value.  Since this is not a geometric
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * operation, the srcPt will equal the dstPt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public final Point2D getPoint2D(Point2D srcPt, Point2D dstPt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (dstPt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            dstPt = new Point2D.Float();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        dstPt.setLocation(srcPt.getX(), srcPt.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return dstPt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Returns the rendering hints for this op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public final RenderingHints getRenderingHints() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return hints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
}