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 / pansharpening / PansharpeningOperation.java @ 43830
History | View | Annotate | Download (6.34 KB)
1 | 43803 | fdiaz | /* 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.pansharpening; |
||
24 | |||
25 | import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
||
26 | import org.gvsig.raster.lib.buffer.api.Band; |
||
27 | import org.gvsig.raster.lib.buffer.api.BufferLocator; |
||
28 | import org.gvsig.raster.lib.buffer.api.BufferManager; |
||
29 | import org.gvsig.raster.lib.buffer.api.NoData; |
||
30 | import org.gvsig.raster.lib.buffer.api.exceptions.BandException; |
||
31 | import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
||
32 | import org.gvsig.raster.lib.buffer.api.exceptions.BufferOperationException; |
||
33 | import org.gvsig.raster.lib.buffer.api.operations.OperationFactory; |
||
34 | import org.gvsig.raster.lib.buffer.api.statistics.Statistics; |
||
35 | import org.gvsig.raster.lib.buffer.spi.exceptions.ProcessingOperationException; |
||
36 | import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation; |
||
37 | import org.gvsig.raster.lib.legend.spi.AbstractColoredOperation; |
||
38 | import org.gvsig.tools.locator.LocatorException; |
||
39 | |||
40 | |||
41 | /**
|
||
42 | * @author fdiaz
|
||
43 | *
|
||
44 | * @deprecated
|
||
45 | * Possibly this class will be deleted
|
||
46 | */
|
||
47 | @Deprecated
|
||
48 | public class PansharpeningOperation extends AbstractColoredOperation{ |
||
49 | |||
50 | static public String STATISTICS_PARAM = "statistics"; |
||
51 | |||
52 | private Statistics statistics;
|
||
53 | private RowProcessor rowProcessor;
|
||
54 | |||
55 | /**
|
||
56 | * @param factory
|
||
57 | *
|
||
58 | */
|
||
59 | public PansharpeningOperation(OperationFactory factory) {
|
||
60 | this.factory = factory;
|
||
61 | } |
||
62 | |||
63 | @Override
|
||
64 | public void preProcess() throws BufferOperationException { |
||
65 | super.preProcess();
|
||
66 | BufferManager manager = BufferLocator.getBufferManager(); |
||
67 | |||
68 | statistics = (Statistics)this.parameters.getDynValue(STATISTICS_PARAM);
|
||
69 | |||
70 | int bands = this.buffer.getBandCount(); |
||
71 | NoData[] noData = this.buffer.getBandNoData(); |
||
72 | |||
73 | try {
|
||
74 | this.outputBuffer = manager.createBuffer(
|
||
75 | this.buffer.getRows(),
|
||
76 | this.buffer.getColumns(),
|
||
77 | this.buffer.getBandTypes(),
|
||
78 | this.buffer.getBandNoData(),
|
||
79 | this.buffer.getProjection(),
|
||
80 | this.buffer.getEnvelope());
|
||
81 | } catch (LocatorException | BufferException | CreateEnvelopeException e) {
|
||
82 | throw new ProcessingOperationException(e); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | @Override
|
||
87 | public void process() throws ProcessingOperationException { |
||
88 | super.process();
|
||
89 | for (int band=0; band<this.buffer.getBandCount(); band++){ |
||
90 | rowProcessor = new ByteRowProcessor(band);
|
||
91 | if (isRGBorGrayBand(band)) {
|
||
92 | Band bufferBand = this.buffer.getBand(band);
|
||
93 | Band outputBufferBand = this.outputBuffer.getBand(band);
|
||
94 | |||
95 | for (int row = 0; row < this.buffer.getRows(); row++) { |
||
96 | Object rowBuffer = bufferBand.createRowBuffer();
|
||
97 | bufferBand.fetchRow(row, rowBuffer); |
||
98 | |||
99 | Object outputRowBuffer = outputBufferBand.createRowBuffer();
|
||
100 | outputBufferBand.fetchRow(row, outputRowBuffer); |
||
101 | |||
102 | rowProcessor.processRow(rowBuffer, outputRowBuffer); |
||
103 | |||
104 | outputBufferBand.putRow(row, outputRowBuffer); |
||
105 | } |
||
106 | } else {
|
||
107 | try {
|
||
108 | this.outputBuffer.getBand(band).copyFrom(this.buffer.getBand(band)); |
||
109 | } catch (BandException e) {
|
||
110 | throw new ProcessingOperationException(e); |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | |||
116 | private boolean isRGBorGrayBand(int band) { |
||
117 | String bandColorInterpretation = colorInterpretation.get(band);
|
||
118 | return (bandColorInterpretation.equals(ColorInterpretation.RED_BAND) ||
|
||
119 | bandColorInterpretation.equals(ColorInterpretation.GREEN_BAND) || |
||
120 | bandColorInterpretation.equals(ColorInterpretation.BLUE_BAND) || |
||
121 | bandColorInterpretation.equals(ColorInterpretation.GRAY_BAND)); |
||
122 | } |
||
123 | |||
124 | @Override
|
||
125 | public void postProcess() throws BufferOperationException { |
||
126 | super.postProcess();
|
||
127 | } |
||
128 | |||
129 | |||
130 | |||
131 | interface RowProcessor { |
||
132 | void processRow(Object inputRow, Object outputRow); |
||
133 | byte processValue(Object value); |
||
134 | }; |
||
135 | |||
136 | private abstract class AbstractRowProcessor implements RowProcessor { |
||
137 | // int band;
|
||
138 | int maxResult = 255; |
||
139 | int minResult = 0; |
||
140 | NoData noData; |
||
141 | |||
142 | public AbstractRowProcessor(int band) { |
||
143 | // this.band = band;
|
||
144 | noData = buffer.getBand(band).getNoData(); |
||
145 | if(noData.isDefined()) {
|
||
146 | minResult = (byte)1; |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | |||
151 | private class ByteRowProcessor extends AbstractRowProcessor { |
||
152 | |||
153 | |||
154 | public ByteRowProcessor(int band) { |
||
155 | super(band);
|
||
156 | } |
||
157 | |||
158 | @Override
|
||
159 | public void processRow(Object inputRow, Object outputRow) { |
||
160 | byte[] inputByteRow = (byte[])inputRow; |
||
161 | byte[] outputByteRow = (byte[])outputRow; |
||
162 | for (int i = 0; i < inputByteRow.length; i++) { |
||
163 | outputByteRow[i] = processValue(inputByteRow[i]); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | @Override
|
||
168 | public byte processValue(Object value) { |
||
169 | if(noData.isDefined() && noData.getValue().equals(value)){
|
||
170 | return (byte)value; |
||
171 | } |
||
172 | |||
173 | int iValue = 0xFF & ((Byte) value).byteValue(); |
||
174 | int result = iValue; // + brightness; |
||
175 | if(result>maxResult){
|
||
176 | result = maxResult; |
||
177 | } |
||
178 | if(result<minResult){
|
||
179 | result = minResult; |
||
180 | } |
||
181 | return (byte)result; |
||
182 | } |
||
183 | } |
||
184 | } |