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

History | View | Annotate | Download (3.31 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.BandNotCompatibleException;
11
import org.gvsig.raster.cache.buffer.exception.OperationNotSupportedException;
12
import org.gvsig.raster.cache.buffer.impl.io.GdalRead;
13
import org.gvsig.raster.cache.buffer.impl.io.GdalWrite;
14
import org.gvsig.raster.cache.buffer.impl.memory.RasterMemoryBuffer;
15

    
16
/**
17
 *  
18
 * @author Nacho Brodin (nachobrodin@gmail.com)
19
 */
20
public class TestAssignBand extends TestCase {
21
        private String rasterIn = "./src/test/resources/image/001m09_1_0.tif";
22
        private String rasterOut = "/tmp/out-4bands.tif";
23
        
24
        public void start() {
25
                this.setUp();
26
                this.testStack();
27
        }
28

    
29
        public void setUp() {
30
                System.err.println("MemoryBuffer TestAssignBand running...");
31
        }
32
        
33
        public void testStack() {
34
                long t1 = System.currentTimeMillis();
35
                GdalRead input1 = null;
36
                GdalRead input2 = null;
37
                GdalWrite out = null;
38
                try {
39
                        //Read input data
40
                        input1 = new GdalRead(rasterIn);
41
                        Object dataIn1 = input1.readBlock(0, 0, input1.getWidth(), input1.getHeight());
42
                        input2 = new GdalRead(rasterIn);
43
                        Object dataIn2 = input1.readBlock(0, 0, input2.getWidth(), input2.getHeight());
44
                        
45
                        //Create Buffer
46
                        Buffer buf1 = new RasterMemoryBuffer(input1.getRasterBufTypeFromGdalType(input1.getDataType()), 
47
                                                                                                                input1.getWidth(), 
48
                                                                                                                input1.getHeight(),
49
                                                                                                                input1.getBandCount(),
50
                                                                                                                true);
51
                        
52
                        //Set data to buffer
53
                        setLines(dataIn1, buf1);
54
                        //Create Buffer
55
                        Buffer buf2 = new RasterMemoryBuffer(input2.getRasterBufTypeFromGdalType(input2.getDataType()), 
56
                                                                                                                input2.getWidth(), 
57
                                                                                                                input2.getHeight(),
58
                                                                                                                input2.getBandCount(),
59
                                                                                                                true);
60
                        
61
                        //Set data to buffer
62
                        setLines(dataIn2, buf2);
63
                        
64
                        Band rb = buf2.getBand(2);
65
                        try {
66
                                buf1.assignBand(1, rb);
67
                        } catch (BandNotCompatibleException e) {
68
                                e.printStackTrace();
69
                        }
70
                        
71
                        //Write output file 
72
                        out = new GdalWrite(rasterOut, 4, input1.getDataType(), input1.getWidth(), input1.getHeight(), GdalWrite.COLOR_INTERP_GRAY);
73
                        out.writeBands(buf1.getBands());
74
                        out.close();        
75
                        
76
                        Band band1 = buf1.getBand(1);
77
                        Band band2 = buf1.getBand(3);
78
                        for (int i = 0; i < band1.getHeight(); i++) {
79
                                for (int j = 0; j < band1.getWidth(); j++) {
80
                                        byte b1 = band1.getElemByte(i, j);
81
                                        byte b2 = band2.getElemByte(i, j);
82
                                        assertEquals(b1, b2);
83
                                }
84
                        }
85
                        
86
                        buf1.free();
87
                        buf2.free();
88
                } catch (GdalException e) {
89
                        e.printStackTrace();
90
                } catch (IOException e) {
91
                        e.printStackTrace();
92
                } catch (InterruptedException e) {
93
                        e.printStackTrace();
94
                }  catch (OperationNotSupportedException e) {
95
                        e.printStackTrace();
96
                }
97
                long t2 = System.currentTimeMillis();
98
                System.out.println("Tiempo MemoryBuffer TestAssignBand: " + (t2 - t1) + " milisegundos");
99
        }
100
        
101
        private void setLines(Object dataIn, Buffer buf) throws OperationNotSupportedException {
102
                if(dataIn instanceof byte[][][]) {
103
                    byte[][][] d = (byte[][][])dataIn;
104
                    for (int iBand = 0; iBand < d.length; iBand++) {
105
                                for (int row = 0; row < d[iBand].length; row++) {
106
                                        buf.setLineInBandByte(d[iBand][row], row, iBand);
107
                                }
108
                        }
109
            }
110
        }
111
        
112
}