Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / buffer / RasterMemoryBuffer.java @ 11453

History | View | Annotate | Download (36.5 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.raster.buffer;
20

    
21
import org.gvsig.raster.dataset.IBuffer;
22

    
23
/**
24
 * Implementaci?n del buffer de datos en memoria. Contiene las operaciones necesarias
25
 * para acceso a datos raster implementando IBuffer y su almacenamiento de datos est? basado
26
 * en arrays tridimensionales. Existe un array para cada tipo de dato pero al instanciarse
27
 * solo se usar? uno de ellos, el que corresponda al tipo de dato del raster manejado. 
28
 * Esto quiere decir que cada RasterMemoryBuffer solo puede contener bandas de un solo tipo de
29
 * dato donde la variable dataType especificar? de que tipo de dato se trata.
30
 * 
31
 * @author Nacho Brodin (nachobrodin@gmail.com)
32
 *
33
 */
34
public class RasterMemoryBuffer extends RasterBuffer {
35
        private ByteBand[]                byteBuf = null;
36
    private ShortBand[]         shortBuf = null;
37
    private IntBand[]                 intBuf = null;
38
    private FloatBand[]        floatBuf = null;
39
    private DoubleBand[]        doubleBuf = null;
40
    
41
    public class ByteBand extends RasterBand{
42
            public byte[][]         buf = null;
43
            
44
            public ByteBand(int height, int width, boolean malloc){
45
                    super(height, width);
46
                    if(malloc)
47
                            buf = new byte[height][width];
48
            }
49

    
50
                public Object getLine(int line) {
51
                        return buf[line];
52
                }
53
                
54
                public void setLine(int line, Object value){
55
                        buf[line] = (byte[])value;
56
                }
57
                
58
                public Object getBuf(){
59
                        return buf;
60
                }
61
    }
62
    
63
    public class ShortBand extends RasterBand{
64
            public short[][]         buf = null;
65
            
66
            public ShortBand(int height, int width, boolean malloc){
67
                    super(height, width);
68
                    if(malloc)
69
                            buf = new short[height][width];
70
            }
71
            
72
            public Object getLine(int line) {
73
                        return buf[line];
74
                }
75
            
76
                public void setLine(int line, Object value){
77
                        buf[line] = (short[])value;
78
                }
79
                
80
                public Object getBuf(){
81
                        return buf;
82
                }
83
    }
84

    
85
        public class IntBand extends RasterBand{
86
                public int[][]         buf = null;
87
                
88
                public IntBand(int height, int width, boolean malloc){
89
                        super(height, width);
90
                        if(malloc)
91
                            buf = new int[height][width];
92
            }
93
                
94
                public Object getLine(int line) {
95
                        return buf[line];
96
                }
97
                
98
                public void setLine(int line, Object value){
99
                        buf[line] = (int[])value;
100
                }
101
                
102
                public Object getBuf(){
103
                        return buf;
104
                }
105
        }
106

    
107
        public class FloatBand extends RasterBand{
108
                public float[][]         buf = null;
109
                
110
                public FloatBand(int height, int width, boolean malloc){
111
                        super(height, width);
112
                        if(malloc)
113
                            buf = new float[height][width];
114
            }
115
                
116
                public Object getLine(int line) {
117
                        return buf[line];
118
                }
119
                
120
                public void setLine(int line, Object value){
121
                        buf[line] = (float[])value;
122
                }
123
                
124
                public Object getBuf(){
125
                        return buf;
126
                }
127
        }
128

    
129
        public class DoubleBand extends RasterBand{
130
                public double[][]         buf = null;
131
                
132
                public DoubleBand(int height, int width, boolean malloc){
133
                        super(height, width);
134
                        if(malloc)
135
                            buf = new double[height][width];
136
            }
137
                
138
                public Object getLine(int line) {
139
                        return buf[line];
140
                }
141
                
142
                public void setLine(int line, Object value){
143
                        buf[line] = (double[])value;
144
                }
145
                
146
                public Object getBuf(){
147
                        return buf;
148
                }
149
        }
150
        
151
    /**
152
     * Constructor
153
     */
154
    public RasterMemoryBuffer() {
155
            
156
    }
157
    
158
    /*
159
     * (non-Javadoc)
160
     * @see org.gvsig.raster.driver.IBuffer#isBandSwitchable()
161
     */
162
    public boolean isBandSwitchable(){
163
            return true;
164
    }
165
    
166
    /**
167
     * Constructor
168
     * @param dataType Tipo de dato
169
     * @param width Ancho
170
     * @param height Alto
171
     * @param bandNr Banda
172
     * @param orig
173
     */
174
    public RasterMemoryBuffer(int dataType, int width, int height, int bandNr, boolean malloc) {
175
            if(malloc)
176
                    malloc(dataType, width, height, bandNr);
177
            else
178
                    loadVariables(dataType, width, height, bandNr);
179
    }
180

    
181
    /*
182
     *  (non-Javadoc)
183
     * @see org.gvsig.fmap.dataaccess.buffer.RasterBuffer#malloc(int, int, int, int)
184
     */
185
    public void malloc(int dataType, int width, int height, int bandNr) {
186
            this.dataType = dataType;
187
        this.width = width;
188
        this.height = height;
189
        this.nBands = bandNr;
190

    
191
        if (dataType == TYPE_BYTE) {
192
            byteBuf = new ByteBand[bandNr];
193
            for(int i = 0; i < bandNr; i++)
194
                    byteBuf[i] = new ByteBand(height, width, true);
195
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
196
            shortBuf = new ShortBand[bandNr];
197
            for(int i = 0; i < bandNr; i++)
198
                    shortBuf[i] = new ShortBand(height, width, true);
199
        } else if (dataType == TYPE_INT) {
200
            intBuf = new IntBand[bandNr];
201
            for(int i = 0; i < bandNr; i++)
202
                    intBuf[i] = new IntBand(height, width, true);
203
        } else if (dataType == TYPE_FLOAT) {
204
            floatBuf = new FloatBand[bandNr];
205
            for(int i = 0; i < bandNr; i++)
206
                    floatBuf[i] = new FloatBand(height, width, true);
207
        } else if (dataType == TYPE_DOUBLE) {
208
            doubleBuf = new DoubleBand[bandNr];
209
            for(int i = 0; i < bandNr; i++)
210
                    doubleBuf[i] = new DoubleBand(height, width, true);
211
        }
212

    
213
    }
214

    
215
    /*
216
     *  (non-Javadoc)
217
     * @see org.gvsig.fmap.dataaccess.buffer.RasterBuffer#mallocOneBand(int, int, int, int)
218
     */
219
    public void mallocOneBand(int dataType, int width, int height, int band){
220
            this.dataType = dataType;
221
        this.width = width;
222
        this.height = height;
223
        
224
        if (dataType == TYPE_BYTE) {
225
                if(byteBuf == null)
226
                        byteBuf = new ByteBand[nBands];
227
            byteBuf[band] = new ByteBand(height, width, true);
228
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
229
                if(shortBuf == null)
230
                        shortBuf = new ShortBand[nBands];
231
            shortBuf[band] = new ShortBand(height, width, true);
232
        } else if (dataType == TYPE_INT) {
233
                if(intBuf == null)
234
                        intBuf = new IntBand[nBands];
235
            intBuf[band] = new IntBand(height, width, true);
236
        } else if (dataType == TYPE_FLOAT) {
237
                if(floatBuf == null)
238
                        floatBuf = new FloatBand[nBands];
239
            floatBuf[band] = new FloatBand(height, width, true);
240
        } else if (dataType == TYPE_DOUBLE) {
241
                if(doubleBuf == null)
242
                        doubleBuf = new DoubleBand[nBands];
243
            doubleBuf[band] = new DoubleBand(height, width, true);
244
        }
245
    }
246
    
247
    /**
248
     * Carga las variables del rasterBuf
249
     * @param dataType Tipo de dato
250
     * @param width Ancho
251
     * @param height Alto
252
     * @param bandNr Banda
253
     * @param orig
254
     */
255
    private void loadVariables(int dataType, int width, int height, int bandNr) {
256
            this.dataType = dataType;
257
        this.width = width;
258
        this.height = height;
259
        this.nBands = bandNr;
260

    
261
        if (dataType == TYPE_BYTE) {
262
                byteBuf = new ByteBand[bandNr];
263
                for(int i = 0; i < bandNr; i ++)
264
                        byteBuf[i] = new ByteBand(height, 0, true);
265
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
266
                shortBuf = new ShortBand[bandNr];
267
                for(int i = 0; i < bandNr; i ++)
268
                        shortBuf[i] = new ShortBand(height, 0, true);
269
        } else if (dataType == TYPE_INT) {
270
                intBuf = new IntBand[bandNr];
271
                for(int i = 0; i < bandNr; i ++)
272
                        intBuf[i] = new IntBand(height, 0, true);
273
        } else if (dataType == TYPE_FLOAT) {
274
                floatBuf = new FloatBand[bandNr];
275
                for(int i = 0; i < bandNr; i ++)
276
                        floatBuf[i] = new FloatBand(height, 0, true);
277
        } else if (dataType == TYPE_DOUBLE) {
278
                doubleBuf = new DoubleBand[bandNr];
279
                for(int i = 0; i < bandNr; i ++)
280
                        doubleBuf[i] = new DoubleBand(height, 0, true);
281
        }
282

    
283
    }
284

    
285
    /*
286
     *  (non-Javadoc)
287
     * @see org.gvsig.fmap.driver.IBuffer#getWidth()
288
     */
289
    public int getWidth() {
290
        return width;
291
    }
292

    
293
    /*
294
     *  (non-Javadoc)
295
     * @see org.gvsig.fmap.driver.IBuffer#getHeight()
296
     */
297
    public int getHeight() {
298
        return height;
299
    }
300

    
301
    /*
302
     *  (non-Javadoc)
303
     * @see org.gvsig.fmap.driver.IBuffer#getBandCount()
304
     */
305
    public int getBandCount() {
306
        return nBands;
307
    }
308

    
309
    /**
310
     * Obtiene el tipo de dato. Los tipos de dato posibles est?n definidos en IRaster.
311
     * @return tipo de datos
312
     */
313
        public int getDataType() {
314
                return dataType;
315
        }
316
        
317
        /**
318
         * Asigna el tipo de dato. Los tipos de dato posibles est?n definidos en IRaster.
319
         * @param dataType Tipo de dato del buffer
320
         */
321
        public void setDataType(int dataType) {
322
                this.dataType = dataType;
323
        }
324

    
325
    /**
326
     * Obtiene el tama?o del tipo de dato en bytes
327
     * @return Tipo de dato
328
     */
329
    public int getDataSize() {
330
        if (dataType == TYPE_BYTE) {
331
            return 1;
332
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
333
            return 2;
334
        } else if (dataType == TYPE_INT) {
335
            return 4;
336
        }else if (dataType == TYPE_FLOAT) {
337
            return 8;
338
        }else if (dataType == TYPE_DOUBLE) {
339
            return 16;
340
        }
341

    
342
        return 0;
343
    }
344

    
345
    /**
346
     * Obtiene el tama?o del buffer
347
     * @return tama?o del buffer
348
     */
349
    public long sizeof() {
350
        return getDataSize() * width * height * nBands;
351
    }
352

    
353
    //***********************************************
354
    //Obtiene una linea de datos con todas las bandas
355
    
356
    public byte[][] getLineByte(int line) {
357
            byte[][] r = new byte[nBands][];
358
            for(int iBand = 0; iBand < nBands; iBand ++)
359
                    r[iBand] = (byte[])byteBuf[iBand].getLine(line);
360
            return r;
361
    }
362

    
363
    public short[][] getLineShort(int line) {
364
            short[][] r = new short[nBands][];
365
            for(int iBand = 0; iBand < nBands; iBand ++)
366
                    r[iBand] = (short[])shortBuf[iBand].getLine(line);
367
            return r;
368
    }
369

    
370
    public int[][] getLineInt(int line) {
371
            int[][] r = new int[nBands][];
372
            for(int iBand = 0; iBand < nBands; iBand ++)
373
                    r[iBand] = (int[])intBuf[iBand].getLine(line);
374
            return r;
375
    }
376
        
377
    public float[][] getLineFloat(int line) {
378
            float[][] r = new float[nBands][];
379
            for(int iBand = 0; iBand < nBands; iBand ++)
380
                    r[iBand] = (float[])floatBuf[iBand].getLine(line);
381
            return r;
382
    }
383
    
384
    public double[][] getLineDouble(int line) {
385
            double[][] r = new double[nBands][];
386
            for(int iBand = 0; iBand < nBands; iBand ++)
387
                    r[iBand] = (double[])doubleBuf[iBand].getLine(line);
388
            return r;
389
    }
390

    
391
    //***********************************************
392
    //Asigna una linea de datos a todas las bandas
393
    
394
    public void setLineByte(byte[][] data, int line) {
395
            for(int iBand = 0; iBand < nBands; iBand ++)
396
                    byteBuf[iBand].setLine(line, data[iBand]);
397
    }
398

    
399
    public void setLineShort(short[][] data, int line) {
400
            for(int iBand = 0; iBand < nBands; iBand ++)
401
                    shortBuf[iBand].setLine(line, data[iBand]);
402
    }
403

    
404
    public void setLineInt(int[][] data, int line) {
405
            for(int iBand = 0; iBand < nBands; iBand ++)
406
                    intBuf[iBand].setLine(line, data[iBand]);
407
    }
408
        
409
    public void setLineFloat(float[][] data, int line) {
410
            for(int iBand = 0; iBand < nBands; iBand ++)
411
                    floatBuf[iBand].setLine(line, data[iBand]);
412
    }
413
    
414
    public void setLineDouble(double[][] data, int line) {
415
            for(int iBand = 0; iBand < nBands; iBand ++)
416
                    doubleBuf[iBand].setLine(line, data[iBand]);
417
    }
418
    
419
    //***********************************************
420
    //Obtiene una linea de datos de una banda
421
    
422
    public byte[] getLineFromBandByte(int line, int band) {
423
        return (byte[])byteBuf[band].getLine(line);
424
    }
425

    
426
    public short[] getLineFromBandShort(int line, int band) {
427
        return (short[])shortBuf[band].getLine(line);
428
    }
429

    
430
    public int[] getLineFromBandInt(int line, int band) {
431
        return (int[])intBuf[band].getLine(line);
432
    }
433
        
434
    public float[] getLineFromBandFloat(int line, int band) {
435
        return (float[])floatBuf[band].getLine(line);
436
    }
437
    
438
    public double[] getLineFromBandDouble(int line, int band) {
439
        return (double[])doubleBuf[band].getLine(line);
440
    }
441
        
442
    //***********************************************
443
    //Asigna una linea de datos a una banda
444
    
445
    public void setLineInBandByte(byte[] data, int line, int band) {
446
        byteBuf[band].setLine(line, data);
447
    }
448

    
449
    public void setLineInBandShort(short[] data, int line, int band) {
450
        shortBuf[band].setLine(line, data);
451
    }
452

    
453
    public void setLineInBandInt(int[] data, int line, int band) {
454
        intBuf[band].setLine(line, data);
455
    }
456
        
457
    public void setLineInBandFloat(float[] data, int line, int band) {
458
        floatBuf[band].setLine(line, data);
459
    }
460
    
461
    public void setLineInBandDouble(double[] data, int line, int band) {
462
        doubleBuf[band].setLine(line, data);
463
    }
464
    
465
    //***********************************************    
466
    //Obtiene un elemento de la matriz
467
    
468
    public byte getElemByte(int line, int col, int band) {
469
        return byteBuf[band].buf[line][col];
470
    }
471
    
472
    public short getElemShort(int line, int col, int band) {
473
        return shortBuf[band].buf[line][col];
474
    }
475
    
476
    public int getElemInt(int line, int col, int band) {
477
        return intBuf[band].buf[line][col];
478
    }
479
    
480
    public float getElemFloat(int line, int col, int band) {
481
        return floatBuf[band].buf[line][col];
482
    }
483
    
484
    public double getElemDouble(int line, int col, int band) {
485
        return doubleBuf[band].buf[line][col];
486
    }
487
    
488
    //**********************************************    
489
    //Asigna un elemento de la matriz
490
    
491
    public void setElem(int line, int col, int band, byte data) {
492
            byteBuf[band].buf[line][col] = data;
493
    }
494
    
495
    public void setElem(int line, int col, int band, short data) {
496
            shortBuf[band].buf[line][col] = data;
497
    }
498
    
499
    public void setElem(int line, int col, int band, int data) {
500
            intBuf[band].buf[line][col] = data;
501
    }
502
    
503
    public void setElem(int line, int col, int band, float data) {
504
            floatBuf[band].buf[line][col] = data;
505
    }
506
    
507
    public void setElem(int line, int col, int band, double data) {
508
            doubleBuf[band].buf[line][col] = data;
509
    }
510
    
511
    //***********************************************
512
    //Copia un elemento de todas la bandas en el buffer pasado por par?metro
513
    
514
    public void getElemByte(int line, int col, byte[] data) {
515
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
516
            data[iBand] = byteBuf[iBand].buf[line][col];
517
    }
518
    
519
    public void getElemShort(int line, int col, short[] data) {
520
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
521
            data[iBand] = shortBuf[iBand].buf[line][col];
522
    }
523
    
524
    public void getElemInt(int line, int col, int[] data) {
525
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
526
            data[iBand] = intBuf[iBand].buf[line][col];
527
    }  
528
    
529
    public void getElemFloat(int line, int col, float[] data) {
530
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
531
            data[iBand] = floatBuf[iBand].buf[line][col];
532
    }
533
    
534
    public void getElemDouble(int line, int col, double[] data) {
535
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
536
            data[iBand] = doubleBuf[iBand].buf[line][col];
537
    }
538

    
539
    //***********************************************
540
    //Asigna un elemento a todas la bandas en el buffer pasado por par?metro
541
    
542
    public void setElemByte(int line, int col, byte[] data) {
543
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
544
            byteBuf[iBand].buf[line][col] = data[iBand];
545
    }
546
    
547
    public void setElemShort(int line, int col, short[] data) {
548
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
549
            shortBuf[iBand].buf[line][col] = data[iBand];
550
    }
551
    
552
    public void setElemInt(int line, int col, int[] data) {
553
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
554
            intBuf[iBand].buf[line][col] = data[iBand];
555
    }
556

    
557
    public void setElemFloat(int line, int col, float[] data) {
558
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
559
            floatBuf[iBand].buf[line][col] = data[iBand];
560
    }
561
    
562
    public void setElemDouble(int line, int col, double[] data) {
563
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
564
            doubleBuf[iBand].buf[line][col] = data[iBand];
565
    }
566
    
567
    //***********************************************
568
    //Obtiene una banda entera
569
    
570
    public IBand getBand(int band){
571
            if (dataType == TYPE_BYTE) {
572
                    return byteBuf[band];
573
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
574
                return shortBuf[band];
575
        } else if (dataType == TYPE_INT) {
576
                return intBuf[band];
577
        }else if (dataType == TYPE_FLOAT) {
578
                return floatBuf[band];
579
        }else if (dataType == TYPE_DOUBLE) {
580
                return doubleBuf[band];
581
        }
582
            return null;
583
    }
584
    
585
    /*
586
     *  (non-Javadoc)
587
     * @see org.gvsig.fmap.driver.IBuffer#getBandBuffer(int)
588
     */
589
    public IBuffer getBandBuffer(int iBand){
590
            RasterMemoryBuffer rmb = new RasterMemoryBuffer(dataType, width, height, 1, false);
591
            if (dataType == TYPE_BYTE) 
592
                    rmb.byteBuf[0].buf = byteBuf[iBand].buf;
593
        else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT))
594
                rmb.shortBuf[0].buf = shortBuf[iBand].buf;
595
        else if (dataType == TYPE_INT)
596
                rmb.intBuf[0].buf = intBuf[iBand].buf;
597
        else if (dataType == TYPE_FLOAT)
598
                rmb.floatBuf[0].buf = floatBuf[iBand].buf;
599
        else if (dataType == TYPE_DOUBLE)
600
                rmb.doubleBuf[0].buf = doubleBuf[iBand].buf;
601
            return rmb;
602
    }
