Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extRasterTools-SE / src / org / gvsig / rastertools / vectorizacion / filter / GrayConversionPreviewRender.java @ 27629

History | View | Annotate | Download (7.77 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 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.rastertools.vectorizacion.filter;
20

    
21
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
22
import org.gvsig.gui.beans.imagenavigator.ImageUnavailableException;
23
import org.gvsig.raster.beans.previewbase.IPreviewRenderProcess;
24
import org.gvsig.raster.dataset.FileNotOpenException;
25
import org.gvsig.raster.dataset.IBuffer;
26
import org.gvsig.raster.dataset.io.RasterDriverException;
27
import org.gvsig.raster.filter.grayscale.GrayScaleManager;
28
import org.gvsig.raster.grid.filter.FilterTypeException;
29
import org.gvsig.raster.grid.filter.RasterFilterList;
30
import org.gvsig.raster.grid.filter.RasterFilterListManager;
31
import org.gvsig.raster.grid.filter.correction.MedianListManager;
32
import org.gvsig.raster.grid.filter.correction.ModeManager;
33
import org.gvsig.raster.grid.filter.enhancement.EnhancementStretchListManager;
34
import org.gvsig.raster.grid.filter.enhancement.LinearStretchParams;
35
import org.gvsig.raster.hierarchy.IRasterRendering;
36
import org.gvsig.raster.hierarchy.IStatistics;
37
import org.gvsig.raster.util.RasterToolsUtil;
38

    
39
/**
40
 * Clase para el renderizado de la vista previa en la conversi?n 
41
 * a escala de grises.
42
 * 10/06/2008
43
 * @author Nacho Brodin nachobrodin@gmail.com
44
 */
45
public class GrayConversionPreviewRender implements IPreviewRenderProcess  {
46

    
47
        private boolean             showPreview            = false;        
48
        private FLyrRasterSE        lyr                    = null;
49
        private GrayConversionData  data                   = null;
50
        
51
        /**
52
         * Constructor. 
53
         * @param lyr
54
         */
55
        public GrayConversionPreviewRender(FLyrRasterSE lyr, GrayConversionData data) {
56
                this.lyr = lyr;
57
                this.data = data;
58
        }
59
        
60
        /*
61
         * (non-Javadoc)
62
         * @see org.gvsig.raster.beans.previewbase.IPreviewRenderProcess#process(org.gvsig.raster.hierarchy.IRasterRendering)
63
         */
64
        public void process(IRasterRendering rendering) throws FilterTypeException, ImageUnavailableException {
65
                if(!showPreview)
66
                        throw new ImageUnavailableException(RasterToolsUtil.getText(this, "panel_preview_not_available"));
67
                
68
                if(lyr == null)
69
                        throw new ImageUnavailableException(RasterToolsUtil.getText(this, "preview_not_available"));
70
                
71
                RasterFilterList filterList = rendering.getRenderFilterList();
72
                RasterFilterListManager filterManager = new RasterFilterListManager(filterList);
73

    
74
                GrayScaleManager manager = new GrayScaleManager(filterManager);
75
                manager.addGrayScaleFilter(data.getBandType());
76
                
77
                addPosterization(filterManager, rendering);
78
                addMode(filterManager, rendering);
79
                addHighPass(filterManager, rendering);
80
                addNoiseReduction(filterManager, rendering);                
81
        }
82
        
83
        /**
84
         * A?ade el paso alto si la opci?n est? activa
85
         * @throws FilterTypeException 
86
         */
87
        private void addHighPass(RasterFilterListManager filterManager, IRasterRendering rendering) throws FilterTypeException {
88
                if(data.isHighPassActive()) {
89

    
90
                }
91
        }
92

    
93
        /**
94
         * A?ade el filtro de moda si la opci?n est? activa
95
         * @throws FilterTypeException 
96
         */
97
        private void addMode(RasterFilterListManager filterManager, IRasterRendering rendering) throws FilterTypeException {
98
                if(data.isModeActive()) {
99
                        ModeManager mlm = new ModeManager(filterManager);
100
                        mlm.addModeFilter(data.getModeThreshold());
101
                }
102
        }
103
        
104
        /**
105
         * A?ade el filtro de ruido si la opci?n est? activa
106
         * @throws FilterTypeException 
107
         */
108
        private void addNoiseReduction(RasterFilterListManager filterManager, IRasterRendering rendering) throws FilterTypeException {
109
                if(data.isNoiseActive()) {
110
                        MedianListManager mlm = new MedianListManager(filterManager);
111
                        mlm.addMedianFilter(data.getNoiseThreshold());
112
                }
113
        }
114
                
115
        /**
116
         * A?ade la posterizaci?n si la opci?n est? activa
117
         * @throws FilterTypeException 
118
         */
119
        public void addPosterization(RasterFilterListManager filterManager, IRasterRendering rendering) throws FilterTypeException {
120
                if(data.isPosterizationActive()) {
121
                        EnhancementStretchListManager elm = new EnhancementStretchListManager(filterManager);
122
                        LinearStretchParams leParams = new LinearStretchParams();
123
                        
124
                        //Obtenemos el m?ximo y el m?nimo para la banda
125
                        IStatistics stats = lyr.getDataSource().getStatistics();
126
                        double min = 0;
127
                        double max = 0;
128
                        try {
129
                                stats.calcFullStatistics();
130
                                if(lyr.getDataType()[0] == IBuffer.TYPE_BYTE) {
131
                                        min = 0;
132
                                        max = 255;
133
                                } else if(data.getBandFromBandType() < 3) {
134
                                        min = stats.getMin()[data.getBandFromBandType()];
135
                                        max = stats.getMax()[data.getBandFromBandType()];
136
                                } else if(data.getBandFromBandType() == 3)  {
137
                                        min = Math.min(Math.min(stats.getMin()[0], stats.getMin()[1]), stats.getMin()[2]);
138
                                        max = Math.max(Math.max(stats.getMax()[0], stats.getMax()[1]), stats.getMax()[2]);
139
                                }
140
                        } catch (InterruptedException e) {
141
                                return;
142
                        } catch (RasterDriverException e) {
143
                                return;
144
                        } catch (FileNotOpenException e) {
145
                                return;
146
                        }
147
                        
148
                        //Creamos arrays de entrada y salida
149
                        double[] in = new double[(data.getPosterizationLevels() - 1) * 2 + 4];
150
                        int[] out = new int[(data.getPosterizationLevels() - 1) * 2 + 4];
151
                        double lastIn = 0, lastOut = 0;
152
                        
153
                        //Inicializamos los valores de los extremos
154
                        lastIn = in[0] = in[1] = min;
155
                        lastOut = out[0] = out[1] = 0;
156
                        in[in.length - 1] = in[in.length - 2] = max;
157
                        out[out.length - 1] = out[out.length - 2] = 255;
158

    
159
                        //Construimos el array de salida
160
                        int nPieces = data.getPosterizationLevels() -1;
161
                        int n = 0;
162
                        int increment = 255 / nPieces;
163
                        for (int i = 3; i < in.length - 2; i++) {
164
                                if((i % 2) != 0) {
165
                                        out[i] = (int)Math.round(lastOut + increment);
166
                                        lastOut = (int)out[i]; 
167
                                } else
168
                                        out[i] = (int)lastOut;
169
                                n++;
170
                        }
171

    
172
                        //Construimos el array de entrada
173
                        //En caso de ser conversi?n a B/W se asigna el valor de umbral para la posterizaci?n
174
                        if(data.getPosterizationLevels() == 2) {
175
                                in[2] = in[3] = lastIn + ((data.getPosterizationThreshold() * (max - min)) / 255);
176
                        } else {
177
                                for (int i = 2; i < in.length - 2; i = i + 2) {
178
                                        in[i] = in[i + 1] = lastIn + ((max - min) / data.getPosterizationLevels());
179
                                        lastIn = (int)in[i];
180
                                }        
181
                        }
182
                        
183
                        //Creamos y a?adimos el filtro
184
                        leParams.rgb = (lyr.getDataType()[0] == IBuffer.TYPE_BYTE);
185
                        leParams.red.stretchIn = in;
186
                        leParams.red.stretchOut = out;
187
                        leParams.green.stretchIn = in;
188
                        leParams.green.stretchOut = out;
189
                        leParams.blue.stretchIn = in;
190
                        leParams.blue.stretchOut = out;
191
                        elm.addEnhancedStretchFilter(leParams, 
192
                                        lyr.getDataSource().getStatistics(), 
193
                                        rendering.getRenderBands(), 
194
                                        false);
195

    
196
                }
197
        }
198

    
199
        /**
200
         * Obtiene el flag que informa de si se est? mostrando la previsualizaci?n o no.
201
         * En caso de no mostrarse se lanza una excepci?n ImageUnavailableExcepcion con el 
202
         * mensaje "La previsualizaci?n no est? disponible para este panel"
203
         * @return
204
         */
205
        public boolean isShowPreview() {
206
                return showPreview;
207
        }
208
        
209
        /**
210
         * Asigna el flag para mostrar u ocultar la preview. En caso de no mostrarse se lanza una 
211
         * excepci?n ImageUnavailableExcepcion con el mensaje "La previsualizaci?n no est? disponible para
212
         * este panel"
213
         * @param showPreview
214
         */
215
        public void setShowPreview(boolean showPreview) {
216
                this.showPreview = showPreview;
217
        }
218

    
219
}