Statistics
| Revision:

root / trunk / libraries / libCresques / src / org / cresques / io / MrSidFile.java @ 13130

History | View | Annotate | Download (29.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.px.Extent;
35

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

    
48

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

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

    
76
                //View
77
                private double                                         zoomoverview = 0.0;
78
                private int                                         eColorSpace;
79
                private int                                         eSampleType;
80
                private int                                         noverviews;
81
                public int                                                 xini;
82
                public int                                                 yini;
83
                /**
84
                 * Ancho y alto de la overview
85
                 */
86
                public int                                                 anchoOver;
87
                public int                                                 altoOver;
88
                public int                                                 blocksize = 1;
89
                /**
90
                 * N?mero de bandas de la imagen
91
                 */
92
                public int                                                 nbands;
93
                /**
94
                 * Posici?n de la esquina superior izquierda en coordenadas pixel
95
                 */
96
                private double                                         currentViewY = -1;
97
                private double                                         currentViewX = 0D;
98
                /**
99
                 * Ancho y alto de la im?gen (pixeles en pantalla)
100
                 */
101
                private int                                         currentViewWidth = -1;
102
                private int                                         currentViewHeight = -1;
103
                /**
104
                 * Ancho y alto de la imagen completa en pixeles
105
                 */
106
                private int                                         currentFullWidth = -1;
107
                private int                                         currentFullHeight = -1;
108
                /**
109
                 * Escala del viewport en X e Y
110
                 */
111
                private double                                         viewportScaleX = 0D;
112
                private double                                         viewportScaleY = 0D;
113
                /**
114
                 * ?ltimo extent de la ventana seleccionada por el usuario. Este extent corresponde al de la
115
                 * imagen, no al del viewport de la vista.
116
                 */
117
                private double[]                                currentImageView = new double[4];
118

    
119
                /**
120
                 * Constructor
121
                 * @param fName
122
                 * @throws MrSIDException
123
                 * @throws IOException
124
                 */
125
                public MrSidNative(String fName) throws MrSIDException, IOException {
126
                                super(fName);
127
                                init(fName);
128
                }
129

    
130
                /**
131
                 * Inicializa las variables de instancia con los valores de la imagen
132
                 * @param fName
133
                 * @throws MrSIDException
134
                 * @throws IOException
135
                 */
136
                private void init(String fName) throws MrSIDException, IOException {
137
                                this.initialize();
138

    
139
                                width = this.getWidth();
140
                                height = this.getHeight();
141
                                eSampleType = this.getDataType();
142
                                nbands = this.getNumBands();
143
                                eColorSpace = this.getColorSpace();
144
                                noverviews = this.getNumLevels();
145

    
146
                                metadata = this.getMetadata();
147

    
148
                                double ox = 0D;
149
                                double oy = 0D;
150
                                double resx = 0D;
151
                                double resy = 0D;
152

    
153
                                LTIGeoCoord geoc = this.getGeoCoord();
154

    
155
                                ox = geoc.getX();
156
                                oy = geoc.getY();
157
                                resx = geoc.getXRes();
158
                                resy = geoc.getYRes();
159

    
160
                                System.out.println("Origin = (" + ox + "," + oy + ")");
161
                                System.out.println("Pixel Size = (" + resx + "," + resy + ")");
162
                                esq.add(new Point2D.Double(ox, oy));
163
                                esq.add(new Point2D.Double(ox + (resx * width), oy));
164
                                esq.add(new Point2D.Double(ox, oy + (resy * height)));
165
                                esq.add(new Point2D.Double(ox + (resx * width), oy + (resy * height)));
166

    
167
                                blocksize = this.getStripHeight();
168
                                System.out.println("StripHeight = (" + blocksize + ")");
169
                }
170

    
171
                /**
172
                 * Asigna el valor de Alpha
173
                 * @param a        alpha
174
                 */
175
                public void setAlpha(int a) {
176
                                alpha = a;
177
                }
178

    
179
                /**
180
                 * Asigna el tipo de datos
181
                 * @param dt        tipo de datos
182
                 */
183
                public void setDataType(int dt) {
184
                                dataType = dt;
185
                }
186

    
187
                /**
188
                 * Obtiene un punto 2D con las coordenadas del raster a partir de uno en coordenadas
189
                 * del punto real.
190
                 * @param pt        punto en coordenadas del punto real
191
                 * @return        punto en coordenadas del raster
192
                 */
193
                public Point2D worldToRaster(Point2D pt) {
194
                                double x = (((double) currentFullWidth) / (esq.maxX - esq.minX)) * (pt.getX() - esq.minX);
195
                                double y = (((double) currentFullHeight) / (esq.maxY - esq.minY)) * (esq.maxY - pt.getY());
196
                                Point2D ptRes = new Point2D.Double(x, y);
197
                                return ptRes;
198
                }
199

    
200
                /**
201
                 * Calcula el overview a usar de la imagen y el viewport a partir del ancho, alto y
202
                 * coordenadas del mundo real
203
                 * @param dWorldTLX        Coordenada X superior izquierda
204
                 * @param dWorldTLY        Coordenada Y superior izquierda
205
                 * @param dWorldBRX        Coordenada X inferior derecha
206
                 * @param dWorldBRY        Coordenada Y inferior derecha
207
                 * @param nWidth        ancho
208
                 * @param nHeight        alto
209
                 */
210
                public void setView(double dWorldTLX, double dWorldTLY, double dWorldBRX,
211
                                                                                                double dWorldBRY, int nWidth, int nHeight) {
212
                        currentImageView[0] = dWorldTLX;
213
                        currentImageView[1] = dWorldTLY;
214
                        currentImageView[2] = dWorldBRX;
215
                        currentImageView[3] = dWorldBRY;
216

    
217
                                //Ancho y alto de la im?gen en pixeles (pixeles de la overview)
218
                                currentFullWidth = width;
219
                                currentFullHeight = height;
220

    
221
                                //Ventana de la imagen. (en tama?o completo)
222
                                //tl->esq sup izda en pixeles
223
                                //br->esq inf der en pixeles
224
                                Point2D tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
225
                                Point2D br = worldToRaster(new Point2D.Double(dWorldBRX, dWorldBRY));
226

    
227
                                //Ancho y alto de la im?gen (pixeles en pantalla)
228
                                currentViewWidth = nWidth;
229
                                currentViewHeight = nHeight;
230

    
231
                                //Posici?n de la esquina superior izquierda en coordenadas pixel
232
                                currentViewX = tl.getX();
233
                                currentViewY = tl.getY();
234

    
235
                                //Escala de la vista en X e Y
236
                                viewportScaleX = (double) currentViewWidth / (br.getX() - tl.getX());
237
                                viewportScaleY = (double) currentViewHeight / (br.getY() - tl.getY());
238

    
239
                                try {
240
                                                // calcula el overview a usar
241
                                                int[] dims = null;
242
                                                double zoom = 1.0;
243
                                                zoomoverview = 1.0;
244

    
245
                                                if (WITH_OVERVIEWS && ((noverviews - 1) > 0)) {
246
                                                                for (int i = (noverviews - 1); i > 0; i--) {
247
                                                                                zoom = LTIUtils.levelToMag(i);
248
                                                                                dims = this.getDimsAtMag(zoom);
249

    
250
                                                                                if (dims[0] > (this.getWidth() * viewportScaleX)) {
251
                                                                                                zoomoverview = zoom;
252
                                                                                                viewportScaleX /= zoomoverview;
253
                                                                                                viewportScaleY /= zoomoverview;
254
                                                                                                currentFullWidth = dims[0];
255
                                                                                                currentFullHeight = dims[1];
256
                                                                                                tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
257
                                                                                                currentViewX = tl.getX();
258
                                                                                                currentViewY = tl.getY();
259

    
260
                                                                                                break;
261
                                                                                }
262
                                                                }
263
                                                }
264

    
265
                                                setDataType(eSampleType);
266
                                } catch (MrSIDException e) {
267
                                                e.printStackTrace();
268
                                }
269
                }
270

    
271
                /**
272
                 * Muestra alguna informaci?n para la depuraci?n
273
                 */
274
                void pintaInfo() {
275
                                try {
276
                                                System.out.println("GeoTransform:");
277

    
278
                                                LTIGeoCoord geoc = this.getGeoCoord();
279

    
280
                                                System.out.println("  param[0]=" + geoc.getX());
281
                                                System.out.println("  param[0]=" + geoc.getY());
282
                                                System.out.println("  param[0]=" + geoc.getXRes());
283
                                                System.out.println("  param[0]=" + geoc.getYRes());
284
                                                System.out.println("  param[0]=" + geoc.getXRot());
285
                                                System.out.println("  param[0]=" + geoc.getYRot());
286
                                                System.out.println("Metadata:");
287

    
288
                                                LTIMetadataDatabase metadata = this.getMetadata();
289

    
290
                                                for (int i = 0; i < metadata.getIndexCount(); i++) {
291
                                                                LTIMetadataRecord rec = null;
292
                                                                rec = metadata.getDataByIndex(i);
293
                                                                System.out.println(rec.getTagName());
294

    
295
                                                                if (rec.isScalar()) {
296
                                                                                System.out.println(rec.getScalarData());
297
                                                                } else if (rec.isVector()) {
298
                                                                                String[] s = rec.getVectorData();
299

    
300
                                                                                for (int j = 0; j < s.length; j++)
301
                                                                                                System.out.println("V" + j + "->" + s[j]);
302
                                                                } else if (rec.isArray()) {
303
                                                                                String[] s = rec.getArrayData();
304

    
305
                                                                                for (int j = 0; j < s.length; j++)
306
                                                                                                System.out.println("A" + j + "->" + s[j]);
307
                                                                } else {
308
                                                                                System.out.println("");
309
                                                                }
310
                                                }
311
                                } catch (MrSIDException e) {
312
                                                // TODO Auto-generated catch block
313
                                                e.printStackTrace();
314
                                }
315
                }
316

    
317
                void pintaPaleta() {
318
                }
319

    
320
                public LTISceneBuffer readBuffer(int x, int y, int SceneWidth, int SceneHeight)throws MrSIDException{
321
                        LTISceneBuffer buffer = null;
322
                        LTIScene scene = new LTIScene(x, y, SceneWidth, SceneHeight, zoomoverview);
323
                        buffer = new LTISceneBuffer(pixel, SceneWidth, SceneHeight, true);
324
                        ((LTIImageStage) this).read(scene, buffer);
325
                        return buffer;
326
                }
327

    
328
                /**
329
                 * Lee la escena de la imagen correspondiente a la vista seleccionada con
330
                 * currentView a trav?s de la libreria de MrSid. Esta escena es cargada sobre
331
                 * un buffer y asignada al par?metro de salida.
332
                 * @param line        Escena leida
333
                 * @throws MrSIDException Lanzada si ocurre un error en la lectura de la escena
334
                 */
335
                public void readScene(int[] line) throws MrSIDException {
336
                        //Posici?n de inicio de la escena en entero para la petici?n a la libreria
337
                                int x = (int) Math.floor(currentViewX);
338
                                int y = (int) Math.floor(currentViewY);
339
                                //Ancho y alto de la escena en pixeles
340
                                int SceneWidth = 0;
341
                                int SceneHeight = 0;
342

    
343
                                try {
344
                        SceneWidth = (int) Math.ceil((((double) currentViewWidth) / viewportScaleX) + 1);
345
                        if (SceneWidth > currentFullWidth)
346
                                SceneWidth = currentFullWidth;
347
                        SceneHeight = (int) Math.ceil((((double) currentViewHeight) / viewportScaleY) + 1);
348
                                if (SceneHeight > currentFullHeight)
349
                                                 SceneHeight = currentFullHeight;
350

    
351
                                                if (SceneWidth == 0)
352
                                                                SceneWidth = 1;
353

    
354
                                                if (SceneHeight == 0)
355
                                                                SceneHeight = 1;
356

    
357
                                                if (pixel == null)
358
                                                                pixel = new LTIPixel(eColorSpace, nbands, eSampleType);
359

    
360
                                                LTISceneBuffer buffer = null;
361

    
362
                                                boolean  sizeOk = false;
363
                        while (!sizeOk){
364
                                sizeOk = true;
365
                                                        try {
366
                                                                buffer = readBuffer(x, y, SceneWidth, SceneHeight);
367
                                                        } catch (MrSIDException ex) {
368
                                                                SceneWidth-- ;
369
                                                                try{
370
                                                                        buffer = readBuffer(x, y, SceneWidth, SceneHeight);
371
                                                                } catch (MrSIDException ex1) {
372
                                                                        SceneWidth++;
373
                                                                        SceneHeight--;
374
                                                                                try{
375
                                                                                        buffer = readBuffer(x, y, SceneWidth, SceneHeight);
376
                                                                                } catch (MrSIDException ex2) {
377
                                                                                        SceneWidth-- ;
378
                                                                                                try{
379
                                                                                                        buffer = readBuffer(x, y, SceneWidth, SceneHeight);
380
                                                                                                } catch (MrSIDException ex3) {
381
                                                                                                        sizeOk = false;
382
                                                                                                }
383
                                                                                }
384
                                                                }
385
                                                        }
386
                        }
387

    
388

    
389
                                                if ((dataType == LTIDataType.LTI_DATATYPE_UINT8) ||
390
                                                                                (dataType == LTIDataType.LTI_DATATYPE_SINT8) ||
391
                                                                                (dataType == LTIDataType.LTI_DATATYPE_SINT16) ||
392
                                                                                (dataType == LTIDataType.LTI_DATATYPE_SINT32) ||
393
                                                                                (dataType == LTIDataType.LTI_DATATYPE_UINT16) ||
394
                                                                                (dataType == LTIDataType.LTI_DATATYPE_UINT32)) {
395
                                                                int kd;
396
                                                                int k;
397
                                                                double scaleX = 1 / viewportScaleX;
398
                                                                double scaleY = 1 / viewportScaleY;
399
                                                                int alpha = (this.alpha & 0xff) << 24;
400

    
401
                                                                if (rBandNr == 1) {
402
                                                                                bandR = buffer.buf1;
403
                                                                } else if (rBandNr == 2) {
404
                                                                                bandR = buffer.buf2;
405
                                                                } else if (rBandNr == 3) {
406
                                                                                bandR = buffer.buf3;
407
                                                                }
408

    
409
                                                                if (gBandNr == 1) {
410
                                                                                bandG = buffer.buf1;
411
                                                                } else if (gBandNr == 2) {
412
                                                                                bandG = buffer.buf2;
413
                                                                } else if (gBandNr == 3) {
414
                                                                                bandG = buffer.buf3;
415
                                                                }
416

    
417
                                                                if (bBandNr == 1) {
418
                                                                                bandB = buffer.buf1;
419
                                                                } else if (bBandNr == 2) {
420
                                                                                bandB = buffer.buf2;
421
                                                                } else if (bBandNr == 3) {
422
                                                                                bandB = buffer.buf3;
423
                                                                }
424

    
425
                                                                //Desplazamiento para la X y la Y leidas. Estas tienen efecto cuando un pixel no empieza a visualizarse
426
                                                                //justo en su esquina superior izquierda y tiene que ser cortado en la visualizaci?n.
427
                                                                double offsetX = Math.abs(currentViewX - ((int)currentViewX));
428
                                                                double offsetY = Math.abs(currentViewY - ((int)currentViewY));
429
                                                                for (int y1 = 0; y1 < currentViewHeight; y1++){
430
                                                                         for (int x1 = 0; x1 < currentViewWidth; x1++) {
431
                                                                                        kd = (y1 * currentViewWidth) + x1;
432
                                                                                        k = (((int) ((y1 * scaleY) + offsetY)) * SceneWidth) +
433
                                                                                                        (int) ((x1 * scaleX) + offsetX);
434

    
435
                                                                                        try {
436
                                                                                                                line[kd] = alpha + ((0xff & bandR[k]) << 16) + ((0xff & bandG[k]) << 8) + (0xff & bandB[k]);
437
                                                                                        } catch (java.lang.ArrayIndexOutOfBoundsException e) {
438
                                                                                        }
439
                                                                         }
440
                                                                }
441
                                                }
442

    
443
                                                buffer = null;
444
                                } catch (MrSIDException e) {
445
                                                e.printStackTrace();
446
                                }
447
                }
448

    
449
                /**
450
                 * Lee una ventana de la imagen y devuelve un buffer de bytes
451
                 * @param ulX        Coordenada X de la esquina superior izquierda
452
                 * @param ulY        Coordenada Y de la esquina superior izquierda
453
                 * @param sizeX        Tama?o X de la imagen
454
                 * @param sizeY        Tama?o Y de la image
455
                 * @param band        N?mero de bandas
456
                 * @return        buffer con la ventana leida
457
                 * @throws MrSIDException
458
                 */
459
                public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band)
