Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libCq CMS for java.old / src / org / cresques / io / MrSidFile.java @ 4578

History | View | Annotate | Download (30.6 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.MrSidNative.Contour;
35
import org.cresques.px.Extent;
36

    
37
import es.gva.cit.jmrsid.LTIColorSpace;
38
import es.gva.cit.jmrsid.LTIDataType;
39
import es.gva.cit.jmrsid.LTIGeoCoord;
40
import es.gva.cit.jmrsid.LTIImageStage;
41
import es.gva.cit.jmrsid.LTIMetadataDatabase;
42
import es.gva.cit.jmrsid.LTIMetadataRecord;
43
import es.gva.cit.jmrsid.LTIPixel;
44
import es.gva.cit.jmrsid.LTIScene;
45
import es.gva.cit.jmrsid.LTISceneBuffer;
46
import es.gva.cit.jmrsid.LTIUtils;
47
import es.gva.cit.jmrsid.MrSIDException;
48
import es.gva.cit.jmrsid.MrSIDImageReader;
49

    
50

    
51
/**
52
 * Soporte para los fichero MrSID de Lizardtech
53
 * @author Nacho Brodin (brodin_ign@gva.es)
54
 */
55
class MrSidNative extends MrSIDImageReader {
56
    static boolean WITH_OVERVIEWS = true;
57

    
58
    /**
59
     * Contorno en coordenadas geogr?ficas. (y Extent del raster).
60
     */
61
    public Contour esq = new Contour();
62
    public int width = 0;
63
    public int height = 0;
64
    public double originX = 0D;
65
    public double originY = 0D;
66
    public String version = "";
67
    public LTIMetadataDatabase metadata;
68
    public LTIPixel pixel = null;
69
    private int alpha = 0;
70
    protected int rBandNr = 1;
71
    protected int gBandNr = 2;
72
    protected int bBandNr = 3;
73
    protected byte[] bandR;
74
    protected byte[] bandG;
75
    protected byte[] bandB;
76
    private int dataType = LTIDataType.LTI_DATATYPE_UINT8;
77

    
78
    //View
79
    double currentViewY = -1;
80
    int currentFullWidth = -1;
81
    int currentFullHeight = -1;
82
    int currentViewWidth = -1;
83
    int currentViewHeight = -1;
84
    double currentViewX = 0D;
85
    double viewportScale = 0D;
86
    double step = 0D;
87
    int currentOverview = -1;
88
    private double zoomoverview = 0.0;
89
    int eColorSpace;
90
    int eSampleType;
91
    public int nbands;
92
    int noverviews;
93
    public int xini;
94
    public int yini;
95
    public int anchoOver;
96
    public int altoOver;
97
    public int blocksize = 1;
98

    
99
    /**
100
     * Constructor
101
     * @param fName
102
     * @throws MrSIDException
103
     * @throws IOException
104
     */
105
    public MrSidNative(String fName) throws MrSIDException, IOException {
106
        super(fName);
107
        init(fName);
108
    }
109
    
110
    /**
111
     * Inicializa las variables de instancia con los valores de la imagen
112
     * @param fName
113
     * @throws MrSIDException
114
     * @throws IOException
115
     */
116
    private void init(String fName) throws MrSIDException, IOException {
117
        this.initialize();
118

    
119
        String ext = fName.toLowerCase().substring(fName.lastIndexOf('.') + 1);
120

    
121
        width = this.getWidth();
122
        height = this.getHeight();
123
        eSampleType = this.getDataType();
124
        nbands = this.getNumBands();
125
        eColorSpace = this.getColorSpace();
126
        noverviews = this.getNumLevels();
127

    
128
        metadata = this.getMetadata();
129

    
130
        double ox = 0D;
131
        double oy = 0D;
132
        double resx = 0D;
133
        double resy = 0D;
134

    
135
        LTIGeoCoord geoc = this.getGeoCoord();
136

    
137
        ox = geoc.getX();
138
        oy = geoc.getY();
139
        resx = geoc.getXRes();
140
        resy = geoc.getYRes();
141

    
142
        System.out.println("Origin = (" + ox + "," + oy + ")");
143
        System.out.println("Pixel Size = (" + resx + "," + resy + ")");
144
        esq.add(new Point2D.Double(ox, oy));
145
        esq.add(new Point2D.Double(ox + (resx * width), oy));
146
        esq.add(new Point2D.Double(ox, oy + (resy * height)));
147
        esq.add(new Point2D.Double(ox + (resx * width), oy + (resy * height)));
148

    
149
        blocksize = this.getStripHeight();
150
        System.out.println("StripHeight = (" + blocksize + ")");
151
    }
152

    
153
    /**
154
     * Asigna el valor de Alpha
155
     * @param a        alpha
156
     */
157
    public void setAlpha(int a) {
158
        alpha = a;
159
    }
160

    
161
    /**
162
     * Asigna el tipo de datos
163
     * @param dt        tipo de datos
164
     */
165
    public void setDataType(int dt) {
166
        dataType = dt;
167
    }
168

    
169
    /**
170
     * Obtiene un punto 2D con las coordenadas del raster a partir de uno en coordenadas
171
     * del punto real.
172
     * @param pt        punto en coordenadas del punto real
173
     * @return        punto en coordenadas del raster
174
     */
175
    public Point2D worldToRaster(Point2D pt) {
176
        double x = (((double) currentFullWidth) / (esq.maxX - esq.minX)) * (pt.getX() -
177
                   esq.minX);
178
        double y = (((double) currentFullHeight) / (esq.maxY - esq.minY)) * (esq.maxY -
179
                   pt.getY());
180
        Point2D ptRes = new Point2D.Double(x, y);
181

    
182
        return ptRes;
183
    }
184

    
185
    /**
186
     * Calcula el overview a usar de la imagen y el viewport a partir del ancho, alto y
187
     * coordenadas del mundo real
188
     * @param dWorldTLX        Coordenada X superior izquierda
189
     * @param dWorldTLY        Coordenada Y superior izquierda
190
     * @param dWorldBRX        Coordenada X inferior derecha
191
     * @param dWorldBRY        Coordenada Y inferior derecha
192
     * @param nWidth        ancho
193
     * @param nHeight        alto
194
     */
195
    public void setView(double dWorldTLX, double dWorldTLY, double dWorldBRX,
196
                        double dWorldBRY, int nWidth, int nHeight) {
197
        //Ancho y alto de la im?gen en pixeles (pixeles de la overview)
198
        currentFullWidth = width;
199
        currentFullHeight = height;
200

    
201
        //Ventana de la imagen. (en tama?o completo)
202
        //tl->esq sup izda en pixeles
203
        //br->esq inf der en pixeles
204
        Point2D tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
205
        Point2D br = worldToRaster(new Point2D.Double(dWorldBRX, dWorldBRY));
206

    
207
        //Ancho y alto de la im?gen (pixeles en pantalla)
208
        currentViewWidth = nWidth;
209
        currentViewHeight = nHeight;
210

    
211
        currentViewX = tl.getX();
212
        currentViewY = tl.getY();
213

    
214
        viewportScale = (double) currentViewWidth / (br.getX() - tl.getX());
215
        currentViewY = tl.getY();
216

    
217
        try {
218
            // calcula el overview a usar
219
            int[] dims = null;
220
            double zoom = 1.0;
221
            zoomoverview = 1.0;
222
            currentOverview = -1;
223

    
224
            if (WITH_OVERVIEWS && ((noverviews - 1) > 0)) {
225
                for (int i = (noverviews - 1); i > 0; i--) {
226
                    zoom = LTIUtils.levelToMag(i);
227
                    dims = this.getDimsAtMag(zoom);
228

    
229
                    if (dims[0] > (this.getWidth() * viewportScale)) {
230
                        currentOverview = i;
231
                        zoomoverview = zoom;
232
                        viewportScale /= zoomoverview;
233
                        currentFullWidth = dims[0];
234
                        currentFullHeight = dims[1];
235
                        tl = worldToRaster(new Point2D.Double(dWorldTLX,
236
                                                              dWorldTLY));
237
                        currentViewX = tl.getX();
238
                        currentViewY = tl.getY();
239

    
240
                        break;
241
                    }
242
                }
243
            }
244

    
245
            setDataType(eSampleType);
246
        } catch (MrSIDException e) {
247
            e.printStackTrace();
248
        }
249
    }
250

    
251
    /**
252
     * Muestra alguna informaci?n para la depuraci?n
253
     */
254
    void pintaInfo() {
255
        try {
256
            System.out.println("GeoTransform:");
257

    
258
            LTIGeoCoord geoc = this.getGeoCoord();
259

    
260
            System.out.println("  param[0]=" + geoc.getX());
261
            System.out.println("  param[0]=" + geoc.getY());
262
            System.out.println("  param[0]=" + geoc.getXRes());
263
            System.out.println("  param[0]=" + geoc.getYRes());
264
            System.out.println("  param[0]=" + geoc.getXRot());
265
            System.out.println("  param[0]=" + geoc.getYRot());
266
            System.out.println("Metadata:");
267

    
268
            LTIMetadataDatabase metadata = this.getMetadata();
269

    
270
            for (int i = 0; i < metadata.getIndexCount(); i++) {
271
                LTIMetadataRecord rec = null;
272
                rec = metadata.getDataByIndex(i);
273
                System.out.println(rec.getTagName());
274

    
275
                if (rec.isScalar()) {
276
                    System.out.println(rec.getScalarData());
277
                } else if (rec.isVector()) {
278
                    String[] s = rec.getVectorData();
279

    
280
                    for (int j = 0; j < s.length; j++)
281
                        System.out.println("V" + j + "->" + s[j]);
282
                } else if (rec.isArray()) {
283
                    String[] s = rec.getArrayData();
284

    
285
                    for (int j = 0; j < s.length; j++)
286
                        System.out.println("A" + j + "->" + s[j]);
287
                } else {
288
                    System.out.println("");
289
                }
290
            }
291
        } catch (MrSIDException e) {
292
            // TODO Auto-generated catch block
293
            e.printStackTrace();
294
        }
295
    }
296

    
297
    void pintaPaleta() {
298
    }
299

    
300
    /**
301
     * Lee la escena de la imagen correspondiente a la vista seleccionada con
302
     * currentView a trav?s de la libreria de MrSid. Esta escena es cargada sobre
303
     * un buffer y asignada al par?metro de salida.
304
     * @param line        Escena leida
305
     * @throws MrSIDException Lanzada si ocurre un error en la lectura de la escena
306
     */
307
    public void readScene(int[] line) throws MrSIDException {
308
        int x = (int) currentViewX;
309
        int y = (int) currentViewY;
310
        int SceneWidth;
311
        int SceneHeight;
312

    
313
        try {
314
            if ((x == 0) && (y == 0)) {
315
                SceneWidth = currentFullWidth;
316
                SceneHeight = currentFullHeight;
317
            } else {
318
                                SceneWidth = (int) Math.round((((double) currentViewWidth) / viewportScale) + 1);
319
                                if (SceneWidth > currentFullWidth)
320
                                        SceneWidth = currentFullWidth;
321
                                SceneHeight = (int) Math.round((((double) currentViewHeight) / viewportScale) + 1);
322
                        if (SceneHeight > currentFullHeight)
323
                                SceneHeight = currentFullHeight;
324
            }
325

    
326
            if (SceneWidth == 0) {
327
                SceneWidth = 1;
328
            }
329

    
330
            if (SceneHeight == 0) {
331
                SceneHeight = 1;
332
            }
333

    
334
            if (pixel == null) {
335
                pixel = new LTIPixel(eColorSpace, nbands, eSampleType);
336
            }
337
            
338
            LTISceneBuffer buffer = null;
339
                        
340
            boolean  hecho = false;
341
                        while (!hecho){
342
                                LTIScene scene = new LTIScene(x, y, SceneWidth, SceneHeight,
343
                                                zoomoverview);
344

    
345
                                // Este deber?a ser el constructor con ventana
346
                                buffer = new LTISceneBuffer(pixel, SceneWidth,
347
                                                SceneHeight, true);
348

    
349
                                hecho = true;
350
                                try {
351
                                        ((LTIImageStage) this).read(scene, buffer);
352
                                        hecho = true;
353
                                } catch (MrSIDException ex) {
354
                                        SceneWidth--;
355
                                        SceneHeight--;
356
                                        hecho = false;
357
                                }
358
                        }
359
                        
360
            if ((dataType == LTIDataType.LTI_DATATYPE_UINT8) ||
361
                    (dataType == LTIDataType.LTI_DATATYPE_SINT8) ||
362
                    (dataType == LTIDataType.LTI_DATATYPE_SINT16) ||
363
                    (dataType == LTIDataType.LTI_DATATYPE_SINT32) ||
364
                    (dataType == LTIDataType.LTI_DATATYPE_UINT16) ||
365
                    (dataType == LTIDataType.LTI_DATATYPE_UINT32)) {
366
                int kd;
367
                int k;
368
                double scale = 1 / viewportScale;
369
                int alpha = (this.alpha & 0xff) << 24;
370
                             
371
                if (rBandNr == 1) {
372
                    bandR = buffer.buf1;
373
                } else if (rBandNr == 2) {
374
                    bandR = buffer.buf2;
375
                } else if (rBandNr == 3) {
376
                    bandR = buffer.buf3;
377
                }
378

    
379
                if (gBandNr == 1) {
380
                    bandG = buffer.buf1;
381
                } else if (gBandNr == 2) {
382
                    bandG = buffer.buf2;
383
                } else if (gBandNr == 3) {
384
                    bandG = buffer.buf3;
385
                }
386

    
387
                if (bBandNr == 1) {
388
                    bandB = buffer.buf1;
389
                } else if (bBandNr == 2) {
390
                    bandB = buffer.buf2;
391
                } else if (bBandNr == 3) {
392
                    bandB = buffer.buf3;
393
                }
394
                
395
                for (int y1 = 0; y1 < currentViewHeight; y1++)
396
                   for (int x1 = 0; x1 < currentViewWidth; x1++) {
397
                      kd = (y1 * currentViewWidth) + x1;
398
                      k = (((int) (y1 * scale)) * SceneWidth) +
399
                          (int) (((double) x1) * scale);
400

    
401
                      try {
402
                            line[kd] = alpha + ((0xff & bandR[k]) << 16) +
403
                                                                                                ((0xff & bandG[k]) << 8) +
404
                                                                                                (0xff & bandB[k]);
405
                      } catch (java.lang.ArrayIndexOutOfBoundsException e) {
406
                      }
407
                 }
408
                
409
            
410
            }
411
                /*if (eColorSpace == LTIColorSpace.LTI_COLORSPACE_GRAYSCALE) */
412
            
413
            buffer = null;
414
        } catch (MrSIDException e) {
415
            e.printStackTrace();
416
        }
417
    }
418

    
419
    /**
420
     * Lee una ventana de la imagen y devuelve un buffer de bytes
421
     * @param ulX        Coordenada X de la esquina superior izquierda
422
     * @param ulY        Coordenada Y de la esquina superior izquierda
423
     * @param sizeX        Tama?o X de la imagen
424
     * @param sizeY        Tama?o Y de la image
425
     * @param band        N?mero de bandas
426
     * @return        buffer con la ventana leida
427
     * @throws MrSIDException
428
     */
429
    public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band)
430
                     throws MrSIDException {
431
        if (pixel == null) {
432
            pixel = new LTIPixel(eColorSpace, nbands, eSampleType);
433
        }
434

    
435
        LTIScene scene = new LTIScene(ulX, ulY, sizeX, sizeY, 1.0);
436
        LTISceneBuffer buffer = new LTISceneBuffer(pixel, sizeX, sizeY, true);
437
        ((LTIImageStage) this).read(scene, buffer);
438

    
439
        if (band == 1) {
440
            return buffer.buf1;
441
        } else if (band == 2) {
442
            return buffer.buf2;
443
        } else if (band == 3) {
444
            return buffer.buf3;
445
        }
446

    
447
        return null;
448
    }
449

    
450
    // Polilinea con extent
451
    class Contour extends Vector {
452
        final private static long serialVersionUID = -3370601314380922368L;
453
        public double minX = Double.MAX_VALUE;
454
        public double minY = Double.MAX_VALUE;
455
        public double maxX = -Double.MAX_VALUE;
456
        public double maxY = -Double.MAX_VALUE;
457

    
458
        public Contour() {
459
            super();
460
        }
461

    
462
        public void add(Point2D pt) {
463
            super.add(pt);
464

    
465
            if (pt.getX() > maxX) {
466
                maxX = pt.getX();
467
            }
468

    
469
            if (pt.getX() < minX) {
470
                minX = pt.getX();
471
            }
472

    
473
            if (pt.getY() > maxY) {
474
                maxY = pt.getY();
475
            }
476

    
477
            if (pt.getY() < minY) {
478
                minY = pt.getY();
479
            }
480
        }
481
    }
482
}
483

    
484

    
485
/**
486
 * @author Nacho Brodin <brodin_ign@gva.es>
487
 *
488
 * Clase encargada del acceso a los datos y repintado de imagenes MrSID. Estos
489
 * son registrados con la extensi?n sid
490
 */
