Statistics
| Revision:

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

History | View | Annotate | Download (26.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.Color;
27
import java.awt.Image;
28
import java.awt.Point;
29
import java.awt.geom.Point2D;
30
import java.awt.image.BufferedImage;
31
import java.awt.image.DataBuffer;
32
import java.io.IOException;
33
import java.util.Vector;
34

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

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

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

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

    
173
                currentViewX = tl.getX();
174
                viewportScale = (double) currentViewWidth/(br.getX()-tl.getX());
175
                step = 1D/viewportScale;
176
                lastReadLine = tl.getY();
177
                try {
178
                        // calcula el overview a usar
179
                        bandR = getRasterBand(1);
180
                        currentOverview = -1;
181
                        if (WITH_OVERVIEWS && bandR.getOverviewCount() > 0) {
182
                                GdalRasterBand ovb = null;
183
                                for (int i=bandR.getOverviewCount()-1; i>0; i--) {              
184
                                        ovb = bandR.getOverview(i);
185
                                        if (ovb.getRasterBandXSize()>getRasterXSize()*viewportScale) {
186
                                                currentOverview = i;
187
                                    viewportScale *= ((double) width/(double) ovb.getRasterBandXSize());
188
                                    step = 1D/viewportScale;
189
                                    currentFullWidth = ovb.getRasterBandXSize();
190
                                    currentFullHeight = ovb.getRasterBandYSize();
191
                                    tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
192
                                    currentViewX = tl.getX();
193
                                    lastReadLine = tl.getY();
194
                                    break;
195
                                        }
196
                                }
197
                        }
198
        
199
                        // Selecciona las bandas y los overviews necesarios
200
                        bandR = getRasterBand(rBandNr);
201
                        setDataType(bandR.getRasterDataType());
202
                        if (this.getRasterCount() > 1) {
203
                                bandG = getRasterBand(gBandNr);
204
                                bandB = getRasterBand(bBandNr);
205
                        }
206
                        if (currentOverview > 0) {
207
                                bandR = bandR.getOverview(currentOverview);
208
                            if (this.getRasterCount() > 1) {
209
                                        bandG = bandG.getOverview(currentOverview);
210
                                        bandB = bandB.getOverview(currentOverview);
211
                                }
212
                        }
213

    
214
                        //System.out.println(band.)
215
                } catch (GdalException e) {
216
                        // TODO Auto-generated catch block
217
                        e.printStackTrace();
218
                }
219

    
220
                System.out.println("GdalFile: TL=("+dWorldTLX+","+dWorldTLY+
221
                        "); BR=("+dWorldBRX+","+dWorldBRY+")\n"+
222
                        "GdalFile: escala="+viewportScale+"; lastReadLine="+lastReadLine+"\n"+
223
                    "Actual Raster Size="+currentFullWidth+"x"+currentFullHeight+
224
                        "\nDataType="+dataType);
225
                return err;
226
        }
227
        
228
        int lastY = -1;
229
        
230
        public void readLine(int[][] line) throws GdalException {
231
                int err = 0;
232
                int nbands = getRasterCount();
233
                GdalRasterBand band = null;
234
        int w = (int)(((double)currentViewWidth)*step);
235
        int x = (int) currentViewX;
236
        int y = (int) lastReadLine;
237
        GdalBuffer r = null, g = null, b = null;
238
        //if (alpha > 0) a = alpha << 24;
239
        if (x+w > bandR.getRasterBandXSize()) 
240
                w = bandR.getRasterBandXSize()-x;
241
                r = bandR.readRaster(x, y, w, 1, w, 1, dataType);
242
                if (bandG != null)
243
                    g = bandG.readRaster(x, y, w, 1, w, 1, dataType);
244
                if (bandB != null)
245
                    b = bandB.readRaster(x, y, w, 1, w, 1, dataType);
246

    
247
                  lastReadLine += step;
248
                  
249
                  int white = Color.BLUE.getRGB(), i2=0, i=0;
250
                  float j =0F;
251
                  
252
                  if (dataType == GDT_CInt16 || dataType == GDT_Int16  || dataType == GDT_UInt16){
253
                          if (g == null){ // Sibgle Band (Typical DEM)
254
                                  for (int k=0; k<4; k++){
255
                                          for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
256
                                                  if(k<3)
257
                                                          line[i][k] = (r.buffShort[(int) j] & 0xffff);
258
                                                  else
259
                                                          line[i][3] = 0xff;
260
                                          }
261
                              }
262
                          }else { // Multiband
263
                                  short px;
264
                                  //System.err.println("readLine(): Raster 16bits multibanda");
265
                                  GdalBuffer [] bands = {r,g,b};
266
                                  for (int k=0; k<4; k++){
267
                                      for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step){
268
                                              if(k<3)
269
                                                      line[i][k] = (bands[k].buffShort[(int) j] & 0xffff);
270
                                              else
271
                                                      line[i][3] = 0xff;
272
                                      }
273
                                  }
274
                          }
