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 / tailtrim / TailTrimOperation.java @ 43803
History | View | Annotate | Download (15.1 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.tailtrim; |
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.spi.exceptions.ProcessingOperationException; |
36 |
import org.gvsig.raster.lib.buffer.spi.operations.AbstractOperation; |
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 TailTrimOperation extends AbstractSpecifiedBandsOperation{ |
46 |
|
47 |
static public String STATISTICS_PARAM = "statistics"; |
48 |
static public String TAIL_TRIM_PERCENT_PARAM = "tail_trim_percent"; |
49 |
|
50 |
private Statistics statistics;
|
51 |
private double tailTrimPercent; |
52 |
private RowProcessor[] rowProcessors; |
53 |
|
54 |
/**
|
55 |
* @param factory
|
56 |
*
|
57 |
*/
|
58 |
public TailTrimOperation(OperationFactory factory) {
|
59 |
this.factory = factory;
|
60 |
} |
61 |
|
62 |
@SuppressWarnings("unchecked") |
63 |
@Override
|
64 |
public void preProcess() throws BufferOperationException { |
65 |
super.preProcess();
|
66 |
BufferManager manager = BufferLocator.getBufferManager(); |
67 |
|
68 |
if(this.parameters.getDynClass().getDynField(STATISTICS_PARAM)!=null) { |
69 |
statistics = (Statistics) this.parameters.getDynValue(STATISTICS_PARAM);
|
70 |
} |
71 |
if (statistics == null) { |
72 |
statistics = this.buffer.getStatistics(null); |
73 |
}; |
74 |
if(this.parameters.getDynClass().getDynField(TAIL_TRIM_PERCENT_PARAM)!=null) { |
75 |
tailTrimPercent = (Double)this.parameters.getDynValue(TAIL_TRIM_PERCENT_PARAM); |
76 |
tailTrimPercent = (tailTrimPercent>100)?100:tailTrimPercent; |
77 |
tailTrimPercent = (tailTrimPercent<0)?0:tailTrimPercent; |
78 |
} else {
|
79 |
tailTrimPercent = 0; // FIXME: ?0 o 100? |
80 |
}; |
81 |
|
82 |
//FIXME: Falta la gesti?n del par?metro copyUnprocessedBands, de momento se copian sin tenerlo en cuenta
|
83 |
|
84 |
int bands = this.buffer.getBandCount(); |
85 |
rowProcessors = new RowProcessor[bands];
|
86 |
int [] bandTypes = new int[bands]; |
87 |
for (int i = 0; i < bandTypes.length; i++) { |
88 |
bandTypes[i] = this.buffer.getBandTypes()[i];
|
89 |
} |
90 |
NoData[] noData = this.buffer.getBandNoData(); |
91 |
NoData[] resultNoData = new NoData[noData.length]; |
92 |
for (int band = 0; band < noData.length; band++) { |
93 |
int bandType = this.buffer.getBand(band).getDataType(); |
94 |
switch (bandType) {
|
95 |
case BufferManager.TYPE_BYTE:
|
96 |
rowProcessors[band] = new ByteRowProcessor(band);
|
97 |
if (isProcessableBand(band)) {
|
98 |
if (noData[band].isDefined()) {
|
99 |
resultNoData[band] = noData[band]; |
100 |
} else {
|
101 |
resultNoData[band] = manager.createNoData((byte) 0, (byte) 0); |
102 |
} |
103 |
} |
104 |
break;
|
105 |
case BufferManager.TYPE_USHORT:
|
106 |
rowProcessors[band] = new UShortRowProcessor(band);
|
107 |
if (isProcessableBand(band)) {
|
108 |
if (noData[band].isDefined()) {
|
109 |
resultNoData[band] = noData[band]; |
110 |
} else {
|
111 |
resultNoData[band] = manager.createNoData((int) 0, (int) 0); |
112 |
} |
113 |
} |
114 |
break;
|
115 |
case BufferManager.TYPE_SHORT:
|
116 |
rowProcessors[band] = new ShortRowProcessor(band);
|
117 |
if (isProcessableBand(band)) {
|
118 |
if (noData[band].isDefined()) {
|
119 |
resultNoData[band] = noData[band]; |
120 |
} else {
|
121 |
resultNoData[band] = manager.createNoData(Short.MIN_VALUE, Short.MIN_VALUE); |
122 |
} |
123 |
} |
124 |
break;
|
125 |
case BufferManager.TYPE_INT:
|
126 |
rowProcessors[band] = new IntRowProcessor(band);
|
127 |
if (isProcessableBand(band)) {
|
128 |
if (noData[band].isDefined()) {
|
129 |
resultNoData[band] = noData[band]; |
130 |
} else {
|
131 |
resultNoData[band] = manager.createNoData(Integer.MIN_VALUE, Integer.MIN_VALUE); |
132 |
} |
133 |
} |
134 |
break;
|
135 |
case BufferManager.TYPE_FLOAT:
|
136 |
rowProcessors[band] = new FloatRowProcessor(band);
|
137 |
if (isProcessableBand(band)) {
|
138 |
if (noData[band].isDefined()) {
|
139 |
resultNoData[band] = noData[band]; |
140 |
} else {
|
141 |
resultNoData[band] = manager.createNoData(Float.MIN_VALUE, Float.MIN_VALUE); |
142 |
} |
143 |
} |
144 |
break;
|
145 |
case BufferManager.TYPE_DOUBLE:
|
146 |
rowProcessors[band] = new DoubleRowProcessor(band);
|
147 |
if (isProcessableBand(band)) {
|
148 |
if (noData[band].isDefined()) {
|
149 |
resultNoData[band] = noData[band]; |
150 |
} else {
|
151 |
resultNoData[band] = manager.createNoData(Double.MIN_VALUE, Double.MIN_VALUE); |
152 |
} |
153 |
} |
154 |
break;
|
155 |
default:
|
156 |
throw new IllegalArgumentException("Unknow type of band '"+band+"'"); |
157 |
} |
158 |
|
159 |
} |
160 |
|
161 |
try {
|
162 |
this.outputBuffer = manager.createBuffer(
|
163 |
this.buffer.getRows(),
|
164 |
this.buffer.getColumns(),
|
165 |
bandTypes, |
166 |
resultNoData, |
167 |
this.buffer.getProjection(),
|
168 |
this.buffer.getEnvelope());
|
169 |
} catch (LocatorException | BufferException | CreateEnvelopeException e) {
|
170 |
throw new BufferOperationException(e); |
171 |
} |
172 |
} |
173 |
|
174 |
@Override
|
175 |
public void process() throws ProcessingOperationException { |
176 |
super.process();
|
177 |
for (int band=0; band<this.buffer.getBandCount(); band++){ |
178 |
if (bandsToProcess.contains(band)) {
|
179 |
Band bufferBand = this.buffer.getBand(band);
|
180 |
Band outputBufferBand = this.outputBuffer.getBand(band);
|
181 |
|
182 |
for (int row = 0; row < this.buffer.getRows(); row++) { |
183 |
Object rowBuffer = bufferBand.createRowBuffer();
|
184 |
bufferBand.fetchRow(row, rowBuffer); |
185 |
|
186 |
Object outputRowBuffer = outputBufferBand.createRowBuffer();
|
187 |
outputBufferBand.fetchRow(row, outputRowBuffer); |
188 |
|
189 |
rowProcessors[band].processRow(rowBuffer, outputRowBuffer); |
190 |
|
191 |
outputBufferBand.putRow(row, outputRowBuffer); |
192 |
} |
193 |
} else {
|
194 |
try {
|
195 |
this.outputBuffer.getBand(band).copyFrom(this.buffer.getBand(band)); |
196 |
} catch (BandException e) {
|
197 |
throw new ProcessingOperationException(e); |
198 |
} |
199 |
} |
200 |
} |
201 |
} |
202 |
|
203 |
@Override
|
204 |
public void postProcess() throws BufferOperationException { |
205 |
super.postProcess();
|
206 |
} |
207 |
|
208 |
/**
|
209 |
* @param band
|
210 |
* @return
|
211 |
*/
|
212 |
private boolean isProcessableBand(int band) { |
213 |
return bandsToProcess.contains(band);
|
214 |
} |
215 |
|
216 |
|
217 |
|
218 |
|
219 |
interface RowProcessor { |
220 |
void processRow(Object inputRow, Object outputRow); |
221 |
Number processValue(Number value); |
222 |
}; |
223 |
|
224 |
private abstract class AbstractRowProcessor implements RowProcessor { |
225 |
int band;
|
226 |
double minValue;
|
227 |
double maxValue;
|
228 |
NoData noData; |
229 |
// NoData outputNoData;
|
230 |
|
231 |
public AbstractRowProcessor(int band) { |
232 |
this.band = band;
|
233 |
noData = buffer.getBand(band).getNoData(); |
234 |
// outputNoData = outputBuffer.getBand(band).getNoData();
|
235 |
|
236 |
double[][] tailTrim = statistics.getTailTrimValue(tailTrimPercent); |
237 |
minValue = tailTrim[this.band][0]; |
238 |
maxValue = tailTrim[this.band][1]; |
239 |
} |
240 |
|
241 |
} |
242 |
|
243 |
private class ByteRowProcessor extends AbstractRowProcessor { |
244 |
|
245 |
|
246 |
public ByteRowProcessor(int band) { |
247 |
super(band);
|
248 |
} |
249 |
|
250 |
@Override
|
251 |
public void processRow(Object inputRow, Object outputRow) { |
252 |
byte[] inputByteRow = (byte[])inputRow; |
253 |
byte[] outputByteRow = (byte[])outputRow; |
254 |
for (int i = 0; i < inputByteRow.length; i++) { |
255 |
outputByteRow[i] = processValue(inputByteRow[i]).byteValue(); |
256 |
} |
257 |
} |
258 |
|
259 |
@Override
|
260 |
public Number processValue(Number value) { |
261 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
262 |
return outputBuffer.getBand(band).getNoData().getValue();
|
263 |
} |
264 |
|
265 |
int iValue = 0xFF & ((Byte) value).byteValue(); |
266 |
|
267 |
if(iValue < minValue || iValue > maxValue){
|
268 |
return outputBuffer.getBand(band).getNoData().getValue();
|
269 |
} else {
|
270 |
return value;
|
271 |
} |
272 |
} |
273 |
|
274 |
} |
275 |
|
276 |
private class ShortRowProcessor extends AbstractRowProcessor { |
277 |
|
278 |
public ShortRowProcessor(int band) { |
279 |
super(band);
|
280 |
} |
281 |
|
282 |
@Override
|
283 |
public void processRow(Object inputRow, Object outputRow) { |
284 |
short[] inputShortRow = (short[])inputRow; |
285 |
short[] outputShortRow = (short[])outputRow; |
286 |
for (int i = 0; i < inputShortRow.length; i++) { |
287 |
outputShortRow[i] = processValue(inputShortRow[i]).shortValue(); |
288 |
} |
289 |
} |
290 |
|
291 |
@Override
|
292 |
public Number processValue(Number value) { |
293 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
294 |
return outputBuffer.getBand(band).getNoData().getValue();
|
295 |
} |
296 |
|
297 |
int iValue = ((Short) value).shortValue(); |
298 |
|
299 |
if(iValue < minValue || iValue > maxValue){
|
300 |
return outputBuffer.getBand(band).getNoData().getValue();
|
301 |
} else {
|
302 |
return value;
|
303 |
} |
304 |
} |
305 |
|
306 |
} |
307 |
|
308 |
private class UShortRowProcessor extends AbstractRowProcessor { |
309 |
|
310 |
public UShortRowProcessor(int band) { |
311 |
super(band);
|
312 |
} |
313 |
|
314 |
@Override
|
315 |
public void processRow(Object inputRow, Object outputRow) { |
316 |
short[] inputShortRow = (short[])inputRow; |
317 |
short[] outputShortRow = (short[])outputRow; |
318 |
for (int i = 0; i < inputShortRow.length; i++) { |
319 |
outputShortRow[i] = processValue(inputShortRow[i]).shortValue(); |
320 |
} |
321 |
} |
322 |
|
323 |
@Override
|
324 |
public Number processValue(Number value) { |
325 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
326 |
return outputBuffer.getBand(band).getNoData().getValue();
|
327 |
} |
328 |
|
329 |
int iValue = 0xFFFF & ((Short) value).shortValue(); |
330 |
|
331 |
if(iValue < minValue || iValue > maxValue){
|
332 |
return outputBuffer.getBand(band).getNoData().getValue();
|
333 |
} else {
|
334 |
return value;
|
335 |
} |
336 |
} |
337 |
|
338 |
} |
339 |
|
340 |
private class IntRowProcessor extends AbstractRowProcessor { |
341 |
|
342 |
public IntRowProcessor(int band) { |
343 |
super(band);
|
344 |
} |
345 |
|
346 |
@Override
|
347 |
public void processRow(Object inputRow, Object outputRow) { |
348 |
int[] inputByteRow = (int[])inputRow; |
349 |
int[] outputByteRow = (int[])outputRow; |
350 |
for (int i = 0; i < inputByteRow.length; i++) { |
351 |
outputByteRow[i] = processValue(inputByteRow[i]).intValue(); |
352 |
} |
353 |
} |
354 |
|
355 |
@Override
|
356 |
public Number processValue(Number value) { |
357 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
358 |
return outputBuffer.getBand(band).getNoData().getValue();
|
359 |
} |
360 |
int iValue = value.intValue();
|
361 |
|
362 |
if(iValue < minValue || iValue > maxValue){
|
363 |
return outputBuffer.getBand(band).getNoData().getValue();
|
364 |
} else {
|
365 |
return value;
|
366 |
} |
367 |
} |
368 |
|
369 |
} |
370 |
private class FloatRowProcessor extends AbstractRowProcessor { |
371 |
|
372 |
public FloatRowProcessor(int band) { |
373 |
super(band);
|
374 |
} |
375 |
|
376 |
@Override
|
377 |
public void processRow(Object inputRow, Object outputRow) { |
378 |
float[] inputFloatRow = (float[])inputRow; |
379 |
float[] outputFloatRow = (float[])outputRow; |
380 |
for (int i = 0; i < inputFloatRow.length; i++) { |
381 |
outputFloatRow[i] = processValue(inputFloatRow[i]).floatValue(); |
382 |
} |
383 |
} |
384 |
|
385 |
@Override
|
386 |
public Number processValue(Number value) { |
387 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
388 |
return outputBuffer.getBand(band).getNoData().getValue();
|
389 |
} |
390 |
float fValue = value.floatValue();
|
391 |
|
392 |
if(fValue < minValue || fValue > maxValue){
|
393 |
return outputBuffer.getBand(band).getNoData().getValue();
|
394 |
} else {
|
395 |
return value;
|
396 |
} |
397 |
} |
398 |
|
399 |
} |
400 |
private class DoubleRowProcessor extends AbstractRowProcessor { |
401 |
|
402 |
|
403 |
public DoubleRowProcessor(int band) { |
404 |
super(band);
|
405 |
} |
406 |
|
407 |
@Override
|
408 |
public void processRow(Object inputRow, Object outputRow) { |
409 |
double[] inputByteRow = (double[])inputRow; |
410 |
double[] outputByteRow = (double[])outputRow; |
411 |
for (int i = 0; i < inputByteRow.length; i++) { |
412 |
outputByteRow[i] = processValue(inputByteRow[i]).doubleValue(); |
413 |
} |
414 |
} |
415 |
|
416 |
@Override
|
417 |
public Number processValue(Number value) { |
418 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
419 |
return outputBuffer.getBand(band).getNoData().getValue();
|
420 |
} |
421 |
double fValue = value.doubleValue();
|
422 |
|
423 |
if(fValue < minValue || fValue > maxValue){
|
424 |
return outputBuffer.getBand(band).getNoData().getValue();
|
425 |
} else {
|
426 |
return value;
|
427 |
} |
428 |
} |
429 |
} |
430 |
|
431 |
} |