Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / enhanced / graphics / HistogramGraphicBase.java @ 20870

History | View | Annotate | Download (9.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.enhanced.graphics;
20

    
21
import java.awt.BorderLayout;
22
import java.awt.Color;
23
import java.util.ArrayList;
24

    
25
import javax.swing.JPanel;
26

    
27
import org.gvsig.raster.beans.canvas.GCanvas;
28
import org.gvsig.raster.beans.canvas.layers.GraphicHistogram;
29
import org.gvsig.raster.beans.canvas.layers.InfoLayer;
30
import org.gvsig.raster.beans.canvas.layers.MinMaxLines;
31
import org.gvsig.raster.beans.canvas.layers.functions.BaseFunction;
32
import org.gvsig.raster.beans.canvas.layers.functions.DensitySlicingLine;
33
import org.gvsig.raster.beans.canvas.layers.functions.StraightLine;
34
import org.gvsig.raster.datastruct.Histogram;
35
/**
36
 * Clase base para los gr?ficos de histogramas de entrada y salida.
37
 * 
38
 * 20/02/2008
39
 * @author Nacho Brodin nachobrodin@gmail.com
40
 */
41
public abstract class HistogramGraphicBase extends JPanel  {
42
        protected Color minMaxLineColor = Color.WHITE;
43
        protected Color borderColor     = Color.WHITE;
44
        protected Color functionColor   = Color.YELLOW;
45
        
46
        /**
47
         * Clase para tener guardados los valores de estado de una banda del histograma
48
         * 
49
         * @version 04/03/2008
50
         * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
51
         */
52
        public class HistogramStatus {
53
                private double[]         histogram        = new double[] { 0, 0, 3, 4, 5, 8, 7, 18, 45, 36, 21, 36, 12, 23, 23, 40, 17, 10, 5, 1, 0, 0, 0 };
54
                private double           min              = 0.0D;
55
                private double           max              = 1.0D;
56
                private BaseFunction     baseFunction     = null;
57
                private GraphicHistogram graphicHistogram = null;
58
                private MinMaxLines      minMaxLines      = null;
59
                private InfoLayer        infoLayer        = null;
60

    
61
                public HistogramStatus(Color color) {
62
                        graphicHistogram = new GraphicHistogram(color);
63
                        graphicHistogram.setType(GraphicHistogram.TYPE_LINE);
64
                        baseFunction = new StraightLine(functionColor);
65
                        minMaxLines = new MinMaxLines(minMaxLineColor);
66
                        infoLayer = new InfoLayer(Color.WHITE);
67
                }
68

    
69
                /**
70
                 * Establece los valores minimo y maximo
71
                 * @param min
72
                 * @param max
73
                 */
74
                public void setLimits(double min, double max) {
75
                        this.min = min;
76
                        this.max = max;
77
                        infoLayer.setLimits(min, max);
78
                }
79
                
80
                /**
81
                 * @return the histogram
82
                 */
83
                public double[] getHistogram() {
84
                        return histogram;
85
                }
86

    
87
                /**
88
                 * @param histogram the histogram to set
89
                 */
90
                public void setHistogram(double[] histogram) {
91
                        graphicHistogram.setHistogramDrawed(histogram);
92
                        this.histogram = histogram;
93
                }
94

    
95
                /**
96
                 * @return the graphicHistogram
97
                 */
98
                public GraphicHistogram getGraphicHistogram() {
99
                        return graphicHistogram;
100
                }
101

    
102
                /**
103
                 * @return the minMaxLines
104
                 */
105
                public MinMaxLines getMinMaxLines() {
106
                        return minMaxLines;
107
                }
108

    
109
                /**
110
                 * @return the straightLine
111
                 */
112
                public BaseFunction getBaseFunction() {
113
                        return baseFunction;
114
                }
115
                
116
                /**
117
                 * Asigna la funci?n dibujada. Por defecto se crea con StraightLine
118
                 * pero puede ser modificada.
119
                 * @param baseFunction BaseFunction
120
                 */
121
                public void setBaseFunction(BaseFunction baseFunction) {
122
                        this.baseFunction = baseFunction;
123
                }
124
                
125
                /**
126
                 * N?mero de cortes cuando es una funci?n level slice
127
                 * @param level
128
                 */
129
                public void setLevel(int level) {
130
                        if(baseFunction instanceof DensitySlicingLine)
131
                                ((DensitySlicingLine)baseFunction).setShape(level);
132
                }
133

    
134
                /**
135
                 * @return the min
136
                 */
137
                public double getMin() {
138
                        return min;
139
                }
140

    
141
                /**
142
                 * @return the max
143
                 */
144
                public double getMax() {
145
                        return max;
146
                }
147

    
148
                /**
149
                 * @return the infoLayer
150
                 */
151
                public InfoLayer getInfoLayer() {
152
                        return infoLayer;
153
                }
154
        }
155

    
156
        /**
157
         * Constantes que identifican un histograma con una banda
158
         */
159
        public final static int    RED             = 0;
160
        public final static int    GREEN           = 1;
161
        public final static int    BLUE            = 2;
162
        public final static int    GRAY            = 3;
163

    
164
        /**
165
         * Constante para poder coger el histograma visualizado en ese momento
166
         */
167
        public final static int    DRAWED            = 4;
168

    
169
        protected GCanvas          canvas          = null;
170

    
171
        protected HistogramStatus  histogramRed    = null;
172
        protected HistogramStatus  histogramGreen  = null;
173
        protected HistogramStatus  histogramBlue   = null;
174
        protected HistogramStatus  histogramGray   = null;
175
        protected HistogramStatus  histogramDrawed = null;
176
        protected int[]            renderBands     = null;
177

    
178
        public HistogramGraphicBase(Histogram hist, int[] renderBands, double[] minList, double[] maxList) {
179
                this.renderBands = renderBands;
180
                setHistogram(hist, minList, maxList);
181
                initialize();
182
        }
183
        
184
        private void initialize() {
185
                this.setLayout(new BorderLayout());
186
                this.add(getCanvas(), BorderLayout.CENTER);
187
        }
188

    
189
        /**
190
         * Crea la lista de objetos dibujables y la guarda en un array
191
         */
192
        public void setHistogram(Histogram hist, double[] minList, double[] maxList) {
193
                long[][] table = hist.getTable();
194
                double histogram[];
195

    
196
                switch (hist.getNumBands()) {
197
                        case 1:
198
                                histogram = new double[table[0].length];
199
                                for (int i = 0; i < histogram.length; i++)
200
                                        histogram[i] = (double) table[0][i];
201
                                setHistogram(histogram, GRAY);
202
                                histogramGray.setLimits(minList[0], maxList[0]);
203
                                break;
204
                        default: // FIXME Mirar cuando deberia entrar aqui
205
                                // A?adimos el histograma de la capa roja si se puede
206
                                if (renderBands.length < 0)
207
                                        break;
208
                                int pos = renderBands[0];
209
                                histogram = new double[table[pos].length];
210
                                for (int i = 0; i < histogram.length; i++)
211
                                        histogram[i] = (double) table[pos][i];
212
                                setHistogram(histogram, RED);
213
                                histogramRed.setLimits(minList[pos], maxList[pos]);
214
                                setHistogramDrawed(RED);
215

    
216
                                // A?adimos el histograma de la capa verde si se puede
217
                                if (renderBands.length < 1)
218
                                        break;
219
                                pos = renderBands[1];
220
                                histogram = new double[table[pos].length];
221
                                for (int i = 0; i < histogram.length; i++)
222
                                        histogram[i] = (double) table[pos][i];
223
                                setHistogram(histogram, GREEN);
224
                                histogramGreen.setLimits(minList[pos], maxList[pos]);
225

    
226
                                // A?adimos el histograma de la capa azul si se puede
227
                                if (renderBands.length < 2)
228
                                        break;
229
                                pos = renderBands[2];
230
                                histogram = new double[table[pos].length];
231
                                for (int i = 0; i < histogram.length; i++)
232
                                        histogram[i] = (double) table[pos][i];
233
                                setHistogram(histogram, BLUE);
234
                                histogramBlue.setLimits(minList[pos], maxList[pos]);
235
                                break;
236
                }
237
        }
238

    
239
        /**
240
         * Asigna el histograma marcado con la interpretaci?n de color indicada.
241
         * @param hist Histograma a asignar
242
         * @param colorInterp Band a la que corresponde el histograma
243
         */
244
        public void setHistogram(double[] hist, int colorInterp) {
245
                switch (colorInterp) {
246
                        case GREEN:
247
                                if (histogramGreen == null)
248
                                        histogramGreen = new HistogramStatus(Color.green);
249
                                histogramGreen.setHistogram(hist);
250
                                break;
251
                        case BLUE:
252
                                if (histogramBlue == null)
253
                                        histogramBlue = new HistogramStatus(Color.blue);
254
                                histogramBlue.setHistogram(hist);
255
                                break;
256
                        case GRAY:
257
                                if (histogramGray == null)
258
                                        histogramGray = new HistogramStatus(Color.gray);
259
                                histogramGray.setHistogram(hist);
260
                                break;
261
                        default: // Banda roja
262
                                if (histogramRed == null)
263
                                        histogramRed = new HistogramStatus(Color.red);
264
                                histogramRed.setHistogram(hist);
265
                                break;
266
                }
267

    
268
                if (histogramDrawed == null)
269
                        setHistogramDrawed(colorInterp);
270
        }
271
        
272
        /**
273
         * Devuelve el estado del histograma seleccionado
274
         * @param colorInterp
275
         * @return
276
         */
277
        public HistogramStatus getHistogramStatus(int colorInterp) {
278
                switch (colorInterp) {
279
                        case RED:
280
                                return histogramRed;
281
                        case GREEN:
282
                                return histogramGreen;
283
                        case BLUE:
284
                                return histogramBlue;
285
                        case GRAY:
286
                                return histogramGray;
287
                        case DRAWED:
288
                                return histogramDrawed;
289
                }
290
                return null;
291
        }
292
        
293
        /**
294
         * Asigna el histograma dibujado 
295
         * @param colorInterp
296
         */
297
        public void setHistogramDrawed(int colorInterp) {
298
                switch (colorInterp) {
299
                        case RED:
300
                                histogramDrawed = histogramRed;
301
                                break;
302
                        case GREEN:
303
                                histogramDrawed = histogramGreen;
304
                                break;
305
                        case BLUE:
306
                                histogramDrawed = histogramBlue;
307
                                break;
308
                        case GRAY:
309
                                histogramDrawed = histogramGray;
310
                                break;
311
                }
312

    
313

    
314
                if (histogramDrawed != null) {
315
                        getCanvas().replaceDrawableElement(histogramDrawed.getGraphicHistogram());
316
                        getCanvas().replaceDrawableElement(histogramDrawed.getMinMaxLines());
317
                        getCanvas().replaceDrawableElement(histogramDrawed.getBaseFunction(), BaseFunction.class);
318
                        getCanvas().replaceDrawableElement(histogramDrawed.getInfoLayer());
319
                        getCanvas().repaint();
320
                }
321
        }
322
        
323
        /**
324
         * Asigna el tipo de histograma Standard/Cumulative
325
         * @param type Tipo de histograma. El valor est? definido en las constantes de GraphicHistogram
326
         */
327
        public void setHistogramType(int type) {
328
                if (histogramDrawed != null)
329
                        histogramDrawed.getGraphicHistogram().setTypeViewed(type);
330
        }
331
                
332
        /**
333
         * Asigna el tipo
334
         * @param type
335
         */
336
        public void setType(int type) {
337
                ArrayList elements = getCanvas().getDrawableElements(GraphicHistogram.class);
338
                for (int i = 0; i < elements.size(); i++)
339
                        ((GraphicHistogram) elements.get(i)).setType(type);
340
        }
341

    
342
        /**
343
         * Obtiene el lienzo donde se dibujan las gr?ficas
344
         * @return GCanvas
345
         */
346
        public abstract GCanvas getCanvas();
347
}