275
                  }
276
                return;
277
        }
278
        
279
        public int readLineRGBA(int [] line) throws GdalException {
280
                int err = 0;
281
                int nbands = getRasterCount();
282
                GdalRasterBand band = null;
283
        int w = (int)(((double)currentViewWidth)*step);
284
        int x = (int) currentViewX;
285
        int y = (int) lastReadLine;
286
        GdalBuffer r = null, g = null, b = null;
287
        //if (alpha > 0) a = alpha << 24;
288
        if (x+w > bandR.getRasterBandXSize()) 
289
                w = bandR.getRasterBandXSize()-x;
290
                r = bandR.readRaster(x, y, w, 1, w, 1, dataType);
291
                if (bandG != null)
292
                    g = bandG.readRaster(x, y, w, 1, w, 1, dataType);
293
                if (bandB != null)
294
                    b = bandB.readRaster(x, y, w, 1, w, 1, dataType);
295

    
296
                  lastReadLine += step;
297
                  
298
                  int white = Color.BLUE.getRGB(), i2=0, i=0;
299
                  float j =0F;
300
                int alpha = (this.alpha & 0xff) << 24;
301
                  if (dataType == GDT_Byte)
302
                          if (g != null)
303
                              for (i=0; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
304
                                      line[i] = alpha + ((r.buffByte[(int) j] & 0xff) << 16) + ((g.buffByte[(int) j] & 0xff) << 8) + (b.buffByte[(int) j] & 0xff);
305
                              }
306
                      else
307
                              for (i=0; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
308
                                      line[i] = alpha + ((r.buffByte[(int) j] & 0xff) << 16) +
309
                                                ((r.buffByte[(int) j] & 0xff) << 8) + (r.buffByte[(int) j] & 0xff);
310
                              }
311
                  else if (dataType == GDT_CInt16 || dataType == GDT_Int16  || dataType == GDT_UInt16)
312
                          if (g == null) // Sibgle Band (Typical DEM)
313
                              /*for (i=0, j=0F, i2 = 1; i<currentViewWidth && i2<r.length;
314
                                      i++, j+=step, i2 = (((int) j)*2)+1) {
315
                                      line[i] = a + ((r[i2-1]) << 8) + r[i2];
316
                              }*/
317
                                    for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
318
                                      line[i] = alpha + r.buffShort[(int) j];
319
                              }
320
                          else { // Multiband
321
                                  // System.err.println("Raster 16bits multibanda");
322
                              for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
323
                                      line[i] = alpha | (((r.buffShort[(int) j] & 0xfff0) << 12) & 0xff0000 ) | 
324
                                                                          (((g.buffShort[(int) j] & 0xfff0) << 4 ) & 0xff00 ) |
325
                                                                          (((b.buffShort[(int) j] & 0xfff0) >> 4 ) & 0xff );
326
                              }
327
                          }
328
            //for (int i=0; i<currentViewWidth; i++) line[i] = 128+128*256+128*256*256;
329

    
330
                return err;
331
        }
332
        
333
        /**
334
         * Lee una franja de la imagen.
335
         * @param bandH Altura de la franja
336
         * @param bufH        Altura del buffer
337
         * @param buf        Buffer con la franja (retorno)
338
         * @return
339
         * @throws GdalException
340
         */
