Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / io / ErmapperDriver.java @ 12712

History | View | Annotate | Download (24 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.dataset.io;
20

    
21
import java.awt.geom.AffineTransform;
22
import java.awt.geom.NoninvertibleTransformException;
23
import java.awt.geom.Point2D;
24
import java.io.File;
25
import java.util.Vector;
26

    
27
import org.cresques.cts.ICoordTrans;
28
import org.cresques.cts.IProjection;
29
import org.gvsig.raster.dataset.BandList;
30
import org.gvsig.raster.dataset.FileNotOpenException;
31
import org.gvsig.raster.dataset.GeoInfo;
32
import org.gvsig.raster.dataset.IBuffer;
33
import org.gvsig.raster.dataset.InvalidSetViewException;
34
import org.gvsig.raster.dataset.NotSupportedExtensionException;
35
import org.gvsig.raster.dataset.RasterDataset;
36
import org.gvsig.raster.dataset.RasterDriverException;
37
import org.gvsig.raster.dataset.io.rmf.ParsingException;
38
import org.gvsig.raster.datastruct.Extent;
39
import org.gvsig.raster.datastruct.Transparency;
40
import org.gvsig.raster.util.RasterUtilities;
41
import org.gvsig.raster.util.extensionPoints.ExtensionPoints;
42
import org.gvsig.raster.util.extensionPoints.ExtensionPointsSingleton;
43

    
44
import com.ermapper.ecw.JNCSException;
45
import com.ermapper.ecw.JNCSFile;
46
import com.ermapper.ecw.JNCSFileNotOpenException;
47
import com.ermapper.ecw.JNCSInvalidSetViewException;
48
import com.ermapper.ecw.JNCSProgressiveUpdate;
49
/**
50
 * Driver de Ecw
51
 *
52
 * @author Nacho Brodin (nachobrodin@gmail.com)
53
 */
54
public class ErmapperDriver extends RasterDataset implements JNCSProgressiveUpdate {
55

    
56
        private JNCSFile                                 file = null;
57

    
58
        /**
59
         * Estado de transparencia del raster.
60
         */
61
        protected Transparency                  fileTransparency = null;
62

    
63
        /**
64
         * Extent de la ventana seleccionada
65
         */
66
        private Extent                  viewRequest = null;
67
        private boolean                                 readCancel = false;
68

    
69
        public static void register() {
70
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
71
                extensionPoints.add("RasterReader", "ecw", ErmapperDriver.class);
72
                extensionPoints.add("RasterReader", "jp2", ErmapperDriver.class);
73
        }
74

    
75
        class Contour extends Vector {
76
                final private static long serialVersionUID = 0;
77
                public double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE;
78
                public double maxX = -Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
79
                public Contour() {
80
                        super();
81
                }
82
                public void add(Point2D pt) {
83
                        super.add(pt);
84
                        if (pt.getX() > maxX) maxX = pt.getX();
85
                        if (pt.getX() < minX) minX = pt.getX();
86
                        if (pt.getY() > maxY) maxY = pt.getY();
87
                        if (pt.getY() < minY) minY = pt.getY();
88
                }
89
        }
90

    
91
        /**
92
         * Constructor. Abre el dataset.
93
         * @param proj Proyecci?n
94
         * @param fName Nombre del fichero ecw
95
         * @throws NotSupportedExtensionException
96
         */
97
        public ErmapperDriver(IProjection proj, Object param)throws NotSupportedExtensionException {
98
                super(proj, ((String)param));
99
                setParam(param);
100
                try {
101
                        
102
                        if (!new File(((String)param)).exists() && !((String)param).startsWith("ecwp:"))
103
                                throw new NotSupportedExtensionException("Extension not supported");
104
                        
105
                        file = new JNCSFile(((String)param), false);
106
                        load();
107
                        bandCount = file.numBands;
108
                        getTransparencyDatasetStatus();
109
                        setDataType(IBuffer.TYPE_BYTE);
110
                        
111
                        super.init();
112
                        
113
                        try {
114
                                loadFromRmf(getRmfBlocksManager());
115
                        } catch (ParsingException e) {
116
                                //No lee desde rmf
117
                        }
118
                        
119
                } catch (Exception e) {
120
                        throw new NotSupportedExtensionException("Extension not supported");
121
                }
122
        }
123

    
124
        /**
125
         * Carga un ECW.
126
         * @param fname
127
         */
128
        public GeoInfo load() {
129
                ownTransformation = new AffineTransform(file.cellIncrementX, 0, 0, file.cellIncrementY, file.originX, file.originY);
130
                externalTransformation = (AffineTransform) ownTransformation.clone();
131
                return this;
132
        }
133
        
134
        /**
135
         * Cierra el fichero ECW
136
         */
137
        public void close() {
138
                if(file != null){
139
                        file.close(true);
140
                        file = null;
141
                }
142
        }
143

    
144
        /**
145
         * Obtiene el objeto que contiene el estado de la transparencia
146
         */
147
        public Transparency getTransparencyDatasetStatus() {
148
                if(fileTransparency == null)
149
                        fileTransparency = new Transparency();
150
                return fileTransparency;
151
        }
152
        
153
        /**
154
         * Devuelve el ancho de la imagen
155
         */
156
        public int getWidth() {
157
                return file.width;
158
        }
159
        
160
        /**
161
         * Devuelve el alto de la imagen
162
         */
163
        public int getHeight() {
164
                return file.height;
165
        }
166

    
167
        /**
168
         * Obtiene el extent de la ?ltima ventana seleccionada.
169
         * @return Extent
170
         */
171
        public Extent getView() {
172
                return viewRequest;
173
        }
174

    
175
        /*
176
         * (non-Javadoc)
177
         * @see org.gvsig.fmap.driver.RasterDataset#setView(org.gvsig.fmap.raster.Extent)
178
         */
179
        public void setView(Extent e) {
180
                viewRequest = new Extent(e);
181
        }
182

    
183
        /**
184
         * Cuando se hace una petici?n de carga de buffer la extensi?n pedida puede estar ajustada a la extensi?n del raster
185
         * o no estarlo. En caso de no estarlo los pixeles del buffer que caen fuera de la extensi?n del raster tendr?n valor
186
         * de NoData. Esta funci?n calcula en que pixel del buffer hay que empezar a escribir en caso de que este sea mayor
187
         * que los datos a leer.
188
         * @param dWorldTLX Posici?n X superior izquierda en coord reales
189
         * @param dWorldTLY Posici?n Y superior izquierda en coord reales
190
         * @param dWorldBRX Posici?n X inferior derecha en coord reales
191
         * @param dWorldBRY Posici?n Y inferior derecha en coord reales
192
         * @param nWidth Ancho en pixeles del buffer
193
         * @param nHeight Alto en pixeles del buffer
194
         * @return desplazamiento dentro del buffer en X e Y
195
         */
196
        private int[] calcStepBuffer(Extent dataExtent, int nWidth, int nHeight, int[] stpBuffer) {
197
                Extent imageExtent = getExtentWithoutRot();
198
                Extent ajustDataExtent = RasterUtilities.calculateAdjustedView(dataExtent, imageExtent);
199
                if(!RasterUtilities.compareExtents(dataExtent, ajustDataExtent)) {
200
                        Point2D p1 = worldToRaster(new Point2D.Double(ajustDataExtent.minX(), ajustDataExtent.maxY()));
201
                        Point2D p2 = worldToRaster(new Point2D.Double(ajustDataExtent.maxX(), ajustDataExtent.minY()));
202
                        Point2D p3 = worldToRaster(new Point2D.Double(dataExtent.minX(), dataExtent.maxY()));
203
                        Point2D p4 = worldToRaster(new Point2D.Double(dataExtent.maxX(), dataExtent.minY()));
204
                        //Ese es el ancho y alto q tendr?a el buffer en caso de haberse ajustado
205
                        int w = (int)Math.abs(Math.ceil(p2.getX()) - Math.floor(p1.getX()));
206
                        int h = (int)Math.abs(Math.floor(p1.getY()) - Math.ceil(p2.getY()));
207
                        
208
                        stpBuffer[0] = (int)(p1.getX() + (-p3.getX()));
209
                        stpBuffer[1] = (int)(p1.getY() + (-p3.getY()));
210
                        stpBuffer[2] = stpBuffer[0] + w;
211
                        stpBuffer[3] = stpBuffer[1] + h;
212
                        return new int[]{w, h};
213
                }
214
                return new int[]{nWidth, nHeight};
215
        }
216

    
217
        /*
218
         * (non-Javadoc)
219
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(double, double, double, double, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer, boolean)
220
         */
221
        public IBuffer getWindowRaster(double x, double y, double w, double h, BandList bandList, IBuffer rasterBuf, boolean adjustToExtent) {
222
                Point2D p1 = new Point2D.Double(x, y);
223
                Point2D p2 = new Point2D.Double(x + w, y - h);
224
                try {
225
                        externalTransformation.inverseTransform(p1, p1);
226
                        externalTransformation.inverseTransform(p2, p2);
227
                        ownTransformation.transform(p1, p1);
228
                        ownTransformation.transform(p2, p2);
229
                } catch (NoninvertibleTransformException e) {
230
                        e.printStackTrace();
231
                }
232

    
233
                Extent selectedExtent = new Extent(p1.getX(), p1.getY(), p2.getX(), p2.getY());
234
                
235
                setView(selectedExtent);
236
                int wPx = rasterBuf.getWidth();
237
                int hPx = rasterBuf.getHeight();
238
                int[] stpBuffer = new int[]{0, 0 , wPx, hPx};
239

    
240
                //TODO: FUNCIONALIDAD: Implementar adjustToExtent = false
241
                /*if(!adjustToExtent){
242
                                         int[] wh = calcStepBuffer(selectedExtent, wPx, hPx, stpBuffer);
243
                                         if(x < 0)
244
                                                 x  = 0;
245
                                         if(y < 0)
246
                                                 y  = 0;
247
                                         readData(buf, bandList, x, y, wh[0], wh[1], wh[0], wh[1], 0, 0, stpBuffer);
248
                                         return;
249
                        }*/
250

    
251
                loadBuffer(viewRequest, wPx, hPx, rasterBuf, bandList, stpBuffer);
252

    
253
                return rasterBuf;
254
        }
255

    
256
        /*
257
         * (non-Javadoc)
258
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(int, int, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer)
259
         */
260
        public IBuffer getWindowRaster(int x, int y, int w, int h, BandList bandList, IBuffer rasterBuf) {
261
                Point2D init = this.rasterToWorld(new Point2D.Double(x, y));
262
                Point2D end = this.rasterToWorld(new Point2D.Double(x + w, y + h));
263
                Extent selectedExtent = new Extent(init.getX(), init.getY(), end.getX(), end.getY());
264
                setView(selectedExtent);
265
                int[] stpBuffer = new int[]{0, 0 , w, h};
266

    
267
                loadBuffer(viewRequest, w, h, rasterBuf, bandList, stpBuffer);
268
                return rasterBuf;
269
        }
270

    
271
        /*
272
         * (non-Javadoc)
273
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer, boolean)
274
         */
275
        public IBuffer getWindowRaster(double minX, double minY, double maxX, double maxY, int bufWidth, int bufHeight, BandList bandList, IBuffer rasterBuf, boolean adjustToExtent) {
276
                Point2D p1 = new Point2D.Double(minX, maxY);
277
                Point2D p2 = new Point2D.Double(maxX, minY);
278
                try {
279
                        externalTransformation.inverseTransform(p1, p1);
280
                        externalTransformation.inverseTransform(p2, p2);
281
                        ownTransformation.transform(p1, p1);
282
                        ownTransformation.transform(p2, p2);
283
                } catch (NoninvertibleTransformException e) {
284
                        e.printStackTrace();
285
                }
286
                Extent selectedExtent = new Extent(p1, p2);
287
                setView(selectedExtent);
288
                int[] stpBuffer = new int[]{0, 0 , bufWidth, bufHeight};
289

    
290
                //TODO: FUNCIONALIDAD: Implementar adjustToExtent = false
291

    
292
                loadBuffer(viewRequest, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
293
                return rasterBuf;
294
        }
295

    
296
        /*
297
         * (non-Javadoc)
298
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(int, int, int, int, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer)
299
         */
300
        public IBuffer getWindowRaster(int x, int y, int w, int h, int bufWidth, int bufHeight, BandList bandList, IBuffer rasterBuf) {
301
                Point2D init = this.rasterToWorld(new Point2D.Double(x, y));
302
                Point2D end = this.rasterToWorld(new Point2D.Double(x + w, y + h));
303
                Extent selectedExtent = new Extent(init.getX(), init.getY(), end.getX(), end.getY());
304
                setView(selectedExtent);
305
                int[] stpBuffer = new int[]{0, 0 , bufWidth, bufHeight};
306

    
307
                loadBuffer(viewRequest, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
308
                return rasterBuf;
309
        }
310

    
311
        /**
312
         * Carga el buffer con las bandas RGB del raster con los par?metros especificados de extensi?n
313
         * y tama?o de buffer. El problema de ecw es que solo podemos leer 3 bandas de una vez ya que solo disponemos
314
         * de una llamada readLineRGBA. Para leer m?s bandas tendremos que hacer multiples llamadas a setView para leer
315
         * 3 cada vez.
316
         *
317
         * Si por ejemplo tenemos un ecw de 6 bandas [0, 1, 2, 3, 4, 5] y queremos cargar un buffer con el siguiente orden
318
         * [0, -, 2, -, 4, -] La variable readBandsFromECW har? la llamada a setView con los valores [0, 2, 4, 0, 0, 0]. La
319
         * funci?n drawRGB ser? la encargada de hacer el switch para obtener [0, -, 2, -, 4, -].
320
         *
321
         * Bug#1: Si se dispone de un ecw de m?s de tres bandas podemos llamar a setView con readBandsFromECW con el orden
322
         * que queramos, por ejemplo [3, 2, 5, 1, 0] pero para ecw de 3 bandas la llamada con las bandas cambiadas no
323
         * hace caso. El caso de readBandsFromECW = [2, 0, 1] ser? tomado siempre como [0, 1, 2].
324
         *
325
         * @param selectedExtent Extensi?n seleccionada
326
         * @param bufWidth Ancho de buffer
327
         * @param bufHeight Alto de buffer
328
         * @param rasterBuf Buffer de datos
329
         */
330
        private void loadBuffer(Extent selectedExtent, int bufWidth, int bufHeight, IBuffer rasterBuf, BandList bandList, int[] stpBuffer){
331
                try{
332
                        //Leemos el raster desde la librer?a
333
                        
334
                        Extent ext = new Extent(file.originX, file.originY, file.originX + (file.width * file.cellIncrementX), file.originY + (file.height * file.cellIncrementY));
335
                        selectedExtent = RasterUtilities.calculateAdjustedView(selectedExtent, ext);
336
                        int[] readBandsFromECW = new int[file.numBands];
337
                        int[] readBands = new int[file.numBands];
338
                        
339
                        
340
                        for(int i = 0; i < readBandsFromECW.length; i ++)
341
                                readBands[i] = -1;
342
                        int cont = 0;
343
                        for(int i = 0; i < file.numBands; i++) {
344
                                int[] bandsToDraw = bandList.getBand(i).getBufferBandListToDraw();
345
                                if(bandsToDraw != null) {
346
                                        for(int j = 0; j < bandsToDraw.length; j++){
347
                                                readBandsFromECW[cont] = i;
348
                                                readBands[cont] = i;
349
                                                cont ++;
350
                                        }
351
                                }
352
                                
353
                        }
354
                        
355
                        if(readCancel) {
356
                                readCancel = false;
357
                                return;
358
                        }
359
                        
360
                        file.setView(file.numBands, readBandsFromECW, selectedExtent.minX(), selectedExtent.maxY(), selectedExtent.maxX(), selectedExtent.minY(), bufWidth, bufHeight);
361
                        
362
                        //Escribimos el raster sobre un IBuffer
363
                        int[] pRGBArray = new int[bufWidth];
364
                        drawRGB(rasterBuf, pRGBArray, readBands, bandList);
365
                        
366
                }catch(JNCSInvalidSetViewException exc){
367
                        exc.printStackTrace();
368
                }catch (JNCSFileNotOpenException e) {
369
                        e.printStackTrace();
370
                }catch (JNCSException ex) {
371
                        ex.printStackTrace();
372
                }
373
                
374
        }
375

    
376

    
377
        private void drawRGB(IBuffer rasterBuf, int[] pRGBArray, int[] readBands, BandList bandList)throws JNCSException{
378
                int bandR = readBands[0];
379
                int bandG = (readBands.length > 1) ? readBands[1] : -1;
380
                int bandB = (readBands.length > 2) ? readBands[2] : -1;
381

    
382
                //********* caso especial que resuelve Bug#1 **********************
383
                if(file.numBands == 3 && bandList.getDrawableBandsCount() < 3) {
384
                        for(int i = 0; i < 3; i ++){
385
                                int[] b = bandList.getBand(i).getBufferBandListToDraw();
386
                                if(b != null){
387
                                        bandG = 1; bandR = 0; bandB = 2;
388
                                }
389
                        }
390
                }
391
                if(file.numBands == 3 && bandR == bandG && bandG == bandB) { //caso especial que resuelve Bug#1
392
                        for(int i = 0; i < 3; i ++) {
393
                                int[] b = bandList.getBand(i).getBufferBandListToDraw();
394
                                if(b != null){
395
                                        if(i == 0){
396
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
397
                                                                                file.readLineRGBA(pRGBArray);
398
                                                                                for(int col = 0; col < pRGBArray.length; col ++){
399
                                                                                        rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
400
                                                                                        rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
401
                                                                                        rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
402
                                                                                }
403
                                                                }
404
                                                return;
405
                                        }
406
                                        if(i == 1) {
407
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
408
                                                                                file.readLineRGBA(pRGBArray);
409
                                                                                for(int col = 0; col < pRGBArray.length; col ++){
410
                                                                                        rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
411
                                                                                        rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
412
                                                                                        rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
413
                                                                                }
414
                                                                }
415
                                                return;
416
                                        }
417
                                        if(i == 2) {
418
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
419
                                                                                file.readLineRGBA(pRGBArray);
420
                                                                                for(int col = 0; col < pRGBArray.length; col ++){
421
                                                                                        rasterBuf.setElem(line, col, bandR, (byte)(pRGBArray[col] & 0x000000ff));
422
                                                                                        rasterBuf.setElem(line, col, bandG, (byte)(pRGBArray[col] & 0x000000ff));
423
                                                                                        rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
424
                                                                                }
425
                                                                }
426
                                                return;
427
                                        }
428
                                }
429
                                if(readCancel) {
430
                                        readCancel = false;
431
                                        return;
432
                                }
433
                        }
434

    
435
                }
436
                //********* END caso especial que resuelve Bug#1 **********************
437

    
438
                if(bandR >= 0 && bandG >= 0 && bandB >= 0) {
439
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
440
                                                        file.readLineRGBA(pRGBArray);
441
                                                        for(int col = 0; col < pRGBArray.length; col ++){
442
                                                                rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
443
                                                                rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
444
                                                                rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
445
                                                        }
446
                                        }
447
                        return;
448
                }
449

    
450
                if(readCancel) {
451
                        readCancel = false;
452
                        return;
453
                }
454

    
455
                if(bandR >= 0 && bandG >= 0) {
456
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
457
                                                        file.readLineRGBA(pRGBArray);
458
                                                        for(int col = 0; col < pRGBArray.length; col ++){
459
                                                                rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
460
                                                                rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
461
                                                        }
462
                                        }
463
                        return;
464
                }
