svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / RasterFilter.java @ 11183
History | View | Annotate | Download (6.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 |
|
23 |
import org.cresques.px.Extent; |
24 |
import org.gvsig.raster.dataset.IBuffer; |
25 |
|
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 { |
33 |
/**
|
34 |
* Nombre del filtro. Este nombre debe asignarlo cada clase de filtro especifico y es necesario
|
35 |
* para leer el nombre en el listado de fitros de la pila.
|
36 |
*/
|
37 |
protected String fName = ""; |
38 |
/**
|
39 |
* Prioridad para la ejecuci?n del filtro en la pila. A mayor valor mayor prioridad en la
|
40 |
* ejecuci?n. Los filtros de una misma prioridad ser?n ejecutados en orden aleatorio.
|
41 |
* La prioridad asignada en la clase a un filtro han de ser valores mayores que 20 ya
|
42 |
* que de 0-20 son utilizados para el orden de usuario.
|
43 |
*/
|
44 |
protected int fPriority = 0; |
45 |
protected IBuffer raster = null; |
46 |
protected int height = 0; |
47 |
protected int width = 0; |
48 |
protected Hashtable params = new Hashtable(); |
49 |
protected Extent extent = null; |
50 |
/**
|
51 |
* Variable que control la aplicaci?n o no del filtro. Si est? a false aunque est? en
|
52 |
* la pila el filtro no se ejecutar?.
|
53 |
*/
|
54 |
protected boolean exec = true; |
55 |
|
56 |
/**
|
57 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
58 |
*/
|
59 |
public static class Kernel{ |
60 |
public double[][] kernel = null; |
61 |
protected double divisor = 0; |
62 |
|
63 |
public Kernel(double[][] k){ |
64 |
this.kernel = k;
|
65 |
|
66 |
for (int i=0;i< kernel.length;i++) |
67 |
for (int j=0;j<kernel[0].length;j++) |
68 |
divisor = divisor + kernel[i][j]; |
69 |
} |
70 |
|
71 |
public Kernel(double[][] k,double divisor){ |
72 |
this.kernel = k;
|
73 |
this.divisor = divisor;
|
74 |
} |
75 |
|
76 |
public double kernelOperation(Kernel k){ |
77 |
double res = 0; |
78 |
for (int i=0;i< kernel.length;i++) |
79 |
for (int j=0;j<kernel[0].length;j++) |
80 |
res += kernel[i][j] * k.kernel[i][j]; |
81 |
return res;
|
82 |
} |
83 |
|
84 |
public double convolution (Kernel k){ |
85 |
double res =0; |
86 |
res = this.kernelOperation(k);
|
87 |
if (this.divisor !=0) |
88 |
res = res/divisor; |
89 |
return Math.abs(res); |
90 |
} |
91 |
|
92 |
public double getDivisor() { |
93 |
return divisor;
|
94 |
} |
95 |
|
96 |
public void setDivisor(double divisor) { |
97 |
this.divisor = divisor;
|
98 |
} |
99 |
|
100 |
public int getLado(){ |
101 |
return kernel.length;
|
102 |
} |
103 |
} |
104 |
|
105 |
/**
|
106 |
* Constructor
|
107 |
*/
|
108 |
public RasterFilter() {
|
109 |
} |
110 |
|
111 |
/**
|
112 |
* Aplica el filtro sobre el raster pasado pixel a pixel
|
113 |
*/
|
114 |
public void execute() { |
115 |
pre(); |
116 |
if (exec) {
|
117 |
for (int col = 0; col < width; col += 1) |
118 |
for (int line = 0; line < height; line += 1) |
119 |
process(col, line); |
120 |
} |
121 |
post(); |
122 |
} |
123 |
|
124 |
/**
|
125 |
* A?ade un par?metro al filtro
|
126 |
* @param name Clave del par?metro
|
127 |
* @param param Objeto pasado como par?metro
|
128 |
*/
|
129 |
public void addParam(String name, Object param) { |
130 |
params.put(name, param); |
131 |
} |
132 |
|
133 |
/**
|
134 |
* Elimina un par?metro del filtro
|
135 |
* @param name Clave del par?metro a eliminar
|
136 |
*/
|
137 |
public void removeParam(String name){ |
138 |
params.remove(name); |
139 |
} |
140 |
|
141 |
/**
|
142 |
* Obtiene un par?metro a partir de la clave
|
143 |
* @param name Par?metro
|
144 |
* @return Par?metro
|
145 |
*/
|
146 |
public Object getParam(String name){ |
147 |
return params.get(name);
|
148 |
} |
149 |
|
150 |
/**
|
151 |
* @param extent The extent to set.
|
152 |
*/
|
153 |
public void setExtent(Extent extent) { |
154 |
this.extent = extent;
|
155 |
} |
156 |
|
157 |
/**
|
158 |
* Obtiene true si el filtro va a ser ejecutado o false si no va a serlo
|
159 |
* @return
|
160 |
*/
|
161 |
public boolean isExec() { |
162 |
return exec;
|
163 |
} |
164 |
|
165 |
/**
|
166 |
* Asigna el valor a la variable exec. Esta estar? a true si el filtro se ejecutar? la pr?xima
|
167 |
* vez que se repinte o false si no se ejecuta.
|
168 |
* @param exec
|
169 |
*/
|
170 |
public void setExec(boolean exec) { |
171 |
this.exec = exec;
|
172 |
} |
173 |
|
174 |
/**
|
175 |
* Obtiene el nombre del filtro. Este nombre debe asignarlo cada clase
|
176 |
* de filtro especifico y es necesario para leer el nombre en el listado de fitros de la pila.
|
177 |
* @return Cadena que representa el nombre del filtro.
|
178 |
*/
|
179 |
public String getName() { |
180 |
return fName;
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* Asigna el nombre del filtro.
|
185 |
* @param fName Cadena que representa el identificador del filtro
|
186 |
*/
|
187 |
public void setName(String fName) { |
188 |
this.fName = fName;
|
189 |
} |
190 |
|
191 |
/**
|
192 |
* Obtiene la prioridad del filtro. Esta prioridad debe asignarla cada clase
|
193 |
* de filtro especifico y es necesaria para el orden de ejecuci?n.
|
194 |
* A mayor valor mayor prioridad en la ejecuci?n. Los filtros de una misma
|
195 |
* prioridad ser?n ejecutados en orden aleatorio. La prioridad asignada en la
|
196 |
* clase a un filtro han de ser valores mayores que 20 ya que de 0-20 son utilizados
|
197 |
* para el orden de usuario.
|
198 |
*
|
199 |
* @return Entero que representa la prioridad del filtro.
|
200 |
*/
|
201 |
public int getPriority() { |
202 |
return fPriority;
|
203 |
} |
204 |
|
205 |
/**
|
206 |
* Asigna la prioridad del filtro.
|
207 |
* A mayor valor mayor prioridad en la ejecuci?n. Los filtros de una misma
|
208 |
* prioridad ser?n ejecutados en orden aleatorio. La prioridad asignada en la
|
209 |
* clase a un filtro han de ser valores mayores que 20 ya que de 0-20 son utilizados
|
210 |
* para el orden de usuario.
|
211 |
* @param fName Cadena que representa el identificador del filtro
|
212 |
*/
|
213 |
public void setPriority(int fPriority) { |
214 |
this.fPriority = fPriority;
|
215 |
} |
216 |
|
217 |
/**
|
218 |
* Funci?n que contiene el c?digo a ejecutar antes de aplicar el filtro
|
219 |
*/
|
220 |
abstract public void pre(); |
221 |
|
222 |
/**
|
223 |
* Funci?n que contiene el c?digo a ejecutar despues de aplicar el filtro
|
224 |
*/
|
225 |
abstract public void post(); |
226 |
|
227 |
/**
|
228 |
* Ejecuci?n del filtro para un pixel de la imagen
|
229 |
*/
|
230 |
abstract public void process(int x, int y); |
231 |
|
232 |
/**
|
233 |
* Obtiene el tipo de datos del raster de entrada
|
234 |
*/
|
235 |
abstract public int getInRasterDataType(); |
236 |
|
237 |
/**
|
238 |
* Obtiene el tipo de datos del raster de salida
|
239 |
*/
|
240 |
abstract public int getOutRasterDataType(); |
241 |
|
242 |
/**
|
243 |
* Obtiene el resultado del filtro despues de su ejecuci?n a trav?s de una clave
|
244 |
* @param name clave para obtener un objeto resultado del filtro.
|
245 |
*/
|
246 |
abstract public Object getResult(String name); |
247 |
|
248 |
} |