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