Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / driver / EcwDriver.java @ 10740

History | View | Annotate | Download (23 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.raster.driver;
20

    
21
import java.awt.geom.Point2D;
22
import java.io.File;
23
import java.util.Vector;
24

    
25
import org.cresques.cts.ICoordTrans;
26
import org.cresques.cts.IProjection;
27
import org.gvsig.raster.driver.datasetproperties.DatasetTransparency;
28
import org.gvsig.raster.extensionPoints.ExtensionPoints;
29
import org.gvsig.raster.extensionPoints.ExtensionPointsSingleton;
30
import org.gvsig.raster.shared.Extent;
31
import org.gvsig.raster.shared.RasterUtilities;
32

    
33
import com.ermapper.ecw.JNCSException;
34
import com.ermapper.ecw.JNCSFile;
35
import com.ermapper.ecw.JNCSFileNotOpenException;
36
import com.ermapper.ecw.JNCSInvalidSetViewException;
37
import com.ermapper.ecw.JNCSProgressiveUpdate;
38

    
39
import es.gva.cit.jgdal.GdalException;
40

    
41

    
42
/**
43
 * Driver de Ecw
44
 * 
45
 * @author Nacho Brodin (nachobrodin@gmail.com)
46
 */
47
public class EcwDriver extends RasterDataset implements JNCSProgressiveUpdate {
48
    
49
        private JNCSFile                                 file = null;
50
        private int                                         currentFullWidth = -1;
51
        private int                                         currentFullHeight = -1;
52
        /**
53
         * Contorno en coordenadas geogr?ficas sin rotaci?n aplicada. Esto es util para poder
54
         * calcular los pixeles necesarios que se van a leer del raster.
55
         */
56
        public Contour                                        bBoxWithoutRot = new Contour();
57
        /**
58
         * Estado de transparencia del raster.
59
         */
60
        protected DatasetTransparency                fileTransparency = null;
61
        
62
        /**
63
         * Extent de la ventana seleccionada
64
         */
65
    private Extent view = null;
66
    
67
        static {
68
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
69
                extensionPoints.add("RasterDriver", "ecw", EcwDriver.class);
70
                extensionPoints.add("RasterDriver", "jp2", EcwDriver.class);
71
        }
72
                
73
        class Contour extends Vector {
74
                final private static long serialVersionUID = 0;
75
                public double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE;
76
                public double maxX = -Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
77
                public Contour() {
78
                        super();
79
                }
80
                public void add(Point2D pt) {
81
                        super.add(pt);
82
                        if (pt.getX() > maxX) maxX = pt.getX();
83
                        if (pt.getX() < minX) minX = pt.getX();
84
                        if (pt.getY() > maxY) maxY = pt.getY();
85
                        if (pt.getY() < minY) minY = pt.getY();
86
                }
87
        }
88
        
89
        /**
90
         * Constructor. Abre el dataset.
91
         * @param proj Proyecci?n
92
         * @param fName Nombre del fichero ecw
93
         * @throws NotSupportedExtensionException
94
         */
95
        public EcwDriver(IProjection proj, String fName)throws NotSupportedExtensionException {
96
                 super(proj, fName);
97
             extent = new Extent();
98
             try {
99

    
100
                     if (!new File(fName).exists() && !fName.startsWith("ecwp:"))
101
                             throw new NotSupportedExtensionException("Extension not supported");
102

    
103
                     file = new JNCSFile(fName, false);
104
                     load();
105
                     //readGeoInfo(fName);
106
                     bandCount = file.numBands;
107
                     getTransparencyDatasetStatus();
108
                     setDataType(IBuffer.TYPE_BYTE);
109
             } catch (Exception e) {
110
                     throw new NotSupportedExtensionException("Extension not supported");
111
             }
112
    }
113

    
114
        /**
115
     * Carga un ECW.
116
     * @param fname
117
     */
118
    public GeoData load() {
119
        double minX;
120
        double minY;
121
        double maxX;
122
        double maxY;
123

    
124
        if(file.cellIncrementY > 0)
125
                file.cellIncrementY = -file.cellIncrementY;
126
        
127
        minX = file.originX;
128
        maxY = file.originY;
129
        maxX = file.originX +
130
               ((double) (file.width) * file.cellIncrementX);
131
        minY = file.originY +
132
               ((double) (file.height) * file.cellIncrementY);
133
        
134
        currentFullWidth = file.width;
135
        currentFullHeight = file.height;
136
        boundingBoxWithoutRotation();
137
        
138
        extent = new Extent(minX, minY, maxX, maxY);
139
        requestExtent = extent;
140
        return this;
141
    }
142
    
143
    /**
144
         * Calcula la bounding box en la que est? metido el raster teniendo en cuenta
145
         * el tama?o de pixel y la rotaci?n. 
146
         */
147
        private void boundingBoxWithoutRotation(){
148
                double ox = file.originX;
149
                double oy = file.originY;
150
                double resx = file.cellIncrementX;
151
                double resy = file.cellIncrementY;
152
                                
153
                bBoxWithoutRot.add(new Point2D.Double(ox, oy));
154
                bBoxWithoutRot.add(new Point2D.Double(ox + resx * (file.width), oy));
155
                bBoxWithoutRot.add(new Point2D.Double(ox, oy + resy * (file.height)));
156
                bBoxWithoutRot.add(new Point2D.Double(ox + resx * (file.width), oy + resy * (file.height)));
157
        }
158

    
159
    /**
160
     * Cierra el fichero ECW
161
     */
162
    public void close() {
163
            if(file != null){
164
                    file.close(true);
165
                    file = null;
166
            }
167
    }
168
    
169
    /**
170
         * Obtiene el objeto que contiene el estado de la transparencia
171
         */
172
        public DatasetTransparency getTransparencyDatasetStatus() {
173
                if(fileTransparency == null)
174
                        fileTransparency = new DatasetTransparency();
175
                return fileTransparency;
176
        }
177
        
178
    /**
179
     * Devuelve el ancho de la imagen
180
     */
181
    public int getWidth() {
182
        return file.width;
183
    }
184

    
185
    /**
186
     * Devuelve el alto de la imagen
187
     */
188
    public int getHeight() {
189
        return file.height;
190
    }
191
    
192
    /**
193
         * Obtiene el extent de la ?ltima ventana seleccionada.
194
         * @return Extent
195
         */
196
        public Extent getView() {
197
        return view;
198
    }
199
        
200
        /*
201
         * (non-Javadoc)
202
         * @see org.gvsig.fmap.driver.RasterDataset#setView(org.gvsig.fmap.raster.Extent)
203
         */
204
        public void setView(Extent e) {
205
            //TODO: FUNCIONALIDAD: Falta aplicar la transformaci?n a la vista en caso de que haya un fichero .rmf. Mirar EcwFile de piloto
206
        view = new Extent(e);
207
    }
208
        
209
        /**
210
         * Cuando se hace una petici?n de carga de buffer la extensi?n pedida puede estar ajustada a la extensi?n del raster
211
         * o no estarlo. En caso de no estarlo los pixeles del buffer que caen fuera de la extensi?n del raster tendr?n valor
212
         * de NoData. Esta funci?n calcula en que pixel del buffer hay que empezar a escribir en caso de que este sea mayor
213
         * que los datos a leer.
214
         * @param dWorldTLX Posici?n X superior izquierda en coord reales
215
         * @param dWorldTLY Posici?n Y superior izquierda en coord reales
216
         * @param dWorldBRX Posici?n X inferior derecha en coord reales
217
         * @param dWorldBRY Posici?n Y inferior derecha en coord reales
218
         * @param nWidth Ancho en pixeles del buffer
219
         * @param nHeight Alto en pixeles del buffer
220
         * @return desplazamiento dentro del buffer en X e Y
221
         */ 
222
        private int[] calcStepBuffer(Extent dataExtent, int nWidth, int nHeight, int[] stpBuffer){
223
            Extent imageExtent = new Extent(bBoxWithoutRot.minX, bBoxWithoutRot.minY, bBoxWithoutRot.maxX, bBoxWithoutRot.maxY);
224
            Extent ajustDataExtent = RasterUtilities.calculateAdjustedView(dataExtent, imageExtent);
225
            if(!RasterUtilities.compareExtents(dataExtent, ajustDataExtent)){
226
                    Point2D p1 = worldToRaster(new Point2D.Double(ajustDataExtent.minX(), ajustDataExtent.maxY()));
227
                    Point2D p2 = worldToRaster(new Point2D.Double(ajustDataExtent.maxX(), ajustDataExtent.minY()));
228
                    Point2D p3 = worldToRaster(new Point2D.Double(dataExtent.minX(), dataExtent.maxY()));
229
                    Point2D p4 = worldToRaster(new Point2D.Double(dataExtent.maxX(), dataExtent.minY()));
230
                    //Ese es el ancho y alto q tendr?a el buffer en caso de haberse ajustado
231
                    int w = (int)Math.abs(Math.ceil(p2.getX()) - Math.floor(p1.getX())); 
232
                    int h = (int)Math.abs(Math.floor(p1.getY()) - Math.ceil(p2.getY()));
233
                    
234
                    stpBuffer[0] = (int)(p1.getX() + (-p3.getX()));
235
                    stpBuffer[1] = (int)(p1.getY() + (-p3.getY()));
236
                    stpBuffer[2] = stpBuffer[0] + w; 
237
                    stpBuffer[3] = stpBuffer[1] + h;
238
                    return new int[]{w, h};
239
            }
240
            return new int[]{nWidth, nHeight};
241
        }
242
        
243
        /*
244
         * (non-Javadoc)
245
         * @see org.gvsig.fmap.driver.RasterDataset#worldToRaster(java.awt.geom.Point2D)
246
         */
247
        public Point2D worldToRaster(Point2D pt) {
248
                double x = ((pt.getX() - bBoxWithoutRot.minX) * ((double) currentFullWidth)) / (double)(bBoxWithoutRot.maxX - bBoxWithoutRot.minX);
249
                double y = ((bBoxWithoutRot.maxY - pt.getY()) * ((double) currentFullHeight)) / (double)(bBoxWithoutRot.maxY - bBoxWithoutRot.minY);
250
                Point2D ptRes = new Point2D.Double(x, y);
251
                return ptRes;
252
        }
253
        
254
        /*
255
         * (non-Javadoc)
256
         * @see org.gvsig.fmap.driver.RasterDataset#rasterToWorld(java.awt.geom.Point2D)
257
         */
258
        public Point2D rasterToWorld(Point2D pt) {
259
                double x = bBoxWithoutRot.minX + ((pt.getX() * (bBoxWithoutRot.maxX - bBoxWithoutRot.minX)) / currentFullWidth);
260
                double y = bBoxWithoutRot.maxY - ((pt.getY() * (bBoxWithoutRot.maxY - bBoxWithoutRot.minY)) / currentFullHeight);
261
                Point2D ptRes = new Point2D.Double(x, y);
262
                return ptRes;
263
        }
264
        
265
        /*
266
         * (non-Javadoc)
267
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(double, double, double, double, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer, boolean)
268
         */
269
        public IBuffer getWindowRaster(double x, double y, double w, double h, BandList bandList, IBuffer rasterBuf, boolean adjustToExtent) {
270
                
271
                //Si cojemos pixeles completos hemos de calcular las coordenadas reales de esos pixeles completos para hacer la petici?n.
272
                //Este el calculo que se hizo en RasterMultiDataset para calcular pixeles completos, convertir a pixel, redondear el primero
273
                //por abajo y el ultimo por arriba
274
                Point2D p1 = worldToRaster(new Point2D.Double(x, y));
275
                Point2D p2 = worldToRaster(new Point2D.Double(x + w, y - h));
276
                Point2D a = rasterToWorld(new Point2D.Double(Math.floor(p1.getX()), Math.floor(p1.getY())));
277
                Point2D b = rasterToWorld(new Point2D.Double(Math.ceil(p2.getX()), Math.ceil(p2.getY())));
278

    
279
                //Extent selectedExtent = new Extent(x, y, x + w, y - h);
280
                Extent selectedExtent = new Extent(a.getX(), a.getY(), b.getX(), b.getY());
281
                setView(selectedExtent);
282
                int wPx = rasterBuf.getWidth();
283
                int hPx = rasterBuf.getHeight();
284
                int[] stpBuffer = new int[]{0, 0 , wPx, hPx};
285
                
286
                //TODO: FUNCIONALIDAD: Implementar adjustToExtent = false
287
                /*if(!adjustToExtent){
288
                       int[] wh = calcStepBuffer(selectedExtent, wPx, hPx, stpBuffer);
289
                       if(x < 0)
290
                               x  = 0;
291
                       if(y < 0)
292
                               y  = 0;
293
                       readData(buf, bandList, x, y, wh[0], wh[1], wh[0], wh[1], 0, 0, stpBuffer);
294
                       return;
295
            }*/
296
                 
297
                loadBuffer(selectedExtent, wPx, hPx, rasterBuf, bandList, stpBuffer);
298
                
299
                return rasterBuf;
300
        }
301

    
302
        /*
303
         * (non-Javadoc)
304
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(int, int, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer)
305
         */
306
        public IBuffer getWindowRaster(int x, int y, int w, int h, BandList bandList, IBuffer rasterBuf) {
307
                Point2D init = this.rasterToWorld(new Point2D.Double(x, y));
308
                Point2D end = this.rasterToWorld(new Point2D.Double(x + w, y + h));
309
                Extent selectedExtent = new Extent(init.getX(), init.getY(), end.getX(), end.getY());
310
                setView(selectedExtent);
311
                int[] stpBuffer = new int[]{0, 0 , w, h};
312
                
313
                loadBuffer(selectedExtent, w, h, rasterBuf, bandList, stpBuffer);
314
                return rasterBuf;
315
        }
316
        
317
        /*
318
         * (non-Javadoc)
319
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer, boolean)
320
         */
321
        public IBuffer getWindowRaster(double minX, double minY, double maxX, double maxY, int bufWidth, int bufHeight, BandList bandList, IBuffer rasterBuf, boolean adjustToExtent) {
322
                Extent selectedExtent = new Extent(minX, maxY, maxX, minY);
323
                setView(selectedExtent);
324
                int[] stpBuffer = new int[]{0, 0 , bufWidth, bufHeight};
325
                
326
                //TODO: FUNCIONALIDAD: Implementar adjustToExtent = false
327
                
328
                loadBuffer(selectedExtent, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
329
                return rasterBuf;
330
        }
331

    
332
        /*
333
         * (non-Javadoc)
334
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(int, int, int, int, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer)
335
         */
336
        public IBuffer getWindowRaster(int x, int y, int w, int h, int bufWidth, int bufHeight, BandList bandList, IBuffer rasterBuf) {
337
                Point2D init = this.rasterToWorld(new Point2D.Double(x, y));
338
                Point2D end = this.rasterToWorld(new Point2D.Double(x + w, y + h));
339
                Extent selectedExtent = new Extent(init.getX(), init.getY(), end.getX(), end.getY());
340
                setView(selectedExtent);
341
                int[] stpBuffer = new int[]{0, 0 , bufWidth, bufHeight};
342
                
343
                loadBuffer(selectedExtent, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
344
                return rasterBuf;
345
        }
346

    
347
        /**
348
         * Carga el buffer con las bandas RGB del raster con los par?metros especificados de extensi?n
349
         * y tama?o de buffer. El problema de ecw es que solo podemos leer 3 bandas de una vez ya que solo disponemos
350
         * de una llamada readLineRGBA. Para leer m?s bandas tendremos que hacer multiples llamadas a setView para leer
351
         * 3 cada vez.
352
         * 
353
         * Si por ejemplo tenemos un ecw de 6 bandas [0, 1, 2, 3, 4, 5] y queremos cargar un buffer con el siguiente orden
354
         * [0, -, 2, -, 4, -] La variable readBandsFromECW har? la llamada a setView con los valores [0, 2, 4, 0, 0, 0]. La
355
         * funci?n drawRGB ser? la encargada de hacer el switch para obtener [0, -, 2, -, 4, -].
356
         * 
357
         * Bug#1: Si se dispone de un ecw de m?s de tres bandas podemos llamar a setView con readBandsFromECW con el orden
358
         * que queramos, por ejemplo [3, 2, 5, 1, 0] pero para ecw de 3 bandas la llamada con las bandas cambiadas no
359
         * hace caso. El caso de readBandsFromECW = [2, 0, 1] ser? tomado siempre como [0, 1, 2]. 
360
         * 
361
         * @param selectedExtent Extensi?n seleccionada
362
         * @param bufWidth Ancho de buffer
363
         * @param bufHeight Alto de buffer
364
         * @param rasterBuf Buffer de datos
365
         */
366
        private void loadBuffer(Extent selectedExtent, int bufWidth, int bufHeight, IBuffer rasterBuf, BandList bandList, int[] stpBuffer){
367
                try{
368
                        //Leemos el raster desde la librer?a 
369
                
370
                        int[] readBandsFromECW = new int[file.numBands];
371
                        int[] readBands = new int[file.numBands];
372
                        
373
                        
374
                        for(int i = 0; i < readBandsFromECW.length; i ++)
375
                                readBands[i] = -1;
376
                        int cont = 0;
377
                        for(int i = 0; i < file.numBands; i++){
378
                                int[] bandsToDraw = bandList.getBand(i).getDataImageBandToDraw();
379
                                if(bandsToDraw != null){
380
                                        for(int j = 0; j < bandsToDraw.length; j++){
381
                                                readBandsFromECW[cont] = i;
382
                                                readBands[cont] = i;
383
                                                cont ++;
384
                                        }
385
                                }
386

    
387
                        }
388
                                                
389
                        file.setView(file.numBands, readBandsFromECW, selectedExtent.minX(), selectedExtent.maxY(), selectedExtent.maxX(), selectedExtent.minY(), bufWidth, bufHeight);
390
                        
391
                        //Escribimos el raster sobre un IBuffer
392
                        int[] pRGBArray = new int[bufWidth];
393
                        drawRGB(rasterBuf, pRGBArray, readBands, bandList);
394
                                                                                                        
395
                }catch(JNCSInvalidSetViewException exc){
396
                        exc.printStackTrace();
397
                }catch (JNCSFileNotOpenException e) {
398
                        e.printStackTrace();
399
                }catch (JNCSException ex) {
400
                        ex.printStackTrace();
401
                }
402
                
403
        }
404
        
405
        
406
        private void drawRGB(IBuffer rasterBuf, int[] pRGBArray, int[] readBands, BandList bandList)throws JNCSException{
407
                byte data = 0;
408
                int bandR = readBands[0];
409
                int bandG = (readBands.length > 1) ? readBands[1] : -1;
410
                int bandB = (readBands.length > 2) ? readBands[2] : -1;
411
        
412
                //********* caso especial que resuelve Bug#1 **********************
413
                if(file.numBands == 3 && bandList.getDrawableBandsCount() < 3){  
414
                        for(int i = 0; i < 3; i ++){
415
                                int[] b = bandList.getBand(i).getDataImageBandToDraw();
416
                                if(b != null){
417
                                        bandG = 1; bandR = 0; bandB = 2;
418
                                }
419
                        }
420
                }
421
                if(file.numBands == 3 && bandR == bandG && bandG == bandB){ //caso especial que resuelve Bug#1
422
                        for(int i = 0; i < 3; i ++){
423
                                int[] b = bandList.getBand(i).getDataImageBandToDraw();
424
                                if(b != null){
425
                                        if(i == 0){
426
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
427
                                            file.readLineRGBA(pRGBArray);
428
                                            for(int col = 0; col < pRGBArray.length; col ++){
429
                                                    rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
430
                                                    rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
431
                                                    rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
432
                                            }
433
                                        }
434
                                                return;
435
                                        }
436
                                        if(i == 1){
437
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
438
                                            file.readLineRGBA(pRGBArray);
439
                                            for(int col = 0; col < pRGBArray.length; col ++){
440
                                                    rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
441
                                                    rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
442
                                                    rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
443
                                            }
444
                                        }
445
                                                return;
446
                                        }
447
                                        if(i == 2){
448
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
449
                                            file.readLineRGBA(pRGBArray);
450
                                            for(int col = 0; col < pRGBArray.length; col ++){
451
                                                    rasterBuf.setElem(line, col, bandR, (byte)(pRGBArray[col] & 0x000000ff));
452
                                                    rasterBuf.setElem(line, col, bandG, (byte)(pRGBArray[col] & 0x000000ff));
453
                                                    rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
454
                                            }
455
                                        }
456
                                                return;
457
                                        }
458
                                }
459
                        }
460

    
461
                }
462
                //********* END caso especial que resuelve Bug#1 **********************
463
                
464
                if(bandR >= 0 && bandG >= 0 && bandB >= 0){
465
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
466
                    file.readLineRGBA(pRGBArray);
467
                    for(int col = 0; col < pRGBArray.length; col ++){
468
                            rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
469
                            rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
470
                            rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
471
                    }
472
                }
473
                        return;
474
                }
475
                
476
                if(bandR >= 0 && bandG >= 0){
477
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
478
                    file.readLineRGBA(pRGBArray);
479
                    for(int col = 0; col < pRGBArray.length; col ++){
480
                            rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
481
                            rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
482
                    }
483
                }
484
                        return;
485
                }
