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