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 / brightness / BrightnessOperation.java @ 8682
History | View | Annotate | Download (8.81 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.brightness; |
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.BandException; |
35 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
36 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferOperationException; |
37 |
import org.gvsig.raster.lib.buffer.api.operations.OperationFactory; |
38 |
import org.gvsig.raster.lib.buffer.spi.exceptions.ProcessingOperationException; |
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.spi.AbstractColoredOperation; |
43 |
import org.gvsig.tools.locator.LocatorException; |
44 |
|
45 |
/**
|
46 |
* @author fdiaz
|
47 |
*
|
48 |
*/
|
49 |
public class BrightnessOperation extends AbstractColoredOperation { |
50 |
|
51 |
static public String BRIGHTNESS_PARAM = "brightness"; |
52 |
static public String OUTPUT_COLOR_INTERPRETATION_PARAM = "output_color_interpretation"; |
53 |
|
54 |
private int brightness; |
55 |
private RowProcessor rowProcessor;
|
56 |
|
57 |
/**
|
58 |
* @param factory
|
59 |
*
|
60 |
*/
|
61 |
public BrightnessOperation(OperationFactory factory) {
|
62 |
this.factory = 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 |
brightness = (Integer) this.parameters.getDynValue(BRIGHTNESS_PARAM); |
72 |
|
73 |
int bands = this.buffer.getBandCount(); |
74 |
NoData[] noData;
|
75 |
if (copyUnprocessedBands) {
|
76 |
this.parameters.setDynValue(OUTPUT_COLOR_INTERPRETATION_PARAM, colorInterpretation);
|
77 |
noData = this.buffer.getBandNoData();
|
78 |
try {
|
79 |
this.outputBuffer =
|
80 |
manager.createBuffer(this.buffer.getRows(), this.buffer.getColumns(), this.buffer.getBandTypes(), |
81 |
this.buffer.getBandNoData(), this.buffer.getProjection(), this.buffer.getEnvelope()); |
82 |
} catch (LocatorException | BufferException | CreateEnvelopeException e) {
|
83 |
throw new ProcessingOperationException(e); |
84 |
} |
85 |
} else {
|
86 |
List<String> colorInterpretations = new ArrayList<String>(); |
87 |
List<NoData> noDatas = new ArrayList<NoData>(); |
88 |
List<Integer> types = new ArrayList<Integer>(); |
89 |
for (int band = 0; band < bands; band++) { |
90 |
if (isProcessableBand(band)) {
|
91 |
colorInterpretations.add(colorInterpretation.get(band)); |
92 |
noDatas.add(this.buffer.getBandNoData()[band]);
|
93 |
types.add(this.buffer.getBandTypes()[band]);
|
94 |
} |
95 |
} |
96 |
if (colorInterpretation.hasAlphaBand()) {
|
97 |
colorInterpretations.add(ColorInterpretation.ALPHA_BAND); |
98 |
} |
99 |
outputColorInterpretation = legendManager.createColorInterpretation(colorInterpretations.toArray(new String[0])); |
100 |
this.parameters.setDynValue(OUTPUT_COLOR_INTERPRETATION_PARAM, outputColorInterpretation);
|
101 |
int[] typesInt = new int[types.size()]; |
102 |
for (Iterator iterator = types.iterator(); iterator.hasNext();) { |
103 |
int i = 0; |
104 |
Integer type = (Integer) iterator.next(); |
105 |
typesInt[i] = type.intValue(); |
106 |
} |
107 |
try {
|
108 |
this.outputBuffer =
|
109 |
manager.createBuffer(this.buffer.getRows(), this.buffer.getColumns(), typesInt, |
110 |
noDatas.toArray(new NoData[0]), this.buffer.getProjection(), this.buffer.getEnvelope()); |
111 |
} catch (LocatorException | BufferException | CreateEnvelopeException e) {
|
112 |
throw new ProcessingOperationException(e); |
113 |
} |
114 |
} |
115 |
} |
116 |
|
117 |
@Override
|
118 |
public void process() throws ProcessingOperationException { |
119 |
super.process();
|
120 |
for (int band = 0; band < this.buffer.getBandCount(); band++) { |
121 |
rowProcessor = new ByteRowProcessor(band);
|
122 |
if (isProcessableBand(band)) {
|
123 |
Band bufferBand = this.buffer.getBand(band);
|
124 |
Band outputBufferBand = this.outputBuffer.getBand(band);
|
125 |
|
126 |
for (int row = 0; row < this.buffer.getRows(); row++) { |
127 |
Object rowBuffer = bufferBand.createRowBuffer();
|
128 |
bufferBand.fetchRow(row, rowBuffer); |
129 |
|
130 |
Object outputRowBuffer = outputBufferBand.createRowBuffer();
|
131 |
|
132 |
rowProcessor.processRow(rowBuffer, outputRowBuffer); |
133 |
|
134 |
outputBufferBand.putRow(row, outputRowBuffer); |
135 |
} |
136 |
} else if (copyUnprocessedBands) { |
137 |
try {
|
138 |
this.outputBuffer.getBand(band).copyFrom(this.buffer.getBand(band)); |
139 |
} catch (BandException e) {
|
140 |
throw new ProcessingOperationException(e); |
141 |
} |
142 |
} else if (colorInterpretation.isAlphaInterpretation(band)) { |
143 |
try {
|
144 |
this.outputBuffer.getBand(outputColorInterpretation.getAlphaBand()).copyFrom(this.buffer.getBand(band)); |
145 |
} catch (BandException e) {
|
146 |
throw new ProcessingOperationException(e); |
147 |
} |
148 |
} |
149 |
} |
150 |
} |
151 |
|
152 |
/**
|
153 |
* @param band
|
154 |
* @return
|
155 |
*/
|
156 |
private boolean isProcessableBand(int band) { |
157 |
return isRGBorGrayBand(band) && this.buffer.getBandTypes()[band] == BufferManager.TYPE_BYTE; |
158 |
} |
159 |
|
160 |
private boolean isRGBorGrayBand(int band) { |
161 |
String bandColorInterpretation = colorInterpretation.get(band);
|
162 |
return (bandColorInterpretation.equals(ColorInterpretation.RED_BAND) ||
|
163 |
bandColorInterpretation.equals(ColorInterpretation.GREEN_BAND) || |
164 |
bandColorInterpretation.equals(ColorInterpretation.BLUE_BAND) || |
165 |
bandColorInterpretation.equals(ColorInterpretation.GRAY_BAND)); |
166 |
} |
167 |
|
168 |
@Override
|
169 |
public void postProcess() throws BufferOperationException { |
170 |
super.postProcess();
|
171 |
} |
172 |
|
173 |
interface RowProcessor { |
174 |
|
175 |
void processRow(Object inputRow, Object outputRow); |
176 |
|
177 |
byte processValue(Object value); |
178 |
}; |
179 |
|
180 |
private abstract class AbstractRowProcessor implements RowProcessor { |
181 |
|
182 |
// int band;
|
183 |
int maxResult = 255; |
184 |
int minResult = 0; |
185 |
NoData noData; |
186 |
|
187 |
public AbstractRowProcessor(int band) { |
188 |
// this.band = band;
|
189 |
noData = buffer.getBand(band).getNoData(); |
190 |
if (noData.isDefined()) {
|
191 |
minResult = (byte) 1; |
192 |
} |
193 |
} |
194 |
} |
195 |
|
196 |
private class ByteRowProcessor extends AbstractRowProcessor { |
197 |
|
198 |
public ByteRowProcessor(int band) { |
199 |
super(band);
|
200 |
} |
201 |
|
202 |
@Override
|
203 |
public void processRow(Object inputRow, Object outputRow) { |
204 |
byte[] inputByteRow = (byte[]) inputRow; |
205 |
byte[] outputByteRow = (byte[]) outputRow; |
206 |
for (int i = 0; i < inputByteRow.length; i++) { |
207 |
outputByteRow[i] = processValue(inputByteRow[i]); |
208 |
} |
209 |
} |
210 |
|
211 |
@Override
|
212 |
public byte processValue(Object value) { |
213 |
if (noData.isDefined() && noData.getValue().equals(value)) {
|
214 |
return (byte) value; |
215 |
} |
216 |
|
217 |
int iValue = 0xFF & ((Byte) value).byteValue(); |
218 |
int result = iValue + brightness;
|
219 |
if (result > maxResult) {
|
220 |
result = maxResult; |
221 |
} |
222 |
if (result < minResult) {
|
223 |
result = minResult; |
224 |
} |
225 |
return (byte) result; |
226 |
} |
227 |
} |
228 |
} |