Revision 10950

View differences:

trunk/extensions/extRasterTools-SE/src/org/gvsig/rastertools/histogram/Histogram.java
1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 *
4
 * Copyright (C) 2004-5.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 * cresques@gmail.com
23
 */
24
package org.gvsig.rastertools.histogram;
25

  
26
import org.cresques.px.PxRaster;
27
import org.gvsig.raster.grid.Grid;
28
/**
29
 * Clase para la gesti?n de histogramas de un raster. Es la encargada del calculo de un histograma
30
 * total o parcial de un raster a partir de los datos de disco. Este calculo se hace leyendo los
31
 * datos por bloques para no cargarlo completamente en memoria. Adem?s tambi?n es la encargada de gestionar
32
 * salvar este histograma a .rmf. En caso de que solicite un histograma del raster completo este ir?
33
 * a buscarlo al fichero rmf asociado antes de calcularlo por si ya existise. Al realizar el calculo
34
 * del histograma de la imagen completa este ser? salvado en el fichero .rmf asociado al raster.
35
 * 
36
 * @author Nacho Brodin (brodin_ign@gva.es)
37
 */
38
public class Histogram extends Thread{
39
	
40
	/**
41
	 * Tama?o del bloque de calculo de histograma. La lectura del raster se divide en partes
42
	 * de BLOCK de altura para no cargar todo el raster en memoria de una vez.
43
	 */
44
	private static final int			BLOCK = 512;
45
	private Grid 						grid = null;
46

  
47
	/**
48
	 * Flag que dice si el histograma a calcualar es el seleccionado en el area de interes
49
	 * o el histograma completo.
50
	 */ 
51
	private boolean 					calcFullHistogram = true;
52
	/**
53
	 * Histograma de la imagen completa
54
	 */
55
	private int[][]						histogram = null;
56
	/**
57
	 * Histograma acumulado de la imagen completa
58
	 */
59
	private int[][]						accumulatedHistogram = null;
60
	/**
61
	 * Variables que definen una ?rea de interes
62
	 */
63
	private int 						xAOI, yAOI, widthAOI, heightAOI;
64
	/**
65
	 * Porcentaje que lleva construido el histograma
66
	 */
67
	private int							percent = 0;
68
	/**
69
	 * Renderizador para el calculo de histogramas a partir 
70
	 * de la visualizaci?n.
71
	 */
72
	private PxRaster					pxRaster = null;
73
	/**
74
	 * Histograma de la vista
75
	 */
76
	private int[][]						viewHistogram = null;
77
	/**
78
	 * Bandas que solicita el cliente. Cada elemento del array es una banda, si est? a true se devuelve 
79
	 * esa banda con histograma calculado y si est? a false se devolver? esa banda con el histograma a 0.
80
	 * Este array solo es ten?do en cuenta para el histograma con fuente de datos reales y no para el de
81
	 * la visualizaci?n.
82
	 */
83
	private boolean[]					showBands = {true, true, true};
84
	/**
85
	 * Constructor 
86
	 * @param grid
87
	 */
88
	public Histogram(Grid grid){
89
		addGrid(grid);
90
	}
91
	
92
	/**
93
	 * Sustituye el grid del histograma 
94
	 * @param grid
95
	 */
96
	public void addGrid(Grid grid){
97
		this.grid = grid;
98
	}
99

  
100
	/**
101
	 * Obtiene el grid asociado al histograma
102
	 * @return Grid
103
	 */
104
	public Grid getGrid() {
105
		return grid;
106
	}
107
	
108
	/**
109
	 * Obtiene el renderizador asociado al histograma
110
	 * @return PxRaster
111
	 */
112
	public PxRaster getRenderizer() {
113
		return pxRaster;
114
	}
115
	
116
	/**
117
	 * Asigna el renderizador para el calculo de histogramas a partir 
118
	 * de la visualizaci?n.
119
	 * @param pxR Renderizador.
120
	 */
121
	public void addRenderizer(PxRaster pxR){
122
		this.pxRaster = pxR;
123
	}
124
	
125
	/**
126
	 * Asigna un area de interes para el calculo de histograma
127
	 * @param x Posici?n X del area de interes
128
	 * @param y Posici?n Y del area de interes
129
	 * @param w Ancho del ?rea de interes
130
	 * @param h Alto del ?rea de interes
131
	 */
132
	public void setAreaOfInterest(int x, int y, int w, int h){
133
		this.xAOI = x;
134
		this.yAOI = y;
135
		this.widthAOI = w;
136
		this.heightAOI = h;
137
	}
138
	
139
	/**
140
	 * Obtiene el histograma de la imagen completa en caso de que este ya haya sido
141
	 * calculado. Si histogram es null habr? que llamar a la funci?n calcFullHistogram para
142
	 * calcularlo.
143
	 * @return histograma de la imagen completa
144
	 */
145
	public int[][] getFullHistogram(){
146
		return histogram;
147
	}
148
	
149
	/**
150
	 * Obtiene el histograma acumulado de la imagen completa en caso de que este ya haya sido
151
	 * calculado. Si histogram es null habr? que llamar a la funci?n calcFullAccumulatedHistogram para
152
	 * calcularlo.
153
	 * @return histograma acumulado de la imagen completa
154
	 */
155
	public int[][] getFullAccumulatedHistogram(){
156
		histogram = null;
157
		
158
		return accumulatedHistogram;
159
	}
160
	
161
	/**
162
	 * Calcula el histograma de la imagen completa.
163
	 */
164
	public synchronized void run(){
165
/*
166
 
167
		while(true){
168
			//Asignamos las bandas que van a dibujarse
169
			int[] drawableBands = new int[grid.getBandCount()];
170
			for(int i = 0; i < grid.getBandCount(); i++)
171
				drawableBands[i] = i;
172
			grid.clearDrawableBand();
173
			grid.addDrawableBands(drawableBands);
174
			
175
			int heightArea = grid.getHeight();
176
			int widthArea = grid.getWidth();
177
			int poxInitX = 0;
178
			int lastY = 0;
179
			if(!calcFullHistogram){ //Calculo del histograma en el ?rea de interes
180
				heightArea = heightAOI;
181
				widthArea = widthAOI;
182
				poxInitX = xAOI;
183
				lastY = yAOI;
184
			}
185
			
186
			//Calculamos bloques de tama?o BLOCK para no leer toda la imagen de golpe
187
			int nBlocks = (int)(heightArea / BLOCK);
188
			int lastBlock = heightArea - (nBlocks * BLOCK);
189
			int incrPercent = (int)(100 / (nBlocks + 1));
190
	
191
			switch(grid.getDataType()){
192
			case RasterBuf.TYPE_BYTE: byteHistogram(poxInitX, lastY, widthArea, nBlocks, lastBlock, incrPercent); break;
193
			case RasterBuf.TYPE_SHORT: shortHistogram(poxInitX, lastY, widthArea, nBlocks, lastBlock, incrPercent); break;
194
			case RasterBuf.TYPE_INT: intHistogram(poxInitX, lastY, widthArea, nBlocks, lastBlock, incrPercent); break;
195
			}
196
			percent = 100;
197
			
198
			//Si hemos calculado el histograma de la imagen completa lo salvamos a disco
199
			if(calcFullHistogram)
200
				try{
201
					writeFullHistogramToRMF();
202
				}catch(IOException ex){
203
					System.err.println("No se ha podido guardar el histograma");
204
				}
205
			grid.free();
206
			this.suspend();
207
		}
208
*/
209
	}
210
	
211
	/**
212
	 * Obtiene el porcentaje que lleva construido el histograma
213
	 * @return porcentaje de construcci?n
214
	 */
215
	public int getPercent() {
216
		return percent;
217
	}
218
}
trunk/extensions/extRasterTools-SE/src/org/gvsig/rastertools/histogram/HistogramProcess.java
1
package org.gvsig.rastertools.histogram;
2

  
3
import org.gvsig.gui.beans.incrementabletask.IIncrementable;
4
import org.gvsig.raster.util.HistogramException;
5
import org.gvsig.raster.util.IHistogramable;
6

  
7
public class HistogramProcess extends Thread implements IIncrementable {
8
	IHistogramable iHistogramable = null;
9
	
10
	public HistogramProcess(IHistogramable iHistogramable) {
11
		this.iHistogramable = iHistogramable;
12
		this.start();
13
	}
14
	
15
	public void run() {
16
		try {
17
			iHistogramable.getHistogram();
18
		} catch (HistogramException e) {
19
			// TODO Auto-generated catch block
20
			e.printStackTrace();
21
		}
22
	}
23
	/*
24
	 * (non-Javadoc)
25
	 * @see org.gvsig.gui.beans.IncrementableTask.IIncrementable#getLabel()
26
	 */
27
	public String getLabel() {
28
		return "Generando histograma, por favor, espere...";
29
	}
30

  
31
	/*
32
	 * (non-Javadoc)
33
	 * @see org.gvsig.gui.beans.IncrementableTask.IIncrementable#getLog()
34
	 */
35
	public String getLog() {
36
		return "Calculando histograma...";
37
	}
38

  
39
	/*
40
	 * (non-Javadoc)
41
	 * @see org.gvsig.gui.beans.IncrementableTask.IIncrementable#getPercent()
42
	 */
43
	public int getPercent() {
44
		return iHistogramable.getPercent();
45
	}
46

  
47
	/*
48
	 * (non-Javadoc)
49
	 * @see org.gvsig.gui.beans.IncrementableTask.IIncrementable#getTitle()
50
	 */
51
	public String getTitle() {
52
		return "Calculando histograma";
53
	}
54
}
0 55

  
trunk/extensions/extRasterTools-SE/src/org/gvsig/rastertools/histogram/ui/HistogramIncrement.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 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.histogram.ui;
20

  
21
import org.cresques.i18n.Messages;
22
import org.gvsig.gui.beans.progresspanel.ProgressPanel;
23
import org.gvsig.rastertools.histogram.Histogram;
24
/**
25
 * <code>HistogramIncrement</code>. Ventana de incremento para la construcci?n
26
 * del histograma
27
 *
28
 * @version 20/03/2007
29
 * @author Nacho Brodin (brodin_ign@gva.es)
30
 * @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
31
 */
