Statistics
| Revision:

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

History | View | Annotate | Download (25.7 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<4; k++){
237
                                          for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step) {
238
                                                  if(k<3)
239
                                                          line[i][k] = (r.buffShort[(int) j] & 0xffff);
240
                                                  else
241
                                                          line[i][3] = 0xff;
242
                                          }
243
                              }
244
                                  
245
                          }else { // Multiband
246
                                  short px;
247
                                  //System.err.println("readLine(): Raster 16bits multibanda");
248
                                  GdalBuffer [] bands = {r,g,b};
249
                                  for (int k=0; k<4; k++){
250
                                      for (i=0, j=0F; i<currentViewWidth && j<r.getSize(); i++, j+=step){
251
                                              if(k<3)
252
                                                      line[i][k] = (bands[k].buffShort[(int) j] & 0xffff);
253
                                              else
254
                                                      line[i][3] = 0xff;
255
                                      }
256
                                  }
257
                          }
258
                  }
259
                return;
260
        }
261
        
262
        public int readLineRGBA(int [] line) throws GdalException {
263
                int err = 0;
264
                int nbands = getRasterCount();
265
                GdalRasterBand band = null;
266
        int w = (int)(((double)currentViewWidth)*step);
267
        int x = (int) currentViewX;
268
        int y = (int) lastReadLine;
269
        GdalBuffer r = null, g = null, b = null;
270
        //if (alpha > 0) a = alpha << 24;
271
        if (x+w > bandR.getRasterBandXSize()) 
272
                w = bandR.getRasterBandXSize()-x;
273
                r = bandR.readRaster(x, y, w, 1, w, 1, dataType);
274
                if (bandG != null)
275
                    g = bandG.readRaster(x, y, w, 1, w, 1, dataType);
276
                if (bandB != null)
277
                    b = bandB.readRaster(x, y, w, 1, w, 1, dataType);
278

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

    
313
                return err;
314
        }
315
        
316
        /**
317
         * Lee una franja de la imagen.
318
         * @param bandH Altura de la franja
319
         * @param bufH        Altura del buffer
320
         * @param buf        Buffer con la franja (retorno)
321
         * @return
322
         * @throws GdalException
323
         */
324
        public int readBandRGBA(int bandH, int bufH, int [] buf) throws GdalException {
325
                int err = 0;
326
                int nbands = getRasterCount();
327
                GdalRasterBand band = null;
328
        int w = (int)(((double)currentViewWidth)*step);
329
        int x = (int)(((double)currentViewX)*step);
330
        int y = (int) lastReadLine;
331
        int h = (int) (((double)bandH)*step);
332
        System.out.println("Leyendo "+y);
333
                
334
        if (x+w > bandR.getRasterBandXSize()) 
335
                w = bandR.getRasterBandXSize()-x;
336
                GdalBuffer r = bandR.readRaster(x, y, w, h, w, h, GDT_Byte);
337
                GdalBuffer g = bandG.readRaster(x, y, w, h, w, h, GDT_Byte);
338
                GdalBuffer b = bandB.readRaster(x, y, w, h, w, h, GDT_Byte);
339

    
340
                  lastReadLine += ((double)bandH)*step;
341
                  
342
                  // TODO Acabar de implementarlo
343
                  float k=0F;
344
                int alpha = (this.alpha & 0xff) << 24;
345
                  for (int j=0, t=0; j<bandH; j++) {
346
                          k = j*w; t=j*currentViewWidth;
347
                          for (int i=0; i<currentViewWidth && k<r.getSize(); i++, k+=step) {
348
                                  buf[t+i] = alpha + ((r.buffByte[(int) k]) << 16) + ((g.buffByte[(int) k]) << 8) + b.buffByte[(int) k];
349
                          }
350
                  }
351
                //for (int i=0; i<currentViewWidth; i++) line[i] = 128+128*256+128*256*256;
352

    
353
                return err;
354
                
355
        }
356

    
357
        void pintaInfo() {
358
                try {
359
                        //System.out.println("Origin = "+originX+","+originY);
360
                        //System.out.println("Origin = "+this.);
361
                        System.out.println("GeoTransform:");
362
                        GeoTransform trans = getGeoTransform();
363
                        for (int i=0; i<6; i++)
364
                                System.out.println("  param["+i+"]="+trans.adfgeotransform[i]);
365
                        System.out.println("Metadata:");
366
                        String [] metadata = getMetadata();
367
                        for (int i=0; i<metadata.length; i++) {
368
                                System.out.println(metadata[i]);
369
                        }
370
                } catch (GdalException e) {
371
                        // TODO Auto-generated catch block
372
                        e.printStackTrace();
373
                }
374
                
375
        }
376
        
377
        void pintaPaleta() {
378
        }
379
        
380
        public int getBlockSize(){
381
                return this.getBlockSize();
382
        }
383
}
384

    
385
/**
386
 * @author Luis W. Sevilla
387
 */
388
public class GdalFile extends GeoRasterFile {
389
        public final static int BAND_HEIGHT = 64;
390
        protected GdalNative file = null;
391

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

    
461
        /* (non-Javadoc)
462
         * @see org.cresques.io.GeoRasterFile#reProject(org.cresques.cts.ICoordTrans)
463
         */
464
        public void reProject(ICoordTrans rp) {
465
                // TODO Auto-generated method stub
466
                
467
        }
468
        /* (non-Javadoc)
469
         * @see org.cresques.io.GeoRasterFile#setTransparency(boolean)
470
         * /
471
        public void setTransparency(boolean t) {
472
                // TODO Auto-generated method stub
473
        }
474
        public void setTransparency(int t) {
475
                // TODO Auto-generated method stub
476
        }*/
477
        /* (non-Javadoc)
478
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans)
479
         */
480
        public Image updateImage(int width, int height, ICoordTrans rp) {
481
                double dFileAspect, dWindowAspect;
482
                int line, pRGBArray[] = null;
483
                Image image = null;
484

    
485
                // Work out the correct aspect for the setView call.
486
                dFileAspect = (double)v.width()/(double)v.height();
487
                dWindowAspect = (double)width /(double)height;
488

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

    
530
                // Work out the correct aspect for the setView call.
531
                dFileAspect = (double)v.width()/(double)v.height();
532
                dWindowAspect = (double)width /(double)height;
533

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

    
676
        }
677

    
678
        /* (non-Javadoc)
679
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans, java.awt.Image, int, int)
680
         */
681
        public Image updateImage(int width, int height, ICoordTrans rp, Image img, int origBand, int destBandFlag){
682
                
683
                double dFileAspect, dWindowAspect;
684
                int line, pRGBArray[] = null;
685

    
686
                // Work out the correct aspect for the setView call.
687
                dFileAspect = (double)v.width()/(double)v.height();
688
                dWindowAspect = (double)width /(double)height;
689

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

    
790