Statistics
| Revision:

root / trunk / libraries / libCq CMS for java.old / src / org / cresques / io / MrSidFile.java @ 3016

History | View | Annotate | Download (30.2 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 a = 0;
309
        int x = (int) currentViewX;
310
        int y = (int) currentViewY;
311
        int SceneWidth;
312
        int SceneHeight;
313

    
314
        try {
315
            if ((x == 0) && (y == 0)) {
316
                SceneWidth = currentFullWidth;
317
                SceneHeight = currentFullHeight;
318
            } else {
319
                SceneWidth = (int) (((double) currentViewWidth) / viewportScale);
320
                SceneHeight = (int) (((double) currentViewHeight) / viewportScale);
321
            }
322

    
323
            if (SceneWidth == 0) {
324
                SceneWidth = 1;
325
            }
326

    
327
            if (SceneHeight == 0) {
328
                SceneHeight = 1;
329
            }
330

    
331
            if (pixel == null) {
332
                pixel = new LTIPixel(eColorSpace, nbands, eSampleType);
333
            }
334

    
335
            LTIScene scene = new LTIScene(x, y, SceneWidth, SceneHeight,
336
                                          zoomoverview);
337

    
338
            // Este deber?a ser el constructor con ventana
339
            LTISceneBuffer buffer = new LTISceneBuffer(pixel, SceneWidth,
340
                                                       SceneHeight, true);
341

    
342
            ((LTIImageStage) this).read(scene, buffer);
343

    
344
            if ((dataType == LTIDataType.LTI_DATATYPE_UINT8) ||
345
                    (dataType == LTIDataType.LTI_DATATYPE_SINT8) ||
346
                    (dataType == LTIDataType.LTI_DATATYPE_SINT16) ||
347
                    (dataType == LTIDataType.LTI_DATATYPE_SINT32) ||
348
                    (dataType == LTIDataType.LTI_DATATYPE_UINT16) ||
349
                    (dataType == LTIDataType.LTI_DATATYPE_UINT32)) {
350
                int kd;
351
                int k;
352
                double scale = 1 / viewportScale;
353
                int alpha = (this.alpha & 0xff) << 24;
354
                             
355
                if (rBandNr == 1) {
356
                    bandR = buffer.buf1;
357
                } else if (rBandNr == 2) {
358
                    bandR = buffer.buf2;
359
                } else if (rBandNr == 3) {
360
                    bandR = buffer.buf3;
361
                }
362

    
363
                if (gBandNr == 1) {
364
                    bandG = buffer.buf1;
365
                } else if (gBandNr == 2) {
366
                    bandG = buffer.buf2;
367
                } else if (gBandNr == 3) {
368
                    bandG = buffer.buf3;
369
                }
370

    
371
                if (bBandNr == 1) {
372
                    bandB = buffer.buf1;
373
                } else if (bBandNr == 2) {
374
                    bandB = buffer.buf2;
375
                } else if (bBandNr == 3) {
376
                    bandB = buffer.buf3;
377
                }
378
                
379
                for (int y1 = 0; y1 < currentViewHeight; y1++)
380
                   for (int x1 = 0; x1 < currentViewWidth; x1++) {
381
                      kd = (y1 * currentViewWidth) + x1;
382
                      k = (((int) (y1 * scale)) * SceneWidth) +
383
                          (int) (((double) x1) * scale);
384

    
385
                      try {
386
                            line[kd] = alpha + ((0xff & bandR[k]) << 16) +
387
                                                                                                ((0xff & bandG[k]) << 8) +
388
                                                                                                (0xff & bandB[k]);
389
                      } catch (java.lang.ArrayIndexOutOfBoundsException e) {
390
                      }
391
                   }
392
                }
393

    
394
                /*if (eColorSpace == LTIColorSpace.LTI_COLORSPACE_GRAYSCALE) */
395
            
396
            buffer = null;
397
        } catch (MrSIDException e) {
398
            e.printStackTrace();
399
        }
400
    }
401

    
402
    /**
403
     * Lee una ventana de la imagen y devuelve un buffer de bytes
404
     * @param ulX        Coordenada X de la esquina superior izquierda
405
     * @param ulY        Coordenada Y de la esquina superior izquierda
406
     * @param sizeX        Tama?o X de la imagen
407
     * @param sizeY        Tama?o Y de la image
408
     * @param band        N?mero de bandas
409
     * @return        buffer con la ventana leida
410
     * @throws MrSIDException
411
     */
412
    public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band)
