KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > image > Kernel


1 /*
2  * @(#)Kernel.java 1.22 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.image;
9
10
11 /**
12  * The <code>Kernel</code> class defines a matrix that describes how a
13  * specified pixel and its surrounding pixels affect the value
14  * computed for the pixel's position in the output image of a filtering
15  * operation. The X origin and Y origin indicate the kernel matrix element
16  * that corresponds to the pixel position for which an output value is
17  * being computed.
18  *
19  * @see ConvolveOp
20  * @version 10 Feb 1997
21  */

22 public class Kernel implements Cloneable JavaDoc {
23     private int width;
24     private int height;
25     private int xOrigin;
26     private int yOrigin;
27     private float data[];
28
29     private static native void initIDs();
30     static {
31         ColorModel.loadLibraries();
32         initIDs();
33     }
34
35     /**
36      * Constructs a <code>Kernel</code> object from an array of floats.
37      * The first <code>width</code>*<code>height</code> elements of
38      * the <code>data</code> array are copied.
39      * If the length of the <code>data</code> array is less
40      * than width*height, an <code>IllegalArgumentException</code> is thrown.
41      * The X origin is (width-1)/2 and the Y origin is (height-1)/2.
42      * @param width width of the kernel
43      * @param height height of the kernel
44      * @param data kernel data in row major order
45      * @throws IllegalArgumentException if the length of <code>data</code>
46      * is less than the product of <code>width</code> and
47      * <code>height</code>
48      */

49     public Kernel(int width, int height, float data[]) {
50         this.width = width;
51         this.height = height;
52         this.xOrigin = (width-1)>>1;
53         this.yOrigin = (height-1)>>1;
54         int len = width*height;
55         if (data.length < len) {
56             throw new IllegalArgumentException JavaDoc("Data array too small "+
57                                                "(is "+data.length+
58                                                " and should be "+len);
59         }
60         this.data = new float[len];
61         System.arraycopy(data, 0, this.data, 0, len);
62
63     }
64
65     /**
66      * Returns the X origin of this <code>Kernel</code>.
67      * @return the X origin.
68      */

69     final public int getXOrigin(){
70         return xOrigin;
71     }
72
73     /**
74      * Returns the Y origin of this <code>Kernel</code>.
75      * @return the Y origin.
76      */

77     final public int getYOrigin() {
78         return yOrigin;
79     }
80
81     /**
82      * Returns the width of this <code>Kernel</code>.
83      * @return the width of this <code>Kernel</code>.
84      */

85     final public int getWidth() {
86         return width;
87     }
88
89     /**
90      * Returns the height of this <code>Kernel</code>.
91      * @return the height of this <code>Kernel</code>.
92      */

93     final public int getHeight() {
94         return height;
95     }
96
97     /**
98      * Returns the kernel data in row major order.
99      * The <code>data</code> array is returned. If <code>data</code>
100      * is <code>null</code>, a new array is allocated.
101      * @param data if non-null, contains the returned kernel data
102      * @return the <code>data</code> array containing the kernel data
103      * in row major order or, if <code>data</code> is
104      * <code>null</code>, a newly allocated array containing
105      * the kernel data in row major order
106      * @throws IllegalArgumentException if <code>data</code> is less
107      * than the size of this <code>Kernel</code>
108      */

109     final public float[] getKernelData(float[] data) {
110         if (data == null) {
111             data = new float[this.data.length];
112         }
113         else if (data.length < this.data.length) {
114             throw new IllegalArgumentException JavaDoc("Data array too small "+
115                                                "(should be "+this.data.length+
116                                                " but is "+
117                                                data.length+" )");
118         }
119         System.arraycopy(this.data, 0, data, 0, this.data.length);
120
121         return data;
122     }
123
124     /**
125      * Clones this object.
126      * @return a clone of this object.
127      */

128     public Object JavaDoc clone() {
129     try {
130         return super.clone();
131     } catch (CloneNotSupportedException JavaDoc e) {
132         // this shouldn't happen, since we are Cloneable
133
throw new InternalError JavaDoc();
134     }
135     }
136 }
137
138
139
140
141
142
143
Popular Tags