gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / deprecated / buffer / test / memory / TestCopyBand.java @ 1965
History | View | Annotate | Download (2.97 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 |
|
16 |
/**
|
17 |
* Copy the band 0 from a buffer over the band 2 in the same buffer using the
|
18 |
* method getBandCopy.
|
19 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
20 |
*/
|
21 |
public class TestCopyBand extends TestCase { |
22 |
private String rasterIn = "./src/test/resources/image/001m09_1_0.tif"; |
23 |
private String rasterOut = "/tmp/out-copyFirstBand.tif"; |
24 |
|
25 |
public void start() { |
26 |
this.setUp();
|
27 |
this.testStack();
|
28 |
} |
29 |
|
30 |
public void setUp() { |
31 |
System.err.println("MemoryBuffer TestCopyBand running..."); |
32 |
} |
33 |
|
34 |
public void testStack() { |
35 |
long t1 = System.currentTimeMillis(); |
36 |
GdalRead input = null;
|
37 |
GdalWrite out = null;
|
38 |
try {
|
39 |
//Read input data
|
40 |
input = new GdalRead(rasterIn);
|
41 |
Object dataIn = input.readBlock(0, 0, input.getWidth(), input.getHeight()); |
42 |
|
43 |
//Create Buffer
|
44 |
Buffer buf1 = new RasterMemoryBuffer(input.getRasterBufTypeFromGdalType(input.getDataType()), |
45 |
input.getWidth(), |
46 |
input.getHeight(), |
47 |
input.getBandCount(), |
48 |
true);
|
49 |
|
50 |
//Set data to buffer
|
51 |
setLines(dataIn, buf1); |
52 |
|
53 |
|
54 |
Band rb = buf1.getBandCopy(0);
|
55 |
|
56 |
try {
|
57 |
buf1.copyBand(2, rb);
|
58 |
} catch (BandNotCompatibleException e) {
|
59 |
e.printStackTrace(); |
60 |
} |
61 |
|
62 |
//Write output file
|
63 |
out = new GdalWrite(rasterOut, 3, input.getDataType(), input.getWidth(), input.getHeight(), GdalWrite.COLOR_INTERP_RGB); |
64 |
out.writeBands(buf1.getBands()); |
65 |
out.close(); |
66 |
|
67 |
Band band1 = buf1.getBand(0);
|
68 |
Band band2 = buf1.getBand(2);
|
69 |
for (int i = 0; i < band1.getHeight(); i++) { |
70 |
for (int j = 0; j < band1.getWidth(); j++) { |
71 |
byte b1 = band1.getElemByte(i, j);
|
72 |
byte b2 = band2.getElemByte(i, j);
|
73 |
assertEquals(b1, b2); |
74 |
} |
75 |
} |
76 |
|
77 |
buf1.free(); |
78 |
} catch (GdalException e) {
|
79 |
e.printStackTrace(); |
80 |
} catch (IOException e) { |
81 |
e.printStackTrace(); |
82 |
} catch (ProcessInterruptedException e) {
|
83 |
e.printStackTrace(); |
84 |
} catch (OperationNotSupportedException e) { |
85 |
e.printStackTrace(); |
86 |
} |
87 |
long t2 = System.currentTimeMillis(); |
88 |
System.out.println("Tiempo MemoryBuffer TestCopyBand: " + (t2 - t1) + " milisegundos"); |
89 |
} |
90 |
|
91 |
private void setLines(Object dataIn, Buffer buf) throws OperationNotSupportedException { |
92 |
if(dataIn instanceof byte[][][]) { |
93 |
byte[][][] d = (byte[][][])dataIn; |
94 |
for (int iBand = 0; iBand < d.length; iBand++) { |
95 |
for (int row = 0; row < d[iBand].length; row++) { |
96 |
buf.setLineInBandByte(d[iBand][row], row, iBand); |
97 |
} |
98 |
} |
99 |
} |
100 |
} |
101 |
|
102 |
} |