Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRaster_io / src / org / gvsig / fmap / dal / coverage / dataset / io / ErmapperDriver.java @ 29713

History | View | Annotate | Download (29.5 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.fmap.dal.coverage.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.RasterDriverException;
38
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation;
39
import org.gvsig.raster.dataset.rmf.ParsingException;
40
import org.gvsig.raster.datastruct.Extent;
41
import org.gvsig.raster.datastruct.Transparency;
42
import org.gvsig.raster.process.RasterTask;
43
import org.gvsig.raster.process.RasterTaskQueue;
44
import org.gvsig.raster.util.RasterUtilities;
45
import org.gvsig.tools.ToolsLocator;
46
import org.gvsig.tools.extensionpoint.ExtensionPoint;
47
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
48

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

    
61
        private JNCSFile                                       file = null;
62

    
63
        /**
64
         * Estado de transparencia del raster.
65
         */
66
        protected Transparency                        fileTransparency = null;
67

    
68
        /**
69
         * Extent de la ventana seleccionada
70
         */
71
        private Extent                        viewRequest = null;
72

    
73
        private DatasetColorInterpretation    colorInterpr = null;
74

    
75
        public static void register() {
76
                ExtensionPointManager extensionPoints =ToolsLocator.getExtensionPointManager();
77
                ExtensionPoint point=extensionPoints.get("RasterReader");
78
//                ExtensionPoint point = ExtensionPoint.getExtensionPoint("RasterReader");
79
                point.append("ecw", "", ErmapperDriver.class);
80
                point.append("jp2", "", ErmapperDriver.class);
81
        }
82

    
83
        class Contour extends Vector {
84
                final private static long serialVersionUID = 0;
85
                public double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE;
86
                public double maxX = -Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
87
                public Contour() {
88
                        super();
89
                }
90
                public void add(Point2D pt) {
91
                        super.add(pt);
92
                        if (pt.getX() > maxX) maxX = pt.getX();
93
                        if (pt.getX() < minX) minX = pt.getX();
94
                        if (pt.getY() > maxY) maxY = pt.getY();
95
                        if (pt.getY() < minY) minY = pt.getY();
96
                }
97
        }
98

    
99
        /**
100
         * Constructor. Abre el dataset.
101
         * @param proj Proyecci?n
102
         * @param fName Nombre del fichero ecw
103
         * @throws NotSupportedExtensionException
104
         */
105
        public ErmapperDriver(IProjection proj, Object param)throws NotSupportedExtensionException {
106
                super(proj, ((String)param));
107
                setParam(param);
108
                try {
109

    
110
                        if (!new File(((String)param)).exists() && !((String)param).startsWith("ecwp:"))
111
                                throw new NotSupportedExtensionException("Extension not supported");
112

    
113
                        file = new JNCSFile(((String)param), false);
114
                        load();
115
                        bandCount = file.numBands;
116
                        getTransparencyDatasetStatus();
117

    
118
                        int[] dt = new int[bandCount];
119
                        for (int i = 0; i < bandCount; i++)
120
                                dt[i] = IBuffer.TYPE_BYTE;
121
                        setDataType(dt);
122

    
123
                        super.init();
124

    
125
                        try {
126
                                loadFromRmf(getRmfBlocksManager());
127
                        } catch (ParsingException e) {
128
                                //No lee desde rmf
129
                        }
130

    
131
                } catch (Exception e) {
132
                        throw new NotSupportedExtensionException("Extension not supported");
133
                }
134
        }
135

    
136
        /**
137
         * Carga un ECW.
138
         * @param fname
139
         */
140
        public GeoInfo load() {
141
                ownTransformation = new AffineTransform(file.cellIncrementX, 0, 0, file.cellIncrementY, file.originX, file.originY);
142
                externalTransformation = (AffineTransform) ownTransformation.clone();
143
                return this;
144
        }
145

    
146
        /**
147
         * Cierra el fichero ECW
148
         */
149
        public void close() {
150
                if(file != null) {
151
                        file.close(false);
152
                        file = null;
153
                }
154
        }
155

    
156
        /**
157
         * Obtiene el objeto que contiene el estado de la transparencia
158
         */
159
        public Transparency getTransparencyDatasetStatus() {
160
                if(fileTransparency == null)
161
                        fileTransparency = new Transparency();
162
                return fileTransparency;
163
        }
164

    
165
        /**
166
         * Devuelve el ancho de la imagen
167
         */
168
        public int getWidth() {
169
                return file.width;
170
        }
171

    
172
        /**
173
         * Devuelve el alto de la imagen
174
         */
175
        public int getHeight() {
176
                return file.height;
177
        }
178

    
179
        /**
180
         * Obtiene el extent de la ?ltima ventana seleccionada.
181
         * @return Extent
182
         */
183
        public Extent getView() {
184
                return viewRequest;
185
        }
186

    
187
        /*
188
         * (non-Javadoc)
189
         * @see org.gvsig.fmap.driver.RasterDataset#setView(org.gvsig.fmap.raster.Extent)
190
         */
191
        public void setView(Extent e) {
192
                viewRequest = new Extent(e);
193
        }
194

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

220
                        stpBuffer[0] = (int)(p1.getX() + (-p3.getX()));
221
                        stpBuffer[1] = (int)(p1.getY() + (-p3.getY()));
222
                        stpBuffer[2] = stpBuffer[0] + w;
223
                        stpBuffer[3] = stpBuffer[1] + h;
224
                        return new int[]{w, h};
225
                }
