Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / branches / org.gvsig.raster.tools_dataaccess_refactoring / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / enhanced / ui / EnhancedHistogramController.java @ 2312

History | View | Annotate | Download (8.45 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.tools.app.basic.tool.enhanced.ui;
23

    
24
import org.gvsig.fmap.dal.coverage.RasterLocator;
25
import org.gvsig.fmap.dal.coverage.datastruct.Params;
26
import org.gvsig.raster.swing.gcanvas.StraightLine;
27
import org.gvsig.raster.tools.app.basic.tool.enhanced.graphics.HistogramGraphicBase;
28
import org.gvsig.raster.tools.app.basic.tool.enhanced.graphics.InputHistogram;
29
import org.gvsig.raster.tools.app.basic.tool.enhanced.graphics.OutputHistogram;
30
import org.gvsig.raster.tools.app.basic.tool.enhanced.graphics.HistogramGraphicBase.HistogramStatus;
31
/**
32
 * Manager para actualizar la vista previa y el histograma de salida del cuadro
33
 * de realce
34
 * 
35
 * @author BorSanZa - Borja S?nchez Zamorano 
36
 */
37
public class EnhancedHistogramController {
38
        private InputHistogram  inputHistogram  = null;
39
        private OutputHistogram outputHistogram = null;
40
        private EnhancedDialog  enhancedDialog  = null;
41

    
42
        public EnhancedHistogramController(InputHistogram inputHistogram, OutputHistogram outputHistogram, EnhancedDialog enhancedDialog) {
43
                this.inputHistogram = inputHistogram;
44
                this.outputHistogram = outputHistogram;
45
                this.enhancedDialog = enhancedDialog;
46
        }
47
        
48
        private void renderingAsGray(HistogramStatus histogram, Params params) {
49
                if (histogram.getBaseFunction() instanceof StraightLine) {
50
                        StraightLine line = (StraightLine) histogram.getBaseFunction();
51
                        double[] valuesIn;
52
                        if (enhancedDialog.getGraphicsPanel().getRGB().isSelected())
53
                                valuesIn = line.getInValues(0, 255);
54
                        else
55
                                valuesIn = line.getInValues(histogram.getMin(), histogram.getMax());
56
                        int[] valuesOut = line.getOutValues();
57
                        params.setParam("StretchInRed", valuesIn, -1, null);
58
                        params.setParam("StretchOutRed", valuesOut, -1, null);
59
                        params.setParam("StretchRedFunctionType", Integer.valueOf(line.getFunctionType()), -1, null);
60
                        params.setParam("StretchRedValueFunction", Double.valueOf(line.getValueFunction()), -1, null);
61
                        params.setParam("StretchInGreen", valuesIn, -1, null);
62
                        params.setParam("StretchOutGreen", valuesOut, -1, null);
63
                        params.setParam("StretchGreenFunctionType", Integer.valueOf(line.getFunctionType()), -1, null);
64
                        params.setParam("StretchGreenValueFunction", Double.valueOf(line.getValueFunction()), -1, null);
65
                        params.setParam("StretchInBlue", valuesIn, -1, null);
66
                        params.setParam("StretchOutBlue", valuesOut, -1, null);
67
                        params.setParam("StretchBlueFunctionType", Integer.valueOf(line.getFunctionType()), -1, null);
68
                        params.setParam("StretchBlueValueFunction", Double.valueOf(line.getValueFunction()), -1, null);
69
                }
70
        }
71
        
72
        private void renderingAsColor(HistogramStatus histogram, 
73
                        Params params, 
74
                        String inColorLabel, 
75
                        String outColorLabel, 
76
                        String functionTypeLabel, 
77
                        String valueFunctionLabel) {
78
                if (histogram != null) {
79
                        if (histogram.getBaseFunction() instanceof StraightLine) {
80
                                StraightLine line = (StraightLine) histogram.getBaseFunction();
81
                                double[] valuesIn;
82
                                if (enhancedDialog.getGraphicsPanel().getRGB().isSelected())
83
                                        valuesIn = line.getInValues(0, 255);
84
                                else
85
                                        valuesIn = line.getInValues(histogram.getMin(), histogram.getMax());
86
                                int[] valuesOut = line.getOutValues();
87

    
88
                                params.setParam(inColorLabel, valuesIn, -1, null);
89
                                params.setParam(outColorLabel, valuesOut, -1, null);
90
                                params.setParam(functionTypeLabel, Integer.valueOf(line.getFunctionType()), -1, null);
91
                                params.setParam(valueFunctionLabel, Double.valueOf(line.getValueFunction()), -1, null);
92
                        }
93
                }
94
        }
95
        
96
        public void updatePreview() {
97
                Params params = RasterLocator.getManager().createParams("", 0, 0, null);
98
                HistogramStatus histogram = null;
99
                
100
                if (enhancedDialog.getLayer().getRender().isRenderingAsGray()) {
101
                        histogram = inputHistogram.getHistogramStatus(HistogramGraphicBase.GRAY);
102
                        renderingAsGray(histogram, params);
103
                } else {
104
                        histogram = inputHistogram.getHistogramStatus(HistogramGraphicBase.RED);
105
                        if (histogram != null) {
106
                                renderingAsColor(
107
                                                histogram, 
108
                                                params, 
109
                                                "StretchInRed", 
110
                                                "StretchOutRed", 
111
                                                "StretchRedFunctionType", 
112
                                                "StretchRedValueFunction");
113
                        }
114
                        histogram = inputHistogram.getHistogramStatus(HistogramGraphicBase.GREEN);
115
                        if (histogram != null) {
116
                                renderingAsColor(
117
                                                histogram, 
118
                                                params, 
119
                                                "StretchInGreen", 
120
                                                "StretchOutGreen", 
121
                                                "StretchGreenFunctionType", 
122
                                                "StretchGreenValueFunction");
123
                        }
124
                        histogram = inputHistogram.getHistogramStatus(HistogramGraphicBase.BLUE);
125
                        if (histogram != null) {
126
                                renderingAsColor(
127
                                                histogram, 
128
                                                params, 
129
                                                "StretchInBlue", 
130
                                                "StretchOutBlue", 
131
                                                "StretchBlueFunctionType", 
132
                                                "StretchBlueValueFunction");
133
                        }
134
                }
135
                
136
                params.setParam("TailTrimRedMin",   Double.valueOf(0.0D), -1, null);
137
                params.setParam("TailTrimRedMax",   Double.valueOf(0.0D), -1, null);
138
                params.setParam("TailTrimGreenMin", Double.valueOf(0.0D), -1, null);
139
                params.setParam("TailTrimGreenMax", Double.valueOf(0.0D), -1, null);
140
                params.setParam("TailTrimBlueMin",  Double.valueOf(0.0D), -1, null);
141
                params.setParam("TailTrimBlueMax",  Double.valueOf(0.0D), -1, null);
142
                
143
                boolean rgb = enhancedDialog.getGraphicsPanel().getRGB().isSelected();
144
                
145
                params.setParam("RGB", new Boolean(rgb), -1, null);
146
                
147
                int[] renderBands = enhancedDialog.getLayer().getRender().getRenderColorInterpretation().buildRenderBands();
148
                String render = "";
149
                for (int i = 0; i < renderBands.length; i++) {
150
                        if (render != "")
151
                                render += " ";
152
                        render = render + "" + renderBands[i];
153
                }
154
                params.setParam("RenderBands", render, -1, null);
155

    
156
                enhancedDialog.getFilteredPreview().getParamsList().clear();
157
                Class<?> filterClass = enhancedDialog.getLayer().getRender().getFilterList().getFilterClassByID("enhanced_stretch");
158
                enhancedDialog.getFilteredPreview().addNewParam("enhanced_stretch", params, filterClass);
159
                
160
                enhancedDialog.getPreviewBasePanel().refreshPreview();
161
        }
162

    
163
        /**
164
         * Actualiza el histograma de salida del cuadro de realce
165
         */
166
        public void updateHistogramOut() {
167
                HistogramStatus histogram = inputHistogram.getHistogramStatus(HistogramGraphicBase.DRAWED);
168
                if (histogram != null) {
169
                        if (histogram.getBaseFunction() instanceof StraightLine) {
170
                                StraightLine line = (StraightLine) histogram.getBaseFunction();
171
                                double[] valuesIn = line.getPercentInValues();
172
                                double[] valuesOut = line.getPercentOutValues();
173

    
174
                                double origenHistogram[] = inputHistogram.getHistogramStatus(HistogramGraphicBase.DRAWED).getHistogram();
175
                                double newHistogram[] = new double[origenHistogram.length];
176

    
177
                                for (int i = 0; i < newHistogram.length; i++)
178
                                        newHistogram[i] = 0.0D;
179

    
180
                                int pos;
181
                                double p;
182
                                for (int i = 0; i < origenHistogram.length; i++) {
183
                                        p = (((double) i) / (origenHistogram.length - 1.0D));
184
                                        
185
                                        for (int j = 0; j < (valuesIn.length - 1); j++) {
186
                                                if (valuesIn[j] == valuesIn[j + 1]) continue;
187
                                                if (p >= valuesIn[j] && p <= valuesIn[j + 1]) {
188
                                                        p = valuesOut[j] + ((valuesOut[j + 1] - valuesOut[j]) * ((p - valuesIn[j]) / (valuesIn[j + 1] - valuesIn[j])));
189
                                                        break;
190
                                                }
191
                                        }
192

    
193
                                        pos = (int) Math.round(p * (origenHistogram.length - 1.0D));
194
                                        if (pos < 0)
195
                                                pos = 0;
196
                                        if (pos > (origenHistogram.length - 1))
197
                                                pos = (origenHistogram.length - 1);
198

    
199
                                        newHistogram[pos] += origenHistogram[i];
200
                                }
201

    
202
                                HistogramStatus histogramOut = outputHistogram.getHistogramStatus(HistogramGraphicBase.DRAWED);
203
                                histogramOut.setHistogram(newHistogram);
204
                                histogramOut.setLimits(0.0D, 255.0D);
205
                                outputHistogram.repaint();
206
                        }
207
                }
208
        }
209
}