603
    //***********************************************
604
    //Inicializa una banda a un valor pasado por par?metro
605
    
606
    public void assign(int band, byte value) {
607
            for(int line = 0; line < height; line ++)
608
                    for(int col = 0; col < width; col ++)
609
                            byteBuf[band].buf[line][col] = value;
610
    }
611
    
612
    public void assign(int band, short value) {
613
            for(int line = 0; line < height; line ++)
614
                    for(int col = 0; col < width; col ++)
615
                            shortBuf[band].buf[line][col] = value;
616
    }
617
    
618
    public void assign(int band, int value) {
619
            for(int line = 0; line < height; line ++)
620
                    for(int col = 0; col < width; col ++)
621
                            intBuf[band].buf[line][col] = value;
622
    }
623

    
624
    public void assign(int band, float value) {
625
            for(int line = 0; line < height; line ++)
626
                    for(int col = 0; col < width; col ++)
627
                            floatBuf[band].buf[line][col] = value;
628
    }
629
    
630
    public void assign(int band, double value) {
631
            for(int line = 0; line < height; line ++)
632
                    for(int col = 0; col < width; col ++)
633
                            doubleBuf[band].buf[line][col] = value;
634
    }
635
    
636
    //***********************************************
637
    //Crea un buffer banda inicializado con el valor pasado por par?metro
