svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / io / raster / RasterFilterStack.java @ 1809
History | View | Annotate | Download (6.53 KB)
1 |
/*
|
---|---|
2 |
* Created on 12-abr-2005
|
3 |
*/
|
4 |
package org.cresques.io.raster; |
5 |
|
6 |
import java.awt.Image; |
7 |
import java.util.Vector; |
8 |
|
9 |
/**
|
10 |
*
|
11 |
* @author Nacho Brodin <brodin_ign@gva.es>
|
12 |
*
|
13 |
* Esta clase es la pila de filtros
|
14 |
*/
|
15 |
public class RasterFilterStack{ |
16 |
|
17 |
private Image image = null; |
18 |
private RasterBuf rasterBuf = null; |
19 |
private RasterFilter rasterFilter = null; |
20 |
private int[] order = null; |
21 |
private RasterStats stats = null; |
22 |
|
23 |
Vector stack = new Vector(); |
24 |
private int typeFilter = -2; |
25 |
|
26 |
class Filter{ |
27 |
|
28 |
public int type = 0; //Tipo de filtro |
29 |
public boolean tmp = true; //Flag de temporalidad |
30 |
public RasterFilter filter = null; //Filtro |
31 |
|
32 |
public Filter(int type, RasterFilter filter){ |
33 |
this.filter = filter;
|
34 |
this.type = type;
|
35 |
} |
36 |
} |
37 |
|
38 |
public RasterFilterStack(RasterStats stats){
|
39 |
this.stats = stats;
|
40 |
} |
41 |
|
42 |
/**
|
43 |
* Resetea la pila de filtros temporales
|
44 |
*/
|
45 |
public void resetTempFilters(){ |
46 |
for(int i=stack.size()-1;i>=0;i--) |
47 |
((Filter)stack.get(i)).tmp = false; |
48 |
} |
49 |
|
50 |
/**
|
51 |
*Elimina del stack los filtros temporales
|
52 |
*/
|
53 |
public void deleteTempFilters(){ |
54 |
for(int i=stack.size()-1;i>=0;i--){ |
55 |
if(((Filter)stack.get(i)).tmp) |
56 |
stack.remove(i); |
57 |
} |
58 |
sort(); |
59 |
} |
60 |
|
61 |
/**
|
62 |
* A?ade un filtro a la pila
|
63 |
* @param type Tipo de filtro
|
64 |
* @param filter filtro a?adido
|
65 |
*/
|
66 |
public void addFilter(int type, RasterFilter filter){ |
67 |
stack.add(new Filter(type, filter)); |
68 |
this.sort();
|
69 |
this.show();
|
70 |
} |
71 |
|
72 |
/**
|
73 |
* Elimina el ?ltimo filtro de la pila
|
74 |
*/
|
75 |
public void removeFilter(){ |
76 |
stack.remove(stack.size()-1);
|
77 |
} |
78 |
|
79 |
/**
|
80 |
* Asigna el tipo filtro
|
81 |
* @param type
|
82 |
*/
|
83 |
public void setDataTypeInFilter(int type){ |
84 |
this.typeFilter = type;
|
85 |
} |
86 |
|
87 |
/**
|
88 |
* Devuelve true si typeA est? por encima de typeB en la pila y false si no lo est?
|
89 |
* @param typeA
|
90 |
* @param typeB
|
91 |
* @return
|
92 |
*/
|
93 |
private boolean isOnTop(int typeA, int typeB){ |
94 |
int posA=-1; |
95 |
int posB=-1; |
96 |
for(int i=0;i<order.length;i++){ |
97 |
if(order[i] == typeA)posA = i;
|
98 |
if(order[i] == typeB)posB = i;
|
99 |
if(posA != -1 && posB != -1)break; |
100 |
} |
101 |
if(posA<posB)return true; |
102 |
else return false; |
103 |
} |
104 |
|
105 |
/**
|
106 |
* Dado un tipo de filtro calcula a partir de la pila en que posici?n est? situado y
|
107 |
* obtiene el tipo de dato de la salida anterior. Esto le dir? al cliente de que tipo
|
108 |
* es el filtro que tiene que introducir.
|
109 |
* @param type
|
110 |
*/
|
111 |
public int getDataTypeInFilter(int type){ |
112 |
for(int i=0; i<stack.size(); i++){ |
113 |
if( isOnTop(((Filter)stack.get(i)).type , type ) ){ |
114 |
return ((Filter)stack.get(i)).filter.getOutRasterDataType(); |
115 |
} |
116 |
} |
117 |
return this.typeFilter; |
118 |
} |
119 |
|
120 |
/**
|
121 |
* Asigna el vector para la ordenaci?n por tipos
|
122 |
* @param order
|
123 |
*/
|
124 |
public void setOrder(int[] order){ |
125 |
this.order = order;
|
126 |
} |
127 |
|
128 |
/**
|
129 |
* Ordena los filtros por el orden establecido
|
130 |
*/
|
131 |
public void sort(){ |
132 |
Vector aux = new Vector(); |
133 |
for(int norden=order.length-1;norden>=0;norden--){ |
134 |
for(int i=stack.size()-1;i>=0;i--){ |
135 |
if(((Filter)stack.get(i)).type == order[norden]){ |
136 |
aux.add(stack.get(i)); |
137 |
} |
138 |
} |
139 |
} |
140 |
stack = aux; |
141 |
aux = null;
|
142 |
//this.setDataTypeInFilter(((Filter)stack.get(stack.size()-1)).filter.getOutRasterDataType());
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* Elimina los filtros de la pila de un determinado tipo
|
147 |
* @param type
|
148 |
*/
|
149 |
public void removeFilter(int type){ |
150 |
for(int i=stack.size()-1;i>=0;i--){ |
151 |
if(((Filter)stack.get(i)).type == type){ |
152 |
stack.remove(i); |
153 |
} |
154 |
} |
155 |
} |
156 |
|
157 |
/**
|
158 |
* Elimina un filtro concreto de la pila
|
159 |
* @param type
|
160 |
*/
|
161 |
public void removeFilter(RasterFilter filter){ |
162 |
for(int i=stack.size()-1;i>=0;i--){ |
163 |
if(((Filter)stack.get(i)).filter.equals(filter)){ |
164 |
stack.remove(i); |
165 |
} |
166 |
} |
167 |
} |
168 |
|
169 |
/**
|
170 |
* Obtiene la cantidad de filtros en la pila
|
171 |
* @return N?mero de filtros apilados
|
172 |
*/
|
173 |
public int lenght(){ |
174 |
return stack.size();
|
175 |
} |
176 |
|
177 |
/**
|
178 |
* Obtiene el filtro apilado de la posici?n i
|
179 |
* @param i Posici?n a acceder en la pila
|
180 |
* @return Filtro
|
181 |
*/
|
182 |
public RasterFilter get(int i){ |
183 |
return ((Filter)stack.get(i)).filter; |
184 |
} |
185 |
|
186 |
/**
|
187 |
* Elimina todos los filtros de la pila
|
188 |
*/
|
189 |
public void clear(){ |
190 |
stack.removeAllElements(); |
191 |
} |
192 |
|
193 |
/**
|
194 |
* Asigna el raster de entrada inicial
|
195 |
* @param raster
|
196 |
*/
|
197 |
public void setInitRasterBuf(Object raster){ |
198 |
if(raster instanceof RasterBuf) |
199 |
rasterBuf = (RasterBuf)raster; |
200 |
if(raster instanceof Image) |
201 |
image = (Image)raster;
|
202 |
} |
203 |
|
204 |
/**
|
205 |
* M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en la
|
206 |
* pila y false si no lo est?.
|
207 |
* @param type Tipo de par?metro a comprobar
|
208 |
* @return true si est? en la pila y false si no lo est?
|
209 |
*/
|
210 |
public boolean isActive(int type){ |
211 |
for(int i=stack.size()-1;i>=0;i--){ |
212 |
if(((Filter)stack.get(i)).type == type) |
213 |
return true; |
214 |
} |
215 |
return false; |
216 |
} |
217 |
|
218 |
/**
|
219 |
* M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en la
|
220 |
* pila y false si no lo est?.
|
221 |
* @param filter Tipo de filtro a comprobar
|
222 |
* @return true si est? en la pila y false si no lo est?
|
223 |
*/
|
224 |
public boolean isActive(RasterFilter filter){ |
225 |
for(int i=stack.size()-1;i>=0;i--){ |
226 |
if(((Filter)stack.get(i)).filter.equals(filter)) |
227 |
return true; |
228 |
} |
229 |
return false; |
230 |
} |
231 |
|
232 |
/**
|
233 |
* Obtiene el objeto RasterStats
|
234 |
* @return
|
235 |
*/
|
236 |
public RasterStats getStats(){
|
237 |
return this.stats; |
238 |
} |
239 |
|
240 |
/**
|
241 |
*
|
242 |
* @param dataType
|
243 |
*/
|
244 |
private void executeFilterByDataType(int dataType){ |
245 |
for(int i=stack.size()-1;i>=0;i--){ |
246 |
RasterFilter filter = ((Filter)stack.get(i)).filter;
|
247 |
if(dataType == RasterBuf.TYPE_IMAGE)
|
248 |
filter.addParam("raster", image);
|
249 |
else
|
250 |
filter.addParam("raster", rasterBuf);
|
251 |
|
252 |
filter.execute(); |
253 |
|
254 |
if(filter.getResult("stats")!=null){ |
255 |
this.stats = (RasterStats)filter.getResult("stats"); |
256 |
//System.out.println("OBTENIENDO STATS="+stats.maxBandValue[0]+" "+stats.maxBandValue[1]+" "+stats.maxBandValue[2]);
|
257 |
} |
258 |
|
259 |
if(filter.getOutRasterDataType() == RasterBuf.TYPE_IMAGE)
|
260 |
if(filter.getResult("raster")!=null) |
261 |
this.image = (Image)filter.getResult("raster"); |
262 |
else
|
263 |
if(filter.getResult("raster")!=null) |
264 |
this.rasterBuf = (RasterBuf)filter.getResult("raster"); |
265 |
|
266 |
} |
267 |
} |
268 |
|
269 |
/**
|
270 |
*Aplica los filtros sobre un Image
|
271 |
*/
|
272 |
public void execute(Image image){ |
273 |
this.image = image;
|
274 |
executeFilterByDataType(RasterBuf.TYPE_IMAGE); |
275 |
} |
276 |
|
277 |
/**
|
278 |
*Aplica los filtros sobre un RasterBuf
|
279 |
*/
|
280 |
public void execute(RasterBuf rasterBuf){ |
281 |
this.rasterBuf = rasterBuf;
|
282 |
executeFilterByDataType(RasterBuf.TYPE_SHORT); |
283 |
} |
284 |
|
285 |
public void show(){ |
286 |
for(int i=stack.size()-1;i>=0;i--){ |
287 |
System.out.println( "FILTRO:"+i+ |
288 |
" TIPO:"+((Filter)stack.get(i)).type+ |
289 |
" TEMP:"+((Filter)stack.get(i)).tmp+ |
290 |
" FIL:"+((Filter)stack.get(i)).filter.toString()); |
291 |
} |
292 |
} |
293 |
|
294 |
} |