226
                return new int[]{nWidth, nHeight};
227
        }*/
228

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

    
245
                Extent selectedExtent = new Extent(p1.getX(), p1.getY(), p2.getX(), p2.getY());
246

    
247
                setView(selectedExtent);
248
                int wPx = rasterBuf.getWidth();
249
                int hPx = rasterBuf.getHeight();
250
                int[] stpBuffer = new int[]{0, 0 , wPx, hPx};
251

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

    
254
                return rasterBuf;
255
        }
256

    
257
        /*
258
         * (non-Javadoc)
259
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(double, double, double, double, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer, boolean)
260
         */
261
        public IBuffer getWindowRaster(double ulx, double uly, double w, double h, BandList bandList, IBuffer rasterBuf, boolean adjustToExtent) throws InterruptedException, RasterDriverException {
262
                //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
263
                //tenemos que averiguar si lrx es x + w o x -w, asi como si lry es y + h o y - h
264
                Extent ext = getExtent();
265
                Point2D pInit = rasterToWorld(new Point2D.Double(0, 0));
266
                Point2D pEnd = rasterToWorld(new Point2D.Double(getWidth(), getHeight()));
267
                double wRaster = Math.abs(pEnd.getX() - pInit.getX());
268
                double hRaster = Math.abs(pEnd.getY() - pInit.getY());
269
                double lrx = (((ext.getULX() - wRaster) > ext.maxX()) || ((ext.getULX() - wRaster) < ext.minX())) ? (ulx + w) : (ulx - w);
270
                double lry = (((ext.getULY() - hRaster) > ext.maxY()) || ((ext.getULY() - hRaster) < ext.minY())) ? (uly + h) : (uly - h);
271

    
272
                Point2D p1 = new Point2D.Double(ulx, uly);
273
                Point2D p2 = new Point2D.Double(lrx, lry);
274
                try {
275
                        externalTransformation.inverseTransform(p1, p1);
276
                        externalTransformation.inverseTransform(p2, p2);
277
                        ownTransformation.transform(p1, p1);
278
                        ownTransformation.transform(p2, p2);
279
                } catch (NoninvertibleTransformException e) {
280
                        throw new RasterDriverException("Noninvertible transform");
281
                }
282

    
283
                Extent selectedExtent = new Extent(p1.getX(), p1.getY(), p2.getX(), p2.getY());
284

    
285
                setView(selectedExtent);
286
                int wPx = rasterBuf.getWidth();
287
                int hPx = rasterBuf.getHeight();
288
                int[] stpBuffer = new int[]{0, 0 , wPx, hPx};
289

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

    
301
                loadBuffer(viewRequest, wPx, hPx, rasterBuf, bandList, stpBuffer);
302

    
303
                return rasterBuf;
304
        }
