Revision 8589

View differences:

org.gvsig.raster/branches/org.gvsig.raster.2.4/org.gvsig.raster/org.gvsig.raster.lib/org.gvsig.raster.lib.legend/org.gvsig.raster.lib.legend.impl/src/main/java/org/gvsig/raster/lib/legend/impl/operations/histogrammatching/HistogramMatchingOperation.java.svntmp
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.histogrammatching;
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.impl.exceptions.ProcessingOperationException;
36
import org.gvsig.raster.lib.buffer.spi.operations.AbstractOperation;
37
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation;
38
import org.gvsig.tools.locator.LocatorException;
39

  
40

  
41
/**
42
 * @author fdiaz
43
 *
44
 */
45
public class HistogramMatchingOperation extends AbstractOperation{
46

  
47
    static public String COLOR_INTERPRETATION_PARAM = "color_interpretation";
48
    static public String STATISTICS_PARAM = "statistics";
49

  
50
    protected ColorInterpretation colorInterpretation;
51
    protected Statistics statistics;
52
    private RowProcessor rowProcessor;
53

  
54
    /**
55
     * @param factory
56
     *
57
     */
58
    public HistogramMatchingOperation(OperationFactory factory) {
59
        this.factory = factory;
60
    }
61

  
62
    @Override
63
    public void preProcess() throws BufferOperationException {
64
        BufferManager manager = BufferLocator.getBufferManager();
65

  
66
        colorInterpretation = (ColorInterpretation) this.parameters.getDynValue(COLOR_INTERPRETATION_PARAM);
67
        statistics = (Statistics)this.parameters.getDynValue(STATISTICS_PARAM);
68

  
69
        int bands = this.buffer.getBandCount();
70
        NoData[] noData = this.buffer.getBandNoData();
71

  
72
        try {
73
            this.outputBuffer = manager.createBuffer(
74
                this.buffer.getRows(),
75
                this.buffer.getColumns(),
76
                this.buffer.getBandTypes(),
77
                this.buffer.getBandNoData(),
78
                this.buffer.getProjection(),
79
                this.buffer.getEnvelope());
80
        } catch (LocatorException | BufferException | CreateEnvelopeException e) {
81
            throw new ProcessingOperationException(e);
82
        }
83
    }
84

  
85
    @Override
86
    public void process() throws ProcessingOperationException {
87
        for (int band=0; band<this.buffer.getBandCount(); band++){
88
            rowProcessor = new ByteRowProcessor(band);
89
            if (isRGBorGrayBand(band)) {
90
                Band bufferBand = this.buffer.getBand(band);
91
                Band outputBufferBand = this.outputBuffer.getBand(band);
92

  
93
                for (int row = 0; row < this.buffer.getRows(); row++) {
94
                    Object rowBuffer = bufferBand.createRowBuffer();
95
                    bufferBand.fetchRow(row, rowBuffer);
96

  
97
                    Object outputRowBuffer = outputBufferBand.createRowBuffer();
98
                    outputBufferBand.fetchRow(row, outputRowBuffer);
99

  
100
                    rowProcessor.processRow(rowBuffer, outputRowBuffer);
101

  
102
                    outputBufferBand.putRow(row, outputRowBuffer);
103
                }
104
            } else {
105
                try {
106
                    this.outputBuffer.getBand(band).copyFrom(this.buffer.getBand(band));
107
                } catch (BandException e) {
108
                    throw new ProcessingOperationException(e);
109
                }
110
            }
111
        }
112
    }
113

  
114
    private boolean isRGBorGrayBand(int band) {
115
        String bandColorInterpretation = colorInterpretation.get(band);
116
        return (bandColorInterpretation.equals(ColorInterpretation.RED_BAND) ||
117
            bandColorInterpretation.equals(ColorInterpretation.GREEN_BAND) ||
118
            bandColorInterpretation.equals(ColorInterpretation.BLUE_BAND) ||
119
            bandColorInterpretation.equals(ColorInterpretation.GRAY_BAND));
120
    }
121

  
122
    @Override
123
    public void postProcess() throws BufferOperationException {
124
        // TODO Auto-generated method stub
125

  
126
    }
127

  
128

  
129

  
130
    interface RowProcessor {
131
        void processRow(Object inputRow, Object outputRow);
132
        byte processValue(Object value);
133
    };
134

  
135
    private abstract class AbstractRowProcessor implements RowProcessor {
136
//        int band;
137
        int maxResult = 255;
138
        int minResult = 0;
139
        NoData noData;
140

  
141
        public AbstractRowProcessor(int band) {
142
//            this.band = band;
143
            noData = buffer.getBand(band).getNoData();
144
            if(noData.isDefined()) {
145
                minResult = (byte)1;
146
            }
147
        }
148
    }
149

  
150
    private class ByteRowProcessor extends AbstractRowProcessor {
151

  
152

  
153
        public ByteRowProcessor(int band) {
154
            super(band);
155
        }
156

  
157
        @Override
158
        public void processRow(Object inputRow, Object outputRow) {
159
            byte[] inputByteRow = (byte[])inputRow;
160
            byte[] outputByteRow = (byte[])outputRow;
161
            for (int i = 0; i < inputByteRow.length; i++) {
162
                outputByteRow[i] = processValue(inputByteRow[i]);
163
            }
164
        }
165

  
166
        @Override
167
        public byte processValue(Object value) {
168
            if(noData.isDefined() && noData.getValue().equals(value)){
169
                return (byte)value;
170
            }
171

  
172
            int iValue = 0xFF & ((Byte) value).byteValue();
173
            int result = iValue; // + brightness;
174
            if(result>maxResult){
175
                result = maxResult;
176
            }
177
            if(result<minResult){
178
                result = minResult;
179
            }
180
            return (byte)result;
181
        }
182
    }
183
}
org.gvsig.raster/branches/org.gvsig.raster.2.4/org.gvsig.raster/org.gvsig.raster.lib/org.gvsig.raster.lib.legend/org.gvsig.raster.lib.legend.impl/src/main/java/org/gvsig/raster/lib/legend/impl/operations/histogrammatching/HistogramMatchingOperationFactory.java
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.histogrammatching;
24

  
25
import java.io.IOException;
26
import java.io.InputStream;
27
import java.util.Map;
28

  
29
import org.xmlpull.v1.XmlPullParserException;
30

  
31
import org.gvsig.raster.lib.buffer.api.Buffer;
32
import org.gvsig.raster.lib.buffer.api.BufferManager;
33
import org.gvsig.raster.lib.buffer.api.operations.Operation;
34
import org.gvsig.raster.lib.buffer.api.operations.OperationFactory;
35
import org.gvsig.tools.ToolsLocator;
36
import org.gvsig.tools.dynobject.DynObject;
37
import org.gvsig.tools.dynobject.DynObjectManager;
38
import org.gvsig.tools.dynobject.DynStruct;
39

  
40

  
41
/**
42
 * @author fdiaz
43
 *
44
 */
