Statistics
| Revision:

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

History | View | Annotate | Download (28.9 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.BandAccessException;
30
import org.gvsig.raster.dataset.BandList;
31
import org.gvsig.raster.dataset.FileNotOpenException;
32
import org.gvsig.raster.dataset.GeoInfo;
33
import org.gvsig.raster.dataset.IBuffer;
34
import org.gvsig.raster.dataset.InvalidSetViewException;
35
import org.gvsig.raster.dataset.NotSupportedExtensionException;
36
import org.gvsig.raster.dataset.RasterDataset;
37
import org.gvsig.raster.dataset.io.rmf.ParsingException;
38
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation;
39
import org.gvsig.raster.datastruct.Extent;
40
import org.gvsig.raster.datastruct.Transparency;
41
import org.gvsig.raster.process.RasterTask;
42
import org.gvsig.raster.process.RasterTaskQueue;
43
import org.gvsig.raster.util.RasterUtilities;
44
import org.gvsig.raster.util.extensionPoints.ExtensionPoint;
45

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

    
58
        private JNCSFile                                       file = null;
59

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

    
65
        /**
66
         * Extent de la ventana seleccionada
67
         */
68
        private Extent                        viewRequest = null;
69
        
70
        private DatasetColorInterpretation    colorInterpr = null;
71

    
72
        public static void register() {
73
                ExtensionPoint point = ExtensionPoint.getExtensionPoint("RasterReader");
74
                point.register("ecw", ErmapperDriver.class);
75
                point.register("jp2", ErmapperDriver.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 ErmapperDriver(IProjection proj, Object param)throws NotSupportedExtensionException {
101
                super(proj, ((String)param));
102
                setParam(param);
103
                try {
104
                        
105
                        if (!new File(((String)param)).exists() && !((String)param).startsWith("ecwp:"))
106
                                throw new NotSupportedExtensionException("Extension not supported");
107
                        
108
                        file = new JNCSFile(((String)param), false);
109
                        load();
110
                        bandCount = file.numBands;
111
                        getTransparencyDatasetStatus();
112
                
113
                        int[] dt = new int[bandCount];
114
                        for (int i = 0; i < bandCount; i++) 
115
                                dt[i] = IBuffer.TYPE_BYTE;
116
                        setDataType(dt);
117
                                        
118
                        super.init();
119
                        
120
                        try {
121
                                loadFromRmf(getRmfBlocksManager());
122
                        } catch (ParsingException e) {
123
                                //No lee desde rmf
124
                        }
125
                        
126
                } catch (Exception e) {
127
                        throw new NotSupportedExtensionException("Extension not supported");
128
                }
129
        }
130

    
131
        /**
132
         * Carga un ECW.
133
         * @param fname
134
         */
135
        public GeoInfo load() {
136
                ownTransformation = new AffineTransform(file.cellIncrementX, 0, 0, file.cellIncrementY, file.originX, file.originY);
137
                externalTransformation = (AffineTransform) ownTransformation.clone();
138
                return this;
139
        }
140
        
141
        /**
142
         * Cierra el fichero ECW
143
         */
144
        public void close() {
145
                if(file != null) {
146
                        file.close(true);
147
                        file = null;
148
                }
149
        }
150

    
151
        /**
152
         * Obtiene el objeto que contiene el estado de la transparencia
153
         */
154
        public Transparency getTransparencyDatasetStatus() {
155
                if(fileTransparency == null)
156
                        fileTransparency = new Transparency();
157
                return fileTransparency;
158
        }
159
        
160
        /**
161
         * Devuelve el ancho de la imagen
162
         */
163
        public int getWidth() {
164
                return file.width;
165
        }
166
        
167
        /**
168
         * Devuelve el alto de la imagen
169
         */
170
        public int getHeight() {
171
                return file.height;
172
        }
173

    
174
        /**
175
         * Obtiene el extent de la ?ltima ventana seleccionada.
176
         * @return Extent
177
         */
178
        public Extent getView() {
179
                return viewRequest;
180
        }
181

    
182
        /*
183
         * (non-Javadoc)
184
         * @see org.gvsig.fmap.driver.RasterDataset#setView(org.gvsig.fmap.raster.Extent)
185
         */
186
        public void setView(Extent e) {
187
                viewRequest = new Extent(e);
188
        }
189

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

    
224
        /*
225
         * (non-Javadoc)
226
         * @see org.gvsig.raster.dataset.RasterDataset#getWindowRaster(double, double, double, double, org.gvsig.raster.dataset.BandList, org.gvsig.raster.dataset.IBuffer)
227
         */
228
        public IBuffer getWindowRaster(double ulx, double uly, double lrx, double lry, BandList bandList, IBuffer rasterBuf) throws InterruptedException, RasterDriverException {                
229
                Point2D p1 = new Point2D.Double(ulx, uly);
230
                Point2D p2 = new Point2D.Double(lrx, lry);
231
                try {
232
                        externalTransformation.inverseTransform(p1, p1);
233
                        externalTransformation.inverseTransform(p2, p2);
234
                        ownTransformation.transform(p1, p1);
235
                        ownTransformation.transform(p2, p2);
236
                } catch (NoninvertibleTransformException e) {
237
                        throw new RasterDriverException("Noninvertible transform");
238
                }
239

    
240
                Extent selectedExtent = new Extent(p1.getX(), p1.getY(), p2.getX(), p2.getY());
241
                
242
                setView(selectedExtent);
243
                int wPx = rasterBuf.getWidth();
244
                int hPx = rasterBuf.getHeight();
245
                int[] stpBuffer = new int[]{0, 0 , wPx, hPx};
246

    
247
                loadBuffer(viewRequest, wPx, hPx, rasterBuf, bandList, stpBuffer);
248

    
249
                return rasterBuf;
250
        }
251
        
252
        /*
253
         * (non-Javadoc)
254
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(double, double, double, double, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer, boolean)
255
         */
256
        public IBuffer getWindowRaster(double ulx, double uly, double w, double h, BandList bandList, IBuffer rasterBuf, boolean adjustToExtent) throws InterruptedException, RasterDriverException {
257
                //El incremento o decremento de las X e Y depende de los signos de rotaci?n y escala en la matriz de transformaci?n. Por esto
258
                //tenemos que averiguar si lrx es x + w o x -w, asi como si lry es y + h o y - h 
259
                Extent ext = getExtent();
260
                Point2D pInit = rasterToWorld(new Point2D.Double(0, 0));
261
                Point2D pEnd = rasterToWorld(new Point2D.Double(getWidth(), getHeight()));
262
                double wRaster = Math.abs(pEnd.getX() - pInit.getX());
263
                double hRaster = Math.abs(pEnd.getY() - pInit.getY());
264
                double lrx = (((ext.getULX() - wRaster) > ext.maxX()) || ((ext.getULX() - wRaster) < ext.minX())) ? (ulx + w) : (ulx - w);
265
                double lry = (((ext.getULY() - hRaster) > ext.maxY()) || ((ext.getULY() - hRaster) < ext.minY())) ? (uly + h) : (uly - h); 
266
                
267
                Point2D p1 = new Point2D.Double(ulx, uly);
268
                Point2D p2 = new Point2D.Double(lrx, lry);
269
                try {
270
                        externalTransformation.inverseTransform(p1, p1);
271
                        externalTransformation.inverseTransform(p2, p2);
272
                        ownTransformation.transform(p1, p1);
273
                        ownTransformation.transform(p2, p2);
274
                } catch (NoninvertibleTransformException e) {
275
                        throw new RasterDriverException("Noninvertible transform");
276
                }
277

    
278
                Extent selectedExtent = new Extent(p1.getX(), p1.getY(), p2.getX(), p2.getY());
279
                
280
                setView(selectedExtent);
281
                int wPx = rasterBuf.getWidth();
282
                int hPx = rasterBuf.getHeight();
283
                int[] stpBuffer = new int[]{0, 0 , wPx, hPx};
284

    
285
                //TODO: FUNCIONALIDAD: Implementar adjustToExtent = false
286
                /*if(!adjustToExtent){
287
                                         int[] wh = calcStepBuffer(selectedExtent, wPx, hPx, stpBuffer);
288
                                         if(x < 0)
289
                                                 x  = 0;
290
                                         if(y < 0)
291
                                                 y  = 0;
292
                                         readData(buf, bandList, x, y, wh[0], wh[1], wh[0], wh[1], 0, 0, stpBuffer);
293
                                         return;
294
                        }*/
295

    
296
                loadBuffer(viewRequest, wPx, hPx, rasterBuf, bandList, stpBuffer);
297

    
298
                return rasterBuf;
299
        }
300

    
301
        /*
302
         * (non-Javadoc)
303
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(int, int, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer)
304
         */
305
        public IBuffer getWindowRaster(int x, int y, int w, int h, BandList bandList, IBuffer rasterBuf) throws InterruptedException, RasterDriverException {
306
                Point2D init = this.rasterToWorld(new Point2D.Double(x, y));
307
                Point2D end = this.rasterToWorld(new Point2D.Double(x + w, y + h));
308
                Extent selectedExtent = new Extent(init.getX(), init.getY(), end.getX(), end.getY());
309
                setView(selectedExtent);
310
                int[] stpBuffer = new int[]{0, 0 , w, h};
311

    
312
                loadBuffer(viewRequest, w, h, rasterBuf, bandList, stpBuffer);
313
                return rasterBuf;
314
        }
315

    
316
        /*
317
         * (non-Javadoc)
318
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer, boolean)
319
         */
320
        public IBuffer getWindowRaster(double ulx, double uly, double lrx, double lry, int bufWidth, int bufHeight, BandList bandList, IBuffer rasterBuf, boolean adjustToExtent) throws InterruptedException, RasterDriverException {
321
                Point2D p1 = new Point2D.Double(ulx, uly);
322
                Point2D p2 = new Point2D.Double(lrx, lry);
323
                try {
324
                        externalTransformation.inverseTransform(p1, p1);
325
                        externalTransformation.inverseTransform(p2, p2);
326
                        ownTransformation.transform(p1, p1);
327
                        ownTransformation.transform(p2, p2);
328
                } catch (NoninvertibleTransformException e) {
329
                        throw new RasterDriverException("Noninvertible transform");
330
                }
331
                Extent selectedExtent = new Extent(p1, p2);
332
                setView(selectedExtent);
333
                int[] stpBuffer = new int[]{0, 0 , bufWidth, bufHeight};
334

    
335
                //TODO: FUNCIONALIDAD: Implementar adjustToExtent = false
336

    
337
                loadBuffer(viewRequest, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
338
                return rasterBuf;
339
        }
340

    
341
        /*
342
         * (non-Javadoc)
343
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(int, int, int, int, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer)
344
         */
345
        public IBuffer getWindowRaster(int x, int y, int w, int h, int bufWidth, int bufHeight, BandList bandList, IBuffer rasterBuf) throws InterruptedException, RasterDriverException {
346
                Point2D init = this.rasterToWorld(new Point2D.Double(x, y));
347
                Point2D end = this.rasterToWorld(new Point2D.Double(x + w, y + h));
348
                Extent selectedExtent = new Extent(init.getX(), init.getY(), end.getX(), end.getY());
349
                setView(selectedExtent);
350
                int[] stpBuffer = new int[]{0, 0 , bufWidth, bufHeight};
351

    
352
                loadBuffer(viewRequest, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
353
                return rasterBuf;
354
        }
355

    
356
        /**
357
         * Carga el buffer con las bandas RGB del raster con los par?metros especificados de extensi?n
358
         * y tama?o de buffer. El problema de ecw es que solo podemos leer 3 bandas de una vez ya que solo disponemos
359
         * de una llamada readLineRGBA. Para leer m?s bandas tendremos que hacer multiples llamadas a setView para leer
360
         * 3 cada vez.
361
         *
362
         * Si por ejemplo tenemos un ecw de 6 bandas [0, 1, 2, 3, 4, 5] y queremos cargar un buffer con el siguiente orden
363
         * [0, -, 2, -, 4, -] La variable readBandsFromECW har? la llamada a setView con los valores [0, 2, 4, 0, 0, 0]. La
364
         * funci?n drawRGB ser? la encargada de hacer el switch para obtener [0, -, 2, -, 4, -].
365
         *
366
         * Bug#1: Si se dispone de un ecw de m?s de tres bandas podemos llamar a setView con readBandsFromECW con el orden
367
         * que queramos, por ejemplo [3, 2, 5, 1, 0] pero para ecw de 3 bandas la llamada con las bandas cambiadas no
368
         * hace caso. El caso de readBandsFromECW = [2, 0, 1] ser? tomado siempre como [0, 1, 2].
369
         *
370
         * @param selectedExtent Extensi?n seleccionada
371
         * @param bufWidth Ancho de buffer
372
         * @param bufHeight Alto de buffer
373
         * @param rasterBuf Buffer de datos
374
         */
375
        private void loadBuffer(Extent selectedExtent, int bufWidth, int bufHeight, IBuffer rasterBuf, BandList bandList, int[] stpBuffer) throws InterruptedException, RasterDriverException {
376
                try{
377
                        RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
378
                        
379
                        //Leemos el raster desde la librer?a
380
                        
381
                        //Extent ext = getExtent();//new Extent(file.originX, file.originY, file.originX + (file.width * file.cellIncrementX), file.originY + (file.height * file.cellIncrementY));
382
                        //selectedExtent = RasterUtilities.calculateAdjustedView(selectedExtent, ext);
383
                        //selectedExtent = RasterUtilities.calculateAdjustedView(selectedExtent, this.getAffineTransform(), new Dimension(file.width, file.height));
384
                        int[] readBandsFromECW = new int[Math.max(file.numBands, 3)];
385
                        int[] readBands = new int[Math.max(file.numBands, 3)];
386
                        
387
                        
388
                        for(int i = 0; i < readBandsFromECW.length; i ++)
389
                                readBands[i] = -1;
390
                        int cont = 0;
391
                        for(int i = 0; i < file.numBands; i++) {
392
                                int[] bandsToDraw = bandList.getBand(i).getBufferBandListToDraw();
393
                                if(bandsToDraw != null) {
394
                                        for(int j = 0; j < bandsToDraw.length; j++){
395
                                                readBandsFromECW[cont] = i;
396
                                                readBands[cont] = i;
397
                                                cont ++;
398
                                        }
399
                                }
400
                                
401
                        }
402
                        
403
                        if(task.getEvent() != null)
404
                                task.manageEvent(task.getEvent());
405
                        
406
                        //Si el ancho y alto pedido no coincide con el estimado es que se intenta resamplear.
407
                        //Otros drivers soportan el resampleo pero no es el caso. Hay que hacer la petici?n y luego remuestrear.
408
                        
409
                        /*if((selectedExtent.width()/file.cellIncrementX) || (selectedExtent.height()/file.cellIncrementY)) {
410
                                
411
                        }*/
412
                        
413
                        if(bufWidth > Math.abs(selectedExtent.width()/file.cellIncrementX))
414
                                bufWidth = (int)Math.abs(selectedExtent.width()/file.cellIncrementX);
415
                        if(bufHeight > Math.abs(selectedExtent.height()/file.cellIncrementY))
416
                                bufHeight = (int)Math.abs(selectedExtent.height()/file.cellIncrementY);
417
                        file.setView(file.numBands, readBandsFromECW, selectedExtent.minX(), selectedExtent.maxY(), selectedExtent.maxX(), selectedExtent.minY(), bufWidth, bufHeight);
418
                                                
419
                        //Escribimos el raster sobre un IBuffer
420
                        int[] pRGBArray = new int[bufWidth];
421
                        drawRGB(rasterBuf, pRGBArray, readBandsFromECW, bandList, task);
422
                        System.gc();
423
                }catch(JNCSInvalidSetViewException exc){
424
                        throw new RasterDriverException("Error setting coords");
425
                }catch (JNCSFileNotOpenException e) {
426
                        throw new RasterDriverException("Error opening file");
427
                }catch (JNCSException ex) {
428
                        throw new RasterDriverException("Error reading data");
429
                }
430
                
431
        }
432

    
433

    
434
        private void drawRGB(IBuffer rasterBuf, int[] pRGBArray, int[] readBands, BandList bandList, RasterTask task) throws JNCSException, InterruptedException {
435
                int bandR = readBands[0];
436
                int bandG = (readBands.length > 1) ? readBands[1] : -1;
437
                int bandB = (readBands.length > 2) ? readBands[2] : -1;
438

    
439
                //********* caso especial que resuelve Bug#1 **********************
440
                if(file.numBands == 3 && bandList.getDrawableBandsCount() < 3) {
441
                        for(int i = 0; i < 3; i ++) {
442
                                int[] b = bandList.getBand(i).getBufferBandListToDraw();
443
                                if(b != null){
444
                                        bandG = 1; bandR = 0; bandB = 2;
445
                                }
446
                        }
447
                }
448
                if(file.numBands == 3 && bandR == bandG && bandG == bandB) { //caso especial que resuelve Bug#1
449
                        for(int i = 0; i < 3; i ++) {
450
                                int[] b = bandList.getBand(i).getBufferBandListToDraw();
451
                                if(b != null) {
452
                                        if(i == 0) {
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] & 0x00ff0000) >> 16));
457
                                                                rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
458
                                                                rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
459
                                                        }
460
                                                }
461
                                                return;
462
                                        }
463
                                        if(i == 1) {
464
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
465
                                                        file.readLineRGBA(pRGBArray);
466
                                                        for(int col = 0; col < pRGBArray.length; col ++){
467
                                                                rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
468
                                                                rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
469
                                                                rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
470
                                                        }
471
                                                }
472
                                                return;
473
                                        }
474
                                        if(i == 2) {
475
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
476
                                                        file.readLineRGBA(pRGBArray);
477
                                                        for(int col = 0; col < pRGBArray.length; col ++){
478
                                                                rasterBuf.setElem(line, col, bandR, (byte)(pRGBArray[col] & 0x000000ff));
479
                                                                rasterBuf.setElem(line, col, bandG, (byte)(pRGBArray[col] & 0x000000ff));
480
                                                                rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
481
                                                        }
482
                                                }
483
                                                return;
484
                                        }
485
                                }
