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 @ 43835
History | View | Annotate | Download (14.9 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 |
interface RowProcessor { |
209 |
void processRow(Object inputRow, Object outputRow); |
210 |
Number processValue(Number value); |
211 |
}; |
212 |
|
213 |
private abstract class AbstractRowProcessor implements RowProcessor { |
214 |
int band;
|
215 |
double minValue;
|
216 |
double maxValue;
|
217 |
NoData noData; |
218 |
// NoData outputNoData;
|
219 |
|
220 |
public AbstractRowProcessor(int band) { |
221 |
this.band = band;
|
222 |
noData = buffer.getBand(band).getNoData(); |
223 |
// outputNoData = outputBuffer.getBand(band).getNoData();
|
224 |
|
225 |
double[][] tailTrim = statistics.getTailTrimValue(tailTrimPercent); |
226 |
minValue = tailTrim[this.band][0]; |
227 |
maxValue = tailTrim[this.band][1]; |
228 |
} |
229 |
|
230 |
} |
231 |
|
232 |
private class ByteRowProcessor extends AbstractRowProcessor { |
233 |
|
234 |
|
235 |
public ByteRowProcessor(int band) { |
236 |
super(band);
|
237 |
} |
238 |
|
239 |
@Override
|
240 |
public void processRow(Object inputRow, Object outputRow) { |
241 |
byte[] inputByteRow = (byte[])inputRow; |
242 |
byte[] outputByteRow = (byte[])outputRow; |
243 |
for (int i = 0; i < inputByteRow.length; i++) { |
244 |
outputByteRow[i] = processValue(inputByteRow[i]).byteValue(); |
245 |
} |
246 |
} |
247 |
|
248 |
@Override
|
249 |
public Number processValue(Number value) { |
250 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
251 |
return outputBuffer.getBand(band).getNoData().getValue();
|
252 |
} |
253 |
|
254 |
int iValue = 0xFF & ((Byte) value).byteValue(); |
255 |
|
256 |
if(iValue < minValue || iValue > maxValue){
|
257 |
return outputBuffer.getBand(band).getNoData().getValue();
|
258 |
} else {
|
259 |
return value;
|
260 |
} |
261 |
} |
262 |
|
263 |
} |
264 |
|
265 |
private class ShortRowProcessor extends AbstractRowProcessor { |
266 |
|
267 |
public ShortRowProcessor(int band) { |
268 |
super(band);
|
269 |
} |
270 |
|
271 |
@Override
|
272 |
public void processRow(Object inputRow, Object outputRow) { |
273 |
short[] inputShortRow = (short[])inputRow; |
274 |
short[] outputShortRow = (short[])outputRow; |
275 |
for (int i = 0; i < inputShortRow.length; i++) { |
276 |
outputShortRow[i] = processValue(inputShortRow[i]).shortValue(); |
277 |
} |
278 |
} |
279 |
|
280 |
@Override
|
281 |
public Number processValue(Number value) { |
282 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
283 |
return outputBuffer.getBand(band).getNoData().getValue();
|
284 |
} |
285 |
|
286 |
int iValue = ((Short) value).shortValue(); |
287 |
|
288 |
if(iValue < minValue || iValue > maxValue){
|
289 |
return outputBuffer.getBand(band).getNoData().getValue();
|
290 |
} else {
|
291 |
return value;
|
292 |
} |
293 |
} |
294 |
|
295 |
} |
296 |
|
297 |
private class UShortRowProcessor extends AbstractRowProcessor { |
298 |
|
299 |
public UShortRowProcessor(int band) { |
300 |
super(band);
|
301 |
} |
302 |
|
303 |
@Override
|
304 |
public void processRow(Object inputRow, Object outputRow) { |
305 |
short[] inputShortRow = (short[])inputRow; |
306 |
short[] outputShortRow = (short[])outputRow; |
307 |
for (int i = 0; i < inputShortRow.length; i++) { |
308 |
outputShortRow[i] = processValue(inputShortRow[i]).shortValue(); |
309 |
} |
310 |
} |
311 |
|
312 |
@Override
|
313 |
public Number processValue(Number value) { |
314 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
315 |
return outputBuffer.getBand(band).getNoData().getValue();
|
316 |
} |
317 |
|
318 |
int iValue = 0xFFFF & ((Short) value).shortValue(); |
319 |
|
320 |
if(iValue < minValue || iValue > maxValue){
|
321 |
return outputBuffer.getBand(band).getNoData().getValue();
|
322 |
} else {
|
323 |
return value;
|
324 |
} |
325 |
} |
326 |
|
327 |
} |
328 |
|
329 |
private class IntRowProcessor extends AbstractRowProcessor { |
330 |
|
331 |
public IntRowProcessor(int band) { |
332 |
super(band);
|
333 |
} |
334 |
|
335 |
@Override
|
336 |
public void processRow(Object inputRow, Object outputRow) { |
337 |
int[] inputByteRow = (int[])inputRow; |
338 |
int[] outputByteRow = (int[])outputRow; |
339 |
for (int i = 0; i < inputByteRow.length; i++) { |
340 |
outputByteRow[i] = processValue(inputByteRow[i]).intValue(); |
341 |
} |
342 |
} |
343 |
|
344 |
@Override
|
345 |
public Number processValue(Number value) { |
346 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
347 |
return outputBuffer.getBand(band).getNoData().getValue();
|
348 |
} |
349 |
int iValue = value.intValue();
|
350 |
|
351 |
if(iValue < minValue || iValue > maxValue){
|
352 |
return outputBuffer.getBand(band).getNoData().getValue();
|
353 |
} else {
|
354 |
return value;
|
355 |
} |
356 |
} |
357 |
|
358 |
} |
359 |
private class FloatRowProcessor extends AbstractRowProcessor { |
360 |
|
361 |
public FloatRowProcessor(int band) { |
362 |
super(band);
|
363 |
} |
364 |
|
365 |
@Override
|
366 |
public void processRow(Object inputRow, Object outputRow) { |
367 |
float[] inputFloatRow = (float[])inputRow; |
368 |
float[] outputFloatRow = (float[])outputRow; |
369 |
for (int i = 0; i < inputFloatRow.length; i++) { |
370 |
outputFloatRow[i] = processValue(inputFloatRow[i]).floatValue(); |
371 |
} |
372 |
} |
373 |
|
374 |
@Override
|
375 |
public Number processValue(Number value) { |
376 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
377 |
return outputBuffer.getBand(band).getNoData().getValue();
|
378 |
} |
379 |
float fValue = value.floatValue();
|
380 |
|
381 |
if(fValue < minValue || fValue > maxValue){
|
382 |
return outputBuffer.getBand(band).getNoData().getValue();
|
383 |
} else {
|
384 |
return value;
|
385 |
} |
386 |
} |
387 |
|
388 |
} |
389 |
private class DoubleRowProcessor extends AbstractRowProcessor { |
390 |
|
391 |
|
392 |
public DoubleRowProcessor(int band) { |
393 |
super(band);
|
394 |
} |
395 |
|
396 |
@Override
|
397 |
public void processRow(Object inputRow, Object outputRow) { |
398 |
double[] inputByteRow = (double[])inputRow; |
399 |
double[] outputByteRow = (double[])outputRow; |
400 |
for (int i = 0; i < inputByteRow.length; i++) { |
401 |
outputByteRow[i] = processValue(inputByteRow[i]).doubleValue(); |
402 |
} |
403 |
} |
404 |
|
405 |
@Override
|
406 |
public Number processValue(Number value) { |
407 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
408 |
return outputBuffer.getBand(band).getNoData().getValue();
|
409 |
} |
410 |
double fValue = value.doubleValue();
|
411 |
|
412 |
if(fValue < minValue || fValue > maxValue){
|
413 |
return outputBuffer.getBand(band).getNoData().getValue();
|
414 |
} else {
|
415 |
return value;
|
416 |
} |
417 |
} |
418 |
} |
419 |
|
420 |
} |