Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / properties / DatasetTransparency.java @ 10996

History | View | Annotate | Download (9.87 KB)

1 10939 nacho
/* 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 GeoRasterFile o varios agrupados GeoMultiRasterFile. Por esto
32
         * el estado de transparencia de un GeoMultiRasterFile ser? la suma de los estados
33
         * de los GeoRasterFile que lo componen.
34
         * </P>
35
         * <P>
36
         * El estado de transparencia de una fuente de datos est?
37
         * asignada como propiedad a un GeoRasterFile 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
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
                 * Inicializa la lista de rangos de transparencia.
195
                 */
196
                public void clearListOfTransparencyRange(){
197
                        transparencyRanges.clear();
198
                }
199
200
                /**
201
                 * Asigna un rango de pixels que son transparentes en el raster.
202
                 * @param range
203
                 */
204
                public void setTransparencyRange(String range)throws TransparencyRangeException{
205
                        TransparencyRange tr = new TransparencyRange();
206
                        int[] red = new int[2];
207
                    int[] green = new int[2];
208
                    int[] blue = new int[2];
209
                    boolean and = true;
210
                    try{
211
                            and = TransparencyRange.stringToInterval(range, red, green, blue);
212
                            if(red[0] == -1)
213
                                    red = null;
214
                            if(green[0] == -1)
215
                                    green = null;
216
                            if(blue[0] == -1)
217
                                    blue = null;
218
                    }catch(IOException e){
219
                            throw new TransparencyRangeException("Cadena de rangos malformada.");
220
                    }
221
                    tr.setAnd(and);
222
                    tr.setRed(red);
223
                    tr.setGreen(green);
224
                    tr.setBlue(blue);
225
                    transparencyRanges.add(tr);
226
                }
227
228
                /**
229
                 * Obtiene el flag de existencia de banda de transparencia.
230
                 * @return true existe una banda con informaci?n de transparencia y false no existe.
231
                 */
232
                public boolean isTransparencyBand() {
233
                        return (transparencyBandNumber < 0);
234
                }
235
236
                /**
237
                 * Asigna el n?mero de banda de transparencia.
238
                 * @param transparencyBand
239
                 */
240
                public void setTransparencyBand(int transparencyBand) {
241
                        transparencyBandNumber = transparencyBand;
242
                }
243
244
                /**
245
                 * Obtiene el n?mero de la banda de transparencia
246
                 * @return
247
                 */
248
                public int getTransparencyBandNumber(){
249
                        return transparencyBandNumber;
250
                }
251
252
                /**
253
                 * Obtiene el color que se aplica a la transparencia
254
                 * @return array de tres elementos con R, G, B del color (valor por defecto 255, 255, 255)
255
                 */
256
                public int[] getTransparencyColor(){
257
                        return new int[]{transparencyColorRed, transparencyColorGreen, transparencyColorBlue};
258
                }
259
260
                /**
261
                 * Asigna el color que se aplica a la transparencia
262
                 * @param rgb array de tres elementos con R, G, B del color (valor por defecto 255, 255, 255)
263
                 */
264
                public void setTransparencyColor(int[] rgb){
265
                        if(rgb == null)
266
                                return;
267
                        transparencyColorRed = rgb[0];
268
                        transparencyColorGreen = rgb[1];
269
                        transparencyColorBlue = rgb[2];
270
                }
271
272
                /**
273
                 * Obtiene el grado de opacidad de todo el raster
274
                 * @return valor del grado de opacidad.
275
                 */
276
                public int getOpacity() {
277
                        return opacity;
278
                }
279
280
                /**
281
                 * Asigna el grado de opacidad de todo el raster
282
                 * @param opacity valor del grado de opacidad.
283
                 */
284
                public void setOpacity(int opacity) {
285
                        this.opacity = opacity;
286
                }
287
288
                /**
289
                 * Obtiene alpha
290
                 * @return valor alpha
291
                 */
292
                public int getAlpha() {
293
                        return alpha;
294
                }
295
296
                /**
297
                 * Asigna alpha
298
                 * @param alpha
299
                 */
300
                public void setAlpha(int alpha) {
301
                        this.alpha = alpha;
302
                }
303
304
                /**
305
                 * Asigna la transparencia a partir de un objeto con los metadatos del raster.
306
                 * @param metadata
307
                 */
308
                public void setTransparencyByPixelFromMetadata(DatasetMetadata metadata){
309
                        if(metadata != null){
310
                                TransparencyRange[] noData = metadata.parserNodataInMetadata();
311
                                if(noData != null){
312
                                        for(int i = 0; i < noData.length; i++)
313
                                                getTransparencyRange().add(noData[i]);
314
                                }
315
                                TransparencyRange noDataValue = metadata.parserNodataByBand();
316
                                if(noData == null  && noDataValue != null)
317
                                        getTransparencyRange().add(noDataValue);
318
                        }
319
320
                }
321
322
        }