32
public class HistogramIncrement extends Thread {
33
	private HistogramPanelListener histogramPanelListener = null;
34

  
35
	private ProgressPanel window = new ProgressPanel();
36

  
37
	/**
38
	 * Constructor del <code>HistogramIncrement</code>.
39
	 * @param hpl Se le pasa el objecto de {@link HistogramPanelListener}
40
	 */
41
	public HistogramIncrement(HistogramPanelListener hpl){
42
		this.setHistogramPanelListener(hpl);
43
	}
44
	
45
	/**
46
	 * Muestra la ventana de incremento con el porcentaje de la construcci?n del
47
	 * histograma.
48
	 */
49
	public void showWindow(){
50
		window.setTitle("Actualizando datos");
51
		window.showLog(false);
52
		window.show();
53
		
54
		if(this.isAlive())
55
			this.resume();
56
		else
57
			this.start();
58
	}
59
	
60
	/**
61
	 * Este thread va leyendo el porcentaje hasta que se completa el histograma.
62
	 */
63
	public synchronized void run(){
64
		while(true){
65
			Histogram hist = this.getHistogramPanelListener().getHistogram();
66
			window.addLineLog("Calculando histograma.");
67
			while (hist.getPercent() < 100){
68
				window.setLabel(Messages.getText("calculando ...") +  hist.getPercent() +"%");
69
				window.setPercent(hist.getPercent());
70
				try {
71
					sleep(100);
72
				} catch (InterruptedException e) {
73
					e.printStackTrace();
74
				}
75
			}
76
			//Cerramos la ventana
77
			window.hide();
78
			window = null;
79
			
80
			//Mostramos el resultado
81
			this.getHistogramPanelListener().readFullHistogramFromThread();
82
			
83
			this.suspend();
84
		}
85
	}
86

  
87
	/**
88
	 * M?todo para obtener el {@link HistogramPanelListener}
89
	 * @return <code>HistogramPanelListener</code>
90
	 */
91
	public HistogramPanelListener getHistogramPanelListener() {
92
		return histogramPanelListener;
93
	}
94

  
95
	/**
96
	 * Definir el {@link HistogramPanelListener}
97
	 * @param hpl {@link HistogramPanelListener}
98
	 */
99
	public void setHistogramPanelListener(HistogramPanelListener hpl) {
100
		histogramPanelListener = hpl;
101
	}
102
} 
trunk/extensions/extRasterTools-SE/src/org/gvsig/rastertools/histogram/ui/HistogramPanelListener.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2005 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.histogram.ui;
20

  
21
import java.awt.event.ActionEvent;
22
import java.awt.event.ActionListener;
23
import java.awt.event.KeyEvent;
24
import java.awt.event.KeyListener;
25

  
26
import javax.swing.JButton;
27
import javax.swing.JComboBox;
28

  
29
import org.gvsig.rastertools.histogram.Histogram;
30
/**
31
 * Listener para eventos del panel de histograma
32
 *
33
 * @version 20/03/2007
34
 * @author Nacho Brodin (brodin_ign@gva.es)
35
 * @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
36
 */