413
                     throws MrSIDException {
414
        if (pixel == null) {
415
            pixel = new LTIPixel(eColorSpace, nbands, eSampleType);
416
        }
417

    
418
        LTIScene scene = new LTIScene(ulX, ulY, sizeX, sizeY, 1.0);
419
        LTISceneBuffer buffer = new LTISceneBuffer(pixel, sizeX, sizeY, true);
420
        ((LTIImageStage) this).read(scene, buffer);
421

    
422
        if (band == 1) {
423
            return buffer.buf1;
424
        } else if (band == 2) {
425
            return buffer.buf2;
426
        } else if (band == 3) {
427
            return buffer.buf3;
428
        }
429

    
430
        return null;
431
    }
432

    
433
    // Polilinea con extent
434
    class Contour extends Vector {
435
        final private static long serialVersionUID = -3370601314380922368L;
436
        public double minX = Double.MAX_VALUE;
437
        public double minY = Double.MAX_VALUE;
438
        public double maxX = -Double.MAX_VALUE;
439
        public double maxY = -Double.MAX_VALUE;
440

    
441
        public Contour() {
442
            super();
443
        }
444

    
445
        public void add(Point2D pt) {
446
            super.add(pt);
447

    
448
            if (pt.getX() > maxX) {
449
                maxX = pt.getX();
450
            }
451

    
452
            if (pt.getX() < minX) {
453
                minX = pt.getX();
454
            }
455

    
456
            if (pt.getY() > maxY) {
457
                maxY = pt.getY();
458
            }
459

    
460
            if (pt.getY() < minY) {
461
                minY = pt.getY();
462
            }
463
        }
464
    }
465
}
466

    
467

    
468
/**
469
 * @author Nacho Brodin <brodin_ign@gva.es>
470
 *
471
 * Clase encargada del acceso a los datos y repintado de imagenes MrSID. Estos
472
 * son registrados con la extensi?n sid
473
 */
474
public class MrSidFile extends GeoRasterFile {
475
    public final static int BAND_HEIGHT = 64;
476

    
477
    static {
478
        GeoRasterFile.registerExtension("sid", MrSidFile.class);
479
    }
480

    
481
    protected MrSidNative file = null;
482
    private Extent v = null;
483

    
484
    /**
485
     * Contructor. Abre el fichero mrsid
486
     * @param proj        Proyecci?n
487
     * @param fName        Nombre del fichero mrsid
488
     */
489
    public MrSidFile(IProjection proj, String fName) {
490
        super(proj, fName);
491
        extent = new Extent();
492

    
493
        try {
494
            file = new MrSidNative(fName);
495
            showOnOpen();
496
            load();
497
            bandCount = file.nbands;
498

    
499
            if (bandCount > 2) {
500
                setBand(RED_BAND, 0);
501
                setBand(GREEN_BAND, 1);
502
                setBand(BLUE_BAND, 2);
503
            } else {
504
                setBand(RED_BAND | GREEN_BAND | BLUE_BAND, 0);
505
            }
506
        } catch (Exception e) {
507
            System.out.println("Error en constructor de MrSID");
508
            e.printStackTrace();
509
            file = null;
510
        }
511
    }
512

    
513
        /**
514
         * Obtenemos o calculamos el extent de la imagen.
515
         */
516
    public GeoFile load() {
517
            
518
                   extent = new Extent(file.esq.minX, file.esq.minY, file.esq.maxX, file.esq.maxY);
519
            
520
            /*if((this.assignedExtent == GeoRasterFile.IMAGE_EXTENT || this.assignedExtent == GeoRasterFile.ORDER ) 
521
                    && file !=null        && file.esq != null){
522
                    extent = new Extent(file.esq.minX, file.esq.minY, file.esq.maxX, file.esq.maxY);
523
                    return this;
524
            }
525
            
526
            if((this.assignedExtent == GeoRasterFile.ASSIGNED_EXTENT || this.assignedExtent == GeoRasterFile.ORDER ) 
527
                    && this.getTempExtent() != null){
528
        
529
                extent = this.getTempExtent();
530
                        file.esq = file.new Contour();
531
                          file.esq.add(new Point2D.Double(extent.minX(), extent.minY()));
532
                          file.esq.add(new Point2D.Double(extent.minX()+(extent.maxX() - extent.minX()), extent.minY()));
533
                          file.esq.add(new Point2D.Double(extent.minX(), extent.minY()+(extent.maxY() - extent.minY())));
534
                          file.esq.add(new Point2D.Double(extent.minX()+(extent.maxX() - extent.minX()), extent.minY()+(extent.maxY() - extent.minY())));
535
        }*/ 
536
        
537
        return this;
538
    }
539

    
540
    /**
541
     * Libera el objeto que ha abierto el fichero
542
     */
543
    public void close() {
544
        file = null;
545
    }
546

    
547
    /**
548
     * Asigna una banda R, G o B
549
     */
550
    public void setBand(int flag, int bandNr) {
551
        super.setBand(flag, bandNr);
552

    
553
        if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) {
554
            file.rBandNr = bandNr + 1;
555
        }
556

    
557
        if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) {
558
            file.gBandNr = bandNr + 1;
559
        }
