Statistics
| Revision:

root / trunk / libraries / libRaster / src-test / org / gvsig / raster / buffer / TestBufferInterpolation.java @ 27361

History | View | Annotate | Download (8.51 KB)

1
/*
2
 * Created on 9-ago-2006
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 */
25
package org.gvsig.raster.buffer;
26

    
27
import java.awt.geom.AffineTransform;
28
import java.io.File;
29
import java.io.IOException;
30

    
31
import junit.framework.TestCase;
32

    
33
import org.gvsig.raster.RasterLibrary;
34
import org.gvsig.raster.dataset.GeoRasterWriter;
35
import org.gvsig.raster.dataset.IBuffer;
36
import org.gvsig.raster.dataset.IDataWriter;
37
import org.gvsig.raster.dataset.InvalidSetViewException;
38
import org.gvsig.raster.dataset.NotSupportedExtensionException;
39
import org.gvsig.raster.dataset.Params;
40
import org.gvsig.raster.dataset.RasterDataset;
41
import org.gvsig.raster.dataset.io.RasterDriverException;
42

    
43
/**
44
 * Test para probar las interpolaciones de un buffer de datos completo. El test crear? un 
45
 * buffer a partir de una imagen de 870x870 y aplicar? una reducci?n usando los m?todos
46
 * de interpolaci?n implementados. Despu?s comparar? el resultado con imagenes ya generadas
47
 * y correctas que se encuentran en el directorio de test-images. 
48
 * @author Nacho Brodin (nachobrodin@gmail.com)
49
 *
50
 */
