root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / histogram / Histogram.java @ 10799
History | View | Annotate | Download (6.43 KB)
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 |
} |