465

    
466
                if(readCancel) {
467
                        readCancel = false;
468
                        return;
469
                }
470

    
471
                if(bandR >= 0){
472
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
473
                                                        file.readLineRGBA(pRGBArray);
474
                                                        for(int col = 0; col < pRGBArray.length; col ++)
475
                                                                rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
476
                                        }
477
                        return;
478
                }
479

    
480
                if(readCancel) {
481
                        readCancel = false;
482
                        return;
483
                }
484

    
485
        }
486

    
487
        public void reProject(ICoordTrans rp) {
488
        }
489

    
490
        /*
491
         * (non-Javadoc)
492
         * @see org.gvsig.raster.dataset.RasterDataset#getBlockSize()
493
         */
494
        public int getBlockSize() {
495
                return 0;
496
        }
497

    
498
        /*
499
         * (non-Javadoc)
500
         * @see org.gvsig.raster.driver.RasterDataset#readCompletetLine(int, int)
501
         */
502
        public Object readCompleteLine(int line, int band) throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
503
                if(line < 0 || line >= file.height || band < 0 || band >= getBandCount())
504
                        throw new InvalidSetViewException("Request out of grid");
505

    
506
                Point2D begin = rasterToWorld(new Point2D.Double(0, line));
507
                Point2D end = rasterToWorld(new Point2D.Double(file.width, line + 1));
