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 / cmyktorgb / CMYKToRGBOperation.java @ 43862
History | View | Annotate | Download (10.6 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.cmyktorgb; |
24 |
|
25 |
import java.util.ArrayList; |
26 |
import java.util.List; |
27 |
|
28 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
29 |
import org.gvsig.raster.lib.buffer.api.Band; |
30 |
import org.gvsig.raster.lib.buffer.api.BufferLocator; |
31 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
32 |
import org.gvsig.raster.lib.buffer.api.NoData; |
33 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
34 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferOperationException; |
35 |
import org.gvsig.raster.lib.buffer.api.operations.OperationFactory; |
36 |
import org.gvsig.raster.lib.buffer.spi.exceptions.ProcessingOperationException; |
37 |
import org.gvsig.raster.lib.legend.api.RasterLegendLocator; |
38 |
import org.gvsig.raster.lib.legend.api.RasterLegendManager; |
39 |
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation; |
40 |
import org.gvsig.raster.lib.legend.impl.ColorManager; |
41 |
import org.gvsig.raster.lib.legend.impl.RasterLegendManagerServices; |
42 |
import org.gvsig.raster.lib.legend.spi.AbstractColoredOperation; |
43 |
import org.gvsig.tools.locator.LocatorException; |
44 |
|
45 |
|
46 |
/**
|
47 |
* @author fdiaz
|
48 |
*
|
49 |
*/
|
50 |
public class CMYKToRGBOperation extends AbstractColoredOperation { |
51 |
|
52 |
/**
|
53 |
* @param factory
|
54 |
*
|
55 |
*/
|
56 |
public CMYKToRGBOperation(OperationFactory factory) {
|
57 |
super(factory);
|
58 |
} |
59 |
|
60 |
@Override
|
61 |
public void preProcess() throws BufferOperationException { |
62 |
super.preProcess();
|
63 |
BufferManager manager = BufferLocator.getBufferManager(); |
64 |
RasterLegendManager legendManager = RasterLegendLocator.getRasterLegendManager(); |
65 |
|
66 |
|
67 |
try {
|
68 |
if (!this.getInputColorInterpretation().isCMYK()) { |
69 |
throw new UnsupportedOperationException("The color interpretation of input buffer isn't CMYK"); |
70 |
} |
71 |
|
72 |
int[] inputBandTypes = getInputBuffer().getBandTypes(); |
73 |
if (inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.CYAN_BAND)] != BufferManager.TYPE_BYTE
|
74 |
|| inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.MAGENTA_BAND)] != BufferManager.TYPE_BYTE |
75 |
|| inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.YELLOW_BAND)] != BufferManager.TYPE_BYTE |
76 |
|| inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.BLACK_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<>(); |
85 |
List<NoData> noDatas = new ArrayList<>(); |
86 |
List<Integer> types = new ArrayList<>(); |
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(ColorInterpretation.UNDEFINED_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(
|
123 |
OUTPUT_COLOR_INTERPRETATION_PARAM, |
124 |
getOutputColorInterpretation()); |
125 |
|
126 |
|
127 |
this.setOutputBuffer(
|
128 |
manager.createBuffer( |
129 |
this.getInputBuffer().getRows(),
|
130 |
this.getInputBuffer().getColumns(),
|
131 |
this.getTypesAsArray(types),
|
132 |
this.getNoDatasAsArray(noDatas),
|
133 |
this.getInputBuffer().getProjection(),
|
134 |
this.getInputBuffer().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 |
super.process();
|
144 |
// List<Integer> bandsToProcess = new ArrayList<Integer>();
|
145 |
|
146 |
if (mustCopyUnprocessedBands()) {
|
147 |
int bands = this.getInputBuffer().getBandCount(); |
148 |
int outBand = getInputColorInterpretation().hasAlphaBand() ? 4 : 3; |
149 |
for (int band = 0; band < bands; band++) { |
150 |
if (getInputColorInterpretation().isAlphaInterpretation(band)) {
|
151 |
getOutputBuffer().getBand(getInputColorInterpretation().getAlphaBand()).copyFrom( |
152 |
this.getInputBuffer().getBand(band));
|
153 |
} else if (!isProcessableBand(band)) { |
154 |
getOutputBuffer().getBand(outBand).copyFrom(this.getInputBuffer().getBand(band));
|
155 |
outBand++; |
156 |
} |
157 |
} |
158 |
} |
159 |
|
160 |
Object[] rowBandsBuffer = new Object[4]; |
161 |
|
162 |
for (int row = 0; row < this.getInputBuffer().getRows(); row++) { |
163 |
Band bufferBandCyan = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.CYAN_BAND)); |
164 |
rowBandsBuffer[0] = bufferBandCyan.createRowBuffer();
|
165 |
bufferBandCyan.fetchRow(row, rowBandsBuffer[0]);
|
166 |
Band bufferBandMagenta = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.MAGENTA_BAND)); |
167 |
rowBandsBuffer[1] = bufferBandMagenta.createRowBuffer();
|
168 |
bufferBandMagenta.fetchRow(row, rowBandsBuffer[1]);
|
169 |
Band bufferBandYellow = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.YELLOW_BAND)); |
170 |
rowBandsBuffer[2] = bufferBandYellow.createRowBuffer();
|
171 |
bufferBandYellow.fetchRow(row, rowBandsBuffer[2]);
|
172 |
Band bufferBandBlack = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.BLACK_BAND)); |
173 |
rowBandsBuffer[3] = bufferBandBlack.createRowBuffer();
|
174 |
bufferBandBlack.fetchRow(row, rowBandsBuffer[3]);
|
175 |
|
176 |
List<Object> outputRowBuffers = new ArrayList<>(); |
177 |
for (int band = 0; band < 3; band++) { |
178 |
Band outputBufferBand = getOutputBuffer().getBand(band); |
179 |
Object outputRowBuffer = outputBufferBand.createRowBuffer();
|
180 |
outputRowBuffers.add(outputRowBuffer); |
181 |
} |
182 |
|
183 |
processRow(rowBandsBuffer, outputRowBuffers); |
184 |
|
185 |
for (int band = 0; band < 3; band++) { |
186 |
Band outputBufferBand = getOutputBuffer().getBand(band); |
187 |
outputBufferBand.putRow(row, outputRowBuffers.get(band)); |
188 |
} |
189 |
|
190 |
} |
191 |
} catch (Exception e) { |
192 |
throw new ProcessingOperationException(e); |
193 |
} |
194 |
|
195 |
} |
196 |
|
197 |
@Override
|
198 |
public void postProcess() throws BufferOperationException { |
199 |
super.postProcess();
|
200 |
|
201 |
} |
202 |
|
203 |
private void processRow(Object[] inputRows, List outputRows) { |
204 |
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 |
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 |
|
214 |
for (int band = 0; band < outputRows.size(); band++) { |
215 |
((byte[])(outputRows.get(band)))[i] = new Double(rgb[band] * 255).byteValue(); |
216 |
} |
217 |
} |
218 |
} |
219 |
|
220 |
/**
|
221 |
* @param band
|
222 |
* @return
|
223 |
*/
|
224 |
@Override
|
225 |
protected boolean isProcessableBand(int band) { |
226 |
return isCMYKBand(band) && this.getInputBuffer().getBandTypes()[band] == BufferManager.TYPE_BYTE; |
227 |
} |
228 |
|
229 |
private boolean isCMYKBand(int band) { |
230 |
String bandColorInterpretation = getInputColorInterpretation().get(band);
|
231 |
return (bandColorInterpretation.equals(ColorInterpretation.CYAN_BAND)
|
232 |
|| bandColorInterpretation.equals(ColorInterpretation.MAGENTA_BAND) |
233 |
|| bandColorInterpretation.equals(ColorInterpretation.YELLOW_BAND) |
234 |
|| bandColorInterpretation.equals(ColorInterpretation.BLACK_BAND)); |
235 |
} |
236 |
} |