491
public class MrSidFile extends GeoRasterFile {
492
    public final static int BAND_HEIGHT = 64;
493

    
494
    static {
495
        GeoRasterFile.registerExtension("sid", MrSidFile.class);
496
    }
497

    
498
    protected MrSidNative file = null;
499
    private Extent v = null;
500

    
501
    /**
502
     * Contructor. Abre el fichero mrsid
503
     * @param proj        Proyecci?n
504
     * @param fName        Nombre del fichero mrsid
505
     */
506
    public MrSidFile(IProjection proj, String fName) {
507
        super(proj, fName);
508
        
509
        extent = new Extent();
510

    
511
        try {
512
            file = new MrSidNative(fName);
513
            showOnOpen();
514
            load();
515
            bandCount = file.nbands;
516

    
517
            if (bandCount > 2) {
518
                setBand(RED_BAND, 0);
519
                setBand(GREEN_BAND, 1);
520
                setBand(BLUE_BAND, 2);
521
            } else {
522
                setBand(RED_BAND | GREEN_BAND | BLUE_BAND, 0);
523
            }
524
        } catch (Exception e) {
525
            System.out.println("Error en constructor de MrSID");
526
            e.printStackTrace();
527
            file = null;
528
        }
529
    }
530

    
531
        /**
532
         * Obtenemos o calculamos el extent de la imagen.
533
         */
534
    public GeoFile load() {
535
            
536
                   extent = new Extent(file.esq.minX, file.esq.minY, file.esq.maxX, file.esq.maxY);
537
            
538
            /*if((this.assignedExtent == GeoRasterFile.IMAGE_EXTENT || this.assignedExtent == GeoRasterFile.ORDER ) 
539
                    && file !=null        && file.esq != null){
540
                    extent = new Extent(file.esq.minX, file.esq.minY, file.esq.maxX, file.esq.maxY);
541
                    return this;
542
            }
543
            
544
            if((this.assignedExtent == GeoRasterFile.ASSIGNED_EXTENT || this.assignedExtent == GeoRasterFile.ORDER ) 
545
                    && this.getTempExtent() != null){
546
        
547
                extent = this.getTempExtent();
548
                        file.esq = file.new Contour();
549
                          file.esq.add(new Point2D.Double(extent.minX(), extent.minY()));
550
                          file.esq.add(new Point2D.Double(extent.minX()+(extent.maxX() - extent.minX()), extent.minY()));
551
                          file.esq.add(new Point2D.Double(extent.minX(), extent.minY()+(extent.maxY() - extent.minY())));
552
                          file.esq.add(new Point2D.Double(extent.minX()+(extent.maxX() - extent.minX()), extent.minY()+(extent.maxY() - extent.minY())));
553
        }*/ 
554
        
555
        return this;
556
    }
557

    
558
    /**
559
     * Libera el objeto que ha abierto el fichero
560
     */
561
    public void close() {
562
            if(file != null){
563
                    file.close();
564
                    file = null;
565
            }
566
    }
567

    
568
    /**
569
     * Asigna una banda R, G o B
570
     */
571
    public void setBand(int flag, int bandNr) {
572
        super.setBand(flag, bandNr);
573

    
574
        if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) {
575
            file.rBandNr = bandNr + 1;
576
        }
577

    
578
        if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) {
579
            file.gBandNr = bandNr + 1;
580
        }
