Statistics
| Revision:

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

History | View | Annotate | Download (23.5 KB)

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

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

    
32
import org.cresques.cts.ICoordTrans;
33
import org.cresques.cts.IProjection;
34
import org.cresques.io.GeoFile;
35
import org.cresques.io.GeoRasterFile;
36
import org.cresques.px.Extent;
37

    
38
import es.gva.cit.jmrsid.*;
39

    
40
/**
41
 * Soporte para los fichero MrSID de Lizardtech
42
 * @author Nacho Brodin (brodin_ign@gva.es)
43
 */
44

    
45
class MrSidNative extends MrSIDImageReader {
46
        
47
        static boolean WITH_OVERVIEWS = true;
48
        // Polilinea con extent
49
        class Contour extends Vector {
50
                public double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE;
51
                public double maxX = -Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
52
                public Contour() {
53
                        super();
54
                }
55
                public void add(Point2D pt) {
56
                        super.add(pt);
57
                        if (pt.getX() > maxX) maxX = pt.getX();
58
                        if (pt.getX() < minX) minX = pt.getX();
59
                        if (pt.getY() > maxY) maxY = pt.getY();
60
                        if (pt.getY() < minY) minY = pt.getY();
61
                }
62
        }
63
        
64
        /**
65
         * Contorno en coordenadas geogr?ficas. (y Extent del raster).
66
         */
67
        public Contour esq = new Contour();
68
        public int width = 0, height = 0;
69
        public double originX = 0D, originY = 0D;
70
        public String version = "";
71
        public LTIMetadataDatabase metadata;
72
        public LTIPixel pixel=null;
73
        
74
        
75
        
76
        private int alpha = 0;
77
        protected int rBandNr = 1, gBandNr = 2, bBandNr = 3;
78
        protected byte [] bandR, bandG, bandB;
79
        private int dataType = LTIDataType.LTI_DATATYPE_UINT8;
80
        
81
        //View
82
        
83
        double currentViewY = -1;
84
        int currentFullWidth = -1;
85
        int currentFullHeight = -1;
86
        int currentViewWidth = -1;
87
        int currentViewHeight = -1;
88
        double currentViewX = 0D;
89
        double viewportScale = 0D;
90
        double step = 0D;
91
        int currentOverview = -1;
92
        private double zoomoverview=0.0;
93
        int eColorSpace;
94
        int eSampleType;
95
        public int nbands;
96
        int noverviews;
97
        public int xini,yini,anchoOver, altoOver;
98
        public int blocksize = 1;
99
        
100
        /**
101
         * Constructor
102
         * @param fName
103
         * @throws MrSIDException
104
         * @throws IOException
105
         */
106
        public MrSidNative(String fName) throws MrSIDException, IOException {
107
                super(fName);
108
                init(fName);
109
        }
110
        
111
        /**
112
         * Inicializa las variables de instancia con los valores de la imagen
113
         * @param fName
114
         * @throws MrSIDException
115
         * @throws IOException
116
         */
117
        private void init(String fName) throws MrSIDException, IOException {
118
                
119
                this.initialize();
120
                
121
                String ext = fName.toLowerCase().substring(fName.lastIndexOf('.')+1);
122

    
123
                width = this.getWidth();
124
                height = this.getHeight();
125
                eSampleType = this.getDataType();
126
                nbands = this.getNumBands();
127
                eColorSpace = this.getColorSpace();
128
                noverviews = this.getNumLevels();
129
                                
130
                metadata = this.getMetadata();
131
                double ox=0D, oy=0D, resx=0D, resy=0D;
132
                        
133
                LTIGeoCoord geoc = this.getGeoCoord();
134
                
135
                ox = geoc.getX();
136
                oy = geoc.getY();
137
                resx = geoc.getXRes();
138
                resy = geoc.getYRes();
139
                        
140
                System.out.println("Origin = ("+ox+","+oy+")");
141
                System.out.println("Pixel Size = ("+resx+","+resy+")");
142
                  esq.add(new Point2D.Double(ox, oy));
143
                  esq.add(new Point2D.Double(ox+resx*width, oy));
144
                  esq.add(new Point2D.Double(ox, oy+resy*height));
145
                  esq.add(new Point2D.Double(ox+resx*width, oy+resy*height));
146
                  
147
                  blocksize = this.getStripHeight();
148
                  System.out.println("StripHeight = ("+blocksize+")");
149
                
150
        }
151
        
152
        /**
153
         * Asigna el valor de Alpha
154
         * @param a        alpha
155
         */
156
        public void setAlpha(int a) { 
157
                alpha = a; 
158
        }
159
        
160
        /**
161
         * Asigna el tipo de datos
162
         * @param dt        tipo de datos
163
         */        
164
        public void setDataType(int dt) { 
165
                dataType = dt; 
166
        }
167
        
168
        /**
169
         * Obtiene un punto 2D con las coordenadas del raster a partir de uno en coordenadas
170
         * del punto real.
171
         * @param pt        punto en coordenadas del punto real
172
         * @return        punto en coordenadas del raster
173
         */
174
        public Point2D worldToRaster(Point2D pt) {
175
                double x = (((double) currentFullWidth)/(esq.maxX-esq.minX))*(pt.getX()-esq.minX);
176
                double y = (((double) currentFullHeight)/(esq.maxY-esq.minY))*(esq.maxY-pt.getY());
177
                Point2D ptRes = new Point2D.Double(x, y);
178
                return ptRes;
179
        }
180
                
181
        /**
182
         * Calcula el overview a usar de la imagen y el viewport a partir del ancho, alto y
183
         * coordenadas del mundo real
184
         * @param dWorldTLX        Coordenada X superior izquierda
185
         * @param dWorldTLY        Coordenada Y superior izquierda
186
         * @param dWorldBRX        Coordenada X inferior derecha
187
         * @param dWorldBRY        Coordenada Y inferior derecha
188
         * @param nWidth        ancho
189
         * @param nHeight        alto
190
         */        
191
        public void setView(double dWorldTLX, double dWorldTLY,
192
                                    double dWorldBRX, double dWorldBRY,
193
                                                int nWidth, int nHeight) {
194
                
195
                //Ancho y alto de la im?gen en pixeles (pixeles de la overview)
196
                currentFullWidth = width;
197
                currentFullHeight = height;
198
                
199
                //Ventana de la imagen. (en tama?o completo)
200
                //tl->esq sup izda en pixeles
201
                //br->esq inf der en pixeles
202
                
203
                Point2D tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
204
                Point2D br = worldToRaster(new Point2D.Double(dWorldBRX, dWorldBRY));
205
                                
206
                //Ancho y alto de la im?gen (pixeles en pantalla)
207
                currentViewWidth = nWidth;
208
                currentViewHeight = nHeight;
209

    
210
                currentViewX = tl.getX();
211
                currentViewY = tl.getY();
212
                
213
                viewportScale = (double) currentViewWidth/(br.getX()-tl.getX());
214
                currentViewY = tl.getY();
215
                try {
216
                        // calcula el overview a usar
217
                        
218
                        int[] dims=null;
219
                        double zoom=1.0;
220
                        zoomoverview=1.0;
221
                        currentOverview = -1;
222
                        if (WITH_OVERVIEWS && noverviews-1 > 0) {
223
                                for (int i=(noverviews-1); i>0; i--) {
224
                                        zoom = LTIUtils.levelToMag(i);
225
                                        dims=this.getDimsAtMag(zoom);
226
                                        if (dims[0]>this.getWidth()*viewportScale) {
227
                                                currentOverview = i;
228
                                                zoomoverview=zoom;
229
                                                viewportScale /= zoomoverview;
230
                                                currentFullWidth = dims[0];
231
                                    currentFullHeight = dims[1];
232
                                    tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
233
                                    currentViewX = tl.getX();
234
                                    currentViewY = tl.getY();
235
                                    break;
236
                                        }
237
                                }
238
                        }
239
                        
240
                        setDataType(eSampleType);
241
                        
242
                        
243
                } catch (MrSIDException e) {
244
                        e.printStackTrace();
245
                }
246
                
247
        }
248
        
249
        /**
250
         * Muestra alguna informaci?n para la depuraci?n
251
         */
252
        void pintaInfo() {
253
                try {
254

    
255
                        System.out.println("GeoTransform:");
256
                        LTIGeoCoord geoc = this.getGeoCoord();
257
                        
258
                        System.out.println("  param[0]="+geoc.getX());
259
                        System.out.println("  param[0]="+geoc.getY());
260
                        System.out.println("  param[0]="+geoc.getXRes());
261
                        System.out.println("  param[0]="+geoc.getYRes());
262
                        System.out.println("  param[0]="+geoc.getXRot());
263
                        System.out.println("  param[0]="+geoc.getYRot());
264
                        System.out.println("Metadata:");
265
                        LTIMetadataDatabase metadata = this.getMetadata();
266
                        for (int i=0; i<metadata.getIndexCount(); i++)
267
                           {
268
                         LTIMetadataRecord rec = null;
269
                         rec = metadata.getDataByIndex(i);
270
                         System.out.println(rec.getTagName());
271
                         
272
                         if(rec.isScalar())System.out.println(rec.getScalarData());
273
                         else if(rec.isVector()){
274
                                 String[] s = rec.getVectorData();
275
                                 for(int j=0;j<s.length;j++)System.out.println("V"+j+"->"+s[j]);
276
                         }else if(rec.isArray()){
277
                                 String[] s = rec.getArrayData();
278
                                 for(int j=0;j<s.length;j++)System.out.println("A"+j+"->"+s[j]);
279
                         }
280
                         
281
                         else System.out.println("");
282
                       }
283
                } catch (MrSIDException e) {
284
                        // TODO Auto-generated catch block
285
                        e.printStackTrace();
286
                }
287
                
288
        }
289
        
290
        void pintaPaleta() {
291
        }
292
        
293
        /**
294
         * Lee la escena de la imagen correspondiente a la vista seleccionada con
295
         * currentView a trav?s de la libreria de MrSid. Esta escena es cargada sobre
296
         * un buffer y asignada al par?metro de salida. 
297
         * @param line        Escena leida        
298
         * @throws MrSIDException Lanzada si ocurre un error en la lectura de la escena
299
         */
300
        public void readScene(int [] line) throws MrSIDException {
301
                //int a = 0;
302
                int x = (int)currentViewX;
303
            int y = (int)currentViewY;
304
        int SceneWidth;
305
        int SceneHeight;
306
        
307
        try{
308
                if(x==0 && y==0){
309
                        SceneWidth = currentFullWidth;
310
                            SceneHeight = currentFullHeight;
311
                }else{
312
                            SceneWidth = (int) (((double)currentViewWidth)/viewportScale);
313
                            SceneHeight = (int) (((double)currentViewHeight)/viewportScale);
314
                }
315
                         
316
                        if(SceneWidth==0)SceneWidth=1;
317
                        if(SceneHeight==0)SceneHeight=1;
318
                        if(pixel==null)pixel = new LTIPixel(eColorSpace, nbands, eSampleType);
319
                        LTIScene scene = new LTIScene(x, y, SceneWidth, SceneHeight, zoomoverview);
320
                        // Este deber?a ser el constructor con ventana
321
                        LTISceneBuffer buffer = new LTISceneBuffer(pixel, SceneWidth, SceneHeight, true);
322
                        
323
                        ((LTIImageStage)this).read(scene, buffer);
324

    
325
                                  
326
                          if (dataType == LTIDataType.LTI_DATATYPE_UINT8 ||
327
                                  dataType == LTIDataType.LTI_DATATYPE_SINT8 ||
328
                                dataType == LTIDataType.LTI_DATATYPE_SINT16 ||
329
                                dataType == LTIDataType.LTI_DATATYPE_SINT32 ||
330
                                dataType == LTIDataType.LTI_DATATYPE_UINT16 ||
331
                                dataType == LTIDataType.LTI_DATATYPE_UINT32){
332
                                  
333
                                  int kd, k;
334
                                  double scale = 1/viewportScale;
335
                                  int alpha = (this.alpha & 0xff) << 24;
336
                                  if (rBandNr == 1) bandR = buffer.buf1;
337
                                  else if (rBandNr == 2) bandR = buffer.buf2;
338
                                  else if (rBandNr == 3) bandR = buffer.buf3;
339
                                  if (gBandNr == 1) bandG = buffer.buf1;
340
                                  else if (gBandNr == 2) bandG = buffer.buf2;
341
                                  else if (gBandNr == 3) bandG = buffer.buf3;
342
                                  if (bBandNr == 1) bandB = buffer.buf1;
343
                                  else if (bBandNr == 2) bandB = buffer.buf2;
344
                                  else if (bBandNr == 3) bandB = buffer.buf3;
345
                                  if (eColorSpace==LTIColorSpace.LTI_COLORSPACE_RGB)
346
                                          //for(int k=0;k<buffer.size;k++)
347
                                          for (int y1=0; y1<currentViewHeight; y1++)
348
                                                  for (int x1=0; x1<currentViewWidth; x1++) {
349
                                                          kd = y1*currentViewWidth+x1;
350
                                                          k = ((int) (y1*scale))*SceneWidth+ (int)(((double) x1)*scale);
351
                                                          try {
352
                                                                  line[kd] = alpha + ((0xff & bandR[k])<<16) + (( 0xff & bandG[k])<<8) + (0xff & bandB[k]);
353
                                                          } catch (java.lang.ArrayIndexOutOfBoundsException e) {
354
                                                          }
355
                                                  }
356
                                  
357
                                  if(eColorSpace==LTIColorSpace.LTI_COLORSPACE_GRAYSCALE)
358
                                          for (int y1=0; y1<currentViewHeight; y1++)
359
                                                  for (int x1=0; x1<currentViewWidth; x1++) {
360
                                                          kd = y1*currentViewWidth+x1;
361
                                                          k = ((int) (y1*scale))*SceneWidth+ (int)(((double) x1)*scale);
362
                                                          try {
363
                                                                  line[kd] = alpha + bandR[k];
364
                                                          } catch (java.lang.ArrayIndexOutOfBoundsException e) {
365
                                                          }
366
                                                  }
367
                                           
368
                                  
369
                          } else if (dataType == LTIDataType.LTI_DATATYPE_FLOAT32 ||
370
                                  dataType == LTIDataType.LTI_DATATYPE_FLOAT32) {
371
                          }
372
                          
373
                        buffer=null;
374
        }catch(MrSIDException e){
375
                e.printStackTrace();
376
        }
377
                
378
        }
379
        
380
        /**
381
         * Lee una ventana de la imagen y devuelve un buffer de bytes
382
         * @param ulX        Coordenada X de la esquina superior izquierda
383
         * @param ulY        Coordenada Y de la esquina superior izquierda
384
         * @param sizeX        Tama?o X de la imagen
385
         * @param sizeY        Tama?o Y de la image
386
         * @param band        N?mero de bandas
387
         * @return        buffer con la ventana leida
388
         * @throws MrSIDException        
389
         */
390
        public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band)throws MrSIDException{
391
                
392
                if(pixel==null)pixel = new LTIPixel(eColorSpace, nbands, eSampleType);
393
                
394
                LTIScene scene=new LTIScene(ulX, ulY, sizeX, sizeY, 1.0);
395
                   LTISceneBuffer buffer=new LTISceneBuffer(pixel, sizeX, sizeY, true);
396
                   ((LTIImageStage)this).read(scene,buffer);
397
                   if(band==1)return buffer.buf1;
398
                   else if(band==2)return buffer.buf2;
399
                   else if(band==3)return buffer.buf3;
400
                   return null;
401
        }
