Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / enhancement / LinearEnhancementFilter.java @ 21803

History | View | Annotate | Download (7.44 KB)

1
/* 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.grid.filter.enhancement;
20

    
21
import org.gvsig.raster.buffer.RasterBuffer;
22
import org.gvsig.raster.dataset.FileNotOpenException;
23
import org.gvsig.raster.dataset.IBuffer;
24
import org.gvsig.raster.dataset.Params;
25
import org.gvsig.raster.dataset.io.RasterDriverException;
26
import org.gvsig.raster.dataset.properties.DatasetListStatistics;
27
import org.gvsig.raster.grid.filter.RasterFilter;
28
/**
29
 * Clase base para los filtros de realzado lineal. Lee el m?nimo y m?xmo de la
30
 * clase Statistic que ser?n calculados por PercentTailTrimFilter o
31
 * ComputeMinMaxFilter dependiendo de si est? activado el recorte de colas o no.
32
 * En Statistic tambi?n est?n los segundos valores despu?s del m?nimo y m?ximo
33
 * que son los que se utilizan con la opci?n eliminar extremos activada. Estos
34
 * se usaran en vez del m?nimo y m?ximo cuando la variable removeExtrema est? a
35
 * true.
36
 *
37
 * @version 31/05/2007
38
 * @author Nacho Brodin (nachobrodin@gmail.com)
39
 */
