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 / grayscale / GrayScaleOperation.java @ 43862

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

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

    
28
import org.gvsig.raster.lib.buffer.api.Band;
29
import org.gvsig.raster.lib.buffer.api.BufferLocator;
30
import org.gvsig.raster.lib.buffer.api.BufferManager;
31
import org.gvsig.raster.lib.buffer.api.NoData;
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.spi.exceptions.ProcessingOperationException;
35
import org.gvsig.raster.lib.legend.api.RasterLegendLocator;
36
import org.gvsig.raster.lib.legend.api.RasterLegendManager;
37
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation;
38
import org.gvsig.raster.lib.legend.spi.AbstractColoredOperation;
39

    
40

    
41
/**
42
 * @author fdiaz
43
 *
44
 */
45
public class GrayScaleOperation extends AbstractColoredOperation {
46

    
47
    /**
48
     * @param factory
49
     *
50
     */
51
    public GrayScaleOperation(OperationFactory factory) {
52
        super(factory);
53
    }
54

    
55
    @Override
56
    public void preProcess() throws BufferOperationException {
57
        super.preProcess();
58
        BufferManager manager = BufferLocator.getBufferManager();
59
        RasterLegendManager legendManager = RasterLegendLocator.getRasterLegendManager();
60

    
61
        try {
62

    
63
            if (!(getInputColorInterpretation().hasAnyRGBBand() || getInputColorInterpretation().hasAnyGrayBand())) {
64
                throw new UnsupportedOperationException(
65
                    "The color interpretation of input buffer isn't RGB nor GRAYSCALE");
66
            }
67

    
68
            int[] inputBandTypes = getInputBuffer().getBandTypes();
69
            if (getInputColorInterpretation().hasAnyRGBBand()) {
70
                if ((getInputColorInterpretation().getBand(ColorInterpretation.RED_BAND)>=0 && inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.RED_BAND)] != BufferManager.TYPE_BYTE)
71
                    || (getInputColorInterpretation().getBand(ColorInterpretation.GREEN_BAND)>=0 && inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.GREEN_BAND)] != BufferManager.TYPE_BYTE)
72
                    || (getInputColorInterpretation().getBand(ColorInterpretation.BLUE_BAND)>=0 && inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.BLUE_BAND)] != BufferManager.TYPE_BYTE)) {
73
                    throw new UnsupportedOperationException("The type of bands isn't BYTE");
74
                }
75
            } else if (getInputColorInterpretation().hasAnyGrayBand()) {
76
                if ((getInputColorInterpretation().getBand(ColorInterpretation.GRAY_BAND)>=0 && inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.GRAY_BAND)] != BufferManager.TYPE_BYTE)) {
77
                    throw new UnsupportedOperationException("The type of band gray isn't BYTE");
78
                }
79
            } else {
80
                throw new UnsupportedOperationException(
81
                    "The color interpretation of input buffer isn't RGB nor GRAYSCALE");
82
            }
83

    
84
            int sourceBands = this.getInputBuffer().getBandCount();
85
            NoData[] sourceNoDatas = this.getInputBuffer().getBandNoData();
86
            int[] sourceTypes = this.getInputBuffer().getBandTypes();
87

    
88
            List<String> colorInterpretations = new ArrayList<>();
89
            List<NoData> noDatas = new ArrayList<>();
90
            List<Integer> types = new ArrayList<>();
91

    
92
            colorInterpretations.add(ColorInterpretation.GRAY_BAND);
93
            types.add(BufferManager.TYPE_BYTE);
94
            noDatas.add(null);
95

    
96
            if (getInputColorInterpretation().hasAlphaBand()) {
97
                if(sourceTypes[getInputColorInterpretation().getAlphaBand()]!=BufferManager.TYPE_BYTE){
98
                    throw new UnsupportedOperationException("The type of ALPHA band isn't BYTE");
99
                }
100
                colorInterpretations.add(ColorInterpretation.ALPHA_BAND);
101
                types.add(sourceTypes[getInputColorInterpretation().getAlphaBand()]);
102
                noDatas.add(sourceNoDatas[getInputColorInterpretation().getAlphaBand()]);
103
            }
104

    
105
            if (mustCopyUnprocessedBands()) {
106
                for (int band = 0; band < sourceBands; band++) {
107
                    if (!isProcessableBand(band) && !getInputColorInterpretation().isAlphaInterpretation(band)) {
108
                        colorInterpretations.add(getInputColorInterpretation().get(band));
109
                        noDatas.add(sourceNoDatas[band]);
110
                        types.add(this.getInputBuffer().getBandTypes()[band]);
111
                    }
112
                }
113
            }
114
            setOutputColorInterpretation(
115
                legendManager.createColorInterpretation(colorInterpretations));