402
}
403

    
404
/** 
405
 * @author Nacho Brodin <brodin_ign@gva.es>
406
 * 
407
 * Clase encargada del acceso a los datos y repintado de imagenes MrSID. Estos
408
 * son registrados con la extensi?n sid
409
 */
410
public class MrSidFile extends GeoRasterFile {
411
        public final static int BAND_HEIGHT = 64;
412
        protected MrSidNative file = null;
413

    
414
        private Extent v = null;
415
        
416
        static {
417
                GeoRasterFile.registerExtension("sid", MrSidFile.class);
418
        }
419
        
420
        /**
421
         * Contructor. Abre el fichero mrsid
422
         * @param proj        Proyecci?n
423
         * @param fName        Nombre del fichero mrsid
424
         */
425
        public MrSidFile(IProjection proj, String fName) {
426
                super(proj, fName);
427
                extent = new Extent();
428
                try {
429
                        file = new MrSidNative(fName); 
430
                        showOnOpen();
431
                        load();
432
                        bandCount = file.nbands;
433
                        if ( bandCount > 2) {
434
                                setBand(RED_BAND,   0);
435
                                setBand(GREEN_BAND, 1);
436
                                setBand(BLUE_BAND,  2);
437
                        } else
438
                                setBand(RED_BAND|GREEN_BAND|BLUE_BAND, 0);
439
                } catch(Exception e){
440
                          System.out.println("Error en constructor de MrSID");
441
                          e.printStackTrace();
442
                          file = null;
443
                }
444
        }
445
        
446
        /**
447
         * 
448
         */
449
        public GeoFile load() {
450
                extent = new Extent(file.esq.minX, file.esq.minY, file.esq.maxX, file.esq.maxY);
451
                return this;
452
        }
453
        
454
        /**
455
         * Libera el objeto que ha abierto el fichero
456
         */
457
        public void close() {
458
                file=null;
459
        }
460
        
461
        /**
462
         * Asigna una banda R, G o B
463
         */
464
        public void setBand(int flag, int bandNr) {
465
                super.setBand(flag, bandNr);
466
                if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) file.rBandNr = bandNr+1;
467
                if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) file.gBandNr = bandNr+1;
