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 / RasterMemoryBuffer.java @ 2443

History | View | Annotate | Download (37.5 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.datastruct.Band;
26

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

    
54
                public Object getLine(int line) {
55
                        return buf[line];
56
                }
57
                
58
                public void setLine(int line, Object value) {
59
                        buf[line] = (byte[])value;
60
                }
61
                
62
                public Object getBuf() {
63
                        return buf;
64
                }
65
                
66
                public void dispose() {
67
                        buf = null;
68
                }
69
                
70
                protected void finalize() throws Throwable {
71
                        buf = null;
72
                        super.finalize();
73
                }
74
    }
75
    
76
    public class ShortBand extends RasterBand{
77
            public short[][]         buf = null;
78
            
79
            public ShortBand(int height, int width, boolean malloc){
80
                    super(height, width);
81
                    if(malloc)
82
                            buf = new short[height][width];
83
            }
84
            
85
            public Object getLine(int line) {
86
                        return buf[line];
87
                }
88
            
89
                public void setLine(int line, Object value){
90
                        buf[line] = (short[])value;
91
                }
92
                
93
                public Object getBuf(){
94
                        return buf;
95
                }
96
                
97
                public void dispose() {
98
                        buf = null;
99
                }
100
                
101
                /*
102
                 * (non-Javadoc)
103
                 * @see java.lang.Object#finalize()
104
                 */
105
                protected void finalize() throws Throwable {
106
                        buf = null;
107
                        super.finalize();
108
                }
109
    }
110

    
111
        public class IntBand extends RasterBand{
112
                public int[][]         buf = null;
113
                
114
                public IntBand(int height, int width, boolean malloc){
115
                        super(height, width);
116
                        if(malloc)
117
                            buf = new int[height][width];
118
            }
119
                
120
                public Object getLine(int line) {
121
                        return buf[line];
122
                }
123
                
124
                public void setLine(int line, Object value){
125
                        buf[line] = (int[])value;
126
                }
127
                
128
                public Object getBuf(){
129
                        return buf;
130
                }
131
                
132
                public void dispose() {
133
                        buf = null;
134
                }
135
                
136
                protected void finalize() throws Throwable {
137
                        buf = null;
138
                        super.finalize();
139
                }
140
        }
141

    
142
        public class FloatBand extends RasterBand{
143
                public float[][]         buf = null;
144
                
145
                public FloatBand(int height, int width, boolean malloc){
146
                        super(height, width);
147
                        if(malloc)
148
                            buf = new float[height][width];
149
            }
150
                
151
                public Object getLine(int line) {
152
                        return buf[line];
153
                }
154
                
155
                public void setLine(int line, Object value){
156
                        buf[line] = (float[])value;
157
                }
158
                
159
                public Object getBuf(){
160
                        return buf;
161
                }
162
                
163
                public void dispose() {
164
                        buf = null;
165
                }
166
                
167
                protected void finalize() throws Throwable {
168
                        buf = null;
169
                        super.finalize();
170
                }
171
        }
172

    
173
        public class DoubleBand extends RasterBand{
174
                public double[][]         buf = null;
175
                
176
                public DoubleBand(int height, int width, boolean malloc){
177
                        super(height, width);
178
                        if(malloc)
179
                            buf = new double[height][width];
180
            }
181
                
182
                public Object getLine(int line) {
183
                        return buf[line];
184
                }
185
                
186
                public void setLine(int line, Object value){
187
                        buf[line] = (double[])value;
188
                }
189
                
190
                public Object getBuf(){
191
                        return buf;
192
                }
193
                
194
                public void dispose() {
195
                        buf = null;
196
                }
197
                
198
                protected void finalize() throws Throwable {
199
                        buf = null;
200
                        super.finalize();
201
                }
202
        }
203
        
204
    /**
205
     * Constructor
206
     */
207
    public RasterMemoryBuffer() {
208
            
209
    }
210
    
211
    public boolean isBandSwitchable() {
212
            return true;
213
    }
214
    
215
    /**
216
     * Constructor
217
     * @param dataType Tipo de dato
218
     * @param width Ancho
219
     * @param height Alto
220
     * @param bandNr Banda
221
     * @param orig
222
     */
223
    public RasterMemoryBuffer(int dataType, int width, int height, int bandNr, boolean malloc) {
224
            if(malloc)
225
                    malloc(dataType, width, height, bandNr);
226
            else
227
                    loadVariables(dataType, width, height, bandNr);
228
    }
