Statistics
| Revision:

svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / io / GdalFile.java @ 2004

History | View | Annotate | Download (25.5 KB)

1
/*
2
 * Created on 25-sep-2004 (20-oct-2004)
3
 */
4
package org.cresques.io;
5

    
6
import java.awt.Color;
7
import java.awt.Image;
8
import java.awt.Point;
9
import java.awt.geom.Point2D;
10
import java.awt.image.BufferedImage;
11
import java.awt.image.DataBuffer;
12
import java.io.IOException;
13
import java.util.Vector;
14

    
15
import org.cresques.cts.ICoordTrans;
16
import org.cresques.cts.IProjection;
17
import org.cresques.io.raster.RasterBuf;
18
import org.cresques.px.Extent;
19

    
20
import es.gva.cit.jgdal.Gdal;
21
import es.gva.cit.jgdal.GdalBuffer;
22
import es.gva.cit.jgdal.GdalException;
23
import es.gva.cit.jgdal.GdalRasterBand;
24
import es.gva.cit.jgdal.GeoTransform;
25
/**
26
 * Soporte 'nativo' para ficheros desde GDAL.
27
 * Este conjunto de funcionalidades est? tomado de manera casi literal
28
 * del soporte para ECW de ermapper.<br>
29
 * Probablemente esto deber?a formar parte del JNI que recubre a la
30
 * librer?a en C extraida de gdal.<br>
31
 * Lo pongo aqu? a manera de ejemplo de como atacar un formato binario
32
 * desde Java.<br><br>   
33
 * @author Luis W. Sevilla.
34
 */
