svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.impl / src / main / java / org / gvsig / fmap / raster / lib / legend / impl / operations / colorbalancergb / AbstractColorBalanceOperation.java @ 44831
History | View | Annotate | Download (10.2 KB)
1 | 44831 | jjdelcerro | /* 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.colorbalancergb; |
||
24 | |||
25 | import java.util.ArrayList; |
||
26 | import java.util.List; |
||
27 | |||
28 | import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
||
29 | import org.gvsig.raster.lib.buffer.api.Band; |
||
30 | import org.gvsig.raster.lib.buffer.api.BufferLocator; |
||
31 | import org.gvsig.raster.lib.buffer.api.BufferManager; |
||
32 | import org.gvsig.raster.lib.buffer.api.NoData; |
||
33 | import org.gvsig.raster.lib.buffer.api.exceptions.BandException; |
||
34 | import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
||
35 | import org.gvsig.raster.lib.buffer.api.exceptions.BufferOperationException; |
||
36 | import org.gvsig.raster.lib.buffer.api.operations.OperationFactory; |
||
37 | import org.gvsig.raster.lib.buffer.spi.exceptions.ProcessingOperationException; |
||
38 | import org.gvsig.raster.lib.legend.api.RasterLegendLocator; |
||
39 | import org.gvsig.raster.lib.legend.api.RasterLegendManager; |
||
40 | import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation; |
||
41 | import org.gvsig.raster.lib.legend.spi.AbstractColoredOperation; |
||
42 | import org.gvsig.tools.locator.LocatorException; |
||
43 | |||
44 | |||
45 | /**
|
||
46 | * @author fdiaz
|
||
47 | *
|
||
48 | */
|
||
49 | public abstract class AbstractColorBalanceOperation extends AbstractColoredOperation { |
||
50 | |||
51 | /**
|
||
52 | * @param factory
|
||
53 | *
|
||
54 | */
|
||
55 | public AbstractColorBalanceOperation(OperationFactory factory) {
|
||
56 | super(factory);
|
||
57 | } |
||
58 | |||
59 | abstract protected void initialiceParams(); |
||
60 | |||
61 | @Override
|
||
62 | public void preProcess() throws BufferOperationException { |
||
63 | super.preProcess();
|
||
64 | BufferManager manager = BufferLocator.getBufferManager(); |
||
65 | RasterLegendManager legendManager = RasterLegendLocator.getRasterLegendManager(); |
||
66 | |||
67 | initialiceParams(); |
||
68 | |||
69 | try {
|
||
70 | if (!(getInputColorInterpretation().isGray() || getInputColorInterpretation().isRGB())) {
|
||
71 | throw new UnsupportedOperationException( |
||
72 | "The color interpretation of input buffer isn't RGB nor GRAYSCALE");
|
||
73 | } |
||
74 | |||
75 | int[] inputBandTypes = getInputBuffer().getBandTypes(); |
||
76 | if (getInputColorInterpretation().isGray()) {
|
||
77 | if (inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.GRAY_BAND)] != BufferManager.TYPE_BYTE) {
|
||
78 | throw new UnsupportedOperationException("The type of GRAY band isn't BYTE"); |
||
79 | } |
||
80 | } else if (getInputColorInterpretation().isRGB()) { |
||
81 | if (inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.RED_BAND)] != BufferManager.TYPE_BYTE
|
||
82 | || inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.GREEN_BAND)] != BufferManager.TYPE_BYTE |
||
83 | || inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.BLUE_BAND)] != BufferManager.TYPE_BYTE) { |
||
84 | throw new UnsupportedOperationException("The type of RGB bands isn't BYTE"); |
||
85 | } |
||
86 | } else {
|
||
87 | throw new UnsupportedOperationException( |
||
88 | "The color interpretation of input buffer isn't RGB nor GRAYSCALE");
|
||
89 | } |
||
90 | |||
91 | int sourceBands = this.getInputBuffer().getBandCount(); |
||
92 | NoData[] sourceNoDatas = this.getInputBuffer().getBandNoData(); |
||
93 | int[] sourceTypes = this.getInputBuffer().getBandTypes(); |
||
94 | |||
95 | List<String> colorInterpretations = new ArrayList<>(); |
||
96 | List<NoData> noDatas = new ArrayList<>(); |
||
97 | List<Integer> types = new ArrayList<>(); |
||
98 | |||
99 | colorInterpretations.add(ColorInterpretation.RED_BAND); |
||
100 | colorInterpretations.add(ColorInterpretation.GREEN_BAND); |
||
101 | colorInterpretations.add(ColorInterpretation.BLUE_BAND); |
||
102 | |||
103 | types.add(BufferManager.TYPE_BYTE); |
||
104 | types.add(BufferManager.TYPE_BYTE); |
||
105 | types.add(BufferManager.TYPE_BYTE); |
||
106 | |||
107 | noDatas.add(null);
|
||
108 | noDatas.add(null);
|
||
109 | noDatas.add(null);
|
||
110 | |||
111 | if (getInputColorInterpretation().hasAlphaBand()) {
|
||
112 | int alphaBand = getInputColorInterpretation().getAlphaBand();
|
||
113 | if(sourceTypes[alphaBand]!=BufferManager.TYPE_BYTE){
|
||
114 | throw new UnsupportedOperationException("The type of ALPHA band isn't BYTE"); |
||
115 | } |
||
116 | colorInterpretations.add(ColorInterpretation.ALPHA_BAND); |
||
117 | types.add(sourceTypes[alphaBand]); |
||
118 | noDatas.add(sourceNoDatas[alphaBand]); |
||
119 | } |
||
120 | |||
121 | if (mustCopyUnprocessedBands()) {
|
||
122 | for (int band = 0; band < sourceBands; band++) { |
||
123 | if (!isProcessableBand(band) && !getInputColorInterpretation().isAlphaInterpretation(band)) {
|
||
124 | colorInterpretations.add(getInputColorInterpretation().get(band)); |
||
125 | noDatas.add(sourceNoDatas[band]); |
||
126 | types.add(this.getInputBuffer().getBandTypes()[band]);
|
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | setOutputColorInterpretation( |
||
132 | legendManager.createColorInterpretation(colorInterpretations)); |
||
133 | this.setParameter(OUTPUT_COLOR_INTERPRETATION_PARAM, getOutputColorInterpretation());
|
||
134 | this.setOutputBuffer(
|
||
135 | manager.createBuffer( |
||
136 | this.getInputBuffer().getRows(),
|
||
137 | this.getInputBuffer().getColumns(),
|
||
138 | this.getTypesAsArray(types),
|
||
139 | this.getNoDatasAsArray(noDatas),
|
||
140 | this.getInputBuffer().getProjection(),
|
||
141 | this.getInputBuffer().getEnvelope()));
|
||
142 | } catch (LocatorException | BufferException | CreateEnvelopeException e) {
|
||
143 | throw new BufferOperationException(e); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | @Override
|
||
148 | public void process() throws ProcessingOperationException { |
||
149 | super.process();
|
||
150 | if (getInputColorInterpretation().hasAlphaBand()) {
|
||
151 | try {
|
||
152 | getOutputBuffer().getBand(3).copyFrom(this.getInputBuffer().getBand(getInputColorInterpretation().getAlphaBand())); |
||
153 | } catch (BandException e) {
|
||
154 | throw new ProcessingOperationException(e); |
||
155 | } |
||
156 | } |
||
157 | Object[] rowBandsBuffer = new Object[3]; |
||
158 | |||
159 | for (int row = 0; row < this.getInputBuffer().getRows(); row++) { |
||
160 | if(getInputColorInterpretation().isGray()){
|
||
161 | Band bufferBandGray = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.GRAY_BAND)); |
||
162 | rowBandsBuffer[0] = bufferBandGray.createRowBuffer();
|
||
163 | bufferBandGray.fetchRow(row, rowBandsBuffer[0]);
|
||
164 | rowBandsBuffer[1] = bufferBandGray.createRowBuffer();
|
||
165 | bufferBandGray.fetchRow(row, rowBandsBuffer[0]);
|
||
166 | rowBandsBuffer[2] = bufferBandGray.createRowBuffer();
|
||
167 | bufferBandGray.fetchRow(row, rowBandsBuffer[0]);
|
||
168 | } else if (getInputColorInterpretation().isRGB()){ |
||
169 | Band bufferBandRed = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.RED_BAND)); |
||
170 | rowBandsBuffer[0] = bufferBandRed.createRowBuffer();
|
||
171 | bufferBandRed.fetchRow(row, rowBandsBuffer[0]);
|
||
172 | Band bufferBandGreen = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.GREEN_BAND)); |
||
173 | rowBandsBuffer[1] = bufferBandGreen.createRowBuffer();
|
||
174 | bufferBandGreen.fetchRow(row, rowBandsBuffer[1]);
|
||
175 | Band bufferBandBlue = getInputBuffer().getBand(getInputColorInterpretation().getBand(ColorInterpretation.BLUE_BAND)); |
||
176 | rowBandsBuffer[2] = bufferBandBlue.createRowBuffer();
|
||
177 | bufferBandBlue.fetchRow(row, rowBandsBuffer[2]);
|
||
178 | } else { //No deber?a pasar por aqu? |
||
179 | throw new UnsupportedOperationException("The color interpretation of input buffer isn't RGB nor GRAYSCALE"); |
||
180 | } |
||
181 | |||
182 | List<Object> outputRowBuffers = new ArrayList<>(); |
||
183 | for (int band = 0; band<3; band++){ |
||
184 | Band outputBufferBand = getOutputBuffer().getBand(band); |
||
185 | Object outputRowBuffer = outputBufferBand.createRowBuffer();
|
||
186 | outputRowBuffers.add(outputRowBuffer); |
||
187 | } |
||
188 | |||
189 | processRow(rowBandsBuffer, outputRowBuffers); |
||
190 | |||
191 | for (int band = 0; band<3; band++){ |
||
192 | Band outputBufferBand = getOutputBuffer().getBand(band); |
||
193 | outputBufferBand.putRow(row, outputRowBuffers.get(band)); |
||
194 | } |
||
195 | |||
196 | } |
||
197 | } |
||
198 | |||
199 | abstract protected void processRow(Object[] inputRows, List outputRows); |
||
200 | |||
201 | @Override
|
||
202 | public void postProcess() throws BufferOperationException { |
||
203 | super.postProcess();
|
||
204 | } |
||
205 | |||
206 | /**
|
||
207 | * @param band
|
||
208 | * @return
|
||
209 | */
|
||
210 | @Override
|
||
211 | protected boolean isProcessableBand(int band) { |
||
212 | return isRGBorGrayBand(band) && this.getInputBuffer().getBandTypes()[band] == BufferManager.TYPE_BYTE; |
||
213 | } |
||
214 | |||
215 | protected boolean isRGBorGrayBand(int band) { |
||
216 | String bandColorInterpretation = getInputColorInterpretation().get(band);
|
||
217 | return (bandColorInterpretation.equals(ColorInterpretation.RED_BAND) ||
|
||
218 | bandColorInterpretation.equals(ColorInterpretation.GREEN_BAND) || |
||
219 | bandColorInterpretation.equals(ColorInterpretation.BLUE_BAND) || |
||
220 | bandColorInterpretation.equals(ColorInterpretation.GRAY_BAND)); |
||
221 | } |
||
222 | |||
223 | } |