40
public class LinearEnhancementFilter extends RasterFilter {
41
        protected double[]                 scale = new double[3];
42
        protected double[]                 offset = new double[3];
43
        protected DatasetListStatistics           stats = null;
44
        protected double[]                 minBandValue        = null;
45
        protected double[]                 maxBandValue        = null;
46
        protected boolean                  removeEnds = false;
47
        protected double                   tailTrim        = 0D;
48
        protected int                      nbands = 3;
49
        protected int[]                    renderBands = null;
50
        public static String[]             names = new String[] {"enhanced"};
51

    
52
        /**
53
         * Construye un LinearEnhancementFilter
54
         */
55
        public LinearEnhancementFilter() {
56
                setName(names[0]);
57
        }
58

    
59
        /*
60
         * (non-Javadoc)
61
         * @see org.gvsig.raster.grid.filter.RasterFilter#pre()
62
         */
63
        public void pre() {
64
                raster = (IBuffer) params.get("raster");
65
                stats = (DatasetListStatistics) params.get("stats");
66
                removeEnds = ((Boolean) params.get("remove")).booleanValue();
67
                tailTrim = ((Double) params.get("tailTrim")).doubleValue();
68
                // En realidad esto no se deberia hacer, pero por logica, un valor del 50%
69
                // devolveria una capa en blanco, asi que evitamos un resultado supuestamente
70
                // no esperado
71
                if ((tailTrim >= 0.5) && (tailTrim < 0.51))
72
                        tailTrim = 0.51;
73
                if ((tailTrim > 0.49) && (tailTrim < 0.50))
74
                        tailTrim = 0.49;
75
                renderBands = (int[]) params.get("renderBands");
76
                if(renderBands != null && renderBands.length < raster.getBandCount()) {
77
                        int[] newRenderBands = new int[raster.getBandCount()];
78
                        for (int i = 0; i < renderBands.length; i++)
79
                                newRenderBands[i] = renderBands[i];
80
                        for (int i = renderBands.length; i < newRenderBands.length; i++)
81
                                newRenderBands[i] = i;
82
                        renderBands = newRenderBands;
83
                }
84
                height = raster.getHeight();
85
                width = raster.getWidth();
86

    
87
                try {
88
                        stats.calcFullStatistics();
89
                } catch (FileNotOpenException e) {
90
                        exec = false;
91
                } catch (RasterDriverException e) {
92
                        exec = false;
93
                } catch (InterruptedException e) {
94
                        exec = false;
95
                }
96
                double[][] tailTrimByBand = (double[][]) stats.getTailTrimValue(tailTrim);
97
                if ((tailTrim != 0) && (tailTrimByBand != null)) { // Max y Min con recorte de colas
98
                        scale = new double[tailTrimByBand.length];
99
                        offset = new double[tailTrimByBand.length];
100
                        minBandValue = new double[tailTrimByBand.length];
101
                        maxBandValue = new double[tailTrimByBand.length];
102
                        for (int i = 0; i < tailTrimByBand.length; i++) {
103
                                minBandValue[i] = tailTrimByBand[i][0];
104
                                maxBandValue[i] = tailTrimByBand[i][1];
105
                        }
106
                } else {
107
                        scale = new double[stats.getMin().length];
108
                        offset = new double[stats.getMin().length];
109
                        if (removeEnds) { // Si est? activado eliminar extremos gastamos el 2? m?ximo/m?nimo
110
                                if(raster.getDataType() == IBuffer.TYPE_BYTE) {
111
                                        minBandValue = stats.getSecondMinRGB();
112
                                        maxBandValue = stats.getSecondMaxRGB();
113
                                } else {
114
                                        minBandValue = stats.getSecondMin();
115
                                        maxBandValue = stats.getSecondMax();
116
                                }
117
                        } else { // Si no est? activado eliminar extremos
118
                                if(raster.getDataType() == IBuffer.TYPE_BYTE) {
119
                                        minBandValue = stats.getMinRGB();
120
                                        maxBandValue = stats.getMaxRGB();
121
                                } else {
122
                                        minBandValue = stats.getMin();
123
                                        maxBandValue = stats.getMax();
124
                                }
125
                        }
126
                }
127

    
128
                for (int i = 0; i < minBandValue.length; i++) {
129
                        scale[i] = 255D / (maxBandValue[i] - minBandValue[i]);
130
                        offset[i] = (255D * minBandValue[i]) / (minBandValue[i] - maxBandValue[i]);
131
                }
132

    
133
                nbands = stats.getBandCount();
134
                rasterResult = RasterBuffer.getBuffer(IBuffer.TYPE_BYTE, raster.getWidth(), raster.getHeight(), raster.getBandCount(), true);
135
        }
136

    
137
        /**
138
         * Obtiene true si est? activado el flag de eliminar extremos y false si no lo
139
         * est?
140
         */
141
        public Boolean getRemoveEnds() {
142
                return new Boolean(removeEnds);
143
        }
144

    
145
        /**
146
         * Obtiene el porcentaje de recorte de colas aplicado o 0 si no tiene.
147
         * @return
148
         */
149
        public Double getTailTrim(){
150
                return new Double(tailTrim);
151
        }
152

    
153
        /*
154
         * (non-Javadoc)
155
         * @see org.gvsig.raster.grid.filter.RasterFilter#getOutRasterDataType()
156
         */
157
        public int getOutRasterDataType() {
158
                return IBuffer.TYPE_BYTE;
159
        }
160

    
161
        /*
162
         * (non-Javadoc)
163
         * @see org.gvsig.raster.grid.filter.RasterFilter#getResult(java.lang.String)
164
         */
165
        public Object getResult(String name) {
166
                if (name.equals("raster")) {
167
                        if (!exec)
168
                                return this.raster;
169
                        return this.rasterResult;
170
                }
171
                return null;
172
        }
173

    
174
        /*
175
         * (non-Javadoc)
176
         * @see org.gvsig.raster.grid.filter.RasterFilter#getGroup()
177
         */
178
        public String getGroup() {
179
                return "radiometricos";
180
        }
181

    
182
        /*
183
         * (non-Javadoc)
184
         * @see org.gvsig.raster.grid.filter.RasterFilter#getUIParams()
185
         */
186
        public Params getUIParams(String nameFilter) {
187
                Params params = new Params();
188
                params.setParam("RemoveEnds",
189
                                new Boolean(removeEnds),
190
                                Params.CHECK,
191
                                null);
192
                params.setParam("TailTrim",
193
                                new Double(Math.round(tailTrim * 100.0)),
194
                                Params.SLIDER,
195
                                new String[]{ "0", "100", "0", "1", "25" }); //min, max, valor defecto, intervalo peque?o, intervalo grande;
196
                return params;
197
        }
198

    
199
        /*
200
         * (non-Javadoc)
201
         * @see org.gvsig.raster.grid.filter.RasterFilter#post()
202
         */
203
        public void post() {
204
                // En caso de que nadie apunte a raster, se liberar? su memoria.
205
                raster = null;
206
        }
207

    
208
        /*
209
         * (non-Javadoc)
210
         * @see org.gvsig.raster.grid.filter.RasterFilter#getInRasterDataType()
211
         */
212
        public int getInRasterDataType() {
213
                return 0;
214
        }
215

    
216
        /*
217
         * (non-Javadoc)
218
         * @see org.gvsig.raster.grid.filter.RasterFilter#process(int, int)
219
         */
220
        public void process(int x, int y) {
221
        }
222

    
223
        /*
224
         * (non-Javadoc)
225
         * @see org.gvsig.raster.grid.filter.RasterFilter#getNames()
226
         */
227
        public String[] getNames() {
228
                return names;
229
        }
230
        
231
        /*
232
         * (non-Javadoc)
233
         * @see org.gvsig.raster.grid.filter.RasterFilter#isVisible()
234
         */
235
        public boolean isVisible() {
236
                return true;
237
        }
238
}