581

    
582
        if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) {
583
            file.bBandNr = bandNr + 1;
584
        }
585
    }
586

    
587
    /**
588
     * Asigna el extent de la vista
589
     */
590
    public void setView(Extent e) {
591
        v = new Extent(e);
592
    }
593

    
594
    /**
595
     * Obtiene el Extent de la vista
596
     */
597
    public Extent getView() {
598
        return v;
599
    }
600

    
601
    /**
602
     * Obtiene el ancho de la imagen
603
     */
604
    public int getWidth() {
605
        return file.width;
606
    }
607

    
608
    /**
609
     * Obtiene el alto de la imagen
610
     */
611
    public int getHeight() {
612
        return file.height;
613
    }
614

    
615
    public void reProject(ICoordTrans rp) {
616
        // TODO Auto-generated method stub        
617
    }
618

    
619
    /**
620
     * Actualiza la imagen. Se encarga de llamar a la funci?n que calcula la vista
621
     * y luego a la que lee la escena sobre un buffer. Vuelca la informaci?n obtenida
622
     * sobre el Image que la visualiza.
623
     */
624
    public Image updateImage(int width, int height, ICoordTrans rp) {
625
        double dFileAspect;
626
        double dWindowAspect;
627
        int line;
628
        int[] pRGBArray = null;
629
        Image image = null;
630

    
631
        // Work out the correct aspect for the setView call.
632
        dFileAspect = (double) v.width() / (double) v.height();
633
        dWindowAspect = (double) width / (double) height;
634

    
635
        if (dFileAspect > dWindowAspect) {
636
            height = (int) ((double) width / dFileAspect);
637
        } else {
638
            width = (int) ((double) height * dFileAspect);
639
        }
640

    
641
        // Set the view
642
        file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), width, height);
