Statistics
| Revision:

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 @ 2310

History | View | Annotate | Download (10.5 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.List;
26

    
27
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
28
/**
29
 * Clase que contiene la interpretaci?n de color por banda. Inicialmente
30
 * es inicializada con los valores contenidos en el raster si los tiene. Despu?s 
31
 * estos valores pueden ser modificados.
32
 * 
33
 * @author Nacho Brodin (nachobrodin@gmail.com)
34
 */
35
public class DataStoreColorInterpretation implements ColorInterpretation {
36

    
37
        /**
38
         * Interpretaci?n de color para cada banda
39
         */
40
        private String[]           colorInterpretation = null;
41
        /**
42
         * true si la imagen tiene una banda con el identificador de interpretaci?n de
43
         * color a Alpha
44
         */
45

    
46
        /**
47
         * Constructor vacio. 
48
         */
49
        public DataStoreColorInterpretation() {
50
                this.colorInterpretation = new String[0];
51
        }
52
        
53
        /**
54
         * Creates an object of this class from a bands renderer array. 
55
         */
56
        public DataStoreColorInterpretation(int[] renderBands) {
57
                if(renderBands == null || renderBands.length == 0) {
58
                        this.colorInterpretation = new String[0];
59
                        return;
60
                }
61
                
62
                this.colorInterpretation = new String[renderBands.length];
63
                for (int i = 0; i < renderBands.length; i++) {
64
                        switch (renderBands[i]) {
65
                        case 0:
66
                                colorInterpretation[i] = RED_BAND;
67
                                break;
68
                        case 1:
69
                                colorInterpretation[i] = GREEN_BAND;
70
                                break;
71
                        case 2:
72
                                colorInterpretation[i] = BLUE_BAND;
73
                                break;
74
                        case 3:
75
                                colorInterpretation[i] = ALPHA_BAND;
76
                                break;
77
                        }
78
                }
79
                for (int i = 0; i < colorInterpretation.length; i++) {
80
                        if(colorInterpretation[i] == null)
81
                                colorInterpretation[i] = UNDEF_BAND;
82
                }
83
        }
84
        
85
        /**
86
         * Constructor que asigna los valores de interpretaci?n de color 
87
         */
88
        public DataStoreColorInterpretation(String[] colorInterp) {
89
                this.colorInterpretation = colorInterp;
90
        }
91
        
92
        public static DataStoreColorInterpretation createDefaultInterpretation(int nBands) {
93
                if(nBands <= 0)
94
                        return null;
95
                if(nBands == 1)
96
                        return new DataStoreColorInterpretation(new String[]{GRAY_BAND});
97
                if(nBands == 2)
98
                        return new DataStoreColorInterpretation(new String[]{GRAY_BAND, ALPHA_BAND});
99
                if(nBands == 3)
100
                        return new DataStoreColorInterpretation(new String[]{RED_BAND, GREEN_BAND, BLUE_BAND});
101
                return new DataStoreColorInterpretation(new String[]{RED_BAND, GREEN_BAND, BLUE_BAND, ALPHA_BAND});
102
        }
103
        
104
        public static DataStoreColorInterpretation createPaletteInterpretation() {
105
                return new DataStoreColorInterpretation(new String[]{PAL_BAND});
106
        }
107

    
108
        /**
109
         * Obtiene una interpretaci?n de color GRAY
110
         * @return DatasetColorInterpretation
111
         */
112
        public static DataStoreColorInterpretation createGrayInterpretation() {
113
                return new DataStoreColorInterpretation(new String[]{GRAY_BAND});
114
        }
115
        
116
        /**
117
         * Obtiene una interpretaci?n de color RGB
118
         * @return DatasetColorInterpretation
119
         */
120
        public static DataStoreColorInterpretation createARGBInterpretation() {
121
                return new DataStoreColorInterpretation(new String[]{RED_BAND, GREEN_BAND, BLUE_BAND, ALPHA_BAND});
122
        }
123
        
124
        /**
125
         * Obtiene una interpretaci?n de color RGB
126
         * @return DatasetColorInterpretation
127
         */
128
        public static DataStoreColorInterpretation createRGBInterpretation() {
129
                return new DataStoreColorInterpretation(new String[]{RED_BAND, GREEN_BAND, BLUE_BAND});
130
        }
131
        
132
        /**
133
         * Constructor que inicializa el n?mero de valores de la interpretaci?n de 
134
         * color. Implica asignar posteriormente los valores a las bandas.
135
         */
136
        public DataStoreColorInterpretation(int values) {
137
                colorInterpretation = new String[values];
138
        }
139
        
140
        /**
141
         * Inicializa el vector de cadenas que contendr?n el nombre de la interpretaci?n 
142
         * de color asignada a cada banda. Este valor es el devuelto por la imagen.
143
         * @param values N?mero de valores
144
         */
145
        public void initColorInterpretation(int values) {
146
                colorInterpretation = new String[values];
147
        }
148
        
149
        public boolean isBGR() {
150
                if(colorInterpretation != null) {
151
                        if (colorInterpretation.length == 3 && 
152
                                colorInterpretation[0] == BLUE_BAND && 
153
                                colorInterpretation[1] == GREEN_BAND && 
154
                                colorInterpretation[2] == RED_BAND)
155
                                return true;
156
                }
157
                return false;
158
        }
159
        
160
        public boolean isRGB() {
161
                if(colorInterpretation != null) {
162
                        if (colorInterpretation.length == 3 && 
163
                                isColorInterpretation(0) && 
164
                                isColorInterpretation(1) && 
165
                                isColorInterpretation(2))
166
                                return true;
167
                }
168
                return false;
169
        }
170
        
171
        public boolean isARGB() {
172
                if(colorInterpretation != null) {
173
                        if (colorInterpretation.length == 4 && 
174
                                isInterpretationDefinedAsColor(0) && 
175
                                isInterpretationDefinedAsColor(1) && 
176
                                isInterpretationDefinedAsColor(2) &&
177
                                isAlphaInterpretation(3))
178
                                return true;
179
                }
180
                return false;
181
        }
182
        
183
        public boolean isPalette() {
184
                if(colorInterpretation != null && 
185
                        colorInterpretation[0].length() >= 1 &&
186
                        colorInterpretation[0].equals(PAL_BAND)) 
187
                        return true;
188
                return false;
189
        }
190
        
191
        private boolean isInterpretationDefinedAsColor(int band) {
192
                return isColorInterpretation(band) || isGrayInterpretation(band);
193
        }
194
        
195
        public boolean isGrayInterpretation(int band) {
196
                return colorInterpretation[band].equals(GRAY_BAND);
197
        }
198
        
199
        public boolean isColorInterpretation(int band) {
200
                return colorInterpretation[band].equals(RED_BAND) || colorInterpretation[band].equals(GREEN_BAND) || colorInterpretation[band].equals(BLUE_BAND); 
201
        }
202
        
203
        public boolean isAlphaInterpretation(int band) {
204
                return colorInterpretation[band].equals(ALPHA_BAND);
205
        }
206
        
207
        public int getAlphaBand() {
208
                String[] values = getValues();
209
                for (int i = 0; i < values.length; i++) {
210
                        if(values[i] != null && values[i].equals(ALPHA_BAND))
211
                                return i;
212
                }
213
                return -1;
214
        }        
215
        
216
        public String[] getValues() {
217
                return colorInterpretation;
218
        }
219
        
220
        /**
221
         * Asigna los valores de la interpretaci?n de color
222
         * @return String[]
223
         */
224
        public void setValues(String[] colorInterp) {
225
                colorInterpretation = colorInterp;
226
        }
227
        
228
        public void setColorInterpValue(int band, String value) {
229
                try{
230
                        //Si ya existia el valor se elimina
231
                        for (int i = 0; i < colorInterpretation.length; i++) {
232
                                if(colorInterpretation[i] != null && colorInterpretation[i].equals(value))
233
                                        colorInterpretation[i] = UNDEF_BAND;
234
                        }
235
                        
236
                        //Se asigna el valor en su entrada. Si excede la longitud se construye uno nuevo m?s largo
237
                        //que en las celdas existentes es copia del anterior
238
                        if(band >= 0 && band < colorInterpretation.length)
239
                                colorInterpretation[band] = value;
240
                        else if(band >= colorInterpretation.length) {
241
                                ColorInterpretation copyCI = new DataStoreColorInterpretation(band + 1);
242
                                for (int i = 0; i < colorInterpretation.length; i++) {
243
                                        copyCI.setColorInterpValue(i, colorInterpretation[i]);
244
                                }
245
                                colorInterpretation = copyCI.getValues();
246
                        }
247
                }catch(ArrayIndexOutOfBoundsException ex) {
248
                        //No asignamos el elemento
249
                }
250
        }
251
        
252
        public int getBand(String id) {
253
                if(colorInterpretation != null) {
254
                        for(int i = 0; i < colorInterpretation.length; i++)
255
                                if(colorInterpretation[i] != null && colorInterpretation[i].equals(id))
256
                                        return i;
257
                }
258
                return -1;
259
        }
260
        
261
        /**
262
         * Obtiene la posici?n de las bandas que contienen el identificador pasado por par?metro 
263
         * o null si no tiene dicho identificador.
264
         * @return Lista con las posiciones de las bandas que contienen el identificador o null si no lo tiene.
265
         */
266
        public int[] getBands(String id) {
267
                if(colorInterpretation != null){
268
                        List<Integer> array = new ArrayList<Integer>();
269
                        for(int i = 0; i < colorInterpretation.length; i++)
270
                                if(colorInterpretation[i].equals(id))
271
                                        array.add(new Integer(i));
272
                        int[] list = new int[array.size()];
273
                        for (int i = 0; i < list.length; i++) 
274
                                list[i] = ((Integer)array.get(i)).intValue();
275
                        return list;
276
                }
277
                return null;
278
        }
279

    
280
        public boolean hasAlphaBand() {
281
                String[] values = getValues();
282
                for (int i = 0; i < values.length; i++) {
283
                        if(values[i]  != null && values[i].equals("Alpha"))
284
                                return true;
285
                }
286
                return false;
287
        }        
288
        
289
        public int length() {
290
                return colorInterpretation.length;
291
        }
292
        
293
        public String get(int i) {
294
                if (i >= colorInterpretation.length)
295
                        return null;
296
                return colorInterpretation[i];
297
        }
298
        
299
        public void addColorInterpretation(ColorInterpretation ci) {
300
                String[] newCI = new String[colorInterpretation.length + ci.length()];
301
                for (int i = 0; i < colorInterpretation.length; i++)
302
                        newCI[i] = colorInterpretation[i];
303
                for (int i = 0; i < ci.length(); i++) {
304
                        newCI[colorInterpretation.length + i] = ci.get(i);
305
                }
306
                this.colorInterpretation = newCI;
307
        }
308
        
309
        public boolean isUndefined() {
310
                for (int i = 0; i < colorInterpretation.length; i++) {
311
                        if (colorInterpretation[i] != null) {
312
                                if (!colorInterpretation[i].equals(UNDEF_BAND) &&
313
                                                !colorInterpretation[i].equals(ALPHA_BAND))
314
                                        return false;
315
                        }
316
                }
317
                return true;
318
        }
319
        
320
        public int[] getRenderBandsFromColorInterpretation() {
321
                if(colorInterpretation == null)
322
                        return null;
323
                int[] renderBands = new int[]{-1, -1, -1, -1};
324
                for (int i = 0; i < colorInterpretation.length; i++) {
325
                        if(colorInterpretation[i].equals(RED_BAND))
326
                                renderBands[0] = i;
327
                        if(colorInterpretation[i].equals(GREEN_BAND))
328
                                renderBands[1] = i;
329
                        if(colorInterpretation[i].equals(BLUE_BAND))
330
                                renderBands[2] = i;
331
                        if(colorInterpretation[i].equals(GRAY_BAND))
332
                                renderBands[0] = renderBands[1] = renderBands[2] = i;
333
                }
334
                
335
                //Ojo! esto no puede hacerse en el bucle anterior pq no detectaria bien casos 
336
                //como ALPHA_BAND, GRAY_BAND
337
                for (int i = 0; i < colorInterpretation.length; i++) {
338
                        if(colorInterpretation[i].equals(ALPHA_BAND))
339
                                renderBands[3] = i;
340
                }
341
                return renderBands;
342
        }
343
        
344
        public ColorInterpretation cloneColorInterpretation() {
345
                DataStoreColorInterpretation ci = new DataStoreColorInterpretation();
346
                String[] l = new String[colorInterpretation.length];
347
                for (int i = 0; i < l.length; i++) {
348
                        l[i] = colorInterpretation[i];
349
                }
350
                ci.colorInterpretation = l;
351
                return ci;
352
        }
353
}