Statistics
| Revision:

root / branches / v02_desarrollo / libraries / libCq CMS for java.old / src / org / cresques / io / MrSidFile.java @ 1131

History | View | Annotate | Download (14.1 KB)

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

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

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

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

    
21

    
22

    
23
/**
24
 * 
25
 * @author nacho
26
 *
27
 * TODO To change the template for this generated type comment go to
28
 * Window - Preferences - Java - Code Style - Code Templates
29
 */
30

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

    
98
                width = this.getWidth();
99
                height = this.getHeight();
100
                eSampleType = this.getDataType();
101
                nbands = this.getNumBands();
102
                eColorSpace = this.getColorSpace();
103
                noverviews = this.getNumLevels();
104
                
105
                metadata = this.getMetadata();
106
                double ox=0D, oy=0D, resx=0D, resy=0D;
107
                        
108
                LTIGeoCoord geoc = this.getGeoCoord();
109
                
110
                ox = geoc.getX();
111
                oy = geoc.getY();
112
                resx = geoc.getXRes();
113
                resy = geoc.getYRes();
114
                        
115
                System.out.println("Origin = ("+ox+","+oy+")");
116
                System.out.println("Pixel Size = ("+resx+","+resy+")");
117
                  esq.add(new Point2D.Double(ox, oy));
118
                  esq.add(new Point2D.Double(ox+resx*width, oy));
119
                  esq.add(new Point2D.Double(ox, oy+resy*height));
120
                  esq.add(new Point2D.Double(ox+resx*width, oy+resy*height));
121
                
122
        }
123
        
124
        /********************************************************************/
125
        
126
        public void setAlpha(int a) { alpha = a; }
127
        
128
        /********************************************************************/
129
        
130
        public void setDataType(int dt) { dataType = dt; }
131
        
132
        /********************************************************************/
133
        
134
        //         Supone rasters no girados
135
        public Point2D worldToRaster(Point2D pt) {
136
                double x = (((double) currentFullWidth)/(esq.maxX-esq.minX))*(pt.getX()-esq.minX);
137
                double y = (((double) currentFullHeight)/(esq.maxY-esq.minY))*(esq.maxY-pt.getY());
138
                Point2D ptRes = new Point2D.Double(x, y);
139
                return ptRes;
140
        }
141
                
142
        /********************************************************************/
143
        
144
        public void setView(double dWorldTLX, double dWorldTLY,
145
                                    double dWorldBRX, double dWorldBRY,
146
                                                int nWidth, int nHeight) {
147
                
148
                //Ancho y alto de la im?gen en pixeles (pixeles de la overview)
149
                currentFullWidth = width;
150
                currentFullHeight = height;
151
                
152
                //Ventana de la imagen. (en tama?o completo)
153
                //tl->esq sup izda en pixeles
154
                //br->esq inf der en pixeles
155
                
156
                Point2D tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
157
                Point2D br = worldToRaster(new Point2D.Double(dWorldBRX, dWorldBRY));
158
                System.out.println("2========>"+tl.getX()+","+tl.getY()+","+br.getX()+","+br.getY()+","+width+","+height);
159
                
160
                //Ancho y alto de la im?gen (pixeles en pantalla)
161
                currentViewWidth = nWidth;
162
                currentViewHeight = nHeight;
163

    
164
                currentViewX = tl.getX();
165
                currentViewY = tl.getY();
166
                
167
                viewportScale = (double) currentViewWidth/(br.getX()-tl.getX());
168
                System.out.println("2.4========>"+currentViewWidth+","+br.getX()+" "+tl.getX()+" "+viewportScale);
169
                currentViewY = tl.getY();
170
                try {
171
                        // calcula el overview a usar
172
                        
173
                        
174
                        int[] dims=null;
175
                        double zoom=1.0;
176
                        zoomoverview=1.0;
177
                        currentOverview = -1;
178
                        if (WITH_OVERVIEWS && noverviews-1 > 0) {
179
                                for (int i=(noverviews-1); i>0; i--) {
180
                                        zoom = LTIUtils.levelToMag(i);
181
                                        dims=this.getDimsAtMag(zoom);
182
                                        System.out.println("2.5========>"+viewportScale+", dims="+dims[0]+","+dims[1]+
183
                                                        " zoom="+zoom+" this.getWidth()"+this.getWidth()+"\nviewportScale"+viewportScale);
184
                                        if (dims[0]>this.getWidth()*viewportScale) {
185
                                                currentOverview = i;
186
                                                System.out.println("2.6========>OVERVIEW SELECC="+i+" zoom="+zoom+" dims="+dims[0]+","+dims[1]);
187
                                                zoomoverview=zoom;
188
                                                viewportScale /= zoomoverview;
189
                                                currentFullWidth = dims[0];
190
                                    currentFullHeight = dims[1];
191
                                    tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
192
                                    currentViewX = tl.getX();
193
                                    currentViewY = tl.getY();
194
                                    break;
195
                                        }
196
                                }
197
                        }
198
                        
199
                        // Selecciona las bandas y los overviews necesarios
200

    
201
                        setDataType(eSampleType);
202
                        
203
                        
204
                } catch (MrSIDException e) {
205
                        e.printStackTrace();
206
                }
207

    
208
                System.out.println("MrSIDFile: TL=("+dWorldTLX+","+dWorldTLY+
209
                        "); BR=("+dWorldBRX+","+dWorldBRY+")\n"+
210
                        "MrSIDFile: escala="+viewportScale+"\n"+
211
                    "Actual Raster Size="+currentFullWidth+"x"+currentFullHeight);
212
                
213
        }