468
                if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) file.bBandNr = bandNr+1;
469
        }
470
        
471
        /**
472
         * Asigna el extent de la vista
473
         */
474
        public void setView(Extent e) { 
475
                v = new Extent(e); 
476
        }
477
        
478
        /**
479
         * Obtiene el Extent de la vista
480
         */
481
        public Extent getView() { 
482
                return v; 
483
        }
484
        
485
        /**
486
         * Obtiene el ancho de la imagen
487
         */
488
        public int getWidth() {        
489
                return file.width; 
490
        }
491
        
492
        /**
493
         * Obtiene el alto de la imagen
494
         */
495
        public int getHeight() { 
496
                return file.height;
497
        }
498

    
499
        
500
        public void reProject(ICoordTrans rp) {
501
                // TODO Auto-generated method stub        
502
        }
503
        
504
        /**
505
         * Actualiza la imagen. Se encarga de llamar a la funci?n que calcula la vista
506
         * y luego a la que lee la escena sobre un buffer. Vuelca la informaci?n obtenida
507
         * sobre el Image que la visualiza.
508
         */
509
        public Image updateImage(int width, int height, ICoordTrans rp) {
510
                double dFileAspect, dWindowAspect;
511
                int line, pRGBArray[] = null;
512
                Image image = null;
513
                
514
                // Work out the correct aspect for the setView call.
515
                dFileAspect = (double)v.width()/(double)v.height();
516
                dWindowAspect = (double)width /(double)height;
517

    
518
                if (dFileAspect > dWindowAspect) {
519
                  height =(int)((double)width/dFileAspect);
520
                } else {
521
                  width = (int)((double)height*dFileAspect);
522
                }
523
                
524
                // Set the view
525
                
526
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),width, height);
527
                                