229

    
230
    public void malloc(int dataType, int width, int height, int bandNr) {
231
            this.dataType = dataType;
232
        this.width = width;
233
        this.height = height;
234
        this.nBands = bandNr;
235

    
236
        if (dataType == TYPE_BYTE) {
237
            byteBuf = new ByteBand[bandNr];
238
            for(int i = 0; i < bandNr; i++)
239
                    byteBuf[i] = new ByteBand(height, width, true);
240
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
241
            shortBuf = new ShortBand[bandNr];
242
            for(int i = 0; i < bandNr; i++)
243
                    shortBuf[i] = new ShortBand(height, width, true);
244
        } else if (dataType == TYPE_INT) {
245
            intBuf = new IntBand[bandNr];
246
            for(int i = 0; i < bandNr; i++)
247
                    intBuf[i] = new IntBand(height, width, true);
248
        } else if (dataType == TYPE_FLOAT) {
249
            floatBuf = new FloatBand[bandNr];
250
            for(int i = 0; i < bandNr; i++)
251
                    floatBuf[i] = new FloatBand(height, width, true);
252
        } else if (dataType == TYPE_DOUBLE) {
253
            doubleBuf = new DoubleBand[bandNr];
254
            for(int i = 0; i < bandNr; i++)
255
                    doubleBuf[i] = new DoubleBand(height, width, true);
256
        }
257

    
258
    }
259

    
260
    public void mallocOneBand(int dataType, int width, int height, int band){
261
            this.dataType = dataType;
262
        this.width = width;
263
        this.height = height;
264
        
265
        if (dataType == TYPE_BYTE) {
266
                if(byteBuf == null)
267
                        byteBuf = new ByteBand[nBands];
268
            byteBuf[band] = new ByteBand(height, width, true);
269
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
270
                if(shortBuf == null)
271
                        shortBuf = new ShortBand[nBands];
272
            shortBuf[band] = new ShortBand(height, width, true);
273
        } else if (dataType == TYPE_INT) {
274
                if(intBuf == null)
275
                        intBuf = new IntBand[nBands];
276
            intBuf[band] = new IntBand(height, width, true);
277
        } else if (dataType == TYPE_FLOAT) {
278
                if(floatBuf == null)
279
                        floatBuf = new FloatBand[nBands];
280
            floatBuf[band] = new FloatBand(height, width, true);
281
        } else if (dataType == TYPE_DOUBLE) {
282
                if(doubleBuf == null)
283
                        doubleBuf = new DoubleBand[nBands];
284
            doubleBuf[band] = new DoubleBand(height, width, true);
285
        }
286
    }
287
    
288
    /**
289
     * Carga las variables del rasterBuf
290
     * @param dataType Tipo de dato
291
     * @param width Ancho
292
     * @param height Alto
293
     * @param bandNr Banda
294
     * @param orig
295
     */
296
    private void loadVariables(int dataType, int width, int height, int bandNr) {
297
            this.dataType = dataType;
298
        this.width = width;
299
        this.height = height;
300
        this.nBands = bandNr;
301

    
302
        if (dataType == TYPE_BYTE) {
303
                byteBuf = new ByteBand[bandNr];
304
                for(int i = 0; i < bandNr; i ++)
305
                        byteBuf[i] = new ByteBand(height, 0, true);
306
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
307
                shortBuf = new ShortBand[bandNr];
308
                for(int i = 0; i < bandNr; i ++)
309
                        shortBuf[i] = new ShortBand(height, 0, true);
310
        } else if (dataType == TYPE_INT) {
311
                intBuf = new IntBand[bandNr];
312
                for(int i = 0; i < bandNr; i ++)
313
                        intBuf[i] = new IntBand(height, 0, true);
314
        } else if (dataType == TYPE_FLOAT) {
315
                floatBuf = new FloatBand[bandNr];
316
                for(int i = 0; i < bandNr; i ++)
317
                        floatBuf[i] = new FloatBand(height, 0, true);
318
        } else if (dataType == TYPE_DOUBLE) {
319
                doubleBuf = new DoubleBand[bandNr];
320
                for(int i = 0; i < bandNr; i ++)
321
                        doubleBuf[i] = new DoubleBand(height, 0, true);
322
        }
323

    
324
    }
325

    
326
    public int getWidth() {
327
        return width;
328
    }
