Statistics
| Revision:

root / trunk / libraries / libCq CMS for java.old / src / org / cresques / io / GdalFile.java @ 4974

History | View | Annotate | Download (30.4 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 * 
4
 * Copyright (C) 2004-5. 
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 * 
22
 * cresques@gmail.com
23
 */
24
package org.cresques.io;
25

    
26
import java.awt.Image;
27
import java.awt.Point;
28
import java.awt.geom.Point2D;
29
import java.awt.image.BufferedImage;
30
import java.awt.image.DataBuffer;
31
import java.io.IOException;
32
import java.util.Vector;
33

    
34
import org.cresques.cts.ICoordTrans;
35
import org.cresques.cts.IProjection;
36
import org.cresques.io.raster.RasterBuf;
37
import org.cresques.px.Extent;
38

    
39
import es.gva.cit.jgdal.Gdal;
40
import es.gva.cit.jgdal.GdalBuffer;
41
import es.gva.cit.jgdal.GdalException;
42
import es.gva.cit.jgdal.GdalRasterBand;
43
import es.gva.cit.jgdal.GeoTransform;
44
/**
45
 * Soporte 'nativo' para ficheros desde GDAL.
46
 * Este conjunto de funcionalidades est? tomado de manera casi literal
47
 * del soporte para ECW de ermapper.<br>
48
 * Probablemente esto deber?a formar parte del JNI que recubre a la
49
 * librer?a en C extraida de gdal.<br>
50
 * Lo pongo aqu? a manera de ejemplo de como atacar un formato binario
51
 * desde Java.<br><br>   
52
 * @author Luis W. Sevilla.
53
 */
54

    
55
class GdalNative extends Gdal {
56
        static boolean WITH_OVERVIEWS = true;
57
        private String ext = "";
58
        private String shortName = "";
59
        
60
        // Polilinea con extent
61
        public class Contour extends Vector {
62
                final private static long serialVersionUID = -3370601314380922368L;
63
                public double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE;
64
                public double maxX = -Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
65
                public Contour() {
66
                        super();
67
                }
68
                public void add(Point2D pt) {
69
                        super.add(pt);
70
                        if (pt.getX() > maxX) maxX = pt.getX();
71
                        if (pt.getX() < minX) minX = pt.getX();
72
                        if (pt.getY() > maxY) maxY = pt.getY();
73
                        if (pt.getY() < minY) minY = pt.getY();
74
                }
75
        }
76
        /**
77
         * Contorno en coordenadas geogr?ficas. (y Extent del raster).
78
         */
79
        
80
        public Contour esq = new Contour();
81
        public int width = 0, height = 0;
82
        public double originX = 0D, originY = 0D;
83
        public String version = "";
84

    
85
        private int alpha = 0;
86
        protected int rBandNr = 1, gBandNr = 2, bBandNr = 3, aBandNr = 4;
87
        private int dataType = GDT_Byte;
88
        
89
        public GdalNative(String fName) throws GdalException, IOException {
90
                super();
91
                init(fName);
92
        }
93
        
94
        private void init(String fName) throws GdalException, IOException {
95
                open(fName,GA_ReadOnly);
96
                ext = fName.toLowerCase().substring(fName.lastIndexOf('.')+1);
97
                if (ext.compareTo("tif") == 0)
98
                        WITH_OVERVIEWS = false;
99
                width = getRasterXSize();
100
                height = getRasterYSize();
101
                setDataType(this.getRasterBand(1).getRasterDataType());
102
                shortName = getDriverShortName();
103
                if (true) { //ext.compareTo("sid") == 0) {
104
                        //String [] metadata = getMetadata();
105
                        double ox=0D, oy=0D, resx=0D, resy=0D;
106
                        try{
107
                                GeoTransform trans = getGeoTransform();
108
                                ox = trans.adfgeotransform[0];
109
                                oy = trans.adfgeotransform[3];
110
                                resx = trans.adfgeotransform[1];
111
                                resy = trans.adfgeotransform[5];
112
                                
113
                                //System.out.println("Origin = ("+ox+","+oy+")");
114
                                System.out.println("Pixel Size = ("+resx+","+resy+")");
115
                                  esq.add(new Point2D.Double(ox, oy));
116
                                  esq.add(new Point2D.Double(ox+resx*width, oy));
117
                                  esq.add(new Point2D.Double(ox, oy+resy*height));
118
                                  esq.add(new Point2D.Double(ox+resx*width, oy+resy*height));
119
                         }catch(GdalException exc){
120
                                 esq.add(new Point2D.Double(0, 0));
121
                                 esq.add(new Point2D.Double(width, 0));
122
                                 esq.add(new Point2D.Double(0, height));
123
                                 esq.add(new Point2D.Double(width, height));
124
                         }
125
                } else  { //version.startsWith("1")) {
126
                          //double [] transParam = getGeoTransform();
127
                          esq.add(new Point2D.Double(0D,height));
128
                          esq.add(new Point2D.Double(0D,0D));
129
                          esq.add(new Point2D.Double(width,0D));
130
                          esq.add(new Point2D.Double(width,height));
131
                  }
132
        }
133
        
134
        public void setAlpha(int a) { alpha = a; }
135
        
136
        public void setDataType(int dt) { dataType = dt; }
137
        public int getDataType() { return dataType; }
138
        
139
        double lastReadLine = -1;
140
        int currentFullWidth = -1;
141
        int currentFullHeight = -1;
142
        int currentViewWidth = -1;
143
        int currentViewHeight = -1;
144
        double currentViewX = 0D;
145
        double viewportScale = 0D;
146
        double wcWidth = 0D;
147
        double step = 0D;
148
        int currentOverview = -1;
149
        
150
        protected GdalRasterBand bandR = null, bandG = null, bandB = null, bandA = null;
151
        
152
        
153
        /**
154
         * Devuelve la banda actualmente en uso para el color especificado.
155
         * @param color 0=Rojo, 1=Green, 2=Blue.
156
         * @return
157
         */
158
        public GdalRasterBand getCurrentBand(int color) {
159
                if (color == 0) 
160
                        return bandR;
161
                else if (color == 1) 
162
                        return bandG;
163
                return bandB;
164
        }
165
        // Supone rasters no girados
166
        public Point2D worldToRaster(Point2D pt) {
167
                double x = (((double) currentFullWidth)/(esq.maxX-esq.minX))*(pt.getX()-esq.minX);
168
                double y = (((double) currentFullHeight)/(esq.maxY-esq.minY))*(esq.maxY-pt.getY());
169
                Point2D ptRes = new Point2D.Double(x, y);
170
                return ptRes;
171
        }
172
        
173
        public int setView(double dWorldTLX, double dWorldTLY,
174
            double dWorldBRX, double dWorldBRY,
175
            int nWidth, int nHeight) {
176
                int err = 0;
177
                currentFullWidth = width;
178
                currentFullHeight = height;
179
                Point2D tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
180
                Point2D br = worldToRaster(new Point2D.Double(dWorldBRX, dWorldBRY));
181
                // Calcula cual es la primera l?nea a leer;
182
                currentViewWidth = nWidth;
183
                currentViewHeight = nHeight;
184
                wcWidth = Math.abs(br.getX() - tl.getX());
185
                
186
                currentViewX = tl.getX();
187
                viewportScale = (double) currentViewWidth/(br.getX()-tl.getX());
188
                step = 1D/viewportScale;
189
                lastReadLine = tl.getY();
190
                try {
191
                        // calcula el overview a usar
192
                        bandR = getRasterBand(1);
193
                        currentOverview = -1;
194
                        if (WITH_OVERVIEWS && bandR.getOverviewCount() > 0) {
195
                                GdalRasterBand ovb = null;
196
                                for (int i=bandR.getOverviewCount()-1; i>0; i--) {              
197
                                        ovb = bandR.getOverview(i);
198
                                        if (ovb.getRasterBandXSize()>getRasterXSize()*viewportScale) {
199
                                                currentOverview = i;
200
                                    viewportScale *= ((double) width/(double) ovb.getRasterBandXSize());
201
                                    step = 1D/viewportScale;
202
                                    currentFullWidth = ovb.getRasterBandXSize();
203
                                    currentFullHeight = ovb.getRasterBandYSize();
204
                                    tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
205
                                    currentViewX = tl.getX();
206
                                    lastReadLine = tl.getY();
207
                                    break;
208
                                        }
209
                                }
210
                        }
211
        
212
                        // Selecciona las bandas y los overviews necesarios
213
                        bandR = getRasterBand(rBandNr);
214
                        setDataType(bandR.getRasterDataType());
215
                        if (this.getRasterCount() > 1) {
216
                                bandG = getRasterBand(gBandNr);
217
                                bandB = getRasterBand(bBandNr);
218
                                if(this.getRasterCount() == 4 && shortName.equals("PNG"))
219
                                        bandA = getRasterBand(aBandNr); 
220
                        }
221
                        if (currentOverview > 0) {
222
                                bandR = bandR.getOverview(currentOverview);
223
                            if (this.getRasterCount() > 1) {
224
                                        bandG = bandG.getOverview(currentOverview);
225
                                        bandB = bandB.getOverview(currentOverview);
226
                                        if(this.getRasterCount() == 4 && shortName.equals("PNG"))
227
                                                bandA = bandA.getOverview(currentOverview);
228
                                }
229
                        }
230

    
231
                        //System.out.println(band.)
232
                } catch (GdalException e) {
233
                        // TODO Auto-generated catch block
234
                        e.printStackTrace();
235
                }
236

    
237
                System.out.println("GdalFile: TL=("+dWorldTLX+","+dWorldTLY+
238
                        "); BR=("+dWorldBRX+","+dWorldBRY+")\n"+
239
                        "GdalFile: escala="+viewportScale+"; lastReadLine="+lastReadLine+"\n"+
240
                    "Actual Raster Size="+currentFullWidth+"x"+currentFullHeight+
241
                        "\nDataType="+dataType);
242
                return err;
243
        }
244
        
245
        int lastY = -1;
246
        
247
        public void readLine(int[][] line) throws GdalException {
248
        int w = (int) Math.ceil(((double)currentViewWidth)*step);
249
        int x = (int) Math.ceil(currentViewX);
250
        int y = (int) Math.ceil(lastReadLine);
251
        GdalBuffer r = null, g = null, b = null, p = null;
252
        GdalBuffer a = new GdalBuffer();
253
        
254
        //if (alpha > 0) a = alpha << 24;
255
        if (x+w > bandR.getRasterBandXSize()) 
256
                w = bandR.getRasterBandXSize()-x;
257
        
258
        if(bandR.getRasterColorTable() != null){
259
                p = bandR.readRasterWithPalette(x, y, w, 1, w, 1, dataType);
260
                a.buffByte = p.buffAPalette;
261
                r = new GdalBuffer();
262
                r.buffByte = p.buffRPalette;
263
                g = new GdalBuffer();
264
                g.buffByte = p.buffGPalette;
265
                b = new GdalBuffer();
266
                b.buffByte = p.buffBPalette;
267
        }else{
268
                a.buffByte = new byte[w];
269
                        r = bandR.readRaster(x, y, w, 1, w, 1, dataType);
270
                        if (bandG != null)
271
                            g = bandG.readRaster(x, y, w, 1, w, 1, dataType);
272
                        if (bandB != null)
273
                            b = bandB.readRaster(x, y, w, 1, w, 1, dataType);
274
        }
275
        
276
                  lastReadLine += step;
277
                  
278
                  int i=0;
279
                  float j =0F;
280
                  
281
                  if (dataType == GDT_CInt16 || dataType == GDT_Int16  || dataType == GDT_UInt16){
282
                          if (g == null){ // Sibgle Band (Typical DEM)
283
                                  for (int k=0; k<4; k++){
284
                                          for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
285
                                                  if(k<3)
286
                                                          line[i][k] = (r.buffShort[(int) j] & 0xffff);
287
                                                  else
288
                                                          line[i][3] = 0xff;
289
                                          }
290
                              }
291
                          }else { // Multiband
292
                                  //System.err.println("readLine(): Raster 16bits multibanda");
293
                                  GdalBuffer [] bands = {r,g,b};
294
                                  for (int k=0; k<4; k++){
295
                                      for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step){
296
                                              if(k<3)
297
                                                      line[i][k] = (bands[k].buffShort[(int) j] & 0xffff);
298
                                              else
299
                                                      line[i][3] = 0xff;
300
                                      }
301
                                  }
302
                          }
303
                  }else if(dataType == GDT_Float32){
304
                          GdalBuffer [] bands = {r,g,b};
305
                        for (int k=0; k<4; k++){
306
                              for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step){
307
                                      //Truncamos el float perdiendo centimetros. Es necesario para visualizar
308
                                      if(k < 3)
309
                                              line[i][k] = (int)bands[0].buffFloat[(int) j];
310
                                      else
311
                                              line[i][3] = 0xff;
312
                              }
313
                        }
314
                  }
