Statistics
| Revision:

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

History | View | Annotate | Download (23.1 KB)

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

    
6
import java.awt.Image;
7
import java.awt.geom.Point2D;
8
import java.awt.image.BufferedImage;
9
import java.io.IOException;
10
import java.util.Vector;
11

    
12
import org.cresques.cts.ICoordTrans;
13
import org.cresques.cts.IProjection;
14
import org.cresques.io.GeoFile;
15
import org.cresques.io.GeoRasterFile;
16
import org.cresques.px.Extent;
17

    
18
import es.gva.cit.jmrsid.*;
19

    
20
/**
21
 * @author Nacho Brodin <brodin_ign@gva.es>
22
 * Soporte para los fichero MrSID de Lizardtech
23
 */
24

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

    
103
                width = this.getWidth();
104
                height = this.getHeight();
105
                eSampleType = this.getDataType();
106
                nbands = this.getNumBands();
107
                eColorSpace = this.getColorSpace();
108
                noverviews = this.getNumLevels();
109
                                
110
                metadata = this.getMetadata();
111
                double ox=0D, oy=0D, resx=0D, resy=0D;
112
                        
113
                LTIGeoCoord geoc = this.getGeoCoord();
114
                
115
                ox = geoc.getX();
116
                oy = geoc.getY();
117
                resx = geoc.getXRes();
118
                resy = geoc.getYRes();
119
                        
120
                System.out.println("Origin = ("+ox+","+oy+")");
121
                System.out.println("Pixel Size = ("+resx+","+resy+")");
122
                  esq.add(new Point2D.Double(ox, oy));
123
                  esq.add(new Point2D.Double(ox+resx*width, oy));
124
                  esq.add(new Point2D.Double(ox, oy+resy*height));
125
                  esq.add(new Point2D.Double(ox+resx*width, oy+resy*height));
126
                  
127
                  blocksize = this.getStripHeight();
128
                  System.out.println("StripHeight = ("+blocksize+")");
129
                
130
        }
131
        
132
        /**
133
         * Asigna el valor de Alpha
134
         * @param a        alpha
135
         */
136
        public void setAlpha(int a) { 
137
                alpha = a; 
138
        }
139
        
140
        /**
141
         * Asigna el tipo de datos
142
         * @param dt        tipo de datos
143
         */        
144
        public void setDataType(int dt) { 
145
                dataType = dt; 
146
        }
147
        
148
        /**
149
         * Obtiene un punto 2D con las coordenadas del raster a partir de uno en coordenadas
150
         * del punto real.
151
         * @param pt        punto en coordenadas del punto real
152
         * @return        punto en coordenadas del raster
153
         */
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
        /**
162
         * Calcula el overview a usar de la imagen y el viewport a partir del ancho, alto y
163
         * coordenadas del mundo real
164
         * @param dWorldTLX        Coordenada X superior izquierda
165
         * @param dWorldTLY        Coordenada Y superior izquierda
166
         * @param dWorldBRX        Coordenada X inferior derecha
167
         * @param dWorldBRY        Coordenada Y inferior derecha
168
         * @param nWidth        ancho
169
         * @param nHeight        alto
170
         */        
