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 / memory / TestAddBand.java @ 992

History | View | Annotate | Download (2.82 KB)

1
package org.gvsig.raster.cache.buffer.impl.memory;
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.io.GdalRead;
12
import org.gvsig.raster.cache.buffer.impl.io.GdalWrite;
13
import org.gvsig.raster.cache.buffer.impl.memory.RasterMemoryBuffer;
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("MemoryBuffer TestAddBand running...");
34
        }
35
        
36
        public void testStack() {
37
                long t1 = System.currentTimeMillis();
38
                GdalRead input = null;
39
                GdalWrite out = null;
40
                try {
41
        
42
                        //Read input data
43
                        input = new GdalRead(rasterIn);
44
                        Object dataIn = input.readBlock(0, 0, input.getWidth(), input.getHeight());
45
                        
46
                        //Create Buffer
47
                        Buffer buf = new RasterMemoryBuffer(input.getRasterBufTypeFromGdalType(input.getDataType()), 
48
                                                                                                                input.getWidth(), 
49
                                                                                                                input.getHeight(),
50
                                                                                                                input.getBandCount(),
51
                                                                                                                true);
52
                        
53
                        //Set data to buffer
54
                        setLines(dataIn, buf);
55
                        buf.addBand(1);
56
                        
57
                        //Write output file 
58
                        out = new GdalWrite(rasterOut, 4, input.getDataType(), input.getWidth(), input.getHeight(), GdalWrite.COLOR_INTERP_GRAY);
59
                        out.writeBands(buf.getBands());
60
                        out.close();        
61
                        
62
                        Band band = buf.getBand(1);
63
                        for (int i = 0; i < band.getHeight(); i++) {
64
                                for (int j = 0; j < band.getWidth(); j++) {
65
                                        byte b = band.getElemByte(i, j);
66
                                        assertEquals(0, b);
67
                                }
68
                        }
69
                        
70
                        buf.free();
71
                } catch (GdalException e) {
72
                        e.printStackTrace();
73
                } catch (IOException e) {
74
                        e.printStackTrace();
75
                } catch (InterruptedException e) {
76
                        e.printStackTrace();
77
                }  catch (OperationNotSupportedException e) {
78
                        e.printStackTrace();
79
                }
80
                long t2 = System.currentTimeMillis();
81
                System.out.println("Tiempo MemoryBuffer TestAddBand: " + (t2 - t1) + " milisegundos");
82
        }
83
        
84
        private void setLines(Object dataIn, Buffer buf) throws OperationNotSupportedException {
85
                if(dataIn instanceof byte[][][]) {
86
                    byte[][][] d = (byte[][][])dataIn;
87
                    for (int iBand = 0; iBand < d.length; iBand++) {
88
                                for (int row = 0; row < d[iBand].length; row++) {
89
                                        buf.setLineInBandByte(d[iBand][row], row, iBand);
90
                                }
91
                        }
92
            }
93
        }
94
        
95
}