Statistics
| Revision:

gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / src / test / java / org / gvsig / raster / cache / buffer / impl / stripecache / horizontal / TestAddBand.java @ 991

History | View | Annotate | Download (2.96 KB)

1
package org.gvsig.raster.cache.buffer.impl.stripecache.horizontal;
2

    
3
import java.io.IOException;
4

    
5
import junit.framework.TestCase;
6

    
7
import org.gvsig.jgdal.GdalException;
8
import org.gvsig.raster.cache.buffer.Band;
9
import org.gvsig.raster.cache.buffer.Buffer;
10
import org.gvsig.raster.cache.buffer.exception.OperationNotSupportedException;
11
import org.gvsig.raster.cache.buffer.impl.BufferCacheManagerImpl;
12
import org.gvsig.raster.cache.buffer.impl.io.GdalRead;
13
import org.gvsig.raster.cache.buffer.impl.io.GdalWrite;
14

    
15
/**
16
 * This test add a band in a raster of 3 bands. As a result of this test a raster
17
 * of four bands is created and saved in the hard disk. The value of additional band
18
 * is zero in all its elements. Finally it gets the band added and compare each values 
19
 * with 0. 
20
 *  
21
 * @author Nacho Brodin (nachobrodin@gmail.com)
22
 */
23
public class TestAddBand extends TestCase {
24
        private String rasterIn = "./src/test/resources/image/001m09_1_0.tif";
25
        private String rasterOut = "/tmp/out-4bands.tif";
26
        
27
        public void start() {
28
                this.setUp();
29
                this.testStack();
30
        }
31

    
32
        public void setUp() {
33
                System.err.println("StripeCache TestAddBand running...");
34
        }
35
        
36
        public void testStack() {
37
                long t1 = System.currentTimeMillis();
38
                GdalRead input = null;
39
                GdalWrite out = null;
40
                try {
41
                        //Reducimos el tama?o de la cache para una prueba con menos datos
42
                        BufferCacheManagerImpl.cacheSize = 2;
43
                        BufferCacheManagerImpl.pageSize = 0.2;
44
                        
45
                        //Read input data
46
                        input = new GdalRead(rasterIn);
47
                        Object dataIn = input.readBlock(0, 0, input.getWidth(), input.getHeight());
48
                        
49
                        //Create Buffer
50
                        Buffer buf = new RasterCacheBuffer(input.getRasterBufTypeFromGdalType(input.getDataType()), 
51
                                                                                                                input.getWidth(), 
52
                                                                                                                input.getHeight(),
53
                                                                                                                input.getBandCount());
54
                        
55
                        //Set data to buffer
56
                        setLines(dataIn, buf);
57
                        buf.addBand(1);
58
                        
59
                        //Write output file 
60
                        out = new GdalWrite(rasterOut, 4, input.getDataType(), input.getWidth(), input.getHeight(), GdalWrite.COLOR_INTERP_GRAY);
61
                        out.writeBands(buf.getBands());
62
                        out.close();        
63
                        
64
                        Band band = buf.getBand(1);
65
                        for (int i = 0; i < band.getHeight(); i++) {
66
                                for (int j = 0; j < band.getWidth(); j++) {
67
                                        byte b = band.getElemByte(i, j);
68
                                        assertEquals(0, b);
69
                                }
70
                        }
71
                        
72
                        buf.free();
73
                } catch (GdalException e) {
74
                        e.printStackTrace();
75
                } catch (IOException e) {
76
                        e.printStackTrace();
77
                } catch (InterruptedException e) {
78
                        e.printStackTrace();
79
                } catch (OperationNotSupportedException e) {
80
                        e.printStackTrace();
81
                }
82
                long t2 = System.currentTimeMillis();
83
                System.out.println("Tiempo StripeCache TestAddBand: " + (t2 - t1) + " milisegundos");
84
        }
85
        
86
        private void setLines(Object dataIn, Buffer buf) throws OperationNotSupportedException {
87
                if(dataIn instanceof byte[][][]) {
88
                    byte[][][] d = (byte[][][])dataIn;
89
                    for (int iBand = 0; iBand < d.length; iBand++) {
90
                                for (int row = 0; row < d[iBand].length; row++) {
91
                                        buf.setLineInBandByte(d[iBand][row], row, iBand);
92
                                }
93
                        }
94
            }
95
        }
96
        
97
}