171
        public void setView(double dWorldTLX, double dWorldTLY,
172
                                    double dWorldBRX, double dWorldBRY,
173
                                                int nWidth, int nHeight) {
174
                
175
                //Ancho y alto de la im?gen en pixeles (pixeles de la overview)
176
                currentFullWidth = width;
177
                currentFullHeight = height;
178
                
179
                //Ventana de la imagen. (en tama?o completo)
180
                //tl->esq sup izda en pixeles
181
                //br->esq inf der en pixeles
182
                
183
                Point2D tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
184
                Point2D br = worldToRaster(new Point2D.Double(dWorldBRX, dWorldBRY));
185
                                
186
                //Ancho y alto de la im?gen (pixeles en pantalla)
187
                currentViewWidth = nWidth;
188
                currentViewHeight = nHeight;
189

    
190
                currentViewX = tl.getX();
191
                currentViewY = tl.getY();
192
                
193
                viewportScale = (double) currentViewWidth/(br.getX()-tl.getX());
194
                currentViewY = tl.getY();
195
                try {
196
                        // calcula el overview a usar
197
                        
198
                        int[] dims=null;
199
                        double zoom=1.0;
200
                        zoomoverview=1.0;
201
                        currentOverview = -1;
202
                        if (WITH_OVERVIEWS && noverviews-1 > 0) {
203
                                for (int i=(noverviews-1); i>0; i--) {
204
                                        zoom = LTIUtils.levelToMag(i);
205
                                        dims=this.getDimsAtMag(zoom);
206
                                        if (dims[0]>this.getWidth()*viewportScale) {
207
                                                currentOverview = i;
208
                                                zoomoverview=zoom;
209
                                                viewportScale /= zoomoverview;
210
                                                currentFullWidth = dims[0];
211
                                    currentFullHeight = dims[1];
212
                                    tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
213
                                    currentViewX = tl.getX();
214
                                    currentViewY = tl.getY();
215
                                    break;
216
                                        }
217
                                }
218
                        }
219
                        
220
                        setDataType(eSampleType);
221
                        
222
                        
223
                } catch (MrSIDException e) {
224
                        e.printStackTrace();
225
                }
226

    
227
                /*System.out.println("MrSIDFile: TL=("+dWorldTLX+","+dWorldTLY+
228
                        "); BR=("+dWorldBRX+","+dWorldBRY+")\n"+
229
                        "MrSIDFile: escala="+viewportScale+"\n"+
230
                    "Actual Raster Size="+currentFullWidth+"x"+currentFullHeight);*/
231
                
232
        }
233
        
234
        /**
235
         * Muestra alguna informaci?n para la depuraci?n
236
         */
237
        void pintaInfo() {
238
                try {
239

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

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

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

    
408
        private Extent v = null;
409
        
410
        static {
411
                GeoRasterFile.registerExtension("sid", MrSidFile.class);
412
        }
413
        
414
        public MrSidFile(IProjection proj, String fName) {
415
                super(proj, fName);
416
                extent = new Extent();
417
                try {
418
                        file = new MrSidNative(fName); 
419
                        showOnOpen();
420
                        load();
421
                        bandCount = file.nbands;
422
                        if ( bandCount > 2) {
423
                                setBand(RED_BAND,   0);
424
                                setBand(GREEN_BAND, 1);
425
                                setBand(BLUE_BAND,  2);
426
                        } else
427
                                setBand(RED_BAND|GREEN_BAND|BLUE_BAND, 0);
428
                } catch(Exception e){
429
                          System.out.println("Error en constructor de MrSID");
430
                          e.printStackTrace();
431
                          file = null;
432
                }
433
        }
434
        
435
        public GeoFile load() {
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
                file=null;
442
        }
443
        
444
        /**
445
         * Asigna una banda R, G o B
446
         */
447
        public void setBand(int flag, int bandNr) {
448
                super.setBand(flag, bandNr);
449
                if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) file.rBandNr = bandNr+1;
450
                if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) file.gBandNr = bandNr+1;
451
                if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) file.bBandNr = bandNr+1;
452
        }
453
        
454
        /**
455
         * Asigna el extent de la vista
456
         */
457
        public void setView(Extent e) { 
458
                v = new Extent(e); 
459
        }
460
        
461
        /**
462
         * Obtiene el Extent de la vista
463
         */
464
        public Extent getView() { 
465
                return v; 
466
        }
467
        
468
        /**
469
         * Obtiene el ancho de la imagen
470
         */
471
        public int getWidth() {        
472
                return file.width; 
473
        }
474
        
475
        /**
476
         * Obtiene el alto de la imagen
477
         */
478
        public int getHeight() { 
479
                return file.height;
480
        }
481

    
482
        
483
        public void reProject(ICoordTrans rp) {
484
                // TODO Auto-generated method stub        
485
        }
486
        
487
        /**
488
         * Actualiza la imagen. Se encarga de llamar a la funci?n que calcula la vista
489
         * y luego a la que lee la escena sobre un buffer. Vuelca la informaci?n obtenida
490
         * sobre el Image que la visualiza.
491
         */