528
                //Impedimos que los valores de ancho y alto de la im?gen sean menores que 1
529
                
530
                if(width<=0)width=1;
531
                if(height<=0)height=1;
532
                
533
                
534
                image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
535
                pRGBArray = new int[width*height];
536
                
537
                try {                
538
                        file.setAlpha(getAlpha());
539
                        setBand(RED_BAND,   rBandNr);
540
                        setBand(GREEN_BAND, gBandNr);
541
                        setBand(BLUE_BAND,  bBandNr);
542

    
543
                        file.readScene(pRGBArray);
544
                        ((BufferedImage)image).setRGB(0, 0, width, height, pRGBArray, 0, width);
545
                        
546
                        
547
                } catch (Exception e) {
548
                        e.printStackTrace();
549
                }
550
                
551
                return image;
552
        }
553
        
554
        /**
555
         * Muestra informaci?n del fichero abierto.
556
         *
557
         */
558
        private void showOnOpen() {
559
                  // Report en la apertura (quitar)
560
                  System.out.println("Fichero MrSID '"+getName()+"' abierto.");
561
                  System.out.println("Version = "+file.version);
562
                  System.out.println("   Size = ("+file.width+","+file.height+")");
563
                System.out.println("   NumBands = ("+file.nbands+")");
564
        
565
                  file.pintaInfo();
566
                  file.pintaPaleta();
567

    
568
        }