560

    
561
        if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) {
562
            file.bBandNr = bandNr + 1;
563
        }
564
    }
565

    
566
    /**
567
     * Asigna el extent de la vista
568
     */
569
    public void setView(Extent e) {
570
        v = new Extent(e);
571
    }
572

    
573
    /**
574
     * Obtiene el Extent de la vista
575
     */
576
    public Extent getView() {
577
        return v;
578
    }
579

    
580
    /**
581
     * Obtiene el ancho de la imagen
582
     */
583
    public int getWidth() {
584
        return file.width;
585
    }
586

    
587
    /**
588
     * Obtiene el alto de la imagen
589
     */
590
    public int getHeight() {
591
        return file.height;
592
    }
593

    
594
    public void reProject(ICoordTrans rp) {
595
        // TODO Auto-generated method stub        
596
    }
597

    
598
    /**
599
     * Actualiza la imagen. Se encarga de llamar a la funci?n que calcula la vista
600
     * y luego a la que lee la escena sobre un buffer. Vuelca la informaci?n obtenida
601
     * sobre el Image que la visualiza.
602
     */
603
    public Image updateImage(int width, int height, ICoordTrans rp) {
604
        double dFileAspect;
605
        double dWindowAspect;
606
        int line;
607
        int[] pRGBArray = null;
608
        Image image = null;
609

    
610
        // Work out the correct aspect for the setView call.
611
        dFileAspect = (double) v.width() / (double) v.height();
612
        dWindowAspect = (double) width / (double) height;
613

    
614
        if (dFileAspect > dWindowAspect) {
615
            height = (int) ((double) width / dFileAspect);
616
        } else {
617
            width = (int) ((double) height * dFileAspect);
618
        }
619

    
620
        // Set the view
621
        file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), width, height);
622

    
623
        //Impedimos que los valores de ancho y alto de la im?gen sean menores que 1
624
        if (width <= 0) {
625
            width = 1;
626
        }
627

    
628
        if (height <= 0) {
629
            height = 1;
630
        }
631

    
632
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
633
        pRGBArray = new int[width * height];
634

    
635
        try {
636
            file.setAlpha(getAlpha());
637
            
638
            setBand(RED_BAND, rBandNr);
639
            setBand(GREEN_BAND, gBandNr);
640
            setBand(BLUE_BAND, bBandNr);
641
                     
642

    
643
            file.readScene(pRGBArray);
644
            ((BufferedImage) image).setRGB(0, 0, width, height, pRGBArray, 0,
645
                                           width);
646
        } catch (Exception e) {
647
            e.printStackTrace();
648
        }
649

    
650
        return image;
651
    }
652

    
653
    /**
654
     * Muestra informaci?n del fichero abierto.
655
     *
656
     */
657
    private void showOnOpen() {
658
        // Report en la apertura (quitar)
659
        System.out.println("Fichero MrSID '" + getName() + "' abierto.");
660
        System.out.println("Version = " + file.version);
661
        System.out.println("   Size = (" + file.width + "," + file.height +
662
                           ")");
663
        System.out.println("   NumBands = (" + file.nbands + ")");
664

    
665
        file.pintaInfo();
666
        file.pintaPaleta();
667
    }
668

    
669
    /**
670
     * Asigna al objeto Image los valores con los dato de la imagen contenidos en el
671
     * vector de enteros.
672
     * @param image        imagen con los datos actuales
673
     * @param startX        inicio de la posici?n en X dentro de la imagen
674
     * @param startY        inicio de la posici?n en X dentro de la imagen
675
     * @param w        Ancho de la imagen
676
     * @param h        Alto de la imagen
677
     * @param rgbArray        vector que contiene la banda que se va a sustituir
678
     * @param offset        desplazamiento
679
     * @param scansize        tama?o de imagen recorrida por cada p
680
     */