305

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

    
317
                loadBuffer(viewRequest, w, h, rasterBuf, bandList, stpBuffer);
318
                return rasterBuf;
319
        }
320

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

    
340
                //TODO: FUNCIONALIDAD: Implementar adjustToExtent = false
341

    
342
                loadBuffer(viewRequest, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
343
                return rasterBuf;
344
        }
345

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

    
357
                loadBuffer(viewRequest, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
358
                return rasterBuf;
359
        }
360

    
361
        /**
362
         * Carga el buffer con las bandas RGB del raster con los par?metros especificados de extensi?n
363
         * y tama?o de buffer. El problema de ecw es que solo podemos leer 3 bandas de una vez ya que solo disponemos
364
         * de una llamada readLineRGBA. Para leer m?s bandas tendremos que hacer multiples llamadas a setView para leer
365
         * 3 cada vez.
366
         *
367
         * Si por ejemplo tenemos un ecw de 6 bandas [0, 1, 2, 3, 4, 5] y queremos cargar un buffer con el siguiente orden
368
         * [0, -, 2, -, 4, -] La variable readBandsFromECW har? la llamada a setView con los valores [0, 2, 4, 0, 0, 0]. La
369
         * funci?n drawRGB ser? la encargada de hacer el switch para obtener [0, -, 2, -, 4, -].
370
         *
371
         * Bug#1: Si se dispone de un ecw de m?s de tres bandas podemos llamar a setView con readBandsFromECW con el orden
372
         * que queramos, por ejemplo [3, 2, 5, 1, 0] pero para ecw de 3 bandas la llamada con las bandas cambiadas no
373
         * hace caso. El caso de readBandsFromECW = [2, 0, 1] ser? tomado siempre como [0, 1, 2].
374
         *
375
         * @param selectedExtent Extensi?n seleccionada
376
         * @param bufWidth Ancho de buffer
377
         * @param bufHeight Alto de buffer
378
         * @param rasterBuf Buffer de datos
379
         */
380
        private void loadBuffer(Extent selectedExtent, int bufWidth, int bufHeight, IBuffer rasterBuf, BandList bandList, int[] stpBuffer) throws InterruptedException, RasterDriverException {
381
                try{
382
                        RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
383

    
384
                        //Leemos el raster desde la librer?a
385

    
386
                        //Extent ext = getExtent();//new Extent(file.originX, file.originY, file.originX + (file.width * file.cellIncrementX), file.originY + (file.height * file.cellIncrementY));
387
                        //selectedExtent = RasterUtilities.calculateAdjustedView(selectedExtent, ext);
388
                        //selectedExtent = RasterUtilities.calculateAdjustedView(selectedExtent, this.getAffineTransform(), new Dimension(file.width, file.height));
389
                        int[] readBandsFromECW = new int[Math.max(file.numBands, 3)];
390
                        int[] readBands = new int[Math.max(file.numBands, 3)];
391

    
392

    
393
                        for(int i = 0; i < readBandsFromECW.length; i ++)
394
                                readBands[i] = -1;
395
                        int cont = 0;
396
                        for(int i = 0; i < file.numBands; i++) {
397
                                int[] bandsToDraw = bandList.getBand(i).getBufferBandListToDraw();
398
                                if(bandsToDraw != null) {
399
                                        for(int j = 0; j < bandsToDraw.length; j++){
400
                                                readBandsFromECW[cont] = i;
401
                                                readBands[cont] = i;
402
                                                cont ++;
403
                                        }
404
                                }
405

    
406
                        }
407

    
408
                        if(task.getEvent() != null)
409
                                task.manageEvent(task.getEvent());
410

    
411
                        //Si el ancho y alto pedido no coincide con el estimado es que se intenta resamplear.
412
                        //Otros drivers soportan el resampleo pero no es el caso. Hay que hacer la petici?n y luego remuestrear.
413

    
414
                        /*if((selectedExtent.width()/file.cellIncrementX) || (selectedExtent.height()/file.cellIncrementY)) {
415

416
                        }*/
417

    
418
                        if(bufWidth > Math.round(Math.abs(selectedExtent.width()/file.cellIncrementX)))
419
                                bufWidth = (int)Math.round(Math.abs(selectedExtent.width()/file.cellIncrementX));
420
                        if(bufHeight > Math.round(Math.abs(selectedExtent.height()/file.cellIncrementY)))
421
                                bufHeight = (int)Math.round(Math.abs(selectedExtent.height()/file.cellIncrementY));
422
                        file.setView(file.numBands, readBandsFromECW, selectedExtent.minX(), selectedExtent.maxY(), selectedExtent.maxX(), selectedExtent.minY(), bufWidth, bufHeight);
423
                        
424
                        //Escribimos el raster sobre un IBuffer
425
                        int[] pRGBArray = new int[bufWidth];
426
                        drawRGB(rasterBuf, pRGBArray, readBandsFromECW, bandList, task);
427

    
428
                }catch(JNCSInvalidSetViewException exc){
429
                        throw new RasterDriverException("Error setting coords");
430
                }catch (JNCSFileNotOpenException e) {
431
                        throw new RasterDriverException("Error opening file");
432
                }catch (JNCSException ex) {
433
                        throw new RasterDriverException("Error reading data");
434
                }
435

    
436
        }
