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 / vertical / TestRemoveBand.java @ 1023

History | View | Annotate | Download (4.12 KB)

1
package org.gvsig.raster.cache.buffer.impl.stripecache.vertical;
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.Buffer;
9
import org.gvsig.raster.cache.buffer.exception.OperationNotSupportedException;
10
import org.gvsig.raster.cache.buffer.exception.ProcessInterruptedException;
11
import org.gvsig.raster.cache.buffer.impl.BufferCacheManagerImpl;
12
import org.gvsig.raster.cache.buffer.impl.io.GdalRead;
13
import org.gvsig.raster.cache.buffer.impl.io.GdalWrite;
14

    
15
/**
16
 * This test read a RGB image and remove one band from the read buffer. Then compare 
17
 * the bands that have not been removed with the source bands. As a result of this test a raster
18
 * of two bands is created and saved in the hard disk
19
 * 
20
 * @author Nacho Brodin (nachobrodin@gmail.com)
21
 */
22
public class TestRemoveBand extends TestCase {
23
        private String rasterIn = "./src/test/resources/image/001m09_1_0.tif";
24
        private String rasterOut = "/tmp/out-2bands.tif";
25
        
26
        public void start() {
27
                this.setUp();
28
                this.testStack();
29
        }
30

    
31
        public void setUp() {
32
                System.err.println("StripeCache TestRemoveBand 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 buf = new RasterVertCacheBuffer(input.getRasterBufTypeFromGdalType(input.getDataType()), 
50
                                                                                                                input.getWidth(), 
51
                                                                                                                input.getHeight(),
52
                                                                                                                input.getBandCount());
53
                        
54
                        //Set data to buffer
55
                        setBytes(dataIn, buf);
56
                        buf.removeBand(1);
57
                        
58
                        //Write output file 
59
                        out = new GdalWrite(rasterOut, 2, input.getDataType(), input.getWidth(), input.getHeight(), GdalWrite.COLOR_INTERP_GRAY);
60
                        out.writeBands(buf.getBands());
61
                        buf.free();
62
                        out.close();        
63
                        
64
                        //Open output file and compare
65
                        GdalRead newFile = new GdalRead(rasterOut);
66
                        Object dataOut = newFile.readBlock(0, 0, input.getWidth(), input.getHeight());
67
                        
68
                        byte[][][] d1 = (byte[][][])dataOut;
69
                        byte[][][] d2 = (byte[][][])dataIn;
70
                        
71
                        for (int row = 0; row < buf.getHeight(); row++) {
72
                                for (int col = 0; col < buf.getWidth(); col++) {
73
                                        if(d1[0][row][col] != d2[0][row][col])
74
                                                System.out.println("BAND:" + 0 +" ROW:" + row + " COL:" + col + " " + d1[0][row][col] + " " + d2[0][row][col]);
75
                                        assertEquals(d1[0][row][col], d2[0][row][col]);
76
                                }
77
                        }
78
                                
79
                        for (int row = 0; row < buf.getHeight(); row++) {
80
                                for (int col = 0; col < buf.getWidth(); col++) {
81
                                        if(d1[1][row][col] != d2[2][row][col])
82
                                                System.out.println("BAND:" + 1 +" ROW:" + row + " COL:" + col + " " + d1[1][row][col] + " " + d2[2][row][col]);
83
                                        assertEquals(d1[1][row][col], d2[2][row][col]);
84
                                }
85
                        }
86
                        
87
                } catch (GdalException e) {
88
                        e.printStackTrace();
89
                } catch (IOException e) {
90
                        e.printStackTrace();
91
                } catch (ProcessInterruptedException e) {
92
                        e.printStackTrace();
93
                } catch (OperationNotSupportedException e) {
94
                        e.printStackTrace();
95
                }
96
                long t2 = System.currentTimeMillis();
97
                System.out.println("Tiempo StripeCache TestRemoveBand: " + (t2 - t1) + " milisegundos");
98
        }
99
        
100
        private void setBytes(Object dataIn, Buffer buf) throws OperationNotSupportedException {
101
                if(dataIn instanceof byte[][][]) {
102
                    byte[][][] d = (byte[][][])dataIn;
103
                    for (int iBand = 0; iBand < d.length; iBand++) {
104
                            for (int col = 0; col < d[iBand][0].length; col++) {
105
                                    for (int row = 0; row < d[iBand].length; row++) {
106
                                                buf.setElem(row, col, iBand, d[iBand][row][col]);
107
                                        }
108
                                }
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
}