638
    
639

    
640
    public IBand createBand(byte defaultValue){
641
            switch(getDataType()){
642
            case RasterBuffer.TYPE_BYTE:ByteBand bb = new ByteBand(width, height, false);
643
                                                                    bb.buf = createByteBand(width, height, defaultValue);
644
                                                                    return bb;
645
            case RasterBuffer.TYPE_SHORT:        ShortBand sb = new ShortBand(width, height, false);
646
                                                                                sb.buf = createShortBand(width, height, defaultValue);
647
                                                                                return sb;
648
            case RasterBuffer.TYPE_INT:        IntBand ib = new IntBand(width, height, false);
649
                                                                        ib.buf = createIntBand(width, height, defaultValue);
650
                                                                        return ib;
651
            case RasterBuffer.TYPE_FLOAT:        FloatBand fb = new FloatBand(width, height, false);
652
                                                                                fb.buf = createFloatBand(width, height, defaultValue);
653
                                                                                return fb;
654
            case RasterBuffer.TYPE_DOUBLE:        DoubleBand db = new DoubleBand(width, height, false);
655
                                                                                db.buf = createDoubleBand(width, height, defaultValue);
656
                                                                                return db;
657
            }
658
            return null;
659
    }
660

    
661
    public byte[][] createByteBand(int width, int height, byte defaultValue){
662
            byte[][] band = new byte[height][width];
663
            if(defaultValue != 0){
664
                    for(int line = 0; line < height; line ++)
665
                            for(int col = 0; col < width; col ++)
666
                                    band[line][col] = defaultValue;
667
            }
668
            return band;
669
    }
