Statistics
| Revision:

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

History | View | Annotate | Download (7.21 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 IBuffer                  rasterResult        = null;
49
        protected int                      nbands = 3;
50
        protected int[]                    renderBands = null;
51
        public static String[]             names = new String[] {"enhanced"};
52

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

    
60
        /*
61
         * (non-Javadoc)
62
         * @see org.gvsig.raster.grid.filter.RasterFilter#pre()
63
         */
64
        public void pre() {
65
                raster = (IBuffer) params.get("raster");
66
                stats = (DatasetListStatistics) params.get("stats");
67
                removeEnds = ((Boolean) params.get("remove")).booleanValue();
68
                tailTrim = ((Double) params.get("tailTrim")).doubleValue();
69
                renderBands = (int[]) params.get("renderBands");
70
                if(renderBands != null && renderBands.length < raster.getBandCount()) {
71
                        int[] newRenderBands = new int[raster.getBandCount()];
72
                        for (int i = 0; i < renderBands.length; i++)
73
                                newRenderBands[i] = renderBands[i];
74
                        for (int i = renderBands.length; i < newRenderBands.length; i++)
75
                                newRenderBands[i] = i;
76
                        renderBands = newRenderBands;
77
                }
78
                height = raster.getHeight();
79
                width = raster.getWidth();
80

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

    
122
                for (int i = 0; i < minBandValue.length; i++) {
123
                        scale[i] = 255D / (maxBandValue[i] - minBandValue[i]);
124
                        offset[i] = (255D * minBandValue[i]) / (minBandValue[i] - maxBandValue[i]);
125
                }
126

    
127
                nbands = stats.getBandCount();
128
                rasterResult = RasterBuffer.getBuffer(IBuffer.TYPE_BYTE, raster.getWidth(), raster.getHeight(), raster.getBandCount(), true);
129
        }
130

    
131
        /**
132
         * Obtiene true si est? activado el flag de eliminar extremos y false si no lo
133
         * est?
134
         */
135
        public Boolean getRemoveEnds() {
136
                return new Boolean(removeEnds);
137
        }
138

    
139
        /**
140
         * Obtiene el porcentaje de recorte de colas aplicado o 0 si no tiene.
141
         * @return
142
         */
143
        public Double getTailTrim(){
144
                return new Double(tailTrim);
145
        }
146

    
147
        /*
148
         * (non-Javadoc)
149
         * @see org.gvsig.raster.grid.filter.RasterFilter#getOutRasterDataType()
150
         */
151
        public int getOutRasterDataType() {
152
                return IBuffer.TYPE_BYTE;
153
        }
154

    
155
        /*
156
         * (non-Javadoc)
157
         * @see org.gvsig.raster.grid.filter.RasterFilter#getResult(java.lang.String)
158
         */
159
        public Object getResult(String name) {
160
                if (name.equals("raster"))
161
                        if(!exec)
162
                                return (Object) this.raster;
163
                        else
164
                                return (Object) this.rasterResult;
165
                return null;
166
        }
167

    
168
        /*
169
         * (non-Javadoc)
170
         * @see org.gvsig.raster.grid.filter.RasterFilter#getGroup()
171
         */
172
        public String getGroup() {
173
                return "radiometricos";
174
        }
175

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

    
193
        /*
194
         * (non-Javadoc)
195
         * @see org.gvsig.raster.grid.filter.RasterFilter#post()
196
         */
197
        public void post() {
198
                // En caso de que nadie apunte a raster, se liberar? su memoria.
199
                raster = null;
200
        }
201

    
202
        /*
203
         * (non-Javadoc)
204
         * @see org.gvsig.raster.grid.filter.RasterFilter#getInRasterDataType()
205
         */
206
        public int getInRasterDataType() {
207
                return 0;
208
        }
209

    
210
        /*
211
         * (non-Javadoc)
212
         * @see org.gvsig.raster.grid.filter.RasterFilter#process(int, int)
213
         */
214
        public void process(int x, int y) {
215
        }
216

    
217
        /*
218
         * (non-Javadoc)
219
         * @see org.gvsig.raster.grid.filter.RasterFilter#getNames()
220
         */
221
        public String[] getNames() {
222
                return names;
223
        }
224
        
225
        /*
226
         * (non-Javadoc)
227
         * @see org.gvsig.raster.grid.filter.RasterFilter#isVisible()
228
         */
229
        public boolean isVisible() {
230
                return true;
231
        }
232
}