Statistics
| Revision:

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

History | View | Annotate | Download (30.4 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) Math.round((((double) currentViewWidth) / viewportScale) + 1);
320
                                if (SceneWidth > currentFullWidth)
321
                                        SceneWidth = currentFullWidth;
322
                                SceneHeight = (int) Math.round((((double) currentViewHeight) / viewportScale) + 1);
323
                        if (SceneHeight > currentFullHeight)
324
                                SceneHeight = currentFullHeight;
325
            }
326

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

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

    
335
            if (pixel == null) {
336
                pixel = new LTIPixel(eColorSpace, nbands, eSampleType);
337
            }
338

    
339
            LTIScene scene = new LTIScene(x, y, SceneWidth, SceneHeight,
340
                                          zoomoverview);
341

    
342
            // Este deber?a ser el constructor con ventana
343
            LTISceneBuffer buffer = new LTISceneBuffer(pixel, SceneWidth,
344
                                                       SceneHeight, true);
345

    
346
            ((LTIImageStage) this).read(scene, buffer);
347

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

    
367
                if (gBandNr == 1) {
368
                    bandG = buffer.buf1;
369
                } else if (gBandNr == 2) {
370
                    bandG = buffer.buf2;
371
                } else if (gBandNr == 3) {
372
                    bandG = buffer.buf3;
373
                }
374

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

    
389
                      try {
390
                            line[kd] = alpha + ((0xff & bandR[k]) << 16) +
391
                                                                                                ((0xff & bandG[k]) << 8) +
392
                                                                                                (0xff & bandB[k]);
393
                      } catch (java.lang.ArrayIndexOutOfBoundsException e) {
394
                      }
395
                 }
396
                
397
            
398
            }
399
                /*if (eColorSpace == LTIColorSpace.LTI_COLORSPACE_GRAYSCALE) */
400
            
401
            buffer = null;
402
        } catch (MrSIDException e) {
403
            e.printStackTrace();
404
        }
405
    }
406

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

    
423
        LTIScene scene = new LTIScene(ulX, ulY, sizeX, sizeY, 1.0);
424
        LTISceneBuffer buffer = new LTISceneBuffer(pixel, sizeX, sizeY, true);
425
        ((LTIImageStage) this).read(scene, buffer);
426

    
427
        if (band == 1) {
428
            return buffer.buf1;
429
        } else if (band == 2) {
430
            return buffer.buf2;
431
        } else if (band == 3) {
432
            return buffer.buf3;
433
        }
434

    
435
        return null;
436
    }
437

    
438
    // Polilinea con extent
439
    class Contour extends Vector {
440
        final private static long serialVersionUID = -3370601314380922368L;
441
        public double minX = Double.MAX_VALUE;
442
        public double minY = Double.MAX_VALUE;
443
        public double maxX = -Double.MAX_VALUE;
444
        public double maxY = -Double.MAX_VALUE;
445

    
446
        public Contour() {
447
            super();
448
        }
449

    
450
        public void add(Point2D pt) {
451
            super.add(pt);
452

    
453
            if (pt.getX() > maxX) {
454
                maxX = pt.getX();
455
            }
456

    
457
            if (pt.getX() < minX) {
458
                minX = pt.getX();
459
            }
460

    
461
            if (pt.getY() > maxY) {
462
                maxY = pt.getY();
463
            }
464

    
465
            if (pt.getY() < minY) {
466
                minY = pt.getY();
467
            }
468
        }
469
    }
470
}
471

    
472

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

    
482
    static {
483
        GeoRasterFile.registerExtension("sid", MrSidFile.class);
484
    }
485

    
486
    protected MrSidNative file = null;
487
    private Extent v = null;
488

    
489
    /**
490
     * Contructor. Abre el fichero mrsid
491
     * @param proj        Proyecci?n
492
     * @param fName        Nombre del fichero mrsid
493
     */
494
    public MrSidFile(IProjection proj, String fName) {
495
        super(proj, fName);
496
        extent = new Extent();
497

    
498
        try {
499
            file = new MrSidNative(fName);
500
            showOnOpen();
501
            load();
502
            bandCount = file.nbands;
503

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

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

    
545
    /**
546
     * Libera el objeto que ha abierto el fichero
547
     */
548
    public void close() {
549
        file = null;
550
    }
551

    
552
    /**
553
     * Asigna una banda R, G o B
554
     */
555
    public void setBand(int flag, int bandNr) {
556
        super.setBand(flag, bandNr);
557

    
558
        if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) {
559
            file.rBandNr = bandNr + 1;
560
        }
561

    
562
        if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) {
563
            file.gBandNr = bandNr + 1;
564
        }
565

    
566
        if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) {
567
            file.bBandNr = bandNr + 1;
568
        }
569
    }
570

    
571
    /**
572
     * Asigna el extent de la vista
573
     */
574
    public void setView(Extent e) {
575
        v = new Extent(e);
576
    }
577

    
578
    /**
579
     * Obtiene el Extent de la vista
580
     */