315
                  
316
                return;
317
        }
318
        
319
        int readLineRGBA(int [] line) throws GdalException {
320
                int err = 0;
321
                
322
        int w = (int) (Math.ceil(((double)currentViewWidth)*step) + 1);
323
        int x = (int) Math.ceil(currentViewX);
324
        int y = (int) Math.ceil(lastReadLine);
325
        GdalBuffer r = null, g = null, b = null, p = null;
326
        GdalBuffer a = new GdalBuffer();
327
        
328
        //if (alpha > 0) a = alpha << 24;
329
        if (x+w > bandR.getRasterBandXSize()) 
330
                w = bandR.getRasterBandXSize()-x;
331
        
332
        if(bandR.getRasterColorTable() != null){
333
                p = bandR.readRasterWithPalette(x, y, w, 1, w, 1, dataType);
334
                a.buffByte = p.buffAPalette;
335
                r = new GdalBuffer();
336
                r.buffByte = p.buffRPalette;
337
                g = new GdalBuffer();
338
                g.buffByte = p.buffGPalette;
339
                b = new GdalBuffer();
340
                b.buffByte = p.buffBPalette;
341
        }else{
342
                if(getRasterCount() == 4 && shortName.equals("PNG")){
343
                        a = bandA.readRaster(x, y, w, 1, w, 1, GDT_Byte);
344
                        r = bandR.readRaster(x, y, w, 1, w, 1, dataType);
345
                            if (bandG != null)
346
                                g = bandG.readRaster(x, y, w, 1, w, 1, dataType);
347
                            if (bandB != null)
348
                                b = bandB.readRaster(x, y, w, 1, w, 1, dataType);        
349
                }else{
350
                        r = bandR.readRaster(x, y, w, 1, w, 1, dataType);
351
                            if (bandG != null)
352
                                g = bandG.readRaster(x, y, w, 1, w, 1, dataType);
353
                            if (bandB != null)
354
                                b = bandB.readRaster(x, y, w, 1, w, 1, dataType);
355
                            a.buffByte = new byte[w];
356
                            for (int i = 0;i < w;i++)
357
                                    a.buffByte[i] = (byte)255;
358
                }
359
        }
360

    
361
                  lastReadLine += step;
362
          
363
                  
364
                  int i=0;
365
                  double j =  Math.abs(currentViewX - ((int)currentViewX));
366
                int alpha = (this.alpha & 0xff) << 24;
367
                //try{
368
                  if (dataType == GDT_Byte){
369
                          if (g != null)
370
                              for (i=0; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
371
                                      int jInt = (int)(j);
372
                                      line[i] = (alpha & ((a.buffByte[jInt])& 0xff) << 24) + ((r.buffByte[jInt] & 0xff) << 16) + ((g.buffByte[jInt] & 0xff) << 8) + (b.buffByte[jInt] & 0xff);
373
                              }
374
                      else
375
                              for (i=0; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
376
                                      int jInt = (int)(j);
377
                                      line[i] = (alpha & ((a.buffByte[jInt])& 0xff) << 24) + ((r.buffByte[jInt] & 0xff) << 16) + ((r.buffByte[jInt] & 0xff) << 8) + (r.buffByte[jInt] & 0xff);
378
                              }
379
                  }else if (dataType == GDT_CInt16 || dataType == GDT_Int16  || dataType == GDT_UInt16){
380
                          if (g == null) // Sibgle Band (Typical DEM)
381
                              /*for (i=0, j=0F, i2 = 1; i<currentViewWidth && i2<r.length;
382
                                      i++, j+=step, i2 = (((int) j)*2)+1) {
383
                                      line[i] = a + ((r[i2-1]) << 8) + r[i2];
384
                              }*/
385
                                    for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
386
                                            int jInt = (int)(j);
387
                                      line[i] = (alpha & ((a.buffByte[jInt])& 0xff) << 24) + r.buffShort[jInt];
388
                              }
389
                          else { // Multiband
390
                                  // System.err.println("Raster 16bits multibanda");
391
                              for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
392
                                      int jInt = (int)(j);
393
                                      line[i] = (alpha & ((a.buffByte[jInt])& 0xff) << 24) | (((r.buffShort[jInt] & 0xfff0) << 12) & 0xff0000 ) | 
394
                                                                                                                           (((g.buffShort[jInt] & 0xfff0) << 4 ) & 0xff00 ) |
395
                                                                                                                           (((b.buffShort[jInt] & 0xfff0) >> 4 ) & 0xff );
396
                              }
397
                          }