486
                                if(task.getEvent() != null)
487
                                        task.manageEvent(task.getEvent());
488
                        }
489

    
490
                }
491
                //********* END caso especial que resuelve Bug#1 **********************
492

    
493
                if(bandR >= 0 && bandG >= 0 && bandB >= 0) {
494
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
495
                                file.readLineRGBA(pRGBArray);
496
                                for(int col = 0; col < pRGBArray.length; col ++){
497
                                        rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
498
                                        rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
499
                                        rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
500
                                }
501
                        }
502
                        return;
503
                }
504

    
505
                if(task.getEvent() != null)
506
                        task.manageEvent(task.getEvent());
507

    
508
                if(bandR >= 0 && bandG >= 0) {
509
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
510
                                file.readLineRGBA(pRGBArray);
511
                                for(int col = 0; col < pRGBArray.length; col ++){
512
                                        rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
513
                                        rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
514
                                }
515
                        }
516
                        return;
517
                }
518

    
519
                if(task.getEvent() != null)
520
                        task.manageEvent(task.getEvent());
521

    
522
                if(bandR >= 0){
523
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
524
                                file.readLineRGBA(pRGBArray);
525
                                for(int col = 0; col < pRGBArray.length; col ++)
526
                                        rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
527
                        }
528
                        return;
