Revision 43862 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/grayscale/GrayScaleOperation.java

View differences:

GrayScaleOperation.java
23 23
package org.gvsig.raster.lib.legend.impl.operations.grayscale;
24 24

  
25 25
import java.util.ArrayList;
26
import java.util.Iterator;
27 26
import java.util.List;
28 27

  
29
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
30 28
import org.gvsig.raster.lib.buffer.api.Band;
31 29
import org.gvsig.raster.lib.buffer.api.BufferLocator;
32 30
import org.gvsig.raster.lib.buffer.api.BufferManager;
33 31
import org.gvsig.raster.lib.buffer.api.NoData;
34
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException;
35 32
import org.gvsig.raster.lib.buffer.api.exceptions.BufferOperationException;
36 33
import org.gvsig.raster.lib.buffer.api.operations.OperationFactory;
37 34
import org.gvsig.raster.lib.buffer.spi.exceptions.ProcessingOperationException;
38
import org.gvsig.raster.lib.buffer.spi.operations.AbstractOperation;
39 35
import org.gvsig.raster.lib.legend.api.RasterLegendLocator;
40 36
import org.gvsig.raster.lib.legend.api.RasterLegendManager;
41 37
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation;
42 38
import org.gvsig.raster.lib.legend.spi.AbstractColoredOperation;
43
import org.gvsig.tools.locator.LocatorException;
44 39

  
45 40

  
46 41
/**
......
54 49
     *
55 50
     */
56 51
    public GrayScaleOperation(OperationFactory factory) {
57
        this.factory = factory;
52
        super(factory);
58 53
    }
59 54

  
60 55
    @Override
......
65 60

  
66 61
        try {
67 62

  
68
            if (!(colorInterpretation.hasAnyRGBBand() || colorInterpretation.hasAnyGrayBand())) {
63
            if (!(getInputColorInterpretation().hasAnyRGBBand() || getInputColorInterpretation().hasAnyGrayBand())) {
69 64
                throw new UnsupportedOperationException(
70 65
                    "The color interpretation of input buffer isn't RGB nor GRAYSCALE");
71 66
            }
72 67

  
73
            int[] inputBandTypes = buffer.getBandTypes();
74
            if (colorInterpretation.hasAnyRGBBand()) {
75
                if ((colorInterpretation.getBand(ColorInterpretation.RED_BAND)>=0 && inputBandTypes[colorInterpretation.getBand(ColorInterpretation.RED_BAND)] != BufferManager.TYPE_BYTE)
76
                    || (colorInterpretation.getBand(ColorInterpretation.GREEN_BAND)>=0 && inputBandTypes[colorInterpretation.getBand(ColorInterpretation.GREEN_BAND)] != BufferManager.TYPE_BYTE)
77
                    || (colorInterpretation.getBand(ColorInterpretation.BLUE_BAND)>=0 && inputBandTypes[colorInterpretation.getBand(ColorInterpretation.BLUE_BAND)] != BufferManager.TYPE_BYTE)) {
68
            int[] inputBandTypes = getInputBuffer().getBandTypes();
69
            if (getInputColorInterpretation().hasAnyRGBBand()) {
70
                if ((getInputColorInterpretation().getBand(ColorInterpretation.RED_BAND)>=0 && inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.RED_BAND)] != BufferManager.TYPE_BYTE)
71
                    || (getInputColorInterpretation().getBand(ColorInterpretation.GREEN_BAND)>=0 && inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.GREEN_BAND)] != BufferManager.TYPE_BYTE)
72
                    || (getInputColorInterpretation().getBand(ColorInterpretation.BLUE_BAND)>=0 && inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.BLUE_BAND)] != BufferManager.TYPE_BYTE)) {
78 73
                    throw new UnsupportedOperationException("The type of bands isn't BYTE");
79 74
                }
80
            } else if (colorInterpretation.hasAnyGrayBand()) {
81
                if ((colorInterpretation.getBand(ColorInterpretation.GRAY_BAND)>=0 && inputBandTypes[colorInterpretation.getBand(ColorInterpretation.GRAY_BAND)] != BufferManager.TYPE_BYTE)) {
75
            } else if (getInputColorInterpretation().hasAnyGrayBand()) {
76
                if ((getInputColorInterpretation().getBand(ColorInterpretation.GRAY_BAND)>=0 && inputBandTypes[getInputColorInterpretation().getBand(ColorInterpretation.GRAY_BAND)] != BufferManager.TYPE_BYTE)) {
82 77
                    throw new UnsupportedOperationException("The type of band gray isn't BYTE");
83 78
                }
84 79
            } else {
......
86 81
                    "The color interpretation of input buffer isn't RGB nor GRAYSCALE");
87 82
            }
