Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / RasterFilter.java @ 15778

History | View | Annotate | Download (7.93 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.raster.grid.filter;
20

    
21
import java.util.Hashtable;
22
import java.util.TreeMap;
23

    
24
import org.gvsig.raster.dataset.IBuffer;
25
import org.gvsig.raster.dataset.Params;
26
import org.gvsig.raster.datastruct.Extent;
27
/**
28
 * Filtro para raster. Ancestro de todos los filtros.
29
 *
30
 * @author Nacho Brodin (nachobrodin@gmail.com)
31
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
32
 */
33
public abstract class RasterFilter implements IRasterFilter, Cloneable {
34
        protected IBuffer                 raster = null;
35
        protected int                         height = 0;
36
        protected int                         width = 0;
37
        protected Hashtable     params = new Hashtable();
38
        protected TreeMap       environment = new TreeMap();
39
        protected Extent                extent = null;
40
        private int                                 percent = 0;
41
        private boolean                        canceled = false;
42
        private String                        fName = "";
43
        /**
44
         * Variable que control la aplicaci?n o no del filtro. Si est? a false aunque est? en
45
         * la pila el filtro no se ejecutar?.
46
         */
47
        protected boolean                 exec = true;
48

    
49
        /**
50
         * Clase que representa un kernel de NxN p?xeles para realizar operaciones sobre
51
         * un pixel.
52
         * @author Nacho Brodin (nachobrodin@gmail.com)
53
         */
54
        public static class Kernel {
55
                public double[][]        kernel        = null;
56
                protected double        divisor = 0;
57

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

    
65
                        for (int i = 0; i < kernel.length; i++)
66
                                for (int j = 0; j < kernel[0].length; j++)
67
                                        divisor = divisor + kernel[i][j];
68
                }
69

    
70
                /**
71
                 * Constructor. Crea la matriz de datos para el kernel.
72
                 * @param k datos del kernel
73
                 */
74
                public Kernel(double[][] k, double divisor) {
75
                        this.kernel = k;
76
                        this.divisor = divisor;
77
                }
78

    
79
                public double kernelOperation(Kernel k) {
80
                        double res = 0;
81
                        for (int i = 0; i < kernel.length; i++)
82
                                for (int j = 0; j < kernel[0].length; j++)
83
                                        res += kernel[i][j] * k.kernel[i][j];
84
                        return res;
85
                }
86

    
87
                /**
88
                 * Aplica la operaci?n de convoluci?n del kernel con otro kernel
89
                 * pasado por par?metro
90
                 * @param k
91
                 * @return
92
                 */
93
                public double convolution(Kernel k) {
94
                        double res = this.kernelOperation(k);
95
                        if (this.divisor != 0)
96
                                res = res / divisor;
97
                        //return Math.abs(res);
98
                        return res;
99
                }
100

    
101
                public double getDivisor() {
102
                        return divisor;
103
                }
104

    
105
                public void setDivisor(double divisor) {
106
                        this.divisor = divisor;
107
                }
108

    
109
                /**
110
                 * Obtiene el tama?o del kernel que viene dado por
111
                 * el n?mero de pixeles de su lado.
112
                 * @return
113
                 */
114
                public int getLado() {
115
                        return kernel.length;
116
                }
117
                
118
                /**
119
                 * Aplica ls operaci?n 0xff para todos los elementos del 
120
                 * kernel. Presupone que este es de tipo byte y no hace ninguna
121
                 * comprobaci?n al respecto. Se deja en manos del usuario aplicar esta
122
                 * operaci?n solo cuando los elementos del kernel sean de este tipo de dato.
123
                 */
124
                public void rgbNormalization() {
125
                        for (int i = 0; i < kernel.length; i++)
126
                                for (int j = 0; j < kernel[0].length; j++)
127
                                        kernel[i][j] = ((byte)kernel[i][j]) & 0xff;
128
                }
129
        }
130

    
131
        /**
132
         * Constructor
133
         */
134
        public RasterFilter() {
135
        }
136

    
137
        /**
138
         * Aplica el filtro sobre el raster pasado pixel a pixel
139
         * @throws InterruptedException
140
         */
141
        public void execute() throws InterruptedException {
142
                pre();
143
                percent = 0;
144
                if (exec) {
145
                        for (int row = 0; row < height; row ++) {
146
                                for (int col = 0; col < width; col ++)
147
                                        process(col, row);
148
                                if (Thread.currentThread().isInterrupted())
149
                                        throw new InterruptedException("Process execute Filter");
150
                                percent = (row * 100) / height;
151
                        }
152
                }
153
                if (isCanceled())
154
                        return;
155
                percent = 100;
156
                post();
157
        }
158

    
159
        /**
160
         * A?ade un par?metro al filtro
161
         *
162
         * @param name Clave del par?metro
163
         * @param param Objeto pasado como par?metro
164
         */
165
        public void addParam(String name, Object param) {
166
                if (param != null)
167
                        params.put(name, param);
168
                else
169
                        params.remove(name);
170
        }
171

    
172
        /**
173
         * Elimina un par?metro del filtro
174
         * @param name Clave del par?metro a eliminar
175
         */
176
        public void removeParam(String name) {
177
                params.remove(name);
178
        }
179

    
180
        /**
181
         * Obtiene un par?metro a partir de la clave
182
         * @param name Par?metro
183
         * @return Par?metro
184
         */
185
        public Object getParam(String name) {
186
                return params.get(name);
187
        }
188

    
189
                /**
190
         * @param extent The extent to set.
191
         */
192
        public void setExtent(Extent extent) {
193
                this.extent = extent;
194
        }
195

    
196
        /**
197
         * Obtiene true si el filtro va a ser ejecutado o false si no va a serlo
198
         * @return
199
         */
200
        public boolean isExec() {
201
                return exec;
202
        }
203

    
204
        /**
205
         * Asigna el valor a la variable exec. Esta estar? a true si el filtro se ejecutar? la pr?xima
206
         * vez que se repinte o false si no se ejecuta.
207
         * @param exec
208
         */
209
        public void setExec(boolean exec) {
210
                this.exec = exec;
211
        }
212

    
213
        /**
214
         * Establece si se ha de cancelar un proceso de filtrado o no.
215
         * @param value
216
         */
217
        public void setCanceled(boolean value) {
218
                canceled = value;
219
        }
220

    
221
        /**
222
         * Devuelve si se ha cancelado el proceso de filtrar.
223
         * @return
224
         */
225
        public boolean isCanceled() {
226
                return canceled;
227
        }
228

    
229
        /**
230
         * Pone a cero el contador del porcentaje del proceso de filtrado
231
         * @return
232
         */
233
        public void resetPercent() {
234
                percent = 0;
235
        }
236

    
237
        /**
238
         * Obtiene el porcentaje recorrido del proceso de filtrado
239
         * @return
240
         */
241
        public int getPercent() {
242
                return percent;
243
        }
244

    
245
        /**
246
         * Funci?n que contiene el c?digo a ejecutar antes de aplicar el filtro
247
         */
248
        abstract public void pre();
249

    
250
        /**
251
         * Funci?n que contiene el c?digo a ejecutar despues de aplicar el filtro
252
         */
253
        abstract public void post();
254

    
255
        /**
256
         * Ejecuci?n del filtro para un pixel de la imagen
257
         */
258
        abstract public void process(int x, int y);
259

    
260
        /**
261
         * Obtiene el tipo de datos del raster de entrada
262
         */
263
        abstract public int getInRasterDataType();
264

    
265
        /**
266
         * Obtiene el tipo de datos del raster de salida
267
         */
268
        abstract public int getOutRasterDataType();
269

    
270
        /**
271
         * Obtiene el resultado del filtro despues de su ejecuci?n a trav?s de una clave
272
         * @param name        clave para obtener un objeto resultado del filtro.
273
         */
274
        abstract public Object getResult(String name);
275

    
276
        /*
277
         * (non-Javadoc)
278
         * @see org.gvsig.raster.grid.filter.IRasterFilter#getGroup()
279
         */
280
        abstract public String getGroup();
281

    
282
        /**
283
         * Obtener que datos puede tratar una interfaz con sus valores
284
         * @param nameFilter. Cada tipo de filtro puede tener parametros distintos
285
         * @return
286
         */
287
        abstract public Params getUIParams(String nameFilter);
288

    
289
        /**
290
         * Obtener que las entradas que puede aperecer un filtro en el interfaz
291
         * @return
292
         */
293
        abstract public String[] getNames();
294

    
295
        /**
296
         * Devolver? un booleano indicando si es visible o no en el panel de filtros.
297
         * @return
298
         */
299
        public boolean isVisible() {
300
                return true;
301
        }
302

    
303
        /* (non-Javadoc)
304
         * @see java.lang.Object#clone()
305
         */
306
        public Object clone() throws CloneNotSupportedException {
307
                return super.clone();
308
        }
309

    
310
        /**
311
         * Devuelve el nombre interno del filtro
312
         * @return the fName
313
         */
314
        public String getName() {
315
                return fName;
316
        }
317

    
318
        /**
319
         * @param name the fName to set
320
         */
321
        public void setName(String name) {
322
                fName = name;
323
        }
324
        
325
        /**
326
         * Obtiene el TreeMap con los par?metros del entorno
327
         * @return TreeMap
328
         */
329
        public TreeMap getEnv() {
330
                return environment;
331
        }
332

    
333
        /**
334
         * Asigna el TreeMap con los par?metros del entorno
335
         * @param env
336
         */
337
        public void setEnv(TreeMap env) {
338
                this.environment = env;
339
        }
340
}