Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / io / EcwDriver.java @ 11067

History | View | Annotate | Download (23.3 KB)

1 10960 nacho
/* 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.dataset.io;
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.dataset.BandList;
28
import org.gvsig.raster.dataset.FileNotOpenException;
29
import org.gvsig.raster.dataset.GeoInfo;
30
import org.gvsig.raster.dataset.IBuffer;
31
import org.gvsig.raster.dataset.InvalidSetViewException;
32
import org.gvsig.raster.dataset.NotSupportedExtensionException;
33
import org.gvsig.raster.dataset.RasterDataset;
34
import org.gvsig.raster.dataset.RasterDriverException;
35
import org.gvsig.raster.dataset.properties.DatasetTransparency;
36
import org.gvsig.raster.shared.Extent;
37
import org.gvsig.raster.util.RasterUtilities;
38
import org.gvsig.raster.util.extensionPoints.ExtensionPoints;
39
import org.gvsig.raster.util.extensionPoints.ExtensionPointsSingleton;
40
41
import com.ermapper.ecw.JNCSException;
42
import com.ermapper.ecw.JNCSFile;
43
import com.ermapper.ecw.JNCSFileNotOpenException;
44
import com.ermapper.ecw.JNCSInvalidSetViewException;
45
import com.ermapper.ecw.JNCSProgressiveUpdate;
46
47
/**
48
 * Driver de Ecw
49
 *
50
 * @author Nacho Brodin (nachobrodin@gmail.com)
51
 */