88 83

  
89
            int sourceBands = this.buffer.getBandCount();
90
            NoData[] sourceNoDatas = this.buffer.getBandNoData();
91
            int[] sourceTypes = this.buffer.getBandTypes();
84
            int sourceBands = this.getInputBuffer().getBandCount();
85
            NoData[] sourceNoDatas = this.getInputBuffer().getBandNoData();
86
            int[] sourceTypes = this.getInputBuffer().getBandTypes();
92 87

  
93
            List<String> colorInterpretations = new ArrayList<String>();
94
            List<NoData> noDatas = new ArrayList<NoData>();
95
            List<Integer> types = new ArrayList<Integer>();
88
            List<String> colorInterpretations = new ArrayList<>();
89
            List<NoData> noDatas = new ArrayList<>();
90
            List<Integer> types = new ArrayList<>();
96 91

  
97 92
            colorInterpretations.add(ColorInterpretation.GRAY_BAND);
98 93
            types.add(BufferManager.TYPE_BYTE);
99 94
            noDatas.add(null);
100 95

  
101
            if (colorInterpretation.hasAlphaBand()) {
102
                if(sourceTypes[colorInterpretation.getAlphaBand()]!=BufferManager.TYPE_BYTE){
96
            if (getInputColorInterpretation().hasAlphaBand()) {
97
                if(sourceTypes[getInputColorInterpretation().getAlphaBand()]!=BufferManager.TYPE_BYTE){
103 98
                    throw new UnsupportedOperationException("The type of ALPHA band isn't BYTE");
104 99
                }
105 100
                colorInterpretations.add(ColorInterpretation.ALPHA_BAND);
106
                types.add(sourceTypes[colorInterpretation.getAlphaBand()]);
107
                noDatas.add(sourceNoDatas[colorInterpretation.getAlphaBand()]);
101
                types.add(sourceTypes[getInputColorInterpretation().getAlphaBand()]);
102
                noDatas.add(sourceNoDatas[getInputColorInterpretation().getAlphaBand()]);
108 103
            }
109 104

  
110
            if (copyUnprocessedBands) {
105
            if (mustCopyUnprocessedBands()) {
111 106
                for (int band = 0; band < sourceBands; band++) {
112
                    if (!isProcessableBand(band) && !colorInterpretation.isAlphaInterpretation(band)) {
113
                        colorInterpretations.add(colorInterpretation.get(band));
107
                    if (!isProcessableBand(band) && !getInputColorInterpretation().isAlphaInterpretation(band)) {
108
                        colorInterpretations.add(getInputColorInterpretation().get(band));
114 109
                        noDatas.add(sourceNoDatas[band]);
115
                        types.add(this.buffer.getBandTypes()[band]);
110
                        types.add(this.getInputBuffer().getBandTypes()[band]);
116 111
                    }
117 112
                }
118 113
            }
119
            outputColorInterpretation =
120
                legendManager.createColorInterpretation(colorInterpretations.toArray(new String[0]));
121
            this.parameters.setDynValue(OUTPUT_COLOR_INTERPRETATION_PARAM, outputColorInterpretation);
122
            int[] typesInt = new int[types.size()];
123
            for (Iterator<Integer> iterator = types.iterator(); iterator.hasNext();) {
124
                int i = 0;
125
                Integer type = (Integer) iterator.next();
126
                typesInt[i] = type.intValue();
127
            }
128
            this.outputBuffer =
129
                manager.createBuffer(this.buffer.getRows(), this.buffer.getColumns(), typesInt,
130
                    noDatas.toArray(new NoData[0]), this.buffer.getProjection(), this.buffer.getEnvelope());
131
        } catch (LocatorException | BufferException | CreateEnvelopeException e) {
114
            setOutputColorInterpretation(
115
                legendManager.createColorInterpretation(colorInterpretations));
116
            this.setParameter(OUTPUT_COLOR_INTERPRETATION_PARAM, getOutputColorInterpretation());
117
            this.setOutputBuffer(
118
                manager.createBuffer(
119
                        this.getInputBuffer().getRows(), 
120
                        this.getInputBuffer().getColumns(), 
121
                        this.getTypesAsArray(types),
122
                        this.getNoDatasAsArray(noDatas), 
123
                        this.getInputBuffer().getProjection(), 
124
                        this.getInputBuffer().getEnvelope()));
125
        } catch (Exception e) {
132 126
            throw new ProcessingOperationException(e);
133 127
        }
134 128
    }
......
139 133
            super.process();
140 134
            List<Integer> bandsToProcess = new ArrayList<Integer>();
141 135

  
142
            Band outputBufferBand = outputBuffer.getBand(0);
136
            Band outputBufferBand = getOutputBuffer().getBand(0);
143 137
            boolean processed = false;
144
            if (copyUnprocessedBands) {
145
                int bands = this.buffer.getBandCount();
146
                int outBand = colorInterpretation.hasAlphaBand() ? 2 : 1;
138
            if (mustCopyUnprocessedBands()) {
139
                int bands = this.getInputBuffer().getBandCount();
140
                int outBand = getInputColorInterpretation().hasAlphaBand() ? 2 : 1;
147 141
                for (int band = 0; band < bands; band++) {
148 142
                    if (isProcessableBand(band)) {
149
                        if (colorInterpretation.isGrayInterpretation(band)) {
150
                            outputBufferBand.copyFrom(this.buffer.getBand(band));
143
                        if (getInputColorInterpretation().isGrayInterpretation(band)) {
144
                            outputBufferBand.copyFrom(this.getInputBuffer().getBand(band));
151 145
                            processed = true;
152 146
                        } else {
153 147
                            bandsToProcess.add(band);
154 148
                        }
155
                    } else if (colorInterpretation.isAlphaInterpretation(band)) {
156
                        outputBuffer.getBand(outputColorInterpretation.getAlphaBand()).copyFrom(
157
                            this.buffer.getBand(band));
149
                    } else if (getInputColorInterpretation().isAlphaInterpretation(band)) {
150
                        getOutputBuffer().getBand(getOutputColorInterpretation().getAlphaBand()).copyFrom(
151
                            this.getInputBuffer().getBand(band));
158 152
                    } else {
159
                        outputBuffer.getBand(outBand).copyFrom(
160
                            this.buffer.getBand(band));
153
                        getOutputBuffer().getBand(outBand).copyFrom(
154
                            this.getInputBuffer().getBand(band));
161 155
                        outBand++;
162 156
                    }
163 157
                }
......
165 159

  
166 160
            if (!processed) {
167 161
                Object[] rowBandsBuffer = new Object[bandsToProcess.size()];
168
                for (int row = 0; row < this.buffer.getRows(); row++) {
162
                for (int row = 0; row < this.getInputBuffer().getRows(); row++) {
169 163
                    for (int i = 0; i < bandsToProcess.size(); i++) {
170 164
                        Integer band = bandsToProcess.get(i);
171 165
                        if (isProcessableBand(band)) {
172
                            Band bufferBand = buffer.getBand(band);
166
                            Band bufferBand = getInputBuffer().getBand(band);
173 167
                            rowBandsBuffer[i] = bufferBand.createRowBuffer();
174 168
                            bufferBand.fetchRow(row, rowBandsBuffer[i]);
175 169
                        }
......
188 182
     * @param band
189 183
     * @return
190 184
     */
191
    private boolean isProcessableBand(int band) {
192
        return isRGBorGrayBand(band) && this.buffer.getBandTypes()[band] == BufferManager.TYPE_BYTE;
185
    @Override
186
    protected boolean isProcessableBand(int band) {
187
        return isRGBorGrayBand(band) && this.getInputBuffer().getBandTypes()[band] == BufferManager.TYPE_BYTE;
193 188
    }
194 189

  
195 190
    private boolean isRGBorGrayBand(int band) {
196
        String bandColorInterpretation = colorInterpretation.get(band);
191
        String bandColorInterpretation = getInputColorInterpretation().get(band);
197 192
        return (bandColorInterpretation.equals(ColorInterpretation.RED_BAND)
198 193
            || bandColorInterpretation.equals(ColorInterpretation.GREEN_BAND)
199 194
            || bandColorInterpretation.equals(ColorInterpretation.BLUE_BAND) || bandColorInterpretation

Also available in: Unified diff