508
                int[] readBandsFromECW = new int[file.numBands];
509
                if(file.numBands <= 3) {
510
                        for(int i = 0; i < file.numBands; i++)
511
                                readBandsFromECW[i] = i;
512
                }else {
513
                        readBandsFromECW[0] = band;
514
                }
515

    
516
                Extent e = new Extent(begin.getX(), begin.getY(), end.getX(), end.getY());
517

    
518
                try {
519
                        int[] value = new int[file.width];
520
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, 1);
521
                        file.readLineRGBA(value);
522

    
523
                        if(file.numBands <= 3) {
524
                                switch(getDataType()) {
525
                                case IBuffer.TYPE_BYTE: byte[] b = new byte[file.width];
526
                                                                                switch(band) {
527
                                                                                case 0: for(int i = 0; i < file.width; i ++)
528
                                                                                                        b[i] = (byte)(((value[i] & 0x00ff0000) >> 16) & 0xff);
529
                                                                                                break;
530
                                                                                case 1: for(int i = 0; i < file.width; i ++)
531
                                                                                                        b[i] = (byte)(((value[i] & 0x0000ff00) >> 8) & 0xff);
532
                                                                                                break;
533
                                                                                case 2: for(int i = 0; i < file.width; i ++)
534
                                                                                                        b[i] = (byte)((value[i] & 0x000000ff) & 0xff);
535
                                                                                                break;
536
                                                                                }
537
                                                                                return b;
538
                                }
539
                        }else {
540
                                switch(getDataType()) {
541
                                case IBuffer.TYPE_BYTE: byte[] b = new byte[file.width];
542
                                                                                for(int i = 0; i < file.width; i ++)
543
                                                                                        b[i] = (byte)(((value[i] & 0x00ff0000) >> 16) & 0xff);
544
                                                                                break;
545
                                }
546
                        }