35

    
36
class GdalNative extends Gdal {
37
        static boolean WITH_OVERVIEWS = true;
38
        // Polilinea con extent
39
        class Contour extends Vector {
40
                public double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE;
41
                public double maxX = -Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
42
                public Contour() {
43
                        super();
44
                }
45
                public void add(Point2D pt) {
46
                        super.add(pt);
47
                        if (pt.getX() > maxX) maxX = pt.getX();
48
                        if (pt.getX() < minX) minX = pt.getX();
49
                        if (pt.getY() > maxY) maxY = pt.getY();
50
                        if (pt.getY() < minY) minY = pt.getY();
51
                }
52
        }
53
        /**
54
         * Contorno en coordenadas geogr?ficas. (y Extent del raster).
55
         */
56
        public Contour esq = new Contour();
57
        public int width = 0, height = 0;
58
        public double originX = 0D, originY = 0D;
59
        public String version = "";
60

    
61
        private int alpha = 0;
62
        protected int rBandNr = 1, gBandNr = 2, bBandNr = 3;
63
        private int dataType = GDT_Byte;
64
        
65
        public GdalNative(String fName) throws GdalException, IOException {
66
                super();
67
                init(fName);
68
        }
69
        
70
        private void init(String fName) throws GdalException, IOException {
71
                open(fName,GA_ReadOnly);
72
                String ext = fName.toLowerCase().substring(fName.lastIndexOf('.')+1);
73
                if (ext.compareTo("tif") == 0)
74
                        WITH_OVERVIEWS = false;
75
                width = getRasterXSize();
76
                height = getRasterYSize();
77
                if (true) { //ext.compareTo("sid") == 0) {
78
                        String [] metadata = getMetadata();
79
                        double ox=0D, oy=0D, resx=0D, resy=0D;
80
                        
81
                        GeoTransform trans = getGeoTransform();
82
                        ox = trans.adfgeotransform[0];
83
                        oy = trans.adfgeotransform[3];
84
                        resx = trans.adfgeotransform[1];
85
                        resy = trans.adfgeotransform[5];
86
                        
87
                        System.out.println("Origin = ("+ox+","+oy+")");
88
                        System.out.println("Pixel Size = ("+resx+","+resy+")");
89
                          esq.add(new Point2D.Double(ox, oy));
90
                          esq.add(new Point2D.Double(ox+resx*width, oy));
91
                          esq.add(new Point2D.Double(ox, oy+resy*height));
92
                          esq.add(new Point2D.Double(ox+resx*width, oy+resy*height));
93
                } else  { //version.startsWith("1")) {
94
                          //double [] transParam = getGeoTransform();
95
                          esq.add(new Point2D.Double(0D,height));
96
                          esq.add(new Point2D.Double(0D,0D));
97
                          esq.add(new Point2D.Double(width,0D));
98
                          esq.add(new Point2D.Double(width,height));
99
                  }
100
                setDataType(this.getRasterBand(1).getRasterDataType());
101
        }
102
        
103
        public void setAlpha(int a) { alpha = a; }
104
        
105
        public void setDataType(int dt) { dataType = dt; }
106
        public int getDataType() { return dataType; }
107
        
108
        double lastReadLine = -1;
109
        int currentFullWidth = -1;
110
        int currentFullHeight = -1;
111
        int currentViewWidth = -1;
112
        int currentViewHeight = -1;
113
        double currentViewX = 0D;
114
        double viewportScale = 0D;
115
        double step = 0D;
116
        int currentOverview = -1;
117
        
118
        protected GdalRasterBand bandR=null, bandG=null, bandB=null;
119
        
120
        
121
        /**
122
         * Devuelve la banda actualmente en uso para el color especificado.
123
         * @param color 0=Rojo, 1=Green, 2=Blue.
124
         * @return
125
         */
126
        public GdalRasterBand getCurrentBand(int color) {
127
                if (color == 0) return bandR;
128
                else if (color == 1) return bandG;
129
                return bandB;
130
        }
131
        // Supone rasters no girados
132
        public Point2D worldToRaster(Point2D pt) {
133
                double x = (((double) currentFullWidth)/(esq.maxX-esq.minX))*(pt.getX()-esq.minX);
134
                double y = (((double) currentFullHeight)/(esq.maxY-esq.minY))*(esq.maxY-pt.getY());
135
                Point2D ptRes = new Point2D.Double(x, y);
136
                return ptRes;
137
        }
138
        
139
        public int setView(double dWorldTLX, double dWorldTLY,
140
            double dWorldBRX, double dWorldBRY,
141
            int nWidth, int nHeight) {
142
                int err = 0;
143
                currentFullWidth = width;
144
                currentFullHeight = height;
145
                Point2D tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
146
                Point2D br = worldToRaster(new Point2D.Double(dWorldBRX, dWorldBRY));
147
                // Calcula cual es la primera l?nea a leer;
148
                currentViewWidth = nWidth;
149
                currentViewHeight = nHeight;
150

    
151
                currentViewX = tl.getX();
152
                viewportScale = (double) currentViewWidth/(br.getX()-tl.getX());
153
                step = 1D/viewportScale;
154
                lastReadLine = tl.getY();
155
                try {
156
                        // calcula el overview a usar
157
                        bandR = getRasterBand(1);
158
                        currentOverview = -1;
159
                        if (WITH_OVERVIEWS && bandR.getOverviewCount() > 0) {
160
                                GdalRasterBand ovb = null;
161
                                for (int i=bandR.getOverviewCount()-1; i>0; i--) {              
162
                                        ovb = bandR.getOverview(i);
163
                                        if (ovb.getRasterBandXSize()>getRasterXSize()*viewportScale) {
164
                                                currentOverview = i;
165
                                    viewportScale *= ((double) width/(double) ovb.getRasterBandXSize());
166
                                    step = 1D/viewportScale;
167
                                    currentFullWidth = ovb.getRasterBandXSize();
168
                                    currentFullHeight = ovb.getRasterBandYSize();
169
                                    tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
170
                                    currentViewX = tl.getX();
171
                                    lastReadLine = tl.getY();
172
                                    break;
173
                                        }
174
                                }
175
                        }
176
        
177
                        // Selecciona las bandas y los overviews necesarios
178
                        bandR = getRasterBand(rBandNr);
179
                        setDataType(bandR.getRasterDataType());
180
                        if (this.getRasterCount() > 1) {
181
                                bandG = getRasterBand(gBandNr);
182
                                bandB = getRasterBand(bBandNr);
183
                        }
184
                        if (currentOverview > 0) {
185
                                bandR = bandR.getOverview(currentOverview);
186
                            if (this.getRasterCount() > 1) {
187
                                        bandG = bandG.getOverview(currentOverview);
188
                                        bandB = bandB.getOverview(currentOverview);
189
                                }
190
                        }
191

    
192
                        //System.out.println(band.)
193
                } catch (GdalException e) {
194
                        // TODO Auto-generated catch block
195
                        e.printStackTrace();
196
                }
197

    
198
                System.out.println("GdalFile: TL=("+dWorldTLX+","+dWorldTLY+
199
                        "); BR=("+dWorldBRX+","+dWorldBRY+")\n"+
200
                        "GdalFile: escala="+viewportScale+"; lastReadLine="+lastReadLine+"\n"+
201
                    "Actual Raster Size="+currentFullWidth+"x"+currentFullHeight+
202
                        "\nDataType="+dataType);
203
                return err;
204
        }
205
        
206
        int lastY = -1;
207
        
208
        public void readLine(int[][] line) throws GdalException {
209
                int err = 0;
210
                int nbands = getRasterCount();
211
                GdalRasterBand band = null;
212
        int w = (int)(((double)currentViewWidth)*step);
213
        int x = (int) currentViewX;
214
        int y = (int) lastReadLine;
215
        GdalBuffer r = null, g = null, b = null;
216
        //if (alpha > 0) a = alpha << 24;
217
        if (x+w > bandR.getRasterBandXSize()) 
218
                w = bandR.getRasterBandXSize()-x;
219
                r = bandR.readRaster(x, y, w, 1, w, 1, dataType);
220
                if (bandG != null)
221
                    g = bandG.readRaster(x, y, w, 1, w, 1, dataType);
222
                if (bandB != null)
223
                    b = bandB.readRaster(x, y, w, 1, w, 1, dataType);
224

    
225
                  lastReadLine += step;
226
                  
227
                  int white = Color.BLUE.getRGB(), i2=0, i=0;
228
                  float j =0F;
229
                  
230
                  if (dataType == GDT_CInt16 || dataType == GDT_Int16  || dataType == GDT_UInt16)
231
                          if (g == null) // Sibgle Band (Typical DEM)
232
                              /*for (i=0, j=0F, i2 = 1; i<currentViewWidth && i2<r.length;
233
                                      i++, j+=step, i2 = (((int) j)*2)+1) {
234
                                      line[i] = a + ((r[i2-1]) << 8) + r[i2];
235
                              }*/
236
                                  for (int k=0; k<3; k++)
237
                                          for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
238
                                                  line[i][k] = (r.buffShort[(int) j] & 0xffff);
239
                              }
240
                          else { // Multiband
241
                                  short px;
242
                                  //System.err.println("readLine(): Raster 16bits multibanda");
243
                                  GdalBuffer [] bands = {r,g,b};
244
                                  for (int k=0; k<3; k++)
245
                                      for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step)
246
                                              line[i][k] = (bands[k].buffShort[(int) j] & 0xffff);
247
                          }
