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