486
                                
487
                if(bandR >= 0){
488
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
489
                    file.readLineRGBA(pRGBArray);
490
                    for(int col = 0; col < pRGBArray.length; col ++)
491
                            rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
492
                }
493
                        return;
494
                }
495

    
496
        }
497
        
498
        public void reProject(ICoordTrans rp) {
499
        }
500

    
501
        public void setExtentTransform(double originX, double originY, double psX, double psY) {
502
        }
503

    
504
        public int getBlockSize() {
505
                return 0;
506
        }
507

    
508
        /*
509
         * (non-Javadoc)
510
         * @see org.gvsig.raster.driver.RasterDataset#readCompletetLine(int, int)
511
         */
512
        public Object readCompletetLine(int line, int band) throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
513
                if(line < 0 || line >= file.height || band < 0 || band >= getBandCount())
514
                        throw new InvalidSetViewException("Request out of grid");
515
                
516
                Point2D begin = rasterToWorld(new Point2D.Double(0, line));
517
                Point2D end = rasterToWorld(new Point2D.Double(file.width, line + 1));
518
                int[] readBandsFromECW = new int[file.numBands];
519
                if(file.numBands <= 3) {
520
                        for(int i = 0; i < file.numBands; i++)
521
                                readBandsFromECW[i] = i;
522
                }else {
523
                        readBandsFromECW[0] = band;
524
                }