460
                                                                                 throws MrSIDException {
461
                                if (pixel == null) {
462
                                                pixel = new LTIPixel(eColorSpace, nbands, eSampleType);
463
                                }
464

    
465
                                LTIScene scene = new LTIScene(ulX, ulY, sizeX, sizeY, 1.0);
466
                                LTISceneBuffer buffer = new LTISceneBuffer(pixel, sizeX, sizeY, true);
467
                                ((LTIImageStage) this).read(scene, buffer);
468

    
469
                                if (band == 1) {
470
                                                return buffer.buf1;
471
                                } else if (band == 2) {
472
                                                return buffer.buf2;
473
                                } else if (band == 3) {
474
                                                return buffer.buf3;
475
                                }
476

    
477
                                return null;
478
                }
479

    
480
                // Polilinea con extent
481
                class Contour extends Vector {
482
                                final private static long serialVersionUID = -3370601314380922368L;
483
                                public double minX = Double.MAX_VALUE;
484
                                public double minY = Double.MAX_VALUE;
485
                                public double maxX = -Double.MAX_VALUE;
486
                                public double maxY = -Double.MAX_VALUE;
487

    
488
                                public Contour() {
489
                                                super();
490
                                }
491

    
492
                                public void add(Point2D pt) {
493
                                                super.add(pt);
494

    
495
                                                if (pt.getX() > maxX) {
496
                                                                maxX = pt.getX();
497
                                                }
498

    
499
                                                if (pt.getX() < minX) {
500
                                                                minX = pt.getX();
501
                                                }
502

    
503
                                                if (pt.getY() > maxY) {
504
                                                                maxY = pt.getY();
505
                                                }
506

    
507
                                                if (pt.getY() < minY) {
508
                                                                minY = pt.getY();
509
                                                }
510
                                }
511
                }
