svn-gvsig-desktop / trunk / libraries / libCq CMS for java.old / src / org / cresques / io / raster / RasterFilterStackManager.java @ 4377
History | View | Annotate | Download (35.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.io.IOException; |
27 |
import java.lang.reflect.Constructor; |
28 |
import java.util.ArrayList; |
29 |
import java.util.Hashtable; |
30 |
import java.util.StringTokenizer; |
31 |
import java.util.regex.Matcher; |
32 |
import java.util.regex.Pattern; |
33 |
|
34 |
import org.cresques.io.GeoRasterFile; |
35 |
|
36 |
|
37 |
/**
|
38 |
* Esta clase es de la parte cliente y es la encargada de la gesti?n
|
39 |
* de la pila de filtros. Es la que conoce el orden en que se deben apilar
|
40 |
* estos para que la ejecuci?n sea correcta. Un cliente que desee aplicar un
|
41 |
* filtro deber? introducirlo en la pila usando para ello esta clase.
|
42 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
43 |
*
|
44 |
*/
|
45 |
public class RasterFilterStackManager implements IStackManager { |
46 |
protected RasterFilterStack filterStack = null; |
47 |
private boolean debug = false; |
48 |
protected Hashtable typeFilters = new Hashtable(); |
49 |
public int[] order = null; |
50 |
private ArrayList managers = new ArrayList(); |
51 |
protected ArrayList filterList = null; |
52 |
|
53 |
/**
|
54 |
* Constructor
|
55 |
* @param filterStack
|
56 |
*/
|
57 |
public RasterFilterStackManager(RasterFilterStack filterStack) {
|
58 |
this.filterStack = filterStack;
|
59 |
typeFilters.put("transparency", new Integer(0)); |
60 |
typeFilters.put("enhanced", new Integer(1)); |
61 |
typeFilters.put("computeminmax", new Integer(2)); |
62 |
typeFilters.put("tail", new Integer(3)); |
63 |
typeFilters.put("removebands", new Integer(4)); |
64 |
typeFilters.put("sharpening", new Integer(5)); |
65 |
init(); |
66 |
this.filterStack.setOrder(order);
|
67 |
} |
68 |
|
69 |
/**
|
70 |
* Registra un manager del sistema
|
71 |
* @param manager
|
72 |
*/
|
73 |
protected void register(IStackManager manager) { |
74 |
managers.add(manager); |
75 |
} |
76 |
|
77 |
/**
|
78 |
* Inicializaci?n. Asigna el orden de los filtros
|
79 |
*
|
80 |
*/
|
81 |
protected void init() { |
82 |
order = new int[typeFilters.size()]; |
83 |
order[0] = ((Integer) typeFilters.get("sharpening")).intValue(); |
84 |
order[1] = ((Integer) typeFilters.get("computeminmax")).intValue(); |
85 |
order[2] = ((Integer) typeFilters.get("tail")).intValue(); |
86 |
order[3] = ((Integer) typeFilters.get("enhanced")).intValue(); |
87 |
order[4] = ((Integer) typeFilters.get("transparency")).intValue(); |
88 |
order[5] = ((Integer) typeFilters.get("removebands")).intValue(); |
89 |
} |
90 |
|
91 |
/**
|
92 |
* A?ade un nuevo tipo de filtro
|
93 |
* @param key Nombre del filtro
|
94 |
* @param type Constante entera asignada a ese tipo
|
95 |
*/
|
96 |
protected void addTypeFilter(String key, int type, int position) { |
97 |
typeFilters.put(key, new Integer(type)); |
98 |
|
99 |
int[] newOrder = new int[order.length + 1]; |
100 |
|
101 |
for (int i = 0; i < position; i++) |
102 |
newOrder[i] = order[i]; |
103 |
|
104 |
newOrder[position] = type; |
105 |
|
106 |
for (int i = position + 1; i < newOrder.length; i++) |
107 |
newOrder[i] = order[i - 1];
|
108 |
|
109 |
order = newOrder; |
110 |
this.filterStack.setOrder(order);
|
111 |
} |
112 |
|
113 |
/**
|
114 |
* Obtiene la constante correspondiente a un tipo de filtro
|
115 |
* @param key Clave para obtener la constante que corresponde al nombre del filtro
|
116 |
* @return Tipo de filtro
|
117 |
*/
|
118 |
public int getTypeFilter(String key) { |
119 |
return ((Integer) typeFilters.get(key)).intValue(); |
120 |
} |
121 |
|
122 |
/**
|
123 |
* A?ade un filtro de transparencia
|
124 |
* @param red Intervalos de la banda del rojo a poner transparentes
|
125 |
* @param green Intervalos de la banda del verde a poner transparentes
|
126 |
* @param blue Intervalos de la banda del azul a poner transparentes
|
127 |
* @param alpha Transparencia
|
128 |
* @param transparencyRed Color en la banda del rojo de la transparencia
|
129 |
* @param transparencyGreen Color en la banda del verde de la transparencia
|
130 |
* @param transparencyBlue Color en la banda del azul de la transparencia
|
131 |
*/
|
132 |
public void addTransparencyFilter(ArrayList rangesList, |
133 |
int alpha, int transparencyRed, |
134 |
int transparencyGreen,
|
135 |
int transparencyBlue) {
|
136 |
RasterFilter filtro = null;
|
137 |
|
138 |
switch (filterStack.getDataTypeInFilter(((Integer) typeFilters.get("transparency")).intValue())) { |
139 |
case RasterBuf.TYPE_IMAGE:
|
140 |
filtro = new TransparencyImageFilter();
|
141 |
break;
|
142 |
case RasterBuf.TYPE_SHORT:
|
143 |
case RasterBuf.TYPE_USHORT:
|
144 |
case RasterBuf.TYPE_INT:
|
145 |
filtro = new TransparencyShortFilter();
|
146 |
break;
|
147 |
} |
148 |
|
149 |
if(filtro != null){ |
150 |
filtro.addParam("rangesList", rangesList);
|
151 |
filtro.addParam("alpha", new Integer(alpha)); |
152 |
filtro.addParam("transparencyRed", new Integer(transparencyRed)); |
153 |
filtro.addParam("transparencyGreen", new Integer(transparencyGreen)); |
154 |
filtro.addParam("transparencyBlue", new Integer(transparencyBlue)); |
155 |
|
156 |
//Elimina los filtros que son equivalentes a este
|
157 |
/*for(int i=0;i<filterStack.lenght();i++){
|
158 |
if( filterStack.get(i) instanceof TransparencyImageFilter ||
|
159 |
filterStack.get(i) instanceof TransparencyShortFilter){
|
160 |
|
161 |
//Si este filtro es equivalente a uno de la pila se elimina este
|
162 |
if(((TransparencyFilter)filtro).isEquivalent((TransparencyFilter)filterStack.get(i)))
|
163 |
filterStack.removeFilter(filterStack.get(i));
|
164 |
|
165 |
}
|
166 |
}
|
167 |
|
168 |
//A?ade el filtro si no hay uno equivalente
|
169 |
|
170 |
boolean equivalentFilter = false;
|
171 |
for(int i=0;i<filterStack.lenght();i++){
|
172 |
if( filterStack.get(i) instanceof TransparencyImageFilter ||
|
173 |
filterStack.get(i) instanceof TransparencyShortFilter){
|
174 |
|
175 |
//Si no existe en la pila un filtro equivalente se a?ade
|
176 |
if(((TransparencyFilter)filterStack.get(i)).isEquivalent((TransparencyFilter)filtro)){
|
177 |
equivalentFilter = true;
|
178 |
break;
|
179 |
}
|
180 |
}
|
181 |
}
|
182 |
if(!equivalentFilter)*/
|
183 |
filterStack.removeFilter(((Integer) typeFilters.get("transparency")).intValue()); |
184 |
filterStack.addFilter(((Integer) typeFilters.get("transparency")).intValue(), |
185 |
filtro); |
186 |
this.controlTypes();
|
187 |
} |
188 |
} |
189 |
|
190 |
/**
|
191 |
* Obtiene la lista de rangos del filtro de transparencia de la pila
|
192 |
* @return ArrayList con la lista de rangos. Cada elemento es un objeto TransparencyRange
|
193 |
*/
|
194 |
public ArrayList getTransparencyRGB(){ |
195 |
for (int i = 0; i < filterStack.lenght(); i++) { |
196 |
if (filterStack.get(i) instanceof TransparencyImageFilter || |
197 |
filterStack.get(i) instanceof TransparencyShortFilter) {
|
198 |
return ((TransparencyFilter) filterStack.get(i)).rangesList;
|
199 |
} |
200 |
} |
201 |
return null; |
202 |
} |
203 |
|
204 |
/**
|
205 |
* A?ade un filtro de eliminado de bandas. Las pone a 0
|
206 |
* @param bands
|
207 |
*/
|
208 |
public void addRemoveBands(String bands) { |
209 |
RasterFilter filtro = null;
|
210 |
|
211 |
switch (filterStack.getDataTypeInFilter(((Integer) typeFilters.get("removebands")).intValue())) { |
212 |
case RasterBuf.TYPE_IMAGE:
|
213 |
filtro = new RemoveBandsImageFilter();
|
214 |
break;
|
215 |
case RasterBuf.TYPE_SHORT:
|
216 |
case RasterBuf.TYPE_USHORT:
|
217 |
case RasterBuf.TYPE_INT:
|
218 |
filtro = new RemoveBandsShortFilter();
|
219 |
break;
|
220 |
} |
221 |
|
222 |
if(filtro != null){ |
223 |
filtro.addParam("stats", filterStack.getStats());
|
224 |
filtro.addParam("bands", bands);
|
225 |
|
226 |
filterStack.removeFilter(((Integer) typeFilters.get("removebands")).intValue()); |
227 |
filterStack.addFilter(((Integer) typeFilters.get("removebands")).intValue(), |
228 |
filtro); |
229 |
this.controlTypes();
|
230 |
} |
231 |
} |
232 |
|
233 |
/**
|
234 |
* A?ade un filtro de recorte de colas.
|
235 |
* @param tail porcentaje de recorte
|
236 |
* @param samples porcentaje de muestras tomadas del total de la imagen
|
237 |
*/
|
238 |
public void addTailFilter(double tail, double samples, |
239 |
boolean removeMaxValue) {
|
240 |
if (filterStack.isActive(((Integer) typeFilters.get("tail")).intValue())) { |
241 |
filterStack.removeFilter(((Integer) typeFilters.get("tail")).intValue()); |
242 |
} |
243 |
|
244 |
RasterFilter filtro = null;
|
245 |
|
246 |
switch (filterStack.getDataTypeInFilter(((Integer) typeFilters.get("tail")).intValue())) { |
247 |
case RasterBuf.TYPE_IMAGE:
|
248 |
filtro = new PercentTailTrimImageFilter();
|
249 |
break;
|
250 |
case RasterBuf.TYPE_SHORT:
|
251 |
case RasterBuf.TYPE_USHORT:
|
252 |
case RasterBuf.TYPE_INT:
|
253 |
filtro = new PercentTailTrimShortFilter();
|
254 |
break;
|
255 |
} |
256 |
|
257 |
if(filtro != null){ |
258 |
filtro.addParam("stats", filterStack.getStats());
|
259 |
filtro.addParam("tail", new Double(tail)); |
260 |
filtro.addParam("samples", new Double(samples)); |
261 |
filtro.addParam("remove", new Boolean(removeMaxValue)); |
262 |
|
263 |
filterStack.addFilter(((Integer) typeFilters.get("tail")).intValue(), |
264 |
filtro); |
265 |
this.controlTypes();
|
266 |
} |
267 |
} |
268 |
|
269 |
/**
|
270 |
* A?ade un filtro de realce. Esta versi?n tiene el par?metro para a?adirle el nombre
|
271 |
* del fichero. Esto se usa para que si a un fichero se le ha calculado ya el recorte de colas
|
272 |
* no se vuelva a calcular, evitando as? que si hacemos un draw a una imagen por bloques cada
|
273 |
* bloque tenga un calculo distinto para el recorte.
|
274 |
*/
|
275 |
public void addEnhancedFilter(boolean remove, String fileName) { |
276 |
if (filterStack.isActive(((Integer) typeFilters.get("enhanced")).intValue())) { |
277 |
filterStack.removeFilter(((Integer) typeFilters.get("enhanced")).intValue()); |
278 |
} |
279 |
|
280 |
RasterFilter filtro = null;
|
281 |
|
282 |
switch (filterStack.getDataTypeInFilter(((Integer) typeFilters.get("enhanced")).intValue())) { |
283 |
case RasterBuf.TYPE_IMAGE:
|
284 |
filtro = new LinearEnhancementImageFilter();
|
285 |
break;
|
286 |
case RasterBuf.TYPE_SHORT:
|
287 |
case RasterBuf.TYPE_USHORT:
|
288 |
case RasterBuf.TYPE_INT:
|
289 |
filtro = new LinearEnhancementShortFilter();
|
290 |
break;
|
291 |
} |
292 |
|
293 |
if(filtro != null){ |
294 |
filtro.addParam("stats", filterStack.getStats());
|
295 |
|
296 |
if (remove) {
|
297 |
filtro.addParam("remove", new Boolean(true)); |
298 |
} else {
|
299 |
filtro.addParam("remove", new Boolean(false)); |
300 |
} |
301 |
|
302 |
if (fileName != null) { |
303 |
filtro.addParam("filename", fileName);
|
304 |
} else {
|
305 |
filtro.addParam("filename", new String("")); |
306 |
} |
307 |
|
308 |
filterStack.addFilter(((Integer) typeFilters.get("enhanced")).intValue(), |
309 |
filtro); |
310 |
this.controlTypes();
|
311 |
} |
312 |
} |
313 |
|
314 |
/**
|
315 |
* A?ade un filtro de realce
|
316 |
*/
|
317 |
public void addEnhancedFilter(boolean remove) { |
318 |
addEnhancedFilter(remove, "");
|
319 |
} |
320 |
|
321 |
/**
|
322 |
* A?ade un filtro ComputeMinMax
|
323 |
*/
|
324 |
public void addComputeMinMaxFilter() { |
325 |
if (!filterStack.isActive(((Integer) typeFilters.get("computeminmax")).intValue())) { //Solo lo a?adimos si no est? |
326 |
|
327 |
RasterFilter filtro = null;
|
328 |
|
329 |
switch (filterStack.getDataTypeInFilter(((Integer) typeFilters.get("computeminmax")).intValue())) { |
330 |
case RasterBuf.TYPE_IMAGE:
|
331 |
filtro = new ComputeMinMaxImageFilter();
|
332 |
break;
|
333 |
case RasterBuf.TYPE_SHORT:
|
334 |
case RasterBuf.TYPE_USHORT:
|
335 |
case RasterBuf.TYPE_INT:
|
336 |
filtro = new ComputeMinMaxShortFilter();
|
337 |
break;
|
338 |
} |
339 |
|
340 |
if(filtro != null){ |
341 |
filtro.addParam("stats", filterStack.getStats());
|
342 |
filterStack.addFilter(((Integer) typeFilters.get("computeminmax")).intValue(), |
343 |
filtro); |
344 |
} |
345 |
} |
346 |
|
347 |
this.controlTypes();
|
348 |
} |
349 |
|
350 |
/**
|
351 |
* A?ade un filtro ComputeMinMax
|
352 |
* @param files Ficheros que componen la imagen
|
353 |
* @param i Posici?n de la imagen de refinado dentro de la lista de GeoRasterFile
|
354 |
* @param order Orden de visualizado de las bandas RGB
|
355 |
* @param alpha Alpha aplicado a toda la imagen
|
356 |
* @param method M?todo de calculo del refinado
|
357 |
* @param coef Coeficiente
|
358 |
*/
|
359 |
public void addSharpeningFilter(GeoRasterFile[] files, int i, int[] order, int alpha, String method, double coef, int coefBrovey) { |
360 |
//Si ya hay un filtro de sharpening nos lo cargamos
|
361 |
if (filterStack.isActive(((Integer) typeFilters.get("sharpening")).intValue())) { |
362 |
filterStack.removeFilter(((Integer) typeFilters.get("sharpening")).intValue()); |
363 |
} |
364 |
|
365 |
RasterFilter filtro = null;
|
366 |
|
367 |
switch (filterStack.getDataTypeInFilter(((Integer) typeFilters.get("sharpening")).intValue())) { |
368 |
case RasterBuf.TYPE_IMAGE:
|
369 |
filtro = new SharpeningImageFilter();
|
370 |
break;
|
371 |
case RasterBuf.TYPE_SHORT:
|
372 |
case RasterBuf.TYPE_USHORT:
|
373 |
case RasterBuf.TYPE_INT:
|
374 |
//filtro = new SharpeningShortFilter();
|
375 |
break;
|
376 |
} |
377 |
|
378 |
if(filtro != null){ |
379 |
filtro.addParam("stats", filterStack.getStats());
|
380 |
filtro.addParam("pancromatica", new Integer(i)); |
381 |
filtro.addParam("files", files);
|
382 |
filtro.addParam("order", order);
|
383 |
filtro.addParam("alpha", new Integer(alpha)); |
384 |
filtro.addParam("method", method);
|
385 |
filtro.addParam("coef", new Double(coef)); |
386 |
filtro.addParam("coefBrovey", new Integer(coefBrovey)); |
387 |
filterStack.addFilter(((Integer) typeFilters.get("sharpening")).intValue(),filtro); |
388 |
|
389 |
this.controlTypes();
|
390 |
} |
391 |
} |
392 |
|
393 |
/**
|
394 |
* Obtiene de la pila el primer filtro del tipo solicitado si existe sino devuelve null
|
395 |
* @param type Tipo de filtro
|
396 |
* @return Filtro
|
397 |
*/
|
398 |
public RasterFilter getFilter(String type){ |
399 |
int intType = getTypeFilter(type);
|
400 |
return filterStack.getByType(intType);
|
401 |
|
402 |
} |
403 |
|
404 |
/**
|
405 |
* Obtiene el tipo de filtro a partir del objeto RasterFilter
|
406 |
* @param rasterFilter Objeto RasterFilter del cual se quiere saber que tipo de filtro contiene
|
407 |
* @return Tipo de filtro seg?n las constantes contenidas en RasterFilterStackManager
|
408 |
*/
|
409 |
protected int getType(RasterFilter rasterFilter) { |
410 |
if (rasterFilter instanceof TransparencyFilter) { |
411 |
return 0; |
412 |
} |
413 |
|
414 |
if (rasterFilter instanceof LinearEnhancementFilter) { |
415 |
return 1; |
416 |
} |
417 |
|
418 |
if (rasterFilter instanceof ComputeMinMaxFilter) { |
419 |
return 2; |
420 |
} |
421 |
|
422 |
if (rasterFilter instanceof PercentTailTrimFilter) { |
423 |
return 3; |
424 |
} |
425 |
|
426 |
return -1; |
427 |
} |
428 |
|
429 |
/**
|
430 |
* Controla que los tipos de los filtros de la pila sean correctos, es decir, que
|
431 |
* el tipo de salida de un filtro de salida coincida con el tipo de la entrada del
|
432 |
* siguiente. En caso de no ser as? crea el filtro de tipo adecuado y lo sustituye
|
433 |
* en el no coincidente. Esto es necesario ya que en la eliminaci?n de filtros puede
|
434 |
* quedarse en inconsistencia de tipos.
|
435 |
*/
|
436 |
protected void controlTypes() { |
437 |
if (debug) {
|
438 |
filterStack.show(); |
439 |
} |
440 |
|
441 |
for (int i = filterStack.lenght(); i >= 0; i--) { |
442 |
if ((i - 1) >= 0) { |
443 |
//Para el primer filtro comprobamos con el tipo de dato de entrada a la pila
|
444 |
if (i == filterStack.lenght()) {
|
445 |
if (filterStack.getInitDataType() !=
|
446 |
filterStack.get(i - 1).getInRasterDataType()) {
|
447 |
String oldClass = filterStack.get(i - 1). |
448 |
getClass(). |
449 |
toString(). |
450 |
substring(filterStack.get(i - 1).
|
451 |
getClass(). |
452 |
toString(). |
453 |
lastIndexOf(".") +1, |
454 |
filterStack.get(i - 1).
|
455 |
getClass(). |
456 |
toString(). |
457 |
length()); |
458 |
Pattern p = Pattern.compile(RasterBuf.typesToString(filterStack.get(i - 1).getInRasterDataType())); |
459 |
Matcher m = p.matcher(oldClass);
|
460 |
String newClass = m.replaceAll(RasterBuf.typesToString(filterStack.getInitDataType()));
|
461 |
|
462 |
try {
|
463 |
Class filterClass = Class.forName("org.cresques.io.raster." + |
464 |
newClass); |
465 |
Constructor con = filterClass.getConstructor(null); |
466 |
RasterFilter newFilter = (RasterFilter) con.newInstance(null);
|
467 |
newFilter.params = filterStack.get(i - 1).params;
|
468 |
filterStack.replace(newFilter, i - 1,
|
469 |
this.getType(newFilter));
|
470 |
} catch (Exception e) { |
471 |
e.printStackTrace(); |
472 |
} |
473 |
} |
474 |
|
475 |
//Desde el filtro 2 en adelante se compara la salida de uno con la entrada del siguiente
|
476 |
} else if (filterStack.get(i).getOutRasterDataType() != |
477 |
filterStack.get(i - 1).getInRasterDataType()) {
|
478 |
String oldClass = filterStack.get(i - 1).getClass(). |
479 |
toString().substring(filterStack.get(i - 1).
|
480 |
getClass(). |
481 |
toString(). |
482 |
lastIndexOf(".") + 1, |
483 |
filterStack.get(i - 1).
|
484 |
getClass(). |
485 |
toString(). |
486 |
length()); |
487 |
Pattern p = Pattern.compile(RasterBuf.typesToString(filterStack.get(i - 1).getInRasterDataType())); |
488 |
Matcher m = p.matcher(oldClass);
|
489 |
String newClass = m.replaceAll(RasterBuf.typesToString(filterStack.get(i).getOutRasterDataType()));
|
490 |
|
491 |
try {
|
492 |
Class filterClass = Class.forName("org.cresques.io.raster." + |
493 |
newClass); |
494 |
Constructor con = filterClass.getConstructor(null); |
495 |
RasterFilter newFilter = (RasterFilter) con.newInstance(null);
|
496 |
newFilter.params = filterStack.get(i - 1).params;
|
497 |
filterStack.replace(newFilter, i - 1,
|
498 |
this.getType(newFilter));
|
499 |
} catch (Exception e) { |
500 |
e.printStackTrace(); |
501 |
} |
502 |
} |
503 |
} |
504 |
} |
505 |
|
506 |
if (debug) {
|
507 |
filterStack.show(); |
508 |
} |
509 |
} |
510 |
|
511 |
/**
|
512 |
* M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en la
|
513 |
* pila y false si no lo est?.
|
514 |
* @param filter Tipo de filtro a comprobar
|
515 |
* @return true si est? en la pila y false si no lo est?
|
516 |
*/
|
517 |
public boolean isActive(int type) { |
518 |
return filterStack.isActive(type);
|
519 |
} |
520 |
|
521 |
/**
|
522 |
* Elimina los filtros de la pila de un determinado tipo
|
523 |
* @param type Tipo de filtro a eliminar
|
524 |
*/
|
525 |
public void removeFilter(int type) { |
526 |
filterStack.removeFilter(type); |
527 |
this.controlTypes();
|
528 |
} |
529 |
|
530 |
/**
|
531 |
* Resetea el flag de temporalidad de los filtros de la pila.
|
532 |
* Esto equivale a fijar los filtros que ya existen en la pila. A partir de
|
533 |
* ese momento los filtros que se introduzcan podr?n ser eliminados de golpe
|
534 |
* llamando a la funci?n deleteTempFilters
|
535 |
*/
|
536 |
public void resetTempFilters() { |
537 |
filterStack.resetTempFilters(); |
538 |
} |
539 |
|
540 |
/**
|
541 |
* Elimina los filtros temporales, es decir, todos los filtros introducidos desde
|
542 |
* el ?ltimo resetTempFilters que se ha realizado.
|
543 |
*/
|
544 |
public void deleteTempFilters() { |
545 |
filterStack.deleteTempFilters(); |
546 |
} |
547 |
|
548 |
/**
|
549 |
* Obtiene el objeto de estadisticas asignado a la pila.
|
550 |
* @return
|
551 |
*/
|
552 |
public RasterStats getStackStats() {
|
553 |
return filterStack.getStats();
|
554 |
} |
555 |
|
556 |
/* (non-Javadoc)
|
557 |
* @see org.cresques.io.raster.StackManager#getStringsFromStack()
|
558 |
*/
|
559 |
public ArrayList getStringsFromStack() { |
560 |
filterList = new ArrayList(); |
561 |
|
562 |
for (int i = 0; i < filterStack.lenght(); i++) { |
563 |
RasterFilter rf = filterStack.get(i); |
564 |
|
565 |
if (rf instanceof TransparencyFilter) { |
566 |
filterList.add("filter.transparency.active=true");
|
567 |
for(int j=0;j<((TransparencyFilter) rf).rangesList.size();j++) |
568 |
filterList.add("filter.transparency.transparencyRange"+j+"="+ |
569 |
((TransparencyRange)((TransparencyFilter) rf).rangesList.get(j)).getStrEntry()); |
570 |
} else if (rf instanceof LinearEnhancementFilter) { |
571 |
filterList.add("filter.enhanced.active=true");
|
572 |
filterList.add("filter.enhanced.remove=" +
|
573 |
((LinearEnhancementFilter) rf).getRemoveExtrema() |
574 |
.toString()); |
575 |
} else if (rf instanceof ComputeMinMaxFilter) { |
576 |
filterList.add("filter.computeminmax.active=true");
|
577 |
} else if (rf instanceof PercentTailTrimFilter) { |
578 |
filterList.add("filter.tail.active=true");
|
579 |
filterList.add("filter.tail.value=" +
|
580 |
this.getStackStats().tailPercent);
|
581 |
filterList.add("filter.tail.remove=" +
|
582 |
((PercentTailTrimFilter) rf).removeMaxValue()); |
583 |
} else if (rf instanceof RemoveBandsFilter) { |
584 |
filterList.add("filter.removebands.active=true");
|
585 |
filterList.add("filter.removebands.bands=" +
|
586 |
((RemoveBandsFilter) rf).bands); |
587 |
} else if (rf instanceof SharpeningFilter) { |
588 |
SharpeningFilter sharp = ((SharpeningFilter) rf); |
589 |
filterList.add("filter.pansharpening.active=true");
|
590 |
filterList.add("filter.pansharpening.posPancromatica="+sharp.posPancromatica);
|
591 |
filterList.add("filter.pansharpening.bandOrder="+sharp.bandOrder[0]+","+sharp.bandOrder[1]+","+sharp.bandOrder[2]); |
592 |
for(int file=0;file<sharp.files.length;file++) |
593 |
filterList.add("filter.pansharpening.file"+file+"="+sharp.files[file].getName()); |
594 |
filterList.add("filter.pansharpening.method="+sharp.method);
|
595 |
filterList.add("filter.pansharpening.coefHSL="+sharp.coef);
|
596 |
filterList.add("filter.pansharpening.coefBrovey="+sharp.coefBrovey);
|
597 |
filterList.add("filter.pansharpening.alpha="+sharp.alpha);
|
598 |
} else { //Se recorren todos los managers registrados comprobando si corresponde a la clase del filtro |
599 |
|
600 |
for (int j = 0; j < managers.size(); j++) |
601 |
((IStackManager) managers.get(j)).getStringsFromStack(); |
602 |
} |
603 |
} |
604 |
|
605 |
return filterList;
|
606 |
} |
607 |
|
608 |
/**
|
609 |
* Crea una pila de filtros a partir de un Array de Strings. Cada elemento del array debe
|
610 |
* tener la forma elemento=valor.
|
611 |
* @param filters
|
612 |
*/
|
613 |
public void createStackFromStrings(ArrayList f) { |
614 |
this.createStackFromStrings(f, new Integer(0)); |
615 |
} |
616 |
|
617 |
private GeoRasterFile[] grfList = null; |
618 |
|
619 |
/**
|
620 |
* Crea una pila de filtros a partir de un Array de Strings. Cada elemento del array debe
|
621 |
* tener la forma elemento=valor.
|
622 |
* @param filters
|
623 |
*/
|
624 |
public void createStackFromStrings(ArrayList f, GeoRasterFile[] grfList) { |
625 |
this.grfList = grfList;
|
626 |
this.createStackFromStrings(f, new Integer(0)); |
627 |
} |
628 |
|
629 |
public void createStackFromStrings(ArrayList f, Integer pos) { |
630 |
ArrayList filters = (ArrayList) f.clone(); |
631 |
filterStack.clear(); |
632 |
|
633 |
int filteri = pos.intValue();
|
634 |
|
635 |
//Busca un filtro activo y despu?s todas las propiedades que necesita ese filtro para
|
636 |
//ser creado. Una vez las tiene a?ade en la pila el tipo de filtro.
|
637 |
while ((filters.size() > 0) && (filteri < filters.size())) { |
638 |
String fil = (String) filters.get(filteri); |
639 |
|
640 |
if (fil.startsWith("filter.transparency.active") && |
641 |
getValue(fil).equals("true")) {
|
642 |
filters.remove(filteri); |
643 |
|
644 |
int[][] r = null; |
645 |
int[][] g = null; |
646 |
int[][] b = null; |
647 |
|
648 |
ArrayList list = new ArrayList(); |
649 |
for (int propFilter = 0;propFilter < filters.size();propFilter++) { |
650 |
String elem = (String) filters.get(propFilter); |
651 |
|
652 |
//TODO: Para compatibilidad con versiones anteriores hay que hacer que a partir de esto se cree un filtro
|
653 |
/*if (elem.startsWith("filter.transparency.rangeR")) {
|
654 |
r = stringToRange(getValue(elem));
|
655 |
filters.remove(propFilter);
|
656 |
propFilter--;
|
657 |
}
|
658 |
|
659 |
if (elem.startsWith("filter.transparency.rangeG")) {
|
660 |
g = stringToRange(getValue(elem));
|
661 |
filters.remove(propFilter);
|
662 |
propFilter--;
|
663 |
}
|
664 |
|
665 |
if (elem.startsWith("filter.transparency.rangeB")) {
|
666 |
b = stringToRange(getValue(elem));
|
667 |
filters.remove(propFilter);
|
668 |
propFilter--;
|
669 |
}*/
|
670 |
if (elem.startsWith("filter.transparency.transparencyRange")) { |
671 |
TransparencyRange tr = new TransparencyRange();
|
672 |
String value = getValue(elem);
|
673 |
tr.setStrEntry(value); |
674 |
int[] red = new int[2]; |
675 |
int[] green = new int[2]; |
676 |
int[] blue = new int[2]; |
677 |
boolean and = true; |
678 |
try{
|
679 |
and = TransparencyRange.stringToInterval(value, red, green, blue); |
680 |
if(red[0] == -1) |
681 |
red = null;
|
682 |
if(green[0] == -1) |
683 |
green = null;
|
684 |
if(blue[0] == -1) |
685 |
blue = null;
|
686 |
}catch(IOException e){ |
687 |
e.printStackTrace(); |
688 |
} |
689 |
tr.setAnd(and); |
690 |
tr.setRed(red); |
691 |
tr.setGreen(green); |
692 |
tr.setBlue(blue); |
693 |
list.add(tr); |
694 |
filters.remove(propFilter); |
695 |
propFilter--; |
696 |
} |
697 |
} |
698 |
|
699 |
this.addTransparencyFilter(list, 0x10, 0xff, 0xff, 0xff); |
700 |
|
701 |
filteri = -1;
|
702 |
} |
703 |
|
704 |
if (fil.startsWith("filter.enhanced.active") && |
705 |
getValue(fil).equals("true")) {
|
706 |
filters.remove(filteri); |
707 |
|
708 |
for (int propFilter = 0;propFilter < filters.size();propFilter++) { |
709 |
String elem = (String) filters.get(propFilter); |
710 |
|
711 |
if (elem.startsWith("filter.enhanced.remove")) { |
712 |
this.addEnhancedFilter(Boolean.valueOf(getValue(elem)) |
713 |
.booleanValue()); |
714 |
filters.remove(propFilter); |
715 |
propFilter--; |
716 |
} |
717 |
} |
718 |
|
719 |
this.addComputeMinMaxFilter();
|
720 |
filteri = -1;
|
721 |
} |
722 |
|
723 |
if (fil.startsWith("filter.tail.active") && |
724 |
getValue(fil).equals("true")) {
|
725 |
filters.remove(filteri); |
726 |
this.removeFilter(this.getTypeFilter("computeminmax")); |
727 |
|
728 |
double recorte = 0D; |
729 |
boolean remove = false; |
730 |
|
731 |
for (int propFilter = 0; propFilter < filters.size();propFilter++) { |
732 |
String elem = (String) filters.get(propFilter); |
733 |
|
734 |
if (elem.startsWith("filter.tail.value")) { |
735 |
recorte = Double.parseDouble(getValue(elem));
|
736 |
filters.remove(propFilter); |
737 |
propFilter--; |
738 |
} |
739 |
|
740 |
if (elem.startsWith("filter.tail.remove")) { |
741 |
remove = Boolean.valueOf(getValue(elem)).booleanValue();
|
742 |
filters.remove(propFilter); |
743 |
propFilter--; |
744 |
} |
745 |
} |
746 |
|
747 |
this.addTailFilter(recorte, 0D, remove); |
748 |
filteri = -1;
|
749 |
} |
750 |
|
751 |
if (fil.startsWith("filter.removebands.active") && |
752 |
getValue(fil).equals("true")) {
|
753 |
filters.remove(filteri); |
754 |
|
755 |
for (int propFilter = 0;propFilter < filters.size();propFilter++) { |
756 |
String elem = (String) filters.get(propFilter); |
757 |
|
758 |
if (elem.startsWith("filter.removebands.bands")) { |
759 |
this.addRemoveBands(getValue(elem));
|
760 |
filters.remove(propFilter); |
761 |
propFilter--; |
762 |
} |
763 |
} |
764 |
filteri = -1;
|
765 |
} |
766 |
|
767 |
if (fil.startsWith("filter.pansharpening.active") && |
768 |
getValue(fil).equals("true")) {
|
769 |
filters.remove(filteri); |
770 |
|
771 |
int posPancromatica = 0; |
772 |
int[] bandOrder = {0, 1, 2}; |
773 |
ArrayList files = new ArrayList(); |
774 |
int alpha = 0; |
775 |
String method = ""; |
776 |
double coef = 0D; |
777 |
int coefBrovey = 0; |
778 |
|
779 |
for (int propFilter = 0;propFilter < filters.size();propFilter++) { |
780 |
String elem = (String) filters.get(propFilter); |
781 |
|
782 |
if (elem.startsWith("filter.pansharpening.posPancromatica")) { |
783 |
posPancromatica = Integer.parseInt(getValue(elem));
|
784 |
filters.remove(propFilter); |
785 |
propFilter--; |
786 |
} |
787 |
if (elem.startsWith("filter.pansharpening.bandOrder")) { |
788 |
String rango = getValue(elem);
|
789 |
bandOrder[0] = Integer.parseInt(rango.substring(0, rango.indexOf(","))); |
790 |
bandOrder[1] = Integer.parseInt(rango.substring(rango.indexOf(",") + 1, rango.lastIndexOf(","))); |
791 |
bandOrder[2] = Integer.parseInt(rango.substring(rango.lastIndexOf(",") + 1, rango.length())); |
792 |
filters.remove(propFilter); |
793 |
propFilter--; |
794 |
} |
795 |
if (elem.startsWith("filter.pansharpening.file")) { |
796 |
files.add(getValue(elem)); |
797 |
filters.remove(propFilter); |
798 |
propFilter--; |
799 |
} |
800 |
if (elem.startsWith("filter.pansharpening.alpha")) { |
801 |
alpha = Integer.parseInt(getValue(elem));
|
802 |
filters.remove(propFilter); |
803 |
propFilter--; |
804 |
} |
805 |
if (elem.startsWith("filter.pansharpening.method")) { |
806 |
method = getValue(elem); |
807 |
filters.remove(propFilter); |
808 |
propFilter--; |
809 |
} |
810 |
if (elem.startsWith("filter.pansharpening.coefHSL")) { |
811 |
coef = Double.parseDouble(getValue(elem));
|
812 |
filters.remove(propFilter); |
813 |
propFilter--; |
814 |
} |
815 |
if (elem.startsWith("filter.pansharpening.coefBrovey")) { |
816 |
coefBrovey = Integer.parseInt(getValue(elem));
|
817 |
//filters.remove(propFilter);
|
818 |
//propFilter--;
|
819 |
} |
820 |
} |
821 |
|
822 |
addSharpeningFilter(this.grfList, posPancromatica, bandOrder, alpha, method, coef, coefBrovey);
|
823 |
filteri = -1;
|
824 |
} |
825 |
|
826 |
for (int j = 0; j < managers.size(); j++) |
827 |
((IStackManager) managers.get(j)).createStackFromStrings(filters, new Integer(filteri)); |
828 |
|
829 |
filteri++; |
830 |
} |
831 |
} |
832 |
|
833 |
/**
|
834 |
* Obtiene el elemento de una cadena de la forma elemento=valor
|
835 |
* @param cadena
|
836 |
* @return
|
837 |
*/
|
838 |
public String getElem(String cadena) { |
839 |
if (cadena != null) { |
840 |
return cadena.substring(0, cadena.indexOf("=")); |
841 |
} else {
|
842 |
return null; |
843 |
} |
844 |
} |
845 |
|
846 |
/**
|
847 |
* Obtiene el valor de una cadena de la forma elemento=valor
|
848 |
* @param cadena
|
849 |
* @return
|
850 |
*/
|
851 |
public String getValue(String cadena) { |
852 |
if (cadena != null) { |
853 |
return cadena.substring(cadena.indexOf("=") + 1, cadena.length()); |
854 |
} else {
|
855 |
return null; |
856 |
} |
857 |
} |
858 |
|
859 |
/**
|
860 |
* Convierte un rango contenido en una array doble en una cadena
|
861 |
* de strings para poder salvar a xml
|
862 |
* @param rang
|
863 |
* @return
|
864 |
*/
|
865 |
private String rangeToString(int[][] rang) { |
866 |
StringBuffer rangoStr = new StringBuffer(); |
867 |
|
868 |
if (rang != null) { |
869 |
for (int i = 0; i < rang.length; i++) { |
870 |
rangoStr.append(String.valueOf(rang[i][0]) + ":"); |
871 |
rangoStr.append(String.valueOf(rang[i][1]) + ":"); |
872 |
} |
873 |
|
874 |
String r = rangoStr.toString();
|
875 |
|
876 |
if (r.endsWith(":")) { |
877 |
r = r.substring(0, r.length() - 1); |
878 |
} |
879 |
|
880 |
return r;
|
881 |
} else {
|
882 |
return null; |
883 |
} |
884 |
} |
885 |
|
886 |
/**
|
887 |
* Convierte una cadena en una lista de rangos numericos para poder
|
888 |
* asignar transparencias a la imagen
|
889 |
* @param rang
|
890 |
* @return
|
891 |
*/
|
892 |
private int[][] stringToRange(String rang) { |
893 |
if ((rang != null) && !rang.equals("null")) { |
894 |
ArrayList lista = new ArrayList(); |
895 |
StringTokenizer tokenizer = new StringTokenizer(rang, ":"); |
896 |
|
897 |
while (tokenizer.hasMoreTokens())
|
898 |
lista.add(tokenizer.nextToken()); |
899 |
|
900 |
int[][] intervalos = new int[(int) (lista.size() / 2)][2]; |
901 |
|
902 |
for (int i = 0; i < lista.size(); i = i + 2) { |
903 |
intervalos[i / 2][0] = Integer.valueOf((String) lista.get(i)) |
904 |
.intValue(); |
905 |
intervalos[i / 2][1] = Integer.valueOf((String) lista.get(i + |
906 |
1))
|
907 |
.intValue(); |
908 |
} |
909 |
|
910 |
return intervalos;
|
911 |
} else {
|
912 |
return null; |
913 |
} |
914 |
} |
915 |
} |