Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer / org.gvsig.raster.lib.buffer.impl / src / main / java / org / gvsig / raster / lib / buffer / impl / operations / linearstretchenhancement / LinearStretchEnhancementOperation.java @ 8682

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
            // TODO Auto-generated catch block
151
            e.printStackTrace();
152
        }
153
    }
154

    
155
    @Override
156
    public void process() throws ProcessingOperationException {
157
        super.process();
158
        for (int band=0; band<this.buffer.getBandCount(); band++){
159
            if (bandsToProcess.contains(band)) {
160
                Band bufferBand = this.buffer.getBand(band);
161
                Band outputBufferBand = this.outputBuffer.getBand(band);
162

    
163
                for (int row = 0; row < this.buffer.getRows(); row++) {
164
                    Object rowBuffer = bufferBand.createRowBuffer();
165
                    bufferBand.fetchRow(row, rowBuffer);
166

    
167
                    Object outputRowBuffer = outputBufferBand.createRowBuffer();
168
                    outputBufferBand.fetchRow(row, outputRowBuffer);
169

    
170
                    rowProcessors[band].processRow(rowBuffer, outputRowBuffer);
171

    
172
                    outputBufferBand.putRow(row, outputRowBuffer);
173
                }
174
            } else {
175
                try {
176
                    this.outputBuffer.getBand(band).copyFrom(this.buffer.getBand(band));
177
                } catch (BandException e) {
178
                    throw new ProcessingOperationException(e);
179
                }
180
            }
181
        }
182
    }
183

    
184
    @Override
185
    public void postProcess()  throws BufferOperationException {
186
        super.postProcess();
187
    }
188

    
189

    
190
    interface RowProcessor {
191
        void processRow(Object inputRow, Object outputRow);
192
        byte processValue(Object value);
193
    };
194

    
195
    private abstract class AbstractRowProcessor implements RowProcessor {
196
        int band;
197
        double minValue;
198
        double maxValue;
199
        double maxResult = 255;
200
        double minResult = 0;
201
        NoData noData;
202

    
203
        public AbstractRowProcessor(int band) {
204
            this.band = band;
205
            noData = buffer.getBand(band).getNoData();
206
            if(noData.isDefined()) {
207
                minResult = (byte)1;
208
            }
209
            minValue = statistics.getMin()[band];
210
            maxValue = statistics.getMax()[band];
211

    
212
            if(removeEnds) {
213
                minValue = statistics.getSecondMin()[band];
214
                maxValue = statistics.getSecondMax()[band];
215
            }
216

    
217
            if(tailTrim) {
218
                double[][] tailTrim = statistics.getTailTrimValue(tailTrimPercent);
219
                minValue = tailTrim[band][0];
220
                maxValue = tailTrim[band][1];
221
            }
222
        }
223

    
224
    }
225

    
226
    private class ByteRowProcessor extends AbstractRowProcessor {
227

    
228

    
229
        public ByteRowProcessor(int band) {
230
            super(band);
231
        }
232

    
233
        @Override
234
        public void processRow(Object inputRow, Object outputRow) {
235
            byte[] inputByteRow = (byte[])inputRow;
236
            byte[] outputByteRow = (byte[])outputRow;
237
            for (int i = 0; i < inputByteRow.length; i++) {
238
                outputByteRow[i] = processValue(inputByteRow[i]);
239
            }
240
        }
241

    
242
        @Override
243
        public byte processValue(Object value) {
244
            if(noData.isDefined() && noData.getValue().equals(value)){
245
                return (byte)0;
246
            }
247

    
248
            int iValue = 0xFF & ((Byte) value).byteValue();
249
            Double dValue = new Double(iValue);
250

    
251
            double result;
252
            if(dValue < minValue){
253
                result = minResult;
254
            } else if (dValue > maxValue){
255
                result = maxResult;
256
            } else {
257
                double ratio = (maxResult - minResult) / (maxValue - minValue);
258
                result = (dValue-minValue) * ratio + minResult;
259
            }
260

    
261
            return (byte)result;
262

    
263
        }
264

    
265
    }
266

    
267
    private class ShortRowProcessor extends AbstractRowProcessor {
268

    
269
        public ShortRowProcessor(int band) {
270
            super(band);
271
        }
272

    
273
        @Override
274
        public void processRow(Object inputRow, Object outputRow) {
275
            short[] inputByteRow = (short[])inputRow;
276
            byte[] outputByteRow = (byte[])outputRow;
277
            for (int i = 0; i < inputByteRow.length; i++) {
278
                outputByteRow[i] = processValue(inputByteRow[i]);
279
            }
280
        }
281

    
282
        @Override
283
        public byte processValue(Object value) {
284
            if(noData.isDefined() && noData.getValue().equals(value)){
285
                return (byte)0;
286
            }
287

    
288
            int iValue = ((Short) value).shortValue();
289
            Double dValue = ((Number) iValue).doubleValue();
290

    
291
            double result;
292
            if(dValue < minValue){
293
                result = minResult;
294
            } else if (dValue > maxValue){
295
                result = maxResult;
296
            } else {
297
                double ratio = (maxResult - minResult) / (maxValue - minValue);
298
                result = (dValue-minValue) * ratio + minResult;
299
            }
300

    
301
            return (byte)result;
302
        }
303

    
304
    }
