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 / grid / filter / enhancement / LinearStretchEnhancementFilter.java @ 2438

History | View | Annotate | Download (10.2 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.grid.filter.enhancement;
23

    
24
import org.gvsig.fmap.dal.coverage.RasterLibrary;
25
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
26
import org.gvsig.fmap.dal.coverage.datastruct.Params;
27
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
28
import org.gvsig.fmap.dal.coverage.exception.FilterAddException;
29
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
30
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
31
import org.gvsig.fmap.dal.coverage.grid.filter.BaseRasterFilter;
32
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
33
import org.gvsig.fmap.dal.coverage.store.props.Statistics;
34
import org.gvsig.raster.impl.datastruct.DefaultStretch;
35
import org.gvsig.raster.impl.store.ParamsImpl;
36
/**
37
 * Clase base para los filtros de realzado lineal. Lee el m?nimo y m?xmo de la
38
 * clase Statistic que ser?n calculados por PercentTailTrimFilter o
39
 * ComputeMinMaxFilter dependiendo de si est? activado el recorte de colas o no.
40
 * En Statistic tambi?n est?n los segundos valores despu?s del m?nimo y m?ximo
41
 * que son los que se utilizan con la opci?n eliminar extremos activada. Estos
42
 * se usaran en vez del m?nimo y m?ximo cuando la variable removeExtrema est? a
43
 * true.
44
 *
45
 * @author Nacho Brodin (nachobrodin@gmail.com)
46
 */
47
public class LinearStretchEnhancementFilter extends BaseRasterFilter {
48
        protected double[][]                      scale              = null;
49
        protected double[][]                      offset             = null;
50
        protected Statistics                          stats              = null;
51
        protected double[]                        minBandValue             = null;
52
        protected double[]                        maxBandValue             = null;
53
        public static String[]                    names              = new String[] {"enhanced_stretch"};
54
        private boolean                           removeEnds         = false;
55
        protected DefaultLinearStretchParams      stretchs           = null;
56
        protected DefaultStretch[]                scaleOffsetList    = null;
57
        protected int[]                           renderBands        = null; 
58

    
59
        /**
60
         * Construye un LinearEnhancementFilter
61
         */
62
        public LinearStretchEnhancementFilter() {
63
                setName(names[0]);
64
        }
65

    
66
        public void pre() throws FilterAddException {
67
                super.pre();
68
                stats = (Statistics)getEnv().get("SrcStatistics");
69
                
70
                stretchs = (DefaultLinearStretchParams) params.get("stretchs");
71
                if (params.get("remove") != null)
72
                        removeEnds = ((Boolean) params.get("remove")).booleanValue();
73

    
74
                if (stretchs == null)
75
                        return;
76
                
77
                try {
78
                        stats.calculate(RasterLibrary.statisticsScale);
79
                } catch (FileNotOpenException e) {
80
                        exec = false;
81
                } catch (RasterDriverException e) {
82
                        exec = false;
83
                } catch (ProcessInterruptedException e) {
84
                        exec = false;
85
                }
86

    
87
                if (raster.getDataType() != Buffer.TYPE_BYTE)
88
                        stretchs.rgb = false;
89
                
90
                ColorInterpretation outputColorInterp = getColorInterpretation();
91
                renderBands = outputColorInterp.buildRenderBands();
92
                
93
                stretchs.setMaxMin(stats, renderBands);
94

    
95
                if (removeEnds)
96
                        stretchs.applyRemoveEndsToStretchs(stats, renderBands);
97

    
98
                stretchs.loadTailTrimValues(stats);
99

    
100
                if (stretchs.hasTailTrim())
101
                        stretchs.applyTrimToStretchs();
102

    
103
                stretchs.calcLinearScaleAndOffset();
104

    
105
                loadStretchList();
106

    
107
                createBufferResult(Buffer.TYPE_BYTE, raster.getBandCount());
108
        }
109

    
110
        /**
111
         * La lista de escalas y desplazamientos es un array de 3 elementos en el que
112
         * cada posici?n es un objeto Stretch con la escala y desplazamiento de la
113
         * banda que se dibuja en esa posici?n. El objetivo es aplicar a cada banda
114
         * el m?ximo y m?nimo que le corresponde. Por ejemplo, cuando tenemos una imagen de
115
         * 3 bandas de tipo short y queremos visualizar en RGB solo la primera banda entonces
116
         * escaleOffsetList tendr? stretchs.red en las 3 posiciones.
117
         */
118
        private void loadStretchList() {
119
                scaleOffsetList = new DefaultStretch[3];
120
                scaleOffsetList[0] = stretchs.red;
121
                scaleOffsetList[1] = stretchs.green;
122
                scaleOffsetList[2] = stretchs.blue;
123
        }
124

    
125
        /**
126
         * Obtiene el porcentaje de recorte de colas aplicado o 0 si no tiene.
127
         * @return
128
         */
129
        public Double getTailTrim() {
130
                double[] tailTrimList;
131
                if (stretchs != null)
132
                        tailTrimList = stretchs.getTailTrimList();
133
                else
134
                        tailTrimList = new double[0];
135
                double median = 0;
136
                double nValues = tailTrimList.length;
137
                for (int i = 0; i < tailTrimList.length; i++)
138
                        median += tailTrimList[i];
139
                return new Double(nValues > 0 ? median / nValues : median);
140
        }
141
        
142
        public boolean isToConvertToRGB() {
143
                return true;
144
        }
145

    
146
        public int getOutRasterDataType() {
147
                return Buffer.TYPE_BYTE;
148
        }
149

    
150
        /**
151
         * Obtiene true si est? activado el flag de eliminar extremos y false si no lo
152
         * est?
153
         */
154
        public Boolean getRemoveEnds() {
155
                return new Boolean(removeEnds);
156
        }
157

    
158
        public String getGroup() {
159
                return "radiometricos";
160
        }
161

    
162
        public Params getUIParams(String nameFilter) {
163
                if(stretchs == null)
164
                        stretchs = (DefaultLinearStretchParams) this.params.get("stretchs");
165
                Params params = new ParamsImpl();
166
                params.setParam("TailTrim",
167
                                new Double(Math.round(getTailTrim().doubleValue() * 100.0)),
168
                                Params.SLIDER,
169
                                new String[]{ "0", "100", "0", "1", "25" }); //min, max, valor defecto, intervalo peque?o, intervalo grande;
170
                
171
                
172
                params.setParam("StretchInRed",
173
                                stretchs.red.stretchIn,
174
                                Params.CHOICE,
175
                                null);
176
                params.setParam("StretchInGreen",
177
                                stretchs.green.stretchIn,
178
                                Params.CHOICE,
179
                                null);
180
                params.setParam("StretchInBlue",
181
                                stretchs.blue.stretchIn,
182
                                Params.CHOICE,
183
                                null);
184
                params.setParam("StretchOutRed",
185
                                stretchs.red.stretchOut,
186
                                Params.CHOICE,
187
                                null);
188
                params.setParam("StretchOutGreen",
189
                                stretchs.green.stretchOut,
190
                                Params.CHOICE,
191
                                null);
192
                params.setParam("StretchOutBlue",
193
                                stretchs.blue.stretchOut,
194
                                Params.CHOICE,
195
                                null);
196
                
197
                
198
                params.setParam("TailTrimRedMin",
199
                                new Double(stretchs.red.tailTrimMin),
200
                                Params.CHOICE,
201
                                null);
202
                params.setParam("TailTrimRedMax",
203
                                new Double(stretchs.red.tailTrimMax),
204
                                Params.CHOICE,
205
                                null);
206
                params.setParam("TailTrimGreenMin",
207
                                new Double(stretchs.green.tailTrimMin),
208
                                Params.CHOICE,
209
                                null);
210
                params.setParam("TailTrimGreenMax",
211
                                new Double(stretchs.green.tailTrimMax),
212
                                Params.CHOICE,
213
                                null);
214
                params.setParam("TailTrimBlueMin",
215
                                new Double(stretchs.blue.tailTrimMin),
216
                                Params.CHOICE,
217
                                null);
218
                params.setParam("TailTrimBlueMax",
219
                                new Double(stretchs.blue.tailTrimMax),
220
                                Params.CHOICE,
221
                                null);
222
                
223
                
224
                params.setParam("RedMaxValue",
225
                                new Double(stretchs.red.maxValue),
226
                                Params.CHOICE,
227
                                null);
228
                params.setParam("RedMinValue",
229
                                new Double(stretchs.red.minValue),
230
                                Params.CHOICE,
231
                                null);
232
                params.setParam("GreenMaxValue",
233
                                new Double(stretchs.green.maxValue),
234
                                Params.CHOICE,
235
                                null);
236
                params.setParam("GreenMinValue",
237
                                new Double(stretchs.green.minValue),
238
                                Params.CHOICE,
239
                                null);
240
                params.setParam("BlueMaxValue",
241
                                new Double(stretchs.blue.maxValue),
242
                                Params.CHOICE,
243
                                null);
244
                params.setParam("BlueMinValue",
245
                                new Double(stretchs.blue.minValue),
246
                                Params.CHOICE,
247
                                null);
248
                
249

    
250
                params.setParam("TailTrimRedValueMax",
251
                                new Double(stretchs.red.tailTrimValueMax),
252
                                Params.CHOICE,
253
                                null);
254
                params.setParam("TailTrimRedValueMin",
255
                                new Double(stretchs.red.tailTrimValueMin),
256
                                Params.CHOICE,
257
                                null);
258
                params.setParam("TailTrimGreenValueMax",
259
                                new Double(stretchs.green.tailTrimValueMax),
260
                                Params.CHOICE,
261
                                null);
262
                params.setParam("TailTrimGreenValueMin",
263
                                new Double(stretchs.green.tailTrimValueMin),
264
                                Params.CHOICE,
265
                                null);
266
                params.setParam("TailTrimBlueValueMax",
267
                                new Double(stretchs.blue.tailTrimValueMax),
268
                                Params.CHOICE,
269
                                null);
270
                params.setParam("TailTrimBlueValueMin",
271
                                new Double(stretchs.blue.tailTrimValueMin),
272
                                Params.CHOICE,
273
                                null);
274
                
275
                params.setParam("RedOffset",
276
                                stretchs.red.offset,
277
                                Params.CHOICE,
278
                                null);
279
                params.setParam("GreenOffset",
280
                                stretchs.green.offset,
281
                                Params.CHOICE,
282
                                null);
283
                params.setParam("BlueOffset",
284
                                stretchs.blue.offset,
285
                                Params.CHOICE,
286
                                null);
287
                params.setParam("RedScale",
288
                                stretchs.red.scale,
289
                                Params.CHOICE,
290
                                null);
291
                params.setParam("GreenScale",
292
                                stretchs.green.scale,
293
                                Params.CHOICE,
294
                                null);
295
                params.setParam("BlueScale",
296
                                stretchs.blue.scale,
297
                                Params.CHOICE,
298
                                null);
299
                
300
                
301
                params.setParam("Remove",
302
                                new Boolean(removeEnds),
303
                                Params.CHOICE,
304
                                null);
305
                /*if(renderBands == null)
306
                        renderBands = (int[]) this.params.get("renderBands");
307
                params.setParam("RenderBands",
308
                                convertArrayToString(renderBands),
309
                                Params.NONE,
310
                                null);*/
311
                params.setParam("RGB",
312
                                new Boolean(stretchs.rgb),
313
                                Params.NONE,
314
                                null);
315
                return params;
316
        }
317

    
318
        /**
319
         * Convierte un array de dobles a una cadena
320
         * @param values
321
         * @return
322
         */
323
        /*private String convertArrayToString(int[] values) {
324
                StringBuffer buffer = new StringBuffer();
325
                for (int i = 0; i < values.length; i++) {
326
                        buffer.append(values[i]);
327
                        if (i < (values.length - 1))
328
                                buffer.append(" ");
329
                }
330
                return buffer.toString();
331
        }*/
332

    
333
        public void post() {
334
                // En caso de que nadie apunte a raster, se liberar? su memoria.
335
                raster = null;
336
        }
337

    
338
        public int getInRasterDataType() {
339
                return 0;
340
        }
341

    
342
        public void process(int x, int y) {
343
        }
344

    
345
        public String[] getNames() {
346
                return names;
347
        }
348

    
349
        public boolean isVisible() {
350
                return false;
351
        }
352

    
353
        /**
354
         * @return the stretchs
355
         */
356
        public DefaultLinearStretchParams getStretchs() {
357
                return stretchs;
358
        }
359
}