643

    
644
        //Impedimos que los valores de ancho y alto de la im?gen sean menores que 1
645
        if (width <= 0) {
646
            width = 1;
647
        }
648

    
649
        if (height <= 0) {
650
            height = 1;
651
        }
652

    
653
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
654
        pRGBArray = new int[width * height];
655

    
656
        try {
657
            file.setAlpha(getAlpha());
658
            
659
            setBand(RED_BAND, rBandNr);
660
            setBand(GREEN_BAND, gBandNr);
661
            setBand(BLUE_BAND, bBandNr);
662
                     
663

    
664
            file.readScene(pRGBArray);
665
            ((BufferedImage) image).setRGB(0, 0, width, height, pRGBArray, 0,
666
                                           width);
667
        } catch (Exception e) {
668
            e.printStackTrace();
669
        }
670

    
671
        return image;
672
    }
673

    
674
    /**
675
     * Muestra informaci?n del fichero abierto.
676
     *
677
     */
678
    private void showOnOpen() {
679
        // Report en la apertura (quitar)
680
        System.out.println("Fichero MrSID '" + getName() + "' abierto.");
681
        System.out.println("Version = " + file.version);
682
        System.out.println("   Size = (" + file.width + "," + file.height +
683
                           ")");
684
        System.out.println("   NumBands = (" + file.nbands + ")");
685

    
686
        //file.pintaInfo();
687
        file.pintaPaleta();
688
    }