581
    public Extent getView() {
582
        return v;
583
    }
584

    
585
    /**
586
     * Obtiene el ancho de la imagen
587
     */
588
    public int getWidth() {
589
        return file.width;
590
    }
591

    
592
    /**
593
     * Obtiene el alto de la imagen
594
     */
595
    public int getHeight() {
596
        return file.height;
597
    }
598

    
599
    public void reProject(ICoordTrans rp) {
600
        // TODO Auto-generated method stub        
601
    }
602

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

    
615
        // Work out the correct aspect for the setView call.
616
        dFileAspect = (double) v.width() / (double) v.height();
617
        dWindowAspect = (double) width / (double) height;
618

    
619
        if (dFileAspect > dWindowAspect) {
620
            height = (int) ((double) width / dFileAspect);
621
        } else {
622
            width = (int) ((double) height * dFileAspect);
623
        }
624

    
625
        // Set the view
626
        file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), width, height);
627

    
628
        //Impedimos que los valores de ancho y alto de la im?gen sean menores que 1
629
        if (width <= 0) {
630
            width = 1;
631
        }
632

    
633
        if (height <= 0) {
634
            height = 1;
635
        }
636

    
637
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
638
        pRGBArray = new int[width * height];
639

    
640
        try {
641
            file.setAlpha(getAlpha());
642
            
643
            setBand(RED_BAND, rBandNr);
644
            setBand(GREEN_BAND, gBandNr);
645
            setBand(BLUE_BAND, bBandNr);
646
                     
647

    
648
            file.readScene(pRGBArray);
649
            ((BufferedImage) image).setRGB(0, 0, width, height, pRGBArray, 0,
650
                                           width);
651
        } catch (Exception e) {
652
            e.printStackTrace();
653
        }
654

    
655
        return image;
656
    }
657

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

    
670
        file.pintaInfo();
671
        file.pintaPaleta();
672
    }
673

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

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

    
713
        if (flags == GeoRasterFile.RED_BAND) {
714
            for (int i = 0; i < line.length; i++)
715
                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
716
        } else if (flags == GeoRasterFile.GREEN_BAND) {
717
            for (int i = 0; i < line.length; i++)
718
                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
719
        } else if (flags == GeoRasterFile.BLUE_BAND) {
720
            for (int i = 0; i < line.length; i++)
721
                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
722
        }
723

    
724
        image.setRGB(startX, startY, w, h, line, offset, scansize);
725
    }
726

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

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

    
790
        image.setRGB(startX, startY, w, h, line, offset, scansize);
791
    }
792

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

    
804
        // Work out the correct aspect for the setView call.
805
        dFileAspect = (double) v.width() / (double) v.height();
806
        dWindowAspect = (double) width / (double) height;
807

    
808
        if (dFileAspect > dWindowAspect) {
809
            height = (int) ((double) width / dFileAspect);
810
        } else {
811
            width = (int) ((double) height * dFileAspect);
812
        }
813

    
814
        // Set the view
815
        file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), width, height);
816

    
817
        //Impedimos que los valores de ancho y alto de la im?gen sean menores que 1
818
        if (width <= 0) {
819
            width = 1;
820
        }
821

    
822
        if (height <= 0) {
823
            height = 1;
824
        }
825

    
826
        file.setAlpha(getAlpha());
827
      
828
        setBand(RED_BAND, rBandNr);
829
        setBand(GREEN_BAND, gBandNr);
830
        setBand(BLUE_BAND, bBandNr);
831
       
832
        pRGBArray = new int[width * height];
833

    
834
        if (img == null) { //Caso en el que se crea una imagen
835
            mrSidImage = new BufferedImage(width, height,
836
                                           BufferedImage.TYPE_INT_ARGB);
837

    
838
            try {
839
                file.readScene(pRGBArray);
840
                setRGBLine((BufferedImage) mrSidImage, 0, 0, width, height,
841
                           pRGBArray, 0, width);
842
            } catch (Exception e) {
843
                e.printStackTrace();
844
            }
845

    
846
            return mrSidImage;
847
        } else { //Caso en el que se actualiza una banda del Image
848

    
849
            try {
850
                file.readScene(pRGBArray);
851
                setRGBLine((BufferedImage) img, 0, 0, width, height, pRGBArray,
852
                           0, width, origBand, destBandFlag);
853
            } catch (Exception e) {
854
                e.printStackTrace();
855
            }
856

    
857
            return img;
858
        }
859
    }
860

    
861
    /* (non-Javadoc)
862
     * @see org.cresques.io.GeoRasterFile#getData(int, int, int)
863
     */
864
    public Object getData(int x, int y, int band) {
865
        // TODO Auto-generated method stub
866
        return null;
867
    }
868

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

    
884
        return null;
885
    }
886

    
887
    /**
888
     * Devuelve el tama?o de bloque
889
     * @return Tama?o de bloque
890
     */
891
    public int getBlockSize() {
892
        return file.blocksize;
893
    }
894
}