Statistics
| Revision:

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

History | View | Annotate | Download (4.28 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.exception.ProcessInterruptedException;
12
import org.gvsig.raster.cache.buffer.exception.WrongParameterException;
13
import org.gvsig.raster.cache.buffer.impl.io.GdalRead;
14
import org.gvsig.raster.cache.buffer.impl.io.GdalWrite;
15

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

    
36
        public void setUp() {
37
                System.err.println("MemoryBuffer TestSwapBands running...");
38
        }
39
        
40
        public void testStack() {
41
                long t1 = System.currentTimeMillis();
42
                GdalRead input1 = null;
43
                GdalWrite out = 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 buf1 = new RasterMemoryBuffer(input1.getRasterBufTypeFromGdalType(input1.getDataType()), 
51
                                                                                                                input1.getWidth(), 
52
                                                                                                                input1.getHeight(),
53
                                                                                                                input1.getBandCount(),
54
                                                                                                                true);
55
                        
56
                        //Set data to buffer
57
                        setBytes(dataIn1, buf1);
58
                                                
59
                        Buffer buf2 = new RasterMemoryBuffer(input1.getRasterBufTypeFromGdalType(input1.getDataType()), 
60
                                                                                                                input1.getWidth(), 
61
                                                                                                                input1.getHeight(),
62
                                                                                                                input1.getBandCount(),
63
                                                                                                                true);
64

    
65
                        //Set data to buffer
66
                        setBytes(dataIn1, buf2);
67
                        try {
68
                                buf2.swapBands(new int[]{2, 0, 1});
69
                        } catch (WrongParameterException e) {
70
                                e.printStackTrace();
71
                        }
72
                        
73
                        //Write output file 
74
                        out = new GdalWrite(rasterOut, input1.getBandCount(), input1.getDataType(), input1.getWidth(), input1.getHeight(), GdalWrite.COLOR_INTERP_RGB);
75
                        out.writeBands(buf2.getBands());
76
                        out.close();        
77
                        
78
                        Band band1 = buf1.getBand(0);
79
                        Band band2 = buf2.getBand(1);
80
                        compareBands(band1, band2);
81
                        
82
                        band1 = buf1.getBand(1);
83
                        band2 = buf2.getBand(2);
84
                        compareBands(band1, band2);
85
                        
86
                        band1 = buf1.getBand(2);
87
                        band2 = buf2.getBand(0);
88
                        compareBands(band1, band2);
89
                        
90
                        buf1.free();
91
                        buf2.free();
92
                } catch (GdalException e) {
93
                        e.printStackTrace();
94
                } catch (IOException e) {
95
                        e.printStackTrace();
96
                } catch (ProcessInterruptedException e) {
97
                        e.printStackTrace();
98
                } catch (OperationNotSupportedException e) {
99
                        e.printStackTrace();
100
                }
101
                long t2 = System.currentTimeMillis();
102
                System.out.println("Tiempo MemoryBuffer TestSwapBands: " + (t2 - t1) + " milisegundos");
103
        }
104
        
105
        public void compareBands(Band band1, Band band2) {
106
                for (int i = 0; i < band1.getHeight(); i++) {
107
                        for (int j = 0; j < band1.getWidth(); j++) {
108
                                byte b1 = band1.getElemByte(i, j);
109
                                byte b2 = band2.getElemByte(i, j);
110
                                assertEquals(b1, b2);
111
                        }
112
                }
113
        }
114
        
115
//        private void setLines(Object dataIn, Buffer buf) throws OperationNotSupportedException {
116
//                if(dataIn instanceof byte[][][]) {
117
//                    byte[][][] d = (byte[][][])dataIn;
118
//                    for (int iBand = 0; iBand < d.length; iBand++) {
119
//                                for (int row = 0; row < d[iBand].length; row++) {
120
//                                        buf.setLineInBandByte(d[iBand][row], row, iBand);
121
//                                }
122
//                        }
123
//            }
124
//        }
125
        
126
        private void setBytes(Object dataIn, Buffer buf) throws OperationNotSupportedException {
127
                if(dataIn instanceof byte[][][]) {
128
                    byte[][][] d = (byte[][][])dataIn;
129
                    for (int iBand = 0; iBand < d.length; iBand++) {
130
                            for (int row = 0; row < d[iBand].length; row++) {
131
                                    for (int col = 0; col < d[iBand][0].length; col++) {
132
                                                buf.setElem(row, col, iBand, d[iBand][row][col]);
133
                                        }
134
                                }
135
                        }
136
            }
137
        }
138
        
139
}