248
                return;
249
        }
250
        
251
        public int readLineRGBA(int [] line) throws GdalException {
252
                int err = 0;
253
                int nbands = getRasterCount();
254
                GdalRasterBand band = null;
255
        int w = (int)(((double)currentViewWidth)*step);
256
        int x = (int) currentViewX;
257
        int y = (int) lastReadLine;
258
        GdalBuffer r = null, g = null, b = null;
259
        //if (alpha > 0) a = alpha << 24;
260
        if (x+w > bandR.getRasterBandXSize()) 
261
                w = bandR.getRasterBandXSize()-x;
262
                r = bandR.readRaster(x, y, w, 1, w, 1, dataType);
263
                if (bandG != null)
264
                    g = bandG.readRaster(x, y, w, 1, w, 1, dataType);
265
                if (bandB != null)
266
                    b = bandB.readRaster(x, y, w, 1, w, 1, dataType);
267

    
268
                  lastReadLine += step;
269
                  
270
                  int white = Color.BLUE.getRGB(), i2=0, i=0;
271
                  float j =0F;
272
                int alpha = (this.alpha & 0xff) << 24;
273
                  if (dataType == GDT_Byte)
274
                          if (g != null)
275
                              for (i=0; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
276
                                      line[i] = alpha + ((r.buffByte[(int) j] & 0xff) << 16) + ((g.buffByte[(int) j] & 0xff) << 8) + (b.buffByte[(int) j] & 0xff);
277
                              }
278
                      else
279
                              for (i=0; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
280
                                      line[i] = alpha + ((r.buffByte[(int) j] & 0xff) << 16) +
281
                                                ((r.buffByte[(int) j] & 0xff) << 8) + (r.buffByte[(int) j] & 0xff);
282
                              }
283
                  else if (dataType == GDT_CInt16 || dataType == GDT_Int16  || dataType == GDT_UInt16)
284
                          if (g == null) // Sibgle Band (Typical DEM)
285
                              /*for (i=0, j=0F, i2 = 1; i<currentViewWidth && i2<r.length;
286
                                      i++, j+=step, i2 = (((int) j)*2)+1) {
287
                                      line[i] = a + ((r[i2-1]) << 8) + r[i2];
288
                              }*/
289
                                    for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
290
                                      line[i] = alpha + r.buffShort[(int) j];
291
                              }