329

    
330
    public int getHeight() {
331
        return height;
332
    }
333

    
334
    public int getBandCount() {
335
        return nBands;
336
    }
337

    
338
    /**
339
     * Obtiene el tipo de dato. Los tipos de dato posibles est?n definidos en IRaster.
340
     * @return tipo de datos
341
     */
342
        public int getDataType() {
343
                return dataType;
344
        }
345
        
346
        /**
347
         * Asigna el tipo de dato. Los tipos de dato posibles est?n definidos en IRaster.
348
         * @param dataType Tipo de dato del buffer
349
         */
350
        public void setDataType(int dataType) {
351
                this.dataType = dataType;
352
        }
353

    
354
    /**
355
     * Obtiene el tama?o del tipo de dato en bytes
356
     * @return Tipo de dato
357
     */
358
    public int getDataSize() {
359
        if (dataType == TYPE_BYTE) {
360
            return 1;
361
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
362
            return 2;
363
        } else if (dataType == TYPE_INT) {
364
            return 4;
365
        }else if (dataType == TYPE_FLOAT) {
366
            return 8;
367
        }else if (dataType == TYPE_DOUBLE) {
368
            return 16;
369
        }
370

    
371
        return 0;
372
    }
373

    
374
    /**
375
     * Obtiene el tama?o del buffer
376
     * @return tama?o del buffer
377
     */
378
    public long sizeof() {
379
        return getDataSize() * width * height * nBands;
380
    }
381

    
382
    //***********************************************
383
    //Obtiene una linea de datos con todas las bandas
384
    
385
    public byte[][] getLineByte(int line) {
386
            byte[][] r = new byte[nBands][];
387
            for(int Band = 0; Band < nBands; Band ++)
388
                    r[Band] = (byte[])byteBuf[Band].getLine(line);
389
            return r;
390
    }
391

    
392
    public short[][] getLineShort(int line) {
393
            short[][] r = new short[nBands][];
394
            for(int Band = 0; Band < nBands; Band ++)
395
                    r[Band] = (short[])shortBuf[Band].getLine(line);
396
            return r;
397
    }
398

    
399
    public int[][] getLineInt(int line) {
400
            int[][] r = new int[nBands][];
401
            for(int Band = 0; Band < nBands; Band ++)
402
                    r[Band] = (int[])intBuf[Band].getLine(line);
403
            return r;
404
    }
405
        
406
    public float[][] getLineFloat(int line) {
407
            float[][] r = new float[nBands][];
408
            for(int Band = 0; Band < nBands; Band ++)
409
                    r[Band] = (float[])floatBuf[Band].getLine(line);
410
            return r;
411
    }
412
    
413
    public double[][] getLineDouble(int line) {
414
            double[][] r = new double[nBands][];
415
            for(int Band = 0; Band < nBands; Band ++)
416
                    r[Band] = (double[])doubleBuf[Band].getLine(line);
417
            return r;
418
    }
419

    
420
    //***********************************************
421
    //Asigna una linea de datos a todas las bandas
422
    
423
    public void setLineByte(byte[][] data, int line) {
424
            for(int Band = 0; Band < nBands; Band ++)
425
                    byteBuf[Band].setLine(line, data[Band]);
426
    }
427

    
428
    public void setLineShort(short[][] data, int line) {
429
            for(int Band = 0; Band < nBands; Band ++)
430
                    shortBuf[Band].setLine(line, data[Band]);
431
    }
432

    
433
    public void setLineInt(int[][] data, int line) {
434
            for(int Band = 0; Band < nBands; Band ++)
435
                    intBuf[Band].setLine(line, data[Band]);
436
    }
437
        
438
    public void setLineFloat(float[][] data, int line) {
439
            for(int Band = 0; Band < nBands; Band ++)
440
                    floatBuf[Band].setLine(line, data[Band]);
441
    }
442
    
443
    public void setLineDouble(double[][] data, int line) {
444
            for(int Band = 0; Band < nBands; Band ++)
445
                    doubleBuf[Band].setLine(line, data[Band]);
446
    }
447
    
448
    //***********************************************
449
    //Obtiene una linea de datos de una banda
450
    
451
    public byte[] getLineFromBandByte(int line, int band) {
452
        return (byte[])byteBuf[band].getLine(line);
453
    }
454

    
455
    public short[] getLineFromBandShort(int line, int band) {
456
        return (short[])shortBuf[band].getLine(line);
457
    }
