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