292
                          else { // Multiband
293
                                  // System.err.println("Raster 16bits multibanda");
294
                              for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
295
                                      line[i] = alpha | (((r.buffShort[(int) j] & 0xfff0) << 12) & 0xff0000 ) | 
296
                                                                          (((g.buffShort[(int) j] & 0xfff0) << 4 ) & 0xff00 ) |
297
                                                                          (((b.buffShort[(int) j] & 0xfff0) >> 4 ) & 0xff );
298
                              }
299
                          }
300
            //for (int i=0; i<currentViewWidth; i++) line[i] = 128+128*256+128*256*256;
301

    
302
                return err;
303
        }
304
        
305
        /**
306
         * Lee una franja de la imagen.
307
         * @param bandH Altura de la franja
308
         * @param bufH        Altura del buffer
309
         * @param buf        Buffer con la franja (retorno)
310
         * @return
311
         * @throws GdalException
312
         */
313
        public int readBandRGBA(int bandH, int bufH, int [] buf) throws GdalException {
314
                int err = 0;
315
                int nbands = getRasterCount();
316
                GdalRasterBand band = null;
317
        int w = (int)(((double)currentViewWidth)*step);
318
        int x = (int)(((double)currentViewX)*step);
319
        int y = (int) lastReadLine;
320
        int h = (int) (((double)bandH)*step);
321
        System.out.println("Leyendo "+y);
322
                
323
        if (x+w > bandR.getRasterBandXSize()) 
324
                w = bandR.getRasterBandXSize()-x;
325
                GdalBuffer r = bandR.readRaster(x, y, w, h, w, h, GDT_Byte);
326
                GdalBuffer g = bandG.readRaster(x, y, w, h, w, h, GDT_Byte);
327
                GdalBuffer b = bandB.readRaster(x, y, w, h, w, h, GDT_Byte);
328

    
329
                  lastReadLine += ((double)bandH)*step;
330
                  
331
                  // TODO Acabar de implementarlo
332
                  float k=0F;
333
                int alpha = (this.alpha & 0xff) << 24;
334
                  for (int j=0, t=0; j<bandH; j++) {
335
                          k = j*w; t=j*currentViewWidth;
336
                          for (int i=0; i<currentViewWidth && k<r.getSize(); i++, k+=step) {
337
                                  buf[t+i] = alpha + ((r.buffByte[(int) k]) << 16) + ((g.buffByte[(int) k]) << 8) + b.buffByte[(int) k];
338
                          }
339
                  }
340
                //for (int i=0; i<currentViewWidth; i++) line[i] = 128+128*256+128*256*256;
341

    
342
                return err;
343
                
344
        }
345

    
346
        void pintaInfo() {
347
                try {
348
                        //System.out.println("Origin = "+originX+","+originY);
349
                        //System.out.println("Origin = "+this.);
350
                        System.out.println("GeoTransform:");
351
                        GeoTransform trans = getGeoTransform();
352
                        for (int i=0; i<6; i++)
353
                                System.out.println("  param["+i+"]="+trans.adfgeotransform[i]);
354
                        System.out.println("Metadata:");
355
                        String [] metadata = getMetadata();
356
                        for (int i=0; i<metadata.length; i++) {
357
                                System.out.println(metadata[i]);
358
                        }
359
                } catch (GdalException e) {
360
                        // TODO Auto-generated catch block
361
                        e.printStackTrace();
362
                }
363
                
364
        }