458

    
459
    public int[] getLineFromBandInt(int line, int band) {
460
        return (int[])intBuf[band].getLine(line);
461
    }
462
        
463
    public float[] getLineFromBandFloat(int line, int band) {
464
        return (float[])floatBuf[band].getLine(line);
465
    }
466
    
467
    public double[] getLineFromBandDouble(int line, int band) {
468
        return (double[])doubleBuf[band].getLine(line);
469
    }
470
        
471
    //***********************************************
472
    //Asigna una linea de datos a una banda
473
    
474
    public void setLineInBandByte(byte[] data, int line, int band) {
475
        byteBuf[band].setLine(line, data);
476
    }
477

    
478
    public void setLineInBandShort(short[] data, int line, int band) {
479
        shortBuf[band].setLine(line, data);
480
    }
481

    
482
    public void setLineInBandInt(int[] data, int line, int band) {
483
        intBuf[band].setLine(line, data);
484
    }
485
        
486
    public void setLineInBandFloat(float[] data, int line, int band) {
487
        floatBuf[band].setLine(line, data);
488
    }
489
    
490
    public void setLineInBandDouble(double[] data, int line, int band) {
491
        doubleBuf[band].setLine(line, data);
492
    }
493
    
494
    //***********************************************    
495
    //Obtiene un elemento de la matriz
496
    
497
    public byte getElemByte(int line, int col, int band) {
498
        return byteBuf[band].buf[line][col];
499
    }
500
    
501
    public short getElemShort(int line, int col, int band) {
502
        return shortBuf[band].buf[line][col];
503
    }
504
    
505
    public int getElemInt(int line, int col, int band) {
506
        return intBuf[band].buf[line][col];
507
    }
508
    
509
    public float getElemFloat(int line, int col, int band) {
510
        return floatBuf[band].buf[line][col];
511
    }
512
    
513
    public double getElemDouble(int line, int col, int band) {
514
        return doubleBuf[band].buf[line][col];
515
    }
516
    
517
    //**********************************************    
518
    //Asigna un elemento de la matriz
519
    
520
    public void setElem(int line, int col, int band, byte data) {
521
            byteBuf[band].buf[line][col] = data;
522
    }
523
    
524
    public void setElem(int line, int col, int band, short data) {
525
            shortBuf[band].buf[line][col] = data;
526
    }
527
    
528
    public void setElem(int line, int col, int band, int data) {
529
            intBuf[band].buf[line][col] = data;
530
    }
531
    
532
    public void setElem(int line, int col, int band, float data) {
533
            floatBuf[band].buf[line][col] = data;
534
    }
535
    
536
    public void setElem(int line, int col, int band, double data) {
537
            doubleBuf[band].buf[line][col] = data;
538
    }
539
    
540
    //***********************************************
541
    //Copia un elemento de todas la bandas en el buffer pasado por par?metro
542
    
543
    public void getElemByte(int line, int col, byte[] data) {
544
            for (int Band = 0; (Band < nBands && Band < data.length); Band++)
545
            data[Band] = byteBuf[Band].buf[line][col];
546
    }
547
    
548
    public void getElemShort(int line, int col, short[] data) {
549
            for (int Band = 0; (Band < nBands && Band < data.length); Band++)
550
            data[Band] = shortBuf[Band].buf[line][col];
551
    }
552
    
553
    public void getElemInt(int line, int col, int[] data) {
554
            for (int Band = 0; (Band < nBands && Band < data.length); Band++)
555
            data[Band] = intBuf[Band].buf[line][col];
556
    }  
557
    
558
    public void getElemFloat(int line, int col, float[] data) {
559
            for (int Band = 0; (Band < nBands && Band < data.length); Band++)
560
            data[Band] = floatBuf[Band].buf[line][col];
561
    }
562
    
563
    public void getElemDouble(int line, int col, double[] data) {
564
            for (int Band = 0; (Band < nBands && Band < data.length); Band++)
565
            data[Band] = doubleBuf[Band].buf[line][col];
566
    }
567

    
568
    //***********************************************
569
    //Asigna un elemento a todas la bandas en el buffer pasado por par?metro
570
    
571
    public void setElemByte(int line, int col, byte[] data) {
572
            for (int Band = 0; (Band < nBands && Band < data.length); Band++)
573
            byteBuf[Band].buf[line][col] = data[Band];
574
    }
575
    