512
}
513

    
514

    
515
/**
516
 * @author Nacho Brodin <brodin_ign@gva.es>
517
 *
518
 * Clase encargada del acceso a los datos y repintado de imagenes MrSID. Estos
519
 * son registrados con la extensi?n sid
520
 */
521
public class MrSidFile extends GeoRasterFile {
522
                public final static int BAND_HEIGHT = 64;
523

    
524
                static {
525
                                GeoRasterFile.registerExtension("sid", MrSidFile.class);
526
                }
527

    
528
                protected MrSidNative file = null;
529
                private Extent v = null;
530

    
531
                /**
532
                 * Contructor. Abre el fichero mrsid
533
                 * @param proj        Proyecci?n
534
                 * @param fName        Nombre del fichero mrsid
535
                 */
536
                public MrSidFile(IProjection proj, String fName) {
537
                                super(proj, fName);
538

    
539
                                extent = new Extent();
540

    
541
                                try {
542
                                                file = new MrSidNative(fName);
543
                                                showOnOpen();
544
                                                load();
545
                                                bandCount = file.nbands;
546

    
547
                                                if (bandCount > 2) {
548
                                                                setBand(RED_BAND, 0);
549
                                                                setBand(GREEN_BAND, 1);
550
                                                                setBand(BLUE_BAND, 2);
551
                                                } else {
552
                                                                setBand(RED_BAND | GREEN_BAND | BLUE_BAND, 0);
553
                                                }
554
                                } catch (Exception e) {
555
                                                System.out.println("Error en constructor de MrSID");
556
                                                e.printStackTrace();
557
                                                file = null;
558
                                }
559
                }
560

    
561
        /**
562
         * Obtenemos o calculamos el extent de la imagen.
563
         */
564
                public GeoFile load() {
565

    
566
                         extent = new Extent(file.esq.minX, file.esq.minY, file.esq.maxX, file.esq.maxY);
567
                         requestExtent = extent;
568

    
569
                        /*if((this.assignedExtent == GeoRasterFile.IMAGE_EXTENT || this.assignedExtent == GeoRasterFile.ORDER )
570
                                && file !=null        && file.esq != null){
571
                                extent = new Extent(file.esq.minX, file.esq.minY, file.esq.maxX, file.esq.maxY);
572
                                return this;
573
                        }
574

575
                        if((this.assignedExtent == GeoRasterFile.ASSIGNED_EXTENT || this.assignedExtent == GeoRasterFile.ORDER )
576
                                && this.getTempExtent() != null){
577

578
                                        extent = this.getTempExtent();
579
                        file.esq = file.new Contour();
580
                                file.esq.add(new Point2D.Double(extent.minX(), extent.minY()));
581
                                file.esq.add(new Point2D.Double(extent.minX()+(extent.maxX() - extent.minX()), extent.minY()));
582
                                file.esq.add(new Point2D.Double(extent.minX(), extent.minY()+(extent.maxY() - extent.minY())));
583
                                file.esq.add(new Point2D.Double(extent.minX()+(extent.maxX() - extent.minX()), extent.minY()+(extent.maxY() - extent.minY())));
584
                                }*/
585

    
586
                                return this;
587
                }
588

    
589
                /**
590
                 * Libera el objeto que ha abierto el fichero
591
                 */
592
                public void close() {
593
                        if(file != null){
594
                                file.close();
595
                                file = null;
596
                        }
597
                }
598

    
599
                /**
600
                 * Asigna una banda R, G o B
601
                 */
602
                public void setBand(int flag, int bandNr) {
603
                                super.setBand(flag, bandNr);
604

    
605
                                if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) {
606
                                                file.rBandNr = bandNr + 1;
607
                                }
608

    
609
                                if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) {
610
                                                file.gBandNr = bandNr + 1;
611
                                }
