root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / properties / DatasetHistogram.java @ 11072
History | View | Annotate | Download (6.55 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.raster.dataset.properties; |
20 |
|
21 |
import org.gvsig.raster.RasterLibrary; |
22 |
import org.gvsig.raster.dataset.FileNotOpenException; |
23 |
import org.gvsig.raster.dataset.IBuffer; |
24 |
import org.gvsig.raster.dataset.InvalidSetViewException; |
25 |
import org.gvsig.raster.dataset.RasterDataset; |
26 |
import org.gvsig.raster.dataset.RasterDriverException; |
27 |
import org.gvsig.raster.util.DataClassList; |
28 |
import org.gvsig.raster.util.Histogram; |
29 |
|
30 |
/**
|
31 |
* Clase para la gesti?n de histogramas de un raster. Es la encargada del calculo de un histograma
|
32 |
* total o parcial de un raster a partir de los datos de disco. Adem?s tambi?n es la encargada de gestionar
|
33 |
* salvar este histograma a .rmf. En caso de que solicite un histograma del raster completo este ir?
|
34 |
* a buscarlo al fichero rmf asociado antes de calcularlo por si ya existise. Al realizar el calculo
|
35 |
* del histograma de la imagen completa este ser? salvado en el fichero .rmf asociado al raster.
|
36 |
*
|
37 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
38 |
*/
|
39 |
public class DatasetHistogram { |
40 |
/**
|
41 |
* clases en las que se divide el histograma
|
42 |
*/
|
43 |
private DataClassList classes = null; |
44 |
/**
|
45 |
* Histograma de la imagen completa
|
46 |
*/
|
47 |
private Histogram histogram = null; |
48 |
/**
|
49 |
* Dataset del cual se calcula el histograma
|
50 |
*/
|
51 |
private RasterDataset dataset = null; |
52 |
|
53 |
private int percent = 0; |
54 |
private boolean canceled = false; |
55 |
|
56 |
/**
|
57 |
* Constructor
|
58 |
* @param dataset
|
59 |
*/
|
60 |
public DatasetHistogram(RasterDataset dataset){
|
61 |
this.dataset = dataset;
|
62 |
} |
63 |
|
64 |
/**
|
65 |
* Asigna una lista de clases sobre la que se calcular? la petici?n de
|
66 |
* histograma.
|
67 |
* @param classes
|
68 |
*/
|
69 |
public void setClasses(DataClassList classes){ |
70 |
this.classes = classes;
|
71 |
} |
72 |
|
73 |
/**
|
74 |
* Obtiene el histograma. Si puede conseguirlo del fichero rmf ir? all? a
|
75 |
* buscarlo sino lo calcular?.
|
76 |
* @return histograma
|
77 |
*/
|
78 |
public Histogram getHistogram() throws FileNotOpenException, RasterDriverException { |
79 |
try {
|
80 |
if (dataset != null) { |
81 |
if (classes == null) { |
82 |
if (dataset.getDataType() == IBuffer.TYPE_BYTE) {
|
83 |
histogram = new Histogram(dataset.getBandCount(), 256); |
84 |
return getHistogramByValue();
|
85 |
} else {
|
86 |
DatasetStatistics stats = dataset.getStatistics(); |
87 |
stats.calcFullStatistics(); |
88 |
classes = DataClassList.getClasses(stats.getMinimun(), stats.getMaximun(), RasterLibrary.defaultNumberOfClasses); |
89 |
} |
90 |
|
91 |
} |
92 |
histogram = new Histogram(dataset.getBandCount(), classes.length());
|
93 |
return getHistogramByClass();
|
94 |
} |
95 |
} catch (InvalidSetViewException e) {
|
96 |
//La vista se selecciona autom?ticamente no deber?a darse esta excepci?n
|
97 |
} |
98 |
return null; |
99 |
} |
100 |
|
101 |
/**
|
102 |
* Obtiene un histograma para valores de tipo byte. El resto de tipos
|
103 |
* de datos se separan por clases, bien introducidas por el usuario
|
104 |
* bien autocalculadas.
|
105 |
*
|
106 |
* @return histograma
|
107 |
*/
|
108 |
private Histogram getHistogramByValue() throws InvalidSetViewException, FileNotOpenException, RasterDriverException { |
109 |
percent = 0;
|
110 |
if (dataset.getDataType() != IBuffer.TYPE_BYTE) return null; |
111 |
|
112 |
for (int band = 0; band < dataset.getBandCount(); band++) { |
113 |
for (int line = 0; line < dataset.getHeight(); line++) { |
114 |
Object obj = dataset.readCompleteLine(line, band);
|
115 |
byte[] bLine = (byte[])obj; |
116 |
for (int k = 0; k < bLine.length; k++) |
117 |
histogram.incrementPxValue(band, (bLine[k] & 0xff));
|
118 |
if (isCanceled()) return null; |
119 |
percent = ((band * dataset.getHeight() + line) * 100) / (dataset.getBandCount() * dataset.getHeight());
|
120 |
} |
121 |
} |
122 |
percent = 100;
|
123 |
return histogram;
|
124 |
} |
125 |
|
126 |
/**
|
127 |
* Obtiene el histograma teniendo en cuenta la lista de clases
|
128 |
* @return Histograma correspondiente a la lista de clases
|
129 |
*/
|
130 |
private Histogram getHistogramByClass() throws InvalidSetViewException, FileNotOpenException, RasterDriverException { |
131 |
percent = 0;
|
132 |
if (classes == null) return null; |
133 |
for (int band = 0; band < dataset.getBandCount(); band++) { |
134 |
for (int line = 0; line < dataset.getHeight(); line++) { |
135 |
Object obj = dataset.readCompleteLine(line, band);
|
136 |
switch(dataset.getDataType()) {
|
137 |
case IBuffer.TYPE_BYTE:
|
138 |
byte[] bLine = (byte[])obj; |
139 |
for (int k = 0; k < bLine.length; k++) { |
140 |
int pixelValue = classes.getClassPosition((double)bLine[k]); |
141 |
if (pixelValue >= 0) histogram.incrementPxValue(band, pixelValue); |
142 |
} |
143 |
break;
|
144 |
case IBuffer.TYPE_SHORT:
|
145 |
short[] sLine = (short[])obj; |
146 |
for (int k = 0; k < sLine.length; k++) { |
147 |
int pixelValue = classes.getClassPosition((double)sLine[k]); |
148 |
if (pixelValue >= 0) histogram.incrementPxValue(band, pixelValue); |
149 |
} |
150 |
break;
|
151 |
case IBuffer.TYPE_INT:
|
152 |
int[] iLine = (int[])obj; |
153 |
for (int k = 0; k < iLine.length; k++) { |
154 |
int pixelValue = classes.getClassPosition((double)iLine[k]); |
155 |
if (pixelValue >= 0) histogram.incrementPxValue(band, pixelValue); |
156 |
} |
157 |
break;
|
158 |
case IBuffer.TYPE_FLOAT:
|
159 |
float[] fLine = (float[])obj; |
160 |
for (int k = 0; k < fLine.length; k++) { |
161 |
int pixelValue = classes.getClassPosition((double)fLine[k]); |
162 |
if (pixelValue >= 0) histogram.incrementPxValue(band, pixelValue); |
163 |
} |
164 |
break;
|
165 |
case IBuffer.TYPE_DOUBLE:
|
166 |
double[] dLine = (double[])obj; |
167 |
for (int k = 0; k < dLine.length; k++) { |
168 |
int pixelValue = classes.getClassPosition((double)dLine[k]); |
169 |
if (pixelValue >= 0) histogram.incrementPxValue(band, pixelValue); |
170 |
} |
171 |
break;
|
172 |
} |
173 |
if (isCanceled()) return null; |
174 |
percent = ((band * dataset.getHeight() + line) * 100) / (dataset.getBandCount() * dataset.getHeight());
|
175 |
} |
176 |
} |
177 |
percent = 100;
|
178 |
return histogram;
|
179 |
} |
180 |
|
181 |
public void resetPercent() { |
182 |
percent=0;
|
183 |
} |
184 |
|
185 |
public int getPercent() { |
186 |
return percent;
|
187 |
} |
188 |
|
189 |
public void setCanceled(boolean value) { |
190 |
canceled = value; |
191 |
} |
192 |
|
193 |
public boolean isCanceled() { |
194 |
return canceled;
|
195 |
} |
196 |
} |