398
                  }
399
                //}catch(ArrayIndexOutOfBoundsException ex){}
400
                return err;
401
        }
402
                
403
        /**
404
         * Lee una franja de la imagen.
405
         * @param bandH Altura de la franja
406
         * @param bufH        Altura del buffer
407
         * @param buf        Buffer con la franja (retorno)
408
         * @return
409
         * @throws GdalException
410
         */
411
        public int readBandRGBA(int bandH, int bufH, int [] buf) throws GdalException {
412
                int err = 0;
413
        int w = (int)(((double)currentViewWidth)*step);
414
        int x = (int)(((double)currentViewX)*step);
415
        int y = (int) lastReadLine;
416
        int h = (int) (((double)bandH)*step);
417
        System.out.println("Leyendo "+y);
418
        GdalBuffer r = null, g = null, b = null, p = null;
419
        GdalBuffer a = new GdalBuffer();
420
        
421
        if (x+w > bandR.getRasterBandXSize()) 
422
                w = bandR.getRasterBandXSize()-x;
423
        
424
        if(bandR.getRasterColorTable() != null){
425
                p = bandR.readRasterWithPalette(x, y, w, h, w, h, GDT_Byte);
426
                a.buffByte = p.buffAPalette;
427
                r = new GdalBuffer();
428
                r.buffByte = p.buffRPalette;
429
                g = new GdalBuffer();
430
                g.buffByte = p.buffGPalette;
431
                b = new GdalBuffer();
432
                b.buffByte = p.buffBPalette;
433
        }else{
434
                if(getRasterCount() == 4 && shortName.equals("PNG")){
435
                        a = bandA.readRaster(x, y, w, h, w, h, GDT_Byte);
436
                        r = bandR.readRaster(x, y, w, h, w, h, dataType);
437
                            if (bandG != null)
438
                                g = bandG.readRaster(x, y, w, h, w, h, dataType);
439
                            if (bandB != null)
440
                                b = bandB.readRaster(x, y, w, h, w, h, dataType);
441
                }else{
442
                        r = bandR.readRaster(x, y, w, h, w, h, dataType);
443
                            if (bandG != null)
444
                                g = bandG.readRaster(x, y, w, h, w, h, dataType);
445
                            if (bandB != null)
446
                                b = bandB.readRaster(x, y, w, h, w, h, dataType);
447
                            a.buffByte = new byte[w];
448
                            for (int i = 0;i < w*h;i++)
449
                                    a.buffByte[i] = (byte)255;
450
                }
451
        }
452
                  lastReadLine += ((double)bandH)*step;
453
                  
454
                  // TODO Acabar de implementarlo
455
                  float k=0F;
456
                int alpha = (this.alpha & 0xff) << 24;
457
                  for (int j=0, t=0; j<bandH; j++) {
458
                          k = j*w; t=j*currentViewWidth;
459
                          for (int i=0; i<currentViewWidth && k<r.getSize(); i++, k+=step) {
460
                                  buf[t+i] = (alpha & ((a.buffByte[(int)j])& 0xff) << 24) + ((r.buffByte[(int) k]) << 16) + ((g.buffByte[(int) k]) << 8) + b.buffByte[(int) k];
461
                          }
462
                  }
463
                
464
                return err;
465
                
466
        }