37
public class HistogramPanelListener implements ActionListener, KeyListener {
38

  
39
	private HistogramPanel			panel = null;
40
	/**
41
	 * Objeto histograma para la gesti?n de procesos de histograma
42
	 */
43
	private	Histogram				histogram = null;
44
	/**
45
	 * ?ltimo histograma visualizado en el gr?fico
46
	 */
47
	private int[][]						lastHistogram = null;
48

  
49
	public static boolean 			comboEventEnable = true;
50
		
51
	public HistogramPanelListener(HistogramPanel p){
52
		panel = p;
53
	}
54
	
55
	public void setControlListeners(){
56
		panel.getGraphicContainer().getPlusButtonControlLeft().addActionListener(this);
57
		panel.getGraphicContainer().getPlusButtonControlRight().addActionListener(this);
58
		panel.getGraphicContainer().getLessButtonControlLeft().addActionListener(this);
59
		panel.getGraphicContainer().getLessButtonControlRight().addActionListener(this);
60
		panel.getGraphicContainer().getTextControlRight().addKeyListener(this);
61
		panel.getGraphicContainer().getTextControlLeft().addKeyListener(this);
62
	}
63

  
64
	
65
	public void keyPressed(KeyEvent e) {
66
		if (e.getKeyCode() == 10) {
67
			updateStatistic();
68
		}
69
	}
70

  
71
	private void updateStatistic() {
72
		// TODO Actualizar tabla de los histogramas.
73
		System.out.println("Botones + o - apretados, actualizar tabla");
74
//		panel.setStatistic(GridStatistic.getBasicStatsFromHistogram(getLastHistogram(), (int)panel.getBoxesValues()[1], (int)panel.getBoxesValues()[0], panel.showBands));
75
	}
76

  
77
	public void actionPerformed(ActionEvent e) {
78
		//Botones de m?s y menos
79
		if (e.getSource() == panel.getGraphicContainer().getPlusButtonControlLeft()  ||
80
				e.getSource() == panel.getGraphicContainer().getLessButtonControlLeft()  ||
81
				e.getSource() == panel.getGraphicContainer().getPlusButtonControlRight() ||
82
				e.getSource() == panel.getGraphicContainer().getLessButtonControlRight() ) {
83
			updateStatistic();
84
			return;
85
		}
86

  
87
		
88
		//--------------------------------------
89
		//Cambiar las bandas en el combo
90
		JComboBox cbb = panel.getJComboBands();
91
		if (comboEventEnable && e.getSource() == cbb){
92
			if(cbb.getSelectedIndex() == 0) {
93
				boolean[] list = panel.getBandListShowInChart();
94
				for (int i = 0; i < list.length; i++)
95
					list[i] = true;
96
				panel.setBandListShowInChart(list);
97
			} else if(cbb.getSelectedItem().equals("R") || cbb.getSelectedItem().equals("Band 0"))
98
				panel.addOrRemoveGraphicBand(0);
99
			else if(cbb.getSelectedItem().equals("G") || cbb.getSelectedItem().equals("Band 1"))
100
				panel.addOrRemoveGraphicBand(1);
101
			else if(cbb.getSelectedItem().equals("B") || cbb.getSelectedItem().equals("Band 2"))
102
				panel.addOrRemoveGraphicBand(2);
103
			else {
104
				for (int i = 3; i < HistogramPanel.MAXBANDS; i++) {
105
					if (cbb.getSelectedItem().equals("Band "+i))
106
						panel.addOrRemoveGraphicBand(i);
107
				}
108
			}
109
		}
110
		
111
		//--------------------------------------		
112
		//Limpiar
113
		JButton clean = panel.getJButtonClear();
114
		if(e.getSource() == clean){
115
			panel.cleanChart();
116
		}
117
		
118
		//--------------------------------------
119
		//Selecci?n de fuente de datos del histograma
120
		JComboBox cbo = panel.getJComboBoxOrigen();
121
		if(comboEventEnable && e.getSource() == cbo){
122
			panel.setHistogramDataSource(cbo.getSelectedIndex());
123
			
124
			//En caso de que el histograma se monte a partir de los datos de la vista ponemos RGB en el combo
125
			if(cbo.getSelectedIndex() == 0)
126
				panel.setRGBInBandList();
127
			
128
			//En caso de que el histograma se monte a partir de los datos reales ponemos el n?mero de bandas en el combo
129
//			if (cbo.getSelectedIndex() == 1 || cbo.getSelectedIndex() == 2)
130
//				panel.setBands(getHistogram().getGrid().getGrid().getBandCount());			
131
		}
132
				
133
		//--------------------------------------
134
		//Selecci?n de histograma acumulado y no acumulado
135
		JComboBox cbt = panel.getJComboBoxTipo();
136
		if (comboEventEnable && e.getSource() == cbt)
137
			panel.setType(cbt.getSelectedIndex());
138
	
139
//		if(comboEventEnable)
140
//			panel.showHistogram();
141

  
142
	}
143

  
144
	/**
145
	 * Obtiene el ?ltimo histograma visualizado
146
	 * @return
147
	 */
148
	public int[][] getLastHistogram() {
149
		return lastHistogram;
150
	}
151

  
152
	/**
153
	 * Asigna el objeto histograma para la gesti?n de procesos de histograma
154
	 * @param histogramObj
155
	 */
156
	public void setHistogram(Histogram histogram) {
157
		this.histogram = histogram;
158
	}
159
	
160
	/**
161
	 * M?todo que ejecuta el thread que comprueba cuando se ha acabado de generar
162
	 * el histograma. Al finalizar, se lee el histograma de la imagen completa 
163
	 * calculado y se muestra.
164
	 */
165
	public void readFullHistogramFromThread(){
166
		if (panel.getType() == 0)
167
			lastHistogram = getHistogram().getFullHistogram();
168
		else if(panel.getType() == 1)
169
			lastHistogram = getHistogram().getFullAccumulatedHistogram();
170

  
171
		this.updateStatistic();
172

  
173
		//Si el histograma es grande lo recortamos para mostrarlo porque jfreeChart se vuelve
174
		//inestable al poner muchos elementos en las X
175
		
176
/*
177

  
178
 		int pos = lastHistogram[0].length;
179
		int[][] newHistogram = lastHistogram;
180
		boolean elem = true;
181
		if(lastHistogram[0].length >= Utilities.MAX_SHORT_BIT_VALUE){
182
			for(int i = (lastHistogram[0].length - 1); i >= 0; i--){
183
				for(int iBand = 0; iBand < lastHistogram.length; iBand ++){
184
					if(lastHistogram[iBand][i] != 0)
185
						elem = false;
186
				}
187
				if(!elem){
188
					pos = i;
189
					break;
190
				}
191
			}
192
			newHistogram = new int[lastHistogram.length][pos];
193
			for(int iBand = 0; iBand < lastHistogram.length; iBand ++){
194
				for(int i = 0; i < newHistogram[iBand].length; i ++){
195
					newHistogram[iBand][i] = lastHistogram[iBand][i];
196
				}
197
			}
198
		}
199
*/
200
/*
201
		GraphicChartPanel gcp = pHistogram.getPGraphic();
202
		
203
		String bandNames[] = new String[lastHistogram.length];
204
		for (int iBand = 0; iBand<lastHistogram.length; iBand++)
205
			bandNames[iBand]=this.getJComboBands().getItemAt(iBand+1).toString();
206
		
207
		gcp.setNewChart(newHistogram, bandNames);
208
*/
209
	}
210

  
211
	/**
212
	 * Obtiene el objeto histograma para la gesti?n de procesos de histograma
213
	 * @return
214
	 */
215
	public Histogram getHistogram() {
216
		return histogram;
217
	}
218

  
219
	public void keyReleased(KeyEvent e) {
220
	}
221

  
222
	public void keyTyped(KeyEvent e) {
223
	}
224
}
trunk/extensions/extRasterTools-SE/src/org/gvsig/rastertools/histogram/ui/HistogramDialog.java
39 39
public class HistogramDialog extends JPanel implements IWindow, ActionListener {
40 40
	private static final long serialVersionUID = 7362459094802955247L;
41 41
	private HistogramPanel 	histogramPanel = null;
42
	
42

  
43 43
	public HistogramDialog(int width, int height){
44 44
		this.setSize(width, height);
45 45
		this.setLayout(new BorderLayout(5, 5));
......
60 60
	}
61 61

  
62 62
	public WindowInfo getWindowInfo() {
63
		WindowInfo m_viewinfo=new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
63
		WindowInfo m_viewinfo=new WindowInfo(WindowInfo.RESIZABLE);
64 64
    	m_viewinfo.setTitle(PluginServices.getText(this, "histograma"));
65 65
     	m_viewinfo.setHeight(this.getHeight());
66 66
     	m_viewinfo.setWidth(this.getWidth());
......
79 79
	}
80 80

  
81 81
	public void actionPerformed(ActionEvent e) {
82
		// TODO: ARQUITECTURA: Este close deberia ir en un listener
82 83
		if (e.getActionCommand().compareTo(ButtonsPanel.BUTTON_CLOSE + "") == 0){
83 84
			close();
84
		}		
85
	}	
85
		}
