Statistics
| Revision:

gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / src / main / java / org / gvsig / raster / cache / buffer / impl / memory / RasterMemoryBuffer.java @ 995

History | View | Annotate | Download (24.1 KB)

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

    
3
import java.io.IOException;
4
import java.util.ArrayList;
5

    
6
import org.gvsig.raster.cache.buffer.Band;
7
import org.gvsig.raster.cache.buffer.Buffer;
8
import org.gvsig.raster.cache.buffer.exception.BandNotCompatibleException;
9
import org.gvsig.raster.cache.buffer.exception.OperationNotSupportedException;
10
import org.gvsig.raster.cache.buffer.exception.WrongParameterException;
11
import org.gvsig.raster.cache.buffer.impl.RasterBuffer;
12

    
13

    
14
/**
15
 * Implementaci?n del buffer de datos en memoria. Contiene las operaciones necesarias
16
 * para acceso a datos raster implementando IRasterBuffer y su almacenamiento de datos est? basado
17
 * en arrays tridimensionales. Existe un array para cada tipo de dato pero al instanciarse
18
 * solo se usar? uno de ellos, el que corresponda al tipo de dato del raster manejado. 
19
 * Esto quiere decir que cada RasterMemoryBuffer solo puede contener bandas de un solo tipo de
20
 * dato donde la variable dataType especificar? de que tipo de dato se trata.
21
 * 
22
 * @author Nacho Brodin (nachobrodin@gmail.com)
23
 *
24
 */