365
        
366
        void pintaPaleta() {
367
        }
368
        
369
        public int getBlockSize(){
370
                return this.getBlockSize();
371
        }
372
}
373

    
374
/**
375
 * @author Luis W. Sevilla
376
 */
377
public class GdalFile extends GeoRasterFile {
378
        public final static int BAND_HEIGHT = 64;
379
        protected GdalNative file = null;
380

    
381
        private Extent v = null;
382
        
383
        static {
384
                //GeoRasterFile.registerExtension("sid", GdalFile.class);
385
                GeoRasterFile.registerExtension("img", GdalFile.class);
386
                GeoRasterFile.registerExtension("tif", GdalFile.class);
387
                GeoRasterFile.registerExtension("tiff", GdalFile.class);
388
                GeoRasterFile.registerExtension("jpg", GdalFile.class);
389
                GeoRasterFile.registerExtension("png", GdalFile.class);
390
        }
391
        
392
        public GdalFile(IProjection proj, String fName) {
393
                super(proj, fName);
394
                extent = new Extent();
395
                try {
396
                        file = new GdalNative(fName);
397
                        showOnOpen();
398
                        load();
399
                        bandCount = file.getRasterCount(); 
400
                        if ( bandCount > 2) {
401
                                setBand(RED_BAND,   0);
402
                                setBand(GREEN_BAND, 1);
403
                                setBand(BLUE_BAND,  2);
404
                        } else
405
                                setBand(RED_BAND|GREEN_BAND|BLUE_BAND, 0);
406
                } catch(Exception e){
407
                          System.out.println("Error en GdalOpen");
408
                          e.printStackTrace();
409
                          file = null;
410
                }
411
                int dataType = file.getDataType();
412
                  if (dataType == Gdal.GDT_Byte)
413
                          setDataType(DataBuffer.TYPE_BYTE);
414
                  else if (dataType == Gdal.GDT_CInt16 || dataType == Gdal.GDT_Int16)
415
                          setDataType(DataBuffer.TYPE_SHORT);
416
                  else if(dataType == Gdal.GDT_UInt16)
417
                          setDataType(DataBuffer.TYPE_USHORT);
418
        }
419
        
420
        public GeoFile load() {
421
                /*double minX, minY, maxX, maxY;
422
                minX = minY = 0D;
423
                maxX = (double) width;
424
                maxY = (double) height;*/
425
                extent = new Extent(file.esq.minX, file.esq.minY, file.esq.maxX, file.esq.maxY);
426
                return this;
427
        }
428
        
429
        public void close() {
430
                try {
431
                        file.close();
432
                } catch (GdalException e) {
433
                        // TODO Auto-generated catch block
434
                        e.printStackTrace();
435
                }
436
        }
437
        
438
        public void setBand(int flag, int bandNr) {
439
                super.setBand(flag, bandNr);
440
                if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) file.rBandNr = bandNr+1;
441
                if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) file.gBandNr = bandNr+1;
442
                if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) file.bBandNr = bandNr+1;
443
        }
444
        public void setView(Extent e) { v = new Extent(e); }
445
        public Extent getView() { return v; }
446
        
447
        public int getWidth() {        return file.width; }
448
        public int getHeight() { return file.height;}
449

    
450
        /* (non-Javadoc)
451
         * @see org.cresques.io.GeoRasterFile#reProject(org.cresques.cts.ICoordTrans)
452
         */
453
        public void reProject(ICoordTrans rp) {
454
                // TODO Auto-generated method stub
455
                
456
        }
457
        /* (non-Javadoc)
458
         * @see org.cresques.io.GeoRasterFile#setTransparency(boolean)
459
         * /
460
        public void setTransparency(boolean t) {
461
                // TODO Auto-generated method stub
462
        }
463
        public void setTransparency(int t) {
464
                // TODO Auto-generated method stub
465
        }*/
466
        /* (non-Javadoc)
467
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans)
468
         */