689

    
690
    /**
691
     * Asigna al objeto Image los valores con los dato de la imagen contenidos en el
692
     * vector de enteros.
693
     * @param image        imagen con los datos actuales
694
     * @param startX        inicio de la posici?n en X dentro de la imagen
695
     * @param startY        inicio de la posici?n en X dentro de la imagen
696
     * @param w        Ancho de la imagen
697
     * @param h        Alto de la imagen
698
     * @param rgbArray        vector que contiene la banda que se va a sustituir
699
     * @param offset        desplazamiento
700
     * @param scansize        tama?o de imagen recorrida por cada p
701
     */
702
    protected void setRGBLine(BufferedImage image, int startX, int startY,
703
                              int w, int h, int[] rgbArray, int offset,
704
                              int scansize) {
705
        image.setRGB(startX, startY, w, h, rgbArray, offset, scansize);
706
    }
707

    
708
    /**
709
     * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores
710
     * con los datos de la imagen contenidos en el vector de enteros. De los valores RGB
711
     * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
712
     * banda correspondiente a flags es sustituida por los datos del vector.
713
     * @param image        imagen con los datos actuales
714
     * @param startX        inicio de la posici?n en X dentro de la imagen
715
     * @param startY        inicio de la posici?n en X dentro de la imagen
716
     * @param w        Ancho de la imagen
717
     * @param h        Alto de la imagen
718
     * @param rgbArray        vector que contiene la banda que se va a sustituir
719
     * @param offset        desplazamiento
720
     * @param scansize        tama?o de imagen recorrida por cada paso
721
     * @param flags        banda que se va a sustituir (Ctes de GeoRasterFile)
722
     */