569
        
570
        /**
571
         * Asigna al objeto Image los valores con los dato de la imagen contenidos en el 
572
         * vector de enteros.
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 p
581
         */
582
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, 
583
                         int offset, int scansize) {
584
                image.setRGB(startX, startY, w, h, rgbArray, offset, scansize);
585
        }
586
        
587
        /**
588
         * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores 
589
         * con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
590
         * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
591
         * banda correspondiente a flags es sustituida por los datos del vector.
592
         * @param image        imagen con los datos actuales
593
         * @param startX        inicio de la posici?n en X dentro de la imagen
594
         * @param startY        inicio de la posici?n en X dentro de la imagen
595
         * @param w        Ancho de la imagen
596
         * @param h        Alto de la imagen
597
         * @param rgbArray        vector que contiene la banda que se va a sustituir
598
         * @param offset        desplazamiento
599
         * @param scansize        tama?o de imagen recorrida por cada paso
600
         * @param flags        banda que se va a sustituir (Ctes de GeoRasterFile)
601
         */
602
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, 
603
                         int offset, int scansize, int flags) {
604
                int [] line = new int[rgbArray.length]; 
605
                image.getRGB(startX, startY, w, h, line, offset, scansize);
606
                if (flags == GeoRasterFile.RED_BAND)
607
                        for (int i=0; i<line.length; i++)
608
                                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
609
                else if (flags == GeoRasterFile.GREEN_BAND)
610
                        for (int i=0; i<line.length; i++)
611
                                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
612
                else if (flags == GeoRasterFile.BLUE_BAND)
613
                        for (int i=0; i<line.length; i++)
614
                                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
615
                image.setRGB(startX, startY, w, h, line, offset, scansize);
616
        }