670
    
671
    public short[][] createShortBand(int width, int height, short defaultValue){
672
            short[][] band = new short[height][width];
673
            if(defaultValue != 0){
674
                    for(int line = 0; line < height; line ++)
675
                            for(int col = 0; col < width; col ++)
676
                                    band[line][col] = defaultValue;
677
            }
678
            return band;
679
    }
680
    
681
    public int[][] createIntBand(int width, int height, int defaultValue){
682
            int[][] band = new int[height][width];
683
            if(defaultValue != 0){
684
                    for(int line = 0; line < height; line ++)
685
                            for(int col = 0; col < width; col ++)
686
                                    band[line][col] = defaultValue;
687
            }
688
            return band;
689
    }
690
    
691
    public float[][] createFloatBand(int width, int height, float defaultValue){
692
            float[][] band = new float[height][width];
693
            if(defaultValue != 0){
694
                    for(int line = 0; line < height; line ++)
695
                            for(int col = 0; col < width; col ++)
696
                                    band[line][col] = defaultValue;
697
            }
698
            return band;
699
    }
700
    
701
    public double[][] createDoubleBand(int width, int height, double defaultValue){
702
            double[][] band = new double[height][width];
703
            if(defaultValue != 0){
704
                    for(int line = 0; line < height; line ++)
705
                            for(int col = 0; col < width; col ++)
706
                                    band[line][col] = defaultValue;
707
            }
708
            return band;
709
    }
