Statistics
| Revision:

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

History | View | Annotate | Download (7.16 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
                renderBands = (int[]) params.get("renderBands");
69
                if(renderBands != null && renderBands.length < raster.getBandCount()) {
70
                        int[] newRenderBands = new int[raster.getBandCount()];
71
                        for (int i = 0; i < renderBands.length; i++)
72
                                newRenderBands[i] = renderBands[i];
73
                        for (int i = renderBands.length; i < newRenderBands.length; i++)
74
                                newRenderBands[i] = i;
75
                        renderBands = newRenderBands;
76
                }
77
                height = raster.getHeight();
78
                width = raster.getWidth();
79

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

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

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

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

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

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

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

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

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

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

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

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

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