gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / GridTransparencyImpl.java @ 723
History | View | Annotate | Download (6.6 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.impl.grid; |
23 |
|
24 |
import org.gvsig.fmap.dal.coverage.datastruct.TransparencyRange; |
25 |
import org.gvsig.fmap.dal.coverage.grid.GridTransparency; |
26 |
import org.gvsig.fmap.dal.coverage.store.props.Transparency; |
27 |
import org.gvsig.raster.impl.store.properties.DataStoreTransparency; |
28 |
import org.gvsig.tools.ToolsLocator; |
29 |
import org.gvsig.tools.dynobject.DynClass; |
30 |
import org.gvsig.tools.dynobject.DynStruct; |
31 |
import org.gvsig.tools.persistence.PersistenceManager; |
32 |
import org.gvsig.tools.persistence.PersistentState; |
33 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
34 |
|
35 |
|
36 |
//TODO: FUNCIONALIDAD: Convertir la transparencia a String y viceversa para salvar proyecto
|
37 |
/**
|
38 |
* <P>
|
39 |
* Representa el estado de transparencia de un grid.
|
40 |
* </P>
|
41 |
* <P>
|
42 |
* El estado de transparencia de un raster puede verse modificado desde los siguientes sitios:
|
43 |
* <UL>
|
44 |
* <LI>Color con transparencia definido en los metadatos de la imagen. Caracteristica de PNG.</LI>
|
45 |
* <LI>Una banda del raster definida como banda de transparencia. PNGs, IMGs o TIFFs
|
46 |
* pueden tener bandas de transparencia</LI>
|
47 |
* <LI>Tablas de color con valores de transparencia. GIF y PNG pueden tener tablas
|
48 |
* que definan entradas de color con transparencia</LI>
|
49 |
* <LI>Informaci?n de transparencia en la cabecera raster. Tipicamente valores NO_DATA suelen
|
50 |
* ser interpretados como transparentes en la visualizaci?n.</LI>
|
51 |
* <LI>Modificaci?n hecha por el usuario. En la visualizaci?n un usuario puede necesitar
|
52 |
* asignar valores o rangos de valores como transparentes.</LI>
|
53 |
* </UL>
|
54 |
* </P>
|
55 |
* <P>
|
56 |
* Estas transparencias quedan representadas por cinco tipo b?sicos:
|
57 |
* <UL>
|
58 |
* <LI><b>Transparencia por rangos. </b>Esta se aplicar? antes que ninguna y siempre sobre los valores
|
59 |
* base del raster.</LI>
|
60 |
* <LI><b>Banda de transparencia en un raster. </b></LI>
|
61 |
* <LI>M?scara de transparencia</LI>
|
62 |
* <LI>Opacidad</LI>
|
63 |
* <LI>Zona de recorte</LI>
|
64 |
* </UL>
|
65 |
* </P>
|
66 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
67 |
*
|
68 |
*/
|
69 |
public class GridTransparencyImpl extends DataStoreTransparency implements GridTransparency { |
70 |
/**
|
71 |
* Constructor. Crea un objeto con las propiedades de la transparencia de un grid.
|
72 |
* Las variables conservan sus valores por defecto
|
73 |
*/
|
74 |
public GridTransparencyImpl(){
|
75 |
super();
|
76 |
activeTransparency(); |
77 |
}; |
78 |
|
79 |
/**
|
80 |
* Constructor. Crea un objeto con las propiedades de la transparencia de fichero
|
81 |
* pasado como par?metro.
|
82 |
* @param fileStatus Transparencia.
|
83 |
*/
|
84 |
public GridTransparencyImpl(Transparency transp) { |
85 |
super(transp);
|
86 |
activeTransparency(); |
87 |
} |
88 |
|
89 |
/*
|
90 |
* (non-Javadoc)
|
91 |
* @see org.gvsig.fmap.dal.coverage.grid.GridTransparency#processRGB(int, int, int, int, int)
|
92 |
*/
|
93 |
public int processRGB(int r, int g, int b, int line, int col) { |
94 |
// Si el valor es noData se pone totalmente transparente y ya no se tiene en
|
95 |
// cuenta nada m?s.
|
96 |
if (originalData != null && isNoDataActive()) { |
97 |
if (isNoData(line, col))
|
98 |
// El alpha no se pone para dejarlo a 0, totalmente transparente
|
99 |
return (r << 16 | g << 8 | b); |
100 |
} |
101 |
|
102 |
/**
|
103 |
* Para sacar el valor del nuevo alpha, lo que hacemos es convertir a
|
104 |
* porcentajes todos los alphas posibles (en porcentajes) y multiplicarlos
|
105 |
* entre si. Con esto conseguimos un nuevo porcentaje de alpha entre 0 y 1.
|
106 |
* Una vez hecho esto se multiplica por 255 para tenerlo dentro del rango
|
107 |
* deseado. Como queremos evitar operaciones de m?s, podemos quitar una
|
108 |
* division de 255 y as? nos ahorramos luego multiplicarlo por 255 otra vez.
|
109 |
*/
|
110 |
|
111 |
// Quitada la division para optimizar
|
112 |
//double a = opacity / 255D;
|
113 |
double a = opacity;
|
114 |
|
115 |
int alphaRange = processRange(r, g, b);
|
116 |
if (alphaRange != 255) |
117 |
a *= (alphaRange / 255D);
|
118 |
|
119 |
if (existAlphaBand() && getAlphaBand() != null) |
120 |
a *= (getAlphaBand().getElemByte(line, col, 0) & 0xff) / 255D; |
121 |
|
122 |
// Quitada la multiplicacion para optimizar
|
123 |
// a = (int)(a * 255D);
|
124 |
|
125 |
return ((int) a << 24) | r << 16 | g << 8 | b; |
126 |
} |
127 |
|
128 |
/**
|
129 |
* Aplica la transparecia por rangos al pixel pasado por par?metro. El valor
|
130 |
* en la posici?n cero es el alpha por lo tanto ser? esta posici?n la que se
|
131 |
* modificar?.
|
132 |
* @param r
|
133 |
* @param g
|
134 |
* @param b
|
135 |
* @return Un valor de 0 a 255. Donde 255 es totalmente opaco
|
136 |
*/
|
137 |
private int processRange(int r, int g, int b) { |
138 |
for (int i = 0; i < transparencyRanges.size(); i++) { |
139 |
TransparencyRange tr = ((TransparencyRange) transparencyRanges.get(i)); |
140 |
if (tr == null) continue; |
141 |
if (tr.isAnd()) {
|
142 |
if (tr.getRed() == null || |
143 |
tr.getGreen() == null ||
|
144 |
tr.getBlue() == null ||
|
145 |
r < tr.getRed()[0] || r > tr.getRed()[1] || |
146 |
g < tr.getGreen()[0] || g > tr.getGreen()[1] || |
147 |
b < tr.getBlue()[0] || b > tr.getBlue()[1]) |
148 |
continue;
|
149 |
|
150 |
return tr.getAlpha();
|
151 |
} else {
|
152 |
if (tr.getRed() != null) { |
153 |
if (r >= tr.getRed()[0] && r <= tr.getRed()[1]) { |
154 |
return tr.getAlpha();
|
155 |
} |
156 |
} |
157 |
if (tr.getGreen() != null) { |
158 |
if (g >= tr.getGreen()[0] && g <= tr.getGreen()[1]) { |
159 |
return tr.getAlpha();
|
160 |
} |
161 |
} |
162 |
if (tr.getBlue() != null) { |
163 |
if (b >= tr.getBlue()[0] && b <= tr.getBlue()[1]) { |
164 |
return tr.getAlpha();
|
165 |
} |
166 |
} |
167 |
} |
168 |
} |
169 |
return 255; |
170 |
} |
171 |
|
172 |
public static void registerPersistent() { |
173 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
174 |
DynStruct definition = manager.addDefinition( |
175 |
GridTransparencyImpl.class, |
176 |
"GridTransparency_Persistent",
|
177 |
"GridTransparency Persistent definition",
|
178 |
null,
|
179 |
null
|
180 |
); |
181 |
definition.extend( |
182 |
(DynClass)ToolsLocator.getPersistenceManager() |
183 |
.getDefinition(Transparency.class)
|
184 |
); |
185 |
} |
186 |
|
187 |
@Override
|
188 |
public void loadFromState(PersistentState state) |
189 |
throws PersistenceException {
|
190 |
super.loadFromState(state);
|
191 |
} |
192 |
|
193 |
@Override
|
194 |
public void saveToState(PersistentState state) throws PersistenceException { |
195 |
super.saveToState(state);
|
196 |
} |
197 |
|
198 |
} |