116
            this.setParameter(OUTPUT_COLOR_INTERPRETATION_PARAM, getOutputColorInterpretation());
117
            this.setOutputBuffer(
118
                manager.createBuffer(
119
                        this.getInputBuffer().getRows(), 
120
                        this.getInputBuffer().getColumns(), 
121
                        this.getTypesAsArray(types),
122
                        this.getNoDatasAsArray(noDatas), 
123
                        this.getInputBuffer().getProjection(), 
124
                        this.getInputBuffer().getEnvelope()));
125
        } catch (Exception e) {
126
            throw new ProcessingOperationException(e);
127
        }
128
    }
129

    
130
    @Override
131
    public void process() throws ProcessingOperationException {
132
        try {
133
            super.process();
134
            List<Integer> bandsToProcess = new ArrayList<Integer>();
135

    
136
            Band outputBufferBand = getOutputBuffer().getBand(0);
137
            boolean processed = false;
138
            if (mustCopyUnprocessedBands()) {
139
                int bands = this.getInputBuffer().getBandCount();
140
                int outBand = getInputColorInterpretation().hasAlphaBand() ? 2 : 1;
141
                for (int band = 0; band < bands; band++) {
142
                    if (isProcessableBand(band)) {
143
                        if (getInputColorInterpretation().isGrayInterpretation(band)) {
144
                            outputBufferBand.copyFrom(this.getInputBuffer().getBand(band));
145
                            processed = true;
146
                        } else {
147
                            bandsToProcess.add(band);
148
                        }
149
                    } else if (getInputColorInterpretation().isAlphaInterpretation(band)) {
150
                        getOutputBuffer().getBand(getOutputColorInterpretation().getAlphaBand()).copyFrom(
151
                            this.getInputBuffer().getBand(band));
152
                    } else {
153
                        getOutputBuffer().getBand(outBand).copyFrom(
154
                            this.getInputBuffer().getBand(band));
155
                        outBand++;
156
                    }
157
                }
158
            }
159

    
160
            if (!processed) {
161
                Object[] rowBandsBuffer = new Object[bandsToProcess.size()];
162
                for (int row = 0; row < this.getInputBuffer().getRows(); row++) {
163
                    for (int i = 0; i < bandsToProcess.size(); i++) {
164
                        Integer band = bandsToProcess.get(i);
165
                        if (isProcessableBand(band)) {
166
                            Band bufferBand = getInputBuffer().getBand(band);
167
                            rowBandsBuffer[i] = bufferBand.createRowBuffer();
168
                            bufferBand.fetchRow(row, rowBandsBuffer[i]);
169
                        }
170
                    }
171
                    Object outputRowBuffer = outputBufferBand.createRowBuffer();
172
                    processRow(rowBandsBuffer, outputRowBuffer);
173
                    outputBufferBand.putRow(row, outputRowBuffer);
174
                }
175
            }
176
        } catch (Exception e) {
177
            throw new ProcessingOperationException(e);
178
        }
179
    }
180

    
181
    /**
182
     * @param band
183
     * @return
184
     */
185
    @Override
186
    protected boolean isProcessableBand(int band) {
187
        return isRGBorGrayBand(band) && this.getInputBuffer().getBandTypes()[band] == BufferManager.TYPE_BYTE;
188
    }
189

    
190
    private boolean isRGBorGrayBand(int band) {
191
        String bandColorInterpretation = getInputColorInterpretation().get(band);
192
        return (bandColorInterpretation.equals(ColorInterpretation.RED_BAND)
193
            || bandColorInterpretation.equals(ColorInterpretation.GREEN_BAND)
194
            || bandColorInterpretation.equals(ColorInterpretation.BLUE_BAND) || bandColorInterpretation
195
                .equals(ColorInterpretation.GRAY_BAND));
196
    }
197

    
198
    @Override
199
    public void postProcess() throws BufferOperationException {
200
        super.postProcess();
201
    }
202

    
203
    private void processRow(Object[] inputRows, Object outputRow) {
204
        byte[][] inputByteRows = new byte[inputRows.length][((byte[]) inputRows[0]).length];
205
        for (int i = 0; i < inputRows.length; i++) {
206
            inputByteRows[i] = (byte[]) inputRows[i];
207
        }
208
        byte[] outputByteRow = (byte[]) outputRow;
209
        for (int i = 0; i < inputByteRows[0].length; i++) {
210

    
211
            int tmp = 0;
212

    
213
            for (int band = 0; band < inputRows.length; band++) {
214
                tmp += 0xFF & ((Byte) inputByteRows[band][i]).byteValue();
215
            }
216
            outputByteRow[i] = (byte) (tmp / inputRows.length);
217
        }
218
    }
219
}