723
    protected void setRGBLine(BufferedImage image, int startX, int startY,
724
                              int w, int h, int[] rgbArray, int offset,
725
                              int scansize, int flags) {
726
        int[] line = new int[rgbArray.length];
727
        image.getRGB(startX, startY, w, h, line, offset, scansize);
728

    
729
        if (flags == GeoRasterFile.RED_BAND) {
730
            for (int i = 0; i < line.length; i++)
731
                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
732
        } else if (flags == GeoRasterFile.GREEN_BAND) {
733
            for (int i = 0; i < line.length; i++)
734
                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
735
        } else if (flags == GeoRasterFile.BLUE_BAND) {
736
            for (int i = 0; i < line.length; i++)
737
                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
738
        }
739

    
740
        image.setRGB(startX, startY, w, h, line, offset, scansize);
741
    }
742

    
743
    /**
744
     * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores
745
     * con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
746
     * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
747
     * banda correspondiente a flags es sustituida por los datos del vector.
748
     * @param image        imagen con los datos actuales
749
     * @param startX        inicio de la posici?n en X dentro de la imagen
750
     * @param startY        inicio de la posici?n en X dentro de la imagen
751
     * @param w        Ancho de la imagen
752
     * @param h        Alto de la imagen
753
     * @param rgbArray        vector que contiene la banda que se va a sustituir
754
     * @param offset        desplazamiento
755
     * @param scansize        tama?o de imagen recorrida por cada paso
756
     * @param origBand        Banda origen del GeoRasterFile
757
     * @param destBandFlag        banda que se va a sustituir (Ctes de GeoRasterFile)
758
     */