547
                        //TODO: FUNCIONALIDAD: Ecw con otro tipo de dato != Byte
548
                } catch (JNCSFileNotOpenException e1) {
549
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
550
                } catch (JNCSInvalidSetViewException e1) {
551
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
552
                } catch (JNCSException e1) {
553
                        throw new RasterDriverException("Error la lectura de datos ecw");
554
                }
555

    
556
                return null;
557
        }
558

    
559
        /*
560
         *  (non-Javadoc)
561
         * @see org.gvsig.raster.dataset.RasterDataset#readBlock(int, int, int)
562
         */
563
        public Object readBlock(int pos, int blockHeight) throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
564
                if(pos < 0)
565
                        throw new InvalidSetViewException("Request out of grid");
566

    
567
                if((pos + blockHeight) > file.height)
568
                        blockHeight = Math.abs(file.height - pos);
569

    
570
                Point2D begin = rasterToWorld(new Point2D.Double(0, pos));
571
                Point2D end = rasterToWorld(new Point2D.Double(file.width, pos + blockHeight));
572
                int[] readBandsFromECW = new int[file.numBands];
573

    
574
                for(int i = 0; i < file.numBands; i++)
575
                        readBandsFromECW[i] = i;
576

    
577
                byte[][][] buf = new byte[file.numBands][blockHeight][file.width];