437

    
438

    
439
        private void drawRGB(IBuffer rasterBuf, int[] pRGBArray, int[] readBands, BandList bandList, RasterTask task) throws JNCSException, InterruptedException {
440
                int bandR = readBands[0];
441
                int bandG = (readBands.length > 1) ? readBands[1] : -1;
442
                int bandB = (readBands.length > 2) ? readBands[2] : -1;
443

    
444
                //********* caso especial que resuelve Bug#1 **********************
445
                if(file.numBands == 3 && bandList.getDrawableBandsCount() < 3) {
446
                        for(int i = 0; i < 3; i ++) {
447
                                int[] b = bandList.getBand(i).getBufferBandListToDraw();
448
                                if(b != null){
449
                                        bandG = 1; bandR = 0; bandB = 2;
450
                                }
451
                        }
452
                }
453
                if(file.numBands == 3 && bandR == bandG && bandG == bandB) { //caso especial que resuelve Bug#1
454
                        for(int i = 0; i < 3; i ++) {
455
                                int[] b = bandList.getBand(i).getBufferBandListToDraw();
456
                                if(b != null) {
457
                                        if(i == 0) {
458
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
459
                                                        try {
460
                                                                file.readLineRGBA(pRGBArray);
461
                                                                for(int col = 0; col < pRGBArray.length; col ++){
462
                                                                        rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
463
                                                                        rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
464
                                                                        rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
465
                                                                }
466
                                                        } catch (JNCSException exc) {
467
                                                        }
468
                                                }
469
                                                return;
470
                                        }
471
                                        if(i == 1) {
472
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
473
                                                        try {
474
                                                                file.readLineRGBA(pRGBArray);
475
                                                                for(int col = 0; col < pRGBArray.length; col ++){
476
                                                                        rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
477
                                                                        rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
478
                                                                        rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
479
                                                                }
480
                                                        } catch (JNCSException exc) {
481
                                                        }
482
                                                }
483
                                                return;
484
                                        }
485
                                        if(i == 2) {
486
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
487
                                                        try {
488
                                                                file.readLineRGBA(pRGBArray);
489
                                                                for(int col = 0; col < pRGBArray.length; col ++){
490
                                                                        rasterBuf.setElem(line, col, bandR, (byte)(pRGBArray[col] & 0x000000ff));
491
                                                                        rasterBuf.setElem(line, col, bandG, (byte)(pRGBArray[col] & 0x000000ff));
492
                                                                        rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
493
                                                                }
494
                                                        } catch (JNCSException exc) {
495
                                                        }
496
                                                }
497
                                                return;
498
                                        }
499
                                }
500
                                if(task.getEvent() != null)
501
                                        task.manageEvent(task.getEvent());
502
                        }
503

    
504
                }
505
                //********* END caso especial que resuelve Bug#1 **********************