305

    
306
    private class UShortRowProcessor extends AbstractRowProcessor {
307

    
308
        public UShortRowProcessor(int band) {
309
            super(band);
310
        }
311

    
312
        @Override
313
        public void processRow(Object inputRow, Object outputRow) {
314
            short[] inputByteRow = (short[])inputRow;
315
            byte[] outputByteRow = (byte[])outputRow;
316
            for (int i = 0; i < inputByteRow.length; i++) {
317
                outputByteRow[i] = processValue(inputByteRow[i]);
318
            }
319
        }
320

    
321
        @Override
322
        public byte processValue(Object value) {
323
            if(noData.isDefined() && noData.getValue().equals(value)){
324
                return (byte)0;
325
            }
326

    
327
            //FIXME ???:
328
            int iValue = 0xFFFF & ((Short) value).shortValue();
329
            Double dValue = ((Number) iValue).doubleValue();
330

    
331
            double result;
332
            if(dValue < minValue){
333
                result = minResult;
334
            } else if (dValue > maxValue){
335
                result = maxResult;
336
            } else {
337
                double ratio = (maxResult - minResult) / (maxValue - minValue);
338
                result = (dValue-minValue) * ratio + minResult;
339
            }
340

    
341
            return (byte)result;
342
        }
343

    
344
    }
345

    
346
    private class IntRowProcessor extends AbstractRowProcessor {
347

    
348
        public IntRowProcessor(int band) {
349
            super(band);
350
        }
351

    
352
        @Override
353
        public void processRow(Object inputRow, Object outputRow) {
354
            int[] inputByteRow = (int[])inputRow;
355
            byte[] outputByteRow = (byte[])outputRow;
356
            for (int i = 0; i < inputByteRow.length; i++) {
357
                outputByteRow[i] = processValue(inputByteRow[i]);
358
            }
359
        }
360

    
361
        @Override
362
        public byte processValue(Object value) {
363
            if(noData.isDefined() && noData.getValue().equals(value)){
364
                return (byte)0;
365
            }
366

    
367
            Double dValue = ((Number) value).doubleValue();
368

    
369
            double result;
370
            if(dValue < minValue){
371
                result = minResult;
372
            } else if (dValue > maxValue){
373
                result = maxResult;
374
            } else {
375
                double ratio = (maxResult - minResult) / (maxValue - minValue);
376
                result = (dValue-minValue) * ratio + minResult;
377
            }
378

    
379
            return (byte)result;
380
        }
381

    
382
    }
383
    private class FloatRowProcessor extends AbstractRowProcessor {
384

    
385
        public FloatRowProcessor(int band) {
386
            super(band);
387
        }
388

    
389
        @Override
390
        public void processRow(Object inputRow, Object outputRow) {
391
            float[] inputByteRow = (float[])inputRow;
392
            byte[] outputByteRow = (byte[])outputRow;
393
            for (int i = 0; i < inputByteRow.length; i++) {
394
                outputByteRow[i] = processValue(inputByteRow[i]);
395
            }
396
        }
397

    
398
        @Override
399
        public byte processValue(Object value) {
400
            if(noData.isDefined() && noData.getValue().equals(value)){
401
                return (byte)0;
402
            }
403

    
404
            Double dValue = ((Number) value).doubleValue();
405

    
406
            double result;
407
            if(dValue < minValue){
408
                result = minResult;
409
            } else if (dValue > maxValue){
410
                result = maxResult;
411
            } else {
412
                double ratio = (maxResult - minResult) / (maxValue - minValue);
413
                result = (dValue-minValue) * ratio + minResult;
414
            }
415

    
416
            return (byte)result;
417
        }
418

    
419
    }
420
    private class DoubleRowProcessor extends AbstractRowProcessor {
421

    
422

    
423
        public DoubleRowProcessor(int band) {
424
            super(band);
425
        }
426

    
427
        @Override
428
        public void processRow(Object inputRow, Object outputRow) {
429
            double[] inputByteRow = (double[])inputRow;
430
            byte[] outputByteRow = (byte[])outputRow;
431
            for (int i = 0; i < inputByteRow.length; i++) {
432
                outputByteRow[i] = processValue(inputByteRow[i]);
433
            }
434
        }
435

    
436
        @Override
437
        public byte processValue(Object value) {
438
            if(noData.isDefined() && noData.getValue().equals(value)){
439
                return (byte)0;
440
            }
441

    
442
            Double dValue = ((Number) value).doubleValue();
443

    
444
            double result;
445
            if(dValue < minValue){
446
                result = minResult;
447
            } else if (dValue > maxValue){
448
                result = maxResult;
449
            } else {
450
                double ratio = (maxResult - minResult) / (maxValue - minValue);
451
                result = (dValue-minValue) * ratio + minResult;
452
            }
453

    
454
            return (byte)result;
455
        }
456

    
457
    }
458

    
459
}