52
public class EcwDriver extends RasterDataset implements JNCSProgressiveUpdate {
53
54
        private JNCSFile                                 file = null;
55
        private int                                         currentFullWidth = -1;
56
        private int                                         currentFullHeight = -1;
57
        /**
58
         * Contorno en coordenadas geogr?ficas sin rotaci?n aplicada. Esto es util para poder
59
         * calcular los pixeles necesarios que se van a leer del raster.
60
         */
61
        public Contour                                        bBoxWithoutRot = new Contour();
62
        /**
63
         * Estado de transparencia del raster.
64
         */
65
        protected DatasetTransparency                fileTransparency = null;
66
67
        /**
68
         * Extent de la ventana seleccionada
69
         */
70
    private Extent view = null;
71
72
        static {
73
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
74
                extensionPoints.add("RasterDriver", "ecw", EcwDriver.class);
75
                extensionPoints.add("RasterDriver", "jp2", EcwDriver.class);
76
        }
77
78
        class Contour extends Vector {
79
                final private static long serialVersionUID = 0;
80
                public double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE;
81
                public double maxX = -Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
82
                public Contour() {
83
                        super();
84
                }
85
                public void add(Point2D pt) {
86
                        super.add(pt);
87
                        if (pt.getX() > maxX) maxX = pt.getX();
88
                        if (pt.getX() < minX) minX = pt.getX();
89
                        if (pt.getY() > maxY) maxY = pt.getY();
90
                        if (pt.getY() < minY) minY = pt.getY();
91
                }
92
        }
93
94
        /**
95
         * Constructor. Abre el dataset.
96
         * @param proj Proyecci?n
97
         * @param fName Nombre del fichero ecw
98
         * @throws NotSupportedExtensionException
99
         */
100
        public EcwDriver(IProjection proj, String fName)throws NotSupportedExtensionException {
101
                 super(proj, fName);
102
             extent = new Extent();
103
             try {
104
105
                     if (!new File(fName).exists() && !fName.startsWith("ecwp:"))
106
                             throw new NotSupportedExtensionException("Extension not supported");
107
108
                     file = new JNCSFile(fName, false);
109
                     load();
110
                     //readGeoInfo(fName);
111
                     bandCount = file.numBands;
112
                     getTransparencyDatasetStatus();
113
                     setDataType(IBuffer.TYPE_BYTE);
114
             } catch (Exception e) {
115
                     throw new NotSupportedExtensionException("Extension not supported");
116
             }
117
    }
118
119
        /**
120
     * Carga un ECW.
121
     * @param fname
122
     */
123
    public GeoInfo load() {
124
        double minX;
125
        double minY;
126
        double maxX;
127
        double maxY;
128
129
        if(file.cellIncrementY > 0)
130
                file.cellIncrementY = -file.cellIncrementY;
131
132
        minX = file.originX;
133
        maxY = file.originY;
134
        maxX = file.originX +
135
               ((double) (file.width) * file.cellIncrementX);
136
        minY = file.originY +
137
               ((double) (file.height) * file.cellIncrementY);
138
139
        currentFullWidth = file.width;
140
        currentFullHeight = file.height;
141
        boundingBoxWithoutRotation();
142
143
        extent = new Extent(minX, minY, maxX, maxY);
144
        requestExtent = extent;
145
        return this;
146
    }
147
148
    /**
149
         * Calcula la bounding box en la que est? metido el raster teniendo en cuenta
150
         * el tama?o de pixel y la rotaci?n.
151
         */
152
        private void boundingBoxWithoutRotation(){
153
                double ox = file.originX;
154
                double oy = file.originY;
155
                double resx = file.cellIncrementX;
156
                double resy = file.cellIncrementY;
157
158
                bBoxWithoutRot.add(new Point2D.Double(ox, oy));
159
                bBoxWithoutRot.add(new Point2D.Double(ox + resx * (file.width), oy));
160
                bBoxWithoutRot.add(new Point2D.Double(ox, oy + resy * (file.height)));
161
                bBoxWithoutRot.add(new Point2D.Double(ox + resx * (file.width), oy + resy * (file.height)));
162
        }
163
164
    /**
165
     * Cierra el fichero ECW
166
     */
167
    public void close() {
168
            if(file != null){
169
                    file.close(true);
170
                    file = null;
171
            }
172
    }
173
174
    /**
175
         * Obtiene el objeto que contiene el estado de la transparencia
176
         */
177
        public DatasetTransparency getTransparencyDatasetStatus() {
178
                if(fileTransparency == null)
179
                        fileTransparency = new DatasetTransparency();
180
                return fileTransparency;
181
        }
182
183
    /**
184
     * Devuelve el ancho de la imagen
185
     */
186
    public int getWidth() {
187
        return file.width;
188
    }
189
190
    /**
191
     * Devuelve el alto de la imagen
192
     */
193
    public int getHeight() {
194
        return file.height;
195
    }
196
197
    /**
198
         * Obtiene el extent de la ?ltima ventana seleccionada.
199
         * @return Extent
200
         */
201
        public Extent getView() {
202
        return view;
203
    }
204
205
        /*
206
         * (non-Javadoc)
207
         * @see org.gvsig.fmap.driver.RasterDataset#setView(org.gvsig.fmap.raster.Extent)
208
         */
209
        public void setView(Extent e) {
210
            //TODO: FUNCIONALIDAD: Falta aplicar la transformaci?n a la vista en caso de que haya un fichero .rmf. Mirar EcwFile de piloto
211
        view = new Extent(e);
212
    }
213
214
        /**
215
         * Cuando se hace una petici?n de carga de buffer la extensi?n pedida puede estar ajustada a la extensi?n del raster
216
         * o no estarlo. En caso de no estarlo los pixeles del buffer que caen fuera de la extensi?n del raster tendr?n valor
217
         * de NoData. Esta funci?n calcula en que pixel del buffer hay que empezar a escribir en caso de que este sea mayor
218
         * que los datos a leer.
219
         * @param dWorldTLX Posici?n X superior izquierda en coord reales
220
         * @param dWorldTLY Posici?n Y superior izquierda en coord reales
221
         * @param dWorldBRX Posici?n X inferior derecha en coord reales
222
         * @param dWorldBRY Posici?n Y inferior derecha en coord reales
223
         * @param nWidth Ancho en pixeles del buffer
224
         * @param nHeight Alto en pixeles del buffer
225
         * @return desplazamiento dentro del buffer en X e Y
226
         */
227
        private int[] calcStepBuffer(Extent dataExtent, int nWidth, int nHeight, int[] stpBuffer){
228
            Extent imageExtent = new Extent(bBoxWithoutRot.minX, bBoxWithoutRot.minY, bBoxWithoutRot.maxX, bBoxWithoutRot.maxY);
229
            Extent ajustDataExtent = RasterUtilities.calculateAdjustedView(dataExtent, imageExtent);
230
            if(!RasterUtilities.compareExtents(dataExtent, ajustDataExtent)){
231
                    Point2D p1 = worldToRaster(new Point2D.Double(ajustDataExtent.minX(), ajustDataExtent.maxY()));
232
                    Point2D p2 = worldToRaster(new Point2D.Double(ajustDataExtent.maxX(), ajustDataExtent.minY()));
233
                    Point2D p3 = worldToRaster(new Point2D.Double(dataExtent.minX(), dataExtent.maxY()));
234
                    Point2D p4 = worldToRaster(new Point2D.Double(dataExtent.maxX(), dataExtent.minY()));
235
                    //Ese es el ancho y alto q tendr?a el buffer en caso de haberse ajustado
236
                    int w = (int)Math.abs(Math.ceil(p2.getX()) - Math.floor(p1.getX()));
237
                    int h = (int)Math.abs(Math.floor(p1.getY()) - Math.ceil(p2.getY()));
238
239
                    stpBuffer[0] = (int)(p1.getX() + (-p3.getX()));
240
                    stpBuffer[1] = (int)(p1.getY() + (-p3.getY()));
241
                    stpBuffer[2] = stpBuffer[0] + w;
242
                    stpBuffer[3] = stpBuffer[1] + h;
243
                    return new int[]{w, h};
244
            }
245
            return new int[]{nWidth, nHeight};
246
        }
247
248
        /*
249
         * (non-Javadoc)
250
         * @see org.gvsig.fmap.driver.RasterDataset#worldToRaster(java.awt.geom.Point2D)
251
         */
252
        public Point2D worldToRaster(Point2D pt) {
253
                double x = ((pt.getX() - bBoxWithoutRot.minX) * ((double) currentFullWidth)) / (double)(bBoxWithoutRot.maxX - bBoxWithoutRot.minX);
254
                double y = ((bBoxWithoutRot.maxY - pt.getY()) * ((double) currentFullHeight)) / (double)(bBoxWithoutRot.maxY - bBoxWithoutRot.minY);
255
                Point2D ptRes = new Point2D.Double(x, y);
256
                return ptRes;
257
        }
258
259
        /*
260
         * (non-Javadoc)
261
         * @see org.gvsig.fmap.driver.RasterDataset#rasterToWorld(java.awt.geom.Point2D)
262
         */
263
        public Point2D rasterToWorld(Point2D pt) {
264
                double x = bBoxWithoutRot.minX + ((pt.getX() * (bBoxWithoutRot.maxX - bBoxWithoutRot.minX)) / currentFullWidth);
265
                double y = bBoxWithoutRot.maxY - ((pt.getY() * (bBoxWithoutRot.maxY - bBoxWithoutRot.minY)) / currentFullHeight);
266
                Point2D ptRes = new Point2D.Double(x, y);
267
                return ptRes;
268
        }
269
270
        /*
271
         * (non-Javadoc)
272
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(double, double, double, double, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer, boolean)
273
         */
274
        public IBuffer getWindowRaster(double x, double y, double w, double h, BandList bandList, IBuffer rasterBuf, boolean adjustToExtent) {
275
276
                //Si cojemos pixeles completos hemos de calcular las coordenadas reales de esos pixeles completos para hacer la petici?n.
277
                //Este el calculo que se hizo en RasterMultiDataset para calcular pixeles completos, convertir a pixel, redondear el primero
278
                //por abajo y el ultimo por arriba
279
                Point2D p1 = worldToRaster(new Point2D.Double(x, y));
280
                Point2D p2 = worldToRaster(new Point2D.Double(x + w, y - h));
281
                Point2D a = rasterToWorld(new Point2D.Double(Math.floor(p1.getX()), Math.floor(p1.getY())));
282
                Point2D b = rasterToWorld(new Point2D.Double(Math.ceil(p2.getX()), Math.ceil(p2.getY())));
283
284
                //Extent selectedExtent = new Extent(x, y, x + w, y - h);
285
                Extent selectedExtent = new Extent(a.getX(), a.getY(), b.getX(), b.getY());
286
                setView(selectedExtent);
287
                int wPx = rasterBuf.getWidth();
288
                int hPx = rasterBuf.getHeight();
289
                int[] stpBuffer = new int[]{0, 0 , wPx, hPx};
290
291
                //TODO: FUNCIONALIDAD: Implementar adjustToExtent = false
292
                /*if(!adjustToExtent){
293
                       int[] wh = calcStepBuffer(selectedExtent, wPx, hPx, stpBuffer);
294
                       if(x < 0)
295
                               x  = 0;
296
                       if(y < 0)
297
                               y  = 0;
298
                       readData(buf, bandList, x, y, wh[0], wh[1], wh[0], wh[1], 0, 0, stpBuffer);
299
                       return;
300
            }*/
301
302
                loadBuffer(selectedExtent, wPx, hPx, rasterBuf, bandList, stpBuffer);
303
304
                return rasterBuf;
305
        }
306
307
        /*
308
         * (non-Javadoc)
309
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(int, int, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer)
310
         */
311
        public IBuffer getWindowRaster(int x, int y, int w, int h, BandList bandList, IBuffer rasterBuf) {
312
                Point2D init = this.rasterToWorld(new Point2D.Double(x, y));
313
                Point2D end = this.rasterToWorld(new Point2D.Double(x + w, y + h));
314
                Extent selectedExtent = new Extent(init.getX(), init.getY(), end.getX(), end.getY());
315
                setView(selectedExtent);
316
                int[] stpBuffer = new int[]{0, 0 , w, h};
317
318
                loadBuffer(selectedExtent, w, h, rasterBuf, bandList, stpBuffer);
319
                return rasterBuf;
320
        }
321
322
        /*
323
         * (non-Javadoc)
324
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer, boolean)
325
         */
326
        public IBuffer getWindowRaster(double minX, double minY, double maxX, double maxY, int bufWidth, int bufHeight, BandList bandList, IBuffer rasterBuf, boolean adjustToExtent) {
327
                Extent selectedExtent = new Extent(minX, maxY, maxX, minY);
328
                setView(selectedExtent);
329
                int[] stpBuffer = new int[]{0, 0 , bufWidth, bufHeight};
330
331
                //TODO: FUNCIONALIDAD: Implementar adjustToExtent = false
332
333
                loadBuffer(selectedExtent, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
334
                return rasterBuf;
335
        }
336
337
        /*
338
         * (non-Javadoc)
339
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(int, int, int, int, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer)
340
         */
341
        public IBuffer getWindowRaster(int x, int y, int w, int h, int bufWidth, int bufHeight, BandList bandList, IBuffer rasterBuf) {
342
                Point2D init = this.rasterToWorld(new Point2D.Double(x, y));
343
                Point2D end = this.rasterToWorld(new Point2D.Double(x + w, y + h));
344
                Extent selectedExtent = new Extent(init.getX(), init.getY(), end.getX(), end.getY());
345
                setView(selectedExtent);
346
                int[] stpBuffer = new int[]{0, 0 , bufWidth, bufHeight};
347
348
                loadBuffer(selectedExtent, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
349
                return rasterBuf;
350
        }
351
352
        /**
353
         * Carga el buffer con las bandas RGB del raster con los par?metros especificados de extensi?n
354
         * y tama?o de buffer. El problema de ecw es que solo podemos leer 3 bandas de una vez ya que solo disponemos
355
         * de una llamada readLineRGBA. Para leer m?s bandas tendremos que hacer multiples llamadas a setView para leer
356
         * 3 cada vez.
357
         *
358
         * Si por ejemplo tenemos un ecw de 6 bandas [0, 1, 2, 3, 4, 5] y queremos cargar un buffer con el siguiente orden
359
         * [0, -, 2, -, 4, -] La variable readBandsFromECW har? la llamada a setView con los valores [0, 2, 4, 0, 0, 0]. La
360
         * funci?n drawRGB ser? la encargada de hacer el switch para obtener [0, -, 2, -, 4, -].
361
         *
362
         * Bug#1: Si se dispone de un ecw de m?s de tres bandas podemos llamar a setView con readBandsFromECW con el orden
363
         * que queramos, por ejemplo [3, 2, 5, 1, 0] pero para ecw de 3 bandas la llamada con las bandas cambiadas no
364
         * hace caso. El caso de readBandsFromECW = [2, 0, 1] ser? tomado siempre como [0, 1, 2].
365
         *
366
         * @param selectedExtent Extensi?n seleccionada
367
         * @param bufWidth Ancho de buffer
368
         * @param bufHeight Alto de buffer
369
         * @param rasterBuf Buffer de datos
370
         */
371
        private void loadBuffer(Extent selectedExtent, int bufWidth, int bufHeight, IBuffer rasterBuf, BandList bandList, int[] stpBuffer){
372
                try{
373
                        //Leemos el raster desde la librer?a
374
375
                        int[] readBandsFromECW = new int[file.numBands];
376
                        int[] readBands = new int[file.numBands];
377
378
379
                        for(int i = 0; i < readBandsFromECW.length; i ++)
380
                                readBands[i] = -1;
381
                        int cont = 0;
382
                        for(int i = 0; i < file.numBands; i++){
383
                                int[] bandsToDraw = bandList.getBand(i).getDataImageBandToDraw();
384
                                if(bandsToDraw != null){
385
                                        for(int j = 0; j < bandsToDraw.length; j++){
386
                                                readBandsFromECW[cont] = i;
387
                                                readBands[cont] = i;
388
                                                cont ++;
389
                                        }
390
                                }
391
392
                        }
393
394
                        file.setView(file.numBands, readBandsFromECW, selectedExtent.minX(), selectedExtent.maxY(), selectedExtent.maxX(), selectedExtent.minY(), bufWidth, bufHeight);
395
396
                        //Escribimos el raster sobre un IBuffer
397
                        int[] pRGBArray = new int[bufWidth];
398
                        drawRGB(rasterBuf, pRGBArray, readBands, bandList);
399
400
                }catch(JNCSInvalidSetViewException exc){
401
                        exc.printStackTrace();
402
                }catch (JNCSFileNotOpenException e) {
403
                        e.printStackTrace();
404
                }catch (JNCSException ex) {
405
                        ex.printStackTrace();
406
                }
407
408
        }
409
410
411
        private void drawRGB(IBuffer rasterBuf, int[] pRGBArray, int[] readBands, BandList bandList)throws JNCSException{
412
                byte data = 0;
413
                int bandR = readBands[0];
414
                int bandG = (readBands.length > 1) ? readBands[1] : -1;
415
                int bandB = (readBands.length > 2) ? readBands[2] : -1;
416
417
                //********* caso especial que resuelve Bug#1 **********************
418
                if(file.numBands == 3 && bandList.getDrawableBandsCount() < 3){
419
                        for(int i = 0; i < 3; i ++){
420
                                int[] b = bandList.getBand(i).getDataImageBandToDraw();
421
                                if(b != null){
422
                                        bandG = 1; bandR = 0; bandB = 2;
423
                                }
424
                        }
425
                }
426
                if(file.numBands == 3 && bandR == bandG && bandG == bandB){ //caso especial que resuelve Bug#1
427
                        for(int i = 0; i < 3; i ++){
428
                                int[] b = bandList.getBand(i).getDataImageBandToDraw();
429
                                if(b != null){
430
                                        if(i == 0){
431
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
432
                                            file.readLineRGBA(pRGBArray);
433
                                            for(int col = 0; col < pRGBArray.length; col ++){
434
                                                    rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
435
                                                    rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
436
                                                    rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
437
                                            }
438
                                        }
439
                                                return;
440
                                        }
441
                                        if(i == 1){
442
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
443
                                            file.readLineRGBA(pRGBArray);
444
                                            for(int col = 0; col < pRGBArray.length; col ++){
445
                                                    rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
446
                                                    rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
447
                                                    rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
448
                                            }
449
                                        }
450
                                                return;
451
                                        }
452
                                        if(i == 2){
453
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
454
                                            file.readLineRGBA(pRGBArray);
455
                                            for(int col = 0; col < pRGBArray.length; col ++){
456
                                                    rasterBuf.setElem(line, col, bandR, (byte)(pRGBArray[col] & 0x000000ff));
457
                                                    rasterBuf.setElem(line, col, bandG, (byte)(pRGBArray[col] & 0x000000ff));
458
                                                    rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
459
                                            }
460
                                        }
461
                                                return;
462
                                        }
463
                                }
464
                        }
465
466
                }
467
                //********* END caso especial que resuelve Bug#1 **********************
468
469
                if(bandR >= 0 && bandG >= 0 && bandB >= 0){
470
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
471
                    file.readLineRGBA(pRGBArray);
472
                    for(int col = 0; col < pRGBArray.length; col ++){
473
                            rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
474
                            rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
475
                            rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
476
                    }
477
                }
478
                        return;
479
                }
