Statistics
| Revision:

gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / deprecated / buffer / test / memory / TestRemoveBand.java @ 1965

History | View | Annotate | Download (3.47 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.Buffer;
9
import org.gvsig.raster.cache.buffer.exception.OperationNotSupportedException;
10
import org.gvsig.raster.cache.buffer.exception.ProcessInterruptedException;
11
import org.gvsig.raster.cache.buffer.impl.io.GdalRead;
12
import org.gvsig.raster.cache.buffer.impl.io.GdalWrite;
13

    
14
/**
15
 * This test read a RGB image and remove one band from the read buffer. Then compare 
16
 * the bands that have not been removed with the source bands. As a result of this test a raster
17
 * of two bands is created and saved in the hard disk
18
 * 
19
 * @author Nacho Brodin (nachobrodin@gmail.com)
20
 */
21
public class TestRemoveBand extends TestCase {
22
        private String rasterIn = "./src/test/resources/image/001m09_1_0.tif";
23
        private String rasterOut = "/tmp/out-2bands.tif";
24
        
25
        public void start() {
26
                this.setUp();
27
                this.testStack();
28
        }
29

    
30
        public void setUp() {
31
                System.err.println("MemoryBuffer TestRemoveBand running...");
32
        }
33
        
34
        public void testStack() {
35
                long t1 = System.currentTimeMillis();
36
                GdalRead input = null;
37
                GdalWrite out = null;
38
                try {
39
                        //Read input data
40
                        input = new GdalRead(rasterIn);
41
                        Object dataIn = input.readBlock(0, 0, input.getWidth(), input.getHeight());
42
                        
43
                        //Create Buffer
44
                        Buffer buf = new RasterMemoryBuffer(input.getRasterBufTypeFromGdalType(input.getDataType()), 
45
                                                                                                                input.getWidth(), 
46
                                                                                                                input.getHeight(),
47
                                                                                                                input.getBandCount(),
48
                                                                                                                true);
49
                        
50
                        //Set data to buffer
51
                        setLines(dataIn, buf);
52
                        buf.removeBand(1);
53
                        
54
                        //Write output file 
55
                        out = new GdalWrite(rasterOut, 2, input.getDataType(), input.getWidth(), input.getHeight(), GdalWrite.COLOR_INTERP_GRAY);
56
                        out.writeBands(buf.getBands());
57
                        buf.free();
58
                        out.close();        
59
                        
60
                        //Open output file and compare
61
                        GdalRead newFile = new GdalRead(rasterOut);
62
                        Object dataOut = newFile.readBlock(0, 0, input.getWidth(), input.getHeight());
63
                        
64
                        byte[][][] d1 = (byte[][][])dataOut;
65
                        byte[][][] d2 = (byte[][][])dataIn;
66
                        
67
                        for (int row = 0; row < buf.getHeight(); row++) {
68
                                for (int col = 0; col < buf.getWidth(); col++) {
69
                                        if(d1[0][row][col] != d2[0][row][col])
70
                                                System.out.println("BAND:" + 0 +" ROW:" + row + " COL:" + col + " " + d1[0][row][col] + " " + d2[0][row][col]);
71
                                        assertEquals(d1[0][row][col], d2[0][row][col]);
72
                                }
73
                        }
74
                                
75
                        for (int row = 0; row < buf.getHeight(); row++) {
76
                                for (int col = 0; col < buf.getWidth(); col++) {
77
                                        if(d1[1][row][col] != d2[2][row][col])
78
                                                System.out.println("BAND:" + 1 +" ROW:" + row + " COL:" + col + " " + d1[1][row][col] + " " + d2[2][row][col]);
79
                                        assertEquals(d1[1][row][col], d2[2][row][col]);
80
                                }
81
                        }
82
                        
83
                } catch (GdalException e) {
84
                        e.printStackTrace();
85
                } catch (IOException e) {
86
                        e.printStackTrace();
87
                } catch (ProcessInterruptedException e) {
88
                        e.printStackTrace();
89
                } catch (OperationNotSupportedException e) {
90
                        e.printStackTrace();
91
                }
92
                long t2 = System.currentTimeMillis();
93
                System.out.println("Tiempo MemoryBuffer TestRemoveBand: " + (t2 - t1) + " milisegundos");
94
        }
95
        
96
        private void setLines(Object dataIn, Buffer buf) throws OperationNotSupportedException {
97
                if(dataIn instanceof byte[][][]) {
98
                    byte[][][] d = (byte[][][])dataIn;
99
                    for (int iBand = 0; iBand < d.length; iBand++) {
100
                                for (int row = 0; row < d[iBand].length; row++) {
101
                                        buf.setLineInBandByte(d[iBand][row], row, iBand);
102
                                }
103
                        }
104
            }
105
        }
106
        
107
}