529
                }
530

    
531
                if(task.getEvent() != null)
532
                        task.manageEvent(task.getEvent());
533

    
534
        }
535

    
536
        public void reProject(ICoordTrans rp) {
537
        }
538

    
539
        /*
540
         * (non-Javadoc)
541
         * @see org.gvsig.raster.dataset.RasterDataset#getBlockSize()
542
         */
543
        public int getBlockSize() {
544
                return 0;
545
        }
546

    
547
        /*
548
         * (non-Javadoc)
549
         * @see org.gvsig.raster.driver.RasterDataset#readCompletetLine(int, int)
550
         */
551
        public Object readCompleteLine(int line, int band) throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
552
                if(line < 0 || line >= file.height || band < 0 || band >= getBandCount())
553
                        throw new InvalidSetViewException("Request out of grid");
554

    
555
                Point2D begin = rasterToWorld(new Point2D.Double(0, line));
556
                Point2D end = rasterToWorld(new Point2D.Double(file.width, line + 1));
557
                int[] readBandsFromECW = new int[file.numBands];
558
                if(file.numBands <= 3) {
559
                        for(int i = 0; i < file.numBands; i++)
560
                                readBandsFromECW[i] = i;
561
                }else {
562
                        readBandsFromECW[0] = band;
563
                }