480
481
                if(bandR >= 0 && bandG >= 0){
482
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
483
                    file.readLineRGBA(pRGBArray);
484
                    for(int col = 0; col < pRGBArray.length; col ++){
485
                            rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
486
                            rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
487
                    }
488
                }
489
                        return;
490
                }
491
492
                if(bandR >= 0){
493
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
494
                    file.readLineRGBA(pRGBArray);
495
                    for(int col = 0; col < pRGBArray.length; col ++)
496
                            rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
497
                }
498
                        return;
499
                }
500
501
        }
502
503
        public void reProject(ICoordTrans rp) {
504
        }
505
506
        public void setExtentTransform(double originX, double originY, double psX, double psY) {
507
        }
508
509
        public int getBlockSize() {
510
                return 0;
511
        }
512
513
        /*
514
         * (non-Javadoc)
515
         * @see org.gvsig.raster.driver.RasterDataset#readCompletetLine(int, int)
516
         */
517
        public Object readCompleteLine(int line, int band) throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
518
                if(line < 0 || line >= file.height || band < 0 || band >= getBandCount())
519
                        throw new InvalidSetViewException("Request out of grid");
520
521
                Point2D begin = rasterToWorld(new Point2D.Double(0, line));
522
                Point2D end = rasterToWorld(new Point2D.Double(file.width, line + 1));