45
public class HistogramMatchingOperationFactory implements OperationFactory {
46
    public static String NAME="HistogramMatching";
47
    public static String DESCRIPTION="Histogram matching operation";//FIXME:
48
    private DynStruct definition;
49

  
50
    /**
51
     *
52
     */
53
    public HistogramMatchingOperationFactory() {
54
        // TODO Auto-generated constructor stub
55
    }
56

  
57
    /* (non-Javadoc)
58
     * @see org.gvsig.raster.lib.buffer.api.operations.OperationFactory#createParameters()
59
     */
60
    @Override
61
    public DynObject createParameters() {
62
        DynObjectManager manager = ToolsLocator.getDynObjectManager();
63
        if (definition == null) {
64
            definition = createParametersDefinition();
65
        }
66
        DynObject parameters = manager.createDynObject(definition);
67
        return parameters;
68
    }
69

  
70
    /* (non-Javadoc)
71
     * @see org.gvsig.raster.lib.buffer.api.operations.OperationFactory#create()
72
     */
73
    @Override
74
    public Operation create() {
75
        return new HistogramMatchingOperation(this);
76
    }
77

  
78
    /* (non-Javadoc)
79
     * @see org.gvsig.raster.lib.buffer.api.operations.OperationFactory#createParametersDefinition()
80
     */
81
    @Override
82
    public DynStruct createParametersDefinition() {
83
        return registerParametersDefinition("HistogramMatchingOperationParameters", "org/gvsig/raster/lib/legend/impl/operations/histogrammatching/HistogramMatchingOperationParameters.xml");
84
    }
85

  
86
    /* (non-Javadoc)
87
     * @see org.gvsig.raster.lib.buffer.api.operations.OperationFactory#getName()
88
     */
89
    @Override
90
    public String getName() {
91
        return NAME;
92
    }
93

  
94
    /* (non-Javadoc)
95
     * @see org.gvsig.raster.lib.buffer.api.operations.OperationFactory#getDescription()
96
     */
97
    @Override
98
    public String getDescription() {
99
        return DESCRIPTION;
100
    }
101

  
102
    /* (non-Javadoc)
103
     * @see org.gvsig.raster.lib.buffer.api.operations.OperationFactory#isApplicable(org.gvsig.raster.lib.buffer.api.Buffer)
104
     */
105
    @Override
106
    public Applicable isApplicable(Buffer buffer) {
107
        int bandsCount = buffer.getBandCount();
108
        int byteBandsCount = 0;
109
        for(int band = 0; band<bandsCount; band++){
110
            if (buffer.getBandTypes()[band]==BufferManager.TYPE_BYTE) {
111
                byteBandsCount++;
112
            }
113
        }
114
        if(byteBandsCount==bandsCount){
115
            return Applicable.YES;
116
        } else if (byteBandsCount==0) {
117
            return Applicable.NO;
118
        }
119
        return Applicable.UNKNOW;
120
    }
121

  
122
    /* (non-Javadoc)
123
     * @see org.gvsig.raster.lib.buffer.api.operations.OperationFactory#isApplicable(org.gvsig.raster.lib.buffer.api.operations.Operation)
124
     */
125
    @Override
126
    public Applicable isApplicable(Operation operation) {
127
        //FIXME:
128
        return Applicable.YES;
129
    }
130

  
131
    protected DynStruct registerParametersDefinition(String name, String filename) {
132
        DynStruct definition = null;
133

  
134
        DynObjectManager manager = ToolsLocator.getDynObjectManager();
135
        definition = manager.get(name);
136
        if ( definition == null) {
137
            InputStream stream = this.getClass().getResourceAsStream(filename);
138
            if( stream == null ) {
139
                throw new IllegalArgumentException("File '"+filename+"' not found");
140
            }
141
            Map<String, DynStruct> definitions = null;
142
            try {
143
                definitions = manager.importDynClassDefinitions(stream, this.getClass().getClassLoader());
144
            } catch (XmlPullParserException e) {
145
                throw new IllegalArgumentException("DynClass '"+name+"' can't parse file '"+filename+"'",e);
146
            } catch (IOException e) {
147
                throw new IllegalArgumentException("DynClass '"+name+"' can't read file '"+filename+"'",e);
148
            }
149
            definition = definitions.get(name);
150
            if ( definition == null) {
151
                throw new IllegalArgumentException("DynClass '"+name+"' not found in file '"+filename+"'");
152
            }
153
        }
154
        return definition;
155
    }
156

  
157
}
org.gvsig.raster/branches/org.gvsig.raster.2.4/org.gvsig.raster/org.gvsig.raster.lib/org.gvsig.raster.lib.legend/org.gvsig.raster.lib.legend.impl/src/main/java/org/gvsig/raster/lib/legend/impl/operations/histogrammatching/HistogramMatchingOperation.java
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.histogrammatching;
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.impl.exceptions.ProcessingOperationException;
36
import org.gvsig.raster.lib.buffer.spi.operations.AbstractOperation;
37
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation;
38
import org.gvsig.tools.locator.LocatorException;
39

  
40

  
41
/**
42
 * @author fdiaz
43
 *
44
 */