612

    
613
                                if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) {
614
                                                file.bBandNr = bandNr + 1;
615
                                }
616
                }
617

    
618
                /**
619
                 * Asigna el extent de la vista
620
                 */
621
                public void setView(Extent e) {
622
                                v = new Extent(e);
623
                }
624

    
625
                /**
626
                 * Obtiene el Extent de la vista
627
                 */
628
                public Extent getView() {
629
                                return v;
630
                }
631

    
632
                /**
633
                 * Obtiene el ancho de la imagen
634
                 */
635
                public int getWidth() {
636
                                return file.width;
637
                }
638

    
639
                /**
640
                 * Obtiene el alto de la imagen
641
                 */
642
                public int getHeight() {
643
                                return file.height;
644
                }
645

    
646
                public void reProject(ICoordTrans rp) {
647
                                // TODO Auto-generated method stub
648
                }
649

    
650
                /**
651
                 * Actualiza la imagen. Se encarga de llamar a la funci?n que calcula la vista
652
                 * y luego a la que lee la escena sobre un buffer. Vuelca la informaci?n obtenida
653
                 * sobre el Image que la visualiza.
654
                 */
655
                public Image updateImage(int width, int height, ICoordTrans rp) {
656
                                int[] pRGBArray = null;
657
                                Image image = null;
658

    
659
                                if(mustVerifySize()){
660
                                        // Work out the correct aspect for the setView call.
661
                                        double dFileAspect = (double) v.width() / (double) v.height();
662
                                        double dWindowAspect = (double) width / (double) height;
663

    
664
                                        if (dFileAspect > dWindowAspect) {
665
                                                        height = (int) ((double) width / dFileAspect);
666
                                        } else {
667
                                                        width = (int) ((double) height * dFileAspect);
668
                                        }
669
                                }
670

    
671
                                // Set the view
672
                                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), width, height);
