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 / rgbtocmyk / RGBToCMYKOperation.java @ 43803

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

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

    
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31

    
32
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
33
import org.gvsig.raster.lib.buffer.api.Band;
34
import org.gvsig.raster.lib.buffer.api.BufferLocator;
35
import org.gvsig.raster.lib.buffer.api.BufferManager;
36
import org.gvsig.raster.lib.buffer.api.NoData;
37
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException;
38
import org.gvsig.raster.lib.buffer.api.exceptions.BufferOperationException;
39
import org.gvsig.raster.lib.buffer.api.operations.OperationFactory;
40
import org.gvsig.raster.lib.buffer.spi.exceptions.ProcessingOperationException;
41
import org.gvsig.raster.lib.legend.api.RasterLegendLocator;
42
import org.gvsig.raster.lib.legend.api.RasterLegendManager;
43
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation;
44
import org.gvsig.raster.lib.legend.impl.ColorManager;
45
import org.gvsig.raster.lib.legend.impl.RasterLegendManagerServices;
46
import org.gvsig.raster.lib.legend.spi.AbstractColoredOperation;
47
import org.gvsig.tools.locator.LocatorException;
48

    
49

    
50
/**
51
 * @author fdiaz
52
 *
53
 */
54
public class RGBToCMYKOperation extends AbstractColoredOperation {
55

    
56
    private static final Logger LOG = LoggerFactory.getLogger(RGBToCMYKOperation.class);
57

    
58
    /**
59
     * @param factory
60
     *
61
     */
62
    public RGBToCMYKOperation(OperationFactory factory) {
63
        this.factory = factory;
64
    }
65

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

    
72
        try {
73
            if (!(colorInterpretation.isGray() || colorInterpretation.isRGB())) {
74
                throw new UnsupportedOperationException(
75
                    "The color interpretation of input buffer isn't RGB nor GRAYSCALE");
76
            }
77

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

    
94
            int sourceBands = this.buffer.getBandCount();
95
            NoData[] sourceNoDatas = this.buffer.getBandNoData();
96
            int[] sourceTypes = this.buffer.getBandTypes();
97

    
98
            List<String> colorInterpretations = new ArrayList<String>();
99
            List<NoData> noDatas = new ArrayList<NoData>();
100
            List<Integer> types = new ArrayList<Integer>();
101

    
102
            colorInterpretations.add(ColorInterpretation.CYAN_BAND);
103
            colorInterpretations.add(ColorInterpretation.MAGENTA_BAND);
104
            colorInterpretations.add(ColorInterpretation.YELLOW_BAND);
105
            colorInterpretations.add(ColorInterpretation.BLACK_BAND);
106

    
107
            types.add(BufferManager.TYPE_BYTE);
108
            types.add(BufferManager.TYPE_BYTE);
109
            types.add(BufferManager.TYPE_BYTE);
110
            types.add(BufferManager.TYPE_BYTE);
111

    
112
            noDatas.add(null);
113
            noDatas.add(null);
114
            noDatas.add(null);
115
            noDatas.add(null);
116

    
117
            if (colorInterpretation.hasAlphaBand()) {
118
                int alphaBand = colorInterpretation.getAlphaBand();
119
                if(sourceTypes[alphaBand]!=BufferManager.TYPE_BYTE){
120
                    throw new UnsupportedOperationException("The type of ALPHA band isn't BYTE");
121
                }
122
                colorInterpretations.add(ColorInterpretation.ALPHA_BAND);
123
                types.add(sourceTypes[alphaBand]);
124
                noDatas.add(sourceNoDatas[alphaBand]);
125
            }
126

    
127
            if (copyUnprocessedBands) {
128
                for (int band = 0; band < sourceBands; band++) {
129
                    if (!isProcessableBand(band) && !colorInterpretation.isAlphaInterpretation(band)) {
130
                        colorInterpretations.add(ColorInterpretation.UNDEFINED_BAND);
131
                        noDatas.add(sourceNoDatas[band]);
132
                        types.add(this.buffer.getBandTypes()[band]);
133
                    }
134
                }
135
            }
136

    
137
            outputColorInterpretation =
138
                legendManager.createColorInterpretation(colorInterpretations.toArray(new String[0]));
139
            this.parameters.setDynValue(OUTPUT_COLOR_INTERPRETATION_PARAM, outputColorInterpretation);
140
            int[] typesInt = new int[types.size()];
141
            for (Iterator<Integer> iterator = types.iterator(); iterator.hasNext();) {
142
                int i = 0;
143
                Integer type = (Integer) iterator.next();
144
                typesInt[i] = type.intValue();
145
            }
146

    
147
            this.outputBuffer =
148
                manager.createBuffer(this.buffer.getRows(), this.buffer.getColumns(), typesInt,
149
                    noDatas.toArray(new NoData[0]), this.buffer.getProjection(), this.buffer.getEnvelope());
150
        } catch (LocatorException | BufferException | CreateEnvelopeException e) {
151
            throw new ProcessingOperationException(e);
152
        }
153
    }
154

    
155
    @Override
