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 / stripecache / horizontal / RasterCacheBuffer.java @ 995

History | View | Annotate | Download (28.8 KB)

1
package org.gvsig.raster.cache.buffer.impl.stripecache.horizontal;
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.BufferNoData;
9
import org.gvsig.raster.cache.buffer.exception.BandNotCompatibleException;
10
import org.gvsig.raster.cache.buffer.exception.InvalidPageNumberException;
11
import org.gvsig.raster.cache.buffer.exception.OperationNotSupportedException;
12
import org.gvsig.raster.cache.buffer.exception.WrongParameterException;
13
import org.gvsig.raster.cache.buffer.impl.RasterBuffer;
14
import org.gvsig.raster.cache.buffer.impl.stripecache.CacheBand;
15
import org.gvsig.raster.cache.buffer.impl.stripecache.LRUAlgorithm;
16

    
17
/**
18
 * Esta clase representa un buffer de datos cacheado. El estar en cache significa que
19
 * solo una parte de los datos estar?n en memoria y que gestiona, dependiendo de las
20
 * peticiones, que partes hay que cargar a memoria y en que momento. Para esto el buffer
21
 * no se llena de golpe sino que utiliza una clase que sirve los datos desde la fuente. 
22
 * Esta clase servidora de datos debe implementar el interfaz ICacheDatasetSourcepara 
23
 * servir datos de la forma requerida.
24
 *   
25
 * @author Nacho Brodin (nachobrodin@gmail.com)
26
 *
27
 */