506

    
507
                if(bandR >= 0 && bandG >= 0 && bandB >= 0) {
508
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
509
                                try {
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
                                                rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
515
                                        }
516
                                } catch (JNCSException exc) {
517
                                }
518
                        }
519
                        return;
520
                }
521

    
522
                if(task.getEvent() != null)
523
                        task.manageEvent(task.getEvent());
524

    
525
                if(bandR >= 0 && bandG >= 0) {
526
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
527
                                try {
528
                                        file.readLineRGBA(pRGBArray);
529
                                        for(int col = 0; col < pRGBArray.length; col ++){
530
                                                rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
531
                                                rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
532
                                        }
533
                                } catch (JNCSException exc) {
534
                                }
535
                        }
536
                        return;
537
                }
538

    
539
                if(task.getEvent() != null)
540
                        task.manageEvent(task.getEvent());
541

    
542
                if(bandR >= 0){
543
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
544
                                try {
545
                                        file.readLineRGBA(pRGBArray);
546
                                        for(int col = 0; col < pRGBArray.length; col ++)
547
                                                rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
548
                                } catch (JNCSException exc) {
549
                                }
550
                        }
551
                        return;
552
                }
553

    
554
                if(task.getEvent() != null)
555
                        task.manageEvent(task.getEvent());
556

    
557
        }
558

    
559
        public void reProject(ICoordTrans rp) {
560
        }
561

    
562
        /*
563
         * (non-Javadoc)
564
         * @see org.gvsig.raster.dataset.RasterDataset#getBlockSize()
565
         */
566
        public int getBlockSize() {
567
                return 0;
568
        }
569

    
570
        /*
571
         * (non-Javadoc)
572
         * @see org.gvsig.raster.driver.RasterDataset#readCompletetLine(int, int)
573
         */
574
        public Object readCompleteLine(int line, int band) throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
575
                if(line < 0 || line >= file.height || band < 0 || band >= getBandCount())
576
                        throw new InvalidSetViewException("Request out of grid");
577

    
578
                Point2D begin = rasterToWorld(new Point2D.Double(0, line));
579
                Point2D end = rasterToWorld(new Point2D.Double(file.width, line + 1));
580
                int[] readBandsFromECW = new int[file.numBands];
581
                if(file.numBands <= 3) {
582
                        for(int i = 0; i < file.numBands; i++)
583
                                readBandsFromECW[i] = i;
584
                }else {
585
                        readBandsFromECW[0] = band;
586
                }
587

    
588
                Extent e = new Extent(begin.getX(), begin.getY(), end.getX(), end.getY());
589

    
590
                try {
591
                        int[] value = new int[file.width];
592
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, 1);
593
                        file.readLineRGBA(value);
594

    
595
                        if(file.numBands <= 3) {
596
                                switch(getDataType()[0]) {
597
                                case IBuffer.TYPE_BYTE: byte[] b = new byte[file.width];
598
                                                                                switch(band) {
599
                                                                                case 0: for(int i = 0; i < file.width; i ++)
600
                                                                                                        b[i] = (byte)(((value[i] & 0x00ff0000) >> 16) & 0xff);
601
                                                                                                break;
602
                                                                                case 1: for(int i = 0; i < file.width; i ++)
603
                                                                                                        b[i] = (byte)(((value[i] & 0x0000ff00) >> 8) & 0xff);
604
                                                                                                break;
605
                                                                                case 2: for(int i = 0; i < file.width; i ++)
606
                                                                                                        b[i] = (byte)((value[i] & 0x000000ff) & 0xff);
607
                                                                                                break;
608
                                                                                }
609
                                                                                return b;
610
                                }
611
                        }else {
612
                                switch(getDataType()[0]) {
613
                                case IBuffer.TYPE_BYTE: byte[] b = new byte[file.width];
614
                                                                                for(int i = 0; i < file.width; i ++)
615
                                                                                        b[i] = (byte)(((value[i] & 0x00ff0000) >> 16) & 0xff);
616
                                                                                break;
617
                                }
618
                        }
619
                        //TODO: FUNCIONALIDAD: Ecw con otro tipo de dato != Byte