492
        public Image updateImage(int width, int height, ICoordTrans rp) {
493
                double dFileAspect, dWindowAspect;
494
                int line, pRGBArray[] = null;
495
                Image image = null;
496
                
497
                // Work out the correct aspect for the setView call.
498
                dFileAspect = (double)v.width()/(double)v.height();
499
                dWindowAspect = (double)width /(double)height;
500

    
501
                if (dFileAspect > dWindowAspect) {
502
                  height =(int)((double)width/dFileAspect);
503
                } else {
504
                  width = (int)((double)height*dFileAspect);
505
                }
506
                
507
                // Set the view
508
                
509
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),width, height);
510
                                
511
                //Impedimos que los valores de ancho y alto de la im?gen sean menores que 1
512
                
513
                if(width<=0)width=1;
514
                if(height<=0)height=1;
515
                
516
                
517
                image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
518
                pRGBArray = new int[width*height];
519
                
520
                try {                
521
                        file.setAlpha(getAlpha());
522
                        setBand(RED_BAND,   rBandNr);
523
                        setBand(GREEN_BAND, gBandNr);
524
                        setBand(BLUE_BAND,  bBandNr);
525

    
526
                        file.readScene(pRGBArray);
527
                        ((BufferedImage)image).setRGB(0, 0, width, height, pRGBArray, 0, width);
528
                        
529
                        
530
                } catch (Exception e) {
531
                        e.printStackTrace();
532
                }
533
                
534
                return image;
535
        }
536
        
537
        
538
        private void showOnOpen() {
539
                  // Report en la apertura (quitar)
540
                  System.out.println("Fichero MrSID '"+getName()+"' abierto.");
541
                  System.out.println("Version = "+file.version);
542
                  System.out.println("   Size = ("+file.width+","+file.height+")");
543
                System.out.println("   NumBands = ("+file.nbands+")");
544
        
545
                  file.pintaInfo();
546
                  file.pintaPaleta();
547

    
548
        }
549
        
550
        /**
551
         * Asigna al objeto Image los valores con los dato de la imagen contenidos en el 
552
         * vector de enteros.
553
         * @param image        imagen con los datos actuales
554
         * @param startX        inicio de la posici?n en X dentro de la imagen
555
         * @param startY        inicio de la posici?n en X dentro de la imagen
556
         * @param w        Ancho de la imagen
557
         * @param h        Alto de la imagen
558
         * @param rgbArray        vector que contiene la banda que se va a sustituir
559
         * @param offset        desplazamiento
560
         * @param scansize        tama?o de imagen recorrida por cada p
561
         */
562
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, 
563
                         int offset, int scansize) {
564
                image.setRGB(startX, startY, w, h, rgbArray, offset, scansize);
565
        }
566
        
567
        /**
568
         * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores 
569
         * con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
570
         * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
571
         * banda correspondiente a flags es sustituida por los datos del vector.
572
         * @param image        imagen con los datos actuales
573
         * @param startX        inicio de la posici?n en X dentro de la imagen
574
         * @param startY        inicio de la posici?n en X dentro de la imagen
575
         * @param w        Ancho de la imagen
576
         * @param h        Alto de la imagen
577
         * @param rgbArray        vector que contiene la banda que se va a sustituir
578
         * @param offset        desplazamiento
579
         * @param scansize        tama?o de imagen recorrida por cada paso
580
         * @param flags        banda que se va a sustituir (Ctes de GeoRasterFile)
581
         */
582
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, 
583
                         int offset, int scansize, int flags) {
584
                int [] line = new int[rgbArray.length]; 
585
                image.getRGB(startX, startY, w, h, line, offset, scansize);
586
                if (flags == GeoRasterFile.RED_BAND)
587
                        for (int i=0; i<line.length; i++)
588
                                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
589
                else if (flags == GeoRasterFile.GREEN_BAND)
590
                        for (int i=0; i<line.length; i++)
591
                                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
592
                else if (flags == GeoRasterFile.BLUE_BAND)
593
                        for (int i=0; i<line.length; i++)
594
                                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
595
                image.setRGB(startX, startY, w, h, line, offset, scansize);
596
        }
597
        
598

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

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

    
742