467

    
468
        void pintaInfo() {
469
                try {
470
                        //System.out.println("Origin = "+originX+","+originY);
471
                        //System.out.println("Origin = "+this.);
472
                        System.out.println("GeoTransform:");
473
                        GeoTransform trans = getGeoTransform();
474
                        for (int i=0; i<6; i++)
475
                                System.out.println("  param["+i+"]="+trans.adfgeotransform[i]);
476
                        System.out.println("Metadata:");
477
                        String [] metadata = getMetadata();
478
                        for (int i=0; i<metadata.length; i++) {
479
                                System.out.println(metadata[i]);
480
                        }
481
                } catch (GdalException e) {
482
                        
483
                }
484
                
485
        }
486
        
487
        void pintaPaleta() {
488
        }
489
        
490
        public int getBlockSize(){
491
                return this.getBlockSize();
492
        }
493
}
494

    
495
/**
496
 * @author Luis W. Sevilla
497
 */
498
public class GdalFile extends GeoRasterFile {
499
        public final static int         BAND_HEIGHT = 64;
500
        protected GdalNative                 file = null;
501

    
502
        private Extent v = null;
503
        
504
        static {
505
                GeoRasterFile.registerExtension("bmp", GdalFile.class);
506
                GeoRasterFile.registerExtension("gif", GdalFile.class);
507
                GeoRasterFile.registerExtension("img", GdalFile.class);
508
                GeoRasterFile.registerExtension("tif", GdalFile.class);
509
                GeoRasterFile.registerExtension("tiff", GdalFile.class);
510
                GeoRasterFile.registerExtension("jpg", GdalFile.class);
511
                GeoRasterFile.registerExtension("png", GdalFile.class);
512
        }
513
        
514
        public GdalFile(IProjection proj, String fName){
515
                super(proj, fName);
516
                extent = new Extent();
517
                try {
518
                        file = new GdalNative(fName);
519
                        load();
520
                        showOnOpen();
521
                        bandCount = file.getRasterCount(); 
522
                        if ( bandCount > 2) {
523
                                setBand(RED_BAND,   0);
524
                                setBand(GREEN_BAND, 1);
525
                                setBand(BLUE_BAND,  2);
526
                        } else
527
                                setBand(RED_BAND|GREEN_BAND|BLUE_BAND, 0);
528
                } catch(Exception e){
529
                          System.out.println("Error en GdalOpen");
530
                          e.printStackTrace();
531
                          file = null;
532
                }
533
                
534
                switch(file.getDataType()){
535
                case 1:setDataType(DataBuffer.TYPE_BYTE);break;//GDT_BYTE 
536
                case 2://GDT_UInt16 
537
                case 3:setDataType(DataBuffer.TYPE_SHORT);break;//GDT_Int16        
538
                case 4://GDT_UInt32
539
                case 5:setDataType(DataBuffer.TYPE_INT);break;//GDT_Int32
540
                case 6:setDataType(DataBuffer.TYPE_FLOAT);break;//GDT_Float32
541
                case 7:setDataType(DataBuffer.TYPE_DOUBLE);break;//GDT_Float64
542
                case 8:setDataType(DataBuffer.TYPE_UNDEFINED);break;//GDT_CInt16
543
                case 9:setDataType(DataBuffer.TYPE_UNDEFINED);break;//GDT_CInt32
544
                case 10:setDataType(DataBuffer.TYPE_UNDEFINED);break;//GDT_CFloat32
545
                case 11:setDataType(DataBuffer.TYPE_UNDEFINED);break;//GDT_CFloat64
546
                }
547
                
548
        }
549
        
550
        /**
551
         * Obtenemos o calculamos el extent de la imagen.
552
         */
553
        public GeoFile load() {
554
        
555
                extent = new Extent(file.esq.minX, file.esq.minY, file.esq.maxX, file.esq.maxY);        
556
                
557
                return this;
558
        }
559
        
560
        /**
561
         * Cierra el fichero de imagen
562
         */
563
        public void close() {
564
                try {
565
                        if(file != null){
566
                                file.close();
567
                                file = null;
568
                        }
569
                } catch (GdalException e) {
570
                        // TODO Auto-generated catch block
571
                        e.printStackTrace();
572
                }
573
        }
574
        
575
        /**
576
         * Asigna a cada banda R,G o B una banda de la imagen
577
         */
578
        public void setBand(int flag, int bandNr) {
579
                super.setBand(flag, bandNr);
580
                if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) file.rBandNr = bandNr+1;
581
                if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) file.gBandNr = bandNr+1;