564

    
565
                Extent e = new Extent(begin.getX(), begin.getY(), end.getX(), end.getY());
566

    
567
                try {
568
                        int[] value = new int[file.width];
569
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, 1);
570
                        file.readLineRGBA(value);
571

    
572
                        if(file.numBands <= 3) {
573
                                switch(getDataType()[0]) {
574
                                case IBuffer.TYPE_BYTE: byte[] b = new byte[file.width];
575
                                                                                switch(band) {
576
                                                                                case 0: for(int i = 0; i < file.width; i ++)
577
                                                                                                        b[i] = (byte)(((value[i] & 0x00ff0000) >> 16) & 0xff);
578
                                                                                                break;
579
                                                                                case 1: for(int i = 0; i < file.width; i ++)
580
                                                                                                        b[i] = (byte)(((value[i] & 0x0000ff00) >> 8) & 0xff);
581
                                                                                                break;
582
                                                                                case 2: for(int i = 0; i < file.width; i ++)
583
                                                                                                        b[i] = (byte)((value[i] & 0x000000ff) & 0xff);
584
                                                                                                break;
585
                                                                                }
586
                                                                                return b;
587
                                }
588
                        }else {
589
                                switch(getDataType()[0]) {
590
                                case IBuffer.TYPE_BYTE: byte[] b = new byte[file.width];
591
                                                                                for(int i = 0; i < file.width; i ++)
592
                                                                                        b[i] = (byte)(((value[i] & 0x00ff0000) >> 16) & 0xff);
593
                                                                                break;
594
                                }
595
                        }