710
    
711
    //***********************************************
712
    //A?ade una banda entera. Los datos son asignados por referencia
713

    
714
    private void addBandByte(int pos, IBand data) {
715
            if(pos < 0)
716
                    return;
717
            ByteBand[] tmp = null;
718
            if(pos >= byteBuf.length){
719
                    tmp = new ByteBand[pos + 1];
720
                    for(int iBand = 0; iBand < byteBuf.length; iBand ++)
721
                            tmp[iBand] = byteBuf[iBand];
722
                    tmp[pos] = (ByteBand)data;
723
            }else{
724
                    tmp = new ByteBand[byteBuf.length + 1];
725
                    for(int iBand = 0; iBand < pos; iBand ++)
726
                            tmp[iBand] = byteBuf[iBand];
727
                    tmp[pos] = (ByteBand)data;
728
                    for(int iBand = pos + 1; iBand <= byteBuf.length; iBand ++)
729
                            tmp[iBand] = byteBuf[iBand - 1];
730
            }
731
            nBands = tmp.length;
732
            byteBuf = tmp;
733
    }
734
    
735
    private void addBandShort(int pos, IBand data) {
736
            if(pos < 0)
737
                    return;
738
            ShortBand[] tmp = null;
739
            if(pos >= shortBuf.length){
740
                    tmp = new ShortBand[pos + 1];
741
                    for(int iBand = 0; iBand < shortBuf.length; iBand ++)
742
                            tmp[iBand] = shortBuf[iBand];
743
                    tmp[pos] = (ShortBand)data;
744
            }else{
745
                    tmp = new ShortBand[shortBuf.length + 1];
746
                    for(int iBand = 0; iBand < pos; iBand ++)
747
                            tmp[iBand] = shortBuf[iBand];
748
                    tmp[pos] = (ShortBand)data;
749
                    for(int iBand = pos + 1; iBand < shortBuf.length; iBand ++)
750
                            tmp[iBand] = shortBuf[iBand - 1];
751
            }
752
            nBands = tmp.length;
753
            shortBuf = tmp;
754
    }
755
    
756
    private void addBandInt(int pos, IBand data) {
757
            if(pos < 0)
758
                    return;
759
            IntBand[] tmp = null;
760
            if(pos >= intBuf.length){
761
                    tmp = new IntBand[pos + 1];
762
                    for(int iBand = 0; iBand < intBuf.length; iBand ++)
763
                            tmp[iBand] = intBuf[iBand];
764
                    tmp[pos] = (IntBand)data;
765
            }else{
766
                    tmp = new IntBand[intBuf.length + 1];
767
                    for(int iBand = 0; iBand < pos; iBand ++)
768
                            tmp[iBand] = intBuf[iBand];
769
                    tmp[pos] = (IntBand)data;
770
                    for(int iBand = pos + 1; iBand < intBuf.length; iBand ++)
771
                            tmp[iBand] = intBuf[iBand - 1];
772
            }
773
            nBands = tmp.length;
774
            intBuf = tmp;
775
    }
776
    
777
    private void addBandFloat(int pos, IBand data) {
778
            if(pos < 0)
779
                    return;
780
            FloatBand[] tmp = null;
781
            if(pos >= floatBuf.length){
782
                    tmp = new FloatBand[pos + 1];
783
                    for(int iBand = 0; iBand < floatBuf.length; iBand ++)
784
                            tmp[iBand] = floatBuf[iBand];
785
                    tmp[pos] = (FloatBand)data;
786
            }else{
787
                    tmp = new FloatBand[floatBuf.length + 1];
788
                    for(int iBand = 0; iBand < pos; iBand ++)
789
                            tmp[iBand] = floatBuf[iBand];
790
                    tmp[pos] = (FloatBand)data;
791
                    for(int iBand = pos + 1; iBand < floatBuf.length; iBand ++)
792
                            tmp[iBand] = floatBuf[iBand - 1];
793
            }
794
            nBands = tmp.length;
795
            floatBuf = tmp;
796
    }