341
        public int readBandRGBA(int bandH, int bufH, int [] buf) throws GdalException {
342
                int err = 0;
343
                int nbands = getRasterCount();
344
                GdalRasterBand band = null;
345
        int w = (int)(((double)currentViewWidth)*step);
346
        int x = (int)(((double)currentViewX)*step);
347
        int y = (int) lastReadLine;
348
        int h = (int) (((double)bandH)*step);
349
        System.out.println("Leyendo "+y);
350
                
351
        if (x+w > bandR.getRasterBandXSize()) 
352
                w = bandR.getRasterBandXSize()-x;
353
                GdalBuffer r = bandR.readRaster(x, y, w, h, w, h, GDT_Byte);
354
                GdalBuffer g = bandG.readRaster(x, y, w, h, w, h, GDT_Byte);
355
                GdalBuffer b = bandB.readRaster(x, y, w, h, w, h, GDT_Byte);
356

    
357
                  lastReadLine += ((double)bandH)*step;
358
                  
359
                  // TODO Acabar de implementarlo
360
                  float k=0F;
361
                int alpha = (this.alpha & 0xff) << 24;
362
                  for (int j=0, t=0; j<bandH; j++) {
363
                          k = j*w; t=j*currentViewWidth;
364
                          for (int i=0; i<currentViewWidth && k<r.getSize(); i++, k+=step) {
365
                                  buf[t+i] = alpha + ((r.buffByte[(int) k]) << 16) + ((g.buffByte[(int) k]) << 8) + b.buffByte[(int) k];
366
                          }
367
                  }
368
                //for (int i=0; i<currentViewWidth; i++) line[i] = 128+128*256+128*256*256;
369

    
370
                return err;
371
                
372
        }
373

    
374
        void pintaInfo() {
375
                try {
376
                        //System.out.println("Origin = "+originX+","+originY);
377
                        //System.out.println("Origin = "+this.);
378
                        System.out.println("GeoTransform:");
379
                        GeoTransform trans = getGeoTransform();
380
                        for (int i=0; i<6; i++)
381
                                System.out.println("  param["+i+"]="+trans.adfgeotransform[i]);
382
                        System.out.println("Metadata:");
383
                        String [] metadata = getMetadata();
384
                        for (int i=0; i<metadata.length; i++) {
385
                                System.out.println(metadata[i]);
386
                        }
387
                } catch (GdalException e) {
388
                        // TODO Auto-generated catch block
389
                        e.printStackTrace();
390
                }
391
                
392
        }
393
        
394
        void pintaPaleta() {
395
        }
396
        
397
        public int getBlockSize(){
398
                return this.getBlockSize();
399
        }
400
}
401

    
402
/**
403
 * @author Luis W. Sevilla
404
 */
405
public class GdalFile extends GeoRasterFile {
406
        public final static int BAND_HEIGHT = 64;
407
        protected GdalNative file = null;
408

    
409
        private Extent v = null;
410
        
411
        static {
412
                //GeoRasterFile.registerExtension("sid", GdalFile.class);
413
                GeoRasterFile.registerExtension("img", GdalFile.class);
414
                GeoRasterFile.registerExtension("tif", GdalFile.class);
415
                GeoRasterFile.registerExtension("tiff", GdalFile.class);
416
                GeoRasterFile.registerExtension("jpg", GdalFile.class);
417
                GeoRasterFile.registerExtension("png", GdalFile.class);
418
        }
419
        
420
        public GdalFile(IProjection proj, String fName){
421
                super(proj, fName);
422
                extent = new Extent();
423
                try {
424
                        file = new GdalNative(fName);
425
                        showOnOpen();
426
                        load();
427
                        bandCount = file.getRasterCount(); 
428
                        if ( bandCount > 2) {
429
                                setBand(RED_BAND,   0);
430
                                setBand(GREEN_BAND, 1);
431
                                setBand(BLUE_BAND,  2);
432
                        } else
433
                                setBand(RED_BAND|GREEN_BAND|BLUE_BAND, 0);
434
                } catch(Exception e){
435
                          System.out.println("Error en GdalOpen");
436
                          e.printStackTrace();
437
                          file = null;
438
                }
439
                int dataType = file.getDataType();
440
                  if (dataType == Gdal.GDT_Byte)
441
                          setDataType(DataBuffer.TYPE_BYTE);
442
                  else if (dataType == Gdal.GDT_CInt16 || dataType == Gdal.GDT_Int16)
443
                          setDataType(DataBuffer.TYPE_SHORT);
444
                  else if(dataType == Gdal.GDT_UInt16)
445
                          setDataType(DataBuffer.TYPE_USHORT);
446
        }
447
        
448
        public GeoFile load() {
449
                /*double minX, minY, maxX, maxY;
450
                minX = minY = 0D;
451
                maxX = (double) width;
452
                maxY = (double) height;*/
453
                extent = new Extent(file.esq.minX, file.esq.minY, file.esq.maxX, file.esq.maxY);
454
                return this;
455
        }
456
        
457
        public void close() {
458
                try {
459
                        file.close();
460
                } catch (GdalException e) {
461
                        // TODO Auto-generated catch block
462
                        e.printStackTrace();
463
                }
464
        }
465
        
466
        public void setBand(int flag, int bandNr) {
467
                super.setBand(flag, bandNr);
468
                if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) file.rBandNr = bandNr+1;
469
                if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) file.gBandNr = bandNr+1;
