Statistics
| Revision:

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

History | View | Annotate | Download (4.91 KB)

1
package org.gvsig.raster.cache.buffer.impl;
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.BufferDataSource;
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.io.BufferDataSourceImpl;
14
import org.gvsig.raster.cache.buffer.impl.io.GdalRead;
15
import org.gvsig.raster.cache.buffer.impl.memory.RasterMemoryBuffer;
16
import org.gvsig.raster.cache.buffer.impl.rocache.RasterReadOnlyBuffer;
17
import org.gvsig.raster.cache.buffer.impl.stripecache.horizontal.RasterCacheBuffer;
18

    
19
/**
20
 * This test calculate the statistics of a image and compare the result with 
21
 * the correct values. 
22
 *  
23
 * @author Nacho Brodin (nachobrodin@gmail.com)
24
 */
25
public class TestStatistics extends TestCase {
26
        private String rasterIn = "./src/test/resources/image/001m09_1_0.tif";
27
        
28
        public void start() {
29
                this.setUp();
30
                this.testStack();
31
        }
32

    
33
        public void setUp() {
34
                System.err.println("TestStatistics running...");
35
        }
36
        
37
        public void testStack() {
38
                long t1 = System.currentTimeMillis();
39
                GdalRead input = null;
40
                try {
41
        
42
                        //Read input data
43
                        input = new GdalRead(rasterIn);
44
                        Object dataIn = input.readBlock(0, 0, input.getWidth(), input.getHeight());
45
                        
46
                        //MEMORY
47
                        Buffer buf = new RasterMemoryBuffer(input.getRasterBufTypeFromGdalType(input.getDataType()), 
48
                                                                                                                input.getWidth(), 
49
                                                                                                                input.getHeight(),
50
                                                                                                                input.getBandCount(),
51
                                                                                                                true);
52
                        
53
                        //Set data to buffer
54
                        setLines(dataIn, buf);
55
                        buf.getStatistics().calculate();                        
56
                        test(buf);
57
                        buf.free();
58
                        
59
                        //CACHE
60
                        buf = new RasterCacheBuffer(input.getRasterBufTypeFromGdalType(input.getDataType()), 
61
                                                                                input.getWidth(), 
62
                                                                                input.getHeight(),
63
                                                                                input.getBandCount());
64
                        
65
                        //Set data to buffer
66
                        setLines(dataIn, buf);
67
                        buf.getStatistics().calculate();                        
68
                        test(buf);
69
                        buf.free();
70
                        
71
                        //READ ONLY
72
                        BufferDataSource ds = null;
73
                        try {
74
                                ds = new BufferDataSourceImpl(rasterIn);
75
                        } catch (IOException e) {
76
                                e.printStackTrace();
77
                        }
78
                        buf = new RasterReadOnlyBuffer(ds);
79
                        buf.getStatistics().calculate();                        
80
                        test(buf);
81
                        buf.free();
82
                        
83
                } catch (GdalException e) {
84
                        e.printStackTrace();
85
                } catch (IOException e) {
86
                        e.printStackTrace();
87
                } catch (ProcessInterruptedException e) {
88
                        e.printStackTrace();
89
                }  catch (OperationNotSupportedException e) {
90
                        e.printStackTrace();
91
                }
92
                long t2 = System.currentTimeMillis();
93
                System.out.println("Tiempo TestStatistics: " + (t2 - t1) + " milisegundos");
94
        }
95
        
96
        private void test(Buffer buf) {
97
                for (int i = 0; i < buf.getBandCount(); i++) {
98
                        Band b = buf.getBand(i);
99
                        /*System.out.println("Band " + i);
100
                        System.out.println("Max: " + b.getMax());
101
                        System.out.println("Min: " + b.getMin());
102
                        System.out.println("Max RGB: " + b.getMaxRGB());
103
                        System.out.println("Min RGB: " + b.getMinRGB());
104
                        System.out.println("SecondMax: " + b.getSecondMax());
105
                        System.out.println("SecondMin: " + b.getSecondMin());
106
                        System.out.println("SecondMax RGB: " + b.getSecondMaxRGB());
107
                        System.out.println("SecondMin RGB: " + b.getSecondMinRGB());
108
                        System.out.println("Mean: " + b.getMean());*/
109
                        if(i == 0) {
110
                                assertEquals((int)b.getMax(), 124);
111
                                assertEquals((int)b.getMin(), -125);
112
                                assertEquals((int)b.getMaxRGB(), 255);
113
                                assertEquals((int)b.getMinRGB(), 0);
114
                                assertEquals((int)b.getSecondMax(), 118);
115
                                assertEquals((int)b.getSecondMin(), -119);
116
                                assertEquals((int)b.getSecondMaxRGB(), 249);
117
                                assertEquals((int)b.getSecondMinRGB(), 6);
118
                                assertEquals((int)b.getMean(), 79);
119
                        }
120
                        if(i == 1) {
121
                                assertEquals((int)b.getMax(), 120);
122
                                assertEquals((int)b.getMin(), -128);
123
                                assertEquals((int)b.getMaxRGB(), 255);
124
                                assertEquals((int)b.getMinRGB(), 0);
125
                                assertEquals((int)b.getSecondMax(), 113);
126
                                assertEquals((int)b.getSecondMin(), -121);
127
                                assertEquals((int)b.getSecondMaxRGB(), 248);
128
                                assertEquals((int)b.getSecondMinRGB(), 8);
129
                                assertEquals((int)b.getMean(), 88);
130
                        }
131
                        if(i == 2) {
132
                                assertEquals((int)b.getMax(), 115);
133
                                assertEquals((int)b.getMin(), -128);
134
                                assertEquals((int)b.getMaxRGB(), 255);
135
                                assertEquals((int)b.getMinRGB(), 0);
136
                                assertEquals((int)b.getSecondMax(), 102);
137
                                assertEquals((int)b.getSecondMin(), -116);
138
                                assertEquals((int)b.getSecondMaxRGB(), 242);
139
                                assertEquals((int)b.getSecondMinRGB(), 13);
140
                                assertEquals((int)b.getMean(), 91);
141
                        }
142
                }
143
        }
144
        
145
        private void setLines(Object dataIn, Buffer buf) throws OperationNotSupportedException {
146
                if(dataIn instanceof byte[][][]) {
147
                    byte[][][] d = (byte[][][])dataIn;
148
                    for (int iBand = 0; iBand < d.length; iBand++) {
149
                                for (int row = 0; row < d[iBand].length; row++) {
150
                                        buf.setLineInBandByte(d[iBand][row], row, iBand);
151
                                }
152
                        }
153
            }
154
        }
155
        
156
}