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 / rgbtohsl / RGBToHSLOperation.java @ 43862
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.rgbtohsl; |
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 RGBToHSLOperation extends AbstractColoredOperation { |
51 |
|
52 |
/**
|
53 |
* @param factory
|
54 |
*
|
55 |
*/
|
56 |
public RGBToHSLOperation(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 |
try {
|
67 |
if (!(getInputColorInterpretation().isGray() || getInputColorInterpretation().isRGB())) {
|
68 |
throw new UnsupportedOperationException( |
69 |
"The color interpretation of input buffer isn't RGB nor GRAYSCALE");
|
70 |
} |
71 |
|
72 |
int[] inputBandTypes = getInputBuffer().getBandTypes(); |
73 |
if (getInputColorInterpretation().isGray()) {
|
74 |
if (inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.GRAY_BAND)] != BufferManager.TYPE_BYTE) {
|
75 |
throw new UnsupportedOperationException("The type of GRAY band isn't BYTE"); |
76 |
} |
77 |
} else if (getInputColorInterpretation().isRGB()) { |
78 |
if (inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.RED_BAND)] != BufferManager.TYPE_BYTE
|
79 |
|| inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.GREEN_BAND)] != BufferManager.TYPE_BYTE |
80 |
|| inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.BLUE_BAND)] != BufferManager.TYPE_BYTE) { |
81 |
throw new UnsupportedOperationException("The type of RGB bands isn't BYTE"); |
82 |
} |
83 |
} else {
|
84 |
throw new UnsupportedOperationException( |
85 |
"The color interpretation of input buffer isn't RGB nor GRAYSCALE");
|
86 |
} |
87 |
|
88 |
int sourceBands = this.getInputBuffer().getBandCount(); |
89 |
NoData[] sourceNoDatas = this.getInputBuffer().getBandNoData(); |
90 |
int[] sourceTypes = this.getInputBuffer().getBandTypes(); |
91 |
|
92 |
List<String> colorInterpretations = new ArrayList<>(); |
93 |
List<NoData> noDatas = new ArrayList<>(); |
94 |
List<Integer> types = new ArrayList<>(); |
95 |
|
96 |
colorInterpretations.add(ColorInterpretation.HUE_BAND); |
97 |
colorInterpretations.add(ColorInterpretation.SATURATION_BAND); |
98 |
colorInterpretations.add(ColorInterpretation.LIGHTNESS_BAND); |
99 |
|
100 |
types.add(BufferManager.TYPE_BYTE); |
101 |
types.add(BufferManager.TYPE_BYTE); |
102 |
types.add(BufferManager.TYPE_BYTE); |
103 |
|
104 |
noDatas.add(null);
|
105 |
noDatas.add(null);
|
106 |
noDatas.add(null);
|
107 |
|
108 |
if (getInputColorInterpretation().hasAlphaBand()) {
|
109 |
int alphaBand = getInputColorInterpretation().getAlphaBand();
|
110 |
if(sourceTypes[alphaBand]!=BufferManager.TYPE_BYTE){
|
111 |
throw new UnsupportedOperationException("The type of ALPHA band isn't BYTE"); |
112 |
} |
113 |
colorInterpretations.add(ColorInterpretation.ALPHA_BAND); |
114 |
types.add(sourceTypes[alphaBand]); |
115 |
noDatas.add(sourceNoDatas[alphaBand]); |
116 |
} |
117 |
|
118 |
if (mustCopyUnprocessedBands()) {
|
119 |
for (int band = 0; band < sourceBands; band++) { |
120 |
if (!isProcessableBand(band) && !getInputColorInterpretation().isAlphaInterpretation(band)) {
|
121 |
colorInterpretations.add(getInputColorInterpretation().get(band)); |
122 |
noDatas.add(sourceNoDatas[band]); |
123 |
types.add(this.getInputBuffer().getBandTypes()[band]);
|
124 |
} |
125 |
} |
126 |
} |
127 |
|
128 |
setOutputColorInterpretation( |
129 |
legendManager.createColorInterpretation(colorInterpretations)); |
130 |
this.setParameter(OUTPUT_COLOR_INTERPRETATION_PARAM, getOutputColorInterpretation());
|
131 |
|
132 |
this.setOutputBuffer(
|
133 |
manager.createBuffer( |
134 |
this.getInputBuffer().getRows(),
|
135 |
this.getInputBuffer().getColumns(),
|
136 |
this.getTypesAsArray(types),
|
137 |
this.getNoDatasAsArray(noDatas),
|
138 |
this.getInputBuffer().getProjection(),
|
139 |
this.getInputBuffer().getEnvelope()));
|
140 |
} catch (LocatorException | BufferException | CreateEnvelopeException e) {
|
141 |
throw new ProcessingOperationException(e); |
142 |
} |
143 |
} |
144 |
|
145 |
@Override
|
146 |
public void process() throws ProcessingOperationException { |
147 |
super.process();
|
148 |
try {
|
149 |
|
150 |
// List<Integer> bandsToProcess = new ArrayList<Integer>();
|
151 |
|
152 |
if (mustCopyUnprocessedBands()) {
|
153 |
int bands = this.getInputBuffer().getBandCount(); |
154 |
int outBand = getInputColorInterpretation().hasAlphaBand() ? 5 : 4; |
155 |
for (int band = 0; band < bands; band++) { |
156 |
if (getInputColorInterpretation().isAlphaInterpretation(band)) {
|
157 |
getOutputBuffer().getBand(getOutputColorInterpretation().getAlphaBand()).copyFrom( |
158 |
this.getInputBuffer().getBand(band));
|
159 |
} else if (!isProcessableBand(band)) { |
160 |
getOutputBuffer().getBand(outBand).copyFrom(this.getInputBuffer().getBand(band));
|
161 |
outBand++; |
162 |
} |
163 |
} |
164 |
} |
165 |
|
166 |
Object[] rowBandsBuffer = new Object[3]; |
167 |
|
168 |
for (int row = 0; row < this.getInputBuffer().getRows(); row++) { |
169 |
if (getInputColorInterpretation().isGray()) {
|
170 |
Band bufferBandGray = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.GRAY_BAND)); |
171 |
rowBandsBuffer[0] = bufferBandGray.createRowBuffer();
|
172 |
bufferBandGray.fetchRow(row, rowBandsBuffer[0]);
|
173 |
rowBandsBuffer[1] = bufferBandGray.createRowBuffer();
|
174 |
bufferBandGray.fetchRow(row, rowBandsBuffer[1]);
|
175 |
rowBandsBuffer[2] = bufferBandGray.createRowBuffer();
|
176 |
bufferBandGray.fetchRow(row, rowBandsBuffer[2]);
|
177 |
} else {
|
178 |
Band bufferBandRed = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.RED_BAND)); |
179 |
rowBandsBuffer[0] = bufferBandRed.createRowBuffer();
|
180 |
bufferBandRed.fetchRow(row, rowBandsBuffer[0]);
|
181 |
Band bufferBandGreen = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.GREEN_BAND)); |
182 |
rowBandsBuffer[1] = bufferBandGreen.createRowBuffer();
|
183 |
bufferBandGreen.fetchRow(row, rowBandsBuffer[1]);
|
184 |
Band bufferBandBlue = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.BLUE_BAND)); |
185 |
rowBandsBuffer[2] = bufferBandBlue.createRowBuffer();
|
186 |
bufferBandBlue.fetchRow(row, rowBandsBuffer[2]);
|
187 |
} |
188 |
|
189 |
List<Object> outputRowBuffers = new ArrayList<>(); |
190 |
for (int band = 0; band < 3; band++) { |
191 |
Band outputBufferBand = getOutputBuffer().getBand(band); |
192 |
Object outputRowBuffer = outputBufferBand.createRowBuffer();
|
193 |
outputRowBuffers.add(outputRowBuffer); |
194 |
} |
195 |
|
196 |
processRow(rowBandsBuffer, outputRowBuffers); |
197 |
|
198 |
for (int band = 0; band < 3; band++) { |
199 |
Band outputBufferBand = getOutputBuffer().getBand(band); |
200 |
outputBufferBand.putRow(row, outputRowBuffers.get(band)); |
201 |
} |
202 |
|
203 |
} |
204 |
} catch (Exception e) { |
205 |
throw new ProcessingOperationException(e); |
206 |
} |
207 |
|
208 |
} |
209 |
|
210 |
@Override
|
211 |
public void postProcess() throws BufferOperationException { |
212 |
super.postProcess();
|
213 |
} |
214 |
|
215 |
private void processRow(Object[] inputRows, List outputRows) { |
216 |
ColorManager colorManager = ((RasterLegendManagerServices)RasterLegendLocator.getRasterLegendManager()).getColorManager(); |
217 |
byte[][] inputByteRows = new byte[inputRows.length][((byte[]) inputRows[0]).length]; |
218 |
for (int i = 0; i < inputRows.length; i++) { |
219 |
inputByteRows[i] = (byte[]) inputRows[i]; |
220 |
} |
221 |
for (int i = 0; i < inputByteRows[0].length; i++) { |
222 |
|
223 |
double[] convertedValues = colorManager.RGBtoHSL(0xFF & inputByteRows[0][i], 0xFF & inputByteRows[1][i], 0xFF & inputByteRows[2][i]); |
224 |
|
225 |
((byte[])(outputRows.get(0)))[i] += new Double((convertedValues[0]*255)/360.0 + 0.5).byteValue(); |
226 |
((byte[])(outputRows.get(1)))[i] += new Double(convertedValues[1]*255+0.5).byteValue(); |
227 |
((byte[])(outputRows.get(2)))[i] += new Double(convertedValues[2]*255+0.5).byteValue(); |
228 |
} |
229 |
} |
230 |
/**
|
231 |
* @param band
|
232 |
* @return
|
233 |
*/
|
234 |
@Override
|
235 |
protected boolean isProcessableBand(int band) { |
236 |
return isRGBorGrayBand(band) && this.getInputBuffer().getBandTypes()[band] == BufferManager.TYPE_BYTE; |
237 |
} |
238 |
|
239 |
private boolean isRGBorGrayBand(int band) { |
240 |
String bandColorInterpretation = getInputColorInterpretation().get(band);
|
241 |
return (bandColorInterpretation.equals(ColorInterpretation.RED_BAND)
|
242 |
|| bandColorInterpretation.equals(ColorInterpretation.GREEN_BAND) |
243 |
|| bandColorInterpretation.equals(ColorInterpretation.BLUE_BAND) || bandColorInterpretation |
244 |
.equals(ColorInterpretation.GRAY_BAND)); |
245 |
} |
246 |
|
247 |
} |