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 @ 43862
History | View | Annotate | Download (6.54 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 | 43862 | jjdelcerro | super(factory);
|
61 | 43803 | fdiaz | } |
62 | |||
63 | @Override
|
||
64 | public void preProcess() throws BufferOperationException { |
||
65 | super.preProcess();
|
||
66 | BufferManager manager = BufferLocator.getBufferManager(); |
||
67 | |||
68 | 43862 | jjdelcerro | statistics = (Statistics)this.getParameter(STATISTICS_PARAM,null); |
69 | 43803 | fdiaz | |
70 | 43862 | jjdelcerro | // int bands = this.getInputBuffer().getBandCount();
|
71 | // NoData[] noData = this.getInputBuffer().getBandNoData();
|
||
72 | 43803 | fdiaz | |
73 | try {
|
||
74 | 43862 | jjdelcerro | this.setOutputBuffer(
|
75 | manager.createBuffer( |
||
76 | this.getInputBuffer().getRows(),
|
||
77 | this.getInputBuffer().getColumns(),
|
||
78 | this.getInputBuffer().getBandTypes(),
|
||
79 | this.getInputBuffer().getBandNoData(),
|
||
80 | this.getInputBuffer().getProjection(),
|
||
81 | this.getInputBuffer().getEnvelope()));
|
||
82 | 43803 | fdiaz | } catch (LocatorException | BufferException | CreateEnvelopeException e) {
|
83 | throw new ProcessingOperationException(e); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | @Override
|
||
88 | public void process() throws ProcessingOperationException { |
||
89 | super.process();
|
||
90 | 43862 | jjdelcerro | for (int band=0; band<this.getInputBuffer().getBandCount(); band++){ |
91 | 43803 | fdiaz | rowProcessor = new ByteRowProcessor(band);
|
92 | if (isRGBorGrayBand(band)) {
|
||
93 | 43862 | jjdelcerro | Band bufferBand = this.getInputBuffer().getBand(band);
|
94 | Band outputBufferBand = this.getOutputBuffer().getBand(band);
|
||
95 | 43803 | fdiaz | |
96 | 43862 | jjdelcerro | for (int row = 0; row < this.getInputBuffer().getRows(); row++) { |
97 | 43803 | fdiaz | Object rowBuffer = bufferBand.createRowBuffer();
|
98 | bufferBand.fetchRow(row, rowBuffer); |
||
99 | |||
100 | Object outputRowBuffer = outputBufferBand.createRowBuffer();
|
||
101 | outputBufferBand.fetchRow(row, outputRowBuffer); |
||
102 | |||
103 | rowProcessor.processRow(rowBuffer, outputRowBuffer); |
||
104 | |||
105 | outputBufferBand.putRow(row, outputRowBuffer); |
||
106 | } |
||
107 | } else {
|
||
108 | try {
|
||
109 | 43862 | jjdelcerro | this.getOutputBuffer().getBand(band).copyFrom(this.getInputBuffer().getBand(band)); |
110 | 43803 | fdiaz | } catch (BandException e) {
|
111 | throw new ProcessingOperationException(e); |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | private boolean isRGBorGrayBand(int band) { |
||
118 | 43862 | jjdelcerro | String bandColorInterpretation = getInputColorInterpretation().get(band);
|
119 | 43803 | fdiaz | return (bandColorInterpretation.equals(ColorInterpretation.RED_BAND) ||
|
120 | bandColorInterpretation.equals(ColorInterpretation.GREEN_BAND) || |
||
121 | bandColorInterpretation.equals(ColorInterpretation.BLUE_BAND) || |
||
122 | bandColorInterpretation.equals(ColorInterpretation.GRAY_BAND)); |
||
123 | } |
||
124 | |||
125 | @Override
|
||
126 | public void postProcess() throws BufferOperationException { |
||
127 | super.postProcess();
|
||
128 | } |
||
129 | |||
130 | |||
131 | |||
132 | interface RowProcessor { |
||
133 | void processRow(Object inputRow, Object outputRow); |
||
134 | byte processValue(Object value); |
||
135 | }; |
||
136 | |||
137 | private abstract class AbstractRowProcessor implements RowProcessor { |
||
138 | // int band;
|
||
139 | int maxResult = 255; |
||
140 | int minResult = 0; |
||
141 | NoData noData; |
||
142 | |||
143 | public AbstractRowProcessor(int band) { |
||
144 | // this.band = band;
|
||
145 | 43862 | jjdelcerro | noData = getInputBuffer().getBand(band).getNoData(); |
146 | 43803 | fdiaz | if(noData.isDefined()) {
|
147 | minResult = (byte)1; |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | |||
152 | private class ByteRowProcessor extends AbstractRowProcessor { |
||
153 | |||
154 | |||
155 | public ByteRowProcessor(int band) { |
||
156 | super(band);
|
||
157 | } |
||
158 | |||
159 | @Override
|
||
160 | public void processRow(Object inputRow, Object outputRow) { |
||
161 | byte[] inputByteRow = (byte[])inputRow; |
||
162 | byte[] outputByteRow = (byte[])outputRow; |
||
163 | for (int i = 0; i < inputByteRow.length; i++) { |
||
164 | outputByteRow[i] = processValue(inputByteRow[i]); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | @Override
|
||
169 | public byte processValue(Object value) { |
||
170 | if(noData.isDefined() && noData.getValue().equals(value)){
|
||
171 | return (byte)value; |
||
172 | } |
||
173 | |||
174 | 43862 | jjdelcerro | int iValue = 0xFF & ((Byte) value); |
175 | 43803 | fdiaz | int result = iValue; // + brightness; |
176 | if(result>maxResult){
|
||
177 | result = maxResult; |
||
178 | } |
||
179 | if(result<minResult){
|
||
180 | result = minResult; |
||
181 | } |
||
182 | return (byte)result; |
||
183 | } |
||
184 | } |
||
185 | } |