759
    protected void setRGBLine(BufferedImage image, int startX, int startY,
760
                              int w, int h, int[] rgbArray, int offset,
761
                              int scansize, int origBand, int destBandFlag) {
762
        int[] line = new int[rgbArray.length];
763
        image.getRGB(startX, startY, w, h, line, offset, scansize);
764

    
765
        if ((origBand == 0) && (destBandFlag == GeoRasterFile.RED_BAND)) {
766
            for (int i = 0; i < line.length; i++)
767
                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
768
        } else if ((origBand == 1) &&
769
                       (destBandFlag == GeoRasterFile.GREEN_BAND)) {
770
            for (int i = 0; i < line.length; i++)
771
                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
772
        } else if ((origBand == 2) &&
773
                       (destBandFlag == GeoRasterFile.BLUE_BAND)) {
774
            for (int i = 0; i < line.length; i++)
775
                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
776
        } else if ((origBand == 0) &&
777
                       (destBandFlag == GeoRasterFile.GREEN_BAND)) {
778
            for (int i = 0; i < line.length; i++)
779
                line[i] = (line[i] & 0xffff00ff) |
780
                          ((rgbArray[i] & 0x00ff0000) >> 8);
781
        } else if ((origBand == 0) &&
782
                       (destBandFlag == GeoRasterFile.BLUE_BAND)) {
783
            for (int i = 0; i < line.length; i++)
784
                line[i] = (line[i] & 0xffffff00) |
785
                          ((rgbArray[i] & 0x00ff0000) >> 16);
786
        } else if ((origBand == 1) && (destBandFlag == GeoRasterFile.RED_BAND)) {
787
            for (int i = 0; i < line.length; i++)
788
                line[i] = (line[i] & 0xff00ffff) |
789
                          ((rgbArray[i] & 0x0000ff00) << 8);
790
        } else if ((origBand == 1) &&
791
                       (destBandFlag == GeoRasterFile.BLUE_BAND)) {
792
            for (int i = 0; i < line.length; i++)
793
                line[i] = (line[i] & 0xffffff00) |
794
                          ((rgbArray[i] & 0x0000ff00) >> 8);
795
        } else if ((origBand == 2) && (destBandFlag == GeoRasterFile.RED_BAND)) {
796
            for (int i = 0; i < line.length; i++)
797
                line[i] = (line[i] & 0xff00ffff) |
798
                          ((rgbArray[i] & 0x000000ff) << 16);
799
        } else if ((origBand == 2) &&
800
                       (destBandFlag == GeoRasterFile.GREEN_BAND)) {
801
            for (int i = 0; i < line.length; i++)
802
                line[i] = (line[i] & 0xffff00ff) |
803
                          ((rgbArray[i] & 0x000000ff) << 8);
804
        }
805

    
806
        image.setRGB(startX, startY, w, h, line, offset, scansize);
807
    }
