Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.impl / src / main / java / org / gvsig / raster / lib / legend / impl / operations / pansharpening / PansharpeningOperation.java @ 43803

History | View | Annotate | Download (6.41 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.legend.impl.operations.pansharpening;
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.legend.api.colorinterpretation.ColorInterpretation;
38
import org.gvsig.raster.lib.legend.spi.AbstractColoredOperation;
39
import org.gvsig.tools.locator.LocatorException;
40

    
41

    
42
/**
43
 * @author fdiaz
44
 *
45
 * @deprecated
46
 * Possibly this class will be deleted
47
 */
48
@Deprecated
49
public class PansharpeningOperation extends AbstractColoredOperation{
50

    
51
    static public String STATISTICS_PARAM = "statistics";
52

    
53
    private Statistics statistics;
54
    private RowProcessor rowProcessor;
55

    
56
    /**
57
     * @param factory
58
     *
59
     */
60
    public PansharpeningOperation(OperationFactory factory) {
61
        this.factory = factory;
62
    }
63

    
64
    @Override
65
    public void preProcess() throws BufferOperationException {
66
        super.preProcess();
67
        BufferManager manager = BufferLocator.getBufferManager();
68

    
69
        statistics = (Statistics)this.parameters.getDynValue(STATISTICS_PARAM);
70

    
71
        int bands = this.buffer.getBandCount();
72
        NoData[] noData = this.buffer.getBandNoData();
73

    
74
        try {
75
            this.outputBuffer = manager.createBuffer(
76
                this.buffer.getRows(),
77
                this.buffer.getColumns(),
78
                this.buffer.getBandTypes(),
79
                this.buffer.getBandNoData(),
80
                this.buffer.getProjection(),
81
                this.buffer.getEnvelope());
82
        } catch (LocatorException | BufferException | CreateEnvelopeException e) {
83
            throw new ProcessingOperationException(e);
84
        }
85
    }
86

    
87
    @Override
88
    public void process() throws ProcessingOperationException {
89
        super.process();
90
        for (int band=0; band<this.buffer.getBandCount(); band++){
91
            rowProcessor = new ByteRowProcessor(band);
92
            if (isRGBorGrayBand(band)) {
93
                Band bufferBand = this.buffer.getBand(band);
94
                Band outputBufferBand = this.outputBuffer.getBand(band);
95

    
96
                for (int row = 0; row < this.buffer.getRows(); row++) {
97
                    Object rowBuffer = bufferBand.createRowBuffer();
98
                    bufferBand.fetchRow(row, rowBuffer);
99

    
100
                    Object outputRowBuffer = outputBufferBand.createRowBuffer();
101
                    outputBufferBand.fetchRow(row, outputRowBuffer);
102

    
103
                    rowProcessor.processRow(rowBuffer, outputRowBuffer);
104

    
105
                    outputBufferBand.putRow(row, outputRowBuffer);
106
                }
107
            } else {
108
                try {
109
                    this.outputBuffer.getBand(band).copyFrom(this.buffer.getBand(band));
110
                } catch (BandException e) {
111
                    throw new ProcessingOperationException(e);
112
                }
113
            }
114
        }
115
    }
116

    
117
    private boolean isRGBorGrayBand(int band) {
118
        String bandColorInterpretation = colorInterpretation.get(band);
119
        return (bandColorInterpretation.equals(ColorInterpretation.RED_BAND) ||
120
            bandColorInterpretation.equals(ColorInterpretation.GREEN_BAND) ||
121
            bandColorInterpretation.equals(ColorInterpretation.BLUE_BAND) ||
122
            bandColorInterpretation.equals(ColorInterpretation.GRAY_BAND));
123
    }
124

    
125
    @Override
126
    public void postProcess() throws BufferOperationException {
127
        super.postProcess();
128
    }
129

    
130

    
131

    
132
    interface RowProcessor {
133
        void processRow(Object inputRow, Object outputRow);
134
        byte processValue(Object value);
135
    };
136

    
137
    private abstract class AbstractRowProcessor implements RowProcessor {
138
//        int band;
139
        int maxResult = 255;
140
        int minResult = 0;
141
        NoData noData;
142

    
143
        public AbstractRowProcessor(int band) {
144
//            this.band = band;
145
            noData = buffer.getBand(band).getNoData();
146
            if(noData.isDefined()) {
147
                minResult = (byte)1;
148
            }
149
        }
150
    }
151

    
152
    private class ByteRowProcessor extends AbstractRowProcessor {
153

    
154

    
155
        public ByteRowProcessor(int band) {
156
            super(band);
157
        }
158

    
159
        @Override
160
        public void processRow(Object inputRow, Object outputRow) {
161
            byte[] inputByteRow = (byte[])inputRow;
162
            byte[] outputByteRow = (byte[])outputRow;
163
            for (int i = 0; i < inputByteRow.length; i++) {
164
                outputByteRow[i] = processValue(inputByteRow[i]);
165
            }
166
        }
167

    
168
        @Override
169
        public byte processValue(Object value) {
170
            if(noData.isDefined() && noData.getValue().equals(value)){
171
                return (byte)value;
172
            }
173

    
174
            int iValue = 0xFF & ((Byte) value).byteValue();
175
            int result = iValue; // + brightness;
176
            if(result>maxResult){
177
                result = maxResult;
178
            }
179
            if(result<minResult){
180
                result = minResult;
181
            }
182
            return (byte)result;
183
        }
184
    }
185
}