svn-gvsig-desktop / tags / v1_1_Build_1010 / libraries / libCq_CMS_praster / src / org / cresques / filter / RasterFilterStackManager.java @ 12804
History | View | Annotate | Download (36.2 KB)
1 | 8026 | nacho | /*
|
---|---|---|---|
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 | 12660 | jmvivo | |
85 | 8026 | nacho | 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 | 12660 | jmvivo | |
93 | 8026 | nacho | /**
|
94 | 12660 | jmvivo | * Registra las clases que tendr�n gesti�n de alg�n filtro. Esta funci�n suele llamarse en
|
95 | 8026 | nacho | * 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 | 12660 | jmvivo | |
107 | 8026 | nacho | /**
|
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 | 12660 | jmvivo | |
121 | 8026 | nacho | //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 | 12660 | jmvivo | |
128 | 8026 | nacho | } |
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 | 12660 | jmvivo | else
|
183 | 8026 | nacho | 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 | 12660 | jmvivo | filtro.addParam("rangesList", rangesList);
|
215 | 8026 | nacho | 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 | 12660 | jmvivo | |
220 | 8026 | nacho | //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 | 12660 | jmvivo | |
225 | 8026 | nacho | //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 | 12660 | jmvivo | |
229 | 8026 | nacho | }
|
230 | }
|
||
231 | 12660 | jmvivo | |
232 | 8026 | nacho | //A�ade el filtro si no hay uno equivalente
|
233 | 12660 | jmvivo | |
234 | 8026 | nacho | 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 | 12660 | jmvivo | |
239 | 8026 | nacho | //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 | 12660 | jmvivo | * @return ArrayList con la lista de rangos. Cada elemento es un objeto TransparencyRange
|
257 | 8026 | nacho | */
|
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 | 12660 | jmvivo | |
268 | 8026 | nacho | /**
|
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 | 12660 | jmvivo | |
286 | 8026 | nacho | if(filtro != null){ |
287 | filtro.addParam("stats", filterStack.getStats());
|
||
288 | filtro.addParam("bands", bands);
|
||
289 | 12660 | jmvivo | |
290 | 8026 | nacho | 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 | 12660 | jmvivo | |
331 | 8026 | nacho | 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 | 12660 | jmvivo | |
337 | 8026 | nacho | 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 | 12660 | jmvivo | |
356 | 8026 | nacho | 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 | 12660 | jmvivo | |
378 | 8026 | nacho | if (remove) {
|
379 | filtro.addParam("remove", new Boolean(true)); |
||
380 | } else {
|
||
381 | filtro.addParam("remove", new Boolean(false)); |
||
382 | } |
||
383 | 12660 | jmvivo | |
384 | 8026 | nacho | if (fileName != null) { |
385 | filtro.addParam("filename", fileName);
|
||
386 | } else {
|
||
387 | filtro.addParam("filename", new String("")); |
||
388 | } |
||
389 | 12660 | jmvivo | |
390 | 8026 | nacho | 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 | 12660 | jmvivo | |
422 | 8026 | nacho | 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 | 12660 | jmvivo | |
432 | 8026 | nacho | /**
|
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 | 12660 | jmvivo | |
441 | 8026 | nacho | } |
442 | 12660 | jmvivo | |
443 | 8026 | nacho | /**
|
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 | 12660 | jmvivo | if (rasterFilter instanceof TransparencyFilter) |
450 | 8026 | nacho | return ((Integer)typeFilters.get("transparency")).intValue(); |
451 | 12660 | jmvivo | |
452 | if (rasterFilter instanceof LinearEnhancementFilter) |
||
453 | 8026 | nacho | return ((Integer)typeFilters.get("enhanced")).intValue(); |
454 | 12660 | jmvivo | |
455 | 8026 | nacho | if (rasterFilter instanceof ComputeMinMaxFilter) |
456 | return ((Integer)typeFilters.get("computeminmax")).intValue(); |
||
457 | 12660 | jmvivo | |
458 | if (rasterFilter instanceof PercentTailTrimFilter) |
||
459 | 8026 | nacho | return ((Integer)typeFilters.get("tail")).intValue(); |
460 | 12660 | jmvivo | |
461 | if (rasterFilter instanceof RemoveBandsFilter) |
||
462 | 8026 | nacho | return ((Integer)typeFilters.get("removebands")).intValue(); |
463 | 12660 | jmvivo | |
464 | 8026 | nacho | 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 | 12660 | jmvivo | |
470 | 8026 | nacho | return -1; |
471 | } |
||
472 | |||
473 | /**
|
||
474 | * Sustituye el tipo de filtro de strPackage por el de newClass
|
||
475 | 12660 | jmvivo | * @param strPackage cadena con el paquete y filtro a sustituir,
|
476 | 8026 | nacho | * 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 | 12660 | jmvivo | |
494 | 8026 | nacho | /**
|
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 | 10645 | nacho | packageFilter = classFilter.substring(classFilter.indexOf(" ") + 1, classFilter.lastIndexOf(".")); |
507 | 8026 | nacho | oldClass = classFilter.substring(classFilter.lastIndexOf(".") +1, classFilter.length()); |
508 | }catch(ArrayIndexOutOfBoundsException ex){ |
||
509 | return;
|
||
510 | } |
||
511 | 12660 | jmvivo | |
512 | 8026 | nacho | 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 | 12660 | jmvivo | |
521 | 8026 | nacho | 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 | 12660 | jmvivo | |
539 | 8026 | nacho | 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 | 12660 | jmvivo | if (debug)
|
554 | 8026 | nacho | filterStack.show(); |
555 | 12660 | jmvivo | |
556 | 8026 | nacho | } |
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 | 12660 | jmvivo | if (((TransparencyFilter) rf).rangesList == null){ |
613 | continue;
|
||
614 | } |
||
615 | 8026 | nacho | filterList.add("filter.transparency.active=true");
|
616 | for(int j=0;j<((TransparencyFilter) rf).rangesList.size();j++) |
||
617 | filterList.add("filter.transparency.transparencyRange"+j+"="+ |
||
618 | ((TransparencyRange)((TransparencyFilter) rf).rangesList.get(j)).getStrEntry()); |
||
619 | } else if (rf instanceof LinearEnhancementFilter) { |
||
620 | filterList.add("filter.enhanced.active=true");
|
||
621 | filterList.add("filter.enhanced.remove=" +
|
||
622 | ((LinearEnhancementFilter) rf).getRemoveExtrema() |
||
623 | .toString()); |
||
624 | } else if (rf instanceof ComputeMinMaxFilter) { |
||
625 | filterList.add("filter.computeminmax.active=true");
|
||
626 | } else if (rf instanceof PercentTailTrimFilter) { |
||
627 | filterList.add("filter.tail.active=true");
|
||
628 | filterList.add("filter.tail.value=" +
|
||
629 | this.getStackStats().tailPercent);
|
||
630 | filterList.add("filter.tail.remove=" +
|
||
631 | ((PercentTailTrimFilter) rf).removeMaxValue()); |
||
632 | } else if (rf instanceof RemoveBandsFilter) { |
||
633 | filterList.add("filter.removebands.active=true");
|
||
634 | filterList.add("filter.removebands.bands=" +
|
||
635 | ((RemoveBandsFilter) rf).bands); |
||
636 | } else { //Se recorren todos los managers registrados comprobando si corresponde a la clase del filtro |
||
637 | /*for (int j = 0; j < filterStack.managers.size(); j++){
|
||
638 | filterList = ((IStackManager) filterStack.managers.get(j)).getStringsFromStack(filterList, rf);
|
||
639 | }*/
|
||
640 | for (int j = 0; j < managers.size(); j++){ |
||
641 | filterList = ((IStackManager) managers.get(j)).getStringsFromStack(filterList, rf); |
||
642 | } |
||
643 | } |
||
644 | } |
||
645 | |||
646 | return filterList;
|
||
647 | } |
||
648 | |||
649 | /* (non-Javadoc)
|
||
650 | * @see org.cresques.io.raster.StackManager#getStringsFromStack()
|
||
651 | */
|
||
652 | public ArrayList getStringsFromStack(ArrayList filterList, RasterFilter rf){ |
||
653 | return null; |
||
654 | } |
||
655 | 12660 | jmvivo | |
656 | 8026 | nacho | /**
|
657 | * Crea una pila de filtros a partir de un Array de Strings. Cada elemento del array debe
|
||
658 | * tener la forma elemento=valor.
|
||
659 | * @param filters
|
||
660 | */
|
||
661 | public void createStackFromStrings(ArrayList f) { |
||
662 | this.grfList = null; |
||
663 | this.createStackFromStrings(f, new Integer(0)); |
||
664 | } |
||
665 | 12660 | jmvivo | |
666 | 8026 | nacho | /**
|
667 | * Crea una pila de filtros a partir de un Array de Strings. Cada elemento del array debe
|
||
668 | * tener la forma elemento=valor.
|
||
669 | * @param filters
|
||
670 | */
|
||
671 | public void createStackFromStrings(ArrayList f, GeoRasterFile[] grfList) { |
||
672 | this.grfList = grfList;
|
||
673 | this.createStackFromStrings(f, new Integer(0)); |
||
674 | } |
||
675 | 12660 | jmvivo | |
676 | 8026 | nacho | public void createStackFromStrings(ArrayList f, Integer pos) { |
677 | ArrayList filters = (ArrayList) f.clone(); |
||
678 | filterStack.clear(); |
||
679 | |||
680 | int filteri = pos.intValue();
|
||
681 | |||
682 | //Busca un filtro activo y despu�s todas las propiedades que necesita ese filtro para
|
||
683 | //ser creado. Una vez las tiene a�ade en la pila el tipo de filtro.
|
||
684 | while ((filters.size() > 0) && (filteri < filters.size())) { |
||
685 | String fil = (String) filters.get(filteri); |
||
686 | |||
687 | if (fil.startsWith("filter.transparency.active") && |
||
688 | getValue(fil).equals("true")) {
|
||
689 | filters.remove(filteri); |
||
690 | 12660 | jmvivo | |
691 | 8026 | nacho | ArrayList list = new ArrayList(); |
692 | for (int propFilter = 0;propFilter < filters.size();propFilter++) { |
||
693 | String elem = (String) filters.get(propFilter); |
||
694 | |||
695 | if (elem.startsWith("filter.transparency.transparencyRange")) { |
||
696 | TransparencyRange tr = new TransparencyRange();
|
||
697 | String value = getValue(elem);
|
||
698 | tr.setStrEntry(value); |
||
699 | int[] red = new int[2]; |
||
700 | int[] green = new int[2]; |
||
701 | int[] blue = new int[2]; |
||
702 | boolean and = true; |
||
703 | try{
|
||
704 | and = TransparencyRange.stringToInterval(value, red, green, blue); |
||
705 | if(red[0] == -1) |
||
706 | red = null;
|
||
707 | if(green[0] == -1) |
||
708 | green = null;
|
||
709 | if(blue[0] == -1) |
||
710 | blue = null;
|
||
711 | }catch(IOException e){ |
||
712 | e.printStackTrace(); |
||
713 | } |
||
714 | tr.setAnd(and); |
||
715 | tr.setRed(red); |
||
716 | tr.setGreen(green); |
||
717 | tr.setBlue(blue); |
||
718 | list.add(tr); |
||
719 | filters.remove(propFilter); |
||
720 | propFilter--; |
||
721 | } |
||
722 | } |
||
723 | |||
724 | this.addTransparencyFilter(list, 0x0, 0xff, 0xff, 0xff); |
||
725 | 12660 | jmvivo | |
726 | 8026 | nacho | filteri = -1;
|
727 | } |
||
728 | |||
729 | if (fil.startsWith("filter.enhanced.active") && |
||
730 | getValue(fil).equals("true")) {
|
||
731 | filters.remove(filteri); |
||
732 | |||
733 | for (int propFilter = 0;propFilter < filters.size();propFilter++) { |
||
734 | String elem = (String) filters.get(propFilter); |
||
735 | |||
736 | if (elem.startsWith("filter.enhanced.remove")) { |
||
737 | this.addEnhancedFilter(Boolean.valueOf(getValue(elem)) |
||
738 | .booleanValue()); |
||
739 | filters.remove(propFilter); |
||
740 | propFilter--; |
||
741 | } |
||
742 | } |
||
743 | |||
744 | this.addComputeMinMaxFilter();
|
||
745 | filteri = -1;
|
||
746 | } |
||
747 | |||
748 | if (fil.startsWith("filter.tail.active") && |
||
749 | getValue(fil).equals("true")) {
|
||
750 | filters.remove(filteri); |
||
751 | this.removeFilter(this.getTypeFilter("computeminmax")); |
||
752 | |||
753 | double recorte = 0D; |
||
754 | boolean remove = false; |
||
755 | |||
756 | for (int propFilter = 0; propFilter < filters.size();propFilter++) { |
||
757 | String elem = (String) filters.get(propFilter); |
||
758 | |||
759 | if (elem.startsWith("filter.tail.value")) { |
||
760 | recorte = Double.parseDouble(getValue(elem));
|
||
761 | filters.remove(propFilter); |
||
762 | propFilter--; |
||
763 | } |
||
764 | |||
765 | if (elem.startsWith("filter.tail.remove")) { |
||
766 | remove = Boolean.valueOf(getValue(elem)).booleanValue();
|
||
767 | filters.remove(propFilter); |
||
768 | propFilter--; |
||
769 | } |
||
770 | } |
||
771 | |||
772 | this.addTailFilter(recorte, 0D, remove); |
||
773 | filteri = -1;
|
||
774 | } |
||
775 | |||
776 | if (fil.startsWith("filter.removebands.active") && |
||
777 | getValue(fil).equals("true")) {
|
||
778 | filters.remove(filteri); |
||
779 | |||
780 | for (int propFilter = 0;propFilter < filters.size();propFilter++) { |
||
781 | String elem = (String) filters.get(propFilter); |
||
782 | |||
783 | if (elem.startsWith("filter.removebands.bands")) { |
||
784 | this.addRemoveBands(getValue(elem));
|
||
785 | filters.remove(propFilter); |
||
786 | propFilter--; |
||
787 | } |
||
788 | } |
||
789 | filteri = -1;
|
||
790 | } |
||
791 | 12660 | jmvivo | |
792 | 8026 | nacho | for (int j = 0; j < managers.size(); j++){ |
793 | filteri = ((IStackManager) managers.get(j)).createStackFromStrings(filters, fil, this.grfList, filteri);
|
||
794 | this.grfList = null; |
||
795 | } |
||
796 | |||
797 | filteri++; |
||
798 | } |
||
799 | } |
||
800 | |||
801 | /**
|
||
802 | * Obtiene el elemento de una cadena de la forma elemento=valor
|
||
803 | * @param cadena
|
||
804 | * @return
|
||
805 | */
|
||
806 | public static String getElem(String cadena) { |
||
807 | if (cadena != null) { |
||
808 | return cadena.substring(0, cadena.indexOf("=")); |
||
809 | } else {
|
||
810 | return null; |
||
811 | } |
||
812 | } |
||
813 | |||
814 | /**
|
||
815 | * Obtiene el valor de una cadena de la forma elemento=valor
|
||
816 | * @param cadena
|
||
817 | * @return
|
||
818 | */
|
||
819 | public static String getValue(String cadena) { |
||
820 | if (cadena != null) { |
||
821 | return cadena.substring(cadena.indexOf("=") + 1, cadena.length()); |
||
822 | } else {
|
||
823 | return null; |
||
824 | } |
||
825 | } |
||
826 | |||
827 | /**
|
||
828 | * Convierte un rango contenido en una array doble en una cadena
|
||
829 | * de strings para poder salvar a xml
|
||
830 | * @param rang
|
||
831 | * @return
|
||
832 | */
|
||
833 | private String rangeToString(int[][] rang) { |
||
834 | StringBuffer rangoStr = new StringBuffer(); |
||
835 | |||
836 | if (rang != null) { |
||
837 | for (int i = 0; i < rang.length; i++) { |
||
838 | rangoStr.append(String.valueOf(rang[i][0]) + ":"); |
||
839 | rangoStr.append(String.valueOf(rang[i][1]) + ":"); |
||
840 | } |
||
841 | |||
842 | String r = rangoStr.toString();
|
||
843 | |||
844 | if (r.endsWith(":")) { |
||
845 | r = r.substring(0, r.length() - 1); |
||
846 | } |
||
847 | |||
848 | return r;
|
||
849 | } else {
|
||
850 | return null; |
||
851 | } |
||
852 | } |
||
853 | |||
854 | /**
|
||
855 | * Convierte una cadena en una lista de rangos numericos para poder
|
||
856 | * asignar transparencias a la imagen
|
||
857 | * @param rang
|
||
858 | * @return
|
||
859 | */
|
||
860 | private int[][] stringToRange(String rang) { |
||
861 | if ((rang != null) && !rang.equals("null")) { |
||
862 | ArrayList lista = new ArrayList(); |
||
863 | StringTokenizer tokenizer = new StringTokenizer(rang, ":"); |
||
864 | |||
865 | while (tokenizer.hasMoreTokens())
|
||
866 | lista.add(tokenizer.nextToken()); |
||
867 | |||
868 | int[][] intervalos = new int[(int) (lista.size() / 2)][2]; |
||
869 | |||
870 | for (int i = 0; i < lista.size(); i = i + 2) { |
||
871 | intervalos[i / 2][0] = Integer.valueOf((String) lista.get(i)) |
||
872 | .intValue(); |
||
873 | intervalos[i / 2][1] = Integer.valueOf((String) lista.get(i + |
||
874 | 1))
|
||
875 | .intValue(); |
||
876 | } |
||
877 | |||
878 | return intervalos;
|
||
879 | } else {
|
||
880 | return null; |
||
881 | } |
||
882 | } |
||
883 | |||
884 | /**
|
||
885 | * Obtiene la pila de filtros
|
||
886 | * @return RasterFilterStack
|
||
887 | */
|
||
888 | public RasterFilterStack getFilterStack() {
|
||
889 | return filterStack;
|
||
890 | } |
||
891 | |||
892 | /**
|
||
893 | * Obtiene la Hashable que contiene los tipos de filtros registrados
|
||
894 | * @return Hashtable
|
||
895 | */
|
||
896 | public Hashtable getTypeFilters() { |
||
897 | return typeFilters;
|
||
898 | } |
||
899 | |||
900 | public int createStackFromStrings(ArrayList filters, String fil, GeoRasterFile[] grfList, int filteri) { |
||
901 | // TODO Auto-generated method stub
|
||
902 | return filteri;
|
||
903 | } |
||
904 | 12660 | jmvivo | |
905 | 8026 | nacho | /**
|
906 | 12660 | jmvivo | * Carga una clase pasada por par�metro. Como argumento del constructor de la clase se
|
907 | 8026 | nacho | * pasar� un RasterFilterStackManager. Esto es usado para instanciar los gestores de filtros
|
908 | * registrados
|
||
909 | * @param clase Clase a instanciar
|
||
910 | * @param stackManager Par�metro del constructor de la clase a instanciar
|
||
911 | * @return Objeto que corresponde a la instancia de la clase pasada.
|
||
912 | */
|
||
913 | public static Object loadClass(Class clase, RasterFilterStackManager stackManager) { |
||
914 | Object obj = null; |
||
915 | Class [] args = {RasterFilterStackManager.class}; |
||
916 | try {
|
||
917 | Constructor hazNuevo = clase.getConstructor(args);
|
||
918 | Object [] args2 = {stackManager}; |
||
919 | obj = hazNuevo.newInstance(args2); |
||
920 | } catch (SecurityException e) { |
||
921 | // TODO Auto-generated catch block
|
||
922 | e.printStackTrace(); |
||
923 | } catch (NoSuchMethodException e) { |
||
924 | // TODO Auto-generated catch block
|
||
925 | e.printStackTrace(); |
||
926 | } catch (IllegalArgumentException e) { |
||
927 | // TODO Auto-generated catch block
|
||
928 | e.printStackTrace(); |
||
929 | } catch (InstantiationException e) { |
||
930 | // TODO Auto-generated catch block
|
||
931 | e.printStackTrace(); |
||
932 | } catch (IllegalAccessException e) { |
||
933 | // TODO Auto-generated catch block
|
||
934 | e.printStackTrace(); |
||
935 | } catch (InvocationTargetException e) { |
||
936 | // TODO Auto-generated catch block
|
||
937 | e.printStackTrace(); |
||
938 | } |
||
939 | return obj;
|
||
940 | } |
||
941 | 12660 | jmvivo | |
942 | 8026 | nacho | /**
|
943 | * Obtiene el manager registrado a partir de la clase
|
||
944 | * @return
|
||
945 | */
|
||
946 | public IStackManager getManagerByClass(Class c){ |
||
947 | for (int j = 0; j < managers.size(); j++){ |
||
948 | if(managers.get(j).getClass().equals(c))
|
||
949 | return (IStackManager)managers.get(j);
|
||
950 | } |
||
951 | return null; |
||
952 | } |
||
953 | |||
954 | } |