582
                if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) file.bBandNr = bandNr+1;
583
        }
584
        
585
        /**
586
         * Asigna el extent de la vista actual
587
         */
588
        public void setView(Extent e) { 
589
                v = new Extent(e); 
590
        }
591
        
592
        /**
593
         * Obtiene extent de la vista actual
594
         */
595
        public Extent getView() { 
596
                return v; 
597
        }
598
        
599
        /**
600
         * Obtiene la anchura del fichero
601
         */
602
        public int getWidth() {        
603
                return file.width; 
604
        }
605
        
606
        /**
607
         * Obtiene la altura del fichero
608
         */
609
        public int getHeight() { 
610
                return file.height;
611
        }
612

    
613
        /* (non-Javadoc)
614
         * @see org.cresques.io.GeoRasterFile#reProject(org.cresques.cts.ICoordTrans)
615
         */
616
        public void reProject(ICoordTrans rp) {
617
                // TODO Auto-generated method stub
618
                
619
        }
620
        
621
        /* (non-Javadoc)
622
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans)
623
         */
624
        public Image updateImage(int width, int height, ICoordTrans rp) {
625
                int line, pRGBArray[] = null;
626
                Image image = null;
627
                        
628
                if (mustVerifySize()) {
629
                        // Work out the correct aspect for the setView call.
630
                        double dFileAspect = (double)v.width()/(double)v.height();
631
                        double dWindowAspect = (double)width /(double)height;
632
        
633
                        if (dFileAspect > dWindowAspect) {
634
                          height =(int)((double)width/dFileAspect);
635
                        } else {
636
                          width = (int)((double)height*dFileAspect);
637
                        }
638
                }
639
                
640
                // Set the view
641
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),
642
                        width, height);