596
                        //TODO: FUNCIONALIDAD: Ecw con otro tipo de dato != Byte
597
                } catch (JNCSFileNotOpenException e1) {
598
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
599
                } catch (JNCSInvalidSetViewException e1) {
600
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
601
                } catch (JNCSException e1) {
602
                        throw new RasterDriverException("Error la lectura de datos ecw");
603
                }
604

    
605
                return null;
606
        }
607

    
608
        /*
609
         *  (non-Javadoc)
610
         * @see org.gvsig.raster.dataset.RasterDataset#readBlock(int, int, int)
611
         */
612
        public Object readBlock(int pos, int blockHeight) throws InvalidSetViewException, FileNotOpenException, RasterDriverException, InterruptedException {
613
                RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
614
                if(pos < 0)
615
                        throw new InvalidSetViewException("Request out of grid");
616

    
617
                if((pos + blockHeight) > file.height)
618
                        blockHeight = Math.abs(file.height - pos);
619

    
620
                Point2D begin = rasterToWorld(new Point2D.Double(0, pos));
621
                Point2D end = rasterToWorld(new Point2D.Double(file.width, pos + blockHeight));
622
                int[] readBandsFromECW = new int[file.numBands];
623

    
624
                for(int i = 0; i < file.numBands; i++)
625
                        readBandsFromECW[i] = i;
626

    
627
                byte[][][] buf = new byte[file.numBands][blockHeight][file.width];
628
                Extent e = new Extent(begin.getX(), begin.getY(), end.getX(), end.getY());
629
                e = RasterUtilities.calculateAdjustedView(getExtent(), e);
630

    
631
                try {
632
                        int[] value = new int[file.width];
633
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, blockHeight);
634

    
635
                        if(file.numBands <= 3) {
636
                                for (int row = 0; row < blockHeight; row++) {
637
                                        file.readLineRGBA(value);
638
                                        switch(getDataType()[0]) {
639
                                        case IBuffer.TYPE_BYTE:
640
                                                for(int col = 0; col < file.width; col ++) {
641
                                                        buf[0][row][col] = (byte)(((value[col] & 0x00ff0000) >> 16) & 0xff);
642
                                                        buf[1][row][col] = (byte)(((value[col] & 0x0000ff00) >> 8) & 0xff);
643
                                                        buf[2][row][col] = (byte)((value[col] & 0x000000ff) & 0xff);
644
                                                }
645
                                                break;
646
                                        }
647
                                }
648

    
649
                                if(task.getEvent() != null)
650
                                        task.manageEvent(task.getEvent());
651

    
652
                        } else {
653
                                //TODO: FUNCIONALIDAD: file.numBands > 3
654
                        }
655

    
656
                        //TODO: FUNCIONALIDAD: Ecw con otro tipo de dato != Byte
657
                } catch (JNCSFileNotOpenException e1) {
658
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
659
                } catch (JNCSInvalidSetViewException e1) {
660
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
661
                } catch (JNCSException e1) {
662
                        throw new RasterDriverException("Error la lectura de datos ecw");
663
                }
