001 /*
002 * Copyright 1997-1999 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt.image;
027
028 /**
029 * The <code>Kernel</code> class defines a matrix that describes how a
030 * specified pixel and its surrounding pixels affect the value
031 * computed for the pixel's position in the output image of a filtering
032 * operation. The X origin and Y origin indicate the kernel matrix element
033 * that corresponds to the pixel position for which an output value is
034 * being computed.
035 *
036 * @see ConvolveOp
037 * @version 10 Feb 1997
038 */
039 public class Kernel implements Cloneable {
040 private int width;
041 private int height;
042 private int xOrigin;
043 private int yOrigin;
044 private float data[];
045
046 private static native void initIDs();
047
048 static {
049 ColorModel.loadLibraries();
050 initIDs();
051 }
052
053 /**
054 * Constructs a <code>Kernel</code> object from an array of floats.
055 * The first <code>width</code>*<code>height</code> elements of
056 * the <code>data</code> array are copied.
057 * If the length of the <code>data</code> array is less
058 * than width*height, an <code>IllegalArgumentException</code> is thrown.
059 * The X origin is (width-1)/2 and the Y origin is (height-1)/2.
060 * @param width width of the kernel
061 * @param height height of the kernel
062 * @param data kernel data in row major order
063 * @throws IllegalArgumentException if the length of <code>data</code>
064 * is less than the product of <code>width</code> and
065 * <code>height</code>
066 */
067 public Kernel(int width, int height, float data[]) {
068 this .width = width;
069 this .height = height;
070 this .xOrigin = (width - 1) >> 1;
071 this .yOrigin = (height - 1) >> 1;
072 int len = width * height;
073 if (data.length < len) {
074 throw new IllegalArgumentException("Data array too small "
075 + "(is " + data.length + " and should be " + len);
076 }
077 this .data = new float[len];
078 System.arraycopy(data, 0, this .data, 0, len);
079
080 }
081
082 /**
083 * Returns the X origin of this <code>Kernel</code>.
084 * @return the X origin.
085 */
086 final public int getXOrigin() {
087 return xOrigin;
088 }
089
090 /**
091 * Returns the Y origin of this <code>Kernel</code>.
092 * @return the Y origin.
093 */
094 final public int getYOrigin() {
095 return yOrigin;
096 }
097
098 /**
099 * Returns the width of this <code>Kernel</code>.
100 * @return the width of this <code>Kernel</code>.
101 */
102 final public int getWidth() {
103 return width;
104 }
105
106 /**
107 * Returns the height of this <code>Kernel</code>.
108 * @return the height of this <code>Kernel</code>.
109 */
110 final public int getHeight() {
111 return height;
112 }
113
114 /**
115 * Returns the kernel data in row major order.
116 * The <code>data</code> array is returned. If <code>data</code>
117 * is <code>null</code>, a new array is allocated.
118 * @param data if non-null, contains the returned kernel data
119 * @return the <code>data</code> array containing the kernel data
120 * in row major order or, if <code>data</code> is
121 * <code>null</code>, a newly allocated array containing
122 * the kernel data in row major order
123 * @throws IllegalArgumentException if <code>data</code> is less
124 * than the size of this <code>Kernel</code>
125 */
126 final public float[] getKernelData(float[] data) {
127 if (data == null) {
128 data = new float[this .data.length];
129 } else if (data.length < this .data.length) {
130 throw new IllegalArgumentException("Data array too small "
131 + "(should be " + this .data.length + " but is "
132 + data.length + " )");
133 }
134 System.arraycopy(this .data, 0, data, 0, this .data.length);
135
136 return data;
137 }
138
139 /**
140 * Clones this object.
141 * @return a clone of this object.
142 */
143 public Object clone() {
144 try {
145 return super .clone();
146 } catch (CloneNotSupportedException e) {
147 // this shouldn't happen, since we are Cloneable
148 throw new InternalError();
149 }
150 }
151 }
|