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 / hsltorgb / HSLToRGBOperation.java @ 43862

History | View | Annotate | Download (10.1 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.hsltorgb;
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 HSLToRGBOperation extends AbstractColoredOperation {
53

    
54
    /**
55
     * @param factory
56
     *
57
     */
58
    public HSLToRGBOperation(OperationFactory factory) {
59
        super(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 (!getInputColorInterpretation().isHSL()) {
70
                throw new UnsupportedOperationException("The color interpretation of input getInputBuffer() isn't HSL");
71
            }
72

    
73
            int[] inputBandTypes = getInputBuffer().getBandTypes();
74
            if (inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.HUE_BAND)] != BufferManager.TYPE_BYTE
75
                || inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.SATURATION_BAND)] != BufferManager.TYPE_BYTE
76
                || inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.LIGHTNESS_BAND)] != BufferManager.TYPE_BYTE) {
77
                throw new UnsupportedOperationException("The type of bands isn't BYTE");
78
            }
79

    
80
            int sourceBands = this.getInputBuffer().getBandCount();
81
            NoData[] sourceNoDatas = this.getInputBuffer().getBandNoData();
82
            int[] sourceTypes = this.getInputBuffer().getBandTypes();
83

    
84
            List<String> colorInterpretations = new ArrayList<String>();
85
            List<NoData> noDatas = new ArrayList<NoData>();
86
            List<Integer> types = new ArrayList<Integer>();
87

    
88
            colorInterpretations.add(ColorInterpretation.RED_BAND);
89
            colorInterpretations.add(ColorInterpretation.GREEN_BAND);
90
            colorInterpretations.add(ColorInterpretation.BLUE_BAND);
91

    
92
            types.add(BufferManager.TYPE_BYTE);
93
            types.add(BufferManager.TYPE_BYTE);
94
            types.add(BufferManager.TYPE_BYTE);
95

    
96
            noDatas.add(null);
97
            noDatas.add(null);
98
            noDatas.add(null);
99

    
100
            if (getInputColorInterpretation().hasAlphaBand()) {
101
                int alphaBand = getInputColorInterpretation().getAlphaBand();
102
                if(sourceTypes[alphaBand]!=BufferManager.TYPE_BYTE){
103
                    throw new UnsupportedOperationException("The type of ALPHA band isn't BYTE");
104
                }
105
                colorInterpretations.add(ColorInterpretation.ALPHA_BAND);
106
                types.add(sourceTypes[alphaBand]);
107
                noDatas.add(sourceNoDatas[alphaBand]);
108
            }
109

    
110
            if (mustCopyUnprocessedBands()) {
111
                for (int band = 0; band < sourceBands; band++) {
112
                    if (!isProcessableBand(band) && !getInputColorInterpretation().isAlphaInterpretation(band)) {
113
                        colorInterpretations.add(getInputColorInterpretation().get(band));
114
                        noDatas.add(sourceNoDatas[band]);
115
                        types.add(this.getInputBuffer().getBandTypes()[band]);
116
                    }
117
                }
118
            }
119

    
120
            setOutputColorInterpretation(
121
                legendManager.createColorInterpretation(colorInterpretations));
122
            this.setParameter(OUTPUT_COLOR_INTERPRETATION_PARAM, getOutputColorInterpretation());
123
            this.setOutputBuffer(
124
                manager.createBuffer(
125
                        this.getInputBuffer().getRows(), 
126
                        this.getInputBuffer().getColumns(), 
127
                        this.getTypesAsArray(types),
128
                        this.getNoDatasAsArray(noDatas), 
129
                        this.getInputBuffer().getProjection(), 
130
                        this.getInputBuffer().getEnvelope()));
131
        } catch (Exception e) {
132
            throw new ProcessingOperationException(e);
133
        }
134
    }
135

    
136
    @Override