664

    
665
                return buf;
666
        }
667

    
668
        /*
669
         * (non-Javadoc)
670
         * @see org.gvsig.raster.driver.RasterDataset#getData(int, int, int)
671
         */
672
        public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
673
                if(x < 0 || y < 0 || x >= file.width || y >= file.height)
674
                        throw new InvalidSetViewException("Request out of grid");
675

    
676
                Point2D begin = new Point2D.Double(x, y);
677
                Point2D end = new Point2D.Double(x + 1, y + 1);
678
                
679
                ownTransformation.transform(begin, begin);
680
                ownTransformation.transform(end, end);
681
                
682
                int[] readBandsFromECW = new int[file.numBands];
683
                if(file.numBands <= 3){
684
                        for(int i = 0; i < file.numBands; i++)
685
                                readBandsFromECW[i] = i;
686
                }else{
687
                        readBandsFromECW[0] = band;
688
                }
689

    
690
                Extent e = new Extent(begin.getX(), begin.getY(), end.getX(), end.getY());
691
                try {
692
                        int[] value = new int[1];
693
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), 1, 1);
694
                        file.readLineRGBA(value);
695
                        if(file.numBands <= 3){
696
                                switch(band){
697
                                case 0: return new Integer((((value[0] & 0x00ff0000) >> 16) & 0xffffffff));
698
                                case 1: return new Integer((((value[0] & 0x0000ff00) >> 8) & 0xffffffff));
699
                                case 2: return new Integer((((value[0] & 0x000000ff)) & 0xffffffff));
700
                                }
701
                        }