525
                
526
                Extent e = new Extent(begin.getX(), begin.getY(), end.getX(), end.getY());
527
                
528
                try {
529
                        int[] value = new int[file.width];
530
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, 1);
531
                        file.readLineRGBA(value);
532
                        
533
                        if(file.numBands <= 3) {
534
                                switch(getDataType()) {
535
                                case IBuffer.TYPE_BYTE: byte[] b = new byte[file.width];
536
                                                                                switch(band) {
537
                                                                                case 0: for(int i = 0; i < file.width; i ++)
538
                                                                                                        b[i] = (byte)(((value[i] & 0x00ff0000) >> 16) & 0xff);
539
                                                                                                break;
540
                                                                                case 1: for(int i = 0; i < file.width; i ++)
541
                                                                                                        b[i] = (byte)(((value[i] & 0x0000ff00) >> 8) & 0xff);
542
                                                                                                break;
543
                                                                                case 2: for(int i = 0; i < file.width; i ++)
544
                                                                                                        b[i] = (byte)((value[i] & 0x000000ff) & 0xff);
545
                                                                                                break;
546
                                                                                }
547
                                                                                return b;
548
                                }
549
                        }else {
550
                                switch(getDataType()) {
551
                                case IBuffer.TYPE_BYTE: byte[] b = new byte[file.width];
552
                                                                                for(int i = 0; i < file.width; i ++)
553
                                                                                        b[i] = (byte)(((value[i] & 0x00ff0000) >> 16) & 0xff);
554
                                                                                break;
555
                                }
556
                        }
