Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / datastruct / Kernel.java @ 2443

History | View | Annotate | Download (5.41 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.impl.datastruct;
23

    
24
import org.gvsig.tools.ToolsLocator;
25
import org.gvsig.tools.dynobject.DynStruct;
26
import org.gvsig.tools.persistence.PersistenceManager;
27
import org.gvsig.tools.persistence.Persistent;
28
import org.gvsig.tools.persistence.PersistentState;
29
import org.gvsig.tools.persistence.exception.PersistenceException;
30

    
31

    
32
/**
33
 * Clase que representa un kernel de NxN p?xeles para realizar operaciones sobre
34
 * un pixel.
35
 * @author Nacho Brodin (nachobrodin@gmail.com)
36
 */
37
public class Kernel implements Persistent {
38
        public double[][]        kernel        = null;
39
        protected double        divisor = 0;
40

    
41
        /**
42
         * This constructor is useful only for persistence
43
         */
44
        public Kernel() {
45
        }
46
        
47
        /**
48
         * Constructor. Crea la matriz de datos para el kernel.
49
         * @param k datos del kernel
50
         */
51
        public Kernel(double[][] k) {
52
                this.kernel = k;
53

    
54
                for (int i = 0; i < kernel.length; i++)
55
                        for (int j = 0; j < kernel[0].length; j++)
56
                                divisor = divisor + kernel[i][j];
57
        }
58

    
59
        /**
60
         * Constructor. Crea la matriz de datos para el kernel.
61
         * @param k datos del kernel
62
         */
63
        public Kernel(double[][] k, double divisor) {
64
                this.kernel = k;
65
                this.divisor = divisor;
66
        }
67

    
68
        public double kernelOperation(Kernel k) {
69
                double res = 0;
70
                for (int i = 0; i < kernel.length; i++)
71
                        for (int j = 0; j < kernel[0].length; j++)
72
                                res += kernel[i][j] * k.kernel[i][j];
73
                return res;
74
        }
75

    
76
        /**
77
         * Aplica la operaci?n de convoluci?n del kernel con otro kernel
78
         * pasado por par?metro
79
         * @param k
80
         * @return
81
         */
82
        public double convolution(Kernel k) {
83
                double res = this.kernelOperation(k);
84
                if (this.divisor != 0)
85
                        res = res / divisor;
86
                //return Math.abs(res);
87
                return res;
88
        }
89

    
90
        public double getDivisor() {
91
                return divisor;
92
        }
93

    
94
        public void setDivisor(double divisor) {
95
                this.divisor = divisor;
96
        }
97

    
98
        /**
99
         * Obtiene el tama?o del kernel que viene dado por
100
         * el n?mero de pixeles de su lado.
101
         * @return
102
         */
103
        public int getLado() {
104
                return kernel.length;
105
        }
106

    
107
        /**
108
         * Aplica ls operaci?n 0xff para todos los elementos del
109
         * kernel. Presupone que este es de tipo byte y no hace ninguna
110
         * comprobaci?n al respecto. Se deja en manos del usuario aplicar esta
111
         * operaci?n solo cuando los elementos del kernel sean de este tipo de dato.
112
         */
113
        public void rgbNormalization() {
114
                for (int i = 0; i < kernel.length; i++)
115
                        for (int j = 0; j < kernel[0].length; j++)
116
                                kernel[i][j] = ((byte)kernel[i][j]) & 0xff;
117
        }
118
        
119
        public void loadFromState(PersistentState state)
120
                        throws PersistenceException {
121
                int side = state.getInt("side");
122
                kernel = new double[side][side];
123
                if(side >= 3) {
124
                        kernel[0] = (double[])state.getDoubleArray("row_0");
125
                        kernel[1] = (double[])state.getDoubleArray("row_1");
126
                        kernel[2] = (double[])state.getDoubleArray("row_2");
127
                }
128
                if(side >= 5) {
129
                        kernel[3] = (double[])state.getDoubleArray("row_3");
130
                        kernel[4] = (double[])state.getDoubleArray("row_4");
131
                }
132
                if(side >= 7) {
133
                        kernel[5] = (double[])state.getDoubleArray("row_5");
134
                        kernel[6] = (double[])state.getDoubleArray("row_6");
135
                }
136
        }
137
        
138
        public void saveToState(PersistentState state) throws PersistenceException {
139
                if(kernel == null)
140
                        return;
141

    
142
                state.set("row_0", kernel[0]);
143
                state.set("row_1", kernel[1]);
144
                state.set("row_2", kernel[2]);
145
                
146
                if(kernel.length == 3) {
147
                        state.set("side", 3);
148
                        return;
149
                }
150

    
151
                state.set("row_3", kernel[3]);
152
                state.set("row_4", kernel[4]);
153
                
154
                if(kernel.length == 5) {
155
                        state.set("side", 5);
156
                        return;
157
                }
158

    
159
                state.set("row_5", kernel[5]);
160
                state.set("row_6", kernel[6]);
161
                
162
                if(kernel.length == 7) {
163
                        state.set("side", 7);
164
                        return;
165
                }
166

    
167
        }        
168

    
169
        public static void registerPersistence() {
170
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
171
                DynStruct definition = manager.getDefinition("Kernel_Persistent");
172
                if( definition == null ) {
173
                        definition = manager.addDefinition(
174
                                        Kernel.class,
175
                                        "Kernel_Persistent",
176
                                        "Kernel Persistency",
177
                                        null, 
178
                                        null
179
                        );
180
                }
181
                
182
                definition.addDynFieldInt("side").setMandatory(false);
183
                definition.addDynFieldList("row_0").setClassOfItems(double.class).setMandatory(false);
184
                definition.addDynFieldList("row_1").setClassOfItems(double.class).setMandatory(false);
185
                definition.addDynFieldList("row_2").setClassOfItems(double.class).setMandatory(false);
186
                definition.addDynFieldList("row_3").setClassOfItems(double.class).setMandatory(false);
187
                definition.addDynFieldList("row_4").setClassOfItems(double.class).setMandatory(false);
188
                definition.addDynFieldList("row_5").setClassOfItems(double.class).setMandatory(false);
189
                definition.addDynFieldList("row_6").setClassOfItems(double.class).setMandatory(false);
190
        }
191
}