Statistics
| Revision:

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

History | View | Annotate | Download (29.2 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
                                                        file.readLineRGBA(pRGBArray);
460
                                                        for(int col = 0; col < pRGBArray.length; col ++){
461
                                                                rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
462
                                                                rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
463
                                                                rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
464
                                                        }
465
                                                }
466
                                                return;
467
                                        }
468
                                        if(i == 1) {
469
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
470
                                                        file.readLineRGBA(pRGBArray);
471
                                                        for(int col = 0; col < pRGBArray.length; col ++){
472
                                                                rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
473
                                                                rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
474
                                                                rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
475
                                                        }
476
                                                }
477
                                                return;
478
                                        }
479
                                        if(i == 2) {
480
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
481
                                                        file.readLineRGBA(pRGBArray);
482
                                                        for(int col = 0; col < pRGBArray.length; col ++){
483
                                                                rasterBuf.setElem(line, col, bandR, (byte)(pRGBArray[col] & 0x000000ff));
484
                                                                rasterBuf.setElem(line, col, bandG, (byte)(pRGBArray[col] & 0x000000ff));
485
                                                                rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
486
                                                        }
487
                                                }
488
                                                return;
489
                                        }
490
                                }
491
                                if(task.getEvent() != null)
492
                                        task.manageEvent(task.getEvent());
493
                        }
494

    
495
                }
496
                //********* END caso especial que resuelve Bug#1 **********************
497

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

    
510
                if(task.getEvent() != null)
511
                        task.manageEvent(task.getEvent());
512

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

    
524
                if(task.getEvent() != null)
525
                        task.manageEvent(task.getEvent());
526

    
527
                if(bandR >= 0){
528
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
529
                                file.readLineRGBA(pRGBArray);
530
                                for(int col = 0; col < pRGBArray.length; col ++)
531
                                        rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
532
                        }
533
                        return;
534
                }
535

    
536
                if(task.getEvent() != null)
537
                        task.manageEvent(task.getEvent());
538

    
539
        }
540

    
541
        public void reProject(ICoordTrans rp) {
542
        }
543

    
544
        /*
545
         * (non-Javadoc)
546
         * @see org.gvsig.raster.dataset.RasterDataset#getBlockSize()
547
         */
548
        public int getBlockSize() {
549
                return 0;
550
        }
551

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

    
560
                Point2D begin = rasterToWorld(new Point2D.Double(0, line));
561
                Point2D end = rasterToWorld(new Point2D.Double(file.width, line + 1));
562
                int[] readBandsFromECW = new int[file.numBands];
563
                if(file.numBands <= 3) {
564
                        for(int i = 0; i < file.numBands; i++)
565
                                readBandsFromECW[i] = i;
566
                }else {
567
                        readBandsFromECW[0] = band;
568
                }
569

    
570
                Extent e = new Extent(begin.getX(), begin.getY(), end.getX(), end.getY());
571

    
572
                try {
573
                        int[] value = new int[file.width];
574
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, 1);
575
                        file.readLineRGBA(value);
576

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

    
610
                return null;
611
        }
612

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

    
622
                if((pos + blockHeight) > file.height)
623
                        blockHeight = Math.abs(file.height - pos);
624

    
625
                Point2D begin = rasterToWorld(new Point2D.Double(0, pos));
626
                Point2D end = rasterToWorld(new Point2D.Double(file.width, pos + blockHeight));
627
                int[] readBandsFromECW = new int[file.numBands];
628

    
629
                for(int i = 0; i < file.numBands; i++)
630
                        readBandsFromECW[i] = i;
631

    
632
                byte[][][] buf = new byte[file.numBands][blockHeight][file.width];
633
                Extent e = new Extent(begin.getX(), begin.getY(), end.getX(), end.getY());
634
                e = RasterUtilities.calculateAdjustedView(getExtent(), e);
635

    
636
                try {
637
                        int[] value = new int[file.width];
638
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, blockHeight);
639

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

    
654
                                if(task.getEvent() != null)
655
                                        task.manageEvent(task.getEvent());
