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 / TestSwapTwoBands.java @ 991

History | View | Annotate | Download (3.66 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 two bands of a raster and save the result in other file.
18
 * Finally it compares the output bands with the input bands.
19
 * 
20
 * @author Nacho Brodin (nachobrodin@gmail.com)
21
 */
22
public class TestSwapTwoBands extends TestCase {
23
        private String rasterIn = "./src/test/resources/image/001m09_1_0.tif";
24
        private String rasterOut = "/tmp/out-swapedTwo.tif";
25
        
26
        public void start() {
27
                this.setUp();
28
                this.testStack();
29
        }
30

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

    
62
                        //Set data to buffer
63
                        setLines(dataIn1, buf2);
64
                        try {
65
                                buf2.swapBands(0, 2);
66
                        } catch (WrongParameterException e) {
67
                                e.printStackTrace();
68
                        }
69
                        
70
                        //Write output file 
71
                        out = new GdalWrite(rasterOut, input1.getBandCount(), input1.getDataType(), input1.getWidth(), input1.getHeight(), GdalWrite.COLOR_INTERP_RGB);
72
                        out.writeBands(buf2.getBands());
73
                        out.close();        
74
                        
75
                        Band band1 = buf1.getBand(0);
76
                        Band band2 = buf2.getBand(2);
77
                        compareBands(band1, band2);
78
                        
79
                        band1 = buf1.getBand(2);
80
                        band2 = buf2.getBand(0);
81
                        compareBands(band1, band2);
82
                        
83
                        band1 = buf1.getBand(1);
84
                        band2 = buf2.getBand(1);
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 (InterruptedException e) {
94
                        e.printStackTrace();
95
                } catch (OperationNotSupportedException e) {
96
                        e.printStackTrace();
97
                }
98
                long t2 = System.currentTimeMillis();
99
                System.out.println("Tiempo StripeCache TestSwapTwoBands: " + (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
                                assertEquals(b1, b2);
108
                        }
109
                }
110
        }
111
        
112
        private void setLines(Object dataIn, Buffer buf) throws OperationNotSupportedException {
113
                if(dataIn instanceof byte[][][]) {
114
                    byte[][][] d = (byte[][][])dataIn;
115
                    for (int iBand = 0; iBand < d.length; iBand++) {
116
                                for (int row = 0; row < d[iBand].length; row++) {
117
                                        buf.setLineInBandByte(d[iBand][row], row, iBand);
118
                                }
119
                        }
120
            }
121
        }
122
        
123
}