Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.impl / src / main / java / org / gvsig / fmap / raster / lib / legend / impl / operations / contrast / ContrastOperation.java @ 44831

History | View | Annotate | Download (9.52 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.contrast;
24

    
25
import java.util.ArrayList;
26
import java.util.List;
27

    
28
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
29
import org.gvsig.raster.lib.buffer.api.Band;
30
import org.gvsig.raster.lib.buffer.api.BufferLocator;
31
import org.gvsig.raster.lib.buffer.api.BufferManager;
32
import org.gvsig.raster.lib.buffer.api.NoData;
33
import org.gvsig.raster.lib.buffer.api.exceptions.BandException;
34
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException;
35
import org.gvsig.raster.lib.buffer.api.exceptions.BufferOperationException;
36
import org.gvsig.raster.lib.buffer.api.operations.OperationFactory;
37
import org.gvsig.raster.lib.buffer.spi.exceptions.ProcessingOperationException;
38
import org.gvsig.raster.lib.legend.api.RasterLegendLocator;
39
import org.gvsig.raster.lib.legend.api.RasterLegendManager;
40
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation;
41
import org.gvsig.raster.lib.legend.spi.AbstractColoredOperation;
42
import org.gvsig.tools.locator.LocatorException;
43

    
44

    
45
/**
46
 * @author fdiaz
47
 *
48
 */
49
public class ContrastOperation extends AbstractColoredOperation{
50

    
51
    static public String CONTRAST_PARAM = "contrast";
52

    
53
    private int contrast;
54
    private RowProcessor rowProcessor;
55

    
56
    /**
57
     * @param factory
58
     *
59
     */
60
    public ContrastOperation(OperationFactory factory) {
61
        super(factory);
62
    }
63

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

    
70
        contrast = (Integer) this.getParameter(CONTRAST_PARAM,0);
71

    
72
        int bands = this.getInputBuffer().getBandCount();
73
        NoData[] noData;
74
        if (mustCopyUnprocessedBands()) {
75
            this.setParameter(OUTPUT_COLOR_INTERPRETATION_PARAM, getInputColorInterpretation());
76
            noData = this.getInputBuffer().getBandNoData();
77
            try {
78
                this.setOutputBuffer( 
79
                    manager.createBuffer(
80
                            this.getInputBuffer().getRows(), 
81
                            this.getInputBuffer().getColumns(), 
82
                            this.getInputBuffer().getBandTypes(),
83
                            this.getInputBuffer().getBandNoData(), 
84
                            this.getInputBuffer().getProjection(), 
85
                            this.getInputBuffer().getEnvelope()));
86
            } catch (Exception e) {
87
                throw new ProcessingOperationException(e);
88
            }
89
        } else {
90
            List<String> colorInterpretations = new ArrayList<>();
91
            List<NoData> noDatas = new ArrayList<>();
92
            List<Integer> types = new ArrayList<>();
93
            for (int band = 0; band < bands; band++) {
94
                if (isProcessableBand(band)) {
95
                    colorInterpretations.add(getInputColorInterpretation().get(band));
96
                    noDatas.add(this.getInputBuffer().getBandNoData()[band]);
97
                    types.add(this.getInputBuffer().getBandTypes()[band]);
98
                }
99
            }
100
            if (getInputColorInterpretation().hasAlphaBand()) {
101
                colorInterpretations.add(ColorInterpretation.ALPHA_BAND);
102
            }
103
            setOutputColorInterpretation(legendManager.createColorInterpretation(
104
                    colorInterpretations));
105
            this.setParameter(
106
                    OUTPUT_COLOR_INTERPRETATION_PARAM, 
107
                    getOutputColorInterpretation());
108
            try {
109
                this.setOutputBuffer(
110
                    manager.createBuffer(
111
                            this.getInputBuffer().getRows(), 
112
                            this.getInputBuffer().getColumns(), 
113
                            this.getTypesAsArray(types),
114
                            this.getNoDatasAsArray(noDatas), 
115
                            this.getInputBuffer().getProjection(), 
116
                            this.getInputBuffer().getEnvelope()));
117
            } catch (LocatorException | BufferException | CreateEnvelopeException e) {
118
                throw new ProcessingOperationException(e);
119
            }
120

    
121
        }
122
    }
123

    
124
    @Override
125
    public void process() throws ProcessingOperationException {
126
        super.process();
127
        for (int band=0; band<this.getInputBuffer().getBandCount(); band++){
128
            rowProcessor = new ByteRowProcessor(band);
129
            if (isProcessableBand(band)) {
130
                Band bufferBand = this.getInputBuffer().getBand(band);
131
                Band outputBufferBand = this.getOutputBuffer().getBand(band);
132

    
133
                for (int row = 0; row < this.getInputBuffer().getRows(); row++) {
134
                    Object rowBuffer = bufferBand.createRowBuffer();
135
                    bufferBand.fetchRow(row, rowBuffer);
136

    
137
                    Object outputRowBuffer = outputBufferBand.createRowBuffer();
138
                    outputBufferBand.fetchRow(row, outputRowBuffer);
139

    
140
                    rowProcessor.processRow(rowBuffer, outputRowBuffer);
141

    
142
                    outputBufferBand.putRow(row, outputRowBuffer);
143
                }
144
            } else if (mustCopyUnprocessedBands()) {
145
                try {
146
                    this.getOutputBuffer().getBand(band).copyFrom(this.getInputBuffer().getBand(band));
147
                } catch (BandException e) {
148
                    throw new ProcessingOperationException(e);
149
                }
150
            } else if (getInputColorInterpretation().isAlphaInterpretation(band)) {
151
                try {
152
                    this.getOutputBuffer().getBand(getOutputColorInterpretation().getAlphaBand()).copyFrom(this.getInputBuffer().getBand(band));
153
                } catch (BandException e) {
154
                    throw new ProcessingOperationException(e);
155
            }
156
        }
157
    }
158
    }
159

    
160
    /**
161
     * @param band
162
     * @return
163
     */
164
    @Override
165
    protected boolean isProcessableBand(int band) {
166
        return isRGBorGrayBand(band) && this.getInputBuffer().getBandTypes()[band]==BufferManager.TYPE_BYTE;
167
    }
168

    
169
    private boolean isRGBorGrayBand(int band) {
170
        String bandColorInterpretation = getInputColorInterpretation().get(band);
171
        return (bandColorInterpretation.equals(ColorInterpretation.RED_BAND) ||
172
            bandColorInterpretation.equals(ColorInterpretation.GREEN_BAND) ||
173
            bandColorInterpretation.equals(ColorInterpretation.BLUE_BAND) ||
174
            bandColorInterpretation.equals(ColorInterpretation.GRAY_BAND));
175
    }
176

    
177
    @Override
178
    public void postProcess() throws BufferOperationException {
179
        super.postProcess();
180
    }
181

    
182
    interface RowProcessor {
183
        void processRow(Object inputRow, Object outputRow);
184
        byte processValue(Object value);
185
    };
186

    
187
    private abstract class AbstractRowProcessor implements RowProcessor {
188
//        int band;
189
        int maxResult = 255;
190
        int minResult = 0;
191
        NoData noData;
192

    
193
        public AbstractRowProcessor(int band) {
194
//            this.band = band;
195
            noData = getInputBuffer().getBand(band).getNoData();
196
            if(noData.isDefined()) {
197
                minResult = (byte)1;
198
            }
199
        }
200
    }
201

    
202
    private class ByteRowProcessor extends AbstractRowProcessor {
203

    
204

    
205
        public ByteRowProcessor(int band) {
206
            super(band);
207
        }
208

    
209
        @Override
210
        public void processRow(Object inputRow, Object outputRow) {
211
            byte[] inputByteRow = (byte[])inputRow;
212
            byte[] outputByteRow = (byte[])outputRow;
213
            for (int i = 0; i < inputByteRow.length; i++) {
214
                outputByteRow[i] = processValue(inputByteRow[i]);
215
            }
216
        }
217

    
218
        @Override
219
        public byte processValue(Object value) {
220
            if(noData.isDefined() && noData.getValue().equals(value)){
221
                return (byte)value;
222
            }
223

    
224
            int iValue = 0xFF & ((Byte) value);
225

    
226
            //FIXME: Copiado del raster viejo. No entiendo los c?lculos
227

    
228
            int result;
229
            int diferencia = 127 - iValue;
230
            if (contrast >= 0) {
231
                result = (int) (iValue - (0.02 * diferencia * contrast)); // ((5.0 * 0.1) / 25) = 0.02
232
            } else {
233
                result = (int) (iValue - (0.004 * diferencia * contrast)); // (0.1 / 25) = 0.004;
234
                if (iValue < 127) {
235
                    if (result > 127) result = 127;
236
                } else {
237
                    if (result < 127) result = 127;
238
                }
239
            }
240

    
241
            if(result>maxResult){
242
                result = maxResult;
243
            }
244
            if(result<minResult){
245
                result = minResult;
246
            }
247
            return (byte)result;
248
        }
249
    }
250
}