620
                } catch (JNCSFileNotOpenException e1) {
621
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
622
                } catch (JNCSInvalidSetViewException e1) {
623
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
624
                } catch (JNCSException e1) {
625
                        throw new RasterDriverException("Error la lectura de datos ecw");
626
                }
627

    
628
                return null;
629
        }
630

    
631
        /*
632
         *  (non-Javadoc)
633
         * @see org.gvsig.raster.dataset.RasterDataset#readBlock(int, int, int)
634
         */
635
        public Object readBlock(int pos, int blockHeight) throws InvalidSetViewException, FileNotOpenException, RasterDriverException, InterruptedException {
636
                RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
637
                if(pos < 0)
638
                        throw new InvalidSetViewException("Request out of grid");
639

    
640
                if((pos + blockHeight) > file.height)
641
                        blockHeight = Math.abs(file.height - pos);
642

    
643
                Point2D begin = rasterToWorld(new Point2D.Double(0, pos));
644
                Point2D end = rasterToWorld(new Point2D.Double(file.width, pos + blockHeight));
645
                int[] readBandsFromECW = new int[file.numBands];
646

    
647
                for(int i = 0; i < file.numBands; i++)
648
                        readBandsFromECW[i] = i;
649

    
650
                byte[][][] buf = new byte[file.numBands][blockHeight][file.width];
651
                Extent e = new Extent(begin.getX(), begin.getY(), end.getX(), end.getY());
652
                e = RasterUtilities.calculateAdjustedView(getExtent(), e);
653

    
654
                try {
655
                        int[] value = new int[file.width];
656
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, blockHeight);
657

    
658
                        if(file.numBands <= 3) {
659
                                for (int row = 0; row < blockHeight; row++) {
660
                                        file.readLineRGBA(value);
661
                                        switch(getDataType()[0]) {
662
                                        case IBuffer.TYPE_BYTE:
663
                                                for(int col = 0; col < file.width; col ++) {
664
                                                        buf[0][row][col] = (byte)(((value[col] & 0x00ff0000) >> 16) & 0xff);
665
                                                        buf[1][row][col] = (byte)(((value[col] & 0x0000ff00) >> 8) & 0xff);
666
                                                        buf[2][row][col] = (byte)((value[col] & 0x000000ff) & 0xff);
667
                                                }
668
                                                break;
669
                                        }
670
                                }
671

    
672
                                if(task.getEvent() != null)
673
                                        task.manageEvent(task.getEvent());
674

    
675
                        } else {
676
                                //TODO: FUNCIONALIDAD: file.numBands > 3
677
                        }
678

    
679
                        //TODO: FUNCIONALIDAD: Ecw con otro tipo de dato != Byte
680
                } catch (JNCSFileNotOpenException e1) {
681
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
682
                } catch (JNCSInvalidSetViewException e1) {
683
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
684
                } catch (JNCSException e1) {
685
                        throw new RasterDriverException("Error la lectura de datos ecw");
686
                }
687

    
688
                return buf;
689
        }
690

    
691
        /*
692
         * (non-Javadoc)
693
         * @see org.gvsig.raster.driver.RasterDataset#getData(int, int, int)
694
         */
695
        public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
696
                if(x < 0 || y < 0 || x >= file.width || y >= file.height)
697
                        throw new InvalidSetViewException("Request out of grid");
698

    
699
                Point2D begin = new Point2D.Double(x, y);
700
                Point2D end = new Point2D.Double(x + 1, y + 1);
701

    
702
                ownTransformation.transform(begin, begin);
703
                ownTransformation.transform(end, end);
704

    
705
                int[] readBandsFromECW = new int[file.numBands];
706
                if(file.numBands <= 3){
707
                        for(int i = 0; i < file.numBands; i++)
708
                                readBandsFromECW[i] = i;
709
                }else{
710
                        readBandsFromECW[0] = band;
711
                }
712

    
713
                Extent e = new Extent(begin.getX(), begin.getY(), end.getX(), end.getY());
714
                try {
715
                        int[] value = new int[1];
716
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), 1, 1);
717
                        file.readLineRGBA(value);