469
        public Image updateImage(int width, int height, ICoordTrans rp) {
470
                double dFileAspect, dWindowAspect;
471
                int line, pRGBArray[] = null;
472
                Image image = null;
473

    
474
                // Work out the correct aspect for the setView call.
475
                dFileAspect = (double)v.width()/(double)v.height();
476
                dWindowAspect = (double)width /(double)height;
477

    
478
                if (dFileAspect > dWindowAspect) {
479
                  height =(int)((double)width/dFileAspect);
480
                } else {
481
                  width = (int)((double)height*dFileAspect);
482
                }
483
                
484
                // Set the view
485
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),
486
                        width, height);
487
                
488
                if(width<=0)width=1;
489
                if(height<=0)height=1;
490
                
491
                image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
492
                //image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
493
                pRGBArray = new int[width/**BAND_HEIGHT*/];
494
                try {
495
                        //int nLin = height % BAND_HEIGHT;
496
                        file.setAlpha(getAlpha());
497
                        setBand(RED_BAND,   rBandNr);
498
                        setBand(GREEN_BAND, gBandNr);
499
                        setBand(BLUE_BAND,  bBandNr);
500
                        for (line=0; line < height; line++) { //+=BAND_HEIGHT) {
501
                                //int bandH = Math.min(BAND_HEIGHT, height-line);
502
                                //file.readBandRGBA(bandH, BAND_HEIGHT, pRGBArray);
503
                                file.readLineRGBA(pRGBArray);
504
                                setRGBLine((BufferedImage) image, 0, line, width, 1/*bandH*/, pRGBArray, 0, width);
505
                        }
506
                } catch (Exception e) {
507
                        // TODO Auto-generated catch block
508
                        e.printStackTrace();
509
                }
510
                
511
                return image;
512
        }
513
        
514
        public RasterBuf getRaster(int width, int height, ICoordTrans rp) {
515
                double dFileAspect, dWindowAspect;
516
                int line, pRGBArray[][] = null;
517
                RasterBuf raster = null;
518

    
519
                // Work out the correct aspect for the setView call.
520
                dFileAspect = (double)v.width()/(double)v.height();
521
                dWindowAspect = (double)width /(double)height;
522

    
523
                if (dFileAspect > dWindowAspect) {
524
                  height =(int)((double)width/dFileAspect);
525
                } else {
526
                  width = (int)((double)height*dFileAspect);
527
                }
528
                
529
                // Set the view
530
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),
531
                        width, height);
532
                
533
                raster = new RasterBuf(DataBuffer.TYPE_INT, width, height, 3, new Point(0,0));
534
                try {
535
                        //int nLin = height % BAND_HEIGHT;
536
                        file.setAlpha(getAlpha());
537
                        setBand(RED_BAND,   rBandNr);
538
                        setBand(GREEN_BAND, gBandNr);
539
                        setBand(BLUE_BAND,  bBandNr);
540
                        for (line=0; line < height; line++) { //+=BAND_HEIGHT) {
541
                                file.readLine(raster.getLineInt(line));
542
                        }
543
                } catch (Exception e) {
544
                        // TODO Auto-generated catch block
545
                        e.printStackTrace();
546
                }
547
                
548
                return raster;
549
        }
550
        
551
        /**
552
         * Asigna al objeto Image los valores con los dato de la imagen contenidos en el 
553
         * vector de enteros.
554
         * @param image        imagen con los datos actuales
555
         * @param startX        inicio de la posici?n en X dentro de la imagen
556
         * @param startY        inicio de la posici?n en X dentro de la imagen
557
         * @param w        Ancho de la imagen
558
         * @param h        Alto de la imagen
559
         * @param rgbArray        vector que contiene la banda que se va a sustituir
560
         * @param offset        desplazamiento
561
         * @param scansize        tama?o de imagen recorrida por cada p
562
         */
563
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, 
564
                         int offset, int scansize) {
565
                image.setRGB(startX, startY, w, h, rgbArray, offset, scansize);
566
        }
567
        
