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 / stripecache / horizontal / TestSwapBands.java @ 991

History | View | Annotate | Download (4.37 KB)

1
package org.gvsig.raster.cache.buffer.impl.stripecache.horizontal;
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.WrongParameterException;
12
import org.gvsig.raster.cache.buffer.impl.BufferCacheManagerImpl;
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("StripeCache TestSwapBands running...");
38
        }
39
        
40
        public void testStack() {
41
                long t1 = System.currentTimeMillis();
42
                GdalRead input1 = null;
43
                GdalWrite out = null;
44
                try {
45
                        //Reducimos el tama?o de la cache para una prueba con menos datos
46
                        BufferCacheManagerImpl.cacheSize = 2;
47
                        BufferCacheManagerImpl.pageSize = 0.2;
48
                        
49
                        //Read input data
50
                        input1 = new GdalRead(rasterIn);
51
                        Object dataIn1 = input1.readBlock(0, 0, input1.getWidth(), input1.getHeight());
52
                                                
53
                        //Create Buffer
54
                        Buffer buf1 = new RasterCacheBuffer(input1.getRasterBufTypeFromGdalType(input1.getDataType()), 
55
                                                                                                                input1.getWidth(), 
56
                                                                                                                input1.getHeight(),
57
                                                                                                                input1.getBandCount());
58
                        
59
                        //Set data to buffer
60
                        setBytes(dataIn1, buf1);
61
                                                
62
                        Buffer buf2 = new RasterCacheBuffer(input1.getRasterBufTypeFromGdalType(input1.getDataType()), 
63
                                                                                                                input1.getWidth(), 
64
                                                                                                                input1.getHeight(),
65
                                                                                                                input1.getBandCount());
66

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