svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / properties / DatasetTransparency.java @ 12066
History | View | Annotate | Download (10.1 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.dataset.properties; |
20 |
|
21 |
import java.io.IOException; |
22 |
import java.util.ArrayList; |
23 |
|
24 |
import org.gvsig.raster.util.TransparencyRange; |
25 |
import org.gvsig.raster.util.TransparencyRangeException; |
26 |
|
27 |
//TODO: TEST: Realizar un test para la comprobaci?n de transparencias y para mergeTransparencyBands
|
28 |
/**
|
29 |
* <P>
|
30 |
* Representa el estado de transparencia de un raster. Este estado puede tenerlo
|
31 |
* un solo fichero RasterDataset o varios agrupados MultiRasterDataset. Por esto
|
32 |
* el estado de transparencia de un MultiRasterDataset ser? la suma de los estados
|
33 |
* de los RasterDataset que lo componen.
|
34 |
* </P>
|
35 |
* <P>
|
36 |
* El estado de transparencia de una fuente de datos est?
|
37 |
* asignada como propiedad a un RasterDataset porque es una propiedad que ofrece el
|
38 |
* propio fichero de datos.
|
39 |
* </P>
|
40 |
* <P>
|
41 |
* El estado de transparencia de la visualizaci?n puede tambi?n verse modificado
|
42 |
* por el propio usuario. Este ya no formar? parte de la transparencia de un
|
43 |
* GeoRasterFile sino que en una capa superior (grid) el objeto grid tendr? asociado
|
44 |
* un objeto TransparencyStatus que ser? la suma del Objeto TransparencyStatus de
|
45 |
* las fuentes de datos que lo componen m?s las modificaciones de transparencias
|
46 |
* hechas por un usuario.
|
47 |
* </P>
|
48 |
* <P>
|
49 |
* El estado de transparencia de un raster puede verse modificado desde distintos
|
50 |
* sitios:
|
51 |
* <UL>
|
52 |
* <LI>Color con transparencia definido en los metadatos de la imagen. Caracteristica de PNG.</LI>
|
53 |
* <LI>Una banda del raster definida como banda de transparencia. PNGs, IMGs o TIFFs
|
54 |
* pueden tener bandas de transparencia</LI>
|
55 |
* <LI>Tablas de color con valores de transparencia. GIF y PNG pueden tener tablas
|
56 |
* que definan entradas de color con transparencia</LI>
|
57 |
* <LI>Informaci?n de transparencia en la cabecera raster. Tipicamente valores NO_DATA suelen
|
58 |
* ser interpretados como transparentes en la visualizaci?n.</LI>
|
59 |
* <LI>Modificaci?n hecha por el usuario. En la visualizaci?n un usuario puede necesitar
|
60 |
* asignar valores o rangos de valores como transparentes.</LI>
|
61 |
* </UL>
|
62 |
* </P>
|
63 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
64 |
*
|
65 |
*/
|
66 |
public class DatasetTransparency{ |
67 |
/**
|
68 |
* N?mero de banda de transparencia.
|
69 |
*/
|
70 |
protected int transparencyBandNumber = -1; |
71 |
/**
|
72 |
* Rangos de transparencia aplicados. Lista de TransparencyRange
|
73 |
*/
|
74 |
protected ArrayList transparencyRanges = new ArrayList(); |
75 |
/**
|
76 |
* Color en la banda del rojo para la transparencia
|
77 |
*/
|
78 |
protected int transparencyColorRed = 0xff; |
79 |
/**
|
80 |
* Color en la banda del verde para la transparencia
|
81 |
*/
|
82 |
protected int transparencyColorGreen = 0xff; |
83 |
/**
|
84 |
* Color en la banda del azul para la transparencia
|
85 |
*/
|
86 |
protected int transparencyColorBlue = 0xff; |
87 |
/**
|
88 |
* Valor de transparencia
|
89 |
*/
|
90 |
protected int alpha = 0x10; |
91 |
/**
|
92 |
* Grado de opacidad de todo el raster
|
93 |
*/
|
94 |
protected int opacity = 0xff; |
95 |
|
96 |
/**
|
97 |
* Constructor
|
98 |
*
|
99 |
*/
|
100 |
public DatasetTransparency(){
|
101 |
|
102 |
} |
103 |
|
104 |
/**
|
105 |
* Constructor de copia
|
106 |
*/
|
107 |
public DatasetTransparency(DatasetTransparency fileStatus){
|
108 |
this.transparencyBandNumber = fileStatus.getTransparencyBandNumber();
|
109 |
this.transparencyColorRed = fileStatus.getTransparencyColor()[0]; |
110 |
this.transparencyColorGreen = fileStatus.getTransparencyColor()[1]; |
111 |
this.transparencyColorBlue = fileStatus.getTransparencyColor()[2]; |
112 |
this.transparencyRanges = (ArrayList)fileStatus.getTransparencyRange().clone(); |
113 |
this.alpha = fileStatus.getAlpha();
|
114 |
this.opacity = fileStatus.getOpacity();
|
115 |
} |
116 |
|
117 |
/**
|
118 |
* @param range rango
|
119 |
* @param banda banda
|
120 |
* @param px pixel
|
121 |
*/
|
122 |
protected boolean processRange(int[] px) { |
123 |
for (int i = 0; i < transparencyRanges.size(); i++) { |
124 |
TransparencyRange tr = ((TransparencyRange)transparencyRanges.get(i)); |
125 |
alpha = tr.getAlpha(); |
126 |
if(tr.isAnd()){
|
127 |
if(tr.getRed() != null) |
128 |
if (px[1] < tr.getRed()[0] || px[1] > tr.getRed()[1]) |
129 |
continue;
|
130 |
if(tr.getGreen() != null) |
131 |
if (px[2] < tr.getGreen()[0] || px[2] > tr.getGreen()[1]) |
132 |
continue;
|
133 |
if(tr.getBlue() != null) |
134 |
if (px[3] < tr.getBlue()[0] || px[3] > tr.getBlue()[1]) |
135 |
continue;
|
136 |
if(tr.getRed() != null || tr.getGreen() != null || tr.getBlue() != null){ |
137 |
setPixelTransparent(px); |
138 |
return true; |
139 |
} |
140 |
|
141 |
}else{
|
142 |
if(tr.getRed() != null){ |
143 |
if (px[1] >= tr.getRed()[0] && px[1] <= tr.getRed()[1]){ |
144 |
setPixelTransparent(px); |
145 |
return true; |
146 |
} |
147 |
} |
148 |
if(tr.getGreen() != null){ |
149 |
if (px[2] >= tr.getGreen()[0] && px[2] <= tr.getGreen()[1]){ |
150 |
setPixelTransparent(px); |
151 |
return true; |
152 |
} |
153 |
} |
154 |
if(tr.getBlue() != null){ |
155 |
if (px[3] >= tr.getBlue()[0] && px[3] <= tr.getBlue()[1]){ |
156 |
setPixelTransparent(px); |
157 |
return true; |
158 |
} |
159 |
} |
160 |
} |
161 |
} |
162 |
return false; |
163 |
} |
164 |
|
165 |
/**
|
166 |
* Asigna al pixel pasado como par?metro como transparente con
|
167 |
* el color de transparencia que est? seleccionado
|
168 |
* @param px Pixel a asignarle transparencia
|
169 |
*/
|
170 |
private void setPixelTransparent(int[] px){ |
171 |
px[0] = this.alpha; |
172 |
px[1] = this.transparencyColorRed; |
173 |
px[2] = this.transparencyColorGreen; |
174 |
px[3] = this.transparencyColorBlue; |
175 |
} |
176 |
|
177 |
/**
|
178 |
* Obtiene los rangos de pixels que son transparentes en el raster.
|
179 |
* @return Rangos de transparencias a aplicar
|
180 |
*/
|
181 |
public ArrayList getTransparencyRange() { |
182 |
return transparencyRanges;
|
183 |
} |
184 |
|
185 |
/**
|
186 |
* Asigna un rango de pixels que son transparentes en el raster.
|
187 |
* @param range
|
188 |
*/
|
189 |
public void setTransparencyRange(TransparencyRange range) { |
190 |
this.transparencyRanges.add(range);
|
191 |
} |
192 |
|
193 |
/**
|
194 |
* Asigna la lista de rangos de transparencia
|
195 |
* @param ranges
|
196 |
*/
|
197 |
public void setTransparencyRangeList(ArrayList ranges) { |
198 |
this.transparencyRanges = ranges;
|
199 |
} |
200 |
|
201 |
/**
|
202 |
* Inicializa la lista de rangos de transparencia.
|
203 |
*/
|
204 |
public void clearListOfTransparencyRange(){ |
205 |
transparencyRanges.clear(); |
206 |
} |
207 |
|
208 |
/**
|
209 |
* Asigna un rango de pixels que son transparentes en el raster.
|
210 |
* @param range
|
211 |
*/
|
212 |
public void setTransparencyRange(String range)throws TransparencyRangeException{ |
213 |
TransparencyRange tr = new TransparencyRange();
|
214 |
int[] red = new int[2]; |
215 |
int[] green = new int[2]; |
216 |
int[] blue = new int[2]; |
217 |
boolean and = true; |
218 |
try{
|
219 |
and = TransparencyRange.stringToInterval(range, red, green, blue); |
220 |
if(red[0] == -1) |
221 |
red = null;
|
222 |
if(green[0] == -1) |
223 |
green = null;
|
224 |
if(blue[0] == -1) |
225 |
blue = null;
|
226 |
}catch(IOException e){ |
227 |
throw new TransparencyRangeException("Cadena de rangos malformada."); |
228 |
} |
229 |
tr.setAnd(and); |
230 |
tr.setRed(red); |
231 |
tr.setGreen(green); |
232 |
tr.setBlue(blue); |
233 |
transparencyRanges.add(tr); |
234 |
} |
235 |
|
236 |
/**
|
237 |
* Obtiene el flag de existencia de banda de transparencia.
|
238 |
* @return true existe una banda con informaci?n de transparencia y false no existe.
|
239 |
*/
|
240 |
public boolean isTransparencyBand() { |
241 |
return (transparencyBandNumber < 0); |
242 |
} |
243 |
|
244 |
/**
|
245 |
* Asigna el n?mero de banda de transparencia.
|
246 |
* @param transparencyBand
|
247 |
*/
|
248 |
public void setTransparencyBand(int transparencyBand) { |
249 |
transparencyBandNumber = transparencyBand; |
250 |
} |
251 |
|
252 |
/**
|
253 |
* Obtiene el n?mero de la banda de transparencia
|
254 |
* @return
|
255 |
*/
|
256 |
public int getTransparencyBandNumber(){ |
257 |
return transparencyBandNumber;
|
258 |
} |
259 |
|
260 |
/**
|
261 |
* Obtiene el color que se aplica a la transparencia
|
262 |
* @return array de tres elementos con R, G, B del color (valor por defecto 255, 255, 255)
|
263 |
*/
|
264 |
public int[] getTransparencyColor(){ |
265 |
return new int[]{transparencyColorRed, transparencyColorGreen, transparencyColorBlue}; |
266 |
} |
267 |
|
268 |
/**
|
269 |
* Asigna el color que se aplica a la transparencia
|
270 |
* @param rgb array de tres elementos con R, G, B del color (valor por defecto 255, 255, 255)
|
271 |
*/
|
272 |
public void setTransparencyColor(int[] rgb){ |
273 |
if(rgb == null) |
274 |
return;
|
275 |
transparencyColorRed = rgb[0];
|
276 |
transparencyColorGreen = rgb[1];
|
277 |
transparencyColorBlue = rgb[2];
|
278 |
} |
279 |
|
280 |
/**
|
281 |
* Obtiene el grado de opacidad de todo el raster
|
282 |
* @return valor del grado de opacidad.
|
283 |
*/
|
284 |
public int getOpacity() { |
285 |
return opacity;
|
286 |
} |
287 |
|
288 |
/**
|
289 |
* Asigna el grado de opacidad de todo el raster
|
290 |
* @param opacity valor del grado de opacidad.
|
291 |
*/
|
292 |
public void setOpacity(int opacity) { |
293 |
this.opacity = opacity;
|
294 |
} |
295 |
|
296 |
/**
|
297 |
* Obtiene alpha
|
298 |
* @return valor alpha
|
299 |
*/
|
300 |
public int getAlpha() { |
301 |
return alpha;
|
302 |
} |
303 |
|
304 |
/**
|
305 |
* Asigna alpha
|
306 |
* @param alpha
|
307 |
*/
|
308 |
public void setAlpha(int alpha) { |
309 |
this.alpha = alpha;
|
310 |
} |
311 |
|
312 |
/**
|
313 |
* Asigna la transparencia a partir de un objeto con los metadatos del raster.
|
314 |
* @param metadata
|
315 |
*/
|
316 |
public void setTransparencyByPixelFromMetadata(DatasetMetadata metadata){ |
317 |
if(metadata != null){ |
318 |
TransparencyRange[] noData = metadata.parserNodataInMetadata();
|
319 |
if(noData != null){ |
320 |
for(int i = 0; i < noData.length; i++) |
321 |
getTransparencyRange().add(noData[i]); |
322 |
} |
323 |
TransparencyRange noDataValue = metadata.parserNodataByBand(); |
324 |
if(noData == null && noDataValue != null) |
325 |
getTransparencyRange().add(noDataValue); |
326 |
} |
327 |
|
328 |
} |
329 |
|
330 |
} |