Statistics
| Revision:

root / trunk / libraries / libCq_CMS_praster / src / org / cresques / filter / enhancement / TransparencyRange.java @ 8026

History | View | Annotate | Download (7.52 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 *
4
 * Copyright (C) 2004-5.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 * cresques@gmail.com
23
 */
24
package org.cresques.filter.enhancement;
25

    
26
import java.io.IOException;
27

    
28
        /**
29
         * Clase que representa a un conjunto de pixeles con
30
         * transparencia. Incluye los rangos de transparencia para cada banda, 
31
         * la cadena que va en la lista y la operaci?n que se realiza entre 
32
         * ellos. Un And significa que ser?n transparentes todos los pixeles
33
         * que cumplan con R con G y con B. Un Or significar? que tendr?n transparencia
34
         * todos los pixeles que cumplan con R con G o con B.  
35
         * @author Nacho Brodin (brodin_ign@gva.es)
36
         *
37
         */
38
        public class TransparencyRange{
39
                private String         strEntry = null;
40
                private int[]        red = null;
41
                private int[]        green = null;
42
                private int[]        blue = null;
43
                private boolean        and = true;
44
                
45
                /**
46
                 * Obtiene la operaci?n  utilizada
47
                 * @param and Si es true significa que se usa un AND y false implica
48
                 * que se usa un OR
49
                 */
50
                public boolean isAnd() {
51
                        return and;
52
                }
53
                
54
                /**
55
                 * Asigna la operaci?n AND como la utilizada
56
                 * @param and booleano que si est? a true significa que el la operaci?n
57
                 * AND es la utilizada como operaci?n.
58
                 */
59
                public void setAnd(boolean and) {
60
                        this.and = and;
61
                }
62
                
63
                /**
64
                 * Obtiene el intervalo de valores correspondiente a la banda del azul
65
                 * @return Array bidimensional de enteros correspondiente a la banda del azul
66
                 */
67
                public int[] getBlue() {
68
                        return blue;
69
                }
70
                
71
                /**
72
                 * Asigna el intervalo de valores correspondiente a la banda del azul
73
                 * @param blue Array bidimensional de enteros correspondiente a la banda del azul
74
                 */
75
                public void setBlue(int[] blue) {
76
                        this.blue = blue;
77
                }
78
                
79
                /**
80
                 * Obtiene el intervalo de valores correspondiente a la banda del verde
81
                 * @return Array bidimensional de enteros correspondiente a la banda del verde
82
                 */
83
                public int[] getGreen() {
84
                        return green;
85
                }
86
                
87
                /**
88
                 * Asigna el intervalo de valores correspondiente a la banda del verde
89
                 * @param green Array bidimensional de enteros correspondiente a la banda del verde
90
                 */
91
                public void setGreen(int[] green) {
92
                        this.green = green;
93
                }
94
                
95
                /**
96
                 * Obtiene el intervalo de valores correspondiente a la banda del rojo
97
                 * @return Array bidimensional de enteros correspondiente a la banda del rojo
98
                 */
99
                public int[] getRed() {
100
                        return red;
101
                }
102
                
103
                /**
104
                 * Asigna el intervalo de valores correspondiente a la banda del rojo
105
                 * @param red Array bidimensional de enteros correspondiente a la banda del rojo
106
                 */
107
                public void setRed(int[] red) {
108
                        this.red = red;
109
                }
110
                
111
                /**
112
                 * Obtiene la cadena que representa una entrada en la tabla.
113
                 * @return Cadena que representa una entrada en la tabla
114
                 */
115
                public String getStrEntry() {
116
                        return strEntry;
117
                }
118
                
119
                /**
120
                 * Asigna la cadena que representa una entrada en la tabla.
121
                 * @param strEntry Cadena que representa una entrada en la tabla.
122
                 */
123
                public void setStrEntry(String strEntry) {
124
                        this.strEntry = strEntry;
125
                }
126
                
127
                
128
                /**
129
                 * Esta funci?n valida la cadena de entrada por medio de una m?quina de estados. Valida las
130
                 * siguientes posibilidades en la cadena de entrada:
131
                 * <LI>
132
                 * <UL>(valor_rojo) & (Valor_verde) & (Valor_azul)</UL>
133
                 * <UL>(valor_rojo) | (Valor_verde) | (Valor_azul)</UL>
134
                 * </LI>
135
                 * Despues de la validaci?n parsea el contenido y carga los par?metros r,g,b con los
136
                 * intervalos de valores. Estos par?metros deben ser pasados como arrays de enteros de 
137
                 * dos elementos.  
138
             * @param values
139
             * @param r        Intervalo de rojo
140
             * @param g Intervalo de verde
141
             * @param b Intervalo de azul
142
             * @return Devuelve true si la operaci?n usada en los intervalos es un AND y false si es un OR
143
             */
144
                public static boolean stringToInterval(String values, int[] r, int[] g, int[] b) throws IOException {
145
                        int status = 0;
146
                    int countAnd = 0, countOr = 0;
147
                    boolean and = true;
148
                    for(int i=0;i<values.length();i++){
149
                      char c = values.charAt(i);
150
                      switch(status){
151
                      case 0:         if(c == ' ')status = 0;
152
                                              else if ((c >= 48 && c <= 57) || c == '*')status = 1;
153
                                             else status = 4;
154
                                             break;
155
                      case 1:         if ((c >= 48 && c <= 57) || (c == ' '))status = 1;
156
                                             else if(c == ':') status = 2;
157
                                             else if(c == '&'){
158
                                                        status = 0;
159
                                                        countAnd++;
160
                                               }else if(c == '|'){
161
                                                        status = 0;
162
                                                        countOr++;
163
                                               }else status = 4;
164
                                             break;
165
                      case 2:        if (c >= 48 && c <= 57) status = 3;
166
                                              else status = 4;
167
                                              break;
168
                      case 3:         if ((c >= 48 && c <= 57) || (c == ' '))status = 3;
169
                                              else if(c == '&'){
170
                                                      status = 0;
171
                                                      countAnd++;
172
                                              }else if(c == '|'){
173
                                                      status = 0;
174
                                                      countOr++;
175
                                              }else status = 4;
176
                                                break;
177
                      case 4:        throw new IOException("Error en la cadena de entrada "+status);
178
                      }
179
                    }
180
                            
181
                    //Si el analisis es correcto parseamos
182
                    if((status == 1 || status ==3) && ((countAnd == 2 && countOr == 0)  || 
183
                                                            (countAnd == 0 && countOr == 2)) ){
184
                              String[] s = values.split(" & ");
185
                                  if(s.length < 3){
186
                                          s = values.split(" \\| ");
187
                                          and = false;
188
                                  }
189
                                  ;
190
                                String[] val = s[0].split(":");
191
                                try{
192
                                        r[0] = Integer.parseInt(val[0]);
193
                                         if(val.length == 2)
194
                                                 r[1] = Integer.parseInt(val[1]);
195
                                         else
196
                                                r[1] = Integer.parseInt(val[0]);
197
                                }catch(NumberFormatException e){
198
                                        r [0] = -1;r [1] = -1;
199
                                }
200
                                 
201
                                val = s[1].split(":");
202
                                try{
203
                                          g[0] = Integer.parseInt(val[0]);
204
                                        if(val.length == 2)
205
                                                g[1] = Integer.parseInt(val[1]);
206
                                        else
207
                                                g[1] = Integer.parseInt(val[0]);
208
                                }catch(NumberFormatException e){
209
                                        g[0] = -1;g[1] = -1;
210
                                }
211
                                  
212
                                val = s[2].split(":");
213
                                try{
214
                                        b[0] = Integer.parseInt(val[0]);
215
                                        if(val.length == 2)
216
                                                b[1] = Integer.parseInt(val[1]);
217
                                        else
218
                                                b[1] = Integer.parseInt(val[0]);  
219
                                }catch(NumberFormatException e){
220
                                        b [0] = -1;b [1] = -1;
221
                                }
222
                  }else
223
                         throw new IOException("Error en la cadena de entrada ");
224
                      
225
                  return and;
226
                }
227
            
228
                /**
229
                 * Carga la cadena StrEntry leyendo los valores en los vectores que representa los intervalos.
230
                 */
231
                public void loadStrEntryFromValues(){
232
                        String separator = " | ";
233
                        if(and)
234
                                separator = " & ";
235
                        strEntry = String.valueOf(red[0] + separator+ green[0] + separator + blue[0]);
236
                }
237
                
238
                /**
239
                 * Muestra los datos del objeto para depuraci?n.
240
                 */
241
                public void show(){
242
                        if(getRed() != null)
243
                                System.out.println(getRed()[0]+" "+getRed()[1]);
244
                        if(getGreen() != null)
245
                                System.out.println(getGreen()[0]+" "+getGreen()[1]);
246
                        if(getBlue() != null)
247
                                System.out.println(getBlue()[0]+" "+getBlue()[1]);
248
                        System.out.println(isAnd());
249
                        System.out.println(getStrEntry());
250
                        System.out.println("--------------------");
251
                }
252
        }