51
public class TestBufferInterpolation extends TestCase {
52
        private String baseDir = "./test-images/";
53
        private String path = baseDir + "03AUG23153350-M2AS-000000122423_01_P001-BROWSE.jpg";
54
        private RasterDataset f = null;        
55
        private BufferFactory ds = null;
56
        private int size = 50;
57
        
58
        private String fileNeighbour1 = baseDir + "neighbour50x50.tif";
59
        private String fileBilinear1 = baseDir + "bilinear50x50.tif";
60
        private String fileInverseDistance1 = baseDir + "inverseDistance50x50.tif";
61
        private String fileBSpline1 = baseDir + "bspline50x50.tif";
62
        //private String fileBicubicSpline1 = baseDir + "bicubicspline50x50.tif";
63
        
64
        private String fileNeighbour = "/tmp/neighbour.tif";
65
        private String fileBilinear = "/tmp/bilinear.tif";
66
        private String fileInverseDistance = "/tmp/invdistance.tif";
67
        private String fileBSpline = "/tmp/bspline.tif";
68
        private String fileBicubicSpline = "/tmp/bicubicspline.tif";
69
        
70
        static {
71
                RasterLibrary.wakeUp();
72
        }
73
        
74
        public void start() {
75
                this.setUp();
76
                this.testStack();
77
        }
78
        
79
        public void setUp() {
80
                System.err.println("TestBufferInterpolation running...");
81
                int[] drawableBands = {0, 1, 2};
82
                try {
83
                        f = RasterDataset.open(null, path);
84
                } catch (NotSupportedExtensionException e) {
85
                        e.printStackTrace();
86
                        return;
87
                } catch (RasterDriverException e) {
88
                        e.printStackTrace();
89
                        return;
90
                }
91
                ds = new BufferFactory(f);
92
                ds.setDrawableBands(drawableBands);
93
                try {
94
                        ds.setAreaOfInterest(0, 0, f.getWidth(), f.getHeight());
95
                } catch (InvalidSetViewException e1) {
96
                        e1.printStackTrace();
97
                } catch (InterruptedException e) {
98
                        e.printStackTrace();
99
                } catch (RasterDriverException e) {
100
                        e.printStackTrace();
101
                }
102
                RasterBuffer buf = (RasterBuffer)ds.getRasterBuf();
103
                
104
                try {
105
                        IBuffer b1 = buf.getAdjustedWindow(size, size, BufferInterpolation.INTERPOLATION_NearestNeighbour);
106
                        convertBufferToTif(fileNeighbour, f.getAffineTransform(), b1);
107
                        b1 = buf.getAdjustedWindow(size, size, BufferInterpolation.INTERPOLATION_Bilinear);
108
                        convertBufferToTif(fileBilinear, f.getAffineTransform(), b1);
109
                        b1 = buf.getAdjustedWindow(size, size, BufferInterpolation.INTERPOLATION_InverseDistance);
110
                        convertBufferToTif(fileInverseDistance, f.getAffineTransform(), b1);
111
                        b1 = buf.getAdjustedWindow(size, size, BufferInterpolation.INTERPOLATION_BSpline);
112
                        convertBufferToTif(fileBSpline, f.getAffineTransform(), b1);
113
                        b1 = buf.getAdjustedWindow(size, size, BufferInterpolation.INTERPOLATION_BicubicSpline);
114
                        convertBufferToTif(fileBicubicSpline, f.getAffineTransform(), b1);
115
                } catch (IOException e) {
116
                        e.printStackTrace();
117
                } catch (InterruptedException e) {
118
                        e.printStackTrace();
119
                } catch (RasterDriverException e) {
120
                        e.printStackTrace();
121
                }  
122
                
123
        }
124
        
125
        public void testStack(){
126
                try {
127
                        compareResult(fileNeighbour, fileNeighbour1);
128
                        compareResult(fileBilinear, fileBilinear1);
129
                        compareResult(fileInverseDistance, fileInverseDistance1);
130
                        compareResult(fileBSpline, fileBSpline1);
131
                } catch (InterruptedException e) {
132
                }
133
                //compareResult(fileBicubicSpline, fileBicubicSpline1);
134
                File file1 = new File(fileNeighbour);
135
                File file2 = new File(fileBilinear);
136
                File file3 = new File(fileInverseDistance);
137
                File file4 = new File(fileBSpline);
138
                //File file5 = new File(fileBicubicSpline);
139
                if(file1.exists())
140
                        file1.delete();
141
                if(file2.exists())
142
                        file2.delete();
143
                if(file3.exists())
144
                        file3.delete();
145
                if(file4.exists())
146
                        file4.delete();
147
                //if(file5.exists())
148
                //        file5.delete();
149
        }
150
        
151
        /**
152
         * Compara dos ficheros raster
153
         * @param f1
154
         * @param f2
155
         * @throws InterruptedException 
156
         */
157
        private void compareResult(String f1, String f2) throws InterruptedException {
158
                int[] drawableBands = {0, 1, 2};
159
                RasterDataset d1 = null;
160
                RasterDataset d2 = null;
161
                try {
162
                        d1 = RasterDataset.open(null, f1);
163
                        d2 = RasterDataset.open(null, f2);
164
                } catch (NotSupportedExtensionException e) {
165
                        e.printStackTrace();
166
                        return;
167
                } catch (RasterDriverException e) {
168
                        e.printStackTrace();
169
                        return;
170
                }
171
                BufferFactory ds = new BufferFactory(d1);
172
                ds.setDrawableBands(drawableBands);
173
                try {
174
                        ds.setAreaOfInterest(0, 0, d1.getWidth(), d1.getHeight());
175
                } catch (InvalidSetViewException e) {
176
                        e.printStackTrace();
177
                } catch (InterruptedException e) {
178
                        e.printStackTrace();
179
                } catch (RasterDriverException e) {
180
                        e.printStackTrace();
181
                }
182
                IBuffer b1 = ds.getRasterBuf();
183
                
184
                ds = new BufferFactory(d2);
185
                ds.setDrawableBands(drawableBands);
186
                try {
187
                        ds.setAreaOfInterest(0, 0, d1.getWidth(), d1.getHeight());
188
                } catch (InvalidSetViewException e) {
189
                        e.printStackTrace();
190
                } catch (InterruptedException e) {
191
                        e.printStackTrace();
192
                } catch (RasterDriverException e) {
193
                        e.printStackTrace();
194
                }
195
                IBuffer b2 = ds.getRasterBuf(); 
196
                
197
                for (int k = 0; k < b1.getBandCount(); k++) {
198
                        for (int i = 0; i < b1.getHeight(); i++) {
199
                                for (int j = 0; j < b1.getWidth(); j++) {
200
                                        switch(b1.getDataType()) {
201
                                        case IBuffer.TYPE_BYTE:
202
                                                assertEquals(b1.getElemByte(i, j, k), b2.getElemByte(i, j, k));
203
                                                break;
204
                                        case IBuffer.TYPE_SHORT:
205
                                                assertEquals(b1.getElemShort(i, j, k), b2.getElemShort(i, j, k));
206
                                                break;
207
                                                
208
                                        case IBuffer.TYPE_INT:
209
                                                assertEquals(b1.getElemInt(i, j, k), b2.getElemInt(i, j, k));
210
                                                break;
211
                                                
212
                                        case IBuffer.TYPE_FLOAT:
213
                                                assertEquals((int)b1.getElemFloat(i, j, k), (int)b2.getElemFloat(i, j, k));
214
                                                break;
215
                                                
216
                                        case IBuffer.TYPE_DOUBLE:
217
                                                assertEquals((int)b1.getElemDouble(i, j, k), (int)b2.getElemDouble(i, j, k));
218
                                                break;
219
                                        }
220
                                }
221
                        }
222
                }
223
        }
224
        
225
        /**
226
         * Funci?n para pruebas.
227
         * Convierte los ficheros generados por la funci?n cachear en ficheros tif para comprobar que est?n
228
         * bien generados.
229
         * @param grf
230
         * @param pageBuffer
231
         * @param pageLines
232
         * @throws IOException
233
         */
234
        private void convertBufferToTif(String fileName, AffineTransform at, IBuffer buffer)throws IOException, InterruptedException, RasterDriverException {
235
                IDataWriter dataWriter1 = new WriterBufferServer(buffer);
236
                GeoRasterWriter grw = null;
237
                try {
238
                        Params params = GeoRasterWriter.getWriter(fileName).getParams();
239
                        params.changeParamValue("blocksize", "7"); //posici?n 7 del array -> 512
240
                        params.changeParamValue("tfw", "false");
241
                        params.changeParamValue("interleave", new Integer(1));//posici?n 1 del array -> PIXEL
242
                        grw = GeoRasterWriter.getWriter(dataWriter1, 
243
                                                                                        fileName,
244
                                                                                        buffer.getBandCount(),
245
                                                                                        at,
246
                                                                                        buffer.getWidth(), 
247
                                                                                        buffer.getHeight(), 
248
                                                                                        buffer.getDataType(),
249
                                                                                        params,
250
                                                                                        null);
251
                        
252
                } catch (NotSupportedExtensionException e) {
253
                        throw new RasterDriverException("");
254
                }
255
                grw.dataWrite();
256
                grw.writeClose();
257
        }
258

    
259
}