Statistics
| Revision:

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

History | View | Annotate | Download (3.46 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.BandNotCompatibleException;
11
import org.gvsig.raster.cache.buffer.exception.OperationNotSupportedException;
12
import org.gvsig.raster.cache.buffer.exception.ProcessInterruptedException;
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
 * Copy the band 0 from a buffer over the band 2 of other buffer using the
19
 * method getBand (reference).
20
 * @author Nacho Brodin (nachobrodin@gmail.com)
21
 */
22
public class TestCopyBand2 extends TestCase {
23
        private String rasterIn = "./src/test/resources/image/001m09_1_0.tif";
24
        private String rasterOut = "/tmp/out-copyFirstBand.tif";
25
        
26
        public void start() {
27
                this.setUp();
28
                this.testStack();
29
        }
30

    
31
        public void setUp() {
32
                System.err.println("StripeCache TestCopyBand2 running...");
33
        }
34
        
35
        public void testStack() {
36
                long t1 = System.currentTimeMillis();
37
                GdalRead input = 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
                        input = new GdalRead(rasterIn);
46
                        Object dataIn = input.readBlock(0, 0, input.getWidth(), input.getHeight());
47
                        
48
                        //Create Buffer
49
                        Buffer buf1 = new RasterCacheBuffer(input.getRasterBufTypeFromGdalType(input.getDataType()), 
50
                                                                                                                input.getWidth(), 
51
                                                                                                                input.getHeight(),
52
                                                                                                                input.getBandCount());
53
                        
54
                        //Set data to buffer
55
                        setLines(dataIn, buf1);
56
                        
57
                        //Create Buffer
58
                        Buffer buf2 = new RasterCacheBuffer(input.getRasterBufTypeFromGdalType(input.getDataType()), 
59
                                                                                                                input.getWidth(), 
60
                                                                                                                input.getHeight(),
61
                                                                                                                input.getBandCount());
62
                        
63
                        //Set data to buffer
64
                        setLines(dataIn, buf2);
65
                        
66
                        Band rb = buf1.getBand(0);
67
                        
68
                        try {
69
                                buf2.copyBand(2, rb);
70
                        } catch (BandNotCompatibleException e) {
71
                                e.printStackTrace();
72
                        }
73
                        
74
                        //Write output file 
75
                        out = new GdalWrite(rasterOut, 3, input.getDataType(), input.getWidth(), input.getHeight(), GdalWrite.COLOR_INTERP_RGB);
76
                        out.writeBands(buf2.getBands());
77
                        out.close();        
78
                        
79
                        Band band1 = buf1.getBand(0);
80
                        Band band2 = buf2.getBand(2);
81
                        for (int i = 0; i < band1.getHeight(); i++) {
82
                                for (int j = 0; j < band1.getWidth(); j++) {
83
                                        byte b1 = band1.getElemByte(i, j);
84
                                        byte b2 = band2.getElemByte(i, j);
85
                                        assertEquals(b1, b2);
86
                                }
87
                        }
88
                        
89
                        buf1.free();
90
                        buf2.free();
91
                } catch (GdalException e) {
92
                        e.printStackTrace();
93
                } catch (IOException e) {
94
                        e.printStackTrace();
95
                } catch (ProcessInterruptedException e) {
96
                        e.printStackTrace();
97
                } catch (OperationNotSupportedException e) {
98
                        e.printStackTrace();
99
                }
100
                long t2 = System.currentTimeMillis();
101
                System.out.println("Tiempo StripeCache TestCopyBand2: " + (t2 - t1) + " milisegundos");
102
        }
103
        
104
        private void setLines(Object dataIn, Buffer buf) throws OperationNotSupportedException {
105
                if(dataIn instanceof byte[][][]) {
106
                    byte[][][] d = (byte[][][])dataIn;
107
                    for (int iBand = 0; iBand < d.length; iBand++) {
108
                                for (int row = 0; row < d[iBand].length; row++) {
109
                                        buf.setLineInBandByte(d[iBand][row], row, iBand);
110
                                }
111
                        }
112
            }
113
        }
114
        
115
}