681
    protected void setRGBLine(BufferedImage image, int startX, int startY,
682
                              int w, int h, int[] rgbArray, int offset,
683
                              int scansize) {
684
        image.setRGB(startX, startY, w, h, rgbArray, offset, scansize);
685
    }
686

    
687
    /**
688
     * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores
689
     * con los datos de la imagen contenidos en el vector de enteros. De los valores RGB
690
     * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
691
     * banda correspondiente a flags es sustituida por los datos del vector.
692
     * @param image        imagen con los datos actuales
693
     * @param startX        inicio de la posici?n en X dentro de la imagen
694
     * @param startY        inicio de la posici?n en X dentro de la imagen
695
     * @param w        Ancho de la imagen
696
     * @param h        Alto de la imagen
697
     * @param rgbArray        vector que contiene la banda que se va a sustituir
698
     * @param offset        desplazamiento
699
     * @param scansize        tama?o de imagen recorrida por cada paso
700
     * @param flags        banda que se va a sustituir (Ctes de GeoRasterFile)
701
     */
702
    protected void setRGBLine(BufferedImage image, int startX, int startY,
703
                              int w, int h, int[] rgbArray, int offset,
704
                              int scansize, int flags) {
705
        int[] line = new int[rgbArray.length];
706
        image.getRGB(startX, startY, w, h, line, offset, scansize);
707

    
708
        if (flags == GeoRasterFile.RED_BAND) {
709
            for (int i = 0; i < line.length; i++)
710
                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
711
        } else if (flags == GeoRasterFile.GREEN_BAND) {
712
            for (int i = 0; i < line.length; i++)
713
                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
714
        } else if (flags == GeoRasterFile.BLUE_BAND) {
715
            for (int i = 0; i < line.length; i++)
716
                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
717
        }
718

    
719
        image.setRGB(startX, startY, w, h, line, offset, scansize);
720
    }
721

    
722
    /**
723
     * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores
724
     * con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
725
     * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
726
     * banda correspondiente a flags es sustituida por los datos del vector.
727
     * @param image        imagen con los datos actuales
728
     * @param startX        inicio de la posici?n en X dentro de la imagen
729
     * @param startY        inicio de la posici?n en X dentro de la imagen
730
     * @param w        Ancho de la imagen
731
     * @param h        Alto de la imagen
732
     * @param rgbArray        vector que contiene la banda que se va a sustituir
733
     * @param offset        desplazamiento
734
     * @param scansize        tama?o de imagen recorrida por cada paso
735
     * @param origBand        Banda origen del GeoRasterFile
736
     * @param destBandFlag        banda que se va a sustituir (Ctes de GeoRasterFile)
737
     */
738
    protected void setRGBLine(BufferedImage image, int startX, int startY,
739
                              int w, int h, int[] rgbArray, int offset,
740
                              int scansize, int origBand, int destBandFlag) {
741
        int[] line = new int[rgbArray.length];
742
        image.getRGB(startX, startY, w, h, line, offset, scansize);
743

    
744
        if ((origBand == 0) && (destBandFlag == GeoRasterFile.RED_BAND)) {
745
            for (int i = 0; i < line.length; i++)
746
                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
747
        } else if ((origBand == 1) &&
748
                       (destBandFlag == GeoRasterFile.GREEN_BAND)) {
749
            for (int i = 0; i < line.length; i++)
750
                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
751
        } else if ((origBand == 2) &&
752
                       (destBandFlag == GeoRasterFile.BLUE_BAND)) {
753
            for (int i = 0; i < line.length; i++)
754
                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
755
        } else if ((origBand == 0) &&
756
                       (destBandFlag == GeoRasterFile.GREEN_BAND)) {
757
            for (int i = 0; i < line.length; i++)
758
                line[i] = (line[i] & 0xffff00ff) |
759
                          ((rgbArray[i] & 0x00ff0000) >> 8);
760
        } else if ((origBand == 0) &&
761
                       (destBandFlag == GeoRasterFile.BLUE_BAND)) {
762
            for (int i = 0; i < line.length; i++)
763
                line[i] = (line[i] & 0xffffff00) |
764
                          ((rgbArray[i] & 0x00ff0000) >> 16);
765
        } else if ((origBand == 1) && (destBandFlag == GeoRasterFile.RED_BAND)) {
766
            for (int i = 0; i < line.length; i++)
767
                line[i] = (line[i] & 0xff00ffff) |
768
                          ((rgbArray[i] & 0x0000ff00) << 8);
769
        } else if ((origBand == 1) &&
770
                       (destBandFlag == GeoRasterFile.BLUE_BAND)) {
771
            for (int i = 0; i < line.length; i++)
772
                line[i] = (line[i] & 0xffffff00) |
773
                          ((rgbArray[i] & 0x0000ff00) >> 8);
774
        } else if ((origBand == 2) && (destBandFlag == GeoRasterFile.RED_BAND)) {
775
            for (int i = 0; i < line.length; i++)
776
                line[i] = (line[i] & 0xff00ffff) |
777
                          ((rgbArray[i] & 0x000000ff) << 16);
778
        } else if ((origBand == 2) &&
779
                       (destBandFlag == GeoRasterFile.GREEN_BAND)) {
780
            for (int i = 0; i < line.length; i++)
781
                line[i] = (line[i] & 0xffff00ff) |
782
                          ((rgbArray[i] & 0x000000ff) << 8);
783
        }
784

    
785
        image.setRGB(startX, startY, w, h, line, offset, scansize);
786
    }