45
public class HistogramMatchingOperation extends AbstractOperation{
46

  
47
    static public String COLOR_INTERPRETATION_PARAM = "color_interpretation";
48
    static public String STATISTICS_PARAM = "statistics";
49

  
50
    protected ColorInterpretation colorInterpretation;
51
    protected Statistics statistics;
52
    private RowProcessor rowProcessor;
53

  
54
    /**
55
     * @param factory
56
     *
57
     */
58
    public HistogramMatchingOperation(OperationFactory factory) {
59
        this.factory = factory;
60
    }
61

  
62
    @Override
63
    public void preProcess() throws BufferOperationException {
64
        BufferManager manager = BufferLocator.getBufferManager();
65

  
66
        colorInterpretation = (ColorInterpretation) this.parameters.getDynValue(COLOR_INTERPRETATION_PARAM);
67
        statistics = (Statistics)this.parameters.getDynValue(STATISTICS_PARAM);
68

  
69
        int bands = this.buffer.getBandCount();
70
        NoData[] noData = this.buffer.getBandNoData();
71

  
72
        try {
73
            this.outputBuffer = manager.createBuffer(
74
                this.buffer.getRows(),
75
                this.buffer.getColumns(),
76
                this.buffer.getBandTypes(),
77
                this.buffer.getBandNoData(),
78
                this.buffer.getProjection(),
79
                this.buffer.getEnvelope());
80
        } catch (LocatorException | BufferException | CreateEnvelopeException e) {
81
            throw new ProcessingOperationException(e);
82
        }
83
    }
84

  
85
    @Override
86
    public void process() throws ProcessingOperationException {
87
        for (int band=0; band<this.buffer.getBandCount(); band++){
88
            rowProcessor = new ByteRowProcessor(band);
89
            if (isRGBorGrayBand(band)) {
90
                Band bufferBand = this.buffer.getBand(band);
91
                Band outputBufferBand = this.outputBuffer.getBand(band);
92

  
93
                for (int row = 0; row < this.buffer.getRows(); row++) {
94
                    Object rowBuffer = bufferBand.createRowBuffer();
95
                    bufferBand.fetchRow(row, rowBuffer);
96

  
97
                    Object outputRowBuffer = outputBufferBand.createRowBuffer();
98
                    outputBufferBand.fetchRow(row, outputRowBuffer);
99

  
100
                    rowProcessor.processRow(rowBuffer, outputRowBuffer);
101

  
102
                    outputBufferBand.putRow(row, outputRowBuffer);
103
                }
104
            } else {
105
                try {
106
                    this.outputBuffer.getBand(band).copyFrom(this.buffer.getBand(band));
107
                } catch (BandException e) {
108
                    throw new ProcessingOperationException(e);
109
                }
110
            }
111
        }
112
    }
113

  
114
    private boolean isRGBorGrayBand(int band) {
115
        String bandColorInterpretation = colorInterpretation.get(band);
116
        return (bandColorInterpretation.equals(ColorInterpretation.RED_BAND) ||
117
            bandColorInterpretation.equals(ColorInterpretation.GREEN_BAND) ||
118
            bandColorInterpretation.equals(ColorInterpretation.BLUE_BAND) ||
119
            bandColorInterpretation.equals(ColorInterpretation.GRAY_BAND));
120
    }
121

  
122
    @Override
123
    public void postProcess() throws BufferOperationException {
124
        // TODO Auto-generated method stub
125

  
126
    }
127

  
128

  
129

  
130
    interface RowProcessor {
131
        void processRow(Object inputRow, Object outputRow);
132
        byte processValue(Object value);
133
    };
134

  
135
    private abstract class AbstractRowProcessor implements RowProcessor {
136
//        int band;
137
        int maxResult = 255;
138
        int minResult = 0;
139
        NoData noData;
140

  
141
        public AbstractRowProcessor(int band) {
142
//            this.band = band;
143
            noData = buffer.getBand(band).getNoData();
144
            if(noData.isDefined()) {
145
                minResult = (byte)1;
146
            }
147
        }
148
    }
149

  
150
    private class ByteRowProcessor extends AbstractRowProcessor {
151

  
152

  
153
        public ByteRowProcessor(int band) {
154
            super(band);
155
        }
156

  
157
        @Override
158
        public void processRow(Object inputRow, Object outputRow) {
159
            byte[] inputByteRow = (byte[])inputRow;
160
            byte[] outputByteRow = (byte[])outputRow;
161
            for (int i = 0; i < inputByteRow.length; i++) {
162
                outputByteRow[i] = processValue(inputByteRow[i]);
163
            }
164
        }
165

  
166
        @Override
167
        public byte processValue(Object value) {
168
            if(noData.isDefined() && noData.getValue().equals(value)){
169
                return (byte)value;
170
            }
171

  
172
            int iValue = 0xFF & ((Byte) value).byteValue();
173
            int result = iValue; // + brightness;
174
            if(result>maxResult){
175
                result = maxResult;
176
            }
177
            if(result<minResult){
178
                result = minResult;
179
            }
180
            return (byte)result;
181
        }
182
    }
183
}

Also available in: Unified diff