576
    public void setElemShort(int line, int col, short[] data) {
577
            for (int Band = 0; (Band < nBands && Band < data.length); Band++)
578
            shortBuf[Band].buf[line][col] = data[Band];
579
    }
580
    
581
    public void setElemInt(int line, int col, int[] data) {
582
            for (int Band = 0; (Band < nBands && Band < data.length); Band++)
583
            intBuf[Band].buf[line][col] = data[Band];
584
    }
585

    
586
    public void setElemFloat(int line, int col, float[] data) {
587
            for (int Band = 0; (Band < nBands && Band < data.length); Band++)
588
            floatBuf[Band].buf[line][col] = data[Band];
589
    }
590
    
591
    public void setElemDouble(int line, int col, double[] data) {
592
            for (int Band = 0; (Band < nBands && Band < data.length); Band++)
593
            doubleBuf[Band].buf[line][col] = data[Band];
594
    }
595
    
596
    //***********************************************
597
    //Obtiene una banda entera
598
    
599
    public Band getBand(int band){
600
            if (dataType == TYPE_BYTE) {
601
                    return byteBuf[band];
602
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
603
                return shortBuf[band];
604
        } else if (dataType == TYPE_INT) {
605
                return intBuf[band];
606
        }else if (dataType == TYPE_FLOAT) {
607
                return floatBuf[band];
608
        }else if (dataType == TYPE_DOUBLE) {
609
                return doubleBuf[band];
610
        }
611
            return null;
612
    }
613
    
614
    /*
615
     *  (non-Javadoc)
616
     * @see org.gvsig.fmap.driver.Buffer#getBandBuffer(int)
617
     */
618
    public Buffer getBandBuffer(int Band){
619
            RasterMemoryBuffer rmb = new RasterMemoryBuffer(dataType, width, height, 1, false);
620
            if (dataType == TYPE_BYTE) 
621
                    rmb.byteBuf[0].buf = byteBuf[Band].buf;
622
        else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT))
623
                rmb.shortBuf[0].buf = shortBuf[Band].buf;
624
        else if (dataType == TYPE_INT)
625
                rmb.intBuf[0].buf = intBuf[Band].buf;
626
        else if (dataType == TYPE_FLOAT)
627
                rmb.floatBuf[0].buf = floatBuf[Band].buf;
628
        else if (dataType == TYPE_DOUBLE)
629
                rmb.doubleBuf[0].buf = doubleBuf[Band].buf;
630
            return rmb;
631
    }
632
    //***********************************************
633
    //Inicializa una banda a un valor pasado por par?metro
634
    
635
    public void assign(int band, byte value) {
636
            for(int line = 0; line < height; line ++)
637
                    for(int col = 0; col < width; col ++)
638
                            byteBuf[band].buf[line][col] = value;
639
    }
640
    
641
    public void assign(int band, short value) {
642
            for(int line = 0; line < height; line ++)
643
                    for(int col = 0; col < width; col ++)
644
                            shortBuf[band].buf[line][col] = value;
645
    }
646
    
647
    public void assign(int band, int value) {
648
            for(int line = 0; line < height; line ++)
649
                    for(int col = 0; col < width; col ++)
650
                            intBuf[band].buf[line][col] = value;
651
    }
652

    
653
    public void assign(int band, float value) {
654
            for(int line = 0; line < height; line ++)
655
                    for(int col = 0; col < width; col ++)
656
                            floatBuf[band].buf[line][col] = value;
657
    }
658
    
659
    public void assign(int band, double value) {
660
            for(int line = 0; line < height; line ++)
661
                    for(int col = 0; col < width; col ++)
662
                            doubleBuf[band].buf[line][col] = value;
663
    }
664
    
665
    //***********************************************
666
    //Crea un buffer banda inicializado con el valor pasado por par?metro
667
    
668

    
669
    public Band createBand(byte defaultValue){
670
            switch(getDataType()){
671
            case RasterBuffer.TYPE_BYTE:ByteBand bb = new ByteBand(width, height, false);
672
                                                                    bb.buf = createByteBand(width, height, defaultValue);
673
                                                                    return bb;
674
            case RasterBuffer.TYPE_SHORT:        ShortBand sb = new ShortBand(width, height, false);
675
                                                                                sb.buf = createShortBand(width, height, defaultValue);
676
                                                                                return sb;
677
            case RasterBuffer.TYPE_INT:        IntBand ib = new IntBand(width, height, false);
678
                                                                        ib.buf = createIntBand(width, height, defaultValue);
679
                                                                        return ib;
680
            case RasterBuffer.TYPE_FLOAT:        FloatBand fb = new FloatBand(width, height, false);
681
                                                                                fb.buf = createFloatBand(width, height, defaultValue);
682
                                                                                return fb;
683
            case RasterBuffer.TYPE_DOUBLE:        DoubleBand db = new DoubleBand(width, height, false);
684
                                                                                db.buf = createDoubleBand(width, height, defaultValue);
685
                                                                                return db;
686
            }
687
            return null;
688
    }
