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

History | View | Annotate | Download (10.1 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 int getOutRasterDataType() {
143
                return Buffer.TYPE_BYTE;
144
        }
145

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

    
154
        public String getGroup() {
155
                return "radiometricos";
156
        }
157

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

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

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

    
329
        public void post() {
330
                // En caso de que nadie apunte a raster, se liberar? su memoria.
331
                raster = null;
332
        }
333

    
334
        public int getInRasterDataType() {
335
                return 0;
336
        }
337

    
338
        public void process(int x, int y) {
339
        }
340

    
341
        public String[] getNames() {
342
                return names;
343
        }
344

    
345
        public boolean isVisible() {
346
                return false;
347
        }
348

    
349
        /**
350
         * @return the stretchs
351
         */
352
        public DefaultLinearStretchParams getStretchs() {
353
                return stretchs;
354
        }
355
}