568
        /**
569
         * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores 
570
         * con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
571
         * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
572
         * banda correspondiente a flags es sustituida por los datos del vector.
573
         * @param image        imagen con los datos actuales
574
         * @param startX        inicio de la posici?n en X dentro de la imagen
575
         * @param startY        inicio de la posici?n en X dentro de la imagen
576
         * @param w        Ancho de la imagen
577
         * @param h        Alto de la imagen
578
         * @param rgbArray        vector que contiene la banda que se va a sustituir
579
         * @param offset        desplazamiento
580
         * @param scansize        tama?o de imagen recorrida por cada paso
581
         * @param flags        banda que se va a sustituir (Ctes de GeoRasterFile)
582
         */
583
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, 
584
                         int offset, int scansize, int flags) {
585
                int [] line = new int[rgbArray.length]; 
586
                image.getRGB(startX, startY, w, h, line, offset, scansize);
587
                if (flags == GeoRasterFile.RED_BAND)
588
                        for (int i=0; i<line.length; i++)
589
                                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
590
                else if (flags == GeoRasterFile.GREEN_BAND)
591
                        for (int i=0; i<line.length; i++)
592
                                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
593
                else if (flags == GeoRasterFile.BLUE_BAND)
594
                        for (int i=0; i<line.length; i++)
595
                                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
596
                image.setRGB(startX, startY, w, h, line, offset, scansize);
597
        }
598
        
599
        /**
600
         * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores 
601
         * con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
602
         * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
603
         * banda correspondiente a flags es sustituida por los datos del vector.
604
         * @param image        imagen con los datos actuales
605
         * @param startX        inicio de la posici?n en X dentro de la imagen
606
         * @param startY        inicio de la posici?n en X dentro de la imagen
607
         * @param w        Ancho de la imagen
608
         * @param h        Alto de la imagen
609
         * @param rgbArray        vector que contiene la banda que se va a sustituir
610
         * @param offset        desplazamiento
611
         * @param scansize        tama?o de imagen recorrida por cada paso
612
         * @param origBand        Banda origen del GeoRasterFile
613
         * @param destBandFlag        banda que se va a sustituir (Ctes de GeoRasterFile)
614
         */
615
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, 
616
                         int offset, int scansize, int origBand, int destBandFlag) {
617
                int [] line = new int[rgbArray.length]; 
618
                image.getRGB(startX, startY, w, h, line, offset, scansize);
619
                if (origBand == 0 && destBandFlag == GeoRasterFile.RED_BAND)
620
                        for (int i=0; i<line.length; i++)
621
                                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
622
                else if (origBand == 1 && destBandFlag == GeoRasterFile.GREEN_BAND)
623
                        for (int i=0; i<line.length; i++)
624
                                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
625
                else if (origBand == 2 && destBandFlag == GeoRasterFile.BLUE_BAND)
626
                        for (int i=0; i<line.length; i++)
627
                                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
628
                
629
                else if (origBand == 0 && destBandFlag == GeoRasterFile.GREEN_BAND)
630
                        for (int i=0; i<line.length; i++)
631
                                line[i] = (line[i] & 0xffff00ff) | ((rgbArray[i] & 0x00ff0000) >> 8) ;
632
                else if (origBand == 0 && destBandFlag == GeoRasterFile.BLUE_BAND)
633
                        for (int i=0; i<line.length; i++)
634
                                line[i] = (line[i] & 0xffffff00) | ((rgbArray[i] & 0x00ff0000) >> 16);
635
                else if (origBand == 1 && destBandFlag == GeoRasterFile.RED_BAND)
636
                        for (int i=0; i<line.length; i++)
637
                                line[i] = (line[i] & 0xff00ffff) | ((rgbArray[i] & 0x0000ff00) << 8);
638
                
639
                else if (origBand == 1 && destBandFlag == GeoRasterFile.BLUE_BAND)
640
                        for (int i=0; i<line.length; i++)
641
                                line[i] = (line[i] & 0xffffff00) | ((rgbArray[i] & 0x0000ff00) >> 8);
642
                else if (origBand == 2 && destBandFlag == GeoRasterFile.RED_BAND)
643
                        for (int i=0; i<line.length; i++)
644
                                line[i] = (line[i] & 0xff00ffff) | ((rgbArray[i] & 0x000000ff) << 16);
645
                else if (origBand == 2 && destBandFlag == GeoRasterFile.GREEN_BAND)
646
                        for (int i=0; i<line.length; i++)
647
                                line[i] = (line[i] & 0xffff00ff) | ((rgbArray[i] & 0x000000ff) << 8);
648
                image.setRGB(startX, startY, w, h, line, offset, scansize);
649
        }