617
        
618

    
619
        /**
620
         * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores 
621
         * con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
622
         * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
623
         * banda correspondiente a flags es sustituida por los datos del vector.
624
         * @param image        imagen con los datos actuales
625
         * @param startX        inicio de la posici?n en X dentro de la imagen
626
         * @param startY        inicio de la posici?n en X dentro de la imagen
627
         * @param w        Ancho de la imagen
628
         * @param h        Alto de la imagen
629
         * @param rgbArray        vector que contiene la banda que se va a sustituir
630
         * @param offset        desplazamiento
631
         * @param scansize        tama?o de imagen recorrida por cada paso
632
         * @param origBand        Banda origen del GeoRasterFile
633
         * @param destBandFlag        banda que se va a sustituir (Ctes de GeoRasterFile)
634
         */
635
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, 
636
                         int offset, int scansize, int origBand, int destBandFlag) {
637
                int [] line = new int[rgbArray.length]; 
638
                image.getRGB(startX, startY, w, h, line, offset, scansize);
639
                if (origBand == 0 && destBandFlag == GeoRasterFile.RED_BAND)
640
                        for (int i=0; i<line.length; i++)
641
                                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
642
                else if (origBand == 1 && destBandFlag == GeoRasterFile.GREEN_BAND)
643
                        for (int i=0; i<line.length; i++)
644
                                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
645
                else if (origBand == 2 && destBandFlag == GeoRasterFile.BLUE_BAND)
646
                        for (int i=0; i<line.length; i++)
647
                                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
648
                
649
                else if (origBand == 0 && destBandFlag == GeoRasterFile.GREEN_BAND)
650
                        for (int i=0; i<line.length; i++)
651
                                line[i] = (line[i] & 0xffff00ff) | ((rgbArray[i] & 0x00ff0000) >> 8) ;
652
                else if (origBand == 0 && destBandFlag == GeoRasterFile.BLUE_BAND)
653
                        for (int i=0; i<line.length; i++)
654
                                line[i] = (line[i] & 0xffffff00) | ((rgbArray[i] & 0x00ff0000) >> 16);
655
                else if (origBand == 1 && destBandFlag == GeoRasterFile.RED_BAND)
656
                        for (int i=0; i<line.length; i++)
657
                                line[i] = (line[i] & 0xff00ffff) | ((rgbArray[i] & 0x0000ff00) << 8);
658
                
659
                else if (origBand == 1 && destBandFlag == GeoRasterFile.BLUE_BAND)
660
                        for (int i=0; i<line.length; i++)
661
                                line[i] = (line[i] & 0xffffff00) | ((rgbArray[i] & 0x0000ff00) >> 8);
662
                else if (origBand == 2 && destBandFlag == GeoRasterFile.RED_BAND)
663
                        for (int i=0; i<line.length; i++)
664
                                line[i] = (line[i] & 0xff00ffff) | ((rgbArray[i] & 0x000000ff) << 16);
665
                else if (origBand == 2 && destBandFlag == GeoRasterFile.GREEN_BAND)
666
                        for (int i=0; i<line.length; i++)
667
                                line[i] = (line[i] & 0xffff00ff) | ((rgbArray[i] & 0x000000ff) << 8);
668
                image.setRGB(startX, startY, w, h, line, offset, scansize);
669
        }