797
    
798
    private void addBandDouble(int pos, IBand data) {
799
            if(pos < 0)
800
                    return;
801
            DoubleBand[] tmp = null;
802
            if(pos >= doubleBuf.length){
803
                    tmp = new DoubleBand[pos + 1];
804
                    for(int iBand = 0; iBand < doubleBuf.length; iBand ++)
805
                            tmp[iBand] = doubleBuf[iBand];
806
                    tmp[pos] = (DoubleBand)data;
807
            }else{
808
                    tmp = new DoubleBand[doubleBuf.length + 1];
809
                    for(int iBand = 0; iBand < pos; iBand ++)
810
                            tmp[iBand] = doubleBuf[iBand];
811
                    tmp[pos] = (DoubleBand)data;
812
                    for(int iBand = pos + 1; iBand < doubleBuf.length; iBand ++)
813
                            tmp[iBand] = doubleBuf[iBand - 1];
814
            }
815
            nBands = tmp.length;
816
            doubleBuf = tmp;
817
    }
818
    
819
    //***********************************************
820
    //Reemplaza una banda entera. Los datos son reemplazados por referencia
821
    
822
   /* public void replaceBandByte(int pos, byte[][] data) {
823
            if(pos >= byteBuf.length)
824
                    return;
825
            byteBuf[pos] = data;
826
    }
827
    
828
    public void replaceBandShort(int pos, short[][] data) {
829
            if(pos >= shortBuf.length)
830
                    return;
831
            shortBuf[pos] = data;
832
    }
833
    
834
    public void replaceBandInt(int pos, int[][] data) {
835
            if(pos >= intBuf.length)
836
                    return;
837
            intBuf[pos] = data;
838
    }
839
    
840
    public void replaceBandFloat(int pos, float[][] data) {
841
            if(pos >= floatBuf.length)
842
                    return;
843
            floatBuf[pos] = data;
844
    }
845
    
846
    public void replaceBandDouble(int pos, double[][] data) {
847
            if(pos >= doubleBuf.length)
848
                    return;
849
            doubleBuf[pos] = data;
850
    }*/
851
    
852
    /**
853
     * Replica la banda de una posici?n sobre otra. Si la banda de destino no existe
854
     * se crea nueva. Si la posici?n de la banda de destino est? intercalada entre bandas 
855
     * que ya existen las otras se desplazan hacia abajo, NO se machacan los datos de ninguna.
856
     * Los datos se replican por referencia por lo que al modificar la banda original las
857
     * del resto quedar?n afectadas.   
858
     * @param orig. Posici?n de la banda de origen. 
859
     * @param dest. Posici?n de la banda destino
860
     */   
861
    public void replicateBand(int orig, int dest){
862
            switch(getDataType()){
863
            case RasterBuffer.TYPE_BYTE:        if(orig >= byteBuf.length)
864
                                                                                        return;
865
                                                                                addBandByte(dest, getBand(orig));
866
                                                                                break;
867
            case RasterBuffer.TYPE_SHORT:        if(orig >= shortBuf.length)
868
                                                                                    return;
869
                                                                            addBandShort(dest, getBand(orig));
870
                                                                            break;
871
            case RasterBuffer.TYPE_INT:         if(orig >= intBuf.length)
872
                                                                                        return;
873
                                                                                addBandInt(dest, getBand(orig));
874
                                                                                break;
875
            case RasterBuffer.TYPE_FLOAT:        if(orig >= floatBuf.length)
876
                                                                                        return;
877
                                                                                addBandFloat(dest, getBand(orig));
878
                                                                                break;
879
            case RasterBuffer.TYPE_DOUBLE:        if(orig >= doubleBuf.length)
880
                                                                                        return;
881
                                                                                addBandDouble(dest, getBand(orig));
882
                                                                                break;
883
            }
884
    }
885
    
886
    /*
887
     *  (non-Javadoc)
888
     * @see org.gvsig.fmap.dataaccess.buffer.RasterBuffer#switchBands(int[])
889
     */
890
    public void switchBands(int[] bandPosition){
891
            if(bandPosition.length != this.getBandCount())
892
                    return;
893
            for(int i = 0; i < bandPosition.length; i++)
894
                    if(bandPosition[i] >=  bandPosition.length || bandPosition[i] < 0)
895
                            return;
896
            
897
            switch (getDataType()) {
898
            case RasterBuffer.TYPE_BYTE:
899
                    ByteBand[] bufB = new ByteBand[this.getBandCount()];
900
                    for(int i = 0; i < bandPosition.length; i++)
901
                            bufB[i] = byteBuf[bandPosition[i]];
902
                    byteBuf = bufB;
903
                    break;
904
            case RasterBuffer.TYPE_DOUBLE:
905
                    DoubleBand[] bufD = new DoubleBand[this.getBandCount()];
906
                    for(int i = 0; i < bandPosition.length; i++)
907
                            bufD[i] = doubleBuf[bandPosition[i]];
908
                    doubleBuf = bufD;
909
                    break;                    
910
            case RasterBuffer.TYPE_FLOAT:
911
                    FloatBand[] bufF = new FloatBand[this.getBandCount()];
912
                    for(int i = 0; i < bandPosition.length; i++)
913
                            bufF[i] = floatBuf[bandPosition[i]];
914
                    floatBuf = bufF;
915
                    break;                     
916
            case RasterBuffer.TYPE_INT:
917
                    IntBand[] bufI = new IntBand[this.getBandCount()];
918
                    for(int i = 0; i < bandPosition.length; i++)
919
                            bufI[i] = intBuf[bandPosition[i]];
920
                    intBuf = bufI;
921
                    break;                     
922
            case RasterBuffer.TYPE_USHORT:
923
            case RasterBuffer.TYPE_SHORT:
924
                    ShortBand[] bufS = new ShortBand[this.getBandCount()];
925
                    for(int i = 0; i < bandPosition.length; i++)
926
                            bufS[i] = shortBuf[bandPosition[i]];
927
                    shortBuf = bufS;
928
                    break; 
929
            }
930
    }
