root / tags / v1_1_Build_1001 / libraries / libCq_CMS_praster / src / org / cresques / filter / RasterFilterStack.java @ 11984
History | View | Annotate | Download (13.1 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.filter; |
25 |
|
26 |
import java.awt.Image; |
27 |
import java.util.ArrayList; |
28 |
import java.util.Vector; |
29 |
|
30 |
import org.cresques.geo.ViewPortData; |
31 |
import org.cresques.io.data.RasterBuf; |
32 |
import org.cresques.io.datastruct.Statistic; |
33 |
import org.cresques.px.Extent; |
34 |
|
35 |
|
36 |
/**
|
37 |
* Esta clase representa la pila de filtros que debe ser manejada desde el
|
38 |
* RasterFilterStackManager.
|
39 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
40 |
*
|
41 |
*/
|
42 |
public class RasterFilterStack { |
43 |
private Image image = null; |
44 |
private RasterBuf rasterBuf = null; |
45 |
private RasterFilter rasterFilter = null; |
46 |
private int[] order = null; |
47 |
private Statistic stats = null; |
48 |
//Pila de objetos Filter (Contiene un RasterFilter)
|
49 |
private Vector stack = new Vector(); |
50 |
private int typeFilter = -1; |
51 |
private ViewPortData viewPortData = null; |
52 |
private Extent extent = null; |
53 |
private int[] stepX = null; |
54 |
private int[] stepY = null; |
55 |
|
56 |
/**
|
57 |
* Constructor
|
58 |
* @param stats
|
59 |
*/
|
60 |
public RasterFilterStack(Statistic stats) {
|
61 |
this.stats = stats;
|
62 |
} |
63 |
|
64 |
/**
|
65 |
* Resetea el flag de temporalidad de los filtros de la pila.
|
66 |
* Esto equivale a fijar los filtros que ya existen en la pila. A partir de
|
67 |
* ese momento los filtros que se introduzcan podr?n ser eliminados de golpe
|
68 |
* llamando a la funci?n deleteTempFilters
|
69 |
*/
|
70 |
public void resetTempFilters() { |
71 |
for (int i = stack.size() - 1; i >= 0; i--) |
72 |
((Filter) stack.get(i)).tmp = false; |
73 |
} |
74 |
|
75 |
/**
|
76 |
* Elimina los filtros temporales, es decir, todos los filtros introducidos desde
|
77 |
* el ?ltimo resetTempFilters que se ha realizado.
|
78 |
*/
|
79 |
public void deleteTempFilters() { |
80 |
for (int i = stack.size() - 1; i >= 0; i--) { |
81 |
if (((Filter) stack.get(i)).tmp) { |
82 |
stack.remove(i); |
83 |
} |
84 |
} |
85 |
|
86 |
sort(); |
87 |
} |
88 |
|
89 |
/**
|
90 |
* A?ade un filtro a la pila
|
91 |
* @param type Tipo de filtro
|
92 |
* @param filter filtro a?adido
|
93 |
*/
|
94 |
public void addFilter(int type, RasterFilter filter) { |
95 |
stack.add(new Filter(type, filter)); |
96 |
this.sort();
|
97 |
} |
98 |
|
99 |
/**
|
100 |
* Elimina el ?ltimo filtro de la pila
|
101 |
*/
|
102 |
public void removeFilter() { |
103 |
stack.remove(stack.size() - 1);
|
104 |
} |
105 |
|
106 |
/**
|
107 |
* Asigna el tipo filtro
|
108 |
* @param type
|
109 |
*/
|
110 |
public void setDataTypeInFilter(int type) { |
111 |
this.typeFilter = type;
|
112 |
} |
113 |
|
114 |
/**
|
115 |
* Devuelve el tipo de dato de retorno al aplicar la pila de filtros
|
116 |
* @return
|
117 |
*/
|
118 |
public int getOutDataType() { |
119 |
if ((image != null) && (rasterBuf == null)) { |
120 |
return RasterBuf.TYPE_IMAGE;
|
121 |
} else {
|
122 |
return rasterBuf.getDataType();
|
123 |
} |
124 |
} |
125 |
|
126 |
/**
|
127 |
* Devuelve el raster o image resultado de la aplicacion de la pila de filtros
|
128 |
* @return
|
129 |
*/
|
130 |
public Object getResult() { |
131 |
if ((image != null) && (rasterBuf == null)) { |
132 |
return image;
|
133 |
} else {
|
134 |
return rasterBuf;
|
135 |
} |
136 |
} |
137 |
|
138 |
/**
|
139 |
* Devuelve true si typeA est? por encima de typeB en la pila y false si no lo est?
|
140 |
* @param typeA Tipo de filtro
|
141 |
* @param typeB Tipo de filtro
|
142 |
* @return
|
143 |
*/
|
144 |
private boolean isOnTop(int typeA, int typeB) { |
145 |
int posA = -1; |
146 |
int posB = -1; |
147 |
|
148 |
for (int i = 0; i < order.length; i++) { |
149 |
if (order[i] == typeA) {
|
150 |
posA = i; |
151 |
} |
152 |
|
153 |
if (order[i] == typeB) {
|
154 |
posB = i; |
155 |
} |
156 |
|
157 |
if ((posA != -1) && (posB != -1)) { |
158 |
break;
|
159 |
} |
160 |
} |
161 |
|
162 |
if (posA < posB) {
|
163 |
return true; |
164 |
} else {
|
165 |
return false; |
166 |
} |
167 |
} |
168 |
|
169 |
/**
|
170 |
* Dado un tipo de filtro calcula a partir de la pila en que posici?n est? situado y
|
171 |
* obtiene el tipo de dato de la salida anterior. Esto le dir? al cliente de que tipo
|
172 |
* es el filtro que tiene que introducir.
|
173 |
* @param type
|
174 |
*/
|
175 |
public int getDataTypeInFilter(int type) { |
176 |
for (int i = 0; i < stack.size(); i++) { |
177 |
if (isOnTop(((Filter) stack.get(i)).type, type)) { |
178 |
return ((Filter) stack.get(i)).filter.getOutRasterDataType(); |
179 |
} |
180 |
} |
181 |
|
182 |
return this.typeFilter; |
183 |
} |
184 |
|
185 |
/**
|
186 |
* Asigna el vector para la ordenaci?n por tipos
|
187 |
* @param order
|
188 |
*/
|
189 |
public void setOrder(int[] order) { |
190 |
this.order = order;
|
191 |
} |
192 |
|
193 |
/**
|
194 |
* Ordena los filtros en el orden establecido por el Manager
|
195 |
*/
|
196 |
public void sort() { |
197 |
Vector aux = new Vector(); |
198 |
|
199 |
for (int norden = order.length - 1; norden >= 0; norden--) { |
200 |
for (int i = stack.size() - 1; i >= 0; i--) { |
201 |
if (((Filter) stack.get(i)).type == order[norden]) { |
202 |
aux.add(stack.get(i)); |
203 |
} |
204 |
} |
205 |
} |
206 |
|
207 |
stack = aux; |
208 |
aux = null;
|
209 |
} |
210 |
|
211 |
/**
|
212 |
* Elimina los filtros de la pila de un determinado tipo
|
213 |
* @param type Tipo de filtro a eliminar
|
214 |
*/
|
215 |
public void removeFilter(int type) { |
216 |
for (int i = stack.size() - 1; i >= 0; i--) { |
217 |
if (((Filter) stack.get(i)).type == type) { |
218 |
stack.remove(i); |
219 |
} |
220 |
} |
221 |
} |
222 |
|
223 |
/**
|
224 |
* Elimina un filtro concreto de la pila
|
225 |
* @param type
|
226 |
*/
|
227 |
public void removeFilter(RasterFilter filter) { |
228 |
for (int i = stack.size() - 1; i >= 0; i--) { |
229 |
if (((Filter) stack.get(i)).filter.equals(filter)) { |
230 |
stack.remove(i); |
231 |
} |
232 |
} |
233 |
} |
234 |
|
235 |
/**
|
236 |
* Obtiene la cantidad de filtros en la pila
|
237 |
* @return N?mero de filtros apilados
|
238 |
*/
|
239 |
public int lenght() { |
240 |
return stack.size();
|
241 |
} |
242 |
|
243 |
/**
|
244 |
* Obtiene el filtro apilado de la posici?n i
|
245 |
* @param i Posici?n a acceder en la pila
|
246 |
* @return Filtro
|
247 |
*/
|
248 |
public RasterFilter get(int i) { |
249 |
return ((Filter) stack.get(i)).filter; |
250 |
} |
251 |
|
252 |
/**
|
253 |
* Obtiene el filtro apilado que corresponde con el tipo
|
254 |
* @param type Tipo de filtro
|
255 |
* @return Filtro en caso de que exista un filtro apilado de ese tipo
|
256 |
* o null si no hay ninguno.
|
257 |
*/
|
258 |
public RasterFilter getByType(int type) { |
259 |
for(int i=0;i<lenght();i++){ |
260 |
if(((Filter) stack.get(i)).type == type) |
261 |
return ((Filter) stack.get(i)).filter; |
262 |
} |
263 |
return null; |
264 |
} |
265 |
|
266 |
/**
|
267 |
* Obtiene el tipo del filtro de la pila de la posici?n i
|
268 |
* @param i Posici?n a acceder en la pila
|
269 |
* @return tipo de filtro
|
270 |
*/
|
271 |
public int getType(int i) { |
272 |
return ((Filter) stack.get(i)).type; |
273 |
} |
274 |
|
275 |
/**
|
276 |
* Elimina todos los filtros de la pila
|
277 |
*/
|
278 |
public void clear() { |
279 |
stack.removeAllElements(); |
280 |
} |
281 |
|
282 |
/**
|
283 |
* Sustituye un filtro de una posici?n de la pila por otro
|
284 |
* @param filter
|
285 |
* @param i
|
286 |
*/
|
287 |
public void replace(RasterFilter filter, int i, int type) { |
288 |
((Filter) stack.get(i)).filter = filter;
|
289 |
((Filter) stack.get(i)).type = type;
|
290 |
} |
291 |
|
292 |
/**
|
293 |
* Asigna el raster de entrada inicial
|
294 |
* @param raster
|
295 |
*/
|
296 |
public void setInitRasterBuf(Object raster) { |
297 |
if (raster instanceof RasterBuf) { |
298 |
rasterBuf = (RasterBuf) raster; |
299 |
this.typeFilter = rasterBuf.getDataType();
|
300 |
} |
301 |
|
302 |
if (raster instanceof Image) { |
303 |
image = (Image) raster;
|
304 |
this.typeFilter = RasterBuf.TYPE_IMAGE;
|
305 |
} |
306 |
} |
307 |
|
308 |
/**
|
309 |
* Devuelve el tipo de datos inicial de la pila
|
310 |
* @return Tipo de dato del raster inicial
|
311 |
*/
|
312 |
public int getInitDataType() { |
313 |
return typeFilter;
|
314 |
} |
315 |
|
316 |
/**
|
317 |
* M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en la
|
318 |
* pila y false si no lo est?.
|
319 |
* @param type Tipo de par?metro a comprobar
|
320 |
* @return true si est? en la pila y false si no lo est?
|
321 |
*/
|
322 |
public boolean isActive(int type) { |
323 |
for (int i = stack.size() - 1; i >= 0; i--) { |
324 |
if (((Filter) stack.get(i)).type == type) { |
325 |
return true; |
326 |
} |
327 |
} |
328 |
|
329 |
return false; |
330 |
} |
331 |
|
332 |
/**
|
333 |
* M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en la
|
334 |
* pila y false si no lo est?.
|
335 |
* @param filter Tipo de filtro a comprobar
|
336 |
* @return true si est? en la pila y false si no lo est?
|
337 |
*/
|
338 |
public boolean isActive(RasterFilter filter) { |
339 |
for (int i = stack.size() - 1; i >= 0; i--) { |
340 |
if (((Filter) stack.get(i)).filter.equals(filter)) { |
341 |
return true; |
342 |
} |
343 |
} |
344 |
|
345 |
return false; |
346 |
} |
347 |
|
348 |
/**
|
349 |
* Obtiene el objeto Statistic
|
350 |
* @return
|
351 |
*/
|
352 |
public Statistic getStats() {
|
353 |
return this.stats; |
354 |
} |
355 |
|
356 |
/**
|
357 |
* Aplica los filtros de la pila sobre el buffer correspondiente
|
358 |
* @param dataType
|
359 |
*/
|
360 |
private void executeFilterByDataType(int dataType) { |
361 |
for (int i = stack.size() - 1; i >= 0; i--) { |
362 |
RasterFilter filter = ((Filter) stack.get(i)).filter;
|
363 |
|
364 |
//System.err.println("SACANDO FILTRO "+i+" "+filter.toString());
|
365 |
if (filter.getInRasterDataType() == RasterBuf.TYPE_IMAGE) {
|
366 |
filter.addParam("raster", image);
|
367 |
} else {
|
368 |
filter.addParam("raster", rasterBuf);
|
369 |
} |
370 |
|
371 |
filter.setExtent(extent); |
372 |
filter.setViewPortData(viewPortData); |
373 |
filter.setStep(stepX, stepY); |
374 |
filter.execute(); |
375 |
|
376 |
if (filter.getResult("stats") != null) { |
377 |
this.stats = (Statistic) filter.getResult("stats"); |
378 |
} |
379 |
|
380 |
if (filter.getOutRasterDataType() == RasterBuf.TYPE_IMAGE) {
|
381 |
if (filter.getResult("raster") != null) { |
382 |
this.image = (Image) filter.getResult("raster"); |
383 |
this.rasterBuf = null; |
384 |
} |
385 |
} else {
|
386 |
if (filter.getResult("raster") != null) { |
387 |
this.rasterBuf = (RasterBuf) filter.getResult("raster"); |
388 |
this.image = null; |
389 |
} |
390 |
} |
391 |
} |
392 |
} |
393 |
|
394 |
/**
|
395 |
* Aplica los filtros sobre un Image
|
396 |
* @param image Buffer inicial sobre el que se aplican los filtros
|
397 |
*/
|
398 |
public void execute(Image image) { |
399 |
this.image = image;
|
400 |
executeFilterByDataType(RasterBuf.TYPE_IMAGE); |
401 |
} |
402 |
|
403 |
/**
|
404 |
* Aplica los filtros sobre un RasterBuf
|
405 |
* @param rasterBuf Buffer inicial sobre el que se aplican los filtros
|
406 |
*/
|
407 |
public RasterBuf execute(RasterBuf rasterBuf) {
|
408 |
this.rasterBuf = rasterBuf;
|
409 |
//executeFilterByDataType(RasterBuf.TYPE_SHORT);
|
410 |
executeFilterByDataType(rasterBuf.getDataType()); |
411 |
return this.rasterBuf; |
412 |
} |
413 |
|
414 |
/**
|
415 |
*Muestra el contenido de la pila de filtros para depuraci?n
|
416 |
*/
|
417 |
public void show() { |
418 |
System.out.println("--------------------------------------------"); |
419 |
|
420 |
for (int i = stack.size() - 1; i >= 0; i--) { |
421 |
System.out.println("FILTRO:" + i + " TIPO:" + |
422 |
((Filter) stack.get(i)).type + " TEMP:" + |
423 |
((Filter) stack.get(i)).tmp + " FIL:" + |
424 |
((Filter) stack.get(i)).filter.toString());
|
425 |
} |
426 |
} |
427 |
|
428 |
/**
|
429 |
* Un RasterFilterStack almacena objetos Filter que representan a un filtro. Esta
|
430 |
* clase contiene la informaci?n necesaria para el mantenimiento de de la pila y el
|
431 |
* RasterFilter para cuando se aplica la pila sobre el raster de entrada.
|
432 |
* @author Nacho Brodin <brodin_ign@gva.es>
|
433 |
*/
|
434 |
class Filter { |
435 |
public int type = 0; //Tipo de filtro. Ctes en RasterFilterStackManager |
436 |
public boolean tmp = true; //Flag de temporalidad |
437 |
public RasterFilter filter = null; //Filtro |
438 |
|
439 |
public Filter(int type, RasterFilter filter) { |
440 |
this.filter = filter;
|
441 |
this.type = type;
|
442 |
} |
443 |
} |
444 |
|
445 |
/**
|
446 |
* @param viewPortData The viewPortData to set.
|
447 |
*/
|
448 |
public void setViewPortData(ViewPortData viewPortData) { |
449 |
this.viewPortData = viewPortData;
|
450 |
} |
451 |
|
452 |
/**
|
453 |
* @param extent The extent to set.
|
454 |
*/
|
455 |
public void setExtent(Extent extent) { |
456 |
this.extent = extent;
|
457 |
} |
458 |
|
459 |
/**
|
460 |
* Asigna el valor del paso
|
461 |
* @param stepx paso en X
|
462 |
* @param stepy paso en Y
|
463 |
*/
|
464 |
public void setStep(int[] stepX, int[] stepY){ |
465 |
this.stepX = stepX;
|
466 |
this.stepY = stepY;
|
467 |
} |
468 |
|
469 |
} |