643
                
644
                if(width<=0)width=1;
645
                if(height<=0)height=1;
646
                
647
                image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
648
                //image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
649
                pRGBArray = new int[width/**BAND_HEIGHT*/];
650
                try {
651
                        //int nLin = height % BAND_HEIGHT;
652
                        file.setAlpha(getAlpha());
653
                        setBand(RED_BAND,   rBandNr);
654
                        setBand(GREEN_BAND, gBandNr);
655
                        setBand(BLUE_BAND,  bBandNr);
656
                        for (line=0; line < height; line++) { //+=BAND_HEIGHT) {
657
                                //int bandH = Math.min(BAND_HEIGHT, height-line);
658
                                //file.readBandRGBA(bandH, BAND_HEIGHT, pRGBArray);
659
                                file.readLineRGBA(pRGBArray);
660
                                setRGBLine((BufferedImage) image, 0, line, width, 1/*bandH*/, pRGBArray, 0, width);
661
                        }
662
                } catch (Exception e) {
663
                        // TODO Auto-generated catch block
664
                        e.printStackTrace();
665
                }
666
                
667
                return image;
668
        }
669
        
670
        public RasterBuf getRaster(int width, int height, ICoordTrans rp) {
671
                int line;
672
                RasterBuf raster = null;
673
                        
674
                if(mustVerifySize()){
675
                        // Work out the correct aspect for the setView call.
676
                        double dFileAspect = (double)v.width()/(double)v.height();
677
                        double dWindowAspect = (double)width /(double)height;
678
        
679
                        if (dFileAspect > dWindowAspect) {
680
                          height =(int)((double)width/dFileAspect);
681
                        } else {
682
                          width = (int)((double)height*dFileAspect);
683
                        }
684
                }
685
                
686
                // Set the view
687
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),
688
                        width, height);
689
                
690
                raster = new RasterBuf(DataBuffer.TYPE_INT, width, height, 4, new Point(0,0));
691
                try {
692
                        //int nLin = height % BAND_HEIGHT;
693
                        file.setAlpha(getAlpha());
694
                        setBand(RED_BAND,   rBandNr);
695
                        setBand(GREEN_BAND, gBandNr);
696
                        setBand(BLUE_BAND,  bBandNr);
697
                        for (line=0; line < height; line++) { //+=BAND_HEIGHT) {
698
                                file.readLine(raster.getLineInt(line));
699
                        }
700
                } catch (Exception e) {
701
                        // TODO Auto-generated catch block
702
                        e.printStackTrace();
703
                }