557
                        //TODO: FUNCIONALIDAD: Ecw con otro tipo de dato != Byte
558
                } catch (JNCSFileNotOpenException e1) {
559
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
560
                } catch (JNCSInvalidSetViewException e1) {
561
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
562
                } catch (JNCSException e1) {
563
                        throw new RasterDriverException("Error la lectura de datos ecw");
564
                }
565
                
566
                return null;
567
        }
568
        
569
        /*
570
         * (non-Javadoc)
571
         * @see org.gvsig.raster.driver.RasterDataset#getData(int, int, int)
572
         */
573
        public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
574
                if(x < 0 || y < 0 || x >= file.width || y >= file.height)
575
                        throw new InvalidSetViewException("Request out of grid");
576
                
577
                Point2D begin = rasterToWorld(new Point2D.Double(x, y));
578
                Point2D end = rasterToWorld(new Point2D.Double(x + 1, y + 1));
579
                int[] readBandsFromECW = new int[file.numBands];
580
                if(file.numBands <= 3){
581
                        for(int i = 0; i < file.numBands; i++)
582
                                readBandsFromECW[i] = i;
583
                }else{
584
                        readBandsFromECW[0] = band;
585
                }
586
                
587
                Extent e = new Extent(begin.getX(), begin.getY(), end.getX(), end.getY());                