470
                if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) file.bBandNr = bandNr+1;
471
        }
472
        public void setView(Extent e) { v = new Extent(e); }
473
        public Extent getView() { return v; }
474
        
475
        public int getWidth() {        return file.width; }
476
        public int getHeight() { return file.height;}
477

    
478
        /* (non-Javadoc)
479
         * @see org.cresques.io.GeoRasterFile#reProject(org.cresques.cts.ICoordTrans)
480
         */
481
        public void reProject(ICoordTrans rp) {
482
                // TODO Auto-generated method stub
483
                
484
        }
485
        /* (non-Javadoc)
486
         * @see org.cresques.io.GeoRasterFile#setTransparency(boolean)
487
         * /
488
        public void setTransparency(boolean t) {
489
                // TODO Auto-generated method stub
490
        }
491
        public void setTransparency(int t) {
492
                // TODO Auto-generated method stub
493
        }*/
494
        /* (non-Javadoc)
495
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans)
496
         */
497
        public Image updateImage(int width, int height, ICoordTrans rp) {
498
                double dFileAspect, dWindowAspect;
499
                int line, pRGBArray[] = null;
500
                Image image = null;
501

    
502
                // Work out the correct aspect for the setView call.
503
                dFileAspect = (double)v.width()/(double)v.height();
504
                dWindowAspect = (double)width /(double)height;
505

    
506
                if (dFileAspect > dWindowAspect) {
507
                  height =(int)((double)width/dFileAspect);
508
                } else {
509
                  width = (int)((double)height*dFileAspect);
510
                }
511
                
512
                // Set the view
513
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),
514
                        width, height);
515
                
516
                if(width<=0)width=1;
517
                if(height<=0)height=1;
518
                
519
                image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
520
                //image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
521
                pRGBArray = new int[width/**BAND_HEIGHT*/];
522
                try {
523
                        //int nLin = height % BAND_HEIGHT;
524
                        file.setAlpha(getAlpha());
525
                        setBand(RED_BAND,   rBandNr);
526
                        setBand(GREEN_BAND, gBandNr);
527
                        setBand(BLUE_BAND,  bBandNr);
528
                        for (line=0; line < height; line++) { //+=BAND_HEIGHT) {
529
                                //int bandH = Math.min(BAND_HEIGHT, height-line);
530
                                //file.readBandRGBA(bandH, BAND_HEIGHT, pRGBArray);
531
                                file.readLineRGBA(pRGBArray);
532
                                setRGBLine((BufferedImage) image, 0, line, width, 1/*bandH*/, pRGBArray, 0, width);
533
                        }
534
                } catch (Exception e) {
535
                        // TODO Auto-generated catch block
536
                        e.printStackTrace();
537
                }
538
                
539
                return image;
540
        }
541
        