689

    
690
    public byte[][] createByteBand(int width, int height, byte defaultValue){
691
            byte[][] band = new byte[height][width];
692
            if(defaultValue != 0){
693
                    for(int line = 0; line < height; line ++)
694
                            for(int col = 0; col < width; col ++)
695
                                    band[line][col] = defaultValue;
696
            }
697
            return band;
698
    }
699
    
700
    public short[][] createShortBand(int width, int height, short defaultValue){
701
            short[][] band = new short[height][width];
702
            if(defaultValue != 0){
703
                    for(int line = 0; line < height; line ++)
704
                            for(int col = 0; col < width; col ++)
705
                                    band[line][col] = defaultValue;
706
            }
707
            return band;
708
    }
709
    
710
    public int[][] createIntBand(int width, int height, int defaultValue){
711
            int[][] band = new int[height][width];
712
            if(defaultValue != 0){
713
                    for(int line = 0; line < height; line ++)
714
                            for(int col = 0; col < width; col ++)
715
                                    band[line][col] = defaultValue;
716
            }
717
            return band;
718
    }
719
    
720
    public float[][] createFloatBand(int width, int height, float defaultValue){
721
            float[][] band = new float[height][width];
722
            if(defaultValue != 0){
723
                    for(int line = 0; line < height; line ++)
724
                            for(int col = 0; col < width; col ++)
725
                                    band[line][col] = defaultValue;
726
            }
727
            return band;
728
    }
729
    
730
    public double[][] createDoubleBand(int width, int height, double defaultValue){
731
            double[][] band = new double[height][width];
732
            if(defaultValue != 0){
733
                    for(int line = 0; line < height; line ++)
734
                            for(int col = 0; col < width; col ++)
735
                                    band[line][col] = defaultValue;
736
            }
737
            return band;
738
    }
739
    
740
    //***********************************************
741
    //A?ade una banda entera. Los datos son asignados por referencia
742

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

    
998
        public void assignBand(int nBand, Band band) {
999
                switch (getDataType()) {
1000
            case RasterBuffer.TYPE_BYTE:
1001
                    byteBuf[nBand] = ((ByteBand)band);
1002
                    break;
1003
            case RasterBuffer.TYPE_DOUBLE:
1004
                    doubleBuf[nBand] = ((DoubleBand)band);                  
1005
                    break;                    
1006
            case RasterBuffer.TYPE_FLOAT:
1007
                    floatBuf[nBand] = ((FloatBand)band);
1008
                    break;                     
1009
            case RasterBuffer.TYPE_INT:
1010
                    intBuf[nBand] = ((IntBand)band);
1011
                    break;                     
1012
            case RasterBuffer.TYPE_USHORT:
1013
            case RasterBuffer.TYPE_SHORT:
1014
                    shortBuf[nBand] = ((ShortBand)band);
1015
                    break; 
1016
            }
1017
        }
1018
    
1019
    public Buffer cloneBuffer(){
1020
            boolean malloc = false;
1021
            if(byteBuf != null || shortBuf != null || intBuf != null || floatBuf != null || doubleBuf != null)
1022
                        malloc = true;
1023
            RasterMemoryBuffer rmb = new RasterMemoryBuffer(dataType, width, height, nBands, malloc);
1024
            for(int Band = 0; Band < nBands; Band ++){
1025
                    for(int row = 0; row < height; row ++){
1026
                            for(int col = 0; col < width; col ++){
1027
                                    if(byteBuf != null)
1028
                                            rmb.setElem(row, col, Band, getElemByte(row, col, Band));
1029
                                    if(shortBuf != null)
1030
                                            rmb.setElem(row, col, Band, getElemShort(row, col, Band));
1031
                                    if(intBuf != null)
1032
                                            rmb.setElem(row, col, Band, getElemInt(row, col, Band));
1033
                                    if(floatBuf != null)
1034
                                            rmb.setElem(row, col, Band, getElemFloat(row, col, Band));
1035
                                    if(doubleBuf != null)
1036
                                            rmb.setElem(row, col, Band, getElemDouble(row, col, Band));
1037
                            }
1038
                    }
1039
            }
1040
                        
1041
            return rmb;
1042
    }