137
    public void process() throws ProcessingOperationException {
138
        super.process();
139
        try {
140

    
141
            // List<Integer> bandsToProcess = new ArrayList<Integer>();
142

    
143
            if (mustCopyUnprocessedBands()) {
144
                int bands = this.getInputBuffer().getBandCount();
145
                int outBand = getInputColorInterpretation().hasAlphaBand() ? 4 : 3;
146
                for (int band = 0; band < bands; band++) {
147
                    if (getInputColorInterpretation().isAlphaInterpretation(band)) {
148
                        getOutputBuffer().getBand(getOutputColorInterpretation().getAlphaBand()).copyFrom(
149
                            this.getInputBuffer().getBand(band));
150
                    } else if (!isProcessableBand(band)) {
151
                        getOutputBuffer().getBand(outBand).copyFrom(this.getInputBuffer().getBand(band));
152
                        outBand++;
153
                    }
154
                }
155
            }
156

    
157
            Object[] rowBandsBuffer = new Object[3];
158

    
159
            for (int row = 0; row < this.getInputBuffer().getRows(); row++) {
160
                Band bufferBandHue = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.HUE_BAND));
161
                rowBandsBuffer[0] = bufferBandHue.createRowBuffer();
162
                bufferBandHue.fetchRow(row, rowBandsBuffer[0]);
163
                Band bufferBandSaturation = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.SATURATION_BAND));
164
                rowBandsBuffer[1] = bufferBandSaturation.createRowBuffer();
165
                bufferBandSaturation.fetchRow(row, rowBandsBuffer[1]);
166
                Band bufferBandLightness = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.LIGHTNESS_BAND));
167
                rowBandsBuffer[2] = bufferBandLightness.createRowBuffer();
168
                bufferBandLightness.fetchRow(row, rowBandsBuffer[2]);
169

    
170
                List<Object> outputRowBuffers = new ArrayList<Object>();
171
                for (int band = 0; band < 3; band++) {
172
                    Band outputBufferBand = getOutputBuffer().getBand(band);
173
                    Object outputRowBuffer = outputBufferBand.createRowBuffer();
174
                    outputRowBuffers.add(outputRowBuffer);
175
                }
176

    
177
                processRow(rowBandsBuffer, outputRowBuffers);
178

    
179
                for (int band = 0; band < 3; band++) {
180
                    Band outputBufferBand = getOutputBuffer().getBand(band);
181
                    outputBufferBand.putRow(row, outputRowBuffers.get(band));
182
                }
183

    
184
            }
185
        } catch (Exception e) {
186
            throw new ProcessingOperationException(e);
187
        }
188

    
189
    }
190

    
191
    @Override
192
    public void postProcess() throws BufferOperationException {
193
        super.postProcess();
194
    }
195

    
196
    private void processRow(Object[] inputRows, List outputRows) {
197
        ColorManager colorManager = ((RasterLegendManagerServices)RasterLegendLocator.getRasterLegendManager()).getColorManager();
198
        byte[][] inputByteRows = new byte[inputRows.length][((byte[]) inputRows[0]).length];
199
        for (int i = 0; i < inputRows.length; i++) {
200
            inputByteRows[i] = (byte[]) inputRows[i];
201
        }
202

    
203
        for (int i = 0; i < inputByteRows[0].length; i++) {
204

    
205
            int[] convertedValues = colorManager.HSLtoRGB((0xFF & inputByteRows[0][i]), (0xFF & inputByteRows[1][i]), (0xFF & inputByteRows[2][i]));
206

    
207
            for (int band = 0; band < outputRows.size(); band++) {
208
                ((byte[])(outputRows.get(band)))[i] = new Integer(convertedValues[band]).byteValue();
209
            }
210
        }
211
    }
212

    
213
    /**
214
     * @param band
215
     * @return
216
     */
217
    @Override
218
    protected boolean isProcessableBand(int band) {
219
        return isHSLBand(band) && this.getInputBuffer().getBandTypes()[band] == BufferManager.TYPE_BYTE;
220
    }
221

    
222
    private boolean isHSLBand(int band) {
223
        String bandColorInterpretation = getInputColorInterpretation().get(band);
224
        return (bandColorInterpretation.equals(ColorInterpretation.HUE_BAND)
225
            || bandColorInterpretation.equals(ColorInterpretation.SATURATION_BAND)
226
            || bandColorInterpretation.equals(ColorInterpretation.LIGHTNESS_BAND));
227
    }
228

    
229
}