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 / contrast / ContrastOperation.java @ 43803
History | View | Annotate | Download (9.33 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.contrast; |
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.buffer.spi.operations.AbstractOperation; |
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.spi.AbstractColoredOperation; |
44 |
import org.gvsig.tools.locator.LocatorException; |
45 |
|
46 |
|
47 |
/**
|
48 |
* @author fdiaz
|
49 |
*
|
50 |
*/
|
51 |
public class ContrastOperation extends AbstractColoredOperation{ |
52 |
|
53 |
static public String CONTRAST_PARAM = "contrast"; |
54 |
|
55 |
private int contrast; |
56 |
private RowProcessor rowProcessor;
|
57 |
|
58 |
/**
|
59 |
* @param factory
|
60 |
*
|
61 |
*/
|
62 |
public ContrastOperation(OperationFactory factory) {
|
63 |
this.factory = factory;
|
64 |
} |
65 |
|
66 |
@Override
|
67 |
public void preProcess() throws BufferOperationException { |
68 |
super.preProcess();
|
69 |
BufferManager manager = BufferLocator.getBufferManager(); |
70 |
RasterLegendManager legendManager = RasterLegendLocator.getRasterLegendManager(); |
71 |
|
72 |
contrast = (Integer) this.parameters.getDynValue(CONTRAST_PARAM); |
73 |
|
74 |
int bands = this.buffer.getBandCount(); |
75 |
NoData[] noData;
|
76 |
if (copyUnprocessedBands) {
|
77 |
this.parameters.setDynValue(OUTPUT_COLOR_INTERPRETATION_PARAM, colorInterpretation);
|
78 |
noData = this.buffer.getBandNoData();
|
79 |
try {
|
80 |
this.outputBuffer =
|
81 |
manager.createBuffer(this.buffer.getRows(), this.buffer.getColumns(), this.buffer.getBandTypes(), |
82 |
this.buffer.getBandNoData(), this.buffer.getProjection(), this.buffer.getEnvelope()); |
83 |
} catch (LocatorException | BufferException | CreateEnvelopeException e) {
|
84 |
throw new ProcessingOperationException(e); |
85 |
} |
86 |
} else {
|
87 |
List<String> colorInterpretations = new ArrayList<String>(); |
88 |
List<NoData> noDatas = new ArrayList<NoData>(); |
89 |
List<Integer> types = new ArrayList<Integer>(); |
90 |
for (int band = 0; band < bands; band++) { |
91 |
if (isProcessableBand(band)) {
|
92 |
colorInterpretations.add(colorInterpretation.get(band)); |
93 |
noDatas.add(this.buffer.getBandNoData()[band]);
|
94 |
types.add(this.buffer.getBandTypes()[band]);
|
95 |
} |
96 |
} |
97 |
if (colorInterpretation.hasAlphaBand()) {
|
98 |
colorInterpretations.add(ColorInterpretation.ALPHA_BAND); |
99 |
} |
100 |
outputColorInterpretation = legendManager.createColorInterpretation(colorInterpretations.toArray(new String[0])); |
101 |
this.parameters.setDynValue(OUTPUT_COLOR_INTERPRETATION_PARAM, outputColorInterpretation);
|
102 |
int[] typesInt = new int[types.size()]; |
103 |
for (Iterator iterator = types.iterator(); iterator.hasNext();) { |
104 |
int i = 0; |
105 |
Integer type = (Integer) iterator.next(); |
106 |
typesInt[i] = type.intValue(); |
107 |
} |
108 |
try {
|
109 |
this.outputBuffer =
|
110 |
manager.createBuffer(this.buffer.getRows(), this.buffer.getColumns(), typesInt, |
111 |
noDatas.toArray(new NoData[0]), this.buffer.getProjection(), this.buffer.getEnvelope()); |
112 |
} catch (LocatorException | BufferException | CreateEnvelopeException e) {
|
113 |
throw new ProcessingOperationException(e); |
114 |
} |
115 |
|
116 |
} |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public void process() throws ProcessingOperationException { |
121 |
super.process();
|
122 |
for (int band=0; band<this.buffer.getBandCount(); band++){ |
123 |
rowProcessor = new ByteRowProcessor(band);
|
124 |
if (isProcessableBand(band)) {
|
125 |
Band bufferBand = this.buffer.getBand(band);
|
126 |
Band outputBufferBand = this.outputBuffer.getBand(band);
|
127 |
|
128 |
for (int row = 0; row < this.buffer.getRows(); row++) { |
129 |
Object rowBuffer = bufferBand.createRowBuffer();
|
130 |
bufferBand.fetchRow(row, rowBuffer); |
131 |
|
132 |
Object outputRowBuffer = outputBufferBand.createRowBuffer();
|
133 |
outputBufferBand.fetchRow(row, outputRowBuffer); |
134 |
|
135 |
rowProcessor.processRow(rowBuffer, outputRowBuffer); |
136 |
|
137 |
outputBufferBand.putRow(row, outputRowBuffer); |
138 |
} |
139 |
} else if (copyUnprocessedBands) { |
140 |
try {
|
141 |
this.outputBuffer.getBand(band).copyFrom(this.buffer.getBand(band)); |
142 |
} catch (BandException e) {
|
143 |
throw new ProcessingOperationException(e); |
144 |
} |
145 |
} else if (colorInterpretation.isAlphaInterpretation(band)) { |
146 |
try {
|
147 |
this.outputBuffer.getBand(outputColorInterpretation.getAlphaBand()).copyFrom(this.buffer.getBand(band)); |
148 |
} catch (BandException e) {
|
149 |
throw new ProcessingOperationException(e); |
150 |
} |
151 |
} |
152 |
} |
153 |
} |
154 |
|
155 |
/**
|
156 |
* @param band
|
157 |
* @return
|
158 |
*/
|
159 |
private boolean isProcessableBand(int band) { |
160 |
return isRGBorGrayBand(band) && this.buffer.getBandTypes()[band]==BufferManager.TYPE_BYTE; |
161 |
} |
162 |
|
163 |
private boolean isRGBorGrayBand(int band) { |
164 |
String bandColorInterpretation = colorInterpretation.get(band);
|
165 |
return (bandColorInterpretation.equals(ColorInterpretation.RED_BAND) ||
|
166 |
bandColorInterpretation.equals(ColorInterpretation.GREEN_BAND) || |
167 |
bandColorInterpretation.equals(ColorInterpretation.BLUE_BAND) || |
168 |
bandColorInterpretation.equals(ColorInterpretation.GRAY_BAND)); |
169 |
} |
170 |
|
171 |
@Override
|
172 |
public void postProcess() throws BufferOperationException { |
173 |
super.postProcess();
|
174 |
} |
175 |
|
176 |
interface RowProcessor { |
177 |
void processRow(Object inputRow, Object outputRow); |
178 |
byte processValue(Object value); |
179 |
}; |
180 |
|
181 |
private abstract class AbstractRowProcessor implements RowProcessor { |
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 |
|
199 |
public ByteRowProcessor(int band) { |
200 |
super(band);
|
201 |
} |
202 |
|
203 |
@Override
|
204 |
public void processRow(Object inputRow, Object outputRow) { |
205 |
byte[] inputByteRow = (byte[])inputRow; |
206 |
byte[] outputByteRow = (byte[])outputRow; |
207 |
for (int i = 0; i < inputByteRow.length; i++) { |
208 |
outputByteRow[i] = processValue(inputByteRow[i]); |
209 |
} |
210 |
} |
211 |
|
212 |
@Override
|
213 |
public byte processValue(Object value) { |
214 |
if(noData.isDefined() && noData.getValue().equals(value)){
|
215 |
return (byte)value; |
216 |
} |
217 |
|
218 |
int iValue = 0xFF & ((Byte) value).byteValue(); |
219 |
|
220 |
//FIXME: Copiado del raster viejo. No entiendo los c?lculos
|
221 |
|
222 |
int result;
|
223 |
int diferencia = 127 - iValue; |
224 |
if (contrast >= 0) { |
225 |
result = (int) (iValue - (0.02 * diferencia * contrast)); // ((5.0 * 0.1) / 25) = 0.02 |
226 |
} else {
|
227 |
result = (int) (iValue - (0.004 * diferencia * contrast)); // (0.1 / 25) = 0.004; |
228 |
if (iValue < 127) { |
229 |
if (result > 127) result = 127; |
230 |
} else {
|
231 |
if (result < 127) result = 127; |
232 |
} |
233 |
} |
234 |
|
235 |
if(result>maxResult){
|
236 |
result = maxResult; |
237 |
} |
238 |
if(result<minResult){
|
239 |
result = minResult; |
240 |
} |
241 |
return (byte)result; |
242 |
} |
243 |
} |
244 |
} |