1043
    
1044
    public void interchangeBands(int band1, int band2){
1045
            switch (getDataType()) {
1046
            case RasterBuffer.TYPE_BYTE:
1047
                    ByteBand auxByte = byteBuf[band1];
1048
                    byteBuf[band1] = byteBuf[band2];
1049
                    byteBuf[band2] = auxByte;
1050
                    break;
1051
            case RasterBuffer.TYPE_DOUBLE:
1052
                    DoubleBand auxDouble = doubleBuf[band1];
1053
                    doubleBuf[band1] = doubleBuf[band2];
1054
                    doubleBuf[band2] = auxDouble;
1055
                    break;                    
1056
            case RasterBuffer.TYPE_FLOAT:
1057
                    FloatBand auxFloat = floatBuf[band1];
1058
                    floatBuf[band1] = floatBuf[band2];
1059
                    floatBuf[band2] = auxFloat;
1060
                    break;                     
1061
            case RasterBuffer.TYPE_INT:
1062
                    IntBand auxInt = intBuf[band1];
1063
                    intBuf[band1] = intBuf[band2];
1064
                    intBuf[band2] = auxInt;
1065
                    break;                     
1066
            case RasterBuffer.TYPE_USHORT:
1067
            case RasterBuffer.TYPE_SHORT:
1068
                    ShortBand auxShort = shortBuf[band1];
1069
                    shortBuf[band1] = shortBuf[band2];
1070
                    shortBuf[band2] = auxShort;
1071
                    break; 
1072
            }
1073
    }
1074
        
1075
    /**
1076
     * Convierte un tipo de dato a cadena
1077
     * @param type Tipo de dato
1078
     * @return cadena  que representa el tipo de dato
1079
     */
1080
    public static String typesToString(int type) {
1081
        switch (type) {
1082
        case RasterBuffer.TYPE_IMAGE:
1083
            return new String("Image");
1084

    
1085
        case RasterBuffer.TYPE_BYTE:
1086
            return new String("Byte");
1087

    
1088
        case RasterBuffer.TYPE_DOUBLE:
1089
            return new String("Double");
1090

    
1091
        case RasterBuffer.TYPE_FLOAT:
1092
            return new String("Float");
1093

    
1094
        case RasterBuffer.TYPE_INT:
1095
                return new String("Integer");
1096
                
1097
        case RasterBuffer.TYPE_USHORT:
1098
        case RasterBuffer.TYPE_SHORT:
1099
            return new String("Short");
1100
        }
1101

    
1102
        return null;
1103
    }
1104
    
1105
        private ByteBand         byteNotValid;
1106
    private ShortBand         shortNotValid;
1107
    private IntBand         intNotValid;
1108
    private FloatBand        floatNotValid;
1109
    private DoubleBand        doubleNotValid;
1110
    
