root / trunk / libraries / libRaster / src / org / gvsig / raster / util / TransparencyRange.java @ 12088
History | View | Annotate | Download (9.92 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.raster.util; |
20 |
|
21 |
import java.io.IOException; |
22 |
|
23 |
import org.gvsig.raster.dataset.RasterDataset; |
24 |
|
25 |
/**
|
26 |
* Clase que representa a un conjunto de pixeles con
|
27 |
* transparencia. Incluye los rangos de transparencia para cada banda,
|
28 |
* la cadena que va en la lista y la operaci?n que se realiza entre
|
29 |
* ellos. Un And significa que ser?n transparentes todos los pixeles
|
30 |
* que cumplan con R con G y con B. Un Or significar? que tendr?n transparencia
|
31 |
* todos los pixeles que cumplan con R con G o con B.
|
32 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
33 |
*
|
34 |
*/
|
35 |
public class TransparencyRange{ |
36 |
private String strEntry = null; |
37 |
private int[] red = null; |
38 |
private int[] green = null; |
39 |
private int[] blue = null; |
40 |
private boolean and = true; |
41 |
private int alpha = 0; |
42 |
|
43 |
/**
|
44 |
* Constructor
|
45 |
*/
|
46 |
public TransparencyRange(){
|
47 |
|
48 |
} |
49 |
|
50 |
/**
|
51 |
* Crea un objeto TransparencyRange a partir de una cadena bien formada
|
52 |
* @param value cadena bien formada que representa un intervalo
|
53 |
*/
|
54 |
public TransparencyRange(String value) { |
55 |
try {
|
56 |
red = new int[2]; |
57 |
green = new int[2]; |
58 |
blue = new int[2]; |
59 |
and = TransparencyRange.stringToInterval(value, red, green, blue); |
60 |
strEntry = value; |
61 |
} catch (IOException e) { |
62 |
//Cadena mal formada
|
63 |
} |
64 |
} |
65 |
|
66 |
/**
|
67 |
* Obtiene la operaci?n utilizada
|
68 |
* @param and Si es true significa que se usa un AND y false implica
|
69 |
* que se usa un OR
|
70 |
*/
|
71 |
public boolean isAnd() { |
72 |
return and;
|
73 |
} |
74 |
|
75 |
/**
|
76 |
* Asigna la operaci?n AND como la utilizada
|
77 |
* @param and booleano que si est? a true significa que el la operaci?n
|
78 |
* AND es la utilizada como operaci?n.
|
79 |
*/
|
80 |
public void setAnd(boolean and) { |
81 |
this.and = and;
|
82 |
} |
83 |
|
84 |
/**
|
85 |
* Obtiene el intervalo de valores correspondiente a la banda del azul
|
86 |
* @return Array bidimensional de enteros correspondiente a la banda del azul
|
87 |
*/
|
88 |
public int[] getBlue() { |
89 |
return blue;
|
90 |
} |
91 |
|
92 |
/**
|
93 |
* Asigna los intervalos de valores correspondientes a las bandas del
|
94 |
* rojo, azul y verde
|
95 |
* @param red Array bidimensional de enteros correspondiente a la banda del rojo
|
96 |
* @param green Array bidimensional de enteros correspondiente a la banda del verde
|
97 |
* @param blue Array bidimensional de enteros correspondiente a la banda del azul
|
98 |
*/
|
99 |
public void setRGB(int[] red, int[] green, int[] blue) { |
100 |
this.red = red;
|
101 |
this.green = green;
|
102 |
this.blue = blue;
|
103 |
} |
104 |
|
105 |
/**
|
106 |
* Asigna el intervalo de valores correspondiente a la banda del azul
|
107 |
* @param blue Array bidimensional de enteros correspondiente a la banda del azul
|
108 |
*/
|
109 |
public void setBlue(int[] blue) { |
110 |
this.blue = blue;
|
111 |
} |
112 |
|
113 |
/**
|
114 |
* Obtiene el intervalo de valores correspondiente a la banda del verde
|
115 |
* @return Array bidimensional de enteros correspondiente a la banda del verde
|
116 |
*/
|
117 |
public int[] getGreen() { |
118 |
return green;
|
119 |
} |
120 |
|
121 |
/**
|
122 |
* Asigna el intervalo de valores correspondiente a la banda del verde
|
123 |
* @param green Array bidimensional de enteros correspondiente a la banda del verde
|
124 |
*/
|
125 |
public void setGreen(int[] green) { |
126 |
this.green = green;
|
127 |
} |
128 |
|
129 |
/**
|
130 |
* Obtiene el intervalo de valores correspondiente a la banda del rojo
|
131 |
* @return Array bidimensional de enteros correspondiente a la banda del rojo
|
132 |
*/
|
133 |
public int[] getRed() { |
134 |
return red;
|
135 |
} |
136 |
|
137 |
/**
|
138 |
* Asigna el intervalo de valores correspondiente a la banda del rojo
|
139 |
* @param red Array bidimensional de enteros correspondiente a la banda del rojo
|
140 |
*/
|
141 |
public void setRed(int[] red) { |
142 |
this.red = red;
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* Obtiene la cadena que representa una entrada en la tabla.
|
147 |
* @return Cadena que representa una entrada en la tabla
|
148 |
*/
|
149 |
public String getStrEntry() { |
150 |
return strEntry;
|
151 |
} |
152 |
|
153 |
/**
|
154 |
* Asigna la cadena que representa una entrada en la tabla.
|
155 |
* @param strEntry Cadena que representa una entrada en la tabla.
|
156 |
*/
|
157 |
public void setStrEntry(String strEntry) { |
158 |
this.strEntry = strEntry;
|
159 |
} |
160 |
|
161 |
|
162 |
/**
|
163 |
* Esta funci?n valida la cadena de entrada por medio de una m?quina de estados. Valida las
|
164 |
* siguientes posibilidades en la cadena de entrada:
|
165 |
* <LI>
|
166 |
* <UL>(valor_rojo) & (Valor_verde) & (Valor_azul)</UL>
|
167 |
* <UL>(valor_rojo) | (Valor_verde) | (Valor_azul)</UL>
|
168 |
* </LI>
|
169 |
* Despues de la validaci?n parsea el contenido y carga los par?metros r,g,b con los
|
170 |
* intervalos de valores. Estos par?metros deben ser pasados como arrays de enteros de
|
171 |
* dos elementos.
|
172 |
* @param values
|
173 |
* @param r Intervalo de rojo
|
174 |
* @param g Intervalo de verde
|
175 |
* @param b Intervalo de azul
|
176 |
* @return Devuelve true si la operaci?n usada en los intervalos es un AND y false si es un OR
|
177 |
*/
|
178 |
public static boolean stringToInterval(String values, int[] r, int[] g, int[] b) throws IOException { |
179 |
int status = 0; |
180 |
int countAnd = 0, countOr = 0; |
181 |
boolean and = true; |
182 |
for(int i = 0; i < values.length(); i ++){ |
183 |
char c = values.charAt(i);
|
184 |
switch(status){
|
185 |
case 0: if(c == ' ')status = 0; |
186 |
else if ((c >= 48 && c <= 57) || c == '*')status = 1; |
187 |
else status = 4; |
188 |
break;
|
189 |
case 1: if ((c >= 48 && c <= 57) || (c == ' '))status = 1; |
190 |
else if(c == ':') status = 2; |
191 |
else if(c == '&'){ |
192 |
status = 0;
|
193 |
countAnd++; |
194 |
}else if(c == '|'){ |
195 |
status = 0;
|
196 |
countOr++; |
197 |
}else status = 4; |
198 |
break;
|
199 |
case 2: if (c >= 48 && c <= 57) status = 3; |
200 |
else status = 4; |
201 |
break;
|
202 |
case 3: if ((c >= 48 && c <= 57) || (c == ' '))status = 3; |
203 |
else if(c == '&'){ |
204 |
status = 0;
|
205 |
countAnd++; |
206 |
}else if(c == '|'){ |
207 |
status = 0;
|
208 |
countOr++; |
209 |
}else status = 4; |
210 |
break;
|
211 |
case 4: throw new IOException("Error en la cadena de entrada "+status); |
212 |
} |
213 |
} |
214 |
|
215 |
//Si el analisis es correcto parseamos
|
216 |
if((status == 1 || status ==3) && ((countAnd == 2 && countOr == 0) || |
217 |
(countAnd == 0 && countOr == 2)) ){ |
218 |
String[] s = values.split(" & "); |
219 |
if(s.length < 3){ |
220 |
s = values.split(" \\| ");
|
221 |
and = false;
|
222 |
} |
223 |
; |
224 |
String[] val = s[0].split(":"); |
225 |
try{
|
226 |
r[0] = Integer.parseInt(val[0]); |
227 |
if(val.length == 2) |
228 |
r[1] = Integer.parseInt(val[1]); |
229 |
else
|
230 |
r[1] = Integer.parseInt(val[0]); |
231 |
}catch(NumberFormatException e){ |
232 |
r [0] = -1;r [1] = -1; |
233 |
} |
234 |
|
235 |
val = s[1].split(":"); |
236 |
try{
|
237 |
g[0] = Integer.parseInt(val[0]); |
238 |
if(val.length == 2) |
239 |
g[1] = Integer.parseInt(val[1]); |
240 |
else
|
241 |
g[1] = Integer.parseInt(val[0]); |
242 |
}catch(NumberFormatException e){ |
243 |
g[0] = -1;g[1] = -1; |
244 |
} |
245 |
|
246 |
val = s[2].split(":"); |
247 |
try{
|
248 |
b[0] = Integer.parseInt(val[0]); |
249 |
if(val.length == 2) |
250 |
b[1] = Integer.parseInt(val[1]); |
251 |
else
|
252 |
b[1] = Integer.parseInt(val[0]); |
253 |
}catch(NumberFormatException e){ |
254 |
b [0] = -1;b [1] = -1; |
255 |
} |
256 |
}else
|
257 |
throw new IOException("Error en la cadena de entrada "); |
258 |
|
259 |
return and;
|
260 |
} |
261 |
|
262 |
/**
|
263 |
* Carga la cadena StrEntry leyendo los valores en los vectores que representa los intervalos.
|
264 |
*/
|
265 |
public void loadStrEntryFromValues(){ |
266 |
String separator = " | "; |
267 |
if(and)
|
268 |
separator = " & ";
|
269 |
strEntry = String.valueOf(red[0] + separator+ green[0] + separator + blue[0]); |
270 |
} |
271 |
|
272 |
/**
|
273 |
* Obtiene el alpha asociado al rango. Por defecto es de 0, es decir
|
274 |
* totalmente transparente pero puede asociarsele un valor distinto
|
275 |
* @return Alpha asociado
|
276 |
*/
|
277 |
public int getAlpha() { |
278 |
return alpha;
|
279 |
} |
280 |
|
281 |
/**
|
282 |
* Asigna el alpha asociado al rango. Por defecto es de 0, es decir
|
283 |
* totalmente transparente pero puede asociarsele un valor distinto
|
284 |
* @param alpha asociado
|
285 |
*/
|
286 |
public void setAlpha(int alpha) { |
287 |
this.alpha = alpha;
|
288 |
} |
289 |
|
290 |
/**
|
291 |
* Realiza la uni?n entre el intervalo actual y el pasado por par?metro
|
292 |
* @param interval intervalo pasado
|
293 |
* @param rgb
|
294 |
* @return union de intervalos
|
295 |
*/
|
296 |
public int[] union(int[] interval, int rgb) { |
297 |
switch(rgb) {
|
298 |
case RasterDataset.RED_BAND: if((red[1] >= interval[0] && red[0] <= interval[0]) || (red[0] <= interval[1] && red[0] >= interval[0])) |
299 |
return new int[]{Math.min(red[0], interval[0]), Math.max(red[1], interval[1])}; |
300 |
case RasterDataset.GREEN_BAND: if((green[1] >= interval[0] && green[0] <= interval[0]) || (green[0] <= interval[1] && green[0] >= interval[0])) |
301 |
return new int[]{Math.min(green[0], interval[0]), Math.max(green[1], interval[1])}; |
302 |
case RasterDataset.BLUE_BAND: if((blue[1] >= interval[0] && blue[0] <= interval[0]) || (blue[0] <= interval[1] && blue[0] >= interval[0])) |
303 |
return new int[]{Math.min(blue[0], interval[0]), Math.max(blue[1], interval[1])}; |
304 |
} |
305 |
return null; |
306 |
} |
307 |
|
308 |
/**
|
309 |
* Muestra los datos del objeto para depuraci?n.
|
310 |
*/
|
311 |
public void show(){ |
312 |
if(getRed() != null) |
313 |
System.out.println(getRed()[0]+" "+getRed()[1]); |
314 |
if(getGreen() != null) |
315 |
System.out.println(getGreen()[0]+" "+getGreen()[1]); |
316 |
if(getBlue() != null) |
317 |
System.out.println(getBlue()[0]+" "+getBlue()[1]); |
318 |
System.out.println(isAnd());
|
319 |
System.out.println(getStrEntry());
|
320 |
System.out.println("--------------------"); |
321 |
} |
322 |
} |