704
                
705
                return raster;
706
        }
707
        
708
        /**
709
         * Asigna al objeto Image los valores con los dato de la imagen contenidos en el 
710
         * vector de enteros.
711
         * @param image        imagen con los datos actuales
712
         * @param startX        inicio de la posici?n en X dentro de la imagen
713
         * @param startY        inicio de la posici?n en X dentro de la imagen
714
         * @param w        Ancho de la imagen
715
         * @param h        Alto de la imagen
716
         * @param rgbArray        vector que contiene la banda que se va a sustituir
717
         * @param offset        desplazamiento
718
         * @param scansize        tama?o de imagen recorrida por cada p
719
         */
720
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, 
721
                         int offset, int scansize) {
722
                image.setRGB(startX, startY, w, h, rgbArray, offset, scansize);
723
        }
724
        
725
        /**
726
         * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores 
727
         * con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
728
         * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
729
         * banda correspondiente a flags es sustituida por los datos del vector.
730
         * @param image        imagen con los datos actuales
731
         * @param startX        inicio de la posici?n en X dentro de la imagen
732
         * @param startY        inicio de la posici?n en X dentro de la imagen
733
         * @param w        Ancho de la imagen
734
         * @param h        Alto de la imagen
735
         * @param rgbArray        vector que contiene la banda que se va a sustituir
736
         * @param offset        desplazamiento
737
         * @param scansize        tama?o de imagen recorrida por cada paso
738
         * @param flags        banda que se va a sustituir (Ctes de GeoRasterFile)
739
         */
740
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, 
741
                         int offset, int scansize, int flags) {
742
                int [] line = new int[rgbArray.length]; 
743
                image.getRGB(startX, startY, w, h, line, offset, scansize);
744
                if (flags == GeoRasterFile.RED_BAND)
745
                        for (int i=0; i<line.length; i++)
746
                                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
747
                else if (flags == GeoRasterFile.GREEN_BAND)
748
                        for (int i=0; i<line.length; i++)
749
                                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
750
                else if (flags == GeoRasterFile.BLUE_BAND)
751
                        for (int i=0; i<line.length; i++)
752
                                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
753
                image.setRGB(startX, startY, w, h, line, offset, scansize);
754
        }
755
        
756
        /**
757
         * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores 
758
         * con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
759
         * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
760
         * banda correspondiente a flags es sustituida por los datos del vector.
761
         * @param image        imagen con los datos actuales
762
         * @param startX        inicio de la posici?n en X dentro de la imagen
763
         * @param startY        inicio de la posici?n en X dentro de la imagen
764
         * @param w        Ancho de la imagen
765
         * @param h        Alto de la imagen
766
         * @param rgbArray        vector que contiene la banda que se va a sustituir
767
         * @param offset        desplazamiento
768
         * @param scansize        tama?o de imagen recorrida por cada paso
769
         * @param origBand        Banda origen del GeoRasterFile
770
         * @param destBandFlag        banda que se va a sustituir (Ctes de GeoRasterFile)
771
         */
772
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, 
773
                         int offset, int scansize, int origBand, int destBandFlag) {
774
                int [] line = new int[rgbArray.length]; 
775
                image.getRGB(startX, startY, w, h, line, offset, scansize);
776
                if (origBand == 0 && destBandFlag == GeoRasterFile.RED_BAND)
777
                        for (int i=0; i<line.length; i++)
778
                                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
779
                else if (origBand == 1 && destBandFlag == GeoRasterFile.GREEN_BAND)
780
                        for (int i=0; i<line.length; i++)
781
                                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
782
                else if (origBand == 2 && destBandFlag == GeoRasterFile.BLUE_BAND)
783
                        for (int i=0; i<line.length; i++)
784
                                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
785
                
786
                else if (origBand == 0 && destBandFlag == GeoRasterFile.GREEN_BAND)
787
                        for (int i=0; i<line.length; i++)
788
                                line[i] = (line[i] & 0xffff00ff) | ((rgbArray[i] & 0x00ff0000) >> 8) ;
789
                else if (origBand == 0 && destBandFlag == GeoRasterFile.BLUE_BAND)
790
                        for (int i=0; i<line.length; i++)
791
                                line[i] = (line[i] & 0xffffff00) | ((rgbArray[i] & 0x00ff0000) >> 16);
792
                else if (origBand == 1 && destBandFlag == GeoRasterFile.RED_BAND)
793
                        for (int i=0; i<line.length; i++)
794
                                line[i] = (line[i] & 0xff00ffff) | ((rgbArray[i] & 0x0000ff00) << 8);
795
                
796
                else if (origBand == 1 && destBandFlag == GeoRasterFile.BLUE_BAND)
797
                        for (int i=0; i<line.length; i++)
798
                                line[i] = (line[i] & 0xffffff00) | ((rgbArray[i] & 0x0000ff00) >> 8);
799
                else if (origBand == 2 && destBandFlag == GeoRasterFile.RED_BAND)
800
                        for (int i=0; i<line.length; i++)
801
                                line[i] = (line[i] & 0xff00ffff) | ((rgbArray[i] & 0x000000ff) << 16);
802
                else if (origBand == 2 && destBandFlag == GeoRasterFile.GREEN_BAND)
803
                        for (int i=0; i<line.length; i++)
804
                                line[i] = (line[i] & 0xffff00ff) | ((rgbArray[i] & 0x000000ff) << 8);
805
                image.setRGB(startX, startY, w, h, line, offset, scansize);
806
        }