523
                int[] readBandsFromECW = new int[file.numBands];
524
                if(file.numBands <= 3) {
525
                        for(int i = 0; i < file.numBands; i++)
526
                                readBandsFromECW[i] = i;
527
                }else {
528
                        readBandsFromECW[0] = band;
529
                }
530
531
                Extent e = new Extent(begin.getX(), begin.getY(), end.getX(), end.getY());
532
533
                try {
534
                        int[] value = new int[file.width];
535
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, 1);
536
                        file.readLineRGBA(value);
537
538
                        if(file.numBands <= 3) {
539
                                switch(getDataType()) {
540
                                case IBuffer.TYPE_BYTE: byte[] b = new byte[file.width];
541
                                                                                switch(band) {
542
                                                                                case 0: for(int i = 0; i < file.width; i ++)
543
                                                                                                        b[i] = (byte)(((value[i] & 0x00ff0000) >> 16) & 0xff);
544
                                                                                                break;
545
                                                                                case 1: for(int i = 0; i < file.width; i ++)
546
                                                                                                        b[i] = (byte)(((value[i] & 0x0000ff00) >> 8) & 0xff);
547
                                                                                                break;
548
                                                                                case 2: for(int i = 0; i < file.width; i ++)
549
                                                                                                        b[i] = (byte)((value[i] & 0x000000ff) & 0xff);
550
                                                                                                break;
551
                                                                                }
552
                                                                                return b;
553
                                }
554
                        }else {
555
                                switch(getDataType()) {
556
                                case IBuffer.TYPE_BYTE: byte[] b = new byte[file.width];
557
                                                                                for(int i = 0; i < file.width; i ++)
558
                                                                                        b[i] = (byte)(((value[i] & 0x00ff0000) >> 16) & 0xff);
559
                                                                                break;
560
                                }
561
                        }