718
                        if(file.numBands <= 3){
719
                                switch(band){
720
                                case 0: return new Integer((((value[0] & 0x00ff0000) >> 16) & 0xffffffff));
721
                                case 1: return new Integer((((value[0] & 0x0000ff00) >> 8) & 0xffffffff));
722
                                case 2: return new Integer((((value[0] & 0x000000ff)) & 0xffffffff));
723
                                }
724
                        }
725
                        return new Integer((((value[0] & 0x00ff0000) >> 16) & 0xffffffff));
726
                } catch (JNCSFileNotOpenException e1) {
727
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
728
                } catch (JNCSInvalidSetViewException e1) {
729
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
730
                } catch (JNCSException e1) {
731
                        throw new RasterDriverException("Error reading ecw data");
732
                }
733
        }
734

    
735
        public void refreshUpdate(int arg0, int arg1, double arg2, double arg3, double arg4, double arg5) {
736
        }
737

    
738
        public void refreshUpdate(int arg0, int arg1, int arg2, int arg3, int arg4, int arg5) {
739
        }
740

    
741
        /**
742
         * Obtiene el objeto que contiene que contiene la interpretaci?n de
743
         * color por banda
744
         * @return
745
         */
746
        public DatasetColorInterpretation getColorInterpretation(){
747
                if(colorInterpr == null) {
748
                        colorInterpr = new DatasetColorInterpretation();
749
                        colorInterpr.initColorInterpretation(getBandCount());
750
                        if(getBandCount() == 1)
751
                                colorInterpr.setColorInterpValue(0, DatasetColorInterpretation.GRAY_BAND);
752
                        if(getBandCount() >= 3) {
753
                                colorInterpr.setColorInterpValue(0, DatasetColorInterpretation.RED_BAND);
754
                                colorInterpr.setColorInterpValue(1, DatasetColorInterpretation.GREEN_BAND);
755
                                colorInterpr.setColorInterpValue(2, DatasetColorInterpretation.BLUE_BAND);
756
                        }
757
                }
758
                return colorInterpr;
759
        }
760

    
761
        /**
762
         * Asigna el objeto que contiene que contiene la interpretaci?n de
763
         * color por banda
764
         * @param DatasetColorInterpretation
765
         */
766
        public void setColorInterpretation(DatasetColorInterpretation colorInterpretation){
767
                this.colorInterpretation = colorInterpretation;
768
        }
769

    
770
        /*
771
         * (non-Javadoc)
772
         * @see org.gvsig.raster.dataset.RasterDataset#getOverviewCount(int)
773
         */
774
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
775
                if(band >= getBandCount())
776
                        throw new BandAccessException("Wrong band");
777
                return 0;
778
        }
779

    
780
        /*
781
         * (non-Javadoc)
782
         * @see org.gvsig.raster.dataset.RasterDataset#getOverviewWidth(int, int)
783
         */
784
        public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
785
                if (band >= getBandCount())
786
                        throw new BandAccessException("Wrong band");
787
                return 0;
788
        }
789

    
790
        /*
791
         * (non-Javadoc)
792
         * @see org.gvsig.raster.dataset.RasterDataset#getOverviewWidth(int, int)
793
         */
794
        public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
795
                if (band >= getBandCount())
796
                        throw new BandAccessException("Wrong band");
797
                return 0;
798
        }
799

    
800
        /*
801
         * (non-Javadoc)
802
         * @see org.gvsig.raster.dataset.RasterDataset#overviewsSupport()
803
         */
804
        public boolean overviewsSupport() {
805
                return false;
806
        }
807

    
808
        /*
809
         * (non-Javadoc)
810
         * @see org.gvsig.raster.driver.GeoData#getStringProjection()
811
         */
812
        public String getStringProjection() throws RasterDriverException{
813
                return file.projection;
814
        }
815

    
816
        /*
817
         * (non-Javadoc)
818
         * @see org.gvsig.raster.driver.GeoData#getStringProjection()
819
         */
820
        public String getWktProjection() {
821
                //System.err.println("======>" + file.projection);
822
                return null;
823
        }
824
}