Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1010 / libraries / libCq_CMS_praster / src / org / cresques / io / data / RasterBuf.java @ 12804

History | View | Annotate | Download (30 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.cresques.io.data;
20

    
21
import java.io.IOException;
22

    
23
import org.cresques.filter.IRaster;
24
import org.cresques.io.GeoRasterWriter;
25
import org.cresques.io.IDataWriter;
26
import org.cresques.px.Extent;
27

    
28

    
29
/**
30
 * Rectangulo de pixeles. Para cada tipo de datos java hay un buffer distinto donde cada elemento es
31
 * accedido de la siguiente forma: [banda][fila][columna]
32
 * m[1][2][0] = cte;-> Sustituye el elemento de la fila 2 de la banda 1 columna 0
33
 * m[1][0] = array; -> Sustituye la fila 0 de la banda 1 
34
 * m[0] = matriz cuadrada; -> Sustituye la banda entera.
35
 * 
36
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
37
 */
38
public class RasterBuf implements IRaster {
39
    private byte[][][]                 byteBuf;
40
    private short[][][]         shortBuf;
41
    private int[][][]                 intBuf;
42
    private float[][][]                floatBuf;
43
    private double[][][]        doubleBuf;
44
    
45
    private int                         width;
46
    private int                         height;
47
    private int                         nBands;
48
    private int                         dataType;
49
        public double                         noDataValue = -99999;
50

    
51
    /**
52
     * Constructor
53
     */
54
    public RasterBuf() {
55
            
56
    }
57
    
58
    /**
59
     * Constructor
60
     * @param dataType Tipo de dato
61
     * @param width Ancho
62
     * @param height Alto
63
     * @param bandNr Banda
64
     * @param orig
65
     */
66
    public RasterBuf(int dataType, int width, int height, int bandNr, boolean malloc) {
67
            if(malloc)
68
                    malloc(dataType, width, height, bandNr);
69
            else
70
                    loadVariables(dataType, width, height, bandNr);
71
    }
72

    
73
    /**
74
     * Reserva de memoria para el rasterbuf
75
     * @param dataType Tipo de dato
76
     * @param width Ancho
77
     * @param height Alto
78
     * @param bandNr Banda
79
     * @param orig
80
     */
81
    public void malloc(int dataType, int width, int height, int bandNr) {
82
            this.dataType = dataType;
83
        this.width = width;
84
        this.height = height;
85
        this.nBands = bandNr;
86

    
87
        if (dataType == TYPE_BYTE) {
88
            byteBuf = new byte[bandNr][height][width];
89
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
90
            shortBuf = new short[bandNr][height][width];
91
        } else if (dataType == TYPE_INT) {
92
            intBuf = new int[bandNr][height][width];
93
        } else if (dataType == TYPE_FLOAT) {
94
            floatBuf = new float[bandNr][height][width];
95
        } else if (dataType == TYPE_DOUBLE) {
96
            doubleBuf = new double[bandNr][height][width];
97
        }
98

    
99
    }
100

    
101
    /**
102
     * Carga las variables del rasterBuf
103
     * @param dataType Tipo de dato
104
     * @param width Ancho
105
     * @param height Alto
106
     * @param bandNr Banda
107
     * @param orig
108
     */
109
    private void loadVariables(int dataType, int width, int height, int bandNr) {
110
            this.dataType = dataType;
111
        this.width = width;
112
        this.height = height;
113
        this.nBands = bandNr;
114

    
115
        if (dataType == TYPE_BYTE) {
116
            byteBuf = new byte[bandNr][height][];
117
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
118
            shortBuf = new short[bandNr][height][];
119
        } else if (dataType == TYPE_INT) {
120
            intBuf = new int[bandNr][height][];
121
        } else if (dataType == TYPE_FLOAT) {
122
            floatBuf = new float[bandNr][height][];
123
        } else if (dataType == TYPE_DOUBLE) {
124
            doubleBuf = new double[bandNr][height][];
125
        }
126

    
127
    }
128

    
129
    
130
    /* (non-Javadoc)
131
     * @see org.cresques.io.raster.IRaster#getWidth()
132
     */
133
    public int getWidth() {
134
        return width;
135
    }
136

    
137
    /* (non-Javadoc)
138
     * @see org.cresques.io.raster.IRaster#getHeight()
139
     */
140
    public int getHeight() {
141
        return height;
142
    }
143

    
144
    /* (non-Javadoc)
145
     * @see org.cresques.io.raster.IRaster#getBandNr()
146
     */
147
    public int getBandCount() {
148
        return nBands;
149
    }
150

    
151
    /* (non-Javadoc)
152
     * @see org.cresques.io.raster.IRaster#getDataType()
153
     */
154
    public int getDataType() {
155
        return dataType;
156
    }
157

    
158
    /**
159
     * Obtiene el tama?o del tipo de dato en bytes
160
     * @return Tipo de dato
161
     */
162
    public int getDataSize() {
163
        if (dataType == TYPE_BYTE) {
164
            return 1;
165
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
166
            return 2;
167
        } else if (dataType == TYPE_INT) {
168
            return 4;
169
        }else if (dataType == TYPE_FLOAT) {
170
            return 8;
171
        }else if (dataType == TYPE_DOUBLE) {
172
            return 16;
173
        }
174

    
175
        return 0;
176
    }
177

    
178
    /**
179
     * Obtiene el tama?o del buffer
180
     * @return tama?o del buffer
181
     */
182
    public int sizeof() {
183
        return getDataSize() * width * height * nBands;
184
    }
185

    
186
    //***********************************************
187
    //Obtiene una linea de datos con todas las bandas
188
    
189
    public byte[][] getLineByte(int line) {
190
            byte[][] r = new byte[nBands][];
191
            for(int iBand = 0; iBand < nBands; iBand ++)
192
                    r[iBand] = byteBuf[iBand][line];
193
            return r;
194
    }
195

    
196
    public short[][] getLineShort(int line) {
197
            short[][] r = new short[nBands][];
198
            for(int iBand = 0; iBand < nBands; iBand ++)
199
                    r[iBand] = shortBuf[iBand][line];
200
            return r;
201
    }
202

    
203
    public int[][] getLineInt(int line) {
204
            int[][] r = new int[nBands][];
205
            for(int iBand = 0; iBand < nBands; iBand ++)
206
                    r[iBand] = intBuf[iBand][line];
207
            return r;
208
    }
209
        
210
    public float[][] getLineFloat(int line) {
211
            float[][] r = new float[nBands][];
212
            for(int iBand = 0; iBand < nBands; iBand ++)
213
                    r[iBand] = floatBuf[iBand][line];
214
            return r;
215
    }
216
    
217
    public double[][] getLineDouble(int line) {
218
            double[][] r = new double[nBands][];
219
            for(int iBand = 0; iBand < nBands; iBand ++)
220
                    r[iBand] = doubleBuf[iBand][line];
221
            return r;
222
    }
223

    
224
    //***********************************************
225
    //Asigna una linea de datos a todas las bandas
226
    
227
    public void setLineByte(byte[][] data, int line) {
228
            for(int iBand = 0; iBand < nBands; iBand ++)
229
                    byteBuf[iBand][line] = data[iBand];
230
    }
231

    
232
    public void setLineShort(short[][] data, int line) {
233
            for(int iBand = 0; iBand < nBands; iBand ++)
234
                    shortBuf[iBand][line] = data[iBand];
235
    }
236

    
237
    public void setLineInt(int[][] data, int line) {
238
            for(int iBand = 0; iBand < nBands; iBand ++)
239
                    intBuf[iBand][line] = data[iBand];
240
    }
241
        
242
    public void setLineFloat(float[][] data, int line) {
243
            for(int iBand = 0; iBand < nBands; iBand ++)
244
                    floatBuf[iBand][line] = data[iBand];
245
    }
246
    
247
    public void setLineDouble(double[][] data, int line) {
248
            for(int iBand = 0; iBand < nBands; iBand ++)
249
                    doubleBuf[iBand][line] = data[iBand];
250
    }
251
    
252
    //***********************************************
253
    //Obtiene una linea de datos de una banda
254
    
255
    public byte[] getLineFromBandByte(int line, int band) {
256
        return byteBuf[band][line];
257
    }
258

    
259
    public short[] getLineFromBandShort(int line, int band) {
260
        return shortBuf[band][line];
261
    }
262

    
263
    public int[] getLineFromBandInt(int line, int band) {
264
        return intBuf[band][line];
265
    }
266
        
267
    public float[] getLineFromBandFloat(int line, int band) {
268
        return floatBuf[band][line];
269
    }
270
    
271
    public double[] getLineFromBandDouble(int line, int band) {
272
        return doubleBuf[band][line];
273
    }
274
        
275
    //***********************************************
276
    //Asigna una linea de datos a una banda
277
    
278
    public void setLineInBandByte(byte[] data, int line, int band) {
279
        byteBuf[band][line] = data;
280
    }
281

    
282
    public void setLineInBandShort(short[] data, int line, int band) {
283
        shortBuf[band][line] = data;
284
    }
285

    
286
    public void setLineInBandInt(int[] data, int line, int band) {
287
        intBuf[band][line] = data;
288
    }
289
        
290
    public void setLineInBandFloat(float[] data, int line, int band) {
291
        floatBuf[band][line] = data;
292
    }
293
    
294
    public void setLineInBandDouble(double[] data, int line, int band) {
295
        doubleBuf[band][line] = data;
296
    }
297
    
298
    //***********************************************    
299
    //Obtiene un elemento de la matriz
300
    
301
    public byte getElemByte(int line, int col, int band) {
302
        return byteBuf[band][line][col];
303
    }
304
    
305
    public short getElemShort(int line, int col, int band) {
306
        return shortBuf[band][line][col];
307
    }
308
    
309
    public int getElemInt(int line, int col, int band) {
310
        return intBuf[band][line][col];
311
    }
312
    
313
    public float getElemFloat(int line, int col, int band) {
314
        return floatBuf[band][line][col];
315
    }
316
    
317
    public double getElemDouble(int line, int col, int band) {
318
        return doubleBuf[band][line][col];
319
    }
320
    
321
    //**********************************************    
322
    //Asigna un elemento de la matriz
323
    
324
    public void setElemByte(int line, int col, int band, byte data) {
325
        byteBuf[band][line][col] = data;
326
    }
327
    
328
    public void setElemShort(int line, int col, int band, short data) {
329
        shortBuf[band][line][col] = data;
330
    }
331
    
332
    public void setElemInt(int line, int col, int band, int data) {
333
        intBuf[band][line][col] = data;
334
    }
335
    
336
    public void setElemFloat(int line, int col, int band, float data) {
337
        floatBuf[band][line][col] = data;
338
    }
339
    
340
    public void setElemDouble(int line, int col, int band, double data) {
341
        doubleBuf[band][line][col] = data;
342
    }
343
    
344
    //***********************************************
345
    //Copia un elemento de todas la bandas en el buffer pasado por par?metro
346
    
347
    public void getElemByte(int line, int col, byte[] data) {
348
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
349
            data[iBand] = byteBuf[iBand][line][col];
350
    }
351
    
352
    public void getElemShort(int line, int col, short[] data) {
353
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
354
            data[iBand] = shortBuf[iBand][line][col];
355
    }
356
    
357
    public void getElemInt(int line, int col, int[] data) {
358
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
359
            data[iBand] = intBuf[iBand][line][col];
360
    }  
361
    
362
    public void getElemFloat(int line, int col, float[] data) {
363
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
364
            data[iBand] = floatBuf[iBand][line][col];
365
    }
366
    
367
    public void getElemDouble(int line, int col, double[] data) {
368
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
369
            data[iBand] = doubleBuf[iBand][line][col];
370
    }
371

    
372
    //***********************************************
373
    //Asigna un elemento a todas la bandas en el buffer pasado por par?metro
374
    
375
    public void setElemByte(int line, int col, byte[] data) {
376
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
377
            byteBuf[iBand][line][col] = data[iBand];
378
    }
379
    
380
    public void setElemShort(int line, int col, short[] data) {
381
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
382
            shortBuf[iBand][line][col] = data[iBand];
383
    }
384
    
385
    public void setElemInt(int line, int col, int[] data) {
386
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
387
            intBuf[iBand][line][col] = data[iBand];
388
    }
389

    
390
    public void setElemFloat(int line, int col, float[] data) {
391
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
392
            floatBuf[iBand][line][col] = data[iBand];
393
    }
394
    
395
    public void setElemDouble(int line, int col, double[] data) {
396
            for (int iBand = 0; (iBand < nBands && iBand < data.length); iBand++)
397
            doubleBuf[iBand][line][col] = data[iBand];
398
    }
399

    
400
    //***********************************************
401
    //Obtiene una banda entera
402
    
403
    public byte[][] getBandByte(int band) {
404
        return byteBuf[band];
405
    }
406

    
407
    public short[][] getBandShort(int band) {
408
        return shortBuf[band];
409
    }
410

    
411
    public int[][] getBandInt(int band) {
412
        return intBuf[band];
413
    }
414
        
415
    public float[][] getBandFloat(int band) {
416
        return floatBuf[band];
417
    }
418
    
419
    public double[][] getBandDouble(int band) {
420
        return doubleBuf[band];
421
    }
422

    
423
    //***********************************************
424
    //Sustituye una banda entera
425
    
426
    public void getBandByte(int band, byte[][] data) {
427
        byteBuf[band] = data;
428
    }
429

    
430
    public void getBandShort(int band, short[][] data) {
431
        shortBuf[band] = data;
432
    }
433

    
434
    public void getBandInt(int band, int[][] data) {
435
        intBuf[band] = data;
436
    }
437
        
438
    public void getBandFloat(int band, float[][] data) {
439
        floatBuf[band] = data;
440
    }
441
    
442
    public void getBandDouble(int band, double[][] data) {
443
        doubleBuf[band] = data;
444
    }
445
    
446
    //***********************************************
447
    //Crea un buffer banda inicializado con el valor pasado por par?metro
448

    
449
    public static byte[][] createByteBand(int width, int height, byte defaultValue){
450
            byte[][] band = new byte[height][width];
451
            if(defaultValue != 0){
452
                    for(int line = 0; line < height; line ++)
453
                            for(int col = 0; col < width; col ++)
454
                                    band[line][col] = defaultValue;
455
            }
456
            return band;
457
    }
458
    
459
    public static short[][] createShortBand(int width, int height, short defaultValue){
460
            short[][] band = new short[height][width];
461
            if(defaultValue != 0){
462
                    for(int line = 0; line < height; line ++)
463
                            for(int col = 0; col < width; col ++)
464
                                    band[line][col] = defaultValue;
465
            }
466
            return band;
467
    }
468
    
469
    public static int[][] createIntBand(int width, int height, int defaultValue){
470
            int[][] band = new int[height][width];
471
            if(defaultValue != 0){
472
                    for(int line = 0; line < height; line ++)
473
                            for(int col = 0; col < width; col ++)
474
                                    band[line][col] = defaultValue;
475
            }
476
            return band;
477
    }
478
    
479
    public static float[][] createFloatBand(int width, int height, float defaultValue){
480
            float[][] band = new float[height][width];
481
            if(defaultValue != 0){
482
                    for(int line = 0; line < height; line ++)
483
                            for(int col = 0; col < width; col ++)
484
                                    band[line][col] = defaultValue;
485
            }
486
            return band;
487
    }
488
    
489
    public static double[][] createDoubleBand(int width, int height, double defaultValue){
490
            double[][] band = new double[height][width];
491
            if(defaultValue != 0){
492
                    for(int line = 0; line < height; line ++)
493
                            for(int col = 0; col < width; col ++)
494
                                    band[line][col] = defaultValue;
495
            }
496
            return band;
497
    }
498
    
499
    //***********************************************
500
    //A?ade una banda entera. Los datos son asignados por referencia
501

    
502
    public void addBandByte(int pos, byte[][] data) {
503
            if(pos < 0)
504
                    return;
505
            byte[][][] tmp = null;
506
            if(pos >= byteBuf.length){
507
                    tmp = new byte[pos + 1][][];
508
                    for(int iBand = 0; iBand < byteBuf.length; iBand ++)
509
                            tmp[iBand] = byteBuf[iBand];
510
                    tmp[pos] = data;
511
            }else{
512
                    tmp = new byte[byteBuf.length + 1][][];
513
                    for(int iBand = 0; iBand < pos; iBand ++)
514
                            tmp[iBand] = byteBuf[iBand];
515
                    tmp[pos] = data;
516
                    for(int iBand = pos + 1; iBand < byteBuf.length; iBand ++)
517
                            tmp[iBand] = byteBuf[iBand - 1];
518
            }
519
            nBands++;
520
            byteBuf = tmp;
521
    }
522
    
523
    public void addBandShort(int pos, short[][] data) {
524
            if(pos < 0)
525
                    return;
526
            short[][][] tmp = null;
527
            if(pos >= shortBuf.length){
528
                    tmp = new short[pos + 1][][];
529
                    for(int iBand = 0; iBand < shortBuf.length; iBand ++)
530
                            tmp[iBand] = shortBuf[iBand];
531
                    tmp[pos] = data;
532
            }else{
533
                    tmp = new short[shortBuf.length + 1][][];
534
                    for(int iBand = 0; iBand < pos; iBand ++)
535
                            tmp[iBand] = shortBuf[iBand];
536
                    tmp[pos] = data;
537
                    for(int iBand = pos + 1; iBand < shortBuf.length; iBand ++)
538
                            tmp[iBand] = shortBuf[iBand - 1];
539
            }
540
            nBands++;
541
            shortBuf = tmp;
542
    }
543
    
544
    public void addBandInt(int pos, int[][] data) {
545
            if(pos < 0)
546
                    return;
547
            int[][][] tmp = null;
548
            if(pos >= intBuf.length){
549
                    tmp = new int[pos + 1][][];
550
                    for(int iBand = 0; iBand < intBuf.length; iBand ++)
551
                            tmp[iBand] = intBuf[iBand];
552
                    tmp[pos] = data;
553
            }else{
554
                    tmp = new int[intBuf.length + 1][][];
555
                    for(int iBand = 0; iBand < pos; iBand ++)
556
                            tmp[iBand] = intBuf[iBand];
557
                    tmp[pos] = data;
558
                    for(int iBand = pos + 1; iBand < intBuf.length; iBand ++)
559
                            tmp[iBand] = intBuf[iBand - 1];
560
            }
561
            nBands++;
562
            intBuf = tmp;
563
    }
564
    
565
    public void addBandFloat(int pos, float[][] data) {
566
            if(pos < 0)
567
                    return;
568
            float[][][] tmp = null;
569
            if(pos >= floatBuf.length){
570
                    tmp = new float[pos + 1][][];
571
                    for(int iBand = 0; iBand < floatBuf.length; iBand ++)
572
                            tmp[iBand] = floatBuf[iBand];
573
                    tmp[pos] = data;
574
            }else{
575
                    tmp = new float[floatBuf.length + 1][][];
576
                    for(int iBand = 0; iBand < pos; iBand ++)
577
                            tmp[iBand] = floatBuf[iBand];
578
                    tmp[pos] = data;
579
                    for(int iBand = pos + 1; iBand < floatBuf.length; iBand ++)
580
                            tmp[iBand] = floatBuf[iBand - 1];
581
            }
582
            nBands++;
583
            floatBuf = tmp;
584
    }
585
    
586
    public void addBandDouble(int pos, double[][] data) {
587
            if(pos < 0)
588
                    return;
589
            double[][][] tmp = null;
590
            if(pos >= doubleBuf.length){
591
                    tmp = new double[pos + 1][][];
592
                    for(int iBand = 0; iBand < doubleBuf.length; iBand ++)
593
                            tmp[iBand] = doubleBuf[iBand];
594
                    tmp[pos] = data;
595
            }else{
596
                    tmp = new double[doubleBuf.length + 1][][];
597
                    for(int iBand = 0; iBand < pos; iBand ++)
598
                            tmp[iBand] = doubleBuf[iBand];
599
                    tmp[pos] = data;
600
                    for(int iBand = pos + 1; iBand < doubleBuf.length; iBand ++)
601
                            tmp[iBand] = doubleBuf[iBand - 1];
602
            }
603
            nBands++;
604
            doubleBuf = tmp;
605
    }
606
    
607
    //***********************************************
608
    //Reemplaza una banda entera. Los datos son reemplazados por referencia
609
    
610
    public void replaceBandByte(int pos, byte[][] data) {
611
            if(pos >= byteBuf.length)
612
                    return;
613
            byteBuf[pos] = data;
614
    }
615
    
616
    public void replaceBandShort(int pos, short[][] data) {
617
            if(pos >= shortBuf.length)
618
                    return;
619
            shortBuf[pos] = data;
620
    }
621
    
622
    public void replaceBandInt(int pos, int[][] data) {
623
            if(pos >= intBuf.length)
624
                    return;
625
            intBuf[pos] = data;
626
    }
627
    
628
    public void replaceBandFloat(int pos, float[][] data) {
629
            if(pos >= floatBuf.length)
630
                    return;
631
            floatBuf[pos] = data;
632
    }
633
    
634
    public void replaceBandDouble(int pos, double[][] data) {
635
            if(pos >= doubleBuf.length)
636
                    return;
637
            doubleBuf[pos] = data;
638
    }
639
    
640
    /**
641
     * Replica la banda de una posici?n sobre otra. Si la banda de destino no existe
642
     * se crea nueva. Si la posici?n de la banda de destino est? intercalada entre bandas 
643
     * que ya existen las otras se desplazan hacia abajo, NO se machacan los datos de ninguna.
644
     * Los datos se replican por referencia por lo que al modificar la banda original las
645
     * del resto quedar?n afectadas.
646
     * @param orig. Posici?n de la banda de origen. 
647
     * @param dest. Posici?n de la banda destino
648
     */   
649
    public void replicateBand(int orig, int dest){
650
            copyBand(this, orig, dest);
651
    }
652
    
653
    /**
654
     * Copia una banda de una imagen desde una posici?n sobre otra en la imagen actual.
655
     *  Si la banda de destino no existe
656
     * se crea nueva. Si la posici?n de la banda de destino est? intercalada entre bandas 
657
     * que ya existen las otras se desplazan hacia abajo, NO se machacan los datos de ninguna.
658
     * Los datos se replican por referencia por lo que al modificar la banda original las
659
     * del resto quedar?n afectadas.
660
     * @param orig. Posici?n de la banda de origen. 
661
     * @param dest. Posici?n de la banda destino
662
     */   
663
    public void copyBand(RasterBuf from, int orig, int dest){
664
            switch(getDataType()){
665
            case RasterBuf.TYPE_BYTE:         if(orig >= byteBuf.length)
666
                                                                                return;
667
                                                                        addBandByte(dest, from.getBandByte(orig));
668
                                                                        break;
669
            case RasterBuf.TYPE_SHORT:         if(orig >= shortBuf.length)
670
                                                                            return;
671
                                                                    addBandShort(dest, from.getBandShort(orig));
672
                                                                    break;
673
            case RasterBuf.TYPE_INT:         if(orig >= intBuf.length)
674
                                                                                return;
675
                                                                        addBandInt(dest, from.getBandInt(orig));
676
                                                                        break;
677
            case RasterBuf.TYPE_FLOAT:         if(orig >= floatBuf.length)
678
                                                                                return;
679
                                                                        addBandFloat(dest, from.getBandFloat(orig));
680
                                                                        break;
681
            case RasterBuf.TYPE_DOUBLE:        if(orig >= doubleBuf.length)
682
                                                                                return;
683
                                                                        addBandDouble(dest, from.getBandDouble(orig));
684
                                                                        break;
685
            }
686
    }
687
    /**
688
     * Convierte un tipo de dato a cadena
689
     * @param type Tipo de dato
690
     * @return cadena  que representa el tipo de dato
691
     */
692
    public static String typesToString(int type) {
693
        switch (type) {
694
        case RasterBuf.TYPE_IMAGE:
695
            return new String("Image");
696

    
697
        case RasterBuf.TYPE_BYTE:
698
            return new String("Byte");
699

    
700
        case RasterBuf.TYPE_DOUBLE:
701
            return new String("Double");
702

    
703
        case RasterBuf.TYPE_FLOAT:
704
            return new String("Float");
705

    
706
        case RasterBuf.TYPE_INT:
707
                return new String("Integer");
708
                
709
        case RasterBuf.TYPE_USHORT:
710
        case RasterBuf.TYPE_SHORT:
711
            return new String("Short");
712
        }
713

    
714
        return null;
715
    }
716
    
717
    /**
718
     * Ajusta el raster al ancho y alto solicitado por el vecino m?s cercano.
719
     * @param w Nuevo ancho
720
     * @param h Nuevo alto
721
     */
722
    public RasterBuf adjustRasterNearestNeighbour(int w, int h, int[] bands){
723
            double stepX = (double)w / (double)width;
724
            double stepY = (double)h / (double)height;
725
            RasterBuf rasterBuf = new RasterBuf(getDataType(), w, h, getBandCount(), true);
726
            
727
            //Si bands == null las bandas a copiar son todas las de la imagen
728
            if(bands == null){
729
                    bands = new int[rasterBuf.getBandCount()];
730
                    for(int iBand = 0; iBand < rasterBuf.getBandCount(); iBand ++)
731
                            bands[iBand] = iBand;
732
            }
733
            
734
            switch (dataType) {
735
        case RasterBuf.TYPE_IMAGE:
736
                    
737
        case RasterBuf.TYPE_BYTE:
738
                for(int iBand = 0; iBand < bands.length; iBand ++){
739
                        if(w <= width){ //submuestreo
740
                                for(int iRow = 0; iRow < height; iRow ++)
741
                                        for(int iCol = 0; iCol < width; iCol ++)
742
                                                rasterBuf.setElemByte((int)(iRow * stepY), (int)(iCol * stepX), bands[iBand], getElemByte(iRow, iCol, iBand));
743
                        }else{ //supermuestreo
744
                                for(int iRow = 0; iRow < h; iRow ++)
745
                                        for(int iCol = 0; iCol < w; iCol ++)
746
                                                rasterBuf.setElemByte(iRow, iCol, bands[iBand], getElemByte((int)(iRow / stepY), (int)(iCol / stepX), iBand));
747
                        }
748
                }
749
                break;
750
        case RasterBuf.TYPE_DOUBLE:
751
                for(int iBand = 0; iBand < bands.length; iBand ++){
752
                        if(w <= width){ //submuestreo
753
                                for(int iRow = 0; iRow < height; iRow ++)
754
                                        for(int iCol = 0; iCol < width; iCol ++)
755
                                                rasterBuf.setElemDouble((int)(iRow * stepY), (int)(iCol * stepX), bands[iBand], getElemDouble(iRow, iCol, iBand));
756
                        }else{ //supermuestreo
757
                                for(int iRow = 0; iRow < h; iRow ++)
758
                                        for(int iCol = 0; iCol < w; iCol ++)
759
                                                rasterBuf.setElemDouble(iRow, iCol, bands[iBand], getElemDouble((int)(iRow / stepY), (int)(iCol / stepX), iBand));
760
                        }
761
                }
762
                break;
763
        case RasterBuf.TYPE_FLOAT:
764
                for(int iBand = 0; iBand < bands.length; iBand ++){
765
                        if(w <= width){ //submuestreo
766
                                for(int iRow = 0; iRow < height; iRow ++)
767
                                        for(int iCol = 0; iCol < width; iCol ++)
768
                                                rasterBuf.setElemFloat((int)(iRow * stepY), (int)(iCol * stepX), bands[iBand], getElemFloat(iRow, iCol, iBand));
769
                        }else{ //supermuestreo
770
                                for(int iRow = 0; iRow < h; iRow ++)
771
                                        for(int iCol = 0; iCol < w; iCol ++)
772
                                                rasterBuf.setElemFloat(iRow, iCol, bands[iBand], getElemFloat((int)(iRow / stepY), (int)(iCol / stepX), iBand));
773
                        }
774
                }
775
                break;
776
        case RasterBuf.TYPE_INT:
777
                for(int iBand = 0; iBand < bands.length; iBand ++){
778
                        if(w <= width){ //submuestreo
779
                                for(int iRow = 0; iRow < height; iRow ++)
780
                                        for(int iCol = 0; iCol < width; iCol ++)
781
                                                rasterBuf.setElemInt((int)(iRow * stepY), (int)(iCol * stepX), bands[iBand], getElemInt(iRow, iCol, iBand));
782
                        }else{ //supermuestreo
783
                                for(int iRow = 0; iRow < h; iRow ++)
784
                                        for(int iCol = 0; iCol < w; iCol ++)
785
                                                rasterBuf.setElemInt(iRow, iCol, bands[iBand], getElemInt((int)(iRow / stepY), (int)(iCol / stepX), iBand));
786
                        }
787
                }
788
                break;
789
        case RasterBuf.TYPE_USHORT:
790
        case RasterBuf.TYPE_SHORT:
791
                for(int iBand = 0; iBand < bands.length; iBand ++){
792
                        if(w <= width){ //submuestreo
793
                                for(int iRow = 0; iRow < height; iRow ++)
794
                                        for(int iCol = 0; iCol < width; iCol ++)
795
                                                rasterBuf.setElemShort((int)(iRow * stepY), (int)(iCol * stepX), bands[iBand], getElemShort(iRow, iCol, iBand));
796
                        }else{ //supermuestreo
797
                                for(int iRow = 0; iRow < h; iRow ++)
798
                                        for(int iCol = 0; iCol < w; iCol ++)
799
                                                rasterBuf.setElemShort(iRow, iCol, bands[iBand], getElemShort((int)(iRow / stepY), (int)(iCol / stepX), iBand));
800
                        }
801
                }
802
                break;
803
        }
804
            return rasterBuf; 
805
    }
806
    
807
    /**
808
     * Salva a un formato soportado por GeoRasterWriter. El formato de destino 
809
     * est? soportado por la extensi?n del fichero seleccionado en el par?metro path.
810
     * @param path Ruta al fichero de destino
811
     * @param ex        Extent
812
     * @throws IOException
813
     */
814
    public void save(String path, Extent ex) throws IOException {
815
                IDataWriter dataWriter = new WriterBufferServer(this);
816
                GeoRasterWriter grw = GeoRasterWriter.getWriter(dataWriter, 
817
                                                                                                                path, 
818
                                                                                                                height, //Tama?o de cada bloque le?do. 
819
                                                                                                                        //Si ponemos el alto de la imagen la leer? toda de golpe 
820
                                                                                                                getBandCount(), 
821
                                                                                                                ex, 
822
                                                                                                                0, 
823
                                                                                                                width, 
824
                                                                                                                height, 
825
                                                                                                                getDataType());
826
                grw.dataWrite();
827
                grw.writeClose();
828
                
829
        }
830
    
831
    /*
832
     *  (non-Javadoc)
833
     * @see org.gvsig.fmap.driver.IBuffer#getNoDataValue()
834
     */
835
    public double getNoDataValue(){
836
            return noDataValue;
837
    }
838
    
839
    /*
840
     *  (non-Javadoc)
841
     * @see org.gvsig.fmap.driver.IBuffer#getByteNoDataValue()
842
     */
843
    public byte getByteNoDataValue(){
844
            return (byte)noDataValue;
845
    }
846
    
847
    /*
848
     *  (non-Javadoc)
849
     * @see org.gvsig.fmap.driver.IBuffer#getShortNoDataValue()
850
     */
851
    public short getShortNoDataValue(){
852
            return (short)noDataValue;
853
    }
854
    
855
    /*
856
     *  (non-Javadoc)
857
     * @see org.gvsig.fmap.driver.IBuffer#getIntNoDataValue()
858
     */
859
    public int getIntNoDataValue(){
860
            return (int)noDataValue;
861
    }
862
    
863
    /*
864
     *  (non-Javadoc)
865
     * @see org.gvsig.fmap.driver.IBuffer#getFloatNoDataValue()
866
     */
867
    public float getFloatNoDataValue(){
868
            return (float)noDataValue;
869
    }
870
    
871
    /*
872
     *  (non-Javadoc)
873
     * @see org.gvsig.fmap.driver.IBuffer#setNoDataValue(double)
874
     */
875
    public void setNoDataValue(double nd){
876
            noDataValue = nd;
877
    }
878
    
879
    public void assign(int band, byte value) {
880
            for(int line = 0; line < height; line ++)
881
                    for(int col = 0; col < width; col ++)
882
                            byteBuf[band][line][col] = value;
883
    }
884
    
885
    public void assign(int band, short value) {
886
            for(int line = 0; line < height; line ++)
887
                    for(int col = 0; col < width; col ++)
888
                            shortBuf[band][line][col] = value;
889
    }
890
    
891
    public void assign(int band, int value) {
892
            for(int line = 0; line < height; line ++)
893
                    for(int col = 0; col < width; col ++)
894
                            intBuf[band][line][col] = value;
895
    }
896

    
897
    public void assign(int band, float value) {
898
            for(int line = 0; line < height; line ++)
899
                    for(int col = 0; col < width; col ++)
900
                            floatBuf[band][line][col] = value;
901
    }
902
    
903
    public void assign(int band, double value) {
904
            for(int line = 0; line < height; line ++)
905
                    for(int col = 0; col < width; col ++)
906
                            doubleBuf[band][line][col] = value;
907
    }
908
    
909
    public void setToNoData(){
910
            if (dataType == TYPE_BYTE) {
911
                    for (int iBand = 0; iBand < this.nBands; iBand++) {
912
                        for(int line = 0; line < height; line ++)
913
                            for(int col = 0; col < width; col ++)
914
                                    byteBuf[iBand][line][col] = (byte)noDataValue;        
915
                    }
916
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
917
                for (int iBand = 0; iBand < this.nBands; iBand++) {
918
                        for(int line = 0; line < height; line ++)
919
                            for(int col = 0; col < width; col ++)
920
                                    shortBuf[iBand][line][col] = (short)noDataValue;        
921
                    }
922
        } else if (dataType == TYPE_INT) {
923
                for (int iBand = 0; iBand < this.nBands; iBand++) {
924
                        for(int line = 0; line < height; line ++)
925
                            for(int col = 0; col < width; col ++)
926
                                    intBuf[iBand][line][col] = (int)noDataValue;        
927
                    }
928
        } else if (dataType == TYPE_FLOAT) {
929
                for (int iBand = 0; iBand < this.nBands; iBand++) {
930
                        for(int line = 0; line < height; line ++)
931
                            for(int col = 0; col < width; col ++)
932
                                    floatBuf[iBand][line][col] = (float)noDataValue;        
933
                    }
934
        } else if (dataType == TYPE_DOUBLE) {
935
                for (int iBand = 0; iBand < this.nBands; iBand++) {
936
                        for(int line = 0; line < height; line ++)
937
                            for(int col = 0; col < width; col ++)
938
                                    doubleBuf[iBand][line][col] = noDataValue;        
939
                    }
940
        }
941
            
942
    }
943
}