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 @ 1676
History | View | Annotate | Download (5.29 KB)
1 | 338 | nbrodin | /* 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 | 1676 | nbrodin | @SuppressWarnings("unused") |
46 | private double scale = 1; |
||
47 | 338 | nbrodin | |
48 | /**
|
||
49 | * Constructor
|
||
50 | * @param dataset
|
||
51 | */
|
||
52 | public BufferHistogramComputer(Buffer buffer) { |
||
53 | this.buffer = buffer;
|
||
54 | } |
||
55 | |||
56 | 1676 | nbrodin | public void setScaleHistogram(double scale) { |
57 | this.scale = scale;
|
||
58 | } |
||
59 | |||
60 | 338 | nbrodin | /*
|
61 | * (non-Javadoc)
|
||
62 | * @see org.gvsig.fmap.dal.coverage.store.props.HistogramComputer#getBufferHistogram()
|
||
63 | */
|
||
64 | public BufferHistogram getBufferHistogram()
|
||
65 | throws ProcessInterruptedException, HistogramException {
|
||
66 | if(histogram == null || refresh) { |
||
67 | 1419 | nbrodin | RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + ""); |
68 | 338 | nbrodin | progressHistogram = 0;
|
69 | double[][] limits = buffer.getAllBandsLimits(); |
||
70 | |||
71 | BufferHistogramImpl hist = new BufferHistogramImpl(buffer.getBandCount(), limits[0], limits[1], buffer.getDataType()); |
||
72 | |||
73 | for (int iBand = 0; iBand < buffer.getBandCount(); iBand++) { |
||
74 | for (int row = 0; row < buffer.getHeight(); row++) { |
||
75 | switch(buffer.getDataType()) {
|
||
76 | case Buffer.TYPE_BYTE: |
||
77 | for (int col = 0; col < buffer.getWidth(); col++) |
||
78 | hist.incrementPxValue(iBand, (double)(buffer.getElemByte(row, col, iBand)));
|
||
79 | break;
|
||
80 | case Buffer.TYPE_SHORT: |
||
81 | for (int col = 0; col < buffer.getWidth(); col++) |
||
82 | hist.incrementPxValue(iBand, (double)(buffer.getElemShort(row, col, iBand)));
|
||
83 | break;
|
||
84 | case Buffer.TYPE_INT: |
||
85 | for (int col = 0; col < buffer.getWidth(); col++) |
||
86 | hist.incrementPxValue(iBand, (double)(buffer.getElemInt(row, col, iBand)));
|
||
87 | break;
|
||
88 | case Buffer.TYPE_FLOAT: |
||
89 | for (int col = 0; col < buffer.getWidth(); col++) |
||
90 | hist.incrementPxValue(iBand, (double)buffer.getElemFloat(row, col, iBand));
|
||
91 | break;
|
||
92 | case Buffer.TYPE_DOUBLE: |
||
93 | for (int col = 0; col < buffer.getWidth(); col++) |
||
94 | hist.incrementPxValue(iBand, buffer.getElemDouble(row, col, iBand)); |
||
95 | break;
|
||
96 | } |
||
97 | |||
98 | if (task.getEvent() != null) |
||
99 | task.manageEvent(task.getEvent()); |
||
100 | |||
101 | progressHistogram = ((iBand * buffer.getHeight() + row) * 100) /(buffer.getHeight() * buffer.getBandCount());
|
||
102 | } |
||
103 | } |
||
104 | progressHistogram = 100;
|
||
105 | histogram = hist; |
||
106 | } |
||
107 | return histogram;
|
||
108 | } |
||
109 | |||
110 | /*
|
||
111 | * (non-Javadoc)
|
||
112 | * @see org.gvsig.fmap.dal.coverage.store.props.HistogramComputer#resetPercent()
|
||
113 | */
|
||
114 | public void resetPercent() { |
||
115 | progressHistogram = 0;
|
||
116 | } |
||
117 | |||
118 | /*
|
||
119 | * (non-Javadoc)
|
||
120 | * @see org.gvsig.fmap.dal.coverage.store.props.HistogramComputer#getPercent()
|
||
121 | */
|
||
122 | public int getPercent() { |
||
123 | return progressHistogram;
|
||
124 | } |
||
125 | |||
126 | /*
|
||
127 | * (non-Javadoc)
|
||
128 | * @see org.gvsig.fmap.dal.coverage.store.props.HistogramComputer#getMinimum()
|
||
129 | */
|
||
130 | public double getMinimum() { |
||
131 | if(limits == null) |
||
132 | try {
|
||
133 | limits = buffer.getLimits(); |
||
134 | } catch (ProcessInterruptedException e) {
|
||
135 | return 0; |
||
136 | } |
||
137 | return limits[0]; |
||
138 | } |
||
139 | |||
140 | /*
|
||
141 | * (non-Javadoc)
|
||
142 | * @see org.gvsig.fmap.dal.coverage.store.props.HistogramComputer#getMaximum()
|
||
143 | */
|
||
144 | public double getMaximum() { |
||
145 | if(limits == null) |
||
146 | try {
|
||
147 | limits = buffer.getLimits(); |
||
148 | } catch (ProcessInterruptedException e) {
|
||
149 | return 0; |
||
150 | } |
||
151 | return limits[1]; |
||
152 | } |
||
153 | |||
154 | /*
|
||
155 | * (non-Javadoc)
|
||
156 | * @see org.gvsig.fmap.dal.coverage.store.props.HistogramComputer#refreshHistogram()
|
||
157 | */
|
||
158 | public void refreshHistogram() { |
||
159 | refresh = true;
|
||
160 | } |
||
161 | 1081 | nbrodin | |
162 | public String getLog() { |
||
163 | return null; |
||
164 | } |
||
165 | |||
166 | public boolean isCancelable() { |
||
167 | return true; |
||
168 | } |
||
169 | |||
170 | public boolean isPausable() { |
||
171 | return false; |
||
172 | } |
||
173 | |||
174 | public void setPercent(int value) { |
||
175 | progressHistogram = value; |
||
176 | } |
||
177 | 338 | nbrodin | } |