Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.library / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer.impl / src / main / java / org / gvsig / raster / lib / buffer / impl / DefaultKernel.java @ 43803

History | View | Annotate | Download (5.84 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.lib.buffer.impl;
23

    
24
import org.gvsig.raster.lib.buffer.api.Kernel;
25
import org.gvsig.tools.ToolsLocator;
26
import org.gvsig.tools.dynobject.DynStruct;
27
import org.gvsig.tools.persistence.PersistenceManager;
28
import org.gvsig.tools.persistence.Persistent;
29
import org.gvsig.tools.persistence.PersistentState;
30
import org.gvsig.tools.persistence.exception.PersistenceException;
31

    
32

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

    
43
        /**
44
         * This constructor is useful only for persistence
45
         */
46
        public DefaultKernel() {
47
        }
48

    
49
        /**
50
         * Constructor. Crea la matriz de datos para el kernel.
51
         * @param k datos del kernel
52
         */
53
        public DefaultKernel(double[][] k) {
54
                this.kernel = k;
55

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

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

    
70

    
71
        @Override
72
        public double[][] getKernel() {
73
            return this.kernel;
74
        }
75

    
76
        /* (non-Javadoc)
77
         * @see org.gvsig.raster.lib.buffer.impl.operations.Kernel#getDivisor()
78
         */
79
        @Override
80
        public double getDivisor() {
81
            return divisor;
82
        }
83

    
84
        /* (non-Javadoc)
85
         * @see org.gvsig.raster.lib.buffer.impl.operations.Kernel#setDivisor(double)
86
         */
87
        @Override
88
        public void setDivisor(double divisor) {
89
            this.divisor = divisor;
90
        }
91

    
92
        /* (non-Javadoc)
93
         * @see org.gvsig.raster.lib.buffer.impl.operations.Kernel#getLado()
94
         */
95
        @Override
96
        public int getSide() {
97
            return kernel.length;
98
        }
99

    
100
        /* (non-Javadoc)
101
     * @see org.gvsig.raster.lib.buffer.impl.operations.Kernel#kernelOperation(org.gvsig.raster.lib.buffer.impl.operations.DefaultKernel)
102
     */
103
        @Override
104
    public double kernelOperation(Kernel k) {
105
                double res = 0;
106
                for (int i = 0; i < kernel.length; i++)
107
                        for (int j = 0; j < kernel[0].length; j++)
108
                                res += kernel[i][j] * k.getKernel()[i][j];
109
                return res;
110
        }
111

    
112
        /* (non-Javadoc)
113
     * @see org.gvsig.raster.lib.buffer.impl.operations.Kernel#convolution(org.gvsig.raster.lib.buffer.impl.operations.DefaultKernel)
114
     */
115
        @Override
116
    public double convolution(Kernel k) {
117
                double res = this.kernelOperation(k);
118
                if (this.divisor != 0)
119
                        res = res / divisor;
120
                return res;
121
        }
122

    
123

    
124
        /* (non-Javadoc)
125
     * @see org.gvsig.raster.lib.buffer.impl.operations.Kernel#rgbNormalization()
126
     */
127
        @Override
128
    public void rgbNormalization() {
129
                for (int i = 0; i < kernel.length; i++)
130
                        for (int j = 0; j < kernel[0].length; j++)
131
                                kernel[i][j] = ((byte)kernel[i][j]) & 0xff;
132
        }
133

    
134
        public void loadFromState(PersistentState state)
135
                        throws PersistenceException {
136
                int side = state.getInt("side");
137
                kernel = new double[side][side];
138
                if(side >= 3) {
139
                        kernel[0] = (double[])state.getDoubleArray("row_0");
140
                        kernel[1] = (double[])state.getDoubleArray("row_1");
141
                        kernel[2] = (double[])state.getDoubleArray("row_2");
142
                }
143
                if(side >= 5) {
144
                        kernel[3] = (double[])state.getDoubleArray("row_3");
145
                        kernel[4] = (double[])state.getDoubleArray("row_4");
146
                }
147
                if(side >= 7) {
148
                        kernel[5] = (double[])state.getDoubleArray("row_5");
149
                        kernel[6] = (double[])state.getDoubleArray("row_6");
150
                }
151
        }
152

    
153
        public void saveToState(PersistentState state) throws PersistenceException {
154
                if(kernel == null)
155
                        return;
156

    
157
                state.set("row_0", kernel[0]);
158
                state.set("row_1", kernel[1]);
159
                state.set("row_2", kernel[2]);
160

    
161
                if(kernel.length == 3) {
162
                        state.set("side", 3);
163
                        return;
164
                }
165

    
166
                state.set("row_3", kernel[3]);
167
                state.set("row_4", kernel[4]);
168

    
169
                if(kernel.length == 5) {
170
                        state.set("side", 5);
171
                        return;
172
                }
173

    
174
                state.set("row_5", kernel[5]);
175
                state.set("row_6", kernel[6]);
176

    
177
                if(kernel.length == 7) {
178
                        state.set("side", 7);
179
                        return;
180
                }
181

    
182
        }
183

    
184
        public static void registerPersistence() {
185
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
186
                DynStruct definition = manager.getDefinition("Kernel_Persistent");
187
                if( definition == null ) {
188
                        definition = manager.addDefinition(
189
                                        DefaultKernel.class,
190
                                        "Kernel_Persistent",
191
                                        "Kernel Persistency",
192
                                        null,
193
                                        null
194
                        );
195
                }
196

    
197
                definition.addDynFieldInt("side").setMandatory(false);
198
                definition.addDynFieldList("row_0").setClassOfItems(double.class).setMandatory(false);
199
                definition.addDynFieldList("row_1").setClassOfItems(double.class).setMandatory(false);
200
                definition.addDynFieldList("row_2").setClassOfItems(double.class).setMandatory(false);
201
                definition.addDynFieldList("row_3").setClassOfItems(double.class).setMandatory(false);
202
                definition.addDynFieldList("row_4").setClassOfItems(double.class).setMandatory(false);
203
                definition.addDynFieldList("row_5").setClassOfItems(double.class).setMandatory(false);
204
                definition.addDynFieldList("row_6").setClassOfItems(double.class).setMandatory(false);
205
        }
206
}