1111
    public void assignBandToNotValid(int Band) {
1112
            switch(getDataType()){
1113
            case Buffer.TYPE_BYTE: if(byteNotValid == null) {
1114
                                                                    byteNotValid = new ByteBand(getHeight(), getWidth(), true);
1115
                                                                    for(int i = 0 ; i < getWidth(); i ++)
1116
                                                                            for(int j = 0 ; j < getHeight(); j ++)
1117
                                                                                    byteNotValid.buf[j][i] = (byte)getNotValidValue();
1118
                                                            }
1119
                                                            byteBuf[Band] = byteNotValid;
1120
                                                            break;
1121
            case Buffer.TYPE_SHORT: if(shortNotValid == null) {
1122
                                                                    shortNotValid = new ShortBand(getHeight(), getWidth(), true);
1123
                                                                    for(int i = 0 ; i < getWidth(); i ++)
1124
                                                                            for(int j = 0 ; j < getHeight(); j ++)
1125
                                                                                    shortNotValid.buf[j][i] = (short)getNotValidValue();
1126
                                                                    }
1127
                                                              shortBuf[Band] = shortNotValid;
1128
                                                              break;
1129
            case Buffer.TYPE_INT:        if(intNotValid == null) {
1130
                                                                        intNotValid = new IntBand(getHeight(), getWidth(), true);
1131
                                                                        for(int i = 0 ; i < getWidth(); i ++)
1132
                                                                                for(int j = 0 ; j < getHeight(); j ++)
1133
                                                                                        intNotValid.buf[j][i] = (int)getNotValidValue();
1134
                                                                }
1135
                                                                intBuf[Band] = intNotValid;
1136
                                                                break;
1137
            case Buffer.TYPE_FLOAT:        if(floatNotValid == null) {
1138
                                                                                floatNotValid = new FloatBand(getHeight(), getWidth(), true);
1139
                                                                                for(int i = 0 ; i < getWidth(); i ++)
1140
                                                                                        for(int j = 0 ; j < getHeight(); j ++)
1141
                                                                                                floatNotValid.buf[j][i] = (float)getNotValidValue();
1142
                                                                        }
1143
                                                                        floatBuf[Band] = floatNotValid;
1144
                                                                        break;
1145
            case Buffer.TYPE_DOUBLE:        if(doubleNotValid == null) {
1146
                                                                                doubleNotValid = new DoubleBand(getHeight(), getWidth(), true);
1147
                                                                                for(int i = 0 ; i < getWidth(); i ++)
1148
                                                                                        for(int j = 0 ; j < getHeight(); j ++)
1149
                                                                                                doubleNotValid.buf[j][i] = (double)getNotValidValue();
1150
                                                                        }
1151
                                                                        doubleBuf[Band] = doubleNotValid;
1152
                                                                        break;
1153
            }
1154
    }
1155
    
1156
    public boolean isReadOnlyBuffer() {
1157
            return false;
1158
    }
1159
    
1160
        /**
1161
         * Libera el buffer de memoria
1162
         */
1163
        public void dispose() {
1164
                if(byteBuf != null) {
1165
                        for (int i = 0; i < byteBuf.length; i++) {
1166
                                byteBuf[i].dispose();
1167
                                byteBuf[i] = null;
1168
                        }
1169
                        byteBuf = null;
1170
                }
1171
                if(shortBuf != null) {
1172
                        for (int i = 0; i < shortBuf.length; i++) {
1173
                                shortBuf[i].dispose();
1174
                                shortBuf[i] = null;
1175
                        }
1176
                        shortBuf = null;
1177
                }
1178
                if(intBuf != null) {
1179
                        for (int i = 0; i < intBuf.length; i++) {
1180
                                intBuf[i].dispose();
1181
                                intBuf[i] = null;
1182
                        }
1183
                        intBuf = null;
1184
                }
1185
                if(floatBuf != null) {
1186
                        for (int i = 0; i < floatBuf.length; i++) {
1187
                                floatBuf[i].dispose();        
1188
                                floatBuf[i] = null;
1189
                        }
1190
                        floatBuf = null;
1191
                }
1192
                if(doubleBuf != null) {
1193
                        for (int i = 0; i < doubleBuf.length; i++) {
1194
                                doubleBuf[i].dispose();
1195
                                doubleBuf[i] = null;
1196
                        }
1197
                        doubleBuf = null;
1198
                }
1199
        }
1200
        
1201
        protected void finalize() throws Throwable {
1202
                if(byteBuf != null) {
1203
                        for (int i = 0; i < byteBuf.length; i++) {
1204
                                byteBuf[i] = null;
1205
                        }
1206
                        byteBuf = null;
1207
                }
1208
                if(shortBuf != null) {
1209
                        for (int i = 0; i < shortBuf.length; i++) {
1210
                                shortBuf[i] = null;
1211
                        }
1212
                        shortBuf = null;
1213
                }
1214
                if(intBuf != null) {
1215
                        for (int i = 0; i < intBuf.length; i++) {
1216
                                intBuf[i] = null;
1217
                        }
1218
                        intBuf = null;
1219
                }
1220
                if(floatBuf != null) {
1221
                        for (int i = 0; i < floatBuf.length; i++) {
1222
                                floatBuf[i] = null;
1223
                        }
1224
                        floatBuf = null;
1225
                }
1226
                if(doubleBuf != null) {
1227
                        for (int i = 0; i < doubleBuf.length; i++) {
1228
                                doubleBuf[i] = null;
1229
                        }
1230
                        doubleBuf = null;
1231
                }
1232
                super.finalize();
1233
        }
1234

    
1235
}