28
public class RasterCacheBuffer extends RasterBuffer {
29

    
30
        private CacheHorzImpl                           cache     = null;
31
        private LRUAlgorithm              lru       = null;
32
        private CacheBandListHorz         bands     = null;
33
        private int                       lastLine  = -1;
34
         
35
    /**
36
     * Constructor. Asigna las variables de inicializaci?n y crea la estructura de 
37
     * la cach? con los datos pasados.
38
     * @param dataType Tipo de dato del buffer
39
     * @param width Ancho del buffer
40
     * @param height Alto del buffer
41
     * @param nBands N?mero de bandas del buffer
42
     */
43
        public RasterCacheBuffer(int dataType, int width, int height, int nBands) {
44
                super(0, 0, width, height, dataType, nBands);
45
                cache = new CacheHorzImpl(nBands, dataType, width, height);
46
                lru = new LRUAlgorithm(cache);
47
        
48
        bands = new CacheBandListHorz();
49
        for (int i = 0; i < nBands; i++) {
50
                switch(dataType) {
51
                case RasterBuffer.TYPE_BYTE: bands.add(new ByteBand(cache, lru, i)); break;
52
                case RasterBuffer.TYPE_SHORT: bands.add(new ShortBand(cache, lru, i)); break;
53
                case RasterBuffer.TYPE_INT: bands.add(new IntBand(cache, lru, i)); break;
54
                case RasterBuffer.TYPE_FLOAT: bands.add(new FloatBand(cache, lru, i)); break;
55
                case RasterBuffer.TYPE_DOUBLE: bands.add(new DoubleBand(cache, lru, i)); break;
56
                }
57
                }
58
        }
59
                
60
        /**
61
         * Borra la cach? cuando se elimina el objeto RasterCache ya que
62
         * los trozos ya no pueden ser referenciados.
63
         */
64
        protected void finalize() throws Throwable {
65
                free();
66
        }
67
        
68
        /*
69
     * (non-Javadoc)
70
     * @see org.gvsig.raster.driver.IRasterBuffer#isBandSwitchable()
71
     */
72
    public boolean isBandSwitchable(){
73
            return true;
74
    }
75
    
76
        /**
77
         * Obtiene la cach?
78
         * @return
79
         */
80
        public CacheHorzImpl getCache() {
81
                return cache;
82
        }
83
        
84
        /**
85
         * Asigna la cache
86
         * @param cache
87
         */
88
        public void setCache(CacheHorzImpl cache) {
89
                this.cache = cache;
90
                this.lru.setCache(cache);
91
        }
92
        
93
        /*
94
     * (non-Javadoc)
95
     * @see org.fv.raster.buffer.IRasterBuffer#isWritable()
96
     */
97
    public boolean isWritable() {
98
            return true;
99
    }
100
    
101
    private boolean exists(int line) {
102
        // Store the last line checked so we don't check it again for each
103
        // pixel of the same line
104
        if (line != lastLine) {
105
            try {
106
                lru.cacheAccess(line, false);
107
                this.lastLine = line;
108
            } catch (InvalidPageNumberException e) {
109
                return false;
110
            } catch (InterruptedException e) {
111
                                return false;
112
                        }
113
        }
114
        return true;
115
    }
116
        
117
        /***********************************************/
118
        
119
        /*
120
         * (non-Javadoc)
121
         * @see org.gvsig.raster.buffer.IRasterBuffer#getLineByte(int)
122
         */
123
    public byte[][] getLineByte(int line) {
124
            if (exists(line)) {
125
                    return cache.getAccessPage().getLineByte(
126
                            (line & cache.getOffset()));
127
            }
128
            return null;
129
    }
130

    
131
        /*
132
         * (non-Javadoc)
133
         * @see org.gvsig.raster.buffer.IRasterBuffer#getLineShort(int)
134
         */
135
        public short[][] getLineShort(int line) {
136
                if (exists(line)) {
137
            return cache.getAccessPage().getLineShort(
138
                (line & cache.getOffset()));
139
        }
140
        return null;
141
        }
142

    
143
        /*
144
         * (non-Javadoc)
145
         * @see org.gvsig.raster.buffer.IRasterBuffer#getLineInt(int)
146
         */
147
        public int[][] getLineInt(int line) {
148
                if (exists(line)) {
149
            return cache.getAccessPage().getLineInt(
150
                            (line & cache.getOffset()));
151
        }
152
        return null;
153
        }
154

    
155
        /*
156
         * (non-Javadoc)
157
         * @see org.gvsig.raster.buffer.IRasterBuffer#getLineFloat(int)
158
         */
159
        public float[][] getLineFloat(int line) {
160
                if (exists(line)) {
161
            return cache.getAccessPage().getLineFloat(
162
                (line & cache.getOffset()));
163
        }
164
        return null;
165
        }
166

    
167
        /*
168
         * (non-Javadoc)
169
         * @see org.gvsig.raster.buffer.IRasterBuffer#getLineDouble(int)
170
         */
171
        public double[][] getLineDouble(int line) {
172
                if (exists(line)) {
173
            return cache.getAccessPage().getLineDouble(
174
                (line & cache.getOffset()));
175
        }
176
        return null;
177
        }
178

    
179
        //***********************************************
180
        
181
        /*
182
         * (non-Javadoc)
183
         * @see org.gvsig.raster.buffer.IRasterBuffer#setLineByte(byte[][], int)
184
         */
185
        public void setLineByte(byte[][] data, int line) {
186
                if (exists(line)) {
187
            cache.getAccessPage().setLineByte(
188
                            data, (line & cache.getOffset()));
189
        }
190
        }
191

    
192
        /*
193
         * (non-Javadoc)
194
         * @see org.gvsig.raster.buffer.IRasterBuffer#setLineShort(short[][], int)
195
         */
196
        public void setLineShort(short[][] data, int line) {
197
                if (exists(line)) {
198
            cache.getAccessPage().setLineShort(
199
                            data, (line & cache.getOffset()));
200
        }
201
        }
202

    
203
        /*
204
         * (non-Javadoc)
205
         * @see org.gvsig.raster.buffer.IRasterBuffer#setLineInt(int[][], int)
206
         */
207
        public void setLineInt(int[][] data, int line) {
208
                if (exists(line)) {
209
            cache.getAccessPage().setLineInt(
210
                            data, (line & cache.getOffset()));
211
        }
212
        }
213

    
214
        /*
215
         * (non-Javadoc)
216
         * @see org.gvsig.raster.buffer.IRasterBuffer#setLineFloat(float[][], int)
217
         */
218
        public void setLineFloat(float[][] data, int line) {
219
                if (exists(line)) {
220
            cache.getAccessPage().setLineFloat(
221
                            data, (line & cache.getOffset()));
222
        }
223
        }
224

    
225
        /*
226
         * (non-Javadoc)
227
         * @see org.gvsig.raster.buffer.IRasterBuffer#setLineDouble(double[][], int)
228
         */
229
        public void setLineDouble(double[][] data, int line) {
230
                if (exists(line)) {
231
            cache.getAccessPage().setLineDouble(
232
                            data, (line & cache.getOffset()));
233
        }
234
        }
235

    
236
        //*********************************************************
237
        
238
        /*
239
         * (non-Javadoc)
240
         * @see org.gvsig.raster.buffer.IRasterBuffer#getLineFromBandByte(int, int)
241
         */
242
        public byte[] getLineFromBandByte(int line, int band) {
243
                if (exists(line)) {
244
            return cache.getAccessPage().getLineFromBandByte(
245
                (line & cache.getOffset()), band);
246
        }
247
        return null;
248
        }
249

    
250
        /*
251
         * (non-Javadoc)
252
         * @see org.gvsig.raster.buffer.IRasterBuffer#getLineFromBandShort(int, int)
253
         */
254
        public short[] getLineFromBandShort(int line, int band) {
255
                if (exists(line)) {
256
            return cache.getAccessPage().getLineFromBandShort(
257
                (line & cache.getOffset()), band);
258
        }
259
        return null;
260
        }
261

    
262
        /*
263
         * (non-Javadoc)
264
         * @see org.gvsig.raster.buffer.IRasterBuffer#getLineFromBandInt(int, int)
265
         */
266
        public int[] getLineFromBandInt(int line, int band) {
267
                if (exists(line)) {
268
            return cache.getAccessPage().getLineFromBandInt(
269
                (line & cache.getOffset()), band);
270
        }
271
        return null;
272
        }
273

    
274
        /*
275
         * (non-Javadoc)
276
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#getLineFromBandFloat(int, int)
277
         */
278
        public float[] getLineFromBandFloat(int line, int band) {
279
        if (exists(line)) {
280
            return cache.getAccessPage().getLineFromBandFloat(
281
                (line & cache.getOffset()), band);
282
        }
283
        return null;
284
        }
285

    
286
        /*
287
         * (non-Javadoc)
288
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#getLineFromBandDouble(int, int)
289
         */
290
        public double[] getLineFromBandDouble(int line, int band) {
291
        if (exists(line)) {
292
            return cache.getAccessPage().getLineFromBandDouble(
293
                (line & cache.getOffset()), band);
294
        }
295
        return null;
296
        }
297

    
298
        //*********************************************************
299
        
300
        /*
301
         * (non-Javadoc)
302
         * @see org.gvsig.raster.buffer.IRasterBuffer#setLineInBandByte(byte[], int, int)
303
         */
304
        public void setLineInBandByte(byte[] data, int line, int band) {
305
        if (exists(line)) {
306
            cache.getAccessPage().setLineInBandByte(data,
307
                (line & cache.getOffset()), band);
308
        }
309
        }
310

    
311
        /*
312
         * (non-Javadoc)
313
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#setLineInBandShort(short[], int, int)
314
         */
315
        public void setLineInBandShort(short[] data, int line, int band) {
316
        if (exists(line)) {
317
            cache.getAccessPage().setLineInBandShort(data,
318
                (line & cache.getOffset()), band);
319
        }
320
        }
321

    
322
        /*
323
         * (non-Javadoc)
324
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#setLineInBandInt(int[], int, int)
325
         */
326
        public void setLineInBandInt(int[] data, int line, int band) {
327
        if (exists(line)) {
328
            cache.getAccessPage().setLineInBandInt(data,
329
                (line & cache.getOffset()), band);
330
        }
331
        }
332

    
333
        /*
334
         * (non-Javadoc)
335
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#setLineInBandFloat(float[], int, int)
336
         */
337
        public void setLineInBandFloat(float[] data, int line, int band) {
338
        if (exists(line)) {
339
            cache.getAccessPage().setLineInBandFloat(data,
340
                (line & cache.getOffset()), band);
341
        }
342
        }
343

    
344
        /*
345
         * (non-Javadoc)
346
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#setLineInBandDouble(double[], int, int)
347
         */
348
        public void setLineInBandDouble(double[] data, int line, int band) {
349
        if (exists(line)) {
350
            cache.getAccessPage().setLineInBandDouble(data,
351
                (line & cache.getOffset()), band);
352
        }
353
        }
354

    
355
        //*********************************************************
356
        
357
        /*
358
         * (non-Javadoc)
359
         * @see org.gvsig.raster.buffer.IRasterBuffer#getElemByte(int, int, int)
360
         */
361
        public byte getElemByte(int line, int col, int band) {
362
        if (exists(line)) {
363
            return cache.getAccessPage().getElemByte(
364
                (line & cache.getOffset()), col, band);
365
                }
366
        return getNoDataValue().isDefined() ? getNoDataValue().getValue().byteValue() : BufferNoData.defaultByteNoDataValue; // No leemos el dato
367
        }
368

    
369
        /*
370
         * (non-Javadoc)
371
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#getElemShort(int, int, int)
372
         */
373
        public short getElemShort(int line, int col, int band) {
374
        if (exists(line)) {
375
            return cache.getAccessPage().getElemShort(
376
                (line & cache.getOffset()), col, band);
377
                }
378
        return getNoDataValue().isDefined() ? getNoDataValue().getValue().shortValue() : BufferNoData.defaultShortNoDataValue; // No leemos el dato
379
        }
380

    
381
        /*
382
         * (non-Javadoc)
383
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#getElemInt(int, int, int)
384
         */
385
        public int getElemInt(int line, int col, int band) {
386
        if (exists(line)) {
387
            return cache.getAccessPage().getElemInt((line & cache.getOffset()),
388
                col, band);
389
                }
390
        return getNoDataValue().isDefined() ? getNoDataValue().getValue().intValue() : BufferNoData.defaultIntegerNoDataValue; // No leemos el dato
391
        }
392

    
393
        /*
394
         * (non-Javadoc)
395
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#getElemFloat(int, int, int)
396
         */
397
        public float getElemFloat(int line, int col, int band) {
398
        if (exists(line)) {
399
            return cache.getAccessPage().getElemFloat(
400
                (line & cache.getOffset()), col, band);
401
                }
402
        return getNoDataValue().isDefined() ? getNoDataValue().getValue().floatValue() : BufferNoData.defaultFloatNoDataValue; // No leemos el dato
403
        }
404

    
405
        /*
406
         * (non-Javadoc)
407
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#getElemDouble(int, int, int)
408
         */
409
        public double getElemDouble(int line, int col, int band) {
410
        if (exists(line)) {
411
            return cache.getAccessPage().getElemDouble(
412
                (line & cache.getOffset()), col, band);
413
                }
414
        return getNoDataValue().isDefined() ? getNoDataValue().getValue().doubleValue() : BufferNoData.defaultDoubleNoDataValue; // No leemos el dato
415
        }
416

    
417
        //*********************************************************
418
        
419
        /*
420
         * (non-Javadoc)
421
         * @see org.gvsig.raster.buffer.IRasterBuffer#setElem(int, int, int, byte)
422
         */
423
        public void setElem(int line, int col, int band, byte data) {
424
                if (exists(line)) {
425
                        cache.getAccessPage().setElem(
426
                                        (line & cache.getOffset()), col, band, data);
427
                }
428
        }
429

    
430
        /*
431
         * (non-Javadoc)
432
         * @see org.gvsig.raster.buffer.IRasterBuffer#setElem(int, int, int, short)
433
         */
434
        public void setElem(int line, int col, int band, short data) {
435
                if (exists(line)) {
436
                        cache.getAccessPage().setElem(
437
                                        (line & cache.getOffset()), col, band, data);
438
                }
439
        }
440

    
441
        /*
442
         * (non-Javadoc)
443
         * @see org.gvsig.raster.buffer.IRasterBuffer#setElem(int, int, int, int)
444
         */
445
        public void setElem(int line, int col, int band, int data) {
446
                if (exists(line)) {
447
                        cache.getAccessPage().setElem(
448
                                        (line & cache.getOffset()), col, band, data);
449
                }
450
        }
451

    
452
        /*
453
         * (non-Javadoc)
454
         * @see org.gvsig.raster.buffer.IRasterBuffer#setElem(int, int, int, float)
455
         */
456
        public void setElem(int line, int col, int band, float data) {
457
                if (exists(line)) {
458
                        cache.getAccessPage().setElem(
459
                                        (line & cache.getOffset()), col, band, data);
460
                }
461
        }
462

    
463
        /*
464
         * (non-Javadoc)
465
         * @see org.gvsig.raster.buffer.IRasterBuffer#setElem(int, int, int, double)
466
         */
467
        public void setElem(int line, int col, int band, double data) {
468
                if (exists(line)) {
469
                        cache.getAccessPage().setElem(
470
                                        (line & cache.getOffset()), col, band, data);
471
                }
472
        }
473
        
474
        //*********************************************************
475
        
476
        /*
477
         * (non-Javadoc)
478
         * @see org.gvsig.raster.buffer.IRasterBuffer#getElemByte(int, int, byte[])
479
         */
480
        public void getElemByte(int line, int col, byte[] data) {
481
                if(exists(line)) {
482
                        cache.getAccessPage().getElemByte(
483
                                        (line & cache.getOffset()), col, data);
484
                        return;
485
                }
486
                if(getNoDataValue().isDefined()) {
487
                        for (int iBand = 0; iBand < data.length; iBand++)
488
                    data[iBand] = getNoDataValue().getValue().byteValue();
489
                } else {
490
                        for (int iBand = 0; iBand < data.length; iBand++)
491
                    data[iBand] = BufferNoData.defaultByteNoDataValue;
492
                }
493
        }
494

    
495
        /*
496
         * (non-Javadoc)
497
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#getElemShort(int, int, short[])
498
         */
499
        public void getElemShort(int line, int col, short[] data) {
500
                if(exists(line)) {
501
                        cache.getAccessPage().getElemShort(
502
                                        (line & cache.getOffset()), col, data);
503
                        return;
504
                }
505
                if(getNoDataValue().isDefined()) {
506
                        for (int iBand = 0; iBand < data.length; iBand++)
507
                    data[iBand] = getNoDataValue().getValue().shortValue();
508
                } else {
509
                        for (int iBand = 0; iBand < data.length; iBand++)
510
                    data[iBand] = BufferNoData.defaultShortNoDataValue;
511
                }
512
        }
513

    
514
        /*
515
         * (non-Javadoc)
516
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#getElemInt(int, int, int[])
517
         */
518
        public void getElemInt(int line, int col, int[] data) {
519
                if(exists(line)) {
520
                        cache.getAccessPage().getElemInt(
521
                                        (line & cache.getOffset()), col, data);
522
                        return;
523
                }
524
                if(getNoDataValue().isDefined()) {
525
                        for (int iBand = 0; iBand < data.length; iBand++)
526
                    data[iBand] = getNoDataValue().getValue().intValue();
527
                } else {
528
                        for (int iBand = 0; iBand < data.length; iBand++)
529
                    data[iBand] = BufferNoData.defaultIntegerNoDataValue;
530
                }
531
        }
532

    
533
        /*
534
         * (non-Javadoc)
535
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#getElemFloat(int, int, float[])
536
         */
537
        public void getElemFloat(int line, int col, float[] data) {
538
                if(exists(line)) {
539
                        cache.getAccessPage().getElemFloat(
540
                                        (line & cache.getOffset()), col, data);
541
                        return;
542
                }
543
                if(getNoDataValue().isDefined()) {
544
                        for (int iBand = 0; iBand < data.length; iBand++)
545
                    data[iBand] = getNoDataValue().getValue().floatValue();
546
                } else {
547
                        for (int iBand = 0; iBand < data.length; iBand++)
548
                    data[iBand] = BufferNoData.defaultFloatNoDataValue;
549
                }
550
        }
551

    
552
        /*
553
         * (non-Javadoc)
554
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#getElemDouble(int, int, double[])
555
         */
556
        public void getElemDouble(int line, int col, double[] data) {
557
                if(exists(line)) {
558
                        cache.getAccessPage().getElemDouble(
559
                                        (line & cache.getOffset()), col, data);
560
                        return;
561
                }
562
                if(getNoDataValue().isDefined()) {
563
                        for (int iBand = 0; iBand < data.length; iBand++)
564
                    data[iBand] = getNoDataValue().getValue().doubleValue();
565
                } else {
566
                        for (int iBand = 0; iBand < data.length; iBand++)
567
                    data[iBand] = BufferNoData.defaultDoubleNoDataValue;
568
                }
569
        }
570
        
571
        //*********************************************************
572
        
573
        /*
574
         * (non-Javadoc)
575
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#setElemByte(int, int, byte[])
576
         */
577
        public void setElemByte(int line, int col, byte[] data) {
578
        if (exists(line)) {
579
            cache.getAccessPage().setElemByte(
580
                            (line & cache.getOffset()), col, data);
581
        }
582
    }
583

    
584
        /*
585
         * (non-Javadoc)
586
         * @see org.gvsig.raster.cache.buffer.IRasterBuffer#setElemShort(int, int, short[])
587
         */
588
    public void setElemShort(int line, int col, short[] data) {
589
        if (exists(line)) {
590
            cache.getAccessPage().setElemShort(
591
                            (line & cache.getOffset()), col, data);
592
        }
593
    }
594

    
595
    /*
596
     * (non-Javadoc)
597
     * @see org.gvsig.raster.cache.buffer.IRasterBuffer#setElemInt(int, int, int[])
598
     */
599
    public void setElemInt(int line, int col, int[] data) {
600
        if (exists(line)) {
601
            cache.getAccessPage().setElemInt(
602
                            (line & cache.getOffset()), col, data);
603
        }
604
    }
605

    
606
    /*
607
     * (non-Javadoc)
608
     * @see org.gvsig.raster.cache.buffer.IRasterBuffer#setElemFloat(int, int, float[])
609
     */
610
    public void setElemFloat(int line, int col, float[] data) {
611
        if (exists(line)) {
612
            cache.getAccessPage().setElemFloat(
613
                            (line & cache.getOffset()), col, data);
614
        }
615
    }
616

    
617
    /*
618
     * (non-Javadoc)
619
     * @see org.gvsig.raster.cache.buffer.IRasterBuffer#setElemDouble(int, int, double[])
620
     */
621
    public void setElemDouble(int line, int col, double[] data) {
622
        if (exists(line)) {
623
            cache.getAccessPage().setElemDouble(
624
                            (line & cache.getOffset()), col, data);
625
        }
626
    }
627

    
628
        //*********************************************************
629
        
630
        public void assign(int band, byte value) {
631
                for(int line = 0; line < getHeight(); line ++){
632
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
633
                    for(int col = 0; col < getWidth(); col ++){
634
                            try {
635
                                    if(beginLine){
636
                                            lru.cacheAccess(line, false);
637
                                            beginLine = false;
638
                                    }
639
                            } catch (InvalidPageNumberException e) {return;
640
                            } catch (InterruptedException e) {return;}
641
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
642
                    }
643
                }
644
        }
645

    
646
        public void assign(int band, short value) {
647
                for(int line = 0; line < getHeight(); line ++){
648
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
649
                    for(int col = 0; col < getWidth(); col ++){
650
                            try {
651
                                    if(beginLine){
652
                                            lru.cacheAccess(line, false);
653
                                            beginLine = false;
654
                                    }
655
                            } catch (InvalidPageNumberException e) {return;
656
                            } catch (InterruptedException e) {return;}
657
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
658
                    }
659
                }                
660
        }
661

    
662
        public void assign(int band, int value) {
663
                for(int line = 0; line < getHeight(); line ++){
664
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
665
                    for(int col = 0; col < getWidth(); col ++){
666
                            try {
667
                                    if(beginLine){
668
                                            lru.cacheAccess(line, false);
669
                                            beginLine = false;
670
                                    }
671
                            } catch (InvalidPageNumberException e) {return;
672
                            } catch (InterruptedException e) {return;}
673
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
674
                    }
675
                }        
676
        }
677

    
678
        public void assign(int band, float value) {
679
                for(int line = 0; line < getHeight(); line ++){
680
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
681
                    for(int col = 0; col < getWidth(); col ++){
682
                            try {
683
                                    if(beginLine){
684
                                            lru.cacheAccess(line, false);
685
                                            beginLine = false;
686
                                    }
687
                            } catch (InvalidPageNumberException e) {return;
688
                            } catch (InterruptedException e) {return;}
689
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
690
                    }
691
                }        
692
        }
693

    
694
        public void assign(int band, double value) {
695
                for(int line = 0; line < getHeight(); line ++){
696
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
697
                    for(int col = 0; col < getWidth(); col ++){
698
                            try {
699
                                    if(beginLine){
700
                                            lru.cacheAccess(line, false);
701
                                            beginLine = false;
702
                                    }
703
                            } catch (InvalidPageNumberException e) {return;
704
                            } catch (InterruptedException e) {return;}
705
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
706
                    }
707
                }
708
        }
709
        
710
        //**************************************
711
    //**********BANDS OPERATIONS************
712
    //**************************************
713
    
714
        /*
715
         * (non-Javadoc)
716
         * @see org.gvsig.raster.buffer.IRasterBuffer#removeBand(int)
717
         */
718
        public void removeBand(int iBand) throws IOException {
719
            if(iBand < 0 || iBand >= getBandCount())
720
                    return;
721
                cache.removeBand(iBand);
722
                bands.remove(iBand);
723
                bandLessLess();
724
        }
725
        
726
        /*
727
         * (non-Javadoc)
728
         * @see org.gvsig.raster.buffer.IRasterBuffer#addBand(int)
729
         */
730
        public Band addBand(int pos) throws IOException {
731
                cache.addBand(pos);
732
                CacheBand cb = bands.add(pos, cache, lru, getDataType());
733
                bandPlusPlus();
734
                return cb;
735
        }
736
        
737
        /*
738
         * (non-Javadoc)
739
         * @see org.gvsig.raster.buffer.IRasterBuffer#getBands()
740
         */
741
        public Band[] getBands() {
742
                Band[] b = new Band[bands.size()];
743
                for (int i = 0; i < bands.size(); i++) 
744
                        b[i] = bands.get(i);
745
                return b;
746
        }
747
        
748
    /*
749
     * (non-Javadoc)
750
     * @see org.fv.raster.buffer.IRasterBuffer#getBandList()
751
     */
752
    public ArrayList<Band> getBandList() {
753
            ArrayList<Band> result = new ArrayList<Band>();
754
            for (int i = 0; i < bands.size(); i++) 
755
                        result.add(bands.get(i));
756
            return result;
757
    }
758
        
759
        /**
760
         * Set the band to the selected position. To assign a cache band firstly it will 
761
         * check if the band to assign is compatible, then it will
762
         * load in each page in disk the page indicated by the target object. Finally it will 
763
         * assign the Cache object to the new band.
764
         * @throws IOException 
765
         */
766
        public void assignBand(int pos, Band band) throws IOException, BandNotCompatibleException {
767
                if(        band.getDataType() != getDataType())
768
                        throw new BandNotCompatibleException("Data type not compatible");
769
                if(getBand(0) != null && band.getClass() != getBand(0).getClass())
770
                        throw new BandNotCompatibleException("buffer type not compatible");
771
                if(        band.getHeight() != cache.getDataSourceHeight() || band.getWidth() != cache.getDataSourceWidth())
772
                                throw new BandNotCompatibleException("Bands with diferents sizes");
773
                
774
                cache.addBand(pos, band);
775
                bands.add(pos, cache, lru, getDataType());
776
                bandPlusPlus();
777
        }
778
        
779
        /*
780
         * (non-Javadoc)
781
         * @see org.gvsig.raster.driver.IRasterBuffer#getBandByRef(int)
782
         */
783
        public Band getBand(int band) {
784
                if(bands.size() == 0 || band < 0 || band > (bands.size() - 1))
785
                        return null;
786
                return bands.get(band);
787
        }
788
        
789
        /*
790
         * (non-Javadoc)
791
         * @see org.gvsig.raster.driver.IRasterBuffer#getBand(int)
792
         */
793
        public Band getBandCopy(int band) {
794
                switch (getDataType()) {
795
                case RasterBuffer.TYPE_BYTE:
796
                        return (ByteBand)((ByteBand)bands.get(band)).clone();
797
                case RasterBuffer.TYPE_SHORT:
798
                        return (ShortBand)((ShortBand)bands.get(band)).clone();
799
                case RasterBuffer.TYPE_INT:
800
                        return (IntBand)((IntBand)bands.get(band)).clone();
801
                case RasterBuffer.TYPE_FLOAT:
802
                        return (FloatBand)((FloatBand)bands.get(band)).clone();
803
                case RasterBuffer.TYPE_DOUBLE:
804
                        return (DoubleBand)((DoubleBand)bands.get(band)).clone();
805
                }
806
                return null;
807
        }
808
        
809
        /*
810
     * (non-Javadoc)
811
     * @see org.gvsig.fmap.driver.IRasterBuffer#getBandBuffer(int)
812
     */
813
    public Buffer getBufferWithOneBand(int iBand) throws IOException {
814
            //Creamos un buffer del mismo tama?o que el original para que la estructura de cache
815
            //sea la misma. Luego eliminaremos las bandas para a?adirle solo una.
816
            RasterCacheBuffer rcb = new RasterCacheBuffer(getDataType(), getWidth(), getHeight(), getBandCount());
817
            for (int i = getBandCount() - 1; i >= 0; i--) 
818
                    rcb.removeBand(i);        
819
            try {
820
                        rcb.assignBand(0, getBand(iBand));
821
                } catch (BandNotCompatibleException e) {
822
                        //It can't be throwed
823
                }
824
            return rcb;
825
    }
826
    
827
    /*
828
     *  (non-Javadoc)
829
     * @see org.gvsig.fmap.dataaccess.buffer.RasterBuffer#switchBands(int[])
830
     */
831
    public void swapBands(int[] bandPosition) throws WrongParameterException, IOException {
832
            super.swapBands(bandPosition);
833
            cache.swapBands(bandPosition);
834
            bands.swapBands(bandPosition);
835
    }
836
    
837
    /*
838
     * (non-Javadoc)
839
     * @see org.gvsig.fmap.driver.IRasterBuffer#swapBands(int, int)
840
     */
841
        public void swapBands(int band1, int band2) throws WrongParameterException, IOException {
842
                if(band1 < 0 || band1 > getBandCount() - 1 || band2 < 0 || band2 > getBandCount() - 1)
843
                        return;
844
                
845
                int[] bandPosition = new int[getBandCount()];
846
                for (int i = 0; i < bandPosition.length; i++) {
847
                        if(i == band1) 
848
                                bandPosition[i] = band2;
849
                        else if(i == band2) 
850
                                bandPosition[i] = band1;
851
                        else
852
                                bandPosition[i] = i;
853
                }
854
                swapBands(bandPosition);
855
        }
856
        
857
        /*
858
     *  (non-Javadoc)
859
     * @see org.gvsig.fmap.driver.IRasterBuffer#copyBand(int, org.gvsig.fmap.dataaccess.buffer.IBand)
860
     */
861
        public void copyBand(int iBand, Band band) throws BandNotCompatibleException, OperationNotSupportedException {
862
                super.copyBand(iBand, band);
863
        }
864
        
865
    /*
866
     *  (non-Javadoc)
867
     * @see org.gvsig.fmap.driver.IRasterBuffer#replicateBand(int, int)
868
     */
869
        public void replicateBand(int orig, int dest) throws IOException {
870
                Band origBand = getBandCopy(orig);
871
                try {
872
                        assignBand(dest, origBand);
873
                } catch (BandNotCompatibleException e) {
874
                        //It can't be throwed
875
                }
876
        }  
877
        
878
        /**
879
         * Create a band with the current cache information.
880
         */
881
        public Band createBand(double defaultValue) {
882
                CacheHorzImpl clonCache = cache.cloneStructure();
883
                LRUAlgorithm lru = new LRUAlgorithm(clonCache);
884
                for (int i = getBandCount() - 1; i >= 0 ; i--) {
885
                        try {
886
                                clonCache.removeBand(i);
887
                        } catch (IOException e) {
888
                                return null;
889
                        }
890
                }
891
                clonCache.createPages(1);
892
                
893
                switch (getDataType()) {
894
                case RasterBuffer.TYPE_BYTE:        
895
                        ByteBand bb = new ByteBand(clonCache, lru, 0);
896
                        bb.assign((byte)defaultValue);
897
                        return bb;
898
                case RasterBuffer.TYPE_SHORT:
899
                        ShortBand sb = new ShortBand(clonCache, lru, 0);
900
                        sb.assign((short)defaultValue);
901
                        return sb;
902
                case RasterBuffer.TYPE_INT:
903
                        IntBand ib = new IntBand(clonCache, lru, 0);
904
                        ib.assign((int)defaultValue);
905
                        return ib;
906
                case RasterBuffer.TYPE_FLOAT:
907
                        FloatBand fb = new FloatBand(clonCache, lru, 0);
908
                        fb.assign((float)defaultValue);
909
                        return fb;
910
                case RasterBuffer.TYPE_DOUBLE:
911
                        DoubleBand db = new DoubleBand(clonCache, lru, 0);
912
                        db.assign(defaultValue);
913
                        return db;
914
                }
915
                return null;
916
        }
917
                
918
        /*
919
         *  (non-Javadoc)
920
         * @see org.gvsig.fmap.driver.IRasterBuffer#assignBandToNotValid(int)
921
         */
922
        public void assignBandToNotValid(int iBand) {
923
                switch (getDataType()) {
924
                case RasterBuffer.TYPE_BYTE:        
925
                        for (int row = 0; row < getHeight(); row++) 
926
                                for (int col = 0; col < getWidth(); col++) 
927
                                        setElem(row, col, iBand, (byte)notValidValue);
928
                case RasterBuffer.TYPE_SHORT:
929
                        for (int row = 0; row < getHeight(); row++) 
930
                                for (int col = 0; col < getWidth(); col++) 
931
                                        setElem(row, col, iBand, (short)notValidValue);
932
                case RasterBuffer.TYPE_INT:
933
                        for (int row = 0; row < getHeight(); row++) 
934
                                for (int col = 0; col < getWidth(); col++) 
935
                                        setElem(row, col, iBand, (int)notValidValue);
936
                case RasterBuffer.TYPE_FLOAT:
937
                        for (int row = 0; row < getHeight(); row++) 
938
                                for (int col = 0; col < getWidth(); col++) 
939
                                        setElem(row, col, iBand, (float)notValidValue);
940
                case RasterBuffer.TYPE_DOUBLE:
941
                        for (int row = 0; row < getHeight(); row++) 
942
                                for (int col = 0; col < getWidth(); col++) 
943
                                        setElem(row, col, iBand, (double)notValidValue);
944
                }
945
        }
946
        
947
        /*
948
         * (non-Javadoc)
949
         * @see org.gvsig.raster.dataset.IRasterBuffer#free()
950
         */
951
        public void free() throws IOException {
952
                cache.clearCache();
953
        }
954
        
955
        /*
956
     *  (non-Javadoc)
957
     * @see org.gvsig.fmap.driver.IRasterBuffer#cloneBuffer()
958
     */
959
    public Buffer cloneBuffer(){
960
            return null;
961
    }
962
}