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