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 @ 43862
History | View | Annotate | Download (11.7 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.List; |
27 |
|
28 |
import org.slf4j.Logger; |
29 |
import org.slf4j.LoggerFactory; |
30 |
|
31 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
32 |
import org.gvsig.raster.lib.buffer.api.Band; |
33 |
import org.gvsig.raster.lib.buffer.api.BufferLocator; |
34 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
35 |
import org.gvsig.raster.lib.buffer.api.NoData; |
36 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
37 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferOperationException; |
38 |
import org.gvsig.raster.lib.buffer.api.operations.OperationFactory; |
39 |
import org.gvsig.raster.lib.buffer.spi.exceptions.ProcessingOperationException; |
40 |
import org.gvsig.raster.lib.legend.api.RasterLegendLocator; |
41 |
import org.gvsig.raster.lib.legend.api.RasterLegendManager; |
42 |
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation; |
43 |
import org.gvsig.raster.lib.legend.impl.ColorManager; |
44 |
import org.gvsig.raster.lib.legend.impl.RasterLegendManagerServices; |
45 |
import org.gvsig.raster.lib.legend.spi.AbstractColoredOperation; |
46 |
import org.gvsig.tools.locator.LocatorException; |
47 |
|
48 |
|
49 |
/**
|
50 |
* @author fdiaz
|
51 |
*
|
52 |
*/
|
53 |
public class RGBToCMYKOperation extends AbstractColoredOperation { |
54 |
|
55 |
private static final Logger LOG = LoggerFactory.getLogger(RGBToCMYKOperation.class); |
56 |
|
57 |
/**
|
58 |
* @param factory
|
59 |
*
|
60 |
*/
|
61 |
public RGBToCMYKOperation(OperationFactory factory) {
|
62 |
super(factory);
|
63 |
} |
64 |
|
65 |
@Override
|
66 |
public void preProcess() throws BufferOperationException { |
67 |
super.preProcess();
|
68 |
BufferManager manager = BufferLocator.getBufferManager(); |
69 |
RasterLegendManager legendManager = RasterLegendLocator.getRasterLegendManager(); |
70 |
|
71 |
try {
|
72 |
if (!(getInputColorInterpretation().isGray() || getInputColorInterpretation().isRGB())) {
|
73 |
throw new UnsupportedOperationException( |
74 |
"The color interpretation of input buffer isn't RGB nor GRAYSCALE");
|
75 |
} |
76 |
|
77 |
int[] inputBandTypes = getInputBuffer().getBandTypes(); |
78 |
if (getInputColorInterpretation().isGray()) {
|
79 |
if (inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.GRAY_BAND)] != BufferManager.TYPE_BYTE) {
|
80 |
throw new UnsupportedOperationException("The type of GRAY band isn't BYTE"); |
81 |
} |
82 |
} else if (getInputColorInterpretation().isRGB()) { |
83 |
if (inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.RED_BAND)] != BufferManager.TYPE_BYTE
|
84 |
|| inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.GREEN_BAND)] != BufferManager.TYPE_BYTE |
85 |
|| inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.BLUE_BAND)] != BufferManager.TYPE_BYTE) { |
86 |
throw new UnsupportedOperationException("The type of RGB bands isn't BYTE"); |
87 |
} |
88 |
} else {
|
89 |
throw new UnsupportedOperationException( |
90 |
"The color interpretation of input buffer isn't RGB nor GRAYSCALE");
|
91 |
} |
92 |
|
93 |
int sourceBands = this.getInputBuffer().getBandCount(); |
94 |
NoData[] sourceNoDatas = this.getInputBuffer().getBandNoData(); |
95 |
int[] sourceTypes = this.getInputBuffer().getBandTypes(); |
96 |
|
97 |
List<String> colorInterpretations = new ArrayList<>(); |
98 |
List<NoData> noDatas = new ArrayList<>(); |
99 |
List<Integer> types = new ArrayList<>(); |
100 |
|
101 |
colorInterpretations.add(ColorInterpretation.CYAN_BAND); |
102 |
colorInterpretations.add(ColorInterpretation.MAGENTA_BAND); |
103 |
colorInterpretations.add(ColorInterpretation.YELLOW_BAND); |
104 |
colorInterpretations.add(ColorInterpretation.BLACK_BAND); |
105 |
|
106 |
types.add(BufferManager.TYPE_BYTE); |
107 |
types.add(BufferManager.TYPE_BYTE); |
108 |
types.add(BufferManager.TYPE_BYTE); |
109 |
types.add(BufferManager.TYPE_BYTE); |
110 |
|
111 |
noDatas.add(null);
|
112 |
noDatas.add(null);
|
113 |
noDatas.add(null);
|
114 |
noDatas.add(null);
|
115 |
|
116 |
if (getInputColorInterpretation().hasAlphaBand()) {
|
117 |
int alphaBand = getInputColorInterpretation().getAlphaBand();
|
118 |
if(sourceTypes[alphaBand]!=BufferManager.TYPE_BYTE){
|
119 |
throw new UnsupportedOperationException("The type of ALPHA band isn't BYTE"); |
120 |
} |
121 |
colorInterpretations.add(ColorInterpretation.ALPHA_BAND); |
122 |
types.add(sourceTypes[alphaBand]); |
123 |
noDatas.add(sourceNoDatas[alphaBand]); |
124 |
} |
125 |
|
126 |
if (mustCopyUnprocessedBands()) {
|
127 |
for (int band = 0; band < sourceBands; band++) { |
128 |
if (!isProcessableBand(band) && !getInputColorInterpretation().isAlphaInterpretation(band)) {
|
129 |
colorInterpretations.add(ColorInterpretation.UNDEFINED_BAND); |
130 |
noDatas.add(sourceNoDatas[band]); |
131 |
types.add(this.getInputBuffer().getBandTypes()[band]);
|
132 |
} |
133 |
} |
134 |
} |
135 |
|
136 |
setOutputColorInterpretation( |
137 |
legendManager.createColorInterpretation(colorInterpretations)); |
138 |
this.setParameter(OUTPUT_COLOR_INTERPRETATION_PARAM, getOutputColorInterpretation());
|
139 |
|
140 |
this.setOutputBuffer(
|
141 |
manager.createBuffer( |
142 |
this.getInputBuffer().getRows(),
|
143 |
this.getInputBuffer().getColumns(),
|
144 |
this.getTypesAsArray(types),
|
145 |
this.getNoDatasAsArray(noDatas),
|
146 |
this.getInputBuffer().getProjection(),
|
147 |
this.getInputBuffer().getEnvelope()));
|
148 |
} catch (LocatorException | BufferException | CreateEnvelopeException e) {
|
149 |
throw new ProcessingOperationException(e); |
150 |
} |
151 |
} |
152 |
|
153 |
@Override
|
154 |
public void process() throws ProcessingOperationException { |
155 |
super.process();
|
156 |
try {
|
157 |
|
158 |
// List<Integer> bandsToProcess = new ArrayList<Integer>();
|
159 |
|
160 |
if (mustCopyUnprocessedBands()) {
|
161 |
int bands = this.getInputBuffer().getBandCount(); |
162 |
int outBand = getInputColorInterpretation().hasAlphaBand() ? 5 : 4; |
163 |
for (int band = 0; band < bands; band++) { |
164 |
if (getInputColorInterpretation().isAlphaInterpretation(band)) {
|
165 |
getOutputBuffer().getBand(getOutputColorInterpretation().getAlphaBand()).copyFrom( |
166 |
this.getInputBuffer().getBand(band));
|
167 |
} else if (!isProcessableBand(band)) { |
168 |
getOutputBuffer().getBand(outBand).copyFrom(this.getInputBuffer().getBand(band));
|
169 |
outBand++; |
170 |
} |
171 |
} |
172 |
} |
173 |
|
174 |
Object[] rowBandsBuffer = new Object[3]; |
175 |
|
176 |
for (int row = 0; row < this.getInputBuffer().getRows(); row++) { |
177 |
if (getInputColorInterpretation().isGray()) {
|
178 |
Band bufferBandGray = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.GRAY_BAND)); |
179 |
rowBandsBuffer[0] = bufferBandGray.createRowBuffer();
|
180 |
bufferBandGray.fetchRow(row, rowBandsBuffer[0]);
|
181 |
rowBandsBuffer[1] = bufferBandGray.createRowBuffer();
|
182 |
bufferBandGray.fetchRow(row, rowBandsBuffer[1]);
|
183 |
rowBandsBuffer[2] = bufferBandGray.createRowBuffer();
|
184 |
bufferBandGray.fetchRow(row, rowBandsBuffer[2]);
|
185 |
} else {
|
186 |
Band bufferBandRed = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.RED_BAND)); |
187 |
rowBandsBuffer[0] = bufferBandRed.createRowBuffer();
|
188 |
bufferBandRed.fetchRow(row, rowBandsBuffer[0]);
|
189 |
Band bufferBandGreen = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.GREEN_BAND)); |
190 |
rowBandsBuffer[1] = bufferBandGreen.createRowBuffer();
|
191 |
bufferBandGreen.fetchRow(row, rowBandsBuffer[1]);
|
192 |
Band bufferBandBlue = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.BLUE_BAND)); |
193 |
rowBandsBuffer[2] = bufferBandBlue.createRowBuffer();
|
194 |
bufferBandBlue.fetchRow(row, rowBandsBuffer[2]);
|
195 |
} |
196 |
|
197 |
List<Object> outputRowBuffers = new ArrayList<Object>(); |
198 |
for (int band = 0; band < 4; band++) { |
199 |
Band outputBufferBand = getOutputBuffer().getBand(band); |
200 |
Object outputRowBuffer = outputBufferBand.createRowBuffer();
|
201 |
outputRowBuffers.add(outputRowBuffer); |
202 |
} |
203 |
|
204 |
processRow(rowBandsBuffer, outputRowBuffers); |
205 |
|
206 |
for (int band = 0; band < 4; band++) { |
207 |
Band outputBufferBand = getOutputBuffer().getBand(band); |
208 |
outputBufferBand.putRow(row, outputRowBuffers.get(band)); |
209 |
} |
210 |
|
211 |
} |
212 |
} catch (Exception e) { |
213 |
throw new ProcessingOperationException(e); |
214 |
} |
215 |
|
216 |
} |
217 |
|
218 |
@Override
|
219 |
public void postProcess() throws BufferOperationException { |
220 |
super.postProcess();
|
221 |
} |
222 |
|
223 |
private void processRow(Object[] inputRows, List outputRows) { |
224 |
ColorManager colorManager = ((RasterLegendManagerServices)RasterLegendLocator.getRasterLegendManager()).getColorManager(); |
225 |
byte[][] inputByteRows = new byte[inputRows.length][((byte[]) inputRows[0]).length]; |
226 |
for (int i = 0; i < inputRows.length; i++) { |
227 |
inputByteRows[i] = (byte[]) inputRows[i]; |
228 |
} |
229 |
|
230 |
for (int i = 0; i < inputByteRows[0].length; i++) { |
231 |
|
232 |
double[] convertedValues = colorManager.RGBtoCMYK(0xFF & inputByteRows[0][i], 0xFF & inputByteRows[1][i], 0xFF & inputByteRows[2][i], 255); |
233 |
|
234 |
for (int band = 0; band < outputRows.size(); band++) { |
235 |
((byte[])(outputRows.get(band)))[i] = new Double(convertedValues[band]).byteValue(); |
236 |
} |
237 |
} |
238 |
} |
239 |
|
240 |
/**
|
241 |
* @param band
|
242 |
* @return
|
243 |
*/
|
244 |
@Override
|
245 |
protected boolean isProcessableBand(int band) { |
246 |
return isRGBorGrayBand(band) && this.getInputBuffer().getBandTypes()[band] == BufferManager.TYPE_BYTE; |
247 |
} |
248 |
|
249 |
private boolean isRGBorGrayBand(int band) { |
250 |
String bandColorInterpretation = getInputColorInterpretation().get(band);
|
251 |
return (bandColorInterpretation.equals(ColorInterpretation.RED_BAND)
|
252 |
|| bandColorInterpretation.equals(ColorInterpretation.GREEN_BAND) |
253 |
|| bandColorInterpretation.equals(ColorInterpretation.BLUE_BAND) || bandColorInterpretation |
254 |
.equals(ColorInterpretation.GRAY_BAND)); |
255 |
} |
256 |
} |