86
	}
86 87
}
trunk/extensions/extRasterTools-SE/src/org/gvsig/rastertools/histogram/ui/HistogramPanel.java
21 21
import java.awt.BorderLayout;
22 22
import java.awt.GridBagConstraints;
23 23
import java.awt.GridBagLayout;
24
import java.util.Vector;
24 25

  
25 26
import javax.swing.JButton;
26 27
import javax.swing.JComboBox;
......
30 31
import org.cresques.filter.RasterBuf;
31 32
import org.cresques.i18n.Messages;
32 33
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
33
import org.gvsig.gui.beans.dialogPanel.DialogPanel;
34
import org.gvsig.gui.beans.defaultbuttonspanel.DefaultButtonsPanel;
34 35
import org.gvsig.gui.beans.graphic.GraphicChartPanel;
35 36
import org.gvsig.gui.beans.graphic.GraphicContainer;
36 37
import org.gvsig.gui.beans.table.TableContainer;
37 38
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
39
import org.gvsig.raster.util.Histogram;
40
import org.gvsig.raster.util.IHistogramable;
38 41
import org.gvsig.raster.util.RasterUtilities;
42
import org.gvsig.rastertools.histogram.HistogramPanelListener;
39 43
/**
40 44
 * <code>HistogramPanel</code>. Interfaz de usuario para la representaci?n de
41 45
 * histogramas.
......
44 48
 * @author Diego Guerrero (diego.guerrero@uclm.es)
45 49
 * @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
46 50
 */