578
                Extent e = new Extent(begin.getX(), begin.getY(), end.getX(), end.getY());
579
                e = RasterUtilities.calculateAdjustedView(getExtent(), e);
580

    
581
                try {
582
                        int[] value = new int[file.width];
583
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, blockHeight);
584

    
585
                        if(file.numBands <= 3) {
586
                                for (int row = 0; row < blockHeight; row++) {
587
                                        file.readLineRGBA(value);
588
                                        switch(getDataType()) {
589
                                        case IBuffer.TYPE_BYTE:
590
                                                for(int col = 0; col < file.width; col ++) {
591
                                                        buf[0][row][col] = (byte)(((value[col] & 0x00ff0000) >> 16) & 0xff);
592
                                                        buf[1][row][col] = (byte)(((value[col] & 0x0000ff00) >> 8) & 0xff);
593
                                                        buf[2][row][col] = (byte)((value[col] & 0x000000ff) & 0xff);
594
                                                }
595
                                                break;
596
                                        }
597
                                }
598

    
599
                                if(readCancel) {
600
                                        readCancel = false;
601
                                        return null;
602
                                }
603

    
604
                        } else {
605
                                //TODO: FUNCIONALIDAD: file.numBands > 3
606
                        }
607

    
608
                        //TODO: FUNCIONALIDAD: Ecw con otro tipo de dato != Byte
