svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / RasterFilter.java @ 11864
History | View | Annotate | Download (6.54 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 |
/**
|
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 { |
34 |
/**
|
35 |
* Nombre del filtro. Este nombre debe asignarlo cada clase de filtro especifico y es necesario
|
36 |
* para leer el nombre en el listado de fitros de la pila.
|
37 |
*/
|
38 |
protected String fName = ""; |
39 |
|
40 |
protected IBuffer raster = null; |
41 |
protected int height = 0; |
42 |
protected int width = 0; |
43 |
protected Hashtable params = new Hashtable(); |
44 |
protected Extent extent = null; |
45 |
private int percent = 0; |
46 |
private boolean canceled = false; |
47 |
/**
|
48 |
* Variable que control la aplicaci?n o no del filtro. Si est? a false aunque est? en
|
49 |
* la pila el filtro no se ejecutar?.
|
50 |
*/
|
51 |
protected boolean exec = true; |
52 |
|
53 |
/**
|
54 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
55 |
*/
|
56 |
public static class Kernel { |
57 |
public double[][] kernel = null; |
58 |
protected double divisor = 0; |
59 |
|
60 |
public Kernel(double[][] k) { |
61 |
this.kernel = k;
|
62 |
|
63 |
for (int i = 0; i < kernel.length; i++) |
64 |
for (int j = 0; j < kernel[0].length; j++) |
65 |
divisor = divisor + kernel[i][j]; |
66 |
} |
67 |
|
68 |
public Kernel(double[][] k,double divisor){ |
69 |
this.kernel = k;
|
70 |
this.divisor = divisor;
|
71 |
} |
72 |
|
73 |
public double kernelOperation(Kernel k) { |
74 |
double res = 0; |
75 |
for (int i = 0; i < kernel.length; i++) |
76 |
for (int j = 0; j < kernel[0].length; j++) |
77 |
res += kernel[i][j] * k.kernel[i][j]; |
78 |
return res;
|
79 |
} |
80 |
|
81 |
public double convolution(Kernel k) { |
82 |
double res = 0; |
83 |
res = this.kernelOperation(k);
|
84 |
if (this.divisor != 0) |
85 |
res = res / divisor; |
86 |
return Math.abs(res); |
87 |
} |
88 |
|
89 |
public double getDivisor() { |
90 |
return divisor;
|
91 |
} |
92 |
|
93 |
public void setDivisor(double divisor) { |
94 |
this.divisor = divisor;
|
95 |
} |
96 |
|
97 |
public int getLado(){ |
98 |
return kernel.length;
|
99 |
} |
100 |
} |
101 |
|
102 |
/**
|
103 |
* Constructor
|
104 |
*/
|
105 |
public RasterFilter() {
|
106 |
} |
107 |
|
108 |
/**
|
109 |
* Aplica el filtro sobre el raster pasado pixel a pixel
|
110 |
*/
|
111 |
public void execute() { |
112 |
pre(); |
113 |
percent = 0;
|
114 |
if (exec) {
|
115 |
for (int col = 0; col < width; col += 1) { |
116 |
for (int line = 0; line < height; line += 1) |
117 |
process(col, line); |
118 |
percent = (col * 100) / width;
|
119 |
} |
120 |
} |
121 |
if (isCanceled())
|
122 |
return;
|
123 |
percent = 100;
|
124 |
post(); |
125 |
} |
126 |
|
127 |
/**
|
128 |
* A?ade un par?metro al filtro
|
129 |
*
|
130 |
* @param name Clave del par?metro
|
131 |
* @param param Objeto pasado como par?metro
|
132 |
*/
|
133 |
public void addParam(String name, Object param) { |
134 |
params.put(name, param); |
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 |
* Obtiene el nombre del filtro. Este nombre debe asignarlo cada clase
|
180 |
* de filtro especifico y es necesario para leer el nombre en el listado de fitros de la pila.
|
181 |
* @return Cadena que representa el nombre del filtro.
|
182 |
*/
|
183 |
public String getName() { |
184 |
return fName;
|
185 |
} |
186 |
|
187 |
/**
|
188 |
* Asigna el nombre del filtro.
|
189 |
* @param fName Cadena que representa el identificador del filtro
|
190 |
*/
|
191 |
public void setName(String fName) { |
192 |
this.fName = fName;
|
193 |
} |
194 |
|
195 |
/**
|
196 |
* Establece si se ha de cancelar un proceso de filtrado o no.
|
197 |
* @param value
|
198 |
*/
|
199 |
public void setCanceled(boolean value) { |
200 |
canceled = value; |
201 |
} |
202 |
|
203 |
/**
|
204 |
* Devuelve si se ha cancelado el proceso de filtrar.
|
205 |
* @return
|
206 |
*/
|
207 |
public boolean isCanceled() { |
208 |
return canceled;
|
209 |
} |
210 |
|
211 |
/**
|
212 |
* Pone a cero el contador del porcentaje del proceso de filtrado
|
213 |
* @return
|
214 |
*/
|
215 |
public void resetPercent() { |
216 |
percent = 0;
|
217 |
} |
218 |
|
219 |
/**
|
220 |
* Obtiene el porcentaje recorrido del proceso de filtrado
|
221 |
* @return
|
222 |
*/
|
223 |
public int getPercent() { |
224 |
return percent;
|
225 |
} |
226 |
|
227 |
/**
|
228 |
* Funci?n que contiene el c?digo a ejecutar antes de aplicar el filtro
|
229 |
*/
|
230 |
abstract public void pre(); |
231 |
|
232 |
/**
|
233 |
* Funci?n que contiene el c?digo a ejecutar despues de aplicar el filtro
|
234 |
*/
|
235 |
abstract public void post(); |
236 |
|
237 |
/**
|
238 |
* Ejecuci?n del filtro para un pixel de la imagen
|
239 |
*/
|
240 |
abstract public void process(int x, int y); |
241 |
|
242 |
/**
|
243 |
* Obtiene el tipo de datos del raster de entrada
|
244 |
*/
|
245 |
abstract public int getInRasterDataType(); |
246 |
|
247 |
/**
|
248 |
* Obtiene el tipo de datos del raster de salida
|
249 |
*/
|
250 |
abstract public int getOutRasterDataType(); |
251 |
|
252 |
/**
|
253 |
* Obtiene el resultado del filtro despues de su ejecuci?n a trav?s de una clave
|
254 |
* @param name clave para obtener un objeto resultado del filtro.
|
255 |
*/
|
256 |
abstract public Object getResult(String name); |
257 |
|
258 |
/*
|
259 |
* (non-Javadoc)
|
260 |
* @see org.gvsig.raster.grid.filter.IRasterFilter#getGroup()
|
261 |
*/
|
262 |
abstract public String getGroup(); |
263 |
|
264 |
/**
|
265 |
* Obtener que datos puede tratar una interfaz con sus valores
|
266 |
* @return
|
267 |
*/
|
268 |
abstract public Params getUIParams(); |
269 |
|
270 |
/* (non-Javadoc)
|
271 |
* @see java.lang.Object#clone()
|
272 |
*/
|
273 |
public Object clone() throws CloneNotSupportedException { |
274 |
return super.clone(); |
275 |
} |
276 |
//abstract public Object clone() throws CloneNotSupportedException;
|
277 |
} |