gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.legend / org.gvsig.raster.lib.legend.impl / src / main / java / org / gvsig / raster / lib / legend / impl / operations / cmyktorgb / CMYKToRGBOperation.java @ 8798
History | View | Annotate | Download (10.4 KB)
1 | 8572 | fdiaz | /* 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.cmyktorgb; |
||
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 | 8682 | fdiaz | import org.gvsig.raster.lib.buffer.spi.exceptions.ProcessingOperationException; |
38 | 8572 | fdiaz | 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 | 8682 | fdiaz | import org.gvsig.raster.lib.legend.spi.AbstractColoredOperation; |
45 | 8572 | fdiaz | import org.gvsig.tools.locator.LocatorException; |
46 | |||
47 | |||
48 | /**
|
||
49 | * @author fdiaz
|
||
50 | *
|
||
51 | */
|
||
52 | 8682 | fdiaz | public class CMYKToRGBOperation extends AbstractColoredOperation { |
53 | 8572 | fdiaz | |
54 | /**
|
||
55 | * @param factory
|
||
56 | *
|
||
57 | */
|
||
58 | public CMYKToRGBOperation(OperationFactory factory) {
|
||
59 | this.factory = factory;
|
||
60 | } |
||
61 | |||
62 | @Override
|
||
63 | public void preProcess() throws BufferOperationException { |
||
64 | 8682 | fdiaz | super.preProcess();
|
65 | 8572 | fdiaz | BufferManager manager = BufferLocator.getBufferManager(); |
66 | RasterLegendManager legendManager = RasterLegendLocator.getRasterLegendManager(); |
||
67 | |||
68 | |||
69 | try {
|
||
70 | 8588 | fdiaz | if (!colorInterpretation.isCMYK()) {
|
71 | 8798 | fdiaz | throw new UnsupportedOperationException("The color interpretation of input buffer isn't CMYK"); |
72 | 8572 | fdiaz | } |
73 | |||
74 | int[] inputBandTypes = buffer.getBandTypes(); |
||
75 | if (inputBandTypes[colorInterpretation.getBand(ColorInterpretation.CYAN_BAND)] != BufferManager.TYPE_BYTE
|
||
76 | || inputBandTypes[colorInterpretation.getBand(ColorInterpretation.MAGENTA_BAND)] != BufferManager.TYPE_BYTE |
||
77 | || inputBandTypes[colorInterpretation.getBand(ColorInterpretation.YELLOW_BAND)] != BufferManager.TYPE_BYTE |
||
78 | || inputBandTypes[colorInterpretation.getBand(ColorInterpretation.BLACK_BAND)] != BufferManager.TYPE_BYTE) { |
||
79 | throw new UnsupportedOperationException("The type of bands isn't BYTE"); |
||
80 | } |
||
81 | |||
82 | int sourceBands = this.buffer.getBandCount(); |
||
83 | NoData[] sourceNoDatas = this.buffer.getBandNoData(); |
||
84 | int[] sourceTypes = this.buffer.getBandTypes(); |
||
85 | |||
86 | List<String> colorInterpretations = new ArrayList<String>(); |
||
87 | List<NoData> noDatas = new ArrayList<NoData>(); |
||
88 | List<Integer> types = new ArrayList<Integer>(); |
||
89 | |||
90 | colorInterpretations.add(ColorInterpretation.RED_BAND); |
||
91 | 8588 | fdiaz | colorInterpretations.add(ColorInterpretation.GREEN_BAND); |
92 | 8572 | fdiaz | colorInterpretations.add(ColorInterpretation.BLUE_BAND); |
93 | |||
94 | types.add(BufferManager.TYPE_BYTE); |
||
95 | types.add(BufferManager.TYPE_BYTE); |
||
96 | types.add(BufferManager.TYPE_BYTE); |
||
97 | |||
98 | noDatas.add(null);
|
||
99 | noDatas.add(null);
|
||
100 | noDatas.add(null);
|
||
101 | |||
102 | if (colorInterpretation.hasAlphaBand()) {
|
||
103 | int alphaBand = colorInterpretation.getAlphaBand();
|
||
104 | if(sourceTypes[alphaBand]!=BufferManager.TYPE_BYTE){
|
||
105 | throw new UnsupportedOperationException("The type of ALPHA band isn't BYTE"); |
||
106 | } |
||
107 | colorInterpretations.add(ColorInterpretation.ALPHA_BAND); |
||
108 | types.add(sourceTypes[alphaBand]); |
||
109 | noDatas.add(sourceNoDatas[alphaBand]); |
||
110 | } |
||
111 | |||
112 | if (copyUnprocessedBands) {
|
||
113 | for (int band = 0; band < sourceBands; band++) { |
||
114 | 8588 | fdiaz | if (!isProcessableBand(band) && !colorInterpretation.isAlphaInterpretation(band)) {
|
115 | colorInterpretations.add(ColorInterpretation.UNDEFINED_BAND); |
||
116 | 8572 | fdiaz | noDatas.add(sourceNoDatas[band]); |
117 | types.add(this.buffer.getBandTypes()[band]);
|
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | |||
122 | outputColorInterpretation = |
||
123 | legendManager.createColorInterpretation(colorInterpretations.toArray(new String[0])); |
||
124 | this.parameters.setDynValue(OUTPUT_COLOR_INTERPRETATION_PARAM, outputColorInterpretation);
|
||
125 | int[] typesInt = new int[types.size()]; |
||
126 | for (Iterator<Integer> iterator = types.iterator(); iterator.hasNext();) { |
||
127 | int i = 0; |
||
128 | Integer type = (Integer) iterator.next(); |
||
129 | typesInt[i] = type.intValue(); |
||
130 | } |
||
131 | |||
132 | this.outputBuffer =
|
||
133 | manager.createBuffer(this.buffer.getRows(), this.buffer.getColumns(), typesInt, |
||
134 | noDatas.toArray(new NoData[0]), this.buffer.getProjection(), this.buffer.getEnvelope()); |
||
135 | } catch (LocatorException | BufferException | CreateEnvelopeException e) {
|
||
136 | throw new ProcessingOperationException(e); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | @Override
|
||
141 | public void process() throws ProcessingOperationException { |
||
142 | try {
|
||
143 | 8682 | fdiaz | super.process();
|
144 | 8572 | fdiaz | // List<Integer> bandsToProcess = new ArrayList<Integer>();
|
145 | |||
146 | if (copyUnprocessedBands) {
|
||
147 | int bands = this.buffer.getBandCount(); |
||
148 | int outBand = colorInterpretation.hasAlphaBand() ? 4 : 3; |
||
149 | for (int band = 0; band < bands; band++) { |
||
150 | if (colorInterpretation.isAlphaInterpretation(band)) {
|
||
151 | outputBuffer.getBand(outputColorInterpretation.getAlphaBand()).copyFrom( |
||
152 | this.buffer.getBand(band));
|
||
153 | } else if (!isProcessableBand(band)) { |
||
154 | outputBuffer.getBand(outBand).copyFrom(this.buffer.getBand(band));
|
||
155 | outBand++; |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | Object[] rowBandsBuffer = new Object[4]; |
||
161 | |||
162 | for (int row = 0; row < this.buffer.getRows(); row++) { |
||
163 | Band bufferBandCyan = buffer.getBand(colorInterpretation.getBand(ColorInterpretation.CYAN_BAND)); |
||
164 | rowBandsBuffer[0] = bufferBandCyan.createRowBuffer();
|
||
165 | bufferBandCyan.fetchRow(row, rowBandsBuffer[0]);
|
||
166 | Band bufferBandMagenta = buffer.getBand(colorInterpretation.getBand(ColorInterpretation.MAGENTA_BAND)); |
||
167 | rowBandsBuffer[1] = bufferBandMagenta.createRowBuffer();
|
||
168 | bufferBandMagenta.fetchRow(row, rowBandsBuffer[1]);
|
||
169 | Band bufferBandYellow = buffer.getBand(colorInterpretation.getBand(ColorInterpretation.YELLOW_BAND)); |
||
170 | rowBandsBuffer[2] = bufferBandYellow.createRowBuffer();
|
||
171 | bufferBandYellow.fetchRow(row, rowBandsBuffer[2]);
|
||
172 | Band bufferBandBlack = buffer.getBand(colorInterpretation.getBand(ColorInterpretation.BLACK_BAND)); |
||
173 | rowBandsBuffer[3] = bufferBandBlack.createRowBuffer();
|
||
174 | bufferBandBlack.fetchRow(row, rowBandsBuffer[3]);
|
||
175 | |||
176 | 8588 | fdiaz | List<Object> outputRowBuffers = new ArrayList<Object>(); |
177 | 8572 | fdiaz | for (int band = 0; band < 3; band++) { |
178 | Band outputBufferBand = outputBuffer.getBand(band); |
||
179 | 8588 | fdiaz | Object outputRowBuffer = outputBufferBand.createRowBuffer();
|
180 | outputRowBuffers.add(outputRowBuffer); |
||
181 | 8572 | fdiaz | } |
182 | |||
183 | 8588 | fdiaz | processRow(rowBandsBuffer, outputRowBuffers); |
184 | 8572 | fdiaz | |
185 | for (int band = 0; band < 3; band++) { |
||
186 | Band outputBufferBand = outputBuffer.getBand(band); |
||
187 | 8588 | fdiaz | outputBufferBand.putRow(row, outputRowBuffers.get(band)); |
188 | 8572 | fdiaz | } |
189 | |||
190 | } |
||
191 | } catch (Exception e) { |
||
192 | throw new ProcessingOperationException(e); |
||
193 | } |
||
194 | |||
195 | } |
||
196 | |||
197 | @Override
|
||
198 | public void postProcess() throws BufferOperationException { |
||
199 | 8682 | fdiaz | super.postProcess();
|
200 | 8572 | fdiaz | |
201 | } |
||
202 | |||
203 | 8588 | fdiaz | private void processRow(Object[] inputRows, List outputRows) { |
204 | 8572 | fdiaz | ColorManager colorManager = ((RasterLegendManagerServices)RasterLegendLocator.getRasterLegendManager()).getColorManager(); |
205 | byte[][] inputByteRows = new byte[inputRows.length][((byte[]) inputRows[0]).length]; |
||
206 | for (int i = 0; i < inputRows.length; i++) { |
||
207 | inputByteRows[i] = (byte[]) inputRows[i]; |
||
208 | } |
||
209 | int[] convertedValues = new int[3]; |
||
210 | for (int i = 0; i < inputByteRows[0].length; i++) { |
||
211 | |||
212 | 8588 | fdiaz | double[] rgb = colorManager.CMYKtoRGB((0xFF & inputByteRows[0][i])/255D, (0xFF & inputByteRows[1][i])/255D, (0xFF & inputByteRows[2][i])/255D, (0xFF & inputByteRows[3][i])/255D); |
213 | 8572 | fdiaz | |
214 | 8588 | fdiaz | for (int band = 0; band < outputRows.size(); band++) { |
215 | ((byte[])(outputRows.get(band)))[i] = new Double(rgb[band] * 255).byteValue(); |
||
216 | 8572 | fdiaz | } |
217 | } |
||
218 | } |
||
219 | |||
220 | /**
|
||
221 | * @param band
|
||
222 | * @return
|
||
223 | */
|
||
224 | private boolean isProcessableBand(int band) { |
||
225 | return isCMYKBand(band) && this.buffer.getBandTypes()[band] == BufferManager.TYPE_BYTE; |
||
226 | } |
||
227 | |||
228 | private boolean isCMYKBand(int band) { |
||
229 | String bandColorInterpretation = colorInterpretation.get(band);
|
||
230 | return (bandColorInterpretation.equals(ColorInterpretation.CYAN_BAND)
|
||
231 | || bandColorInterpretation.equals(ColorInterpretation.MAGENTA_BAND) |
||
232 | || bandColorInterpretation.equals(ColorInterpretation.YELLOW_BAND) |
||
233 | || bandColorInterpretation.equals(ColorInterpretation.BLACK_BAND)); |
||
234 | } |
||
235 | } |