931
    
932
    /*
933
     *  (non-Javadoc)
934
     * @see org.gvsig.fmap.driver.IBuffer#copyBand(int, org.gvsig.fmap.dataaccess.buffer.IBand)
935
     */
936
    public void copyBand(int nBand, IBand band) {
937
            switch (band.getDataType()) {
938
            case RasterBuffer.TYPE_BYTE:
939
                    byteBuf[nBand] = new ByteBand(band.getHeight(), band.getWidth(), true);
940
                    byte[][] bb = ((ByteBand)band).buf;
941
                    for(int i = 0; i < bb.length; i ++)
942
                            for(int j = 0; j < bb[i].length; j ++)
943
                                    byteBuf[nBand].buf[i][j] = bb[i][j];
944
                    break;
945
            case RasterBuffer.TYPE_DOUBLE:
946
                    doubleBuf[nBand] = new DoubleBand(band.getHeight(), band.getWidth(), true);
947
                    double[][] db = ((DoubleBand)band).buf;
948
                    for(int i = 0; i < db.length; i ++)
949
                            for(int j = 0; j < db[i].length; j ++)
950
                                    doubleBuf[nBand].buf[i][j] = db[i][j];                    
951
                    break;                    
952
            case RasterBuffer.TYPE_FLOAT:
953
                    floatBuf[nBand] = new FloatBand(band.getHeight(), band.getWidth(), true);
954
                    float[][] fb = ((FloatBand)band).buf;
955
                    for(int i = 0; i < fb.length; i ++)
956
                            for(int j = 0; j < fb[i].length; j ++)
957
                                    floatBuf[nBand].buf[i][j] = fb[i][j];
958
                    break;                     
959
            case RasterBuffer.TYPE_INT:
960
                    intBuf[nBand] = new IntBand(band.getHeight(), band.getWidth(), true);
961
                    int[][] ib = ((IntBand)band).buf;
962
                    for(int i = 0; i < ib.length; i ++)
963
                            for(int j = 0; j < ib[i].length; j ++)
964
                                    intBuf[nBand].buf[i][j] = ib[i][j];
965
                    break;                     
966
            case RasterBuffer.TYPE_USHORT:
967
            case RasterBuffer.TYPE_SHORT:
968
                    shortBuf[nBand] = new ShortBand(band.getHeight(), band.getWidth(), true);
969
                    short[][] sb = ((ShortBand)band).buf;
970
                    for(int i = 0; i < sb.length; i ++)
971
                            for(int j = 0; j < sb[i].length; j ++)
972
                                    shortBuf[nBand].buf[i][j] = sb[i][j];
973
                    break; 
974
            }
975
        }
976

    
977
    /*
978
     *  (non-Javadoc)
979
     * @see org.gvsig.fmap.driver.IBuffer#assignBand(int, org.gvsig.fmap.dataaccess.buffer.IBand)
980
     */
981
        public void assignBand(int nBand, IBand band) {
982
                switch (getDataType()) {
983
            case RasterBuffer.TYPE_BYTE:
984
                    byteBuf[nBand] = ((ByteBand)band);
985
                    break;
986
            case RasterBuffer.TYPE_DOUBLE:
987
                    doubleBuf[nBand] = ((DoubleBand)band);                  
988
                    break;                    
989
            case RasterBuffer.TYPE_FLOAT:
990
                    floatBuf[nBand] = ((FloatBand)band);
991
                    break;                     
992
            case RasterBuffer.TYPE_INT:
993
                    intBuf[nBand] = ((IntBand)band);
994
                    break;                     
995
            case RasterBuffer.TYPE_USHORT:
996
            case RasterBuffer.TYPE_SHORT:
997
                    shortBuf[nBand] = ((ShortBand)band);
998
                    break; 
999
            }
1000
        }
1001
    
1002
    /*
1003
     *  (non-Javadoc)
1004
     * @see org.gvsig.fmap.driver.IBuffer#cloneBuffer()
1005
     */
1006
    public IBuffer cloneBuffer(){
1007
            boolean malloc = false;
1008
            if(byteBuf != null || shortBuf != null || intBuf != null || floatBuf != null || doubleBuf != null)
1009
                        malloc = true;
1010
            RasterMemoryBuffer rmb = new RasterMemoryBuffer(dataType, width, height, nBands, malloc);
1011
            for(int iBand = 0; iBand < nBands; iBand ++){
1012
                    for(int row = 0; row < height; row ++){
1013
                            for(int col = 0; col < width; col ++){
1014
                                    if(byteBuf != null)
1015
                                            rmb.setElem(row, col, iBand, getElemByte(row, col, iBand));
1016
                                    if(shortBuf != null)
1017
                                            rmb.setElem(row, col, iBand, getElemShort(row, col, iBand));
1018
                                    if(intBuf != null)
1019
                                            rmb.setElem(row, col, iBand, getElemInt(row, col, iBand));
1020
                                    if(floatBuf != null)
1021
                                            rmb.setElem(row, col, iBand, getElemFloat(row, col, iBand));
1022
                                    if(doubleBuf != null)
1023
                                            rmb.setElem(row, col, iBand, getElemDouble(row, col, iBand));
1024
                            }
1025
                    }
1026
            }
1027
                        
1028
            return rmb;
1029
    }
1030
    
1031
    /*
1032
     * (non-Javadoc)
1033
     * @see org.gvsig.fmap.driver.IBuffer#interchangeBands(int, int)
1034
     */
