gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / store / properties / DataStoreColorInterpretation.java @ 2370
History | View | Annotate | Download (12.4 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.store.properties; |
23 |
|
24 |
import java.util.ArrayList; |
25 |
import java.util.Arrays; |
26 |
import java.util.List; |
27 |
|
28 |
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation; |
29 |
import org.gvsig.tools.ToolsLocator; |
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 |
* Clase que contiene la interpretaci?n de color por banda. Inicialmente
|
36 |
* es inicializada con los valores contenidos en el raster si los tiene. Despu?s
|
37 |
* estos valores pueden ser modificados.
|
38 |
*
|
39 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
40 |
*/
|
41 |
public class DataStoreColorInterpretation implements ColorInterpretation { |
42 |
/**
|
43 |
* Interpretaci?n de color para cada banda
|
44 |
*/
|
45 |
private String[] colorInterpretation = null; |
46 |
/**
|
47 |
* true si la imagen tiene una banda con el identificador de interpretaci?n de
|
48 |
* color a Alpha
|
49 |
*/
|
50 |
|
51 |
/**
|
52 |
* Constructor vacio.
|
53 |
*/
|
54 |
public DataStoreColorInterpretation() {
|
55 |
this.colorInterpretation = new String[0]; |
56 |
} |
57 |
|
58 |
/**
|
59 |
* Constructor que asigna los valores de interpretaci?n de color
|
60 |
*/
|
61 |
public DataStoreColorInterpretation(String[] colorInterp) { |
62 |
this.colorInterpretation = colorInterp;
|
63 |
} |
64 |
|
65 |
public static DataStoreColorInterpretation createDefaultInterpretation(int nBands) { |
66 |
if(nBands <= 0) |
67 |
return null; |
68 |
if(nBands == 1) |
69 |
return new DataStoreColorInterpretation(new String[]{GRAY_BAND}); |
70 |
if(nBands == 2) |
71 |
return new DataStoreColorInterpretation(new String[]{GRAY_BAND, ALPHA_BAND}); |
72 |
if(nBands == 3) |
73 |
return new DataStoreColorInterpretation(new String[]{RED_BAND, GREEN_BAND, BLUE_BAND}); |
74 |
return new DataStoreColorInterpretation(new String[]{RED_BAND, GREEN_BAND, BLUE_BAND, ALPHA_BAND}); |
75 |
} |
76 |
|
77 |
public static DataStoreColorInterpretation createPaletteInterpretation() { |
78 |
return new DataStoreColorInterpretation(PALETTE); |
79 |
} |
80 |
|
81 |
/**
|
82 |
* Obtiene una interpretaci?n de color GRAY
|
83 |
* @return DatasetColorInterpretation
|
84 |
*/
|
85 |
public static DataStoreColorInterpretation createGrayInterpretation() { |
86 |
return new DataStoreColorInterpretation(GRAYSCALE); |
87 |
} |
88 |
|
89 |
/**
|
90 |
* Obtiene una interpretaci?n de color ARGB
|
91 |
* @return DatasetColorInterpretation
|
92 |
*/
|
93 |
public static DataStoreColorInterpretation createRGBAInterpretation() { |
94 |
return new DataStoreColorInterpretation(ARGB); |
95 |
} |
96 |
|
97 |
/**
|
98 |
* Obtiene una interpretaci?n de color RGB
|
99 |
* @return DatasetColorInterpretation
|
100 |
*/
|
101 |
public static DataStoreColorInterpretation createRGBInterpretation() { |
102 |
return new DataStoreColorInterpretation(RGB); |
103 |
} |
104 |
|
105 |
/**
|
106 |
* Constructor que inicializa el n?mero de valores de la interpretaci?n de
|
107 |
* color. Implica asignar posteriormente los valores a las bandas.
|
108 |
*/
|
109 |
public DataStoreColorInterpretation(int bandCount) { |
110 |
colorInterpretation = new String[bandCount]; |
111 |
} |
112 |
|
113 |
public DataStoreColorInterpretation(String colorInterpretationConstant) { |
114 |
if(colorInterpretationConstant.equals(RGB)) {
|
115 |
colorInterpretation = new String[]{RED_BAND, GREEN_BAND, BLUE_BAND}; |
116 |
} |
117 |
if(colorInterpretationConstant.equals(BGR)) {
|
118 |
colorInterpretation = new String[]{BLUE_BAND, GREEN_BAND, RED_BAND}; |
119 |
} |
120 |
if(colorInterpretationConstant.equals(ARGB)) {
|
121 |
colorInterpretation = new String[]{RED_BAND, GREEN_BAND, BLUE_BAND, ALPHA_BAND}; |
122 |
} |
123 |
if(colorInterpretationConstant.equals(GRAYSCALE)) {
|
124 |
colorInterpretation = new String[]{GRAY_BAND}; |
125 |
} |
126 |
if(colorInterpretationConstant.equals(PALETTE)) {
|
127 |
colorInterpretation = new String[]{PAL_BAND}; |
128 |
} |
129 |
} |
130 |
|
131 |
/**
|
132 |
* Inicializa el vector de cadenas que contendr?n el nombre de la interpretaci?n
|
133 |
* de color asignada a cada banda. Este valor es el devuelto por la imagen.
|
134 |
* @param values N?mero de valores
|
135 |
*/
|
136 |
public void initColorInterpretation(int values) { |
137 |
colorInterpretation = new String[values]; |
138 |
} |
139 |
|
140 |
public boolean hasInterpretation() { |
141 |
if(colorInterpretation == null) |
142 |
return false; |
143 |
for (int i = 0; i < colorInterpretation.length; i++) { |
144 |
if( colorInterpretation[i].equals(RED_BAND) ||
|
145 |
colorInterpretation[i].equals(GREEN_BAND) || |
146 |
colorInterpretation[i].equals(BLUE_BAND) || |
147 |
colorInterpretation[i].equals(RED_GREEN_BAND) || |
148 |
colorInterpretation[i].equals(RED_BLUE_BAND) || |
149 |
colorInterpretation[i].equals(GRAY_BAND) || |
150 |
colorInterpretation[i].equals(PAL_BAND)) |
151 |
return true; |
152 |
} |
153 |
return false; |
154 |
} |
155 |
|
156 |
public boolean isBGR() { |
157 |
if(colorInterpretation != null) { |
158 |
if (colorInterpretation.length == 3 && |
159 |
colorInterpretation[0] == BLUE_BAND &&
|
160 |
colorInterpretation[1] == GREEN_BAND &&
|
161 |
colorInterpretation[2] == RED_BAND)
|
162 |
return true; |
163 |
} |
164 |
return false; |
165 |
} |
166 |
|
167 |
public boolean isRGB() { |
168 |
if(colorInterpretation != null) { |
169 |
if (colorInterpretation.length == 3 && |
170 |
isColorInterpretation(0) &&
|
171 |
isColorInterpretation(1) &&
|
172 |
isColorInterpretation(2))
|
173 |
return true; |
174 |
} |
175 |
return false; |
176 |
} |
177 |
|
178 |
public boolean isRGBA() { |
179 |
if(colorInterpretation != null) { |
180 |
if (colorInterpretation.length == 4 && |
181 |
isInterpretationDefinedAsColor(0) &&
|
182 |
isInterpretationDefinedAsColor(1) &&
|
183 |
isInterpretationDefinedAsColor(2) &&
|
184 |
isAlphaInterpretation(3))
|
185 |
return true; |
186 |
} |
187 |
return false; |
188 |
} |
189 |
|
190 |
public boolean isPalette() { |
191 |
if(colorInterpretation != null && |
192 |
colorInterpretation[0].length() >= 1 && |
193 |
colorInterpretation[0].equals(PAL_BAND))
|
194 |
return true; |
195 |
return false; |
196 |
} |
197 |
|
198 |
private boolean isInterpretationDefinedAsColor(int band) { |
199 |
return isColorInterpretation(band) || isGrayInterpretation(band);
|
200 |
} |
201 |
|
202 |
public boolean isGrayInterpretation(int band) { |
203 |
return colorInterpretation[band].equals(GRAY_BAND);
|
204 |
} |
205 |
|
206 |
public boolean isColorInterpretation(int band) { |
207 |
return colorInterpretation[band].equals(RED_BAND) || colorInterpretation[band].equals(GREEN_BAND) || colorInterpretation[band].equals(BLUE_BAND);
|
208 |
} |
209 |
|
210 |
public boolean isAlphaInterpretation(int band) { |
211 |
return colorInterpretation[band].equals(ALPHA_BAND);
|
212 |
} |
213 |
|
214 |
public int getAlphaBand() { |
215 |
String[] values = getValues(); |
216 |
for (int i = 0; i < values.length; i++) { |
217 |
if(values[i] != null && values[i].equals(ALPHA_BAND)) |
218 |
return i;
|
219 |
} |
220 |
return -1; |
221 |
} |
222 |
|
223 |
public String[] getValues() { |
224 |
return colorInterpretation;
|
225 |
} |
226 |
|
227 |
/**
|
228 |
* Asigna los valores de la interpretaci?n de color
|
229 |
* @return String[]
|
230 |
*/
|
231 |
public void setValues(String[] colorInterp) { |
232 |
colorInterpretation = colorInterp; |
233 |
} |
234 |
|
235 |
public void setColorInterpValue(int band, String value) { |
236 |
try{
|
237 |
//Si ya existia el valor se elimina
|
238 |
for (int i = 0; i < colorInterpretation.length; i++) { |
239 |
if(colorInterpretation[i] != null && colorInterpretation[i].equals(value)) |
240 |
colorInterpretation[i] = UNDEF_BAND; |
241 |
} |
242 |
|
243 |
//Se asigna el valor en su entrada. Si excede la longitud se construye uno nuevo m?s largo
|
244 |
//que en las celdas existentes es copia del anterior
|
245 |
if(band >= 0 && band < colorInterpretation.length) |
246 |
colorInterpretation[band] = value; |
247 |
else if(band >= colorInterpretation.length) { |
248 |
ColorInterpretation copyCI = new DataStoreColorInterpretation(band + 1); |
249 |
for (int i = 0; i < colorInterpretation.length; i++) { |
250 |
copyCI.setColorInterpValue(i, colorInterpretation[i]); |
251 |
} |
252 |
colorInterpretation = copyCI.getValues(); |
253 |
} |
254 |
}catch(ArrayIndexOutOfBoundsException ex) { |
255 |
//No asignamos el elemento
|
256 |
} |
257 |
} |
258 |
|
259 |
public int getBand(String id) { |
260 |
if(colorInterpretation != null) { |
261 |
for(int i = 0; i < colorInterpretation.length; i++) |
262 |
if(colorInterpretation[i] != null && colorInterpretation[i].equals(id)) |
263 |
return i;
|
264 |
} |
265 |
return -1; |
266 |
} |
267 |
|
268 |
/**
|
269 |
* Obtiene la posici?n de las bandas que contienen el identificador pasado por par?metro
|
270 |
* o null si no tiene dicho identificador.
|
271 |
* @return Lista con las posiciones de las bandas que contienen el identificador o null si no lo tiene.
|
272 |
*/
|
273 |
public int[] getBands(String id) { |
274 |
if(colorInterpretation != null){ |
275 |
List<Integer> array = new ArrayList<Integer>(); |
276 |
for(int i = 0; i < colorInterpretation.length; i++) |
277 |
if(colorInterpretation[i].equals(id))
|
278 |
array.add(new Integer(i)); |
279 |
int[] list = new int[array.size()]; |
280 |
for (int i = 0; i < list.length; i++) |
281 |
list[i] = ((Integer)array.get(i)).intValue();
|
282 |
return list;
|
283 |
} |
284 |
return null; |
285 |
} |
286 |
|
287 |
public boolean hasAlphaBand() { |
288 |
String[] values = getValues(); |
289 |
for (int i = 0; i < values.length; i++) { |
290 |
if(values[i] != null && values[i].equals("Alpha")) |
291 |
return true; |
292 |
} |
293 |
return false; |
294 |
} |
295 |
|
296 |
public int length() { |
297 |
return colorInterpretation.length;
|
298 |
} |
299 |
|
300 |
public String get(int i) { |
301 |
if (i >= colorInterpretation.length)
|
302 |
return null; |
303 |
return colorInterpretation[i];
|
304 |
} |
305 |
|
306 |
public void addColorInterpretation(ColorInterpretation ci) { |
307 |
String[] newCI = new String[colorInterpretation.length + ci.length()]; |
308 |
for (int i = 0; i < colorInterpretation.length; i++) |
309 |
newCI[i] = colorInterpretation[i]; |
310 |
for (int i = 0; i < ci.length(); i++) { |
311 |
newCI[colorInterpretation.length + i] = ci.get(i); |
312 |
} |
313 |
this.colorInterpretation = newCI;
|
314 |
} |
315 |
|
316 |
public boolean isUndefined() { |
317 |
for (int i = 0; i < colorInterpretation.length; i++) { |
318 |
if (colorInterpretation[i] != null) { |
319 |
if (!colorInterpretation[i].equals(UNDEF_BAND) &&
|
320 |
!colorInterpretation[i].equals(ALPHA_BAND)) |
321 |
return false; |
322 |
} |
323 |
} |
324 |
return true; |
325 |
} |
326 |
|
327 |
public int[] buildRenderBands() { |
328 |
if(colorInterpretation == null) |
329 |
return null; |
330 |
int[] renderBands = new int[]{-1, -1, -1, -1}; |
331 |
for (int i = 0; i < colorInterpretation.length; i++) { |
332 |
if(colorInterpretation[i].equals(RED_BAND))
|
333 |
renderBands[0] = i;
|
334 |
if(colorInterpretation[i].equals(GREEN_BAND))
|
335 |
renderBands[1] = i;
|
336 |
if(colorInterpretation[i].equals(BLUE_BAND))
|
337 |
renderBands[2] = i;
|
338 |
if(colorInterpretation[i].equals(GRAY_BAND))
|
339 |
renderBands[0] = renderBands[1] = renderBands[2] = i; |
340 |
if(colorInterpretation[i].equals(RED_GREEN_BAND))
|
341 |
renderBands[0] = renderBands[1] = i; |
342 |
if(colorInterpretation[i].equals(RED_BLUE_BAND))
|
343 |
renderBands[0] = renderBands[2] = i; |
344 |
if(colorInterpretation[i].equals(GREEN_BLUE_BAND))
|
345 |
renderBands[1] = renderBands[2] = i; |
346 |
} |
347 |
|
348 |
//Ojo! esto no puede hacerse en el bucle anterior pq no detectaria bien casos
|
349 |
//como ALPHA_BAND, GRAY_BAND
|
350 |
for (int i = 0; i < colorInterpretation.length; i++) { |
351 |
if(colorInterpretation[i].equals(ALPHA_BAND))
|
352 |
renderBands[3] = i;
|
353 |
} |
354 |
return renderBands;
|
355 |
} |
356 |
|
357 |
public ColorInterpretation cloneColorInterpretation() {
|
358 |
DataStoreColorInterpretation ci = new DataStoreColorInterpretation();
|
359 |
String[] l = new String[colorInterpretation.length]; |
360 |
for (int i = 0; i < l.length; i++) { |
361 |
l[i] = colorInterpretation[i]; |
362 |
} |
363 |
ci.colorInterpretation = l; |
364 |
return ci;
|
365 |
} |
366 |
|
367 |
public static DynStruct registerDynClass() { |
368 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
369 |
DynStruct definition = manager.getDefinition("Color_Interpretation_Persistent");
|
370 |
if( definition == null ) { |
371 |
definition = manager.addDefinition( |
372 |
DataStoreColorInterpretation.class, |
373 |
"Color_Interpretation_Persistent",
|
374 |
"Color interpretation Persistent definition (FIXME)",
|
375 |
null,
|
376 |
null
|
377 |
); |
378 |
} |
379 |
|
380 |
definition.addDynFieldList("colorinterpretation")
|
381 |
.setClassOfItems(String.class)
|
382 |
.setMandatory(false);
|
383 |
|
384 |
return definition;
|
385 |
} |
386 |
|
387 |
public void saveToState(PersistentState state) throws PersistenceException { |
388 |
if(colorInterpretation != null && colorInterpretation.length > 0) |
389 |
state.set("colorinterpretation", Arrays.asList(colorInterpretation)); |
390 |
} |
391 |
|
392 |
@SuppressWarnings("unchecked") |
393 |
public void loadFromState(PersistentState state) |
394 |
throws PersistenceException {
|
395 |
List<String> descriptions = (List<String>) state.get("colorinterpretation"); |
396 |
setValues(descriptions.toArray(new String[descriptions.size()])); |
397 |
} |
398 |
} |