562
                        //TODO: FUNCIONALIDAD: Ecw con otro tipo de dato != Byte
563
                } catch (JNCSFileNotOpenException e1) {
564
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
565
                } catch (JNCSInvalidSetViewException e1) {
566
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
567
                } catch (JNCSException e1) {
568
                        throw new RasterDriverException("Error la lectura de datos ecw");
569
                }
570
571
                return null;
572
        }
573
574
        /*
575
         * (non-Javadoc)
576
         * @see org.gvsig.raster.driver.RasterDataset#getData(int, int, int)
577
         */
578
        public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
579
                if(x < 0 || y < 0 || x >= file.width || y >= file.height)
580
                        throw new InvalidSetViewException("Request out of grid");
581
582
                Point2D begin = rasterToWorld(new Point2D.Double(x, y));
583
                Point2D end = rasterToWorld(new Point2D.Double(x + 1, y + 1));
584
                int[] readBandsFromECW = new int[file.numBands];
585
                if(file.numBands <= 3){
586
                        for(int i = 0; i < file.numBands; i++)
587
                                readBandsFromECW[i] = i;
588
                }else{
589
                        readBandsFromECW[0] = band;
590
                }
591
592
                Extent e = new Extent(begin.getX(), begin.getY(), end.getX(), end.getY());