47
public class HistogramPanel extends DialogPanel {
51
public class HistogramPanel extends DefaultButtonsPanel {
48 52
	private static final long serialVersionUID = 2772897994667886753L;
49 53

  
50 54
	private HistogramPanelListener  	histogramPanelListener = null;
55
	private Vector comboSource = new Vector();
51 56

  
52 57
	public static int					MAXBANDS = 10;  
53 58
	private int 						HSUP = 60;
......
239 244
	 */
240 245
	public JComboBox getJComboBoxTipo() {
241 246
		if (jComboBoxTipo == null) {
242
			String lista [] = {Messages.getText("no_acumulado"),Messages.getText("acumulado")};
247
			String lista [] = new String[Histogram.getHistogramTypesCount()];
248
			for (int i = 0; i < lista.length; i++) {
249
				lista[i] = Messages.getText(Histogram.getType(i));
250
			}
243 251
			jComboBoxTipo = new JComboBox(lista);
244 252
			jComboBoxTipo.addActionListener(getHistogramPanelListener());
245
			jComboBoxTipo.setPreferredSize(new java.awt.Dimension(150,25));
253
			jComboBoxTipo.setPreferredSize(new java.awt.Dimension(150, 25));
246 254
		}
247 255
		return jComboBoxTipo;
248 256
	}
......
254 262
	 */
255 263
	public JComboBox getJComboBoxOrigen() {
256 264
		if (jComboBoxOrigen == null) {
257
			String lista [] = {Messages.getText("datos_visualizados"),Messages.getText("vista_datasource"),Messages.getText("imagen_completa")};
258
			jComboBoxOrigen = new JComboBox(lista);
265
			jComboBoxOrigen = new JComboBox();
259 266
			jComboBoxOrigen.addActionListener(getHistogramPanelListener());
260 267
			jComboBoxOrigen.setPreferredSize(new java.awt.Dimension(150,25));
261 268
		}
......
477 484
		}
478 485
		return histogramPanelListener;
479 486
	}
487
	
488
	public void setHistogramableSource(IHistogramable lyr, String name) {
489
		Vector aux = new Vector();
490
		aux.add(lyr);
491
		aux.add(name);
492
		comboSource.add(aux);
493
		updateComboBoxSource();
494
	}
495

  
496
	public Vector getComboSource() {
497
		return comboSource;
498
	}
499

  
500
	private void updateComboBoxSource() {
501
		getJComboBoxOrigen().removeAllItems();
502
		for (int i = 0; i < comboSource.size(); i++) {
503
			getJComboBoxOrigen().addItem(((Vector) comboSource.get(i)).get(1));
504
		}
505
		
506
	}
507

  
508
	public void clearSources() {
509
		comboSource = new Vector();
510
		updateComboBoxSource();
511
	}
480 512
}
trunk/extensions/extRasterTools-SE/src/org/gvsig/rastertools/histogram/ui/HistogramTocMenuEntry.java
57 57
	}
58 58
	