214
        
215
        /********************************************************************/
216
        
217
        void pintaInfo() {
218
                try {
219

    
220
                        System.out.println("GeoTransform:");
221
                        LTIGeoCoord geoc = this.getGeoCoord();
222
                        
223
                        System.out.println("  param[0]="+geoc.getX());
224
                        System.out.println("  param[0]="+geoc.getY());
225
                        System.out.println("  param[0]="+geoc.getXRes());
226
                        System.out.println("  param[0]="+geoc.getYRes());
227
                        System.out.println("  param[0]="+geoc.getXRot());
228
                        System.out.println("  param[0]="+geoc.getYRot());
229
                        System.out.println("Metadata:");
230
                        LTIMetadataDatabase metadata = this.getMetadata();
231
                        for (int i=0; i<metadata.getIndexCount(); i++)
232
                           {
233
                         LTIMetadataRecord rec = null;
234
                         rec = metadata.getDataByIndex(i);
235
                         System.out.println(rec.getTagName());
236
                         
237
                         if(rec.isScalar())System.out.println(rec.getScalarData());
238
                         else if(rec.isVector()){
239
                                 String[] s = rec.getVectorData();
240
                                 for(int j=0;j<s.length;j++)System.out.println("V"+j+"->"+s[j]);
241
                         }else if(rec.isArray()){
242
                                 String[] s = rec.getArrayData();
243
                                 for(int j=0;j<s.length;j++)System.out.println("A"+j+"->"+s[j]);
244
                         }
245
                         
246
                         else System.out.println("");
247
                       }
248
                } catch (MrSIDException e) {
249
                        // TODO Auto-generated catch block
250
                        e.printStackTrace();
251
                }
252
                
253
        }
254
        
255
        /********************************************************************/
256
        
257
        void pintaPaleta() {
258
        }
259
        
260
        /********************************************************************/
261
        
