Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / enhanced / graphics / HistogramGraphicBase.java @ 21135

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

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

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

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

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

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

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

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

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

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

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

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

    
170
        protected GCanvas         canvas          = null;
171

    
172
        protected HistogramStatus histogramRed    = null;
173
        protected HistogramStatus histogramGreen  = null;
174
        protected HistogramStatus histogramBlue   = null;
175
        protected HistogramStatus histogramGray   = null;
176
        protected HistogramStatus histogramDrawed = null;
177
        private FLyrRasterSE      lyr             = null;
178

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

    
190
        /**
191
         * Crea la lista de objetos dibujables y la guarda en un array
192
         */
193
        public void setHistogram(Histogram hist, double[] minList, double[] maxList) {
194
                long[][] table = hist.getTable();
195
                if (lyr.isRenderingAsGray()) {
196
                        setHistogramBand(GRAY, minList, maxList, table);
197
                        setHistogramBand(RED, minList, maxList, table);
198
                        setHistogramBand(GREEN, minList, maxList, table);
199
                        setHistogramBand(BLUE, minList, maxList, table);
200
                } else {
201
                        setHistogramBand(RED, minList, maxList, table);
202
                        setHistogramDrawed(RED);
203
                        setHistogramBand(GREEN, minList, maxList, table);
204
                        setHistogramBand(BLUE, minList, maxList, table);
205
                }
206
        }
207
        
208
        /**
209
         * Define el histograma para una banda de color
210
         * @param COLOR Banda al que se le aplicara el histograma
211
         * @param minList Lista de minimos
212
         * @param maxList Lista de maximos
213
         * @param table Tabla de valores de un histograma
214
         */
215
        private void setHistogramBand(int COLOR, double[] minList, double[] maxList, long[][] table) {
216
                int position = 0;
217
                switch (COLOR) {
218
                        case GREEN:
219
                                position = 1;
220
                                break;
221
                        case BLUE:
222
                                position = 2;
223
                                break;
224
                }
225

    
226
                int[] renderBands = lyr.getRenderBands();
227

    
228
                if (position >= renderBands.length)
229
                        return;
230

    
231
                int band = renderBands[position];
232

    
233
                if ((band < 0) || (band >= table.length))
234
                        return;
235

    
236
                double histogram[] = new double[table[band].length];
237
                for (int i = 0; i < histogram.length; i++)
238
                        histogram[i] = (double) table[band][i];
239
                setHistogram(histogram, COLOR);
240

    
241
                HistogramStatus histogramStatus = getHistogramStatus(COLOR);
242

    
243
                if (histogramStatus == null)
244
                        return;
245

    
246
                if ((band >= minList.length) || (band >= maxList.length))
247
                        return;
248

    
249
                histogramStatus.setLimits(minList[band], maxList[band]);
250
        }
251

    
252
        /**
253
         * Asigna el histograma marcado con la interpretaci?n de color indicada.
254
         * @param hist Histograma a asignar
255
         * @param colorInterp Band a la que corresponde el histograma
256
         */
257
        public void setHistogram(double[] hist, int colorInterp) {
258
                switch (colorInterp) {
259
                        case GREEN:
260
                                if (histogramGreen == null)
261
                                        histogramGreen = new HistogramStatus(Color.green);
262
                                histogramGreen.setHistogram(hist);
263
                                break;
264
                        case BLUE:
265
                                if (histogramBlue == null)
266
                                        histogramBlue = new HistogramStatus(Color.blue);
267
                                histogramBlue.setHistogram(hist);
268
                                break;
269
                        case GRAY:
270
                                if (histogramGray == null)
271
                                        histogramGray = new HistogramStatus(Color.gray);
272
                                histogramGray.setHistogram(hist);
273
                                break;
274
                        default: // Banda roja
275
                                if (histogramRed == null)
276
                                        histogramRed = new HistogramStatus(Color.red);
277
                                histogramRed.setHistogram(hist);
278
                                break;
279
                }
280

    
281
                if (histogramDrawed == null)
282
                        setHistogramDrawed(colorInterp);
283
        }
284
        
285
        /**
286
         * Devuelve el estado del histograma seleccionado
287
         * @param colorInterp
288
         * @return
289
         */
290
        public HistogramStatus getHistogramStatus(int colorInterp) {
291
                switch (colorInterp) {
292
                        case RED:
293
                                return histogramRed;
294
                        case GREEN:
295
                                return histogramGreen;
296
                        case BLUE:
297
                                return histogramBlue;
298
                        case GRAY:
299
                                return histogramGray;
300
                        case DRAWED:
301
                                return histogramDrawed;
302
                }
303
                return null;
304
        }
305
        
306
        /**
307
         * Asigna el histograma dibujado 
308
         * @param colorInterp
309
         */
310
        public void setHistogramDrawed(int colorInterp) {
311
                switch (colorInterp) {
312
                        case RED:
313
                                histogramDrawed = histogramRed;
314
                                break;
315
                        case GREEN:
316
                                histogramDrawed = histogramGreen;
317
                                break;
318
                        case BLUE:
319
                                histogramDrawed = histogramBlue;
320
                                break;
321
                        case GRAY:
322
                                histogramDrawed = histogramGray;
323
                                break;
324
                }
325

    
326

    
327
                if (histogramDrawed != null) {
328
                        getCanvas().replaceDrawableElement(histogramDrawed.getGraphicHistogram());
329
                        getCanvas().replaceDrawableElement(histogramDrawed.getMinMaxLines());
330
                        getCanvas().replaceDrawableElement(histogramDrawed.getBaseFunction(), BaseFunction.class);
331
                        getCanvas().replaceDrawableElement(histogramDrawed.getInfoLayer());
332
                        getCanvas().repaint();
333
                }
334
        }
335
        
336
        /**
337
         * Asigna el tipo de histograma Standard/Cumulative
338
         * @param type Tipo de histograma. El valor est? definido en las constantes de GraphicHistogram
339
         */
340
        public void setHistogramType(int type) {
341
                if (histogramDrawed != null)
342
                        histogramDrawed.getGraphicHistogram().setTypeViewed(type);
343
        }
344
                
345
        /**
346
         * Asigna el tipo
347
         * @param type
348
         */
349
        public void setType(int type) {
350
                ArrayList elements = getCanvas().getDrawableElements(GraphicHistogram.class);
351
                for (int i = 0; i < elements.size(); i++)
352
                        ((GraphicHistogram) elements.get(i)).setType(type);
353
        }
354

    
355
        /**
356
         * Obtiene el lienzo donde se dibujan las gr?ficas
357
         * @return GCanvas
358
         */
359
        public abstract GCanvas getCanvas();
360
}