59 59
	public void execute(ITocItem item, FLayer[] selectedItems) {
60
/*
60

  
61
		
61 62
		if (selectedItems.length == 1 )
62 63
			lyr = selectedItems[0];
63 64
		else
64 65
			return;
65 66

  
66

  
67
//		stackManager = new RasterFilterStackManager(((FLyrRaster)lyr).getSource().getFilterStack());
68 67
		lyr = getNodeLayer(item);
69

  
70
	      
71
		if(lyr instanceof FLyrRaster) {
72
//			stackManager = new RasterFilterStackManager(((FLyrRaster)lyr).getSource().getFilterStack());
73
*/
74
//		histogramDialog = new HistogramDialog(width, height);
68
		
75 69
		histogramDialog = new HistogramDialog(width, height);
76 70
		histogramDialog.setSize(width, height);
77
		
78
			
79
 
80
//			if ( ((FLyrRaster)lyr).getGrid().getDataType() == RasterBuf.TYPE_DOUBLE ||
81
//					((FLyrRaster)lyr).getGrid().getDataType() == RasterBuf.TYPE_FLOAT)
82
//				histogramDialog.setOnlyViewValue();
83
  		
84
//			histogramDialog.setHistogramObj(((FLyrRaster)lyr).getGrid().getHistogram());
85
  		
86
  		//Par?metros de inicializaci?n del histograma
87 71

  
88
			histogramDialog.getHistogramPanel().setHistogramDataSource(0); //Fuente de datos; la vista
89
			histogramDialog.getHistogramPanel().setType(0); //Tipo de histograma; no acumulado
90
			histogramDialog.getHistogramPanel().setRGBInBandList(); //Asignaci?n R,G,B en el combo
72
		histogramDialog.getHistogramPanel().clearSources();
73
		histogramDialog.getHistogramPanel().setHistogramableSource(((FLyrRasterSE)lyr).getSource().getRender().getLastRenderBuffer(), PluginServices.getText(this, "datos_visualizados"));
74
		histogramDialog.getHistogramPanel().setHistogramableSource(((FLyrRasterSE)lyr).getSource().getDatasource().getGeoRasterMultiFile(), PluginServices.getText(this, "vista_datasource"));
91 75

  
92
//			histogramDialog.getHistogramPanel().getHistogramPanelListener().showHistogram(); //Dibujamos histograma
76
		//Par?metros de inicializaci?n del histograma
77

  
78
		histogramDialog.getHistogramPanel().setHistogramDataSource(0); //Fuente de datos; la vista
79
		histogramDialog.getHistogramPanel().setType(0); //Tipo de histograma; no acumulado
80
		histogramDialog.getHistogramPanel().setRGBInBandList(); //Asignaci?n R,G,B en el combo
81

  
82
		histogramDialog.getHistogramPanel().getHistogramPanelListener().showHistogram(); //Dibujamos histograma
93 83
 		       					
94 84
		PluginServices.getMDIManager().addWindow(histogramDialog);
95
//		}
96 85
	}
97 86
}
trunk/extensions/extRasterTools-SE/src/org/gvsig/rastertools/histogram/HistogramPanelListener.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2005 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.histogram;
20

  
21
import java.awt.event.ActionEvent;
22
import java.awt.event.ActionListener;
23
import java.awt.event.KeyEvent;
24
import java.awt.event.KeyListener;
25
import java.util.Vector;
26

  
27
import javax.swing.JButton;
28
import javax.swing.JComboBox;
29

  
30
import org.gvsig.gui.beans.incrementabletask.IncrementableTask;
31
import org.gvsig.raster.dataset.properties.DatasetStatistics;
32
import org.gvsig.raster.util.IHistogramable;
33
import org.gvsig.rastertools.histogram.ui.HistogramPanel;
34
/**
35
 * Listener para eventos del panel de histograma
36
 *
37
 * @version 20/03/2007
38
 * @author Nacho Brodin (brodin_ign@gva.es)
39
 * @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
40
 */
