summaryrefslogtreecommitdiffstats
path: root/awt/java/awt/image/Kernel.java
blob: c6f00e2b8b5eec67e7b47029da97759bc44ace9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * @author Oleg V. Khaschansky
 * @version $Revision$
 *
 * @date: Sep 28, 2005
 */

package java.awt.image;

import org.apache.harmony.awt.internal.nls.Messages;

/**
 * The Kernel class provides a matrix. This matrix is stored as a float array
 * which describes how a specified pixel affects the value calculated for 
 * the pixel's position in the output image of a filtering operation. 
 * The X, Y origins indicate the kernel matrix element which corresponds to 
 * the pixel position for which an output value is being calculated.
 */
public class Kernel implements Cloneable {
    
    /** The x origin. */
    private final int xOrigin;
    
    /** The y origin. */
    private final int yOrigin;
    
    /** The width. */
    private int width;
    
    /** The height. */
    private int height;
    
    /** The data. */
    float data[];

    /**
     * Instantiates a new Kernel with the specified float array.
     * The width*height elements of the data array are copied. 
     * 
     * @param width the width of the Kernel.
     * @param height the height of the Kernel.
     * @param data the data of Kernel.
     */
    public Kernel(int width, int height, float[] data) {
        int dataLength = width*height;
        if (data.length < dataLength) {
            // awt.22B=Length of data should not be less than width*height
            throw new IllegalArgumentException(Messages.getString("awt.22B")); //$NON-NLS-1$
        }

        this.width = width;
        this.height = height;

        this.data = new float[dataLength];
        System.arraycopy(data, 0, this.data, 0, dataLength);

        xOrigin = (width-1)/2;
        yOrigin = (height-1)/2;
    }

    /**
     * Gets the width of this Kernel.
     * 
     * @return the width of this Kernel.
     */
    public final int getWidth() {
        return width;
    }

    /**
     * Gets the height of this Kernel.
     * 
     * @return the height of this Kernel.
     */
    public final int getHeight() {
        return height;
    }

    /**
     * Gets the float data array of this Kernel.
     * 
     * @param data the float array where the resulted data will be stored.
     * 
     * @return the float data array of this Kernel.
     */
    public final float[] getKernelData(float[] data) {
        if (data == null) {
            data = new float[this.data.length];
        }
        System.arraycopy(this.data, 0, data, 0, this.data.length);

        return data;
    }

    /**
     * Gets the X origin of this Kernel.
     * 
     * @return the X origin of this Kernel.
     */
    public final int getXOrigin() {
        return xOrigin;
    }

    /**
     * Gets the Y origin of this Kernel.
     * 
     * @return the Y origin of this Kernel.
     */
    public final int getYOrigin() {
        return yOrigin;
    }

    /**
     * Returns a copy of this Kernel object.
     * 
     * @return the copy of this Kernel object.
     */
    @Override
    public Object clone() {
        return new Kernel(width, height, data);
    }
}