1035
    public void interchangeBands(int band1, int band2){
1036
            switch (getDataType()) {
1037
            case RasterBuffer.TYPE_BYTE:
1038
                    ByteBand auxByte = byteBuf[band1];
1039
                    byteBuf[band1] = byteBuf[band2];
1040
                    byteBuf[band2] = auxByte;
1041
                    break;
1042
            case RasterBuffer.TYPE_DOUBLE:
1043
                    DoubleBand auxDouble = doubleBuf[band1];
1044
                    doubleBuf[band1] = doubleBuf[band2];
1045
                    doubleBuf[band2] = auxDouble;
1046
                    break;                    
1047
            case RasterBuffer.TYPE_FLOAT:
1048
                    FloatBand auxFloat = floatBuf[band1];
1049
                    floatBuf[band1] = floatBuf[band2];
1050
                    floatBuf[band2] = auxFloat;
1051
                    break;                     
1052
            case RasterBuffer.TYPE_INT:
1053
                    IntBand auxInt = intBuf[band1];
1054
                    intBuf[band1] = intBuf[band2];
1055
                    intBuf[band2] = auxInt;
1056
                    break;                     
1057
            case RasterBuffer.TYPE_USHORT:
1058
            case RasterBuffer.TYPE_SHORT:
1059
                    ShortBand auxShort = shortBuf[band1];
1060
                    shortBuf[band1] = shortBuf[band2];
1061
                    shortBuf[band2] = auxShort;
1062
                    break; 
1063
            }
1064
    }
1065
        
1066
    /**
1067
     * Convierte un tipo de dato a cadena
1068
     * @param type Tipo de dato
1069
     * @return cadena  que representa el tipo de dato
1070
     */
1071
    public static String typesToString(int type) {
1072
        switch (type) {
1073
        case RasterBuffer.TYPE_IMAGE:
1074
            return new String("Image");
1075

    
1076
        case RasterBuffer.TYPE_BYTE:
1077
            return new String("Byte");
1078

    
1079
        case RasterBuffer.TYPE_DOUBLE:
1080
            return new String("Double");
1081

    
1082
        case RasterBuffer.TYPE_FLOAT:
1083
            return new String("Float");
1084

    
1085
        case RasterBuffer.TYPE_INT:
1086
                return new String("Integer");
1087
                
1088
        case RasterBuffer.TYPE_USHORT:
1089
        case RasterBuffer.TYPE_SHORT:
1090
            return new String("Short");
1091
        }
1092

    
1093
        return null;
1094
    }
1095
    
1096
        private ByteBand         byteNotValid;
1097
    private ShortBand         shortNotValid;
1098
    private IntBand         intNotValid;
1099
    private FloatBand        floatNotValid;
1100
    private DoubleBand        doubleNotValid;
1101
    
1102
        /*
1103
         *  (non-Javadoc)
1104
         * @see org.gvsig.fmap.driver.IBuffer#assignBandToNotValid(int)
1105
         */
1106
    public void assignBandToNotValid(int iBand) {
1107
            switch(getDataType()){
1108
            case IBuffer.TYPE_BYTE: if(byteNotValid == null) {
1109
                                                                    byteNotValid = new ByteBand(getHeight(), getWidth(), true);
1110
                                                                    for(int i = 0 ; i < getWidth(); i ++)
1111
                                                                            for(int j = 0 ; j < getHeight(); j ++)
1112
                                                                                    byteNotValid.buf[j][i] = (byte)getNotValidValue();
1113
                                                            }
1114
                                                            byteBuf[iBand] = byteNotValid;
1115
                                                            break;
1116
            case IBuffer.TYPE_SHORT: if(shortNotValid == null) {
1117
                                                                    shortNotValid = new ShortBand(getHeight(), getWidth(), true);
1118
                                                                    for(int i = 0 ; i < getWidth(); i ++)
1119
                                                                            for(int j = 0 ; j < getHeight(); j ++)
1120
                                                                                    shortNotValid.buf[j][i] = (short)getNotValidValue();
1121
                                                                    }
1122
                                                              shortBuf[iBand] = shortNotValid;
1123
                                                              break;
1124
            case IBuffer.TYPE_INT:        if(intNotValid == null) {
1125
                                                                        intNotValid = new IntBand(getHeight(), getWidth(), true);
1126
                                                                        for(int i = 0 ; i < getWidth(); i ++)
1127
                                                                                for(int j = 0 ; j < getHeight(); j ++)
1128
                                                                                        intNotValid.buf[j][i] = (int)getNotValidValue();
1129
                                                                }
1130
                                                                intBuf[iBand] = intNotValid;
1131
                                                                break;
1132
            case IBuffer.TYPE_FLOAT:        if(floatNotValid == null) {
1133
                                                                                floatNotValid = new FloatBand(getHeight(), getWidth(), true);
1134
                                                                                for(int i = 0 ; i < getWidth(); i ++)
1135
                                                                                        for(int j = 0 ; j < getHeight(); j ++)
1136
                                                                                                floatNotValid.buf[j][i] = (float)getNotValidValue();
1137
                                                                        }
1138
                                                                        floatBuf[iBand] = floatNotValid;
1139
                                                                        break;
1140
            case IBuffer.TYPE_DOUBLE:        if(doubleNotValid == null) {
1141
                                                                                doubleNotValid = new DoubleBand(getHeight(), getWidth(), true);
1142
                                                                                for(int i = 0 ; i < getWidth(); i ++)
1143
                                                                                        for(int j = 0 ; j < getHeight(); j ++)
1144
                                                                                                doubleNotValid.buf[j][i] = (double)getNotValidValue();
1145
                                                                        }
1146
                                                                        doubleBuf[iBand] = doubleNotValid;
1147
                                                                        break;
1148
            }
1149
    }
1150
}