588
                try {
589
                        int[] value = new int[1];
590
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), 1, 1);
591
                        file.readLineRGBA(value);
592
                        if(file.numBands <= 3){
593
                                switch(band){
594
                                case 0: return new Integer((((value[0] & 0x00ff0000) >> 16) & 0xffffffff));
595
                                case 1: return new Integer((((value[0] & 0x0000ff00) >> 8) & 0xffffffff));
596
                                case 2: return new Integer((((value[0] & 0x000000ff)) & 0xffffffff));
597
                                }
598
                        }
599
                        return new Integer((((value[0] & 0x00ff0000) >> 16) & 0xffffffff));
600
                } catch (JNCSFileNotOpenException e1) {
601
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
602
                } catch (JNCSInvalidSetViewException e1) {
603
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
604
                } catch (JNCSException e1) {
605
                        throw new RasterDriverException("Error reading ecw data");
606
                }
607
        }
608
        
609
        public void refreshUpdate(int arg0, int arg1, double arg2, double arg3, double arg4, double arg5) {
610
        }
611

    
612
        public void refreshUpdate(int arg0, int arg1, int arg2, int arg3, int arg4, int arg5) {
613
        }
614
        
615
        /*
616
         * (non-Javadoc)
617
         * @see org.gvsig.raster.driver.GeoData#getStringProjection()
618
         */
619
        public String getStringProjection() throws RasterDriverException{
620
                return file.projection;
621
        }
622
        
623
}
624

    
625

    
626