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