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 / rgbtohsl / RGBToHSLOperation.java @ 43803

History | View | Annotate | Download (11.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.legend.impl.operations.rgbtohsl;
24

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

    
29
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
30
import org.gvsig.raster.lib.buffer.api.Band;
31
import org.gvsig.raster.lib.buffer.api.BufferLocator;
32
import org.gvsig.raster.lib.buffer.api.BufferManager;
33
import org.gvsig.raster.lib.buffer.api.NoData;
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.buffer.spi.operations.AbstractOperation;
39
import org.gvsig.raster.lib.legend.api.RasterLegendLocator;
40
import org.gvsig.raster.lib.legend.api.RasterLegendManager;
41
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation;
42
import org.gvsig.raster.lib.legend.impl.ColorManager;
43
import org.gvsig.raster.lib.legend.impl.RasterLegendManagerServices;
44
import org.gvsig.raster.lib.legend.spi.AbstractColoredOperation;
45
import org.gvsig.tools.locator.LocatorException;
46

    
47

    
48
/**
49
 * @author fdiaz
50
 *
51
 */
52
public class RGBToHSLOperation extends AbstractColoredOperation {
53

    
54
    /**
55
     * @param factory
56
     *
57
     */
58
    public RGBToHSLOperation(OperationFactory factory) {
59
        this.factory = factory;
60
    }
61

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

    
68
        try {
69
            if (!(colorInterpretation.isGray() || colorInterpretation.isRGB())) {
70
                throw new UnsupportedOperationException(
71
                    "The color interpretation of input buffer isn't RGB nor GRAYSCALE");
72
            }
73

    
74
            int[] inputBandTypes = buffer.getBandTypes();
75
            if (colorInterpretation.isGray()) {
76
                if (inputBandTypes[colorInterpretation.getBand(ColorInterpretation.GRAY_BAND)] != BufferManager.TYPE_BYTE) {
77
                    throw new UnsupportedOperationException("The type of GRAY band isn't BYTE");
78
                }
79
            } else if (colorInterpretation.isRGB()) {
80
                if (inputBandTypes[colorInterpretation.getBand(ColorInterpretation.RED_BAND)] != BufferManager.TYPE_BYTE
81
                    || inputBandTypes[colorInterpretation.getBand(ColorInterpretation.GREEN_BAND)] != BufferManager.TYPE_BYTE
82
                    || inputBandTypes[colorInterpretation.getBand(ColorInterpretation.BLUE_BAND)] != BufferManager.TYPE_BYTE) {
83
                    throw new UnsupportedOperationException("The type of RGB bands isn't BYTE");
84
                }
85
            } else {
86
                throw new UnsupportedOperationException(
87
                    "The color interpretation of input buffer isn't RGB nor GRAYSCALE");
88
            }
89

    
90
            int sourceBands = this.buffer.getBandCount();
91
            NoData[] sourceNoDatas = this.buffer.getBandNoData();
92
            int[] sourceTypes = this.buffer.getBandTypes();
93

    
94
            List<String> colorInterpretations = new ArrayList<String>();
95
            List<NoData> noDatas = new ArrayList<NoData>();
96
            List<Integer> types = new ArrayList<Integer>();
97

    
98
            colorInterpretations.add(ColorInterpretation.HUE_BAND);
99
            colorInterpretations.add(ColorInterpretation.SATURATION_BAND);
100
            colorInterpretations.add(ColorInterpretation.LIGHTNESS_BAND);
101

    
102
            types.add(BufferManager.TYPE_BYTE);
103
            types.add(BufferManager.TYPE_BYTE);
104
            types.add(BufferManager.TYPE_BYTE);
105

    
106
            noDatas.add(null);
107
            noDatas.add(null);
108
            noDatas.add(null);
109

    
110
            if (colorInterpretation.hasAlphaBand()) {
111
                int alphaBand = colorInterpretation.getAlphaBand();
112
                if(sourceTypes[alphaBand]!=BufferManager.TYPE_BYTE){
113
                    throw new UnsupportedOperationException("The type of ALPHA band isn't BYTE");
114
                }
115
                colorInterpretations.add(ColorInterpretation.ALPHA_BAND);
116
                types.add(sourceTypes[alphaBand]);
117
                noDatas.add(sourceNoDatas[alphaBand]);
118
            }
119

    
120
            if (copyUnprocessedBands) {
121
                for (int band = 0; band < sourceBands; band++) {
122
                    if (!isProcessableBand(band) && !colorInterpretation.isAlphaInterpretation(band)) {
123
                        colorInterpretations.add(colorInterpretation.get(band));
124
                        noDatas.add(sourceNoDatas[band]);
125
                        types.add(this.buffer.getBandTypes()[band]);
126
                    }
127
                }
128
            }
129

    
130
            outputColorInterpretation =
131
                legendManager.createColorInterpretation(colorInterpretations.toArray(new String[0]));
132
            this.parameters.setDynValue(OUTPUT_COLOR_INTERPRETATION_PARAM, outputColorInterpretation);
133
            int[] typesInt = new int[types.size()];
134
            for (Iterator<Integer> iterator = types.iterator(); iterator.hasNext();) {
135
                int i = 0;
136
                Integer type = (Integer) iterator.next();
137
                typesInt[i] = type.intValue();
138
            }
139

    
140
            this.outputBuffer =
141
                manager.createBuffer(this.buffer.getRows(), this.buffer.getColumns(), typesInt,
142
                    noDatas.toArray(new NoData[0]), this.buffer.getProjection(), this.buffer.getEnvelope());
143
        } catch (LocatorException | BufferException | CreateEnvelopeException e) {
144
            throw new ProcessingOperationException(e);
145
        }
146
    }
147

    
148
    @Override
149
    public void process() throws ProcessingOperationException {
150
        super.process();
151
        try {
152

    
153
            // List<Integer> bandsToProcess = new ArrayList<Integer>();
154

    
155
            if (copyUnprocessedBands) {
156
                int bands = this.buffer.getBandCount();
157
                int outBand = colorInterpretation.hasAlphaBand() ? 5 : 4;
158
                for (int band = 0; band < bands; band++) {
159
                    if (colorInterpretation.isAlphaInterpretation(band)) {
160
                        outputBuffer.getBand(outputColorInterpretation.getAlphaBand()).copyFrom(
161
                            this.buffer.getBand(band));
162
                    } else if (!isProcessableBand(band)) {
163
                        outputBuffer.getBand(outBand).copyFrom(this.buffer.getBand(band));
164
                        outBand++;
165
                    }
166
                }
167
            }
168

    
169
            Object[] rowBandsBuffer = new Object[3];
170

    
171
            for (int row = 0; row < this.buffer.getRows(); row++) {
172
                if (colorInterpretation.isGray()) {
173
                    Band bufferBandGray = buffer.getBand(colorInterpretation.getBand(ColorInterpretation.GRAY_BAND));
174
                    rowBandsBuffer[0] = bufferBandGray.createRowBuffer();
175
                    bufferBandGray.fetchRow(row, rowBandsBuffer[0]);
176
                    rowBandsBuffer[1] = bufferBandGray.createRowBuffer();
177
                    bufferBandGray.fetchRow(row, rowBandsBuffer[1]);
178
                    rowBandsBuffer[2] = bufferBandGray.createRowBuffer();
179
                    bufferBandGray.fetchRow(row, rowBandsBuffer[2]);
180
                } else {
181
                    Band bufferBandRed = buffer.getBand(colorInterpretation.getBand(ColorInterpretation.RED_BAND));
182
                    rowBandsBuffer[0] = bufferBandRed.createRowBuffer();
183
                    bufferBandRed.fetchRow(row, rowBandsBuffer[0]);
184
                    Band bufferBandGreen = buffer.getBand(colorInterpretation.getBand(ColorInterpretation.GREEN_BAND));
185
                    rowBandsBuffer[1] = bufferBandGreen.createRowBuffer();
186
                    bufferBandGreen.fetchRow(row, rowBandsBuffer[1]);
187
                    Band bufferBandBlue = buffer.getBand(colorInterpretation.getBand(ColorInterpretation.BLUE_BAND));
188
                    rowBandsBuffer[2] = bufferBandBlue.createRowBuffer();
189
                    bufferBandBlue.fetchRow(row, rowBandsBuffer[2]);
190
                }
191

    
192
                List<Object> outputRowBuffers = new ArrayList<Object>();
193
                for (int band = 0; band < 3; band++) {
194
                    Band outputBufferBand = outputBuffer.getBand(band);
195
                    Object outputRowBuffer = outputBufferBand.createRowBuffer();
196
                    outputRowBuffers.add(outputRowBuffer);
197
                }
198

    
199
                processRow(rowBandsBuffer, outputRowBuffers);
200

    
201
                for (int band = 0; band < 3; band++) {
202
                    Band outputBufferBand = outputBuffer.getBand(band);
203
                    outputBufferBand.putRow(row, outputRowBuffers.get(band));
204
                }
205

    
206
            }
207
        } catch (Exception e) {
208
            throw new ProcessingOperationException(e);
209
        }
210

    
211
    }
212

    
213
    @Override
214
    public void postProcess() throws BufferOperationException {
215
        super.postProcess();
216
    }
217

    
218
    private void processRow(Object[] inputRows, List outputRows) {
219
        ColorManager colorManager = ((RasterLegendManagerServices)RasterLegendLocator.getRasterLegendManager()).getColorManager();
220
        byte[][] inputByteRows = new byte[inputRows.length][((byte[]) inputRows[0]).length];
221
        for (int i = 0; i < inputRows.length; i++) {
222
            inputByteRows[i] = (byte[]) inputRows[i];
223
        }
224
        for (int i = 0; i < inputByteRows[0].length; i++) {
225

    
226
            double[] convertedValues = colorManager.RGBtoHSL(0xFF & inputByteRows[0][i], 0xFF & inputByteRows[1][i], 0xFF & inputByteRows[2][i]);
227

    
228
            ((byte[])(outputRows.get(0)))[i] += new Double((convertedValues[0]*255)/360.0 + 0.5).byteValue();
229
            ((byte[])(outputRows.get(1)))[i] += new Double(convertedValues[1]*255+0.5).byteValue();
230
            ((byte[])(outputRows.get(2)))[i] += new Double(convertedValues[2]*255+0.5).byteValue();
231
        }
232
    }
233
    /**
234
     * @param band
235
     * @return
236
     */
237
    private boolean isProcessableBand(int band) {
238
        return isRGBorGrayBand(band) && this.buffer.getBandTypes()[band] == BufferManager.TYPE_BYTE;
239
    }
240

    
241
    private boolean isRGBorGrayBand(int band) {
242
        String bandColorInterpretation = colorInterpretation.get(band);
243
        return (bandColorInterpretation.equals(ColorInterpretation.RED_BAND)
244
            || bandColorInterpretation.equals(ColorInterpretation.GREEN_BAND)
245
            || bandColorInterpretation.equals(ColorInterpretation.BLUE_BAND) || bandColorInterpretation
246
                .equals(ColorInterpretation.GRAY_BAND));
247
    }
248

    
249
}