262
        public int readScene(int [] line) throws MrSIDException {
263
                
264
                int a = 0;
265
                int x = (int)currentViewX;
266
            int y = (int)currentViewY;
267
        int SceneWidth;
268
        int SceneHeight;
269
        
270
        try{
271
                if(x==0 && y==0){
272
                        SceneWidth = currentFullWidth;
273
                            SceneHeight = currentFullHeight;
274
                }else{
275
                            SceneWidth = (int) (((double)currentViewWidth)/viewportScale);
276
                            SceneHeight = (int) (((double)currentViewHeight)/viewportScale);
277
                }
278
                
279
                        
280
                        
281
                        if(SceneWidth==0)SceneWidth=1;
282
                        if(SceneHeight==0)SceneHeight=1;
283
                        System.out.println("2.9========>ColorSpace="+eColorSpace+" nbands="+nbands+" eSampleType="+eSampleType);
284
                        if(pixel==null)pixel = new LTIPixel(eColorSpace, nbands, eSampleType);
285
                        LTIScene scene = new LTIScene(x, y, SceneWidth, SceneHeight, zoomoverview);
286
                        // Este deber?a ser el constructor con ventana
287
                        LTISceneBuffer buffer = new LTISceneBuffer(pixel, SceneWidth, SceneHeight, 1);
288
                        System.out.println("3========>currentViewX/Y=("+currentViewX+","+currentViewY+
289
                                ")\n\t currentView Size=("+currentViewWidth+","+currentViewHeight+
290
                                ")\n\t currentScene Size=("+SceneWidth+","+SceneHeight+
291
                                ")\n\t (x,y)=("+x+","+y+") CurrentFull=("+currentFullWidth+","+currentFullHeight+
292
                                ")\n\t AnchoAlto=("+this.getWidth()+","+this.getHeight()+
293
                                ")\n\t Buff-length="+buffer.buf1.length+" zoom="+zoomoverview + 
294
                                ")\n\t viewportscale="+viewportScale);
295
                        ((LTIImageStage)this).read(scene, buffer);
296

    
297
                                  
298
                          if (dataType == LTIDataType.LTI_DATATYPE_UINT8 ||
299
                                  dataType == LTIDataType.LTI_DATATYPE_SINT8 ||
300
                                dataType == LTIDataType.LTI_DATATYPE_SINT16 ||
301
                                dataType == LTIDataType.LTI_DATATYPE_SINT32 ||
302
                                dataType ==LTIDataType.LTI_DATATYPE_UINT16 ||
303
                                dataType == LTIDataType.LTI_DATATYPE_UINT32){
304
                                  
305
                                  int kd, k;
306
                                  double scale = 1/viewportScale;
307
                                  if(eColorSpace==LTIColorSpace.LTI_COLORSPACE_RGB)
308
                                          //for(int k=0;k<buffer.size;k++)
309
                                          for (int y1=0; y1<currentViewHeight; y1++)
310
                                                  for (int x1=0; x1<currentViewWidth; x1++) {
311
                                                          kd = y1*currentViewWidth+x1;
312
                                                          k = ((int) (y1*scale))*SceneWidth+ (int)(((double) x1)*scale);
313
                                                          try {
314
                                                                  line[kd] = ((buffer.buf1[k])<<16) + ((buffer.buf2[k])<<8) + buffer.buf3[k];
315
                                                          } catch (java.lang.ArrayIndexOutOfBoundsException e) {
316
                                                          }
317
                                                  }
318
                                  
319
                                  if(eColorSpace==LTIColorSpace.LTI_COLORSPACE_GRAYSCALE)
320
                                           for(k=0;k<buffer.size;k++)
321
                                                   line[k]=buffer.buf1[k];
322
                                  
323
                          } else if (dataType == LTIDataType.LTI_DATATYPE_FLOAT32 ||
324
                                  dataType == LTIDataType.LTI_DATATYPE_FLOAT32) {
325
                          }
326
                          
327
                        buffer=null;
328
        }catch(MrSIDException e){
329
                e.printStackTrace();
330
        }
331
                
332
       return 0;
333
        }
334
}
335

    
336
public class MrSidFile extends GeoRasterFile {
337
        public final static int BAND_HEIGHT = 64;
338
        protected MrSidNative file = null;
339

    
340

    
341
        private Extent v = null;
342
        
343
        static {
344
                GeoRasterFile.registerExtension("sid", MrSidFile.class);
345
        }
346
        
347
        public MrSidFile(IProjection proj, String fName) {
348
                super(proj, fName);
349
                extent = new Extent();
350
                try {
351
                        file = new MrSidNative(fName); 
352
                        showOnOpen();
353
                        load();
354
                        bandCount = file.nbands;
355
                        if ( bandCount >= 3) {
356
                                setBand(RED_BAND,   1);
357
                                setBand(GREEN_BAND, 2);
358
                                setBand(BLUE_BAND,  3);
359
                        }
360
                } catch(Exception e){
361
                          System.out.println("Error en constructor de MrSID");
362
                          e.printStackTrace();
363
                          file = null;
364
                }
365
        }
366
        
367
        public GeoFile load() {
368
                extent = new Extent(file.esq.minX, file.esq.minY, file.esq.maxX, file.esq.maxY);
369
                return this;
370
        }
371
        
372
        public void close() {
373
                file=null;
374
        }
375
        
376
        public void setBand(int flag, int bandNr) {
377
                super.setBand(flag, bandNr);
378
                if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) file.rBandNr = bandNr;
379
                if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) file.gBandNr = bandNr;
