jdk/src/share/classes/java/awt/image/Kernel.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
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, 1999, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The <code>Kernel</code> class defines a matrix that describes how a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * specified pixel and its surrounding pixels affect the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * computed for the pixel's position in the output image of a filtering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * operation.  The X origin and Y origin indicate the kernel matrix element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * that corresponds to the pixel position for which an output value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * being computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @see ConvolveOp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class Kernel implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private int  width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private int  height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private int  xOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private int  yOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private float data[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        ColorModel.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Constructs a <code>Kernel</code> object from an array of floats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * The first <code>width</code>*<code>height</code> elements of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * the <code>data</code> array are copied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * If the length of the <code>data</code> array is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * than width*height, an <code>IllegalArgumentException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * The X origin is (width-1)/2 and the Y origin is (height-1)/2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param width         width of the kernel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param height        height of the kernel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param data          kernel data in row major order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @throws IllegalArgumentException if the length of <code>data</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *         is less than the product of <code>width</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *         <code>height</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public Kernel(int width, int height, float data[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.width  = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.xOrigin  = (width-1)>>1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.yOrigin  = (height-1)>>1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        int len = width*height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (data.length < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new IllegalArgumentException("Data array too small "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                               "(is "+data.length+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                               " and should be "+len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this.data = new float[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        System.arraycopy(data, 0, this.data, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Returns the X origin of this <code>Kernel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @return the X origin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    final public int getXOrigin(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return xOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Returns the Y origin of this <code>Kernel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @return the Y origin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    final public int getYOrigin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return yOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Returns the width of this <code>Kernel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @return the width of this <code>Kernel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    final public int getWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Returns the height of this <code>Kernel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @return the height of this <code>Kernel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    final public int getHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Returns the kernel data in row major order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * The <code>data</code> array is returned.  If <code>data</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * is <code>null</code>, a new array is allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param data  if non-null, contains the returned kernel data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @return the <code>data</code> array containing the kernel data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *         in row major order or, if <code>data</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *         <code>null</code>, a newly allocated array containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *         the kernel data in row major order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws IllegalArgumentException if <code>data</code> is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *         than the size of this <code>Kernel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    final public float[] getKernelData(float[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (data == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            data = new float[this.data.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        else if (data.length < this.data.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new IllegalArgumentException("Data array too small "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                               "(should be "+this.data.length+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                               " but is "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                               data.length+" )");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        System.arraycopy(this.data, 0, data, 0, this.data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Clones this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @return a clone of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            // this shouldn't happen, since we are Cloneable
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   150
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}