609
                } catch (JNCSFileNotOpenException e1) {
610
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
611
                } catch (JNCSInvalidSetViewException e1) {
612
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
613
                } catch (JNCSException e1) {
614
                        throw new RasterDriverException("Error la lectura de datos ecw");
615
                }
616

    
617
                return buf;
618
        }
619

    
620
        /*
621
         * (non-Javadoc)
622
         * @see org.gvsig.raster.driver.RasterDataset#getData(int, int, int)
623
         */
624
        public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
625
                if(x < 0 || y < 0 || x >= file.width || y >= file.height)
626
                        throw new InvalidSetViewException("Request out of grid");
627

    
628
                Point2D begin = rasterToWorld(new Point2D.Double(x, y));
629
                Point2D end = rasterToWorld(new Point2D.Double(x + 1, y + 1));
630
                int[] readBandsFromECW = new int[file.numBands];
631
                if(file.numBands <= 3){
632
                        for(int i = 0; i < file.numBands; i++)
633
                                readBandsFromECW[i] = i;
634
                }else{
635
                        readBandsFromECW[0] = band;
636
                }
637

    
638
                Extent e = new Extent(begin.getX(), begin.getY(), end.getX(), end.getY());
639
                try {
640
                        int[] value = new int[1];
641
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), 1, 1);
642
                        file.readLineRGBA(value);