673

    
674
                                //Impedimos que los valores de ancho y alto de la im?gen sean menores que 1
675
                                if (width <= 0) {
676
                                                width = 1;
677
                                }
678

    
679
                                if (height <= 0) {
680
                                                height = 1;
681
                                }
682

    
683
                                image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
684
                                pRGBArray = new int[width * height];
685

    
686
                                try {
687
                                                file.setAlpha(getAlpha());
688

    
689
                                                setBand(RED_BAND, rBandNr);
690
                                                setBand(GREEN_BAND, gBandNr);
691
                                                setBand(BLUE_BAND, bBandNr);
692

    
693

    
694
                                                file.readScene(pRGBArray);
695
                                                ((BufferedImage) image).setRGB(0, 0, width, height, pRGBArray, 0,
696
                                                                                                                                                                         width);
697
                                } catch (Exception e) {
698
                                                e.printStackTrace();
699
                                }
700

    
701
                                return image;
702
                }
703

    
704
                /**
705
                 * Muestra informaci?n del fichero abierto.
706
                 *
707
                 */
708
                private void showOnOpen() {
709
                                // Report en la apertura (quitar)
710
                                System.out.println("Fichero MrSID '" + getName() + "' abierto.");
711
                                System.out.println("Version = " + file.version);
712
                                System.out.println("   Size = (" + file.width + "," + file.height +
713
                                                                                                         ")");
714
                                System.out.println("   NumBands = (" + file.nbands + ")");
715

    
716
                                //file.pintaInfo();
717
                                file.pintaPaleta();
718
                }