41
public class HistogramPanelListener implements ActionListener, KeyListener {
42

  
43
	private HistogramPanel			panel = null;
44
	private IncrementableTask incrementableTask = null;
45
	/**
46
	 * Objeto histograma para la gesti?n de procesos de histograma
47
	 */
48
//	private	Histogram				histogram = null;
49
	/**
50
	 * ?ltimo histograma visualizado en el gr?fico
51
	 */
52
	private int[][]						lastHistogram = null;
53

  
54
	public static boolean 			comboEventEnable = true;
55
		
56
	public HistogramPanelListener(HistogramPanel p){
57
		panel = p;
58
	}
59
	
60
	public void setControlListeners(){
61
		panel.getGraphicContainer().getPlusButtonControlLeft().addActionListener(this);
62
		panel.getGraphicContainer().getPlusButtonControlRight().addActionListener(this);
63
		panel.getGraphicContainer().getLessButtonControlLeft().addActionListener(this);
64
		panel.getGraphicContainer().getLessButtonControlRight().addActionListener(this);
65
		panel.getGraphicContainer().getTextControlRight().addKeyListener(this);
66
		panel.getGraphicContainer().getTextControlLeft().addKeyListener(this);
67
	}
68

  
69
	
70
	public void keyPressed(KeyEvent e) {
71
		if (e.getKeyCode() == 10) {
72
			updateStatistic();
73
		}
74
	}
75

  
76
	private void updateStatistic() {
77
		// TODO Actualizar tabla de los histogramas.
78
		System.out.println("Botones + o - apretados, actualizar tabla");
79
		panel.setStatistic(DatasetStatistics.getBasicStatsFromHistogram(getLastHistogram(), (int)panel.getBoxesValues()[1], (int)panel.getBoxesValues()[0], panel.showBands));
80
	}
81

  
82
	public void actionPerformed(ActionEvent e) {
83
		//Botones de m?s y menos
84
		if (e.getSource() == panel.getGraphicContainer().getPlusButtonControlLeft()  ||
85
				e.getSource() == panel.getGraphicContainer().getLessButtonControlLeft()  ||
86
				e.getSource() == panel.getGraphicContainer().getPlusButtonControlRight() ||
87
				e.getSource() == panel.getGraphicContainer().getLessButtonControlRight() ) {
88
			updateStatistic();
89
			return;
90
		}
91
		
92
		//--------------------------------------
93
		//Cambiar las bandas en el combo
94
		JComboBox cbb = panel.getJComboBands();
95
		if (comboEventEnable && e.getSource() == cbb) {
96
			if(cbb.getSelectedIndex() == 0) {
97
				boolean[] list = panel.getBandListShowInChart();
98
				for (int i = 0; i < list.length; i++)
99
					list[i] = true;
100
				panel.setBandListShowInChart(list);
101
			} else if(cbb.getSelectedItem().equals("R") || cbb.getSelectedItem().equals("Band 0"))
102
				panel.addOrRemoveGraphicBand(0);
103
			else if(cbb.getSelectedItem().equals("G") || cbb.getSelectedItem().equals("Band 1"))
104
				panel.addOrRemoveGraphicBand(1);
105
			else if(cbb.getSelectedItem().equals("B") || cbb.getSelectedItem().equals("Band 2"))
106
				panel.addOrRemoveGraphicBand(2);
107
			else {
108
				for (int i = 3; i < HistogramPanel.MAXBANDS; i++) {
109
					if (cbb.getSelectedItem().equals("Band "+i))
110
						panel.addOrRemoveGraphicBand(i);
111
				}
112
			}
113
			return;
114
		}
115
		
116
		//--------------------------------------		
117
		//Limpiar
118
		JButton clean = panel.getJButtonClear();
119
		if (e.getSource() == clean) {
120
			panel.cleanChart();
121
			return;
122
		}
123
		
124
		//--------------------------------------
125
		//Selecci?n de fuente de datos del histograma
126
		JComboBox cbo = panel.getJComboBoxOrigen();
127
		if (comboEventEnable && e.getSource() == cbo) {
128
			
129
			System.out.println(cbo.getSelectedIndex() + "");
130
//			showHistogram();
131
//			panel.setHistogramDataSource(cbo.getSelectedIndex());
132
			
133
			//En caso de que el histograma se monte a partir de los datos de la vista ponemos RGB en el combo
134
//			if(cbo.getSelectedIndex() == 0)
135
//				panel.setRGBInBandList();
136
			
137
			//En caso de que el histograma se monte a partir de los datos reales ponemos el n?mero de bandas en el combo
138
//			if (cbo.getSelectedIndex() == 1 || cbo.getSelectedIndex() == 2)
139
//				panel.setBands(getHistogram().getGrid().getGrid().getBandCount());			
140
		}
141
				
142
		//--------------------------------------
143
		//Selecci?n de histograma acumulado y no acumulado
144
		JComboBox cbt = panel.getJComboBoxTipo();
145
		if (comboEventEnable && e.getSource() == cbt) {
146
			System.out.println(cbt.getSelectedIndex() + "");
147
			panel.setType(cbt.getSelectedIndex());
148
		}
149
	
150
//		if(comboEventEnable)
151
//			panel.showHistogram();
152

  
153
	}
154

  
155
	/**
156
	 * Obtiene el ?ltimo histograma visualizado
157
	 * @return
158
	 */
159
	public int[][] getLastHistogram() {
160
		return lastHistogram;
161
	}
162

  
163
	/**
164
	 * Asigna el objeto histograma para la gesti?n de procesos de histograma
165
	 * @param histogramObj
166
	 */
167
/*
168
	public void setHistogram(Histogram histogram) {
169
		this.histogram = histogram;
170
	}
171
*/
172
	/**
173
	 * M?todo que ejecuta el thread que comprueba cuando se ha acabado de generar
174
	 * el histograma. Al finalizar, se lee el histograma de la imagen completa 
175
	 * calculado y se muestra.
176
	 */
177
/*
178
	public void readFullHistogramFromThread(){
179
		if (panel.getType() == 0)
180
			lastHistogram = getHistogram().getFullHistogram();
181
		else if(panel.getType() == 1)
182
			lastHistogram = getHistogram().getFullAccumulatedHistogram();
183

  
184
		this.updateStatistic();
185

  
186
		//Si el histograma es grande lo recortamos para mostrarlo porque jfreeChart se vuelve
187
		//inestable al poner muchos elementos en las X
188
		
189
/*
190

  
191
 		int pos = lastHistogram[0].length;
192
		int[][] newHistogram = lastHistogram;
193
		boolean elem = true;
194
		if(lastHistogram[0].length >= Utilities.MAX_SHORT_BIT_VALUE){
195
			for(int i = (lastHistogram[0].length - 1); i >= 0; i--){
196
				for(int iBand = 0; iBand < lastHistogram.length; iBand ++){
197
					if(lastHistogram[iBand][i] != 0)
198
						elem = false;
199
				}
200
				if(!elem){
201
					pos = i;
202
					break;
203
				}
204
			}
205
			newHistogram = new int[lastHistogram.length][pos];
206
			for(int iBand = 0; iBand < lastHistogram.length; iBand ++){
207
				for(int i = 0; i < newHistogram[iBand].length; i ++){
208
					newHistogram[iBand][i] = lastHistogram[iBand][i];
209
				}
210
			}
211
		}
212
*/
213
/*
214
		GraphicChartPanel gcp = pHistogram.getPGraphic();
215
		
216
		String bandNames[] = new String[lastHistogram.length];
217
		for (int iBand = 0; iBand<lastHistogram.length; iBand++)
218
			bandNames[iBand]=this.getJComboBands().getItemAt(iBand+1).toString();
219
		
220
		gcp.setNewChart(newHistogram, bandNames);
221
*/
222
//	}
223
	
224
	public void showHistogram() {
225
		IHistogramable iaux = (IHistogramable) ((Vector) panel.getComboSource().get(panel.getJComboBoxOrigen().getSelectedIndex())).get(0);
226
		HistogramProcess histogramProcess = new HistogramProcess(iaux);
227
		incrementableTask = new IncrementableTask(histogramProcess);
228
		incrementableTask.showWindow();
229
	}
230
/*
231
	public void showHistogram(){
232
		// Tipo noacumulado = 0, acumulado = 1
233
		switch (panel.getType()) {
234
			case 0:
235
				// Fte de datos vista = 0, extent = 1, completo = 2
236
				switch (panel.getHistogramDataSource()) {
237
					case 0:
238
						requestDataType = RasterBuf.TYPE_BYTE;
239
						calcHistogramFromView(panel.showBands); 
240
						break;
241
					case 1:
242
						if(this.getHistogram().getGrid().getDataType() == RasterBuf.TYPE_SHORT)
243
							requestDataType = RasterBuf.TYPE_SHORT;
244
						else
245
							requestDataType = RasterBuf.TYPE_BYTE;	
246
						calcHistogramFromDataSourceAndExtentView(panel.showBands);
247
						break;
248
					case 2:
249
						if(this.getHistogram().getGrid().getDataType() == RasterBuf.TYPE_SHORT)
250
							requestDataType = RasterBuf.TYPE_SHORT;
251
						else
252
							requestDataType = RasterBuf.TYPE_BYTE;
253
						calcFullHistogramFromDataSource(panel.showBands);	
254
						break;
255
				}
256
				break;
257
			case 1:
258
				// Fte de datos vista = 0, extent = 1, completo = 2
259
				switch (panel.getHistogramDataSource()) {
260
					case 0:
261
						requestDataType = RasterBuf.TYPE_BYTE;
262
						calcAccumulatedHistogramFromView(panel.showBands); 
263
						break;
264
					case 1:
265
						if(this.getHistogram().getGrid().getDataType() == RasterBuf.TYPE_SHORT)
266
							requestDataType = RasterBuf.TYPE_SHORT;
267
						else
268
							requestDataType = RasterBuf.TYPE_BYTE;
269
						calcHistogramFromDataSourceAndExtentView(panel.showBands);
270
						break;
271
					case 2:
272
						if(this.getHistogram().getGrid().getDataType() == RasterBuf.TYPE_SHORT)
273
							requestDataType = RasterBuf.TYPE_SHORT;
274
						else
275
							requestDataType = RasterBuf.TYPE_BYTE;
276
						calcFullAccumulatedHistogramFromDataSource(panel.showBands);
277
						break;
278
				}
279
				break;
280
		}
281
	}
282
*/
283
	/**
284
	 * Obtiene el objeto histograma para la gesti?n de procesos de histograma
285
	 * @return
286
	 */
287
/*
288
	public Histogram getHistogram() {
289
		return histogram;
290
	}
291
*/
292
	
293
	public void keyReleased(KeyEvent e) {
294
	}
295

  
296
	public void keyTyped(KeyEvent e) {
297
	}
298

  
299
	/**
300
	 * Calcula el histograma completo
301
	 * @param bandas que se desean visualizar. Cada elemento del vector es una banda y el boolean, si
302
	 * est? a true se visualizar? esa banda y si est? a false no se visualizar?
303
	 */
304
/*
305
	private void calcFullHistogramFromDataSource(boolean[] bands){
306
		if (histogram != null){
307
			histogram.calcFullHistogram(bands);
308
			incrementPanel = new HistogramIncrement(this);	
309
			incrementPanel.showWindow();
310
		}
311
	}
312
*/
313
	
314
	/**
315
	 * Calcula el histograma de la vista y solo con los datos de visualizaci?n
316
	 * de las bandas R, G o B solicitadas
317
	 * @param bandas que se desean visualizar. Cada elemento del vector es una banda y el boolean, si
318
	 * est? a true se visualizar? esa banda y si est? a false no se visualizar?
319
	 */
320
/*
321
	private void calcHistogramFromView(boolean[] bands){
322
		if(histogram != null){
323
			lastHistogram = histogram.calcHistogramFromView(bands);
324
			panel.showBands = bands;
325
			updateStatistic();
326

  
327
			GraphicChartPanel gcp = panel.getGraphicContainer().getPGraphic();
328
			
329
			String bandNames[] = new String[bands.length];
330
			for (int iBand = 0; iBand<bands.length; iBand++)
331
				bandNames[iBand] = panel.getJComboBands().getItemAt(iBand+1).toString();
332
			
333
			gcp.setNewChart(lastHistogram, bandNames);
334
		}
335
	}
336
*/
337
}
0 338

  

Also available in: Unified diff