Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / datastruct / DefaultStretch.java @ 168

History | View | Annotate | Download (6.97 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.datastruct;
23

    
24
import org.gvsig.fmap.dal.coverage.datastruct.Stretch;
25
import org.gvsig.fmap.dal.coverage.store.props.Statistics;
26

    
27
public class DefaultStretch implements Stretch {
28
        /**
29
         * Valores de los datos de entrada correspondientes al m?nimo y al m?ximo de 
30
         * cada tramo. Estos tendr?n un rango entre el m?nimo y el m?ximo en cada banda
31
         * de la imagen. 
32
         */
33
        public double[]        stretchIn           = null;
34
        /**
35
         * Valores de los datos de salida correspondientes al m?nimo y al m?ximo de 
36
         * cada tramo. Estos tendr?n un rango entre 0 y 255.
37
         */
38
        public int[]           stretchOut          = null;
39
        /**
40
         * Porcentajes de recorte de colas (Valores de entrada)
41
         */
42
        public double          tailTrimMin         = 0;
43
        public double          tailTrimMax         = 0;
44
        /**
45
         * Valores de recorte de colas (Valores de salida calculados por el filtro TailTrim)
46
         */
47
        public double          tailTrimValueMin    = 0;
48
        public double          tailTrimValueMax    = 0;
49
        /**
50
         * Valores de escala a calcular por el filtro lineal por tramos.
51
         * Cada elemento del array es un tramo.
52
         */
53
        public double[]        scale               = null;
54
        /**
55
         * Valores de desplazamiento a calcular por el filtro lineal por tramos.
56
         * Cada elemento del array es un tramo.
57
         */
58
        public double[]        offset              = null;
59
        /**
60
         * Valores m?ximos y m?nimos
61
         */
62
        public double          maxValue            = 0;
63
        public double          minValue            = 0;
64
        
65
        /**
66
         * Funcion grafica que se ha usado para generar el LinearStretchParams
67
         * 0 - Lineal
68
         * 1 - Exponencial / Logaritmica
69
         * 2 - Raiz cuadrada / Cuadrada
70
         * 3 - Level slice
71
         */
72
        public int             functionType        = 0;
73

    
74
        /**
75
         * Valor de la gr?fica para la funci?n. No se usa en una gr?fica lineal.
76
         */
77
        public double          valueFunction       = 0.0;
78
        
79
        /**
80
         * Aplica el recorte de colas sobre los extremos de los m?ximos y m?nimos de entrada.
81
         * La aplicaci?n de esta funci?n considera que ya se han calculado los valores con 
82
         * loadTailTrimValues.
83
         */
84
        public void applyTrimToStretchs() {
85
                if(stretchIn != null && stretchIn.length >= 2) {
86
                        minValue = stretchIn[0] = tailTrimValueMin;
87
                        maxValue = stretchIn[stretchIn.length - 1] = tailTrimValueMax;
88
                }
89
        }
90
        
91
        /**
92
         * Aplica el eliminado de extremos. Para ello utiliza el segundo m?ximo y m?nimo de entrada.
93
         */
94
        /*public void applyRemoveEndsToStretchs(double secondMin, double secondMax) {
95
                if(stretchIn != null && stretchIn.length >= 2) {
96
                        minValue = stretchIn[0] = secondMin;
97
                        maxValue = stretchIn[stretchIn.length - 1] = secondMax;
98
                }
99
        }*/
100
        
101
        /**
102
         * Consulta si tiene alg?n valor el recorte de colas
103
         * @return true si tiene valor el recorte de colas y false si no lo tiene
104
         */
105
        public boolean hasTailTrim() {
106
                return (tailTrimMin != 0 || tailTrimMax != 0);
107
        }
108
        
109
        /**
110
         * Calcula la escala y el desplazamiento teniendo en cuenta que
111
         * ya tenga todos los valores de entrada asignados.
112
         *
113
         */
114
        public void calcLinearScaleAndOffset() {
115
                if(stretchIn != null && stretchOut != null) {
116
                        //simplifyStretch();
117
                        scale = new double[stretchIn.length - 1];
118
                        offset = new double[stretchIn.length - 1];
119
                        for (int i = 0; i < scale.length; i++) {
120
                                double rgbRange = (stretchOut[i + 1] - stretchOut[i]);
121
                                if((stretchIn[i + 1] - stretchIn[i]) == 0)
122
                                        scale[i] = 0;
123
                                else
124
                                        scale[i] = rgbRange / (stretchIn[i + 1] - stretchIn[i]);
125
                                offset[i] = stretchOut[i];
126
                        }
127
                }
128
        }
129
        
130
        /**
131
         * Elimina puntos redundantes en stretchIn y stretchOut
132
         */
133
//        private void simplifyStretch() {
134
//                boolean simplified = false;
135
//                for (int i = 0; i < (stretchIn.length - 1); i++) {
136
//                        if(stretchIn[i] == stretchIn[i + 1]) {
137
//                                double[] auxIn = new double[stretchIn.length -1];
138
//                                int[] auxOut = new int[stretchIn.length -1];
139
//                                for (int j = 0; j < auxIn.length; j++) {
140
//                                        if(j < i) {
141
//                                                auxIn[j] = stretchIn[j];
142
//                                                auxOut[j] = stretchOut[j];
143
//                                        } else {
144
//                                                auxIn[j] = stretchIn[j + 1];
145
//                                                auxOut[j] = stretchOut[j + 1];
146
//                                        }
147
//                                                
148
//                                }
149
//                                stretchIn = auxIn;
150
//                                stretchOut = auxOut;
151
//                                simplified = true;
152
//                        }
153
//                }
154
//                if(simplified)
155
//                        simplifyStretch();
156
//        }
157
        
158
        /**
159
         * Asigna el m?ximo y el m?nimo
160
         * @param stats
161
         * @param type
162
         * @param band
163
         */
164
        public void setMaxMin(Statistics stats, int band, boolean rgb) {
165
                try {
166
                        if (rgb) {
167
                                if (stats.getMinByteUnsigned() != null)
168
                                        minValue = stats.getMinByteUnsigned()[band];
169
                                if (stats.getMaxByteUnsigned() != null)
170
                                        maxValue = stats.getMaxByteUnsigned()[band];
171
                        } else {
172
                                if (stats.getMin() != null) {
173
                                        minValue = stats.getMin()[band];
174
                                }
175
                                if (stats.getMax() != null) {
176
                                        maxValue = stats.getMax()[band];
177
                                }
178
                        }
179
                        if (stretchIn == null) {
180
                                stretchIn = new double[] { minValue, maxValue };
181
                        } else {
182
                                stretchIn[0] = minValue;
183
                                stretchIn[stretchIn.length - 1] = maxValue;
184
                        }
185
                } catch (ArrayIndexOutOfBoundsException ex) {
186
                        // No se asigna el m?ximo o m?nimo
187
                }
188
        }
189
        
190
        /**
191
         * Aplica el eliminado de extremos. Para ello utiliza el segundo m?ximo y m?nimo de entrada.
192
         */
193
        public void applyRemoveEndsToStretchs(Statistics stats, boolean rgb, int band) {
194
                if(stretchIn == null)
195
                        return;
196
                try {
197
                        if(rgb) {
198
                                if(stats.getSecondMinByteUnsigned() != null)
199
                                        stretchIn[0] = minValue = stats.getSecondMinByteUnsigned()[band];
200
                                if(stats.getSecondMaxByteUnsigned() != null)
201
                                        stretchIn[stretchIn.length - 1] = maxValue = stats.getSecondMaxByteUnsigned()[band];
202
                        } else {
203
                                if(stats.getSecondMin() != null)
204
                                        stretchIn[0] = minValue = stats.getSecondMin()[band];
205
                                if(stats.getMax() != null)
206
                                        stretchIn[stretchIn.length - 1] = maxValue = stats.getSecondMax()[band];
207
                        }
208
                } catch (ArrayIndexOutOfBoundsException ex) {
209
                        //No se asigna el m?ximo o m?nimo 
210
                }
211
        }
212

    
213
        public double[] getOffset() {
214
                return offset;
215
        }
216

    
217
        public double getMaxValue() {
218
                return maxValue;
219
        }
220

    
221
        public double getMinValue() {
222
                return minValue;
223
        }
224

    
225
        public int getFunctionType() {
226
                return functionType;
227
        }
228

    
229
        public double[] getStretchIn() {
230
                return stretchIn;
231
        }
232

    
233
        public int[] getStretchOut() {
234
                return stretchOut;
235
        }
236

    
237
        public double getValueFunction() {
238
                return valueFunction;
239
        }
240
}