Statistics
| Revision:

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

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

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

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

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