719

    
720
                /**
721
                 * Asigna al objeto Image los valores con los dato de la imagen contenidos en el
722
                 * vector de enteros.
723
                 * @param image        imagen con los datos actuales
724
                 * @param startX        inicio de la posici?n en X dentro de la imagen
725
                 * @param startY        inicio de la posici?n en X dentro de la imagen
726
                 * @param w        Ancho de la imagen
727
                 * @param h        Alto de la imagen
728
                 * @param rgbArray        vector que contiene la banda que se va a sustituir
729
                 * @param offset        desplazamiento
730
                 * @param scansize        tama?o de imagen recorrida por cada p
731
                 */
732
                protected void setRGBLine(BufferedImage image, int startX, int startY,
733
                                                                                                                        int w, int h, int[] rgbArray, int offset,
734
                                                                                                                        int scansize) {
735
                                image.setRGB(startX, startY, w, h, rgbArray, offset, scansize);
736
                }
737

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

    
759
                                if (flags == GeoRasterFile.RED_BAND) {
760
                                                for (int i = 0; i < line.length; i++)
761
                                                                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
762
                                } else if (flags == GeoRasterFile.GREEN_BAND) {
763
                                                for (int i = 0; i < line.length; i++)
764
                                                                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
765
                                } else if (flags == GeoRasterFile.BLUE_BAND) {
766
                                                for (int i = 0; i < line.length; i++)
767
                                                                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
768
                                }
