gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / deprecated / buffer / test / memory / TestCopyBand2.java @ 1965
History | View | Annotate | Download (3.33 KB)
1 |
package org.gvsig.raster.cache.buffer.impl.memory; |
---|---|
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.io.GdalRead; |
14 |
import org.gvsig.raster.cache.buffer.impl.io.GdalWrite; |
15 |
import org.gvsig.raster.cache.buffer.impl.stripecache.horizontal.RasterCacheBuffer; |
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("MemoryBuffer TestCopyBand2 running..."); |
33 |
} |
34 |
|
35 |
public void testStack() { |
36 |
long t1 = System.currentTimeMillis(); |
37 |
GdalRead input = null;
|
38 |
GdalWrite out = null;
|
39 |
try {
|
40 |
//Read input data
|
41 |
input = new GdalRead(rasterIn);
|
42 |
Object dataIn = input.readBlock(0, 0, input.getWidth(), input.getHeight()); |
43 |
|
44 |
//Create Buffer
|
45 |
Buffer buf1 = new RasterMemoryBuffer(input.getRasterBufTypeFromGdalType(input.getDataType()), |
46 |
input.getWidth(), |
47 |
input.getHeight(), |
48 |
input.getBandCount(), |
49 |
true);
|
50 |
|
51 |
//Set data to buffer
|
52 |
setLines(dataIn, buf1); |
53 |
|
54 |
//Create Buffer
|
55 |
Buffer buf2 = new RasterCacheBuffer(input.getRasterBufTypeFromGdalType(input.getDataType()), |
56 |
input.getWidth(), |
57 |
input.getHeight(), |
58 |
input.getBandCount()); |
59 |
|
60 |
//Set data to buffer
|
61 |
setLines(dataIn, buf2); |
62 |
|
63 |
Band rb = buf1.getBand(0);
|
64 |
|
65 |
try {
|
66 |
buf2.copyBand(2, rb);
|
67 |
} catch (BandNotCompatibleException e) {
|
68 |
e.printStackTrace(); |
69 |
} |
70 |
|
71 |
//Write output file
|
72 |
out = new GdalWrite(rasterOut, 3, input.getDataType(), input.getWidth(), input.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 |
for (int i = 0; i < band1.getHeight(); i++) { |
79 |
for (int j = 0; j < band1.getWidth(); j++) { |
80 |
byte b1 = band1.getElemByte(i, j);
|
81 |
byte b2 = band2.getElemByte(i, j);
|
82 |
assertEquals(b1, b2); |
83 |
} |
84 |
} |
85 |
|
86 |
buf1.free(); |
87 |
buf2.free(); |
88 |
} catch (GdalException e) {
|
89 |
e.printStackTrace(); |
90 |
} catch (IOException e) { |
91 |
e.printStackTrace(); |
92 |
} catch (ProcessInterruptedException e) {
|
93 |
e.printStackTrace(); |
94 |
} catch (OperationNotSupportedException e) { |
95 |
e.printStackTrace(); |
96 |
} |
97 |
long t2 = System.currentTimeMillis(); |
98 |
System.out.println("Tiempo MemoryBuffer TestCopyBand2: " + (t2 - t1) + " milisegundos"); |
99 |
} |
100 |
|
101 |
private void setLines(Object dataIn, Buffer buf) throws OperationNotSupportedException { |
102 |
if(dataIn instanceof byte[][][]) { |
103 |
byte[][][] d = (byte[][][])dataIn; |
104 |
for (int iBand = 0; iBand < d.length; iBand++) { |
105 |
for (int row = 0; row < d[iBand].length; row++) { |
106 |
buf.setLineInBandByte(d[iBand][row], row, iBand); |
107 |
} |
108 |
} |
109 |
} |
110 |
} |
111 |
|
112 |
} |