643
                        if(file.numBands <= 3){
644
                                switch(band){
645
                                case 0: return new Integer((((value[0] & 0x00ff0000) >> 16) & 0xffffffff));
646
                                case 1: return new Integer((((value[0] & 0x0000ff00) >> 8) & 0xffffffff));
647
                                case 2: return new Integer((((value[0] & 0x000000ff)) & 0xffffffff));
648
                                }
649
                        }
650
                        return new Integer((((value[0] & 0x00ff0000) >> 16) & 0xffffffff));
651
                } catch (JNCSFileNotOpenException e1) {
652
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
653
                } catch (JNCSInvalidSetViewException e1) {
654
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
655
                } catch (JNCSException e1) {
656
                        throw new RasterDriverException("Error reading ecw data");
657
                }
658
        }
659

    
660
        public void refreshUpdate(int arg0, int arg1, double arg2, double arg3, double arg4, double arg5) {
661
        }
662

    
663
        public void refreshUpdate(int arg0, int arg1, int arg2, int arg3, int arg4, int arg5) {
664
        }
665

    
666
        /*
667
         * (non-Javadoc)
668
         * @see org.gvsig.raster.driver.GeoData#getStringProjection()
669
         */
670
        public String getStringProjection() throws RasterDriverException{
671
                return file.projection;
672
        }
673

    
674
        /*
675
         *  (non-Javadoc)
676
         * @see org.gvsig.raster.dataset.RasterDataset#setCanceled(boolean, int)
677
         */
678
        public void setCanceled(boolean value, int process) {
679
                super.setCanceled(value, process);
680

    
681
                if(process == CANCEL_READ || process == 0)
682
                        readCancel = value;
683
        }
684

    
685
        /*
686
         *  (non-Javadoc)
687
         * @see org.gvsig.raster.dataset.RasterDataset#isCanceled(int)
688
         */
689
        public boolean isCanceled(int process) {
690
                if(process == CANCEL_READ) {
691
                        return readCancel;
692
                } else
693
                        return super.isCanceled(process);
694
        }
695

    
696
        /*
697
         * (non-Javadoc)
698
         * @see org.gvsig.raster.driver.GeoData#getStringProjection()
699
         */
700
        public String getWktProjection() throws RasterDriverException{
701
                //System.err.println("======>" + file.projection);
702
                return null;
703
        }
704

    
705
}
706

    
707

    
708