769

    
770
                                image.setRGB(startX, startY, w, h, line, offset, scansize);
771
                }
772

    
773
                /**
774
                 * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores
775
                 * con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
776
                 * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
777
                 * banda correspondiente a flags es sustituida por los datos del vector.
778
                 * @param image        imagen con los datos actuales
779
                 * @param startX        inicio de la posici?n en X dentro de la imagen
780
                 * @param startY        inicio de la posici?n en X dentro de la imagen
781
                 * @param w        Ancho de la imagen
782
                 * @param h        Alto de la imagen
783
                 * @param rgbArray        vector que contiene la banda que se va a sustituir
784
                 * @param offset        desplazamiento
785
                 * @param scansize        tama?o de imagen recorrida por cada paso
786
                 * @param origBand        Banda origen del GeoRasterFile
787
                 * @param destBandFlag        banda que se va a sustituir (Ctes de GeoRasterFile)
788
                 */
789
                protected void setRGBLine(BufferedImage image, int startX, int startY,
790
                                                                                                                        int w, int h, int[] rgbArray, int offset,
791
                                                                                                                        int scansize, int origBand, int destBandFlag) {
792
                                int[] line = new int[rgbArray.length];
793
                                image.getRGB(startX, startY, w, h, line, offset, scansize);
794

    
795
                                if ((origBand == 0) && (destBandFlag == GeoRasterFile.RED_BAND)) {
796
                                                for (int i = 0; i < line.length; i++)
797
                                                                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
798
                                } else if ((origBand == 1) &&
799
                                                                                         (destBandFlag == GeoRasterFile.GREEN_BAND)) {
800
                                                for (int i = 0; i < line.length; i++)
801
                                                                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
802
                                } else if ((origBand == 2) &&
803
                                                                                         (destBandFlag == GeoRasterFile.BLUE_BAND)) {
804
                                                for (int i = 0; i < line.length; i++)
805
                                                                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
806
                                } else if ((origBand == 0) &&
807
                                                                                         (destBandFlag == GeoRasterFile.GREEN_BAND)) {
808
                                                for (int i = 0; i < line.length; i++)
809
                                                                line[i] = (line[i] & 0xffff00ff) |
810
                                                                                                        ((rgbArray[i] & 0x00ff0000) >> 8);
811
                                } else if ((origBand == 0) &&
812
                                                                                         (destBandFlag == GeoRasterFile.BLUE_BAND)) {
813
                                                for (int i = 0; i < line.length; i++)
814
                                                                line[i] = (line[i] & 0xffffff00) |
815
                                                                                                        ((rgbArray[i] & 0x00ff0000) >> 16);
816
                                } else if ((origBand == 1) && (destBandFlag == GeoRasterFile.RED_BAND)) {
817
                                                for (int i = 0; i < line.length; i++)
818
                                                                line[i] = (line[i] & 0xff00ffff) |
819
                                                                                                        ((rgbArray[i] & 0x0000ff00) << 8);
820
                                } else if ((origBand == 1) &&
821
                                                                                         (destBandFlag == GeoRasterFile.BLUE_BAND)) {
822
                                                for (int i = 0; i < line.length; i++)
823
                                                                line[i] = (line[i] & 0xffffff00) |
824
                                                                                                        ((rgbArray[i] & 0x0000ff00) >> 8);
825
                                } else if ((origBand == 2) && (destBandFlag == GeoRasterFile.RED_BAND)) {
826
                                                for (int i = 0; i < line.length; i++)
827
                                                                line[i] = (line[i] & 0xff00ffff) |
828
                                                                                                        ((rgbArray[i] & 0x000000ff) << 16);
829
                                } else if ((origBand == 2) &&
830
                                                                                         (destBandFlag == GeoRasterFile.GREEN_BAND)) {
831
                                                for (int i = 0; i < line.length; i++)
832
                                                                line[i] = (line[i] & 0xffff00ff) |
833
                                                                                                        ((rgbArray[i] & 0x000000ff) << 8);
834
                                }
835

    
836
                                image.setRGB(startX, startY, w, h, line, offset, scansize);
837
                }