702
                        return new Integer((((value[0] & 0x00ff0000) >> 16) & 0xffffffff));
703
                } catch (JNCSFileNotOpenException e1) {
704
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
705
                } catch (JNCSInvalidSetViewException e1) {
706
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
707
                } catch (JNCSException e1) {
708
                        throw new RasterDriverException("Error reading ecw data");
709
                }
710
        }
711

    
712
        public void refreshUpdate(int arg0, int arg1, double arg2, double arg3, double arg4, double arg5) {
713
        }
714

    
715
        public void refreshUpdate(int arg0, int arg1, int arg2, int arg3, int arg4, int arg5) {
716
        }
717
        
718
        /**
719
         * Obtiene el objeto que contiene que contiene la interpretaci?n de 
720
         * color por banda
721
         * @return
722
         */
723
        public DatasetColorInterpretation getColorInterpretation(){
724
                if(colorInterpr == null) {
725
                        colorInterpr = new DatasetColorInterpretation();
726
                        colorInterpr.initColorInterpretation(getBandCount());
727
                        if(getBandCount() == 1)
728
                                colorInterpr.setColorInterpValue(0, DatasetColorInterpretation.GRAY_BAND);
729
                        if(getBandCount() >= 3) {
730
                                colorInterpr.setColorInterpValue(0, DatasetColorInterpretation.RED_BAND);
731
                                colorInterpr.setColorInterpValue(1, DatasetColorInterpretation.GREEN_BAND);
732
                                colorInterpr.setColorInterpValue(2, DatasetColorInterpretation.BLUE_BAND);
733
                        }
734
                }
735
                return colorInterpr;
736
        }