808

    
809
    /* (non-Javadoc)
810
     * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans, java.awt.Image, int)
811
     */
812
    public Image updateImage(int width, int height, ICoordTrans rp, Image img,
813
                             int origBand, int destBandFlag) {
814
        double dFileAspect;
815
        double dWindowAspect;
816
        int line;
817
        int[] pRGBArray = null;
818
        Image mrSidImage = null;
819

    
820
        // Work out the correct aspect for the setView call.
821
        dFileAspect = (double) v.width() / (double) v.height();
822
        dWindowAspect = (double) width / (double) height;
823

    
824
        if (dFileAspect > dWindowAspect) {
825
            height = (int) ((double) width / dFileAspect);
826
        } else {
827
            width = (int) ((double) height * dFileAspect);
828
        }
829

    
830
        // Set the view
831
        file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), width, height);
832

    
833
        //Impedimos que los valores de ancho y alto de la im?gen sean menores que 1
834
        if (width <= 0) {
835
            width = 1;
836
        }
837

    
838
        if (height <= 0) {
839
            height = 1;
840
        }
841

    
842
        file.setAlpha(getAlpha());
843
      
844
        setBand(RED_BAND, rBandNr);
845
        setBand(GREEN_BAND, gBandNr);
846
        setBand(BLUE_BAND, bBandNr);
847
       
848
        pRGBArray = new int[width * height];
849

    
850
        if (img == null) { //Caso en el que se crea una imagen
851
            mrSidImage = new BufferedImage(width, height,
852
                                           BufferedImage.TYPE_INT_ARGB);
853

    
854
            try {
855
                file.readScene(pRGBArray);
856
                setRGBLine((BufferedImage) mrSidImage, 0, 0, width, height,
857
                           pRGBArray, 0, width);
858
            } catch (Exception e) {
859
                e.printStackTrace();
860
            }
861

    
862
            return mrSidImage;
863
        } else { //Caso en el que se actualiza una banda del Image
864

    
865
            try {
866
                file.readScene(pRGBArray);
867
                setRGBLine((BufferedImage) img, 0, 0, width, height, pRGBArray,
868
                           0, width, origBand, destBandFlag);
869
            } catch (Exception e) {
870
                e.printStackTrace();
871
            }
872

    
873
            return img;
874
        }
875
    }
876

    
877
    /* (non-Javadoc)
878
     * @see org.cresques.io.GeoRasterFile#getData(int, int, int)
879
     */
880
    public Object getData(int x, int y, int band) {
881
        // TODO Auto-generated method stub
882
        return null;
883
    }
884

    
885
    /**
886
     * Devuelve los datos de una ventana solicitada
887
     * @param ulX        coordenada X superior izda.
888
     * @param ulY        coordenada Y superior derecha.
889
     * @param sizeX        tama?o en X de la ventana.
890
     * @param sizeY tama?o en Y de la ventana.
891
     * @param band        Banda solicitada.
892
     */
893
    public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band) {
894
        try {
895
            return file.getWindow(ulX, ulY, sizeX, sizeY, band);
896
        } catch (MrSIDException e) {
897
            e.printStackTrace();
898
        }
899

    
900
        return null;
901
    }
902

    
903
    /**
904
     * Devuelve el tama?o de bloque
905
     * @return Tama?o de bloque
906
     */
907
    public int getBlockSize() {
908
        return file.blocksize;
909
    }
910
}