Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / buffer / BufferHistogramComputer.java @ 5462

History | View | Annotate | Download (4.65 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.impl.buffer;
23

    
24
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
25
import org.gvsig.fmap.dal.coverage.exception.HistogramException;
26
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
27
import org.gvsig.fmap.dal.coverage.store.props.HistogramComputer;
28
import org.gvsig.raster.impl.datastruct.BufferHistogramImpl;
29
import org.gvsig.raster.impl.process.RasterTask;
30
import org.gvsig.raster.impl.process.RasterTaskQueue;
31
import org.gvsig.fmap.dal.coverage.datastruct.BufferHistogram;
32

    
33

    
34
/**
35
 * This class computes the histogram for a <code>Buffer</code>
36
 * 
37
 * @author Nacho Brodin (nachobrodin@gmail.com)
38
 */
39
public class BufferHistogramComputer implements HistogramComputer {
40
        private BufferHistogram             histogram            = null;
41
        private Buffer                      buffer               = null;
42
        protected int                                    progressHistogram    = 0;
43
        private double[]                    limits               = null;
44
        private boolean                     refresh              = false;
45
        @SuppressWarnings("unused")
46
        private double                      scale                = 1;
47
        
48
        /**
49
         * Constructor
50
         * @param dataset
51
         */
52
        public BufferHistogramComputer(Buffer buffer) {
53
                this.buffer = buffer;
54
        }
55
        
56
        public void setScaleHistogram(double scale) {
57
                this.scale = scale;
58
        }
59
        
60
        public BufferHistogram getBufferHistogram()
61
        throws ProcessInterruptedException, HistogramException {
62
                if(histogram == null || refresh) {
63
                        RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + "");
64
                        progressHistogram = 0;
65
                        double[][] limits = buffer.getAllBandsLimits();
66

    
67
                        BufferHistogramImpl hist = new BufferHistogramImpl(buffer.getBandCount(), limits[0], limits[1], buffer.getDataType());
68

    
69
                        for (int iBand = 0; iBand < buffer.getBandCount(); iBand++) {
70
                                for (int row = 0; row < buffer.getHeight(); row++) {
71
                                        switch(buffer.getDataType()) {
72
                                        case Buffer.TYPE_BYTE:
73
                                                for (int col = 0; col < buffer.getWidth(); col++) 
74
                                                        hist.incrementPxValue(iBand, (double)(buffer.getElemByte(row, col, iBand)));
75
                                                break;
76
                                        case Buffer.TYPE_SHORT:
77
                                                for (int col = 0; col < buffer.getWidth(); col++) 
78
                                                        hist.incrementPxValue(iBand, (double)(buffer.getElemShort(row, col, iBand)));
79
                                                break;
80
                                        case Buffer.TYPE_INT:
81
                                                for (int col = 0; col < buffer.getWidth(); col++) 
82
                                                        hist.incrementPxValue(iBand, (double)(buffer.getElemInt(row, col, iBand)));
83
                                                break;
84
                                        case Buffer.TYPE_FLOAT:
85
                                                for (int col = 0; col < buffer.getWidth(); col++) 
86
                                                        hist.incrementPxValue(iBand, (double)buffer.getElemFloat(row, col, iBand));
87
                                                break;
88
                                        case Buffer.TYPE_DOUBLE:
89
                                                for (int col = 0; col < buffer.getWidth(); col++) 
90
                                                        hist.incrementPxValue(iBand, buffer.getElemDouble(row, col, iBand));
91
                                                break;
92
                                        }
93

    
94
                                        if (task.getEvent() != null)
95
                                                task.manageEvent(task.getEvent());
96

    
97
                                        progressHistogram = ((iBand * buffer.getHeight() + row) * 100) /(buffer.getHeight() * buffer.getBandCount());
98
                                }
99
                        }
100
                        progressHistogram = 100;
101
                        histogram = hist;
102
                }
103
                return histogram;
104
        }
105

    
106
        public void resetPercent() {
107
                progressHistogram = 0;
108
        }
109

    
110
        public int getPercent() {
111
                return progressHistogram;
112
        }
113

    
114
        public double getMinimum() {
115
                if(limits == null)
116
                        try {
117
                                limits = buffer.getLimits();
118
                        } catch (ProcessInterruptedException e) {
119
                                return 0;
120
                        }
121
                return limits[0];
122
        }
123

    
124
        public double getMaximum() {
125
                if(limits == null)
126
                        try {
127
                                limits = buffer.getLimits();
128
                        } catch (ProcessInterruptedException e) {
129
                                return 0;
130
                        }
131
                return limits[1];
132
        }
133
        
134
        public void refreshHistogram() {
135
                refresh = true;
136
        }
137

    
138
        public String getLog() {
139
                return null;
140
        }
141

    
142
        public boolean isCancelable() {
143
                return true;
144
        }
145

    
146
        public boolean isPausable() {
147
                return false;
148
        }
149

    
150
        public void setPercent(int value) {
151
                progressHistogram = value;
152
        }
153
}