787

    
788
    /* (non-Javadoc)
789
     * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans, java.awt.Image, int)
790
     */
791
    public Image updateImage(int width, int height, ICoordTrans rp, Image img,
792
                             int origBand, int destBandFlag) {
793
        double dFileAspect;
794
        double dWindowAspect;
795
        int line;
796
        int[] pRGBArray = null;
797
        Image mrSidImage = null;
798

    
799
        // Work out the correct aspect for the setView call.
800
        dFileAspect = (double) v.width() / (double) v.height();
801
        dWindowAspect = (double) width / (double) height;
802

    
803
        if (dFileAspect > dWindowAspect) {
804
            height = (int) ((double) width / dFileAspect);
805
        } else {
806
            width = (int) ((double) height * dFileAspect);
807
        }
808

    
809
        // Set the view
810
        file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), width, height);
811

    
812
        //Impedimos que los valores de ancho y alto de la im?gen sean menores que 1
813
        if (width <= 0) {
814
            width = 1;
815
        }
816

    
817
        if (height <= 0) {
818
            height = 1;
819
        }
820

    
821
        file.setAlpha(getAlpha());
822
      
823
        setBand(RED_BAND, rBandNr);
824
        setBand(GREEN_BAND, gBandNr);
825
        setBand(BLUE_BAND, bBandNr);
826
       
827
        pRGBArray = new int[width * height];
828

    
829
        if (img == null) { //Caso en el que se crea una imagen
830
            mrSidImage = new BufferedImage(width, height,
831
                                           BufferedImage.TYPE_INT_ARGB);
832

    
833
            try {
834
                file.readScene(pRGBArray);
835
                setRGBLine((BufferedImage) mrSidImage, 0, 0, width, height,
836
                           pRGBArray, 0, width);
837
            } catch (Exception e) {
838
                e.printStackTrace();
839
            }
840

    
841
            return mrSidImage;
842
        } else { //Caso en el que se actualiza una banda del Image
843

    
844
            try {
845
                file.readScene(pRGBArray);
846
                setRGBLine((BufferedImage) img, 0, 0, width, height, pRGBArray,
847
                           0, width, origBand, destBandFlag);
848
            } catch (Exception e) {
849
                e.printStackTrace();
850
            }
851

    
852
            return img;
853
        }
854
    }
855

    
856
    /* (non-Javadoc)
857
     * @see org.cresques.io.GeoRasterFile#getData(int, int, int)
858
     */
859
    public Object getData(int x, int y, int band) {
860
        // TODO Auto-generated method stub
861
        return null;
862
    }
863

    
864
    /**
865
     * Devuelve los datos de una ventana solicitada
866
     * @param ulX        coordenada X superior izda.
867
     * @param ulY        coordenada Y superior derecha.
868
     * @param sizeX        tama?o en X de la ventana.
869
     * @param sizeY tama?o en Y de la ventana.
870
     * @param band        Banda solicitada.
871
     */
872
    public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band) {
873
        try {
874
            return file.getWindow(ulX, ulY, sizeX, sizeY, band);
875
        } catch (MrSIDException e) {
876
            e.printStackTrace();
877
        }
878

    
879
        return null;
880
    }
881

    
882
    /**
883
     * Devuelve el tama?o de bloque
884
     * @return Tama?o de bloque
885
     */
886
    public int getBlockSize() {
887
        return file.blocksize;
888
    }
889
}