593
                try {
594
                        int[] value = new int[1];
595
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), 1, 1);
596
                        file.readLineRGBA(value);
597
                        if(file.numBands <= 3){
598
                                switch(band){
599
                                case 0: return new Integer((((value[0] & 0x00ff0000) >> 16) & 0xffffffff));
600
                                case 1: return new Integer((((value[0] & 0x0000ff00) >> 8) & 0xffffffff));
601
                                case 2: return new Integer((((value[0] & 0x000000ff)) & 0xffffffff));
602
                                }
603
                        }
604
                        return new Integer((((value[0] & 0x00ff0000) >> 16) & 0xffffffff));
605
                } catch (JNCSFileNotOpenException e1) {
606
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
607
                } catch (JNCSInvalidSetViewException e1) {
608
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
609
                } catch (JNCSException e1) {
610
                        throw new RasterDriverException("Error reading ecw data");
611
                }
612
        }
613
614
        public void refreshUpdate(int arg0, int arg1, double arg2, double arg3, double arg4, double arg5) {
615
        }
616
617
        public void refreshUpdate(int arg0, int arg1, int arg2, int arg3, int arg4, int arg5) {
618
        }
619
620
        /*
621
         * (non-Javadoc)
622
         * @see org.gvsig.raster.driver.GeoData#getStringProjection()
623
         */
624
        public String getStringProjection() throws RasterDriverException{
625
                return file.projection;
626
        }
627
628
}
629
630