670
        
671
        /* (non-Javadoc)
672
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans, java.awt.Image, int)
673
         */
674
        public Image updateImage(int width, int height, ICoordTrans rp, Image img, int origBand, int destBandFlag) {
675
                double dFileAspect, dWindowAspect;
676
                int line, pRGBArray[] = null;
677
                Image mrSidImage = null;
678
                
679
                // Work out the correct aspect for the setView call.
680
                dFileAspect = (double)v.width()/(double)v.height();
681
                dWindowAspect = (double)width /(double)height;
682

    
683
                if (dFileAspect > dWindowAspect) {
684
                  height =(int)((double)width/dFileAspect);
685
                } else {
686
                  width = (int)((double)height*dFileAspect);
687
                }
688
                
689
                // Set the view
690
                
691
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),width, height);
692
                                
693
                //Impedimos que los valores de ancho y alto de la im?gen sean menores que 1
694
                
695
                if(width<=0)width=1;
696
                if(height<=0)height=1;
697
                
698
                file.setAlpha(getAlpha());
699
                setBand(RED_BAND,   rBandNr);
700
                setBand(GREEN_BAND, gBandNr);
701
                setBand(BLUE_BAND,  bBandNr);
702
                
703
                pRGBArray = new int[width*height];
704
                
705
                if(img==null){        //Caso en el que se crea una imagen
706
                        mrSidImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
707
                        try {                        
708
                                file.readScene(pRGBArray);
709
                                //((BufferedImage)mrSidImage).setRGB(0, 0, width, height, pRGBArray, 0, width);
710
                                setRGBLine((BufferedImage) mrSidImage, 0, 0, width, height, pRGBArray, 0, width);
711
                        } catch (Exception e) {
712
                                e.printStackTrace();
713
                        }
714
                        return mrSidImage;
715
                }else{ //Caso en el que se actualiza una banda del Image
716
                        try {                        
717
                                file.readScene(pRGBArray);
718
                                //((BufferedImage)img).setRGB(0, 0, width, height, pRGBArray, 0, width);
719
                                setRGBLine((BufferedImage) img, 0, 0, width, height, pRGBArray, 0, width, origBand, destBandFlag);
720
                        } catch (Exception e) {
721
                                e.printStackTrace();
722
                        }
723
                        return img;
724
                }
725
        }
726
        
727
        /* (non-Javadoc)
728
         * @see org.cresques.io.GeoRasterFile#getData(int, int, int)
729
         */
730
        public Object getData(int x, int y, int band) {
731
                // TODO Auto-generated method stub
732
                return null;
733
        }
734
        
735
        /**
736
         * Devuelve los datos de una ventana solicitada
737
         * @param ulX        coordenada X superior izda.
738
         * @param ulY        coordenada Y superior derecha.
739
         * @param sizeX        tama?o en X de la ventana.
740
         * @param sizeY tama?o en Y de la ventana.
741
         * @param band        Banda solicitada.
742
         */
743
        public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band){
744
                try{
745
                        return file.getWindow(ulX, ulY, sizeX, sizeY, band);
746
                }catch(MrSIDException e){
747
                        e.printStackTrace();
748
                }
749
                return null;
750
        }
751
        
752
                
753
        /**
754
         * Devuelve el tama?o de bloque
755
         * @return Tama?o de bloque
756
         */
757
        public int getBlockSize(){        
758
          return file.blocksize;
759
        }
760
}
761

    
762