Statistics
| Revision:

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

History | View | Annotate | Download (6.57 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

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

    
47
        /**
48
         * @author Nacho Brodin (nachobrodin@gmail.com)
49
         */
50
        public static class Kernel {
51
                public double[][]        kernel        = null;
52
                protected double        divisor = 0;
53

    
54
                public Kernel(double[][] k) {
55
                        this.kernel = k;
56

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

    
62
                public Kernel(double[][] k,double divisor) {
63
                        this.kernel = k;
64
                        this.divisor = divisor;
65
                }
66

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

    
75
                public double convolution(Kernel k) {
76
                        double res = 0;
77
                        res = this.kernelOperation(k);
78
                        if (this.divisor != 0)
79
                                res = res / divisor;
80
                        return Math.abs(res);
81
                }
82

    
83
                public double getDivisor() {
84
                        return divisor;
85
                }
86

    
87
                public void setDivisor(double divisor) {
88
                        this.divisor = divisor;
89
                }
90

    
91
                public int getLado() {
92
                        return kernel.length;
93
                }
94
        }
95

    
96
        /**
97
         * Constructor
98
         */
99
        public RasterFilter() {
100
        }
101

    
102
        /**
103
         * Aplica el filtro sobre el raster pasado pixel a pixel
104
         * @throws InterruptedException
105
         */
106
        public void execute() throws InterruptedException {
107
                pre();
108
                percent = 0;
109
                if (exec) {
110
                        for (int row = 0; row < height; row ++) {
111
                                for (int col = 0; col < width; col ++)
112
                                        process(col, row);
113
                                if (Thread.currentThread().isInterrupted())
114
                                        throw new InterruptedException("Process execute Filter");
115
                                percent = (row * 100) / height;
116
                        }
117
                }
118
                if (isCanceled())
119
                        return;
120
                percent = 100;
121
                post();
122
        }
123

    
124
        /**
125
         * A?ade un par?metro al filtro
126
         *
127
         * @param name Clave del par?metro
128
         * @param param Objeto pasado como par?metro
129
         */
130
        public void addParam(String name, Object param) {
131
                if (param != null)
132
                        params.put(name, param);
133
                else
134
                        params.remove(name);
135
        }
136

    
137
        /**
138
         * Elimina un par?metro del filtro
139
         * @param name Clave del par?metro a eliminar
140
         */
141
        public void removeParam(String name) {
142
                params.remove(name);
143
        }
144

    
145
        /**
146
         * Obtiene un par?metro a partir de la clave
147
         * @param name Par?metro
148
         * @return Par?metro
149
         */
150
        public Object getParam(String name) {
151
                return params.get(name);
152
        }
153

    
154
                /**
155
         * @param extent The extent to set.
156
         */
157
        public void setExtent(Extent extent) {
158
                this.extent = extent;
159
        }
160

    
161
        /**
162
         * Obtiene true si el filtro va a ser ejecutado o false si no va a serlo
163
         * @return
164
         */
165
        public boolean isExec() {
166
                return exec;
167
        }
168

    
169
        /**
170
         * Asigna el valor a la variable exec. Esta estar? a true si el filtro se ejecutar? la pr?xima
171
         * vez que se repinte o false si no se ejecuta.
172
         * @param exec
173
         */
174
        public void setExec(boolean exec) {
175
                this.exec = exec;
176
        }
177

    
178
        /**
179
         * Establece si se ha de cancelar un proceso de filtrado o no.
180
         * @param value
181
         */
182
        public void setCanceled(boolean value) {
183
                canceled = value;
184
        }
185

    
186
        /**
187
         * Devuelve si se ha cancelado el proceso de filtrar.
188
         * @return
189
         */
190
        public boolean isCanceled() {
191
                return canceled;
192
        }
193

    
194
        /**
195
         * Pone a cero el contador del porcentaje del proceso de filtrado
196
         * @return
197
         */
198
        public void resetPercent() {
199
                percent = 0;
200
        }
201

    
202
        /**
203
         * Obtiene el porcentaje recorrido del proceso de filtrado
204
         * @return
205
         */
206
        public int getPercent() {
207
                return percent;
208
        }
209

    
210
        /**
211
         * Funci?n que contiene el c?digo a ejecutar antes de aplicar el filtro
212
         */
213
        abstract public void pre();
214

    
215
        /**
216
         * Funci?n que contiene el c?digo a ejecutar despues de aplicar el filtro
217
         */
218
        abstract public void post();
219

    
220
        /**
221
         * Ejecuci?n del filtro para un pixel de la imagen
222
         */
223
        abstract public void process(int x, int y);
224

    
225
        /**
226
         * Obtiene el tipo de datos del raster de entrada
227
         */
228
        abstract public int getInRasterDataType();
229

    
230
        /**
231
         * Obtiene el tipo de datos del raster de salida
232
         */
233
        abstract public int getOutRasterDataType();
234

    
235
        /**
236
         * Obtiene el resultado del filtro despues de su ejecuci?n a trav?s de una clave
237
         * @param name        clave para obtener un objeto resultado del filtro.
238
         */
239
        abstract public Object getResult(String name);
240

    
241
        /*
242
         * (non-Javadoc)
243
         * @see org.gvsig.raster.grid.filter.IRasterFilter#getGroup()
244
         */
245
        abstract public String getGroup();
246

    
247
        /**
248
         * Obtener que datos puede tratar una interfaz con sus valores
249
         * @param nameFilter. Cada tipo de filtro puede tener parametros distintos
250
         * @return
251
         */
252
        abstract public Params getUIParams(String nameFilter);
253

    
254
        /**
255
         * Obtener que las entradas que puede aperecer un filtro en el interfaz
256
         * @return
257
         */
258
        abstract public String[] getNames();
259

    
260
        /**
261
         * Devolver? un booleano indicando si es visible o no en el panel de filtros.
262
         * @return
263
         */
264
        public boolean isVisible() {
265
                return true;
266
        }
267

    
268
        /* (non-Javadoc)
269
         * @see java.lang.Object#clone()
270
         */
271
        public Object clone() throws CloneNotSupportedException {
272
                return super.clone();
273
        }
274

    
275
        /**
276
         * Devuelve el nombre interno del filtro
277
         * @return the fName
278
         */
279
        public String getName() {
280
                return fName;
281
        }
282

    
283
        /**
284
         * @param name the fName to set
285
         */
286
        public void setName(String name) {
287
                fName = name;
288
        }
289
}