380
                if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) file.bBandNr = bandNr;
381
        }
382
        public void setView(Extent e) { v = new Extent(e); }
383
        public Extent getView() { return v; }
384
        
385
        public int getWidth() {        return file.width; }
386
        public int getHeight() { return file.height;}
387

    
388
        /* (non-Javadoc)
389
         * @see org.cresques.io.GeoRasterFile#reProject(org.cresques.cts.ICoordTrans)
390
         */
391
        public void reProject(ICoordTrans rp) {
392
                // TODO Auto-generated method stub
393
                
394
        }
395
        /* (non-Javadoc)
396
         * @see org.cresques.io.GeoRasterFile#setTransparency(boolean)
397
         */
398
        public void setTransparency(boolean t) {
399
                // TODO Auto-generated method stub
400
        }
401
        public void setTransparency(int t) {
402
                // TODO Auto-generated method stub
403
        }
404
        /* (non-Javadoc)
405
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans)
406
         */
407
        public Image updateImage(int width, int height, ICoordTrans rp) {
408
                double dFileAspect, dWindowAspect;
409
                int line, pRGBArray[] = null;
410
                Image image = null;
411
                
412
                // Work out the correct aspect for the setView call.
413
                dFileAspect = (double)v.width()/(double)v.height();
414
                dWindowAspect = (double)width /(double)height;
415

    
416
                if (dFileAspect > dWindowAspect) {
417
                  height =(int)((double)width/dFileAspect);
418
                } else {
419
                  width = (int)((double)height*dFileAspect);
420
                }
421
                
422
                // Set the view
423
                
424
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),width, height);
425
                System.out.println("1========>"+v.minX()+","+ v.maxY()+","+v.maxX()+","+v.minY()+","+width+","+height);
426
                if (tFilter != null)
427
                        file.setAlpha(getAlpha());
428
                
429
                //Impedimos que los valores de ancho y alto de la im?gen sean menores que 1
430
                
431
                if(width<=0)width=1;
432
                if(height<=0)height=1;
433
                
434
                
435
                image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
436
                pRGBArray = new int[width*height];
437
                
438
                try {                
439
                        System.out.println("1.5========>"+width+","+height);
440
                        file.readScene(pRGBArray);
441
                        ((BufferedImage)image).setRGB(0, 0, width, height, pRGBArray, 0, width);
442
                        
443
                        
444
                } catch (Exception e) {
445
                        e.printStackTrace();
446
                }
447
                
448
                return image;
449
        }
450
        
451
        
452
        private void showOnOpen() {
453
                  // Report en la apertura (quitar)
454
                  System.out.println("Fichero GDAL '"+getName()+"' abierto.");
455
                  System.out.println("Version = "+file.version);
456
                  System.out.println("   Size = ("+file.width+","+file.height+")");
457
                System.out.println("   NumBands = ("+file.nbands+")");
458
        
459
                  file.pintaInfo();
460
                  file.pintaPaleta();
461

    
462
        }
463

    
464
        /* (non-Javadoc)
465
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans, java.awt.Image, int)
466
         */
467
        public Image updateImage(int width, int height, ICoordTrans rp, Image img, int flags) {
468
                // TODO Auto-generated method stub
469
                return null;
470
        }
471

    
472
        /* (non-Javadoc)
473
         * @see org.cresques.io.GeoRasterFile#getData(int, int, int)
474
         */
475
        public Object getData(int x, int y, int band) {
476
                // TODO Auto-generated method stub
477
                return null;
478
        }
479
}
480

    
481