Statistics
| Revision:

gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / src / main / java / org / gvsig / raster / cache / buffer / impl / RasterBuffer.java @ 1001

History | View | Annotate | Download (8.98 KB)

1
package org.gvsig.raster.cache.buffer.impl;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.io.IOException;
5

    
6
import org.gvsig.raster.cache.buffer.Band;
7
import org.gvsig.raster.cache.buffer.Buffer;
8
import org.gvsig.raster.cache.buffer.BufferStats;
9
import org.gvsig.raster.cache.buffer.exception.BandNotCompatibleException;
10
import org.gvsig.raster.cache.buffer.exception.HistogramException;
11
import org.gvsig.raster.cache.buffer.exception.OperationNotSupportedException;
12
import org.gvsig.raster.cache.buffer.exception.WrongParameterException;
13
import org.gvsig.raster.cache.buffer.histogram.HistogramComputer;
14
import org.gvsig.raster.cache.buffer.impl.histogram.BufferHistogramComputer;
15

    
16
/**
17
 * Base class for a raster buffer.
18
 * @author Nacho Brodin (nachobrodin@gmail.com)
19
 *
20
 */
21
public abstract class RasterBuffer extends RasterBase implements Buffer {        
22
        private BufferStats          statistics            = null;
23
        private Rectangle2D          dataExtent            = null;
24
        private HistogramComputer    histogramComputer     = null;
25
        private Object               store                 = null;
26
        
27
        /**
28
         * Valor con el que se rellena una banda no valida del buffer. Una banda no valida es la que 
29
         * no tiene datos asignados y tampoco puede ser null. Todas las bandas no validas de un buffer
30
         * apuntan por referencia a la misma banda.
31
         */
32
        protected double             notValidValue         = 0D;
33
        
34
        /**
35
         * Sets size and position values
36
         * @param  x
37
         *         Upper left X position 
38
         * @param  y
39
         *         Upper left X position
40
         * @param  w
41
         *         Width
42
         * @param  h
43
         *         Height
44
         */
45
        public RasterBuffer(int x, int y, int w, int h) {
46
                super(x, y, w, h);
47
        }
48
        
49
        /**
50
         * Sets size and position values
51
         * @param  x
52
         *         Upper left X position 
53
         * @param  y
54
         *         Upper left X position
55
         * @param  w
56
         *         Width
57
         * @param  h
58
         *         Height
59
         */
60
        public RasterBuffer(int x, int y, int w, int h, int dataType, int nBands) {
61
                super(x, y, w, h, dataType, nBands);
62
        }
63
        
64
        public static int getBytesFromRasterBufType(int rasterBufType) {
65
                switch (rasterBufType) {
66
                        case TYPE_BYTE: return 1;
67
                        case TYPE_USHORT:
68
                        case TYPE_SHORT: return 2;
69
                        case TYPE_INT:
70
                        case TYPE_FLOAT:
71
                        case TYPE_DOUBLE: return 8;
72
                }
73
                return 0; // TYPE_UNDEFINED
74
        }
75
        
76
        /*
77
         * (non-Javadoc)
78
         * @see org.gvsig.raster.cache.buffer.Buffer#setStore(java.lang.Object)
79
         */
80
        public void setStore(Object store) {
81
                this.store = store;
82
        }
83
        
84
        /*
85
         * (non-Javadoc)
86
         * @see org.gvsig.raster.cache.buffer.Buffer#getStore()
87
         */
88
        public Object getStore() {
89
                return store;
90
        }
91
        
92
        /**
93
         * Gets a bouding box of this buffer
94
         * @return
95
         */
96
        public Rectangle2D getDataExtent() {
97
                return dataExtent;
98
        }
99
        
100
        /**
101
         * Sets a bounding box of this buffer
102
         * @param r
103
         */
104
        public void setDataExtent(Rectangle2D r) {
105
                this.dataExtent = r;
106
        }
107
        
108
        /*
109
         * (non-Javadoc)
110
         * @see org.gvsig.raster.cache.buffer.Buffer#addDrawableBands(int[])
111
         */
112
        public void addDrawableBands(int[] bands) {
113
                
114
        }
115
        
116
        /*
117
         * (non-Javadoc)
118
         * @see org.gvsig.raster.cache.buffer.Buffer#copyBand(int, org.gvsig.raster.cache.buffer.Band)
119
         */
120
        public void copyBand(int iBand, Band band) throws BandNotCompatibleException, OperationNotSupportedException {
121
                if(band.getDataType() != this.getDataType())
122
                        throw new BandNotCompatibleException("Diferent data types");
123
                if(band.getWidth() != this.getWidth())
124
                        throw new BandNotCompatibleException("Diferent sizes");
125
                if(band.getHeight() != this.getHeight())
126
                        throw new BandNotCompatibleException("Diferent sizes");
127
                if(iBand < 0 || iBand > getBandCount() - 1)
128
                        throw new BandNotCompatibleException("Wrong position");
129
                
130
                switch (getDataType()) {
131
                case RasterBuffer.TYPE_BYTE:        
132
                        for (int row = 0; row < getHeight(); row++) 
133
                                for (int col = 0; col < getWidth(); col++) 
134
                                        setElem(row, col, iBand, band.getElemByte(row, col));
135
                        break;
136
                case RasterBuffer.TYPE_SHORT:
137
                        for (int row = 0; row < getHeight(); row++) 
138
                                for (int col = 0; col < getWidth(); col++) 
139
                                        setElem(row, col, iBand, band.getElemShort(row, col));
140
                        break;
141
                case RasterBuffer.TYPE_INT:
142
                        for (int row = 0; row < getHeight(); row++) 
143
                                for (int col = 0; col < getWidth(); col++) 
144
                                        setElem(row, col, iBand, band.getElemInt(row, col));
145
                        break;
146
                case RasterBuffer.TYPE_FLOAT:
147
                        for (int row = 0; row < getHeight(); row++) 
148
                                for (int col = 0; col < getWidth(); col++) 
149
                                        setElem(row, col, iBand, band.getElemFloat(row, col));
150
                        break;
151
                case RasterBuffer.TYPE_DOUBLE:
152
                        for (int row = 0; row < getHeight(); row++) 
153
                                for (int col = 0; col < getWidth(); col++) 
154
                                        setElem(row, col, iBand, band.getElemDouble(row, col));
155
                        break;
156
                }
157
        }
158
                
159
        /**
160
         * Cambia bandas de posici?n. Las posiciones deben existir como bandas del raster. 
161
         * Cada elemento del array representa una banda existente en el buffer (de longitud
162
         * rasterBuf.length) y el valor contenido dentro la banda que le corresponde. Por ejemplo
163
         * si pasamos un array {1, 0, 3, 2} significa que el buffer tiene cuatro bandas y que 
164
         * cambiamos la 0 por la 1 y la 2 por la 3. Un array {0, 1, 2, 3} en el mismo 
165
         * caso no producir?a nig?n cambio.
166
         * 
167
         * Si quisieramos asignar en un buffer monobanda su banda a la segunda posici?n habria
168
         * que insertar una vacia, por ejemplo con addBandFloat(0, null) se insertaria una 
169
         * banda nula en la posici?n 0 y la banda que estaba en la 0 pasar?a a la segunda.
170
         * @throws WrongParameterException 
171
         * @throws IOException 
172
         * 
173
         */
174
        public void swapBands(int[] bandPosition) throws WrongParameterException, IOException {
175
                if(bandPosition.length != getBandCount())
176
                    throw new WrongParameterException("Number of elements not valid");
177
            for (int i = 0; i < bandPosition.length; i++) {
178
                        if(bandPosition[i] < 0 || bandPosition[i] > getBandCount())
179
                                throw new WrongParameterException("Array not valid");
180
                }
181
        }
182
        
183
        /*
184
         * (non-Javadoc)
185
         * @see org.gvsig.raster.cache.buffer.Buffer#swapBands(int, int)
186
         */
187
        public void swapBands(int band1, int band2) throws WrongParameterException, IOException {
188
                if(band1 < 0 || band1 > getBandCount() - 1 || band2 < 0 || band2 > getBandCount() - 1)
189
                        throw new WrongParameterException("Wrong band number");
190
        }
191
                
192
        /**
193
         * Convierte un tipo de dato a cadena
194
         * @param type Tipo de dato
195
         * @return cadena  que representa el tipo de dato
196
         */
197
        public static String typesToString(int type) {
198
                switch (type) {
199
                case RasterBuffer.TYPE_BYTE:
200
                        return new String("Byte");
201

    
202
                case RasterBuffer.TYPE_DOUBLE:
203
                        return new String("Double");
204

    
205
                case RasterBuffer.TYPE_FLOAT:
206
                        return new String("Float");
207

    
208
                case RasterBuffer.TYPE_INT:
209
                        return new String("Integer");
210

    
211
                case RasterBuffer.TYPE_USHORT:
212
                case RasterBuffer.TYPE_SHORT:
213
                        return new String("Short");
214
                }
215

    
216
                return null;
217
        }
218
        
219
        /*
220
         * (non-Javadoc)
221
         * @see org.gvsig.raster.cache.buffer.Buffer#isInside(int, int)
222
         */
223
        public boolean isInside(int x, int y) {
224
                if (x < 0 || y < 0 || x >= getWidth() || y >= getHeight())
225
                        return false;
226
                return true;
227
        }
228
                
229
        /*
230
         * (non-Javadoc)
231
         * @see org.gvsig.raster.cache.buffer.Buffer#getNotValidValue()
232
         */
233
        public double getNotValidValue(){
234
                return notValidValue;
235
        }
236
        
237
        /*
238
         * (non-Javadoc)
239
         * @see org.gvsig.raster.cache.buffer.Buffer#setNotValidValue(double)
240
         */
241
        public void setNotValidValue(double value){
242
                this.notValidValue = value;
243
        }
244
        
245
        /*
246
         * (non-Javadoc)
247
         * @see org.gvsig.raster.cache.buffer.Buffer#cloneBuffer()
248
         */
249
        public abstract Buffer cloneBuffer();
250

    
251
        /*
252
         * (non-Javadoc)
253
         * @see org.gvsig.raster.cache.buffer.Buffer#getAdjustedWindow(int, int, int)
254
         */
255
        public Buffer getAdjustedWindow(int w, int h, int interpolationMethod) throws InterruptedException {
256
                BufferInterpolationImpl interp = new BufferInterpolationImpl(this);
257

    
258
                if (w == getWidth() && h == getHeight())
259
                        return this;
260
                Buffer rasterBuf = null;
261
                switch (interpolationMethod) {
262
                        case BufferInterpolationImpl.INTERPOLATION_NearestNeighbour:
263
                                rasterBuf = interp.adjustRasterNearestNeighbourInterpolation(w, h);
264
                                break;
265
                        case BufferInterpolationImpl.INTERPOLATION_Bilinear:
266
                                rasterBuf = interp.adjustRasterBilinearInterpolation(w, h);
267
                                break;
268
                        case BufferInterpolationImpl.INTERPOLATION_InverseDistance:
269
                                rasterBuf = interp.adjustRasterInverseDistanceInterpolation(w, h);
270
                                break;
271
                        case BufferInterpolationImpl.INTERPOLATION_Bicubic:
272
                                rasterBuf = interp.adjustRasterBicubicSplineInterpolation(w, h);
273
                                break;
274
                        case BufferInterpolationImpl.INTERPOLATION_BSpline:
275
                                rasterBuf = interp.adjustRasterBSplineInterpolation(w, h);
276
                                break;
277
                }
278
                if (rasterBuf != null)
279
                        return rasterBuf;
280
                else
281
                        return this;
282
        }
283
        
284
        /*
285
         * (non-Javadoc)
286
         * @see org.gvsig.raster.cache.buffer.Buffer#getStatistics()
287
         */
288
        public BufferStats getStatistics() {
289
                if(statistics == null)
290
                        statistics = new BufferStatsImpl(this);
291
                return statistics;
292
        }
293
        
294
        /*
295
         * (non-Javadoc)
296
         * @see org.gvsig.raster.cache.buffer.histogram.Histogramable#getHistogramComputer()
297
         */
298
        public HistogramComputer getHistogramComputer() throws HistogramException, InterruptedException {
299
                if(histogramComputer == null)
300
                        histogramComputer = new BufferHistogramComputer(this);
301
                return histogramComputer;
302
        }
303
        
304
}