svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.library / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer.impl / src / main / java / org / gvsig / raster / lib / buffer / impl / operations / linearstretchenhancement / LinearStretchEnhancementOperation.java @ 43803
History | View | Annotate | Download (15.4 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2017 gvSIG Association
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.raster.lib.buffer.impl.operations.linearstretchenhancement; |
24 |
|
25 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
26 |
import org.gvsig.raster.lib.buffer.api.Band; |
27 |
import org.gvsig.raster.lib.buffer.api.BufferLocator; |
28 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
29 |
import org.gvsig.raster.lib.buffer.api.NoData; |
30 |
import org.gvsig.raster.lib.buffer.api.exceptions.BandException; |
31 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
32 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferOperationException; |
33 |
import org.gvsig.raster.lib.buffer.api.operations.OperationFactory; |
34 |
import org.gvsig.raster.lib.buffer.api.statistics.Statistics; |
35 |
import org.gvsig.raster.lib.buffer.impl.DefaultNoData; |
36 |
import org.gvsig.raster.lib.buffer.spi.exceptions.ProcessingOperationException; |
37 |
import org.gvsig.raster.lib.buffer.spi.operations.AbstractSpecifiedBandsOperation; |
38 |
import org.gvsig.tools.locator.LocatorException; |
39 |
|
40 |
|
41 |
/**
|
42 |
* @author fdiaz
|
43 |
*
|
44 |
*/
|
45 |
public class LinearStretchEnhancementOperation extends AbstractSpecifiedBandsOperation{ |
46 |
|
47 |
static public String STATISTICS_PARAM = "statistics"; |
48 |
static public String REMOVE_ENDS_PARAM = "remove_ends"; |
49 |
static public String TAIL_TRIM_PARAM = "tail_trim"; |
50 |
static public String TAIL_TRIM_PERCENT_PARAM = "tail_trim_percent"; |
51 |
|
52 |
private Statistics statistics;
|
53 |
private boolean removeEnds; |
54 |
private boolean tailTrim; |
55 |
private double tailTrimPercent; |
56 |
private RowProcessor[] rowProcessors; |
57 |
|
58 |
/**
|
59 |
* @param factory
|
60 |
*
|
61 |
*/
|
62 |
public LinearStretchEnhancementOperation(OperationFactory factory) {
|
63 |
this.factory = factory;
|
64 |
} |
65 |
|
66 |
@Override
|
67 |
public void preProcess() throws BufferOperationException { |
68 |
super.preProcess();
|
69 |
BufferManager manager = BufferLocator.getBufferManager(); |
70 |
|
71 |
if(this.parameters.getDynClass().getDynField(STATISTICS_PARAM)!=null) { |
72 |
statistics = (Statistics) this.parameters.getDynValue(STATISTICS_PARAM);
|
73 |
} |
74 |
if (statistics == null) { |
75 |
statistics = this.buffer.getStatistics(null); |
76 |
}; |
77 |
if(this.parameters.getDynClass().getDynField(REMOVE_ENDS_PARAM)!=null) { |
78 |
removeEnds = (Boolean)this.parameters.getDynValue(REMOVE_ENDS_PARAM); |
79 |
} else {
|
80 |
removeEnds = false;
|
81 |
}; |
82 |
if(this.parameters.getDynClass().getDynField(TAIL_TRIM_PARAM)!=null) { |
83 |
tailTrim = (Boolean)this.parameters.getDynValue(TAIL_TRIM_PARAM); |
84 |
} else {
|
85 |
tailTrim = false;
|
86 |
}; |
87 |
if(this.parameters.getDynClass().getDynField(TAIL_TRIM_PERCENT_PARAM)!=null) { |
88 |
tailTrimPercent = (Double)this.parameters.getDynValue(TAIL_TRIM_PERCENT_PARAM); |
89 |
tailTrimPercent = (tailTrimPercent>100)?100:tailTrimPercent; |
90 |
tailTrimPercent = (tailTrimPercent<0)?0:tailTrimPercent; |
91 |
} else {
|
92 |
tailTrimPercent = 0; // FIXME: ?0 o 100? |
93 |
}; |
94 |
|
95 |
int bands = this.buffer.getBandCount(); |
96 |
rowProcessors = new RowProcessor[bands];
|
97 |
int [] bandTypes = new int[bands]; |
98 |
//FIXME: Falta la gesti?n del par?metro copyUnprocessedBands, de momento se copian sin tenerlo en cuenta
|
99 |
for (int i = 0; i < bandTypes.length; i++) { |
100 |
if(bandsToProcess.contains(i)){
|
101 |
bandTypes[i] = BufferManager.TYPE_BYTE; |
102 |
} else {
|
103 |
bandTypes[i] = this.buffer.getBandTypes()[i];
|
104 |
} |
105 |
} |
106 |
NoData[] noData = this.buffer.getBandNoData(); |
107 |
NoData[] resultNoData = new NoData[noData.length]; |
108 |
for (int band = 0; band < noData.length; band++) { |
109 |
int bandType = this.buffer.getBand(band).getDataType(); |
110 |
switch (bandType) {
|
111 |
case BufferManager.TYPE_BYTE:
|
112 |
rowProcessors[band] = new ByteRowProcessor(band);
|
113 |
break;
|
114 |
case BufferManager.TYPE_USHORT:
|
115 |
rowProcessors[band] = new UShortRowProcessor(band);
|
116 |
break;
|
117 |
case BufferManager.TYPE_SHORT:
|
118 |
rowProcessors[band] = new ShortRowProcessor(band);
|
119 |
break;
|
120 |
case BufferManager.TYPE_INT:
|
121 |
rowProcessors[band] = new IntRowProcessor(band);
|
122 |
break;
|
123 |
case BufferManager.TYPE_FLOAT:
|
124 |
rowProcessors[band] = new FloatRowProcessor(band);
|
125 |
break;
|
126 |
case BufferManager.TYPE_DOUBLE:
|
127 |
rowProcessors[band] = new DoubleRowProcessor(band);
|
128 |
break;
|
129 |
default:
|
130 |
throw new IllegalArgumentException("Unknow type of band '"+band+"'"); |
131 |
} |
132 |
|
133 |
if(noData[band].isDefined()){
|
134 |
resultNoData[band] = new DefaultNoData((byte)0); |
135 |
resultNoData[band] = manager.createNoData((byte)0,(byte)0); |
136 |
} else {
|
137 |
resultNoData[band]=manager.createNoData(null, null); |
138 |
} |
139 |
} |
140 |
|
141 |
try {
|
142 |
this.outputBuffer = manager.createBuffer(
|
143 |
this.buffer.getRows(),
|
144 |
this.buffer.getColumns(),
|
145 |
bandTypes, |
146 |
resultNoData, |
147 |
this.buffer.getProjection(),
|
148 |
this.buffer.getEnvelope());
|
149 |
} catch (LocatorException | BufferException | CreateEnvelopeException e) {
|
150 |
throw new ProcessingOperationException(e); |
151 |
} |
152 |
} |
153 |
|
154 |
@Override
|
155 |
public void process() throws ProcessingOperationException { |
156 |
super.process();
|
157 |
for (int band=0; band<this.buffer.getBandCount(); band++){ |
158 |
if (bandsToProcess.contains(band)) {
|
159 |
Band bufferBand = this.buffer.getBand(band);
|
160 |
Band outputBufferBand = this.outputBuffer.getBand(band);
|
161 |
|
162 |
for (int row = 0; row < this.buffer.getRows(); row++) { |
163 |
Object rowBuffer = bufferBand.createRowBuffer();
|
164 |
bufferBand.fetchRow(row, rowBuffer); |
165 |
|
166 |
Object outputRowBuffer = outputBufferBand.createRowBuffer();
|
167 |
outputBufferBand.fetchRow(row, outputRowBuffer); |
168 |
|
169 |
rowProcessors[band].processRow(rowBuffer, outputRowBuffer); |
170 |
|
171 |
outputBufferBand.putRow(row, outputRowBuffer); |
172 |
} |
173 |
} else {
|
174 |
try {
|
175 |
this.outputBuffer.getBand(band).copyFrom(this.buffer.getBand(band)); |
176 |
} catch (BandException e) {
|
177 |
throw new ProcessingOperationException(e); |
178 |
} |
179 |
} |
180 |
} |
181 |
} |
182 |
|
183 |
@Override
|
184 |
public void postProcess() throws BufferOperationException { |
185 |
super.postProcess();
|
186 |
} |
187 |
|
188 |
|
189 |
interface RowProcessor { |
190 |
void processRow(Object inputRow, Object outputRow); |
191 |
byte processValue(Object value); |
192 |
}; |
193 |
|
194 |
private abstract class AbstractRowProcessor implements RowProcessor { |
195 |
int band;
|
196 |
double minValue;
|
197 |
double maxValue;
|
198 |
double maxResult = 255; |
199 |
double minResult = 0; |
200 |
NoData noData; |
201 |
|
202 |
public AbstractRowProcessor(int band) { |
203 |
this.band = band;
|
204 |
noData = buffer.getBand(band).getNoData(); |
205 |
if(noData.isDefined()) {
|
206 |
minResult = (byte)1; |
207 |
} |
208 |
minValue = statistics.getMin()[band]; |
209 |
maxValue = statistics.getMax()[band]; |
210 |
|
211 |
if(removeEnds) {
|
212 |
minValue = statistics.getSecondMin()[band]; |
213 |
maxValue = statistics.getSecondMax()[band]; |
214 |
} |
215 |
|
216 |
if(tailTrim) {
|
217 |
double[][] tailTrim = statistics.getTailTrimValue(tailTrimPercent); |
218 |
minValue = tailTrim[band][0];
|
219 |
maxValue = tailTrim[band][1];
|
220 |
} |
221 |
} |
222 |
|
223 |
} |
224 |
|
225 |
private class ByteRowProcessor extends AbstractRowProcessor { |
226 |
|
227 |
|
228 |
public ByteRowProcessor(int band) { |
229 |
super(band);
|
230 |
} |
231 |
|
232 |
@Override
|
233 |
public void processRow(Object inputRow, Object outputRow) { |
234 |
byte[] inputByteRow = (byte[])inputRow; |
235 |
byte[] outputByteRow = (byte[])outputRow; |
236 |
for (int i = 0; i < inputByteRow.length; i++) { |
237 |
outputByteRow[i] = processValue(inputByteRow[i]); |
238 |
} |
239 |
} |
240 |
|
241 |
@Override
|
242 |
public byte processValue(Object value) { |
243 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
244 |
return (byte)0; |
245 |
} |
246 |
|
247 |
int iValue = 0xFF & ((Byte) value).byteValue(); |
248 |
Double dValue = new Double(iValue); |
249 |
|
250 |
double result;
|
251 |
if(dValue < minValue){
|
252 |
result = minResult; |
253 |
} else if (dValue > maxValue){ |
254 |
result = maxResult; |
255 |
} else {
|
256 |
double ratio = (maxResult - minResult) / (maxValue - minValue);
|
257 |
result = (dValue-minValue) * ratio + minResult; |
258 |
} |
259 |
|
260 |
return (byte)result; |
261 |
|
262 |
} |
263 |
|
264 |
} |
265 |
|
266 |
private class ShortRowProcessor extends AbstractRowProcessor { |
267 |
|
268 |
public ShortRowProcessor(int band) { |
269 |
super(band);
|
270 |
} |
271 |
|
272 |
@Override
|
273 |
public void processRow(Object inputRow, Object outputRow) { |
274 |
short[] inputByteRow = (short[])inputRow; |
275 |
byte[] outputByteRow = (byte[])outputRow; |
276 |
for (int i = 0; i < inputByteRow.length; i++) { |
277 |
outputByteRow[i] = processValue(inputByteRow[i]); |
278 |
} |
279 |
} |
280 |
|
281 |
@Override
|
282 |
public byte processValue(Object value) { |
283 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
284 |
return (byte)0; |
285 |
} |
286 |
|
287 |
int iValue = ((Short) value).shortValue(); |
288 |
Double dValue = ((Number) iValue).doubleValue(); |
289 |
|
290 |
double result;
|
291 |
if(dValue < minValue){
|
292 |
result = minResult; |
293 |
} else if (dValue > maxValue){ |
294 |
result = maxResult; |
295 |
} else {
|
296 |
double ratio = (maxResult - minResult) / (maxValue - minValue);
|
297 |
result = (dValue-minValue) * ratio + minResult; |
298 |
} |
299 |
|
300 |
return (byte)result; |
301 |
} |
302 |
|
303 |
} |
304 |
|
305 |
private class UShortRowProcessor extends AbstractRowProcessor { |
306 |
|
307 |
public UShortRowProcessor(int band) { |
308 |
super(band);
|
309 |
} |
310 |
|
311 |
@Override
|
312 |
public void processRow(Object inputRow, Object outputRow) { |
313 |
short[] inputByteRow = (short[])inputRow; |
314 |
byte[] outputByteRow = (byte[])outputRow; |
315 |
for (int i = 0; i < inputByteRow.length; i++) { |
316 |
outputByteRow[i] = processValue(inputByteRow[i]); |
317 |
} |
318 |
} |
319 |
|
320 |
@Override
|
321 |
public byte processValue(Object value) { |
322 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
323 |
return (byte)0; |
324 |
} |
325 |
|
326 |
//FIXME ???:
|
327 |
int iValue = 0xFFFF & ((Short) value).shortValue(); |
328 |
Double dValue = ((Number) iValue).doubleValue(); |
329 |
|
330 |
double result;
|
331 |
if(dValue < minValue){
|
332 |
result = minResult; |
333 |
} else if (dValue > maxValue){ |
334 |
result = maxResult; |
335 |
} else {
|
336 |
double ratio = (maxResult - minResult) / (maxValue - minValue);
|
337 |
result = (dValue-minValue) * ratio + minResult; |
338 |
} |
339 |
|
340 |
return (byte)result; |
341 |
} |
342 |
|
343 |
} |
344 |
|
345 |
private class IntRowProcessor extends AbstractRowProcessor { |
346 |
|
347 |
public IntRowProcessor(int band) { |
348 |
super(band);
|
349 |
} |
350 |
|
351 |
@Override
|
352 |
public void processRow(Object inputRow, Object outputRow) { |
353 |
int[] inputByteRow = (int[])inputRow; |
354 |
byte[] outputByteRow = (byte[])outputRow; |
355 |
for (int i = 0; i < inputByteRow.length; i++) { |
356 |
outputByteRow[i] = processValue(inputByteRow[i]); |
357 |
} |
358 |
} |
359 |
|
360 |
@Override
|
361 |
public byte processValue(Object value) { |
362 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
363 |
return (byte)0; |
364 |
} |
365 |
|
366 |
Double dValue = ((Number) value).doubleValue(); |
367 |
|
368 |
double result;
|
369 |
if(dValue < minValue){
|
370 |
result = minResult; |
371 |
} else if (dValue > maxValue){ |
372 |
result = maxResult; |
373 |
} else {
|
374 |
double ratio = (maxResult - minResult) / (maxValue - minValue);
|
375 |
result = (dValue-minValue) * ratio + minResult; |
376 |
} |
377 |
|
378 |
return (byte)result; |
379 |
} |
380 |
|
381 |
} |
382 |
private class FloatRowProcessor extends AbstractRowProcessor { |
383 |
|
384 |
public FloatRowProcessor(int band) { |
385 |
super(band);
|
386 |
} |
387 |
|
388 |
@Override
|
389 |
public void processRow(Object inputRow, Object outputRow) { |
390 |
float[] inputByteRow = (float[])inputRow; |
391 |
byte[] outputByteRow = (byte[])outputRow; |
392 |
for (int i = 0; i < inputByteRow.length; i++) { |
393 |
outputByteRow[i] = processValue(inputByteRow[i]); |
394 |
} |
395 |
} |
396 |
|
397 |
@Override
|
398 |
public byte processValue(Object value) { |
399 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
400 |
return (byte)0; |
401 |
} |
402 |
|
403 |
Double dValue = ((Number) value).doubleValue(); |
404 |
|
405 |
double result;
|
406 |
if(dValue < minValue){
|
407 |
result = minResult; |
408 |
} else if (dValue > maxValue){ |
409 |
result = maxResult; |
410 |
} else {
|
411 |
double ratio = (maxResult - minResult) / (maxValue - minValue);
|
412 |
result = (dValue-minValue) * ratio + minResult; |
413 |
} |
414 |
|
415 |
return (byte)result; |
416 |
} |
417 |
|
418 |
} |
419 |
private class DoubleRowProcessor extends AbstractRowProcessor { |
420 |
|
421 |
|
422 |
public DoubleRowProcessor(int band) { |
423 |
super(band);
|
424 |
} |
425 |
|
426 |
@Override
|
427 |
public void processRow(Object inputRow, Object outputRow) { |
428 |
double[] inputByteRow = (double[])inputRow; |
429 |
byte[] outputByteRow = (byte[])outputRow; |
430 |
for (int i = 0; i < inputByteRow.length; i++) { |
431 |
outputByteRow[i] = processValue(inputByteRow[i]); |
432 |
} |
433 |
} |
434 |
|
435 |
@Override
|
436 |
public byte processValue(Object value) { |
437 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
438 |
return (byte)0; |
439 |
} |
440 |
|
441 |
Double dValue = ((Number) value).doubleValue(); |
442 |
|
443 |
double result;
|
444 |
if(dValue < minValue){
|
445 |
result = minResult; |
446 |
} else if (dValue > maxValue){ |
447 |
result = maxResult; |
448 |
} else {
|
449 |
double ratio = (maxResult - minResult) / (maxValue - minValue);
|
450 |
result = (dValue-minValue) * ratio + minResult; |
451 |
} |
452 |
|
453 |
return (byte)result; |
454 |
} |
455 |
|
456 |
} |
457 |
|
458 |
} |