gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / filter / enhancement / EqualizationFilter.java @ 2443
History | View | Annotate | Download (7.03 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.impl.grid.filter.enhancement; |
23 |
|
24 |
import org.gvsig.fmap.dal.coverage.RasterLibrary; |
25 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
26 |
import org.gvsig.fmap.dal.coverage.datastruct.BufferHistogram; |
27 |
import org.gvsig.fmap.dal.coverage.datastruct.Params; |
28 |
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException; |
29 |
import org.gvsig.fmap.dal.coverage.exception.FilterAddException; |
30 |
import org.gvsig.fmap.dal.coverage.exception.HistogramException; |
31 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
32 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
33 |
import org.gvsig.fmap.dal.coverage.grid.filter.BaseRasterFilter; |
34 |
import org.gvsig.fmap.dal.coverage.store.props.HistogramComputer; |
35 |
import org.gvsig.fmap.dal.coverage.store.props.Statistics; |
36 |
import org.gvsig.raster.impl.buffer.RasterBuffer; |
37 |
import org.gvsig.raster.impl.datastruct.BufferHistogramImpl; |
38 |
import org.gvsig.raster.impl.store.ParamsImpl; |
39 |
/**
|
40 |
* Clase base para los filtros de ecualizaci?n de histograma.
|
41 |
*
|
42 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
43 |
*/
|
44 |
public class EqualizationFilter extends BaseRasterFilter { |
45 |
protected Statistics stats = null; |
46 |
protected double[] minBandValue = null; |
47 |
protected double[] maxBandValue = null; |
48 |
protected int nbands = 3; |
49 |
protected int[] k = null; |
50 |
public static String[] names = new String[] {"equalization"}; |
51 |
protected BufferHistogram histogram = null; |
52 |
protected long nPixels = 0; |
53 |
protected int[][] resValues = null; |
54 |
protected int nElements = 0; |
55 |
protected int[] ecualizedBands = null; |
56 |
protected double[][] aproximationNeg = null; |
57 |
protected int nClasses = RasterLibrary.defaultNumberOfClasses; |
58 |
protected long[][] lahe = null; |
59 |
protected long[][] laheNegative = null; |
60 |
|
61 |
/**
|
62 |
* Construye un LinearEnhancementFilter
|
63 |
*/
|
64 |
public EqualizationFilter() {
|
65 |
setName(names[0]);
|
66 |
} |
67 |
|
68 |
public void pre() throws FilterAddException { |
69 |
super.pre();
|
70 |
stats = (Statistics)getEnv().get("SrcStatistics");
|
71 |
HistogramComputer hist = (HistogramComputer)getEnv().get("SrcHistogram");
|
72 |
try {
|
73 |
if (!stats.isCalculated()) {
|
74 |
try {
|
75 |
stats.calculate(RasterLibrary.statisticsScale); |
76 |
} catch (FileNotOpenException e) {
|
77 |
// No podemos aplicar el filtro
|
78 |
return;
|
79 |
} catch (RasterDriverException e) {
|
80 |
// No podemos aplicar el filtro
|
81 |
return;
|
82 |
} |
83 |
} |
84 |
} catch (ProcessInterruptedException e) {
|
85 |
//Si se ha interrumpido no a?adimos el filtro
|
86 |
} |
87 |
|
88 |
try {
|
89 |
this.histogram = hist.getBufferHistogram();
|
90 |
} catch (ProcessInterruptedException e) {
|
91 |
exec = false;
|
92 |
} catch (HistogramException e) {
|
93 |
exec = false;
|
94 |
} |
95 |
|
96 |
ecualizedBands = (int[]) params.get("ecualizedBands"); |
97 |
if(ecualizedBands == null) |
98 |
ecualizedBands = getColorInterpretation().buildRenderBands(); |
99 |
|
100 |
height = raster.getHeight(); |
101 |
width = raster.getWidth(); |
102 |
nPixels = height * width; |
103 |
|
104 |
try {
|
105 |
stats.calculate(RasterLibrary.statisticsScale); |
106 |
} catch (FileNotOpenException e) {
|
107 |
exec = false;
|
108 |
} catch (RasterDriverException e) {
|
109 |
exec = false;
|
110 |
} catch (ProcessInterruptedException e) {
|
111 |
exec = false;
|
112 |
} |
113 |
|
114 |
if(raster.getDataType() == Buffer.TYPE_BYTE) { |
115 |
minBandValue = stats.getMinByteUnsigned(); |
116 |
maxBandValue = stats.getMaxByteUnsigned(); |
117 |
} |
118 |
if((minBandValue == null || maxBandValue == null) || (minBandValue[0] == 0 && maxBandValue[0] == 0)) { |
119 |
minBandValue = new double[raster.getBandCount()]; |
120 |
maxBandValue = new double[raster.getBandCount()]; |
121 |
if(raster.getDataType() == Buffer.TYPE_BYTE) { |
122 |
for (int i = 0; i < minBandValue.length; i++) { |
123 |
minBandValue[i] = 0;
|
124 |
maxBandValue[i] = 255;
|
125 |
} |
126 |
} else {
|
127 |
minBandValue = stats.getMin(); |
128 |
maxBandValue = stats.getMax(); |
129 |
} |
130 |
} |
131 |
|
132 |
BufferHistogram rgbHistogram = null;
|
133 |
if(raster.getDataType() == Buffer.TYPE_BYTE) |
134 |
rgbHistogram = BufferHistogramImpl.convertHistogramToRGB(histogram); |
135 |
else
|
136 |
rgbHistogram = histogram; |
137 |
double[][] accumNormalize = BufferHistogramImpl.convertTableToNormalizeAccumulate(rgbHistogram.getTable()); |
138 |
double[][] accumNormalizeNeg = BufferHistogramImpl.convertTableToNormalizeAccumulate(rgbHistogram.getNegativeTable()); |
139 |
|
140 |
int value = 255; |
141 |
if(raster.getDataType() != RasterBuffer.TYPE_BYTE)
|
142 |
value = nClasses; |
143 |
|
144 |
lahe = lahe(accumNormalize, value); |
145 |
laheNegative = lahe(accumNormalizeNeg, value); |
146 |
nElements = (laheNegative[0].length - 1); |
147 |
|
148 |
nbands = stats.getBandCount(); |
149 |
createBufferResult(raster.getDataType(), raster.getBandCount()); |
150 |
} |
151 |
|
152 |
/**
|
153 |
* M?todo lahe para la ecualizaci?n. Cada posici?n del array resultante tendr? el valor de salida para
|
154 |
* un valor de entrada dado.
|
155 |
* @param accumNorm Histograma acumulado
|
156 |
* @param value Valor m?ximo
|
157 |
* @return
|
158 |
*/
|
159 |
private long[][] lahe(double[][] accumNorm, int value) { |
160 |
long[][] res = new long[accumNorm.length][accumNorm[0].length]; |
161 |
for (int i = 0; i < res.length; i++) |
162 |
for (int j = 0; j < res[i].length; j++) |
163 |
res[i][j] = Math.round(accumNorm[i][j] * value);
|
164 |
return res;
|
165 |
} |
166 |
|
167 |
/**
|
168 |
* Consulta si la ecualizaci?n est? activa para una banda o no
|
169 |
* @param band N?mero de banda a consultar si se ha activado la ecualizaci?n
|
170 |
* @return true si est? activa para esa banda y false si no lo est?.
|
171 |
*/
|
172 |
protected boolean equalizationActive(int band) { |
173 |
for (int i = 0; i < ecualizedBands.length; i++) { |
174 |
if(band == ecualizedBands[i])
|
175 |
return true; |
176 |
} |
177 |
return false; |
178 |
} |
179 |
|
180 |
public int getOutRasterDataType() { |
181 |
return Buffer.TYPE_BYTE; |
182 |
} |
183 |
|
184 |
public String getGroup() { |
185 |
return "realces"; |
186 |
} |
187 |
|
188 |
public Params getUIParams(String nameFilter) { |
189 |
return new ParamsImpl(); |
190 |
} |
191 |
|
192 |
public void post() { |
193 |
// En caso de que nadie apunte a raster, se liberar? su memoria.
|
194 |
raster = null;
|
195 |
} |
196 |
|
197 |
public int getInRasterDataType() { |
198 |
return 0; |
199 |
} |
200 |
|
201 |
public void process(int x, int y) { |
202 |
} |
203 |
|
204 |
public String[] getNames() { |
205 |
return names;
|
206 |
} |
207 |
|
208 |
public boolean isVisible() { |
209 |
return true; |
210 |
} |
211 |
} |