737
        
738
        /**
739
         * Asigna el objeto que contiene que contiene la interpretaci?n de 
740
         * color por banda
741
         * @param DatasetColorInterpretation
742
         */
743
        public void setColorInterpretation(DatasetColorInterpretation colorInterpretation){
744
                this.colorInterpretation = colorInterpretation;
745
        }
746
        
747
        /*
748
         * (non-Javadoc)
749
         * @see org.gvsig.raster.dataset.RasterDataset#getOverviewCount(int)
750
         */
751
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
752
                if(band >= getBandCount())
753
                        throw new BandAccessException("Wrong band");
754
                return 0;
755
        }
756
        
757
        /*
758
         * (non-Javadoc)
759
         * @see org.gvsig.raster.dataset.RasterDataset#getOverviewWidth(int, int)
760
         */
761
        public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
762
                if (band >= getBandCount())
763
                        throw new BandAccessException("Wrong band");
764
                return 0;
765
        }
766

    
767
        /*
768
         * (non-Javadoc)
769
         * @see org.gvsig.raster.dataset.RasterDataset#getOverviewWidth(int, int)
770
         */
771
        public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
772
                if (band >= getBandCount())
773
                        throw new BandAccessException("Wrong band");
774
                return 0;
775
        }
776
        
777
        /*
778
         * (non-Javadoc)
779
         * @see org.gvsig.raster.dataset.RasterDataset#overviewsSupport()
780
         */
781
        public boolean overviewsSupport() {
782
                return false;
783
        }
784

    
785
        /*
786
         * (non-Javadoc)
787
         * @see org.gvsig.raster.driver.GeoData#getStringProjection()
788
         */
789
        public String getStringProjection() throws RasterDriverException{
790
                return file.projection;
791
        }
792

    
793
        /*
794
         * (non-Javadoc)
795
         * @see org.gvsig.raster.driver.GeoData#getStringProjection()
796
         */
797
        public String getWktProjection() {
798
                //System.err.println("======>" + file.projection);
799
                return null;
800
        }
801
}