542
        public RasterBuf getRaster(int width, int height, ICoordTrans rp) {
543
                double dFileAspect, dWindowAspect;
544
                int line, pRGBArray[][] = null;
545
                RasterBuf raster = null;
546

    
547
                // Work out the correct aspect for the setView call.
548
                dFileAspect = (double)v.width()/(double)v.height();
549
                dWindowAspect = (double)width /(double)height;
550

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

    
693
        }
694

    
695
        /* (non-Javadoc)
696
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans, java.awt.Image, int, int)
697
         */
698
        public Image updateImage(int width, int height, ICoordTrans rp, Image img, int origBand, int destBandFlag){
699
                
700
                double dFileAspect, dWindowAspect;
701
                int line, pRGBArray[] = null;
702

    
703
                // Work out the correct aspect for the setView call.
704
                dFileAspect = (double)v.width()/(double)v.height();
705
                dWindowAspect = (double)width /(double)height;
706

    
707
                if (dFileAspect > dWindowAspect) {
708
                  height =(int)((double)width/dFileAspect);
709
                } else {
710
                  width = (int)((double)height*dFileAspect);
711
                }
712
                
713
                // Set the view
714
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),
715
                        width, height);
716
                
717
                if(width<=0)width=1;
718
                if(height<=0)height=1;
719
                
720
                pRGBArray = new int[width/**BAND_HEIGHT*/];
721
                try {
722
                        setBand(RED_BAND,   rBandNr);
723
                        setBand(GREEN_BAND, gBandNr);
724
                        setBand(BLUE_BAND,  bBandNr);
725
                        file.setAlpha(getAlpha());
726
                        if(img!=null){
727
                                for (line=0; line < height; line++) { 
728
                                        file.readLineRGBA(pRGBArray);
729
                                        setRGBLine((BufferedImage) img, 0, line, width, 1/*bandH*/, pRGBArray, 0, width, origBand, destBandFlag);
730
                                }
731
                                return img;
732
                        }else{
733
                                Image image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
734
                                for (line=0; line < height; line++) { 
735
                                        file.readLineRGBA(pRGBArray);
736
                                        setRGBLine((BufferedImage) image, 0, line, width, 1/*bandH*/, pRGBArray, 0, width);
737
                                }
738
                                return image;
739
                        }
740
                } catch (Exception e) {
741
                        // TODO Auto-generated catch block
742
                        e.printStackTrace();
743
                }
744
                
745
                return img;
746
        }
747
        
748
        /* (non-Javadoc)
749
         * @see org.cresques.io.GeoRasterFile#getData(int, int, int)
750
         */
751
        public Object getData(int x, int y, int band) {
752
                // TODO Auto-generated method stub
753
                return null;
754
        }
755
        
756
        /**
757
         * Devuelve los datos de una ventana solicitada
758
         * @param ulX        coordenada X superior izda.
759
         * @param ulY        coordenada Y superior derecha.
760
         * @param sizeX        tama?o en X de la ventana.
761
         * @param sizeY tama?o en Y de la ventana.
762
         * @param band        Banda solicitada.
763
         */
764
        public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band){
765
                
766
                return null;
767
        }
768
        
769
        /**
770
         * Obtiene la zona (Norte / Sur)
771
         * @return true si la zona es norte y false si es sur
772
         */
773
        
774
        public boolean getZone(){
775
                
776
                return false;
777
        }
778
        
779
        /**
780
         *Devuelve el n?mero de zona UTM
781
         *@return N?mero de zona 
782
         */
783
        
784
        public int getUTM(){
785
                
786
                return 0;        
787
        }
788
        
789
        /**
790
         * Obtiene el sistema de coordenadas geograficas
791
         * @return Sistema de coordenadas geogr?ficas
792
         */
793
        public String getGeogCS(){
794
                
795
                return new String("");        
796
        }
797
        /**
798
         * Devuelve el tama?o de bloque
799
         * @return Tama?o de bloque
800
         */
801
        public int getBlockSize(){
802
     //TODO Nacho: Implementar getBlockSize de EcwFile        
803
          return file.getBlockSize();
804
        }
805
}
806

    
807