838

    
839
                /* (non-Javadoc)
840
                 * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans, java.awt.Image, int)
841
                 */
842
                public Image updateImage(int width, int height, ICoordTrans rp, Image img,
843
                                                                                                                 int origBand, int destBandFlag) throws SupersamplingNotSupportedException{
844
                                int[] pRGBArray = null;
845
                                Image mrSidImage = null;
846

    
847
                                if(mustVerifySize()){
848
                                        // Work out the correct aspect for the setView call.
849
                                        double dFileAspect = (double) v.width() / (double) v.height();
850
                                        double dWindowAspect = (double) width / (double) height;
851

    
852
                                        if (dFileAspect > dWindowAspect) {
853
                                                        height = (int) ((double) width / dFileAspect);
854
                                        } else {
855
                                                        width = (int) ((double) height * dFileAspect);
856
                                        }
857
                                }
858
                                // Set the view
859
                                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), width, height);
860

    
861
                                //Impedimos que los valores de ancho y alto de la im?gen sean menores que 1
862
                                if (width <= 0) {
863
                                                width = 1;
864
                                }
865

    
866
                                if (height <= 0) {
867
                                                height = 1;
868
                                }
869

    
870
                                file.setAlpha(getAlpha());
871

    
872
                                setBand(RED_BAND, rBandNr);
873
                                setBand(GREEN_BAND, gBandNr);
874
                                setBand(BLUE_BAND, bBandNr);
875

    
876
                                pRGBArray = new int[width * height];
877

    
878
                                if (img == null) { //Caso en el que se crea una imagen
879
                                                mrSidImage = new BufferedImage(width, height,
880
                                                                                                                                                                         BufferedImage.TYPE_INT_ARGB);
881

    
882
                                                try {
883
                                                                file.readScene(pRGBArray);
884
                                                                setRGBLine((BufferedImage) mrSidImage, 0, 0, width, height, pRGBArray, 0, width);
885
                                                } catch (Exception e) {
886
                                                                e.printStackTrace();
887
                                                }
888

    
889
                                                return mrSidImage;
890
                                } else { //Caso en el que se actualiza una banda del Image
891

    
892
                                                try {
893
                                                                file.readScene(pRGBArray);
894
                                                                setRGBLine((BufferedImage) img, 0, 0, width, height, pRGBArray, 0, width, origBand, destBandFlag);
895
                                                } catch (Exception e) {
896
                                                                e.printStackTrace();
897
                                                }
898

    
899
                                                return img;
900
                                }
901
                }
902

    
903
                /* (non-Javadoc)
904
                 * @see org.cresques.io.GeoRasterFile#getData(int, int, int)
905
                 */
906
                public Object getData(int x, int y, int band) {
907
                                // TODO Auto-generated method stub
908
                                return null;
909
                }
910

    
911
                /**
912
                 * Devuelve los datos de una ventana solicitada
913
                 * @param ulX        coordenada X superior izda.
914
                 * @param ulY        coordenada Y superior derecha.
915
                 * @param sizeX        tama?o en X de la ventana.
916
                 * @param sizeY tama?o en Y de la ventana.
917
                 * @param band        Banda solicitada.
918
                 */
919
                public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band) {
920
                                try {
921
                                                return file.getWindow(ulX, ulY, sizeX, sizeY, band);
922
                                } catch (MrSIDException e) {
923
                                                e.printStackTrace();
924
                                }
925

    
926
                                return null;
927
                }
928

    
929
                /**
930
                 * Devuelve el tama?o de bloque
931
                 * @return Tama?o de bloque
932
                 */
933
                public int getBlockSize() {
934
                                return file.blocksize;
935
                }
936

    
937
                /**
938
         * Calcula la transformaci?n que se produce sobre la vista cuando la imagen tiene un fichero .rmf
939
         * asociado. En Ecw el origen de coordenadas en Y es el valor m?ximo y decrece hasta el m?nimo.
940
         * @param originX Origen de la imagen en la coordenada X
941
         * @param originY Origen de la imagen en la coordenada Y
942
         */
943
                public void setExtentTransform(double originX, double originY, double psX, double psY) {
944

    
945
        }
946
}