svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / io / raster / RasterFilterStack.java @ 2312
History | View | Annotate | Download (9.6 KB)
1 |
/*
|
---|---|
2 |
* Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
3 |
*
|
4 |
* Copyright (C) 2004-5.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
* For more information, contact:
|
21 |
*
|
22 |
* cresques@gmail.com
|
23 |
*/
|
24 |
package org.cresques.io.raster; |
25 |
|
26 |
import java.awt.Image; |
27 |
import java.util.Vector; |
28 |
|
29 |
/**
|
30 |
* Esta clase representa la pila de filtros que debe ser manejada desde el
|
31 |
* RasterFilterStackManager.
|
32 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
33 |
*
|
34 |
*/
|
35 |
public class RasterFilterStack{ |
36 |
|
37 |
private Image image = null; |
38 |
private RasterBuf rasterBuf = null; |
39 |
private RasterFilter rasterFilter = null; |
40 |
private int[] order = null; |
41 |
private RasterStats stats = null; |
42 |
|
43 |
Vector stack = new Vector(); |
44 |
private int typeFilter = -2; |
45 |
|
46 |
/**
|
47 |
* Un RasterFilterStack almacena objetos Filter que representan a un filtro. Esta
|
48 |
* clase contiene la informaci?n necesaria para el mantenimiento de de la pila y el
|
49 |
* RasterFilter para cuando se aplica la pila sobre el raster de entrada.
|
50 |
* @author Nacho Brodin <brodin_ign@gva.es>
|
51 |
*/
|
52 |
class Filter{ |
53 |
|
54 |
public int type = 0; //Tipo de filtro. Ctes en RasterFilterStackManager |
55 |
public boolean tmp = true; //Flag de temporalidad |
56 |
public RasterFilter filter = null; //Filtro |
57 |
|
58 |
public Filter(int type, RasterFilter filter){ |
59 |
this.filter = filter;
|
60 |
this.type = type;
|
61 |
} |
62 |
} |
63 |
|
64 |
/**
|
65 |
* Constructor
|
66 |
* @param stats
|
67 |
*/
|
68 |
public RasterFilterStack(RasterStats stats){
|
69 |
this.stats = stats;
|
70 |
} |
71 |
|
72 |
/**
|
73 |
* Resetea el flag de temporalidad de los filtros de la pila.
|
74 |
* Esto equivale a fijar los filtros que ya existen en la pila. A partir de
|
75 |
* ese momento los filtros que se introduzcan podr?n ser eliminados de golpe
|
76 |
* llamando a la funci?n deleteTempFilters
|
77 |
*/
|
78 |
public void resetTempFilters(){ |
79 |
for(int i=stack.size()-1;i>=0;i--) |
80 |
((Filter)stack.get(i)).tmp = false; |
81 |
} |
82 |
|
83 |
/**
|
84 |
* Elimina los filtros temporales, es decir, todos los filtros introducidos desde
|
85 |
* el ?ltimo resetTempFilters que se ha realizado.
|
86 |
*/
|
87 |
public void deleteTempFilters(){ |
88 |
for(int i=stack.size()-1;i>=0;i--){ |
89 |
if(((Filter)stack.get(i)).tmp) |
90 |
stack.remove(i); |
91 |
} |
92 |
sort(); |
93 |
} |
94 |
|
95 |
/**
|
96 |
* A?ade un filtro a la pila
|
97 |
* @param type Tipo de filtro
|
98 |
* @param filter filtro a?adido
|
99 |
*/
|
100 |
public void addFilter(int type, RasterFilter filter){ |
101 |
stack.add(new Filter(type, filter)); |
102 |
this.sort();
|
103 |
} |
104 |
|
105 |
/**
|
106 |
* Elimina el ?ltimo filtro de la pila
|
107 |
*/
|
108 |
public void removeFilter(){ |
109 |
stack.remove(stack.size()-1);
|
110 |
} |
111 |
|
112 |
/**
|
113 |
* Asigna el tipo filtro
|
114 |
* @param type
|
115 |
*/
|
116 |
public void setDataTypeInFilter(int type){ |
117 |
this.typeFilter = type;
|
118 |
} |
119 |
|
120 |
/**
|
121 |
* Devuelve el tipo de dato de retorno al aplicar la pila de filtros
|
122 |
* @return
|
123 |
*/
|
124 |
public int getOutDataType(){ |
125 |
if(image != null && rasterBuf == null) |
126 |
return RasterBuf.TYPE_IMAGE;
|
127 |
else
|
128 |
return rasterBuf.getDataType();
|
129 |
} |
130 |
|
131 |
/**
|
132 |
* Devuelve el raster o image resultado de la aplicacion de la pila de filtros
|
133 |
* @return
|
134 |
*/
|
135 |
public Object getResult(){ |
136 |
if(image != null && rasterBuf == null) |
137 |
return image;
|
138 |
else
|
139 |
return rasterBuf;
|
140 |
} |
141 |
|
142 |
/**
|
143 |
* Devuelve true si typeA est? por encima de typeB en la pila y false si no lo est?
|
144 |
* @param typeA Tipo de filtro
|
145 |
* @param typeB Tipo de filtro
|
146 |
* @return
|
147 |
*/
|
148 |
private boolean isOnTop(int typeA, int typeB){ |
149 |
int posA=-1; |
150 |
int posB=-1; |
151 |
for(int i=0;i<order.length;i++){ |
152 |
if(order[i] == typeA)posA = i;
|
153 |
if(order[i] == typeB)posB = i;
|
154 |
if(posA != -1 && posB != -1)break; |
155 |
} |
156 |
if(posA<posB)return true; |
157 |
else return false; |
158 |
} |
159 |
|
160 |
/**
|
161 |
* Dado un tipo de filtro calcula a partir de la pila en que posici?n est? situado y
|
162 |
* obtiene el tipo de dato de la salida anterior. Esto le dir? al cliente de que tipo
|
163 |
* es el filtro que tiene que introducir.
|
164 |
* @param type
|
165 |
*/
|
166 |
public int getDataTypeInFilter(int type){ |
167 |
for(int i=0; i<stack.size(); i++){ |
168 |
if( isOnTop(((Filter)stack.get(i)).type , type ) ){ |
169 |
return ((Filter)stack.get(i)).filter.getOutRasterDataType(); |
170 |
} |
171 |
} |
172 |
return this.typeFilter; |
173 |
} |
174 |
|
175 |
/**
|
176 |
* Asigna el vector para la ordenaci?n por tipos
|
177 |
* @param order
|
178 |
*/
|
179 |
public void setOrder(int[] order){ |
180 |
this.order = order;
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* Ordena los filtros en el orden establecido por el Manager
|
185 |
*/
|
186 |
public void sort(){ |
187 |
Vector aux = new Vector(); |
188 |
for(int norden=order.length-1;norden>=0;norden--){ |
189 |
for(int i=stack.size()-1;i>=0;i--){ |
190 |
if(((Filter)stack.get(i)).type == order[norden]){ |
191 |
aux.add(stack.get(i)); |
192 |
} |
193 |
} |
194 |
} |
195 |
stack = aux; |
196 |
aux = null;
|
197 |
} |
198 |
|
199 |
/**
|
200 |
* Elimina los filtros de la pila de un determinado tipo
|
201 |
* @param type Tipo de filtro a eliminar
|
202 |
*/
|
203 |
public void removeFilter(int type){ |
204 |
for(int i=stack.size()-1;i>=0;i--){ |
205 |
if(((Filter)stack.get(i)).type == type){ |
206 |
stack.remove(i); |
207 |
} |
208 |
} |
209 |
} |
210 |
|
211 |
/**
|
212 |
* Elimina un filtro concreto de la pila
|
213 |
* @param type
|
214 |
*/
|
215 |
public void removeFilter(RasterFilter filter){ |
216 |
for(int i=stack.size()-1;i>=0;i--){ |
217 |
if(((Filter)stack.get(i)).filter.equals(filter)){ |
218 |
stack.remove(i); |
219 |
} |
220 |
} |
221 |
} |
222 |
|
223 |
/**
|
224 |
* Obtiene la cantidad de filtros en la pila
|
225 |
* @return N?mero de filtros apilados
|
226 |
*/
|
227 |
public int lenght(){ |
228 |
return stack.size();
|
229 |
} |
230 |
|
231 |
/**
|
232 |
* Obtiene el filtro apilado de la posici?n i
|
233 |
* @param i Posici?n a acceder en la pila
|
234 |
* @return Filtro
|
235 |
*/
|
236 |
public RasterFilter get(int i){ |
237 |
return ((Filter)stack.get(i)).filter; |
238 |
} |
239 |
|
240 |
/**
|
241 |
* Obtiene el tipo del filtro de la pila de la posici?n i
|
242 |
* @param i Posici?n a acceder en la pila
|
243 |
* @return tipo de filtro
|
244 |
*/
|
245 |
public int getType(int i){ |
246 |
return ((Filter)stack.get(i)).type; |
247 |
} |
248 |
|
249 |
/**
|
250 |
* Elimina todos los filtros de la pila
|
251 |
*/
|
252 |
public void clear(){ |
253 |
stack.removeAllElements(); |
254 |
} |
255 |
|
256 |
/**
|
257 |
* Sustituye un filtro de una posici?n de la pila por otro
|
258 |
* @param filter
|
259 |
* @param i
|
260 |
*/
|
261 |
public void replace(RasterFilter filter, int i, int type){ |
262 |
((Filter)stack.get(i)).filter = filter;
|
263 |
((Filter)stack.get(i)).type = type;
|
264 |
} |
265 |
|
266 |
/**
|
267 |
* Asigna el raster de entrada inicial
|
268 |
* @param raster
|
269 |
*/
|
270 |
public void setInitRasterBuf(Object raster){ |
271 |
if(raster instanceof RasterBuf){ |
272 |
rasterBuf = (RasterBuf)raster; |
273 |
this.typeFilter = rasterBuf.getDataType();
|
274 |
} |
275 |
if(raster instanceof Image){ |
276 |
image = (Image)raster;
|
277 |
this.typeFilter = RasterBuf.TYPE_IMAGE;
|
278 |
} |
279 |
} |
280 |
|
281 |
/**
|
282 |
* Devuelve el tipo de datos inicial de la pila
|
283 |
* @return Tipo de dato del raster inicial
|
284 |
*/
|
285 |
public int getInitDataType(){ |
286 |
return typeFilter;
|
287 |
} |
288 |
|
289 |
/**
|
290 |
* M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en la
|
291 |
* pila y false si no lo est?.
|
292 |
* @param type Tipo de par?metro a comprobar
|
293 |
* @return true si est? en la pila y false si no lo est?
|
294 |
*/
|
295 |
public boolean isActive(int type){ |
296 |
for(int i=stack.size()-1;i>=0;i--){ |
297 |
if(((Filter)stack.get(i)).type == type) |
298 |
return true; |
299 |
} |
300 |
return false; |
301 |
} |
302 |
|
303 |
/**
|
304 |
* M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en la
|
305 |
* pila y false si no lo est?.
|
306 |
* @param filter Tipo de filtro a comprobar
|
307 |
* @return true si est? en la pila y false si no lo est?
|
308 |
*/
|
309 |
public boolean isActive(RasterFilter filter){ |
310 |
for(int i=stack.size()-1;i>=0;i--){ |
311 |
if(((Filter)stack.get(i)).filter.equals(filter)) |
312 |
return true; |
313 |
} |
314 |
return false; |
315 |
} |
316 |
|
317 |
/**
|
318 |
* Obtiene el objeto RasterStats
|
319 |
* @return
|
320 |
*/
|
321 |
public RasterStats getStats(){
|
322 |
return this.stats; |
323 |
} |
324 |
|
325 |
/**
|
326 |
* Aplica los filtros de la pila sobre el buffer correspondiente
|
327 |
* @param dataType
|
328 |
*/
|
329 |
private void executeFilterByDataType(int dataType){ |
330 |
for(int i=stack.size()-1;i>=0;i--){ |
331 |
RasterFilter filter = ((Filter)stack.get(i)).filter;
|
332 |
//System.err.println("SACANDO FILTRO "+i+" "+filter.toString());
|
333 |
if(filter.getInRasterDataType() == RasterBuf.TYPE_IMAGE)
|
334 |
filter.addParam("raster", image);
|
335 |
else
|
336 |
filter.addParam("raster", rasterBuf);
|
337 |
|
338 |
filter.execute(); |
339 |
|
340 |
if(filter.getResult("stats")!=null) |
341 |
this.stats = (RasterStats)filter.getResult("stats"); |
342 |
|
343 |
if(filter.getOutRasterDataType() == RasterBuf.TYPE_IMAGE){
|
344 |
if(filter.getResult("raster")!=null){ |
345 |
this.image = (Image)filter.getResult("raster"); |
346 |
this.rasterBuf = null; |
347 |
} |
348 |
}else{
|
349 |
if(filter.getResult("raster")!=null){ |
350 |
this.rasterBuf = (RasterBuf)filter.getResult("raster"); |
351 |
this.image = null; |
352 |
} |
353 |
} |
354 |
|
355 |
} |
356 |
} |
357 |
|
358 |
/**
|
359 |
* Aplica los filtros sobre un Image
|
360 |
* @param image Buffer inicial sobre el que se aplican los filtros
|
361 |
*/
|
362 |
public void execute(Image image){ |
363 |
this.image = image;
|
364 |
executeFilterByDataType(RasterBuf.TYPE_IMAGE); |
365 |
} |
366 |
|
367 |
/**
|
368 |
* Aplica los filtros sobre un RasterBuf
|
369 |
* @param rasterBuf Buffer inicial sobre el que se aplican los filtros
|
370 |
*/
|
371 |
public void execute(RasterBuf rasterBuf){ |
372 |
this.rasterBuf = rasterBuf;
|
373 |
executeFilterByDataType(RasterBuf.TYPE_SHORT); |
374 |
} |
375 |
|
376 |
/**
|
377 |
*Muestra el contenido de la pila de filtros para depuraci?n
|
378 |
*/
|
379 |
public void show(){ |
380 |
System.out.println("--------------------------------------------"); |
381 |
for(int i=stack.size()-1;i>=0;i--){ |
382 |
System.out.println( "FILTRO:"+i+ |
383 |
" TIPO:"+((Filter)stack.get(i)).type+ |
384 |
" TEMP:"+((Filter)stack.get(i)).tmp+ |
385 |
" FIL:"+((Filter)stack.get(i)).filter.toString()); |
386 |
} |
387 |
} |
388 |
|
389 |
} |