Statistics
| Revision:

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

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

    
32
        public void setUp() {
33
                System.err.println("StripeCache TestSwapTwoBands running...");
34
        }
35
        
36
        public void testStack() {
37
                long t1 = System.currentTimeMillis();
38
                GdalRead input1 = null;
39
                GdalWrite out = null;
40
                try {
41
                        //Reducimos el tama?o de la cache para una prueba con menos datos
42
                        BufferCacheManagerImpl.cacheSize = 2;
43
                        BufferCacheManagerImpl.pageSize = 0.2;
44
                        
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 RasterCacheBuffer(input1.getRasterBufTypeFromGdalType(input1.getDataType()), 
51
                                                                                                                input1.getWidth(), 
52
                                                                                                                input1.getHeight(),
53
                                                                                                                input1.getBandCount());
54
                        
55
                        //Set data to buffer
56
                        setLines(dataIn1, buf1);
57
                                                
58
                        Buffer buf2 = new RasterCacheBuffer(input1.getRasterBufTypeFromGdalType(input1.getDataType()), 
59
                                                                                                                input1.getWidth(), 
60
                                                                                                                input1.getHeight(),
61
                                                                                                                input1.getBandCount());
62

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