650
        
651
        private void showOnOpen() {
652
                  // Report en la apertura (quitar)
653
                  System.out.println("Fichero GDAL '"+getName()+"' abierto.");
654
                  System.out.println("Version = "+file.version);
655
                  System.out.println("   Size = ("+file.width+","+file.height+")");
656
                  try {
657
                        System.out.println("   NumBands = ("+file.getRasterCount()+")");
658
                } catch (GdalException e) {
659
                        // TODO Auto-generated catch block
660
                        e.printStackTrace();
661
                }
662
                  file.pintaInfo();
663
                  file.pintaPaleta();
664

    
665
        }
666

    
667
        /* (non-Javadoc)
668
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans, java.awt.Image, int, int)
669
         */
670
        public Image updateImage(int width, int height, ICoordTrans rp, Image img, int origBand, int destBandFlag){
671
                
672
                double dFileAspect, dWindowAspect;
673
                int line, pRGBArray[] = null;
674

    
675
                // Work out the correct aspect for the setView call.
676
                dFileAspect = (double)v.width()/(double)v.height();
677
                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
                // Set the view
686
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),
687
                        width, height);
688
                
689
                if(width<=0)width=1;
690
                if(height<=0)height=1;
691
                
692
                pRGBArray = new int[width/**BAND_HEIGHT*/];
693
                try {
694
                        setBand(RED_BAND,   rBandNr);
695
                        setBand(GREEN_BAND, gBandNr);
696
                        setBand(BLUE_BAND,  bBandNr);
697
                        file.setAlpha(getAlpha());
698
                        if(img!=null){
699
                                for (line=0; line < height; line++) { 
700
                                        file.readLineRGBA(pRGBArray);
701
                                        setRGBLine((BufferedImage) img, 0, line, width, 1/*bandH*/, pRGBArray, 0, width, origBand, destBandFlag);
702
                                }
703
                                return img;
704
                        }else{
705
                                Image image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
706
                                for (line=0; line < height; line++) { 
707
                                        file.readLineRGBA(pRGBArray);
708
                                        setRGBLine((BufferedImage) image, 0, line, width, 1/*bandH*/, pRGBArray, 0, width);
709
                                }
710
                                return image;
711
                        }
712
                } catch (Exception e) {
713
                        // TODO Auto-generated catch block
714
                        e.printStackTrace();
715
                }
716
                
717
                return img;
718
        }
719
        
720
        /* (non-Javadoc)
721
         * @see org.cresques.io.GeoRasterFile#getData(int, int, int)
722
         */
723
        public Object getData(int x, int y, int band) {
724
                // TODO Auto-generated method stub
725
                return null;
726
        }
727
        
728
        /**
729
         * Devuelve los datos de una ventana solicitada
730
         * @param ulX        coordenada X superior izda.
731
         * @param ulY        coordenada Y superior derecha.
732
         * @param sizeX        tama?o en X de la ventana.
733
         * @param sizeY tama?o en Y de la ventana.
734
         * @param band        Banda solicitada.
735
         */
736
        public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band){
737
                
738
                return null;
739
        }
740
        
741
        /**
742
         * Obtiene la zona (Norte / Sur)
743
         * @return true si la zona es norte y false si es sur
744
         */
745
        
746
        public boolean getZone(){
747
                
748
                return false;
749
        }
750
        
751
        /**
752
         *Devuelve el n?mero de zona UTM
753
         *@return N?mero de zona 
754
         */
755
        
756
        public int getUTM(){
757
                
758
                return 0;        
759
        }
760
        
761
        /**
762
         * Obtiene el sistema de coordenadas geograficas
763
         * @return Sistema de coordenadas geogr?ficas
764
         */
765
        public String getGeogCS(){
766
                
767
                return new String("");        
768
        }
769
        /**
770
         * Devuelve el tama?o de bloque
771
         * @return Tama?o de bloque
772
         */
773
        public int getBlockSize(){
774
     //TODO Nacho: Implementar getBlockSize de EcwFile        
775
          return file.getBlockSize();
776
        }
777
}
778

    
779