25
public class RasterMemoryBuffer extends RasterBuffer {
26
        protected ArrayList<MemoryBand> bands = new ArrayList<MemoryBand>();
27
       
28
    /**
29
     * Constructor
30
     * @param dataType Tipo de dato
31
     * @param width Ancho
32
     * @param height Alto
33
     * @param bandNr Banda
34
     * @param orig
35
     */
36
    public RasterMemoryBuffer(int dataType, int width, int height, int bandNr, boolean malloc) {
37
            super(0, 0, width, height, dataType, bandNr);
38
                   load(dataType, width, height, bandNr, malloc);
39
    }
40
    
41
    /*
42
     *  (non-Javadoc)
43
     * @see org.gvsig.fmap.dataaccess.buffer.RasterBuffer#malloc(int, int, int, int)
44
     */
45
    private void load(int dataType, int width, int height, int bandNr, boolean malloc) {
46
        if (dataType == TYPE_BYTE) {
47
            for(int i = 0; i < bandNr; i++)
48
                    bands.add(new ByteBand(width, height, i, malloc));
49
        } else if ((dataType == TYPE_SHORT) | (dataType == TYPE_USHORT)) {
50
            for(int i = 0; i < bandNr; i++)
51
                    bands.add(new ShortBand(width, height, i, malloc));
52
        } else if (dataType == TYPE_INT) {
53
            for(int i = 0; i < bandNr; i++)
54
                    bands.add(new IntBand(width, height, i, malloc));
55
        } else if (dataType == TYPE_FLOAT) {
56
            for(int i = 0; i < bandNr; i++)
57
                    bands.add(new FloatBand(width, height, i, malloc));
58
        } else if (dataType == TYPE_DOUBLE) {
59
            for(int i = 0; i < bandNr; i++)
60
                    bands.add(new DoubleBand(width, height, i, malloc));
61
        }
62

    
63
    }
64
        
65
    /**
66
     * Obtiene el tama?o del tipo de dato en bytes
67
     * @return Tipo de dato
68
     */
69
    public int getDataSize() {
70
        if (getDataType() == TYPE_BYTE) {
71
            return 1;
72
        } else if ((getDataType() == TYPE_SHORT) | (getDataType() == TYPE_USHORT)) {
73
            return 2;
74
        } else if (getDataType() == TYPE_INT) {
75
            return 4;
76
        }else if (getDataType() == TYPE_FLOAT) {
77
            return 8;
78
        }else if (getDataType() == TYPE_DOUBLE) {
79
            return 16;
80
        }
81

    
82
        return 0;
83
    }
84

    
85
    /*
86
     * (non-Javadoc)
87
     * @see org.fv.raster.buffer.IRasterBuffer#isWritable()
88
     */
89
    public boolean isWritable() {
90
            return true;
91
    }
92
    
93
    //***********************************************
94
    //Obtiene una linea de datos con todas las bandas
95
    
96
    public byte[][] getLineByte(int line) {
97
            byte[][] r = new byte[getBandCount()][];
98
            for(int iBand = 0; iBand < getBandCount(); iBand ++)
99
                    r[iBand] = bands.get(iBand).getByteLine(line);
100
            return r;
101
    }
102

    
103
    public short[][] getLineShort(int line) {
104
            short[][] r = new short[getBandCount()][];
105
            for(int iBand = 0; iBand < getBandCount(); iBand ++)
106
                    r[iBand] = bands.get(iBand).getShortLine(line);
107
            return r;
108
    }
109

    
110
    public int[][] getLineInt(int line) {
111
            int[][] r = new int[getBandCount()][];
112
            for(int iBand = 0; iBand < getBandCount(); iBand ++)
113
                    r[iBand] = bands.get(iBand).getIntLine(line);
114
            return r;
115
    }
116
        
117
    public float[][] getLineFloat(int line) {
118
            float[][] r = new float[getBandCount()][];
119
            for(int iBand = 0; iBand < getBandCount(); iBand ++)
120
                    r[iBand] = bands.get(iBand).getFloatLine(line);
121
            return r;
122
    }
123
    
124
    public double[][] getLineDouble(int line) {
125
            double[][] r = new double[getBandCount()][];
126
            for(int iBand = 0; iBand < getBandCount(); iBand ++)
127
                    r[iBand] = bands.get(iBand).getDoubleLine(line);
128
            return r;
129
    }
130

    
131
    //***********************************************
132
    //Asigna una linea de datos a todas las bandas
133
    
134
    public void setLineByte(byte[][] data, int line) {
135
            for(int iBand = 0; iBand < getBandCount(); iBand ++)
136
                    bands.get(iBand).setByteLine(data[iBand], line);
137
    }
138

    
139
    public void setLineShort(short[][] data, int line) {
140
            for(int iBand = 0; iBand < getBandCount(); iBand ++)
141
                    bands.get(iBand).setShortLine(data[iBand], line);
142
    }
143

    
144
    public void setLineInt(int[][] data, int line) {
145
            for(int iBand = 0; iBand < getBandCount(); iBand ++)
146
                    bands.get(iBand).setIntLine(data[iBand], line);
147
    }
148
        
149
    public void setLineFloat(float[][] data, int line) {
150
            for(int iBand = 0; iBand < getBandCount(); iBand ++)
151
                    bands.get(iBand).setFloatLine(data[iBand], line);
152
    }
153
    
154
    public void setLineDouble(double[][] data, int line) {
155
            for(int iBand = 0; iBand < getBandCount(); iBand ++)
156
                    bands.get(iBand).setDoubleLine(data[iBand], line);
157
    }
158
    
159
    //***********************************************
160
    //Obtiene una linea de datos de una banda
161
    
162
    public byte[] getLineFromBandByte(int line, int iBand) {
163
        return bands.get(iBand).getByteLine(line);
164
    }
165

    
166
    public short[] getLineFromBandShort(int line, int iBand) {
167
        return bands.get(iBand).getShortLine(line);
168
    }
169

    
170
    public int[] getLineFromBandInt(int line, int iBand) {
171
        return bands.get(iBand).getIntLine(line);
172
    }
173
        
174
    public float[] getLineFromBandFloat(int line, int iBand) {
175
        return bands.get(iBand).getFloatLine(line);
176
    }
177
    
178
    public double[] getLineFromBandDouble(int line, int iBand) {
179
        return bands.get(iBand).getDoubleLine(line);
180
    }
181
        
182
    //***********************************************
183
    //Asigna una linea de datos a una banda
184
    
185
    public void setLineInBandByte(byte[] data, int line, int iBand) {
186
            bands.get(iBand).setByteLine(data, line);
187
    }
188

    
189
    public void setLineInBandShort(short[] data, int line, int iBand) {
190
            bands.get(iBand).setShortLine(data, line);
191
    }
192

    
193
    public void setLineInBandInt(int[] data, int line, int iBand) {
194
            bands.get(iBand).setIntLine(data, line);
195
    }
196
        
197
    public void setLineInBandFloat(float[] data, int line, int iBand) {
198
            bands.get(iBand).setFloatLine(data, line);
199
    }
200
    
201
    public void setLineInBandDouble(double[] data, int line, int iBand) {
202
            bands.get(iBand).setDoubleLine(data, line);
203
    }
204
    
205
    //***********************************************    
206
    //Obtiene un elemento de la matriz
207
    
208
    public byte getElemByte(int line, int col, int iBand) {
209
            return bands.get(iBand).getElemByte(line, col);
210
    }
211
    
212
    public short getElemShort(int line, int col, int iBand) {
213
            return bands.get(iBand).getElemShort(line, col);
214
    }
215
    
216
    public int getElemInt(int line, int col, int iBand) {
217
            return bands.get(iBand).getElemInt(line, col);
218
    }
219
    
220
    public float getElemFloat(int line, int col, int iBand) {
221
            return bands.get(iBand).getElemFloat(line, col);
222
    }
223
    
224
    public double getElemDouble(int line, int col, int iBand) {
225
            return bands.get(iBand).getElemDouble(line, col);
226
    }
227
    
228
    //**********************************************    
229
    //Asigna un elemento de la matriz
230
    
231
    public void setElem(int line, int col, int iBand, byte data) {
232
            bands.get(iBand).setElem(line, col, data);
233
    }
234
    
235
    public void setElem(int line, int col, int iBand, short data) {
236
            bands.get(iBand).setElem(line, col, data);
237
    }
238
    
239
    public void setElem(int line, int col, int iBand, int data) {
240
            bands.get(iBand).setElem(line, col, data);
241
    }
242
    
243
    public void setElem(int line, int col, int iBand, float data) {
244
            bands.get(iBand).setElem(line, col, data);
245
    }
246
    
247
    public void setElem(int line, int col, int iBand, double data) {
248
            bands.get(iBand).setElem(line, col, data);
249
    }
250
    
251
    //***********************************************
252
    //Copia un elemento de todas la bandas en el buffer pasado por par?metro
253
    
254
    public void getElemByte(int line, int col, byte[] data) {
255
            for (int iBand = 0; (iBand < getBandCount() && iBand < data.length); iBand++)
256
            data[iBand] = bands.get(iBand).getElemByte(line, col);
257
    }
258
    
259
    public void getElemShort(int line, int col, short[] data) {
260
            for (int iBand = 0; (iBand < getBandCount() && iBand < data.length); iBand++)
261
            data[iBand] = bands.get(iBand).getElemShort(line, col);
262
    }
263
    
264
    public void getElemInt(int line, int col, int[] data) {
265
            for (int iBand = 0; (iBand < getBandCount() && iBand < data.length); iBand++)
266
            data[iBand] = bands.get(iBand).getElemInt(line, col);
267
    }  
268
    
269
    public void getElemFloat(int line, int col, float[] data) {
270
            for (int iBand = 0; (iBand < getBandCount() && iBand < data.length); iBand++)
271
            data[iBand] = bands.get(iBand).getElemFloat(line, col);
272
    }
273
    
274
    public void getElemDouble(int line, int col, double[] data) {
275
            for (int iBand = 0; (iBand < getBandCount() && iBand < data.length); iBand++)
276
            data[iBand] = bands.get(iBand).getElemDouble(line, col);
277
    }
278

    
279
    //***********************************************
280
    //Asigna un elemento a todas la bandas en el buffer pasado por par?metro
281
    
282
    public void setElemByte(int line, int col, byte[] data) {
283
            for (int iBand = 0; (iBand < getBandCount() && iBand < data.length); iBand++)
284
                    bands.get(iBand).setElem(line, col, data[iBand]);
285
    }
286
    
287
    public void setElemShort(int line, int col, short[] data) {
288
            for (int iBand = 0; (iBand < getBandCount() && iBand < data.length); iBand++)
289
                    bands.get(iBand).setElem(line, col, data[iBand]);
290
    }
291
    
292
    public void setElemInt(int line, int col, int[] data) {
293
            for (int iBand = 0; (iBand < getBandCount() && iBand < data.length); iBand++)
294
                    bands.get(iBand).setElem(line, col, data[iBand]);
295
    }
296

    
297
    public void setElemFloat(int line, int col, float[] data) {
298
            for (int iBand = 0; (iBand < getBandCount() && iBand < data.length); iBand++)
299
                    bands.get(iBand).setElem(line, col, data[iBand]);
300
    }
301
    
302
    public void setElemDouble(int line, int col, double[] data) {
303
            for (int iBand = 0; (iBand < getBandCount() && iBand < data.length); iBand++)
304
                    bands.get(iBand).setElem(line, col, data[iBand]);
305
    }
306
    
307
    //***********************************************
308
    //Obtiene una banda entera
309
    
310
    /*
311
     * (non-Javadoc)
312
     * @see org.gvsig.raster.buffer.IRasterBuffer#getBandByRef(int)
313
     */
314
    public Band getBand(int iBand){
315
            return bands.get(iBand);
316
    }
317
    
318
    /*
319
     * (non-Javadoc)
320
     * @see org.gvsig.raster.buffer.IRasterBuffer#getBand(int)
321
     */
322
    public Band getBandCopy(int iBand) {
323
            if (getDataType() == TYPE_BYTE) {
324
                    ByteBand bb = new ByteBand(getWidth(), getHeight(), 0, true);
325
                    for (int row = 0; row < bb.getHeight(); row++) 
326
                            for (int col = 0; col < bb.getWidth(); col++) 
327
                                    bb.setElem(row, col, bands.get(iBand).getElemByte(row, col));
328
                    return bb;
329
        } else if ((getDataType() == TYPE_SHORT) | (getDataType() == TYPE_USHORT)) {
330
                    ShortBand sb = new ShortBand(getWidth(), getHeight(), 0, true);
331
                    for (int row = 0; row < sb.getHeight(); row++) 
332
                            for (int col = 0; col < sb.getWidth(); col++) 
333
                                    sb.setElem(row, col, bands.get(iBand).getElemShort(row, col));
334
                    return sb;
335
        } else if (getDataType() == TYPE_INT) {
336
                    IntBand ib = new IntBand(getWidth(), getHeight(), 0, true);
337
                    for (int row = 0; row < ib.getHeight(); row++) 
338
                            for (int col = 0; col < ib.getWidth(); col++) 
339
                                    ib.setElem(row, col, bands.get(iBand).getElemInt(row, col));
340
                    return ib;
341
        }else if (getDataType() == TYPE_FLOAT) {
342
                    FloatBand fb = new FloatBand(getWidth(), getHeight(), 0, true);
343
                    for (int row = 0; row < fb.getHeight(); row++) 
344
                            for (int col = 0; col < fb.getWidth(); col++) 
345
                                    fb.setElem(row, col, bands.get(iBand).getElemFloat(row, col));
346
                    return fb;
347
        }else if (getDataType() == TYPE_DOUBLE) {
348
                    DoubleBand db = new DoubleBand(getWidth(), getHeight(), 0, true);
349
                    for (int row = 0; row < db.getHeight(); row++) 
350
                            for (int col = 0; col < db.getWidth(); col++) 
351
                                    db.setElem(row, col, bands.get(iBand).getElemDouble(row, col));
352
                    return db;
353
        }
354
            return null;
355
    }
356
    
357
    /*
358
     * (non-Javadoc)
359
     * @see org.gvsig.raster.buffer.IRasterBuffer#getBands()
360
     */
361
    public Band[] getBands() {
362
            Band[] b = new Band[bands.size()];
363
                for (int i = 0; i < bands.size(); i++) 
364
                        b[i] = bands.get(i);
365
                return b;
366
    }
367
    
368
        /*
369
     * (non-Javadoc)
370
     * @see org.fv.raster.buffer.IRasterBuffer#getBandList()
371
     */
372
    public ArrayList<Band> getBandList() {
373
            ArrayList<Band> result = new ArrayList<Band>();
374
            for (int i = 0; i < bands.size(); i++) 
375
                        result.add(bands.get(i));
376
            return result;
377
    }
378
    
379
    /*
380
     *  (non-Javadoc)
381
     * @see org.gvsig.fmap.driver.IRasterBuffer#getBandBuffer(int)
382
     */
383
    public Buffer getBufferWithOneBand(int iBand) throws OperationNotSupportedException {
384
            RasterMemoryBuffer rmb = new RasterMemoryBuffer(getDataType(), getWidth(), getHeight(), 1, false);
385
            if (getDataType() == TYPE_BYTE) 
386
                    rmb.getBand(0).setByteBlock(bands.get(iBand).getByteBlock(-1), -1);
387
        else if ((getDataType() == TYPE_SHORT) | (getDataType() == TYPE_USHORT))
388
                rmb.getBand(0).setShortBlock(bands.get(iBand).getShortBlock(-1), -1);
389
        else if (getDataType() == TYPE_INT)
390
                rmb.getBand(0).setIntBlock(bands.get(iBand).getIntBlock(-1), -1);
391
        else if (getDataType() == TYPE_FLOAT)
392
                rmb.getBand(0).setFloatBlock(bands.get(iBand).getFloatBlock(-1), -1);
393
        else if (getDataType() == TYPE_DOUBLE)
394
                rmb.getBand(0).setDoubleBlock(bands.get(iBand).getDoubleBlock(-1), -1);
395
            return rmb;
396
    }
397
    //***********************************************
398
    //Inicializa una banda a un valor pasado por par?metro
399
    
400
    public void assign(int iBand, byte value) {
401
            for(int line = 0; line < getHeight(); line ++)
402
                    for(int col = 0; col < getWidth(); col ++)
403
                            bands.get(iBand).setElem(line, col, value);
404
    }
405
    
406
    public void assign(int iBand, short value) {
407
            for(int line = 0; line < getHeight(); line ++)
408
                    for(int col = 0; col < getWidth(); col ++)
409
                            bands.get(iBand).setElem(line, col, value);
410
    }
411
    
412
    public void assign(int iBand, int value) {
413
            for(int line = 0; line < getHeight(); line ++)
414
                    for(int col = 0; col < getWidth(); col ++)
415
                            bands.get(iBand).setElem(line, col, value);
416
    }
417

    
418
    public void assign(int iBand, float value) {
419
            for(int line = 0; line < getHeight(); line ++)
420
                    for(int col = 0; col < getWidth(); col ++)
421
                            bands.get(iBand).setElem(line, col, value);
422
    }
423
    
424
    public void assign(int iBand, double value) {
425
            for(int line = 0; line < getHeight(); line ++)
426
                    for(int col = 0; col < getWidth(); col ++)
427
                            bands.get(iBand).setElem(line, col, value);
428
    }
429
    
430
    
431
    //***********************************************
432
    //Crea un buffer banda inicializado con el valor pasado por par?metro
433
    
434

    
435
    public Band createBand(double defaultValue){
436
            switch(getDataType()){
437
            case RasterBuffer.TYPE_BYTE:ByteBand bb = new ByteBand(getWidth(), getHeight(), -1, false);
438
                                                                    bb.buf = createByteBand(getWidth(), getHeight(), (byte)defaultValue);
439
                                                                    return bb;
440
            case RasterBuffer.TYPE_SHORT:        ShortBand sb = new ShortBand(getWidth(), getHeight(), -1, false);
441
                                                                                sb.buf = createShortBand(getWidth(), getHeight(), (short)defaultValue);
442
                                                                                return sb;
443
            case RasterBuffer.TYPE_INT:        IntBand ib = new IntBand(getWidth(), getHeight(), -1, false);
444
                                                                        ib.buf = createIntBand(getWidth(), getHeight(), (int)defaultValue);
445
                                                                        return ib;
446
            case RasterBuffer.TYPE_FLOAT:        FloatBand fb = new FloatBand(getWidth(), getHeight(), -1, false);
447
                                                                                fb.buf = createFloatBand(getWidth(), getHeight(), (float)defaultValue);
448
                                                                                return fb;
449
            case RasterBuffer.TYPE_DOUBLE:        DoubleBand db = new DoubleBand(getWidth(), getHeight(), -1, false);
450
                                                                                db.buf = createDoubleBand(getWidth(), getHeight(), (double)defaultValue);
451
                                                                                return db;
452
            }
453
            return null;
454
    }
455

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

    
509
    private void addBandByReference(int iBand, Band data) {
510
            if(iBand < 0 || !(data instanceof MemoryBand))
511
                    return;
512
            if(iBand > getBandCount())
513
                    bands.add((MemoryBand)data);
514
            bands.add(iBand, (MemoryBand)data);
515
            
516
            setBandCount(bands.size());
517
    }
518
            
519
    /**
520
     * Replica la banda de una posici?n sobre otra. Si la banda de destino no existe
521
     * se crea nueva. Si la posici?n de la banda de destino est? intercalada entre bandas 
522
     * que ya existen las otras se desplazan hacia abajo, NO se machacan los datos de ninguna.
523
     * Los datos se replican por referencia por lo que al modificar la banda original las
524
     * del resto quedar?n afectadas.   
525
     * @param orig. Posici?n de la banda de origen. 
526
     * @param dest. Posici?n de la banda destino
527
     */   
528
    public void replicateBand(int orig, int dest){
529
            if(orig >= bands.size())
530
                        return;
531
                addBandByReference(dest, getBand(orig));
532
    }
533
    
534
    /*
535
     *  (non-Javadoc)
536
     * @see org.gvsig.fmap.dataaccess.buffer.RasterBuffer#switchBands(int[])
537
     */
538
    public void swapBands(int[] bandPosition) throws WrongParameterException, IOException {
539
            super.swapBands(bandPosition);
540
            ArrayList<MemoryBand> result = new ArrayList<MemoryBand>();
541
                for (int i = 0; i < bands.size(); i++) 
542
                        result.add(bands.get(bandPosition[i]));
543
                bands = result;
544
    }
545
    
546
    /*
547
     *  (non-Javadoc)
548
     * @see org.gvsig.fmap.driver.IRasterBuffer#copyBand(int, org.gvsig.fmap.dataaccess.buffer.IBand)
549
     */
550
    public void copyBand(int nBand, Band band) throws BandNotCompatibleException, OperationNotSupportedException {
551
            super.copyBand(nBand, band);
552
        }
553

    
554
    /*
555
     *  (non-Javadoc)
556
     * @see org.gvsig.fmap.driver.IRasterBuffer#assignBand(int, org.gvsig.fmap.dataaccess.buffer.IBand)
557
     */
558
        public void assignBand(int iBand, Band data) throws BandNotCompatibleException {
559
                if(        data.getDataType() != getDataType())
560
                        throw new BandNotCompatibleException("Data type not compatible");
561
                if(!(data instanceof MemoryBand))
562
                        throw new BandNotCompatibleException("buffer type not compatible");
563
                if(        data.getHeight() != getHeight() || data.getWidth() != getWidth())
564
                        throw new BandNotCompatibleException("Bands with diferents sizes");
565
                
566
                if(iBand < 0 || !(data instanceof MemoryBand))
567
                    return;
568
            if(iBand > getBandCount())
569
                    bands.add((MemoryBand)data);
570
            bands.add(iBand, (MemoryBand)data);
571
            
572
            setBandCount(bands.size());
573
        }
574
    
575
    /*
576
     *  (non-Javadoc)
577
     * @see org.gvsig.fmap.driver.IRasterBuffer#cloneBuffer()
578
     */
579
    public Buffer cloneBuffer(){
580
            RasterMemoryBuffer rmb = new RasterMemoryBuffer(getDataType(), getWidth(), getHeight(), getBandCount(), false);
581
            for(int iBand = 0; iBand < getBandCount(); iBand ++) {
582
                    MemoryBand band = bands.get(iBand).clone();
583
                    try {
584
                            rmb.removeBand(iBand);
585
                                rmb.assignBand(iBand, band);
586
                        } catch (BandNotCompatibleException e) {
587
                        }
588
            }
589
            return rmb;
590
    }
591
    
592
    /*
593
     * (non-Javadoc)
594
     * @see org.gvsig.fmap.driver.IRasterBuffer#swapBands(int, int)
595
     */
596
    public void swapBands(int band1, int band2) throws WrongParameterException, IOException {
597
            super.swapBands(band1, band2);
598
            
599
            int[] bandPosition = new int[getBandCount()];
600
                for (int i = 0; i < bandPosition.length; i++) {
601
                        if(i == band1) 
602
                                bandPosition[i] = band2;
603
                        else if(i == band2) 
604
                                bandPosition[i] = band1;
605
                        else
606
                                bandPosition[i] = i;
607
                }
608
                swapBands(bandPosition);
609
    }
610
        
611
    /**
612
     * Convierte un tipo de dato a cadena
613
     * @param type Tipo de dato
614
     * @return cadena  que representa el tipo de dato
615
     */
616
    public static String typesToString(int type) {
617
        switch (type) {
618
        case RasterBuffer.TYPE_BYTE:
619
            return new String("Byte");
620

    
621
        case RasterBuffer.TYPE_DOUBLE:
622
            return new String("Double");
623

    
624
        case RasterBuffer.TYPE_FLOAT:
625
            return new String("Float");
626

    
627
        case RasterBuffer.TYPE_INT:
628
                return new String("Integer");
629
                
630
        case RasterBuffer.TYPE_USHORT:
631
        case RasterBuffer.TYPE_SHORT:
632
            return new String("Short");
633
        }
634

    
635
        return null;
636
    }
637
    
638
        /*
639
         *  (non-Javadoc)
640
         * @see org.gvsig.fmap.driver.IRasterBuffer#assignBandToNotValid(int)
641
         */
642
    public void assignBandToNotValid(int iBand) {
643
            if(iBand < 0)
644
                    return;
645
            if(getBandCount() > iBand) 
646
                    bands.add((MemoryBand)createBand(getNotValidValue()));
647
            bands.add(iBand, (MemoryBand)createBand(getNotValidValue()));
648
    }
649
    
650
    /*
651
     * (non-Javadoc)
652
     * @see org.gvsig.raster.buffer.IRasterBuffer#removeBand(int)
653
     */
654
    public void removeBand(int iBand) {
655
            if(iBand < 0 || iBand >= getBandCount())
656
                    return;
657
            bands.remove(iBand);
658
            bandLessLess();
659
    }
660
    
661
    /*
662
     * (non-Javadoc)
663
     * @see org.gvsig.raster.buffer.IRasterBuffer#addBand()
664
     */
665
    public Band addBand(int pos) {
666
            bandPlusPlus();
667
            switch(getDataType()){
668
            case RasterBuffer.TYPE_BYTE:ByteBand bb = new ByteBand(getWidth(), getHeight(), pos, true);
669
                                                                    bands.add(pos, bb);
670
                                                                    return bb;
671
            case RasterBuffer.TYPE_SHORT:ShortBand sb = new ShortBand(getWidth(), getHeight(), pos, true);
672
                                                                        bands.add(pos, sb);
673
                                                                        return sb;
674
            case RasterBuffer.TYPE_INT:        IntBand ib = new IntBand(getWidth(), getHeight(), pos, true);
675
                                                                    bands.add(pos, ib);
676
                                                                        return ib;
677
            case RasterBuffer.TYPE_FLOAT:        FloatBand fb = new FloatBand(getWidth(), getHeight(), pos, true);
678
                                                                            bands.add(pos, fb);
679
                                                                                return fb;
680
            case RasterBuffer.TYPE_DOUBLE:        DoubleBand db = new DoubleBand(getWidth(), getHeight(), pos, true);
681
                                                                            bands.add(pos, db);
682
                                                                                return db;
683
            }
684
            return null;
685
    }
686

    
687
        /**
688
         * Libera el buffer de memoria
689
         */
690
        public void free() {
691
                for (int i = 0; i < bands.size(); i++) 
692
                        bands.get(i).free();
693
        }
694
}