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

History | View | Annotate | Download (7.06 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
         * Constructor que asigna los valores de interpretaci?n de color 
55
         */
56
        public DataStoreColorInterpretation(String[] colorInterp) {
57
                this.colorInterpretation = colorInterp;
58
        }
59
        
60
        public static DataStoreColorInterpretation createDefaultInterpretation(int nBands) {
61
                if(nBands <= 0)
62
                        return null;
63
                if(nBands == 1)
64
                        return new DataStoreColorInterpretation(new String[]{GRAY_BAND});
65
                if(nBands == 2)
66
                        return new DataStoreColorInterpretation(new String[]{GRAY_BAND, ALPHA_BAND});
67
                if(nBands == 3)
68
                        return new DataStoreColorInterpretation(new String[]{RED_BAND, GREEN_BAND, BLUE_BAND});
69
                return new DataStoreColorInterpretation(new String[]{RED_BAND, GREEN_BAND, BLUE_BAND, ALPHA_BAND});
70
        }
71
        
72
        public static DataStoreColorInterpretation createPaletteInterpretation() {
73
                return new DataStoreColorInterpretation(new String[]{PAL_BAND});
74
        }
75

    
76
        /**
77
         * Obtiene una interpretaci?n de color GRAY
78
         * @return DatasetColorInterpretation
79
         */
80
        public static DataStoreColorInterpretation createGrayInterpretation() {
81
                return new DataStoreColorInterpretation(new String[]{GRAY_BAND});
82
        }
83
        
84
        /**
85
         * Obtiene una interpretaci?n de color RGB
86
         * @return DatasetColorInterpretation
87
         */
88
        public static DataStoreColorInterpretation createARGBInterpretation() {
89
                return new DataStoreColorInterpretation(new String[]{RED_BAND, GREEN_BAND, BLUE_BAND, ALPHA_BAND});
90
        }
91
        
92
        /**
93
         * Obtiene una interpretaci?n de color RGB
94
         * @return DatasetColorInterpretation
95
         */
96
        public static DataStoreColorInterpretation createRGBInterpretation() {
97
                return new DataStoreColorInterpretation(new String[]{RED_BAND, GREEN_BAND, BLUE_BAND});
98
        }
99
        
100
        /**
101
         * Constructor que inicializa el n?mero de valores de la interpretaci?n de 
102
         * color. Implica asignar posteriormente los valores a las bandas.
103
         */
104
        public DataStoreColorInterpretation(int values) {
105
                colorInterpretation = new String[values];
106
        }
107
        
108
        /**
109
         * Inicializa el vector de cadenas que contendr?n el nombre de la interpretaci?n 
110
         * de color asignada a cada banda. Este valor es el devuelto por la imagen.
111
         * @param values N?mero de valores
112
         */
113
        public void initColorInterpretation(int values) {
114
                colorInterpretation = new String[values];
115
        }
116
        
117
        public boolean isRGB() {
118
                if(colorInterpretation != null) {
119
                        if (colorInterpretation.length == 3 && 
120
                                colorInterpretation[0] == RED_BAND && 
121
                                colorInterpretation[1] == BLUE_BAND && 
122
                                colorInterpretation[2] == BLUE_BAND)
123
                                return true;
124
                }
125
                return false;
126
        }
127
        
128
        public boolean isARGB() {
129
                if(colorInterpretation != null) {
130
                        if (colorInterpretation.length == 4 && 
131
                                colorInterpretation[0] == RED_BAND && 
132
                                colorInterpretation[1] == BLUE_BAND && 
133
                                colorInterpretation[2] == BLUE_BAND &&
134
                                colorInterpretation[3] == ALPHA_BAND)
135
                                return true;
136
                }
137
                return false;
138
        }
139
        
140
        public String[] getValues() {
141
                return colorInterpretation;
142
        }
143
        
144
        /**
145
         * Asigna los valores de la interpretaci?n de color
146
         * @return String[]
147
         */
148
        public void setValues(String[] colorInterp) {
149
                colorInterpretation = colorInterp;
150
        }
151
        
152
        public void setColorInterpValue(int band, String value) {
153
                try{
154
                        colorInterpretation[band] = value;
155
                }catch(ArrayIndexOutOfBoundsException ex) {
156
                        //No asignamos el elemento
157
                }
158
        }
159
        
160
        public int getBand(String id) {
161
                if(colorInterpretation != null) {
162
                        for(int i = 0; i < colorInterpretation.length; i++)
163
                                if(colorInterpretation[i] != null && colorInterpretation[i].equals(id))
164
                                        return i;
165
                }
166
                return -1;
167
        }
168
        
169
        /**
170
         * Obtiene la posici?n de las bandas que contienen el identificador pasado por par?metro 
171
         * o null si no tiene dicho identificador.
172
         * @return Lista con las posiciones de las bandas que contienen el identificador o null si no lo tiene.
173
         */
174
        public int[] getBands(String id) {
175
                if(colorInterpretation != null){
176
                        List<Integer> array = new ArrayList<Integer>();
177
                        for(int i = 0; i < colorInterpretation.length; i++)
178
                                if(colorInterpretation[i].equals(id))
179
                                        array.add(new Integer(i));
180
                        int[] list = new int[array.size()];
181
                        for (int i = 0; i < list.length; i++) 
182
                                list[i] = ((Integer)array.get(i)).intValue();
183
                        return list;
184
                }
185
                return null;
186
        }
187

    
188
        public boolean hasAlphaBand() {
189
                String[] values = getValues();
190
                for (int i = 0; i < values.length; i++) {
191
                        if(values[i]  != null && values[i].equals("Alpha"))
192
                                return true;
193
                }
194
                return false;
195
        }        
196
        
197
        public int length() {
198
                return colorInterpretation.length;
199
        }
200
        
201
        public String get(int i) {
202
                if (i >= colorInterpretation.length)
203
                        return null;
204
                return colorInterpretation[i];
205
        }
206
        
207
        public void addColorInterpretation(ColorInterpretation ci) {
208
                String[] newCI = new String[colorInterpretation.length + ci.length()];
209
                for (int i = 0; i < colorInterpretation.length; i++)
210
                        newCI[i] = colorInterpretation[i];
211
                for (int i = 0; i < ci.length(); i++) {
212
                        newCI[colorInterpretation.length + i] = ci.get(i);
213
                }
214
                this.colorInterpretation = newCI;
215
        }
216
        
217
        public boolean isUndefined() {
218
                for (int i = 0; i < colorInterpretation.length; i++) {
219
                        if (colorInterpretation[i] != null) {
220
                                if (!colorInterpretation[i].equals(UNDEF_BAND) &&
221
                                                !colorInterpretation[i].equals(ALPHA_BAND))
222
                                        return false;
223
                        }
224
                }
225
                return true;
226
        }
227
        
228
        public ColorInterpretation cloneColorInterpretation() {
229
                DataStoreColorInterpretation ci = new DataStoreColorInterpretation();
230
                String[] l = new String[colorInterpretation.length];
231
                for (int i = 0; i < l.length; i++) {
232
                        l[i] = colorInterpretation[i];
233
                }
234
                ci.colorInterpretation = l;
235
                return ci;
236
        }
237
}