807
        
808
        private void showOnOpen() {
809
                  // Report en la apertura (quitar)
810
                  System.out.println("Fichero GDAL '"+getName()+"' abierto.");
811
                  System.out.println("Version = "+file.version);
812
                  System.out.println("   Size = ("+file.width+","+file.height+")");
813
                  try {
814
                        System.out.println("   NumBands = ("+file.getRasterCount()+")");
815
                } catch (GdalException e) {
816
                        // TODO Auto-generated catch block
817
                        e.printStackTrace();
818
                }
819
                  //file.pintaInfo();
820
                  file.pintaPaleta();
821

    
822
        }
823

    
824
        /* (non-Javadoc)
825
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans, java.awt.Image, int, int)
826
         */
827
        public Image updateImage(int width, int height, ICoordTrans rp, Image img, int origBand, int destBandFlag){
828
                int line, pRGBArray[] = null;
829
                        
830
                if(mustVerifySize()){
831
                        // Work out the correct aspect for the setView call.
832
                        double dFileAspect = (double)v.width()/(double)v.height();
833
                        double dWindowAspect = (double)width /(double)height;
834
        
835
                        if (dFileAspect > dWindowAspect) {
836
                          height =(int)((double)width/dFileAspect);
837
                        } else {
838
                          width = (int)((double)height*dFileAspect);
839
                        }
840
                }
841
                
842
                // Set the view
843
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),
844
                        width, height);
845
                
846
                if(width<=0)width=1;
847
                if(height<=0)height=1;
848
                
849
                pRGBArray = new int[width/**BAND_HEIGHT*/];
850
                try {
851
                        setBand(RED_BAND,   rBandNr);
852
                        setBand(GREEN_BAND, gBandNr);
853
                        setBand(BLUE_BAND,  bBandNr);
854
                        file.setAlpha(getAlpha());
855
                        if(img!=null){
856
                                for (line=0; line < height; line++) { 
857
                                        file.readLineRGBA(pRGBArray);
858
                                        setRGBLine((BufferedImage) img, 0, line, width, 1/*bandH*/, pRGBArray, 0, width, origBand, destBandFlag);
859
                                }
860
                                return img;
861
                        }else{
862
                                Image image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
863
                                for (line=0; line < height; line++) { 
864
                                        file.readLineRGBA(pRGBArray);
865
                                        setRGBLine((BufferedImage) image, 0, line, width, 1/*bandH*/, pRGBArray, 0, width);
866
                                }
867
                                return image;
868
                        }
869
                } catch (Exception e) {
870
                        // TODO Auto-generated catch block
871
                        e.printStackTrace();
872
                }
873
                
874
                return img;
875
        }
876
        
877
        /* (non-Javadoc)
878
         * @see org.cresques.io.GeoRasterFile#getData(int, int, int)
879
         */
880
        public Object getData(int x, int y, int band) {
881
                // TODO Auto-generated method stub
882
                return null;
883
        }
884
        
885
        /**
886
         * Devuelve los datos de una ventana solicitada
887
         * @param ulX        coordenada X superior izda.
888
         * @param ulY        coordenada Y superior derecha.
889
         * @param sizeX        tama?o en X de la ventana.
890
         * @param sizeY tama?o en Y de la ventana.
891
         * @param band        Banda solicitada.
892
         */
893
        public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band){
894
                
895
                return null;
896
        }
897
        
898
        /**
899
         * Obtiene la zona (Norte / Sur)
900
         * @return true si la zona es norte y false si es sur
901
         */
902
        
903
        public boolean getZone(){
904
                
905
                return false;
906
        }
907
        
908
        /**
909
         *Devuelve el n?mero de zona UTM
910
         *@return N?mero de zona 
911
         */
912
        
913
        public int getUTM(){
914
                
915
                return 0;        
916
        }
917
        
918
        /**
919
         * Obtiene el sistema de coordenadas geograficas
920
         * @return Sistema de coordenadas geogr?ficas
921
         */
922
        public String getGeogCS(){
923
                
924
                return new String("");        
925
        }
926
        /**
927
         * Devuelve el tama?o de bloque
928
         * @return Tama?o de bloque
929
         */
930
        public int getBlockSize(){
931
     //TODO Nacho: Implementar getBlockSize de EcwFile        
932
          return file.getBlockSize();
933
        }
934
}
935

    
936