156
    public void process() throws ProcessingOperationException {
157
        super.process();
158
        try {
159

    
160
            // List<Integer> bandsToProcess = new ArrayList<Integer>();
161

    
162
            if (copyUnprocessedBands) {
163
                int bands = this.buffer.getBandCount();
164
                int outBand = colorInterpretation.hasAlphaBand() ? 5 : 4;
165
                for (int band = 0; band < bands; band++) {
166
                    if (colorInterpretation.isAlphaInterpretation(band)) {
167
                        outputBuffer.getBand(outputColorInterpretation.getAlphaBand()).copyFrom(
168
                            this.buffer.getBand(band));
169
                    } else if (!isProcessableBand(band)) {
170
                        outputBuffer.getBand(outBand).copyFrom(this.buffer.getBand(band));
171
                        outBand++;
172
                    }
173
                }
174
            }
175

    
176
            Object[] rowBandsBuffer = new Object[3];
177

    
178
            for (int row = 0; row < this.buffer.getRows(); row++) {
179
                if (colorInterpretation.isGray()) {
180
                    Band bufferBandGray = buffer.getBand(colorInterpretation.getBand(ColorInterpretation.GRAY_BAND));
181
                    rowBandsBuffer[0] = bufferBandGray.createRowBuffer();
182
                    bufferBandGray.fetchRow(row, rowBandsBuffer[0]);
183
                    rowBandsBuffer[1] = bufferBandGray.createRowBuffer();
184
                    bufferBandGray.fetchRow(row, rowBandsBuffer[1]);
185
                    rowBandsBuffer[2] = bufferBandGray.createRowBuffer();
186
                    bufferBandGray.fetchRow(row, rowBandsBuffer[2]);
187
                } else {
188
                    Band bufferBandRed = buffer.getBand(colorInterpretation.getBand(ColorInterpretation.RED_BAND));
189
                    rowBandsBuffer[0] = bufferBandRed.createRowBuffer();
190
                    bufferBandRed.fetchRow(row, rowBandsBuffer[0]);
191
                    Band bufferBandGreen = buffer.getBand(colorInterpretation.getBand(ColorInterpretation.GREEN_BAND));
192
                    rowBandsBuffer[1] = bufferBandGreen.createRowBuffer();
193
                    bufferBandGreen.fetchRow(row, rowBandsBuffer[1]);
194
                    Band bufferBandBlue = buffer.getBand(colorInterpretation.getBand(ColorInterpretation.BLUE_BAND));
195
                    rowBandsBuffer[2] = bufferBandBlue.createRowBuffer();
196
                    bufferBandBlue.fetchRow(row, rowBandsBuffer[2]);
197
                }
198

    
199
                List<Object> outputRowBuffers = new ArrayList<Object>();
200
                for (int band = 0; band < 4; band++) {
201
                    Band outputBufferBand = outputBuffer.getBand(band);
202
                    Object outputRowBuffer = outputBufferBand.createRowBuffer();
203
                    outputRowBuffers.add(outputRowBuffer);
204
                }
205

    
206
                processRow(rowBandsBuffer, outputRowBuffers);
207

    
208
                for (int band = 0; band < 4; band++) {
209
                    Band outputBufferBand = outputBuffer.getBand(band);
210
                    outputBufferBand.putRow(row, outputRowBuffers.get(band));
211
                }
212

    
213
            }
214
        } catch (Exception e) {
215
            throw new ProcessingOperationException(e);
216
        }
217

    
218
    }
219

    
220
    @Override
221
    public void postProcess() throws BufferOperationException {
222
        super.postProcess();
223
    }
224

    
225
    private void processRow(Object[] inputRows, List outputRows) {
226
        ColorManager colorManager = ((RasterLegendManagerServices)RasterLegendLocator.getRasterLegendManager()).getColorManager();
227
        byte[][] inputByteRows = new byte[inputRows.length][((byte[]) inputRows[0]).length];
228
        for (int i = 0; i < inputRows.length; i++) {
229
            inputByteRows[i] = (byte[]) inputRows[i];
230
        }
231

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

    
234
            double[] convertedValues = colorManager.RGBtoCMYK(0xFF & inputByteRows[0][i], 0xFF & inputByteRows[1][i], 0xFF & inputByteRows[2][i], 255);
235

    
236
            for (int band = 0; band < outputRows.size(); band++) {
237
                ((byte[])(outputRows.get(band)))[i] = new Double(convertedValues[band]).byteValue();
238
            }
239
        }
240
    }
241

    
242
    /**
243
     * @param band
244
     * @return
245
     */
246
    private boolean isProcessableBand(int band) {
247
        return isRGBorGrayBand(band) && this.buffer.getBandTypes()[band] == BufferManager.TYPE_BYTE;
248
    }
249

    
250
    private boolean isRGBorGrayBand(int band) {
251
        String bandColorInterpretation = colorInterpretation.get(band);
252
        return (bandColorInterpretation.equals(ColorInterpretation.RED_BAND)
253
            || bandColorInterpretation.equals(ColorInterpretation.GREEN_BAND)
254
            || bandColorInterpretation.equals(ColorInterpretation.BLUE_BAND) || bandColorInterpretation
255
                .equals(ColorInterpretation.GRAY_BAND));
256
    }
257
}