Statistics
| Revision:

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

History | View | Annotate | Download (3.63 KB)

1
package org.gvsig.raster.cache.buffer.impl.rocache;
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.BufferDataSource;
11
import org.gvsig.raster.cache.buffer.exception.OperationNotSupportedException;
12
import org.gvsig.raster.cache.buffer.exception.ProcessInterruptedException;
13
import org.gvsig.raster.cache.buffer.exception.WrongParameterException;
14
import org.gvsig.raster.cache.buffer.impl.io.BufferDataSourceImpl;
15
import org.gvsig.raster.cache.buffer.impl.io.GdalRead;
16
import org.gvsig.raster.cache.buffer.impl.memory.RasterMemoryBuffer;
17

    
18
/**
19
 * This test swap several bands of a raster compare the result with a memory buffer
20
 * swaped.
21
 * <P>
22
 * The array to test is {2, 0, 1}. That means the band 0 goes to the position two, the
23
 * band one goes to the position zero and the band two goes to the position one. 
24
 * When the swap operation is executed, this test will check comparing the result 
25
 * bands with the input bands 
26
 * </P>
27
 * @author Nacho Brodin (nachobrodin@gmail.com)
28
 */
29
public class TestSwapBands extends TestCase {
30
        private String rasterIn = "./src/test/resources/image/001m09_1_0.tif";
31
        
32
        public void start() {
33
                this.setUp();
34
                this.testStack();
35
        }
36

    
37
        public void setUp() {
38
                System.err.println("ROCache TestSwapBands running...");
39
        }
40
        
41
        public void testStack() {
42
                long t1 = System.currentTimeMillis();
43
                GdalRead input1 = null;
44
                try {
45
                        //Read input data
46
                        input1 = new GdalRead(rasterIn);
47
                        Object dataIn1 = input1.readBlock(0, 0, input1.getWidth(), input1.getHeight());
48

    
49
                        //Create Buffer                                                
50
                        Buffer buf2 = new RasterMemoryBuffer(input1.getRasterBufTypeFromGdalType(input1.getDataType()), 
51
                                        input1.getWidth(), 
52
                                        input1.getHeight(),
53
                                        input1.getBandCount(),
54
                                        true);
55

    
56
                        //Set data to buffer
57
                        setLines(dataIn1, buf2);
58

    
59
                        BufferDataSource ds = null;
60
                        try {
61
                                ds = new BufferDataSourceImpl(rasterIn);
62
                        } catch (IOException e) {
63
                                e.printStackTrace();
64
                        }
65
                        RasterReadOnlyBuffer buf1 = new RasterReadOnlyBuffer(ds);
66
                        try {
67
                                buf2.swapBands(new int[]{2, 0, 1});
68
                                buf1.swapBands(new int[]{2, 0, 1});
69
                        } catch (WrongParameterException e) {
70
                                e.printStackTrace();
71
                        }
72
                        
73
                        //Write output file 
74
                                                
75
                        Band band1 = buf1.getBand(0);
76
                        Band band2 = buf2.getBand(0);
77
                        compareBands(band1, band2);
78
                        
79
                        band1 = buf1.getBand(1);
80
                        band2 = buf2.getBand(1);
81
                        compareBands(band1, band2);
82
                        
83
                        band1 = buf1.getBand(2);
84
                        band2 = buf2.getBand(2);
85
                        compareBands(band1, band2);
86
                        
87
                        buf1.free();
88
                        buf2.free();
89
                } catch (GdalException e) {
90
                        e.printStackTrace();
91
                } catch (IOException e) {
92
                        e.printStackTrace();
93
                } catch (ProcessInterruptedException e) {
94
                        e.printStackTrace();
95
                } catch (OperationNotSupportedException e) {
96
                        e.printStackTrace();
97
                }
98
                long t2 = System.currentTimeMillis();
99
                System.out.println("Tiempo ROCache TestSwapBands: " + (t2 - t1) + " milisegundos");
100
        }
101
        
102
        public void compareBands(Band band1, Band band2) {
103
                for (int i = 0; i < band1.getHeight(); i++) {
104
                        for (int j = 0; j < band1.getWidth(); j++) {
105
                                byte b1 = band1.getElemByte(i, j);
106
                                byte b2 = band2.getElemByte(i, j);
107
                                if(b1 != b2)
108
                                        System.out.println(i + " " + j);
109
                                assertEquals(b1, b2);
110
                        }
111
                }
112
        }
113
        
114
        private void setLines(Object dataIn, Buffer buf) throws OperationNotSupportedException {
115
                if(dataIn instanceof byte[][][]) {
116
                    byte[][][] d = (byte[][][])dataIn;
117
                    for (int iBand = 0; iBand < d.length; iBand++) {
118
                                for (int row = 0; row < d[iBand].length; row++) {
119
                                        buf.setLineInBandByte(d[iBand][row], row, iBand);
120
                                }
121
                        }
122
            }
123
        }
124
        
125
}