656

    
657
                        } else {
658
                                //TODO: FUNCIONALIDAD: file.numBands > 3
659
                        }
660

    
661
                        //TODO: FUNCIONALIDAD: Ecw con otro tipo de dato != Byte
662
                } catch (JNCSFileNotOpenException e1) {
663
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
664
                } catch (JNCSInvalidSetViewException e1) {
665
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
666
                } catch (JNCSException e1) {
667
                        throw new RasterDriverException("Error la lectura de datos ecw");
668
                }
669

    
670
                return buf;
671
        }
672

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

    
681
                Point2D begin = new Point2D.Double(x, y);
682
                Point2D end = new Point2D.Double(x + 1, y + 1);
683

    
684
                ownTransformation.transform(begin, begin);
685
                ownTransformation.transform(end, end);
686

    
687
                int[] readBandsFromECW = new int[file.numBands];
688
                if(file.numBands <= 3){
689
                        for(int i = 0; i < file.numBands; i++)
690
                                readBandsFromECW[i] = i;
691
                }else{
692
                        readBandsFromECW[0] = band;
693
                }
694

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

    
717
        public void refreshUpdate(int arg0, int arg1, double arg2, double arg3, double arg4, double arg5) {
718
        }
719

    
720
        public void refreshUpdate(int arg0, int arg1, int arg2, int arg3, int arg4, int arg5) {
721
        }
722

    
723
        /**
724
         * Obtiene el objeto que contiene que contiene la interpretaci?n de
725
         * color por banda
726
         * @return
727
         */
728
        public DatasetColorInterpretation getColorInterpretation(){
729
                if(colorInterpr == null) {
730
                        colorInterpr = new DatasetColorInterpretation();
731
                        colorInterpr.initColorInterpretation(getBandCount());
732
                        if(getBandCount() == 1)
733
                                colorInterpr.setColorInterpValue(0, DatasetColorInterpretation.GRAY_BAND);
734
                        if(getBandCount() >= 3) {
735
                                colorInterpr.setColorInterpValue(0, DatasetColorInterpretation.RED_BAND);
736
                                colorInterpr.setColorInterpValue(1, DatasetColorInterpretation.GREEN_BAND);
737
                                colorInterpr.setColorInterpValue(2, DatasetColorInterpretation.BLUE_BAND);
738
                        }
739
                }
740
                return colorInterpr;
741
        }
742

    
743
        /**
744
         * Asigna el objeto que contiene que contiene la interpretaci?n de
745
         * color por banda
746
         * @param DatasetColorInterpretation
747
         */
748
        public void setColorInterpretation(DatasetColorInterpretation colorInterpretation){
749
                this.colorInterpretation = colorInterpretation;
750
        }
751

    
752
        /*
753
         * (non-Javadoc)
754
         * @see org.gvsig.raster.dataset.RasterDataset#getOverviewCount(int)
755
         */
756
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
757
                if(band >= getBandCount())
758
                        throw new BandAccessException("Wrong band");
759
                return 0;
760
        }
761

    
762
        /*
763
         * (non-Javadoc)
764
         * @see org.gvsig.raster.dataset.RasterDataset#getOverviewWidth(int, int)
765
         */
766
        public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
767
                if (band >= getBandCount())
768
                        throw new BandAccessException("Wrong band");
769
                return 0;
770
        }
771

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

    
782
        /*
783
         * (non-Javadoc)
784
         * @see org.gvsig.raster.dataset.RasterDataset#overviewsSupport()
785
         */
786
        public boolean overviewsSupport() {
787
                return false;
788
        }
789

    
790
        /*
791
         * (non-Javadoc)
792
         * @see org.gvsig.raster.driver.GeoData#getStringProjection()
793
         */
794
        public String getStringProjection() throws RasterDriverException{
795
                return file.projection;
796
        }
797

    
798
        /*
799
         * (non-Javadoc)
800
         * @see org.gvsig.raster.driver.GeoData#getStringProjection()
801
         */
802
        public String getWktProjection() {
803
                //System.err.println("======>" + file.projection);
804
                return null;
805
        }
806
}