001    /*
002     * ConvolutionKernelData
003     * 
004     * Copyright (c) 2001, 2002 Marco Schmidt.
005     * All rights reserved.
006     */
007    
008    package net.sourceforge.jiu.filters;
009    
010    /**
011     * This class encapsulates the information for a specific convolution kernel filter.
012     * An object of this class is used in combination with {@link ConvolutionKernelFilter}.
013     * Several kernel data objects are predefined in that class.
014     *
015     * @author Marco Schmidt
016     * @see ConvolutionKernelFilter
017     */
018    public class ConvolutionKernelData
019    {
020            private int[] data;
021            private int width;
022            private int height;
023            private int div;
024            private int bias;
025            private String name;
026    
027            /**
028             * Creates a new kernel from the arguments.
029             * Calls the various set methods to actually store these arguments.
030             */
031            public ConvolutionKernelData(String name, int[] data, int width, int height, int div, int bias)
032            {
033                    setName(name);
034                    setData(data);
035                    setWidth(width);
036                    setHeight(height);
037                    setDiv(div);
038                    setBias(bias);
039                    check();
040            }
041    
042            /**
043             * Checks if this kernel's data is valid and throws an IllegalArgumentException if anything
044             * is wrong.
045             * Otherwise, does nothing.
046             */
047            public void check()
048            {
049                    if (data.length < width * height)
050                    {
051                            throw new IllegalArgumentException("Kernel data array must have at least width * height elements.");
052                    }
053            }
054    
055            /**
056             * Returns this kernel's bias value.
057             * See {@link ConvolutionKernelFilter} for an explanation of this and other kernel properties.
058             * @see #setBias
059             */
060            public int getBias()
061            {
062                    return bias;
063            }
064    
065            /**
066             * Returns this kernel's div value.
067             * Must not be <code>0</code>.
068             * See {@link ConvolutionKernelFilter} for an explanation of this and other kernel properties.
069             * @see #setDiv
070             */
071            public int getDiv()
072            {
073                    return div;
074            }
075    
076            /**
077             * Returns the kernel data.
078             * See {@link ConvolutionKernelFilter} for an explanation of this and other kernel properties.
079             * @see #setData
080             */
081            public int[] getData()
082            {
083                    return data;
084            }
085    
086            /**
087             * Returns this kernel's height, an odd positive number.
088             * See {@link ConvolutionKernelFilter} for an explanation of this and other kernel properties.
089             */
090            public int getHeight()
091            {
092                    return height;
093            }
094    
095            /**
096             * Returns this kernel's name.
097             */
098            public String getName()
099            {
100                    return name;
101            }
102    
103            /**
104             * Returns this kernel's width, an odd positive number.
105             * See {@link ConvolutionKernelFilter} for an explanation of this and other kernel properties.
106             */
107            public int getWidth()
108            {
109                    return width;
110            }
111    
112            /**
113             * Set new bias value.
114             * See {@link ConvolutionKernelFilter} for an explanation of this and other kernel properties.
115             */
116            public void setBias(int newBias)
117            {
118                    bias = newBias;
119            }
120    
121            /**
122             * Sets the data array to be used in this kernel.
123             * Must have at least getWidth() times getHeight() elements - however,
124             * this constraint is not checked in this method (setting
125             * width and height may happen later).
126             * Call {@link #check} 
127             * @param newData
128             */
129            public void setData(int[] newData)
130            {
131                    if (newData == null)
132                    {
133                            throw new IllegalArgumentException("The data array must not be null.");
134                    }
135                    if (newData.length < 1)
136                    {
137                            throw new IllegalArgumentException("The data array must have a length of at least 1.");
138                    }
139                    data = newData;
140            }
141    
142            public void setDiv(int newDiv)
143            {
144                    if (newDiv == 0)
145                    {
146                            throw new IllegalArgumentException("Div value must not be 0.");
147                    }
148                    div = newDiv;
149            }
150    
151            public void setHeight(int newHeight)
152            {
153                    if (newHeight < 1)
154                    {
155                            throw new IllegalArgumentException("Height must be 1 or larger.");
156                    }
157                    if ((newHeight % 2) == 0)
158                    {
159                            throw new IllegalArgumentException("Height must not be an even number.");
160                    }
161                    height = newHeight;
162            }
163    
164            public void setName(String newName)
165            {
166                    name = newName;
167            }
168    
169            public void setWidth(int newWidth)
170            {
171                    if (newWidth < 1)
172                    {
173                            throw new IllegalArgumentException("Width must be 1 or larger.");
174                    }
175                    if ((newWidth % 2) == 0)
176                    {
177                            throw new IllegalArgumentException("Width must not be an even number.");
178                    }
179                    width = newWidth;
180            }
181    }