Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.io / org.gvsig.raster.io.base / src / main / java / org / gvsig / fmap / dal / coverage / dataset / io / ErmapperDriver.java @ 86

History | View | Annotate | Download (29.9 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.fmap.dal.coverage.dataset.io;
23

    
24
import java.awt.geom.AffineTransform;
25
import java.awt.geom.NoninvertibleTransformException;
26
import java.awt.geom.Point2D;
27
import java.io.File;
28
import java.util.Vector;
29

    
30
import org.cresques.cts.ICoordTrans;
31
import org.cresques.cts.IProjection;
32
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
33
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
34
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
35
import org.gvsig.fmap.dal.coverage.exception.BandAccessException;
36
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
37
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
38
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
39
import org.gvsig.fmap.dal.coverage.exception.ParsingException;
40
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
41
import org.gvsig.fmap.dal.coverage.process.RasterTask;
42
import org.gvsig.fmap.dal.coverage.process.RasterTaskQueue;
43
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
44
import org.gvsig.raster.impl.datastruct.ExtentImpl;
45
import org.gvsig.raster.impl.datastruct.TransparencyImpl;
46
import org.gvsig.raster.impl.provider.DefaultRasterProvider;
47
import org.gvsig.raster.impl.store.GeoInfo;
48
import org.gvsig.raster.impl.store.properties.DatasetColorInterpretation;
49
import org.gvsig.raster.util.RasterUtilities;
50
import org.gvsig.tools.ToolsLocator;
51
import org.gvsig.tools.extensionpoint.ExtensionPoint;
52
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
53

    
54
import com.ermapper.ecw.JNCSException;
55
import com.ermapper.ecw.JNCSFile;
56
import com.ermapper.ecw.JNCSFileNotOpenException;
57
import com.ermapper.ecw.JNCSInvalidSetViewException;
58
import com.ermapper.ecw.JNCSProgressiveUpdate;
59
/**
60
 * Driver de Ecw
61
 *
62
 * @author Nacho Brodin (nachobrodin@gmail.com)
63
 */
64
public class ErmapperDriver extends DefaultRasterProvider implements JNCSProgressiveUpdate {
65

    
66
        private JNCSFile                                       file = null;
67

    
68
        /**
69
         * Estado de transparencia del raster.
70
         */
71
        protected Transparency                        fileTransparency = null;
72

    
73
        /**
74
         * Extent de la ventana seleccionada
75
         */
76
        private Extent                        viewRequest = null;
77

    
78
        private DatasetColorInterpretation    colorInterpr = null;
79

    
80
        public static void register() {
81
                ExtensionPointManager extensionPoints =ToolsLocator.getExtensionPointManager();
82
                ExtensionPoint point=extensionPoints.get("RasterReader");
83
//                ExtensionPoint point = ExtensionPoint.getExtensionPoint("RasterReader");
84
                point.append("ecw", "", ErmapperDriver.class);
85
                point.append("jp2", "", ErmapperDriver.class);
86
        }
87

    
88
        @SuppressWarnings("unchecked")
89
        class Contour extends Vector {
90
                final private static long serialVersionUID = 0;
91
                public double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE;
92
                public double maxX = -Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
93
                public Contour() {
94
                        super();
95
                }
96
                public void add(Point2D pt) {
97
                        super.add(pt);
98
                        if (pt.getX() > maxX) maxX = pt.getX();
99
                        if (pt.getX() < minX) minX = pt.getX();
100
                        if (pt.getY() > maxY) maxY = pt.getY();
101
                        if (pt.getY() < minY) minY = pt.getY();
102
                }
103
        }
104

    
105
        /**
106
         * Constructor. Abre el dataset.
107
         * @param proj Proyecci?n
108
         * @param fName Nombre del fichero ecw
109
         * @throws NotSupportedExtensionException
110
         */
111
        public ErmapperDriver(IProjection proj, Object param)throws NotSupportedExtensionException {
112
                super(proj, ((String)param));
113
                setParam(param);
114
                try {
115

    
116
                        if (!new File(((String)param)).exists() && !((String)param).startsWith("ecwp:"))
117
                                throw new NotSupportedExtensionException("Extension not supported");
118

    
119
                        file = new JNCSFile(((String)param), false);
120
                        load();
121
                        bandCount = file.numBands;
122
                        getTransparency();
123

    
124
                        int[] dt = new int[bandCount];
125
                        for (int i = 0; i < bandCount; i++)
126
                                dt[i] = Buffer.TYPE_BYTE;
127
                        setDataType(dt);
128

    
129
                        super.init();
130

    
131
                        try {
132
                                loadFromRmf(getRmfBlocksManager());
133
                        } catch (ParsingException e) {
134
                                //No lee desde rmf
135
                        }
136

    
137
                } catch (Exception e) {
138
                        throw new NotSupportedExtensionException("Extension not supported");
139
                }
140
        }
141

    
142
        /**
143
         * Carga un ECW.
144
         * @param fname
145
         */
146
        public GeoInfo load() {
147
                ownTransformation = new AffineTransform(file.cellIncrementX, 0, 0, file.cellIncrementY, file.originX, file.originY);
148
                externalTransformation = (AffineTransform) ownTransformation.clone();
149
                return this;
150
        }
151

    
152
        /**
153
         * Cierra el fichero ECW
154
         */
155
        public void close() {
156
                if(file != null) {
157
                        file.close(false);
158
                        file = null;
159
                }
160
        }
161

    
162
        /**
163
         * Obtiene el objeto que contiene el estado de la transparencia
164
         */
165
        public Transparency getTransparency() {
166
                if(fileTransparency == null)
167
                        fileTransparency = new TransparencyImpl();
168
                return fileTransparency;
169
        }
170

    
171
        /**
172
         * Devuelve el ancho de la imagen
173
         */
174
        public double getWidth() {
175
                return file.width;
176
        }
177

    
178
        /**
179
         * Devuelve el alto de la imagen
180
         */
181
        public double getHeight() {
182
                return file.height;
183
        }
184

    
185
        /**
186
         * Obtiene el extent de la ?ltima ventana seleccionada.
187
         * @return Extent
188
         */
189
        public Extent getView() {
190
                return viewRequest;
191
        }
192

    
193
        /*
194
         * (non-Javadoc)
195
         * @see org.gvsig.fmap.driver.RasterDataset#setView(org.gvsig.fmap.raster.Extent)
196
         */
197
        public void setView(Extent e) {
198
                viewRequest = new ExtentImpl(e);
199
        }
200

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

226
                        stpBuffer[0] = (int)(p1.getX() + (-p3.getX()));
227
                        stpBuffer[1] = (int)(p1.getY() + (-p3.getY()));
228
                        stpBuffer[2] = stpBuffer[0] + w;
229
                        stpBuffer[3] = stpBuffer[1] + h;
230
                        return new int[]{w, h};
231
                }
232
                return new int[]{nWidth, nHeight};
233
        }*/
234

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

    
251
                Extent selectedExtent = new ExtentImpl(p1.getX(), p1.getY(), p2.getX(), p2.getY());
252

    
253
                setView(selectedExtent);
254
                int wPx = rasterBuf.getWidth();
255
                int hPx = rasterBuf.getHeight();
256
                int[] stpBuffer = new int[]{0, 0 , wPx, hPx};
257

    
258
                loadBuffer(viewRequest, wPx, hPx, rasterBuf, bandList, stpBuffer);
259

    
260
                return rasterBuf;
261
        }
262

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

    
278
                Point2D p1 = new Point2D.Double(ulx, uly);
279
                Point2D p2 = new Point2D.Double(lrx, lry);
280
                try {
281
                        externalTransformation.inverseTransform(p1, p1);
282
                        externalTransformation.inverseTransform(p2, p2);
283
                        ownTransformation.transform(p1, p1);
284
                        ownTransformation.transform(p2, p2);
285
                } catch (NoninvertibleTransformException e) {
286
                        throw new RasterDriverException("Noninvertible transform");
287
                }
288

    
289
                Extent selectedExtent = new ExtentImpl(p1.getX(), p1.getY(), p2.getX(), p2.getY());
290

    
291
                setView(selectedExtent);
292
                int wPx = rasterBuf.getWidth();
293
                int hPx = rasterBuf.getHeight();
294
                int[] stpBuffer = new int[]{0, 0 , wPx, hPx};
295

    
296
                //TODO: FUNCIONALIDAD: Implementar adjustToExtent = false
297
                /*if(!adjustToExtent){
298
                                         int[] wh = calcStepBuffer(selectedExtent, wPx, hPx, stpBuffer);
299
                                         if(x < 0)
300
                                                 x  = 0;
301
                                         if(y < 0)
302
                                                 y  = 0;
303
                                         readData(buf, bandList, x, y, wh[0], wh[1], wh[0], wh[1], 0, 0, stpBuffer);
304
                                         return;
305
                        }*/
306

    
307
                loadBuffer(viewRequest, wPx, hPx, rasterBuf, bandList, stpBuffer);
308

    
309
                return rasterBuf;
310
        }
311

    
312
        /*
313
         * (non-Javadoc)
314
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(int, int, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.Buffer)
315
         */
316
        public Buffer getWindowRaster(int x, int y, int w, int h, BandList bandList, Buffer rasterBuf) throws InterruptedException, RasterDriverException {
317
                Point2D init = this.rasterToWorld(new Point2D.Double(x, y));
318
                Point2D end = this.rasterToWorld(new Point2D.Double(x + w, y + h));
319
                Extent selectedExtent = new ExtentImpl(init.getX(), init.getY(), end.getX(), end.getY());
320
                setView(selectedExtent);
321
                int[] stpBuffer = new int[]{0, 0 , w, h};
322

    
323
                loadBuffer(viewRequest, w, h, rasterBuf, bandList, stpBuffer);
324
                return rasterBuf;
325
        }
326

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

    
346
                //TODO: FUNCIONALIDAD: Implementar adjustToExtent = false
347

    
348
                loadBuffer(viewRequest, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
349
                return rasterBuf;
350
        }
351

    
352
        /*
353
         * (non-Javadoc)
354
         * @see org.gvsig.fmap.driver.RasterDataset#getWindowRaster(int, int, int, int, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.Buffer)
355
         */
356
        public Buffer getWindowRaster(int x, int y, int w, int h, int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf) throws InterruptedException, RasterDriverException {
357
                Point2D init = this.rasterToWorld(new Point2D.Double(x, y));
358
                Point2D end = this.rasterToWorld(new Point2D.Double(x + w, y + h));
359
                Extent selectedExtent = new ExtentImpl(init.getX(), init.getY(), end.getX(), end.getY());
360
                setView(selectedExtent);
361
                int[] stpBuffer = new int[]{0, 0 , bufWidth, bufHeight};
362

    
363
                loadBuffer(viewRequest, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
364
                return rasterBuf;
365
        }
366

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

    
390
                        //Leemos el raster desde la librer?a
391

    
392
                        //Extent ext = getExtent();//new Extent(file.originX, file.originY, file.originX + (file.width * file.cellIncrementX), file.originY + (file.height * file.cellIncrementY));
393
                        //selectedExtent = RasterUtilities.calculateAdjustedView(selectedExtent, ext);
394
                        //selectedExtent = RasterUtilities.calculateAdjustedView(selectedExtent, this.getAffineTransform(), new Dimension(file.width, file.height));
395
                        int[] readBandsFromECW = new int[Math.max(file.numBands, 3)];
396
                        int[] readBands = new int[Math.max(file.numBands, 3)];
397

    
398

    
399
                        for(int i = 0; i < readBandsFromECW.length; i ++)
400
                                readBands[i] = -1;
401
                        int cont = 0;
402
                        for(int i = 0; i < file.numBands; i++) {
403
                                int[] bandsToDraw = bandList.getBand(i).getBufferBandListToDraw();
404
                                if(bandsToDraw != null) {
405
                                        for(int j = 0; j < bandsToDraw.length; j++){
406
                                                readBandsFromECW[cont] = i;
407
                                                readBands[cont] = i;
408
                                                cont ++;
409
                                        }
410
                                }
411

    
412
                        }
413

    
414
                        if(task.getEvent() != null)
415
                                task.manageEvent(task.getEvent());
416

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

    
420
                        /*if((selectedExtent.width()/file.cellIncrementX) || (selectedExtent.height()/file.cellIncrementY)) {
421

422
                        }*/
423

    
424
                        if(bufWidth > Math.round(Math.abs(selectedExtent.width()/file.cellIncrementX)))
425
                                bufWidth = (int)Math.round(Math.abs(selectedExtent.width()/file.cellIncrementX));
426
                        if(bufHeight > Math.round(Math.abs(selectedExtent.height()/file.cellIncrementY)))
427
                                bufHeight = (int)Math.round(Math.abs(selectedExtent.height()/file.cellIncrementY));
428
                        file.setView(file.numBands, readBandsFromECW, selectedExtent.minX(), selectedExtent.maxY(), selectedExtent.maxX(), selectedExtent.minY(), bufWidth, bufHeight);
429
                        
430
                        //Escribimos el raster sobre un Buffer
431
                        int[] pRGBArray = new int[bufWidth];
432
                        drawRGB(rasterBuf, pRGBArray, readBandsFromECW, bandList, task);
433

    
434
                }catch(JNCSInvalidSetViewException exc){
435
                        throw new RasterDriverException("Error setting coords");
436
                }catch (JNCSFileNotOpenException e) {
437
                        throw new RasterDriverException("Error opening file");
438
                }catch (JNCSException ex) {
439
                        throw new RasterDriverException("Error reading data");
440
                }
441

    
442
        }
443

    
444

    
445
        private void drawRGB(Buffer rasterBuf, int[] pRGBArray, int[] readBands, BandList bandList, RasterTask task) throws JNCSException, InterruptedException {
446
                int bandR = readBands[0];
447
                int bandG = (readBands.length > 1) ? readBands[1] : -1;
448
                int bandB = (readBands.length > 2) ? readBands[2] : -1;
449

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

    
510
                }
511
                //********* END caso especial que resuelve Bug#1 **********************
512

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

    
528
                if(task.getEvent() != null)
529
                        task.manageEvent(task.getEvent());
530

    
531
                if(bandR >= 0 && bandG >= 0) {
532
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
533
                                try {
534
                                        file.readLineRGBA(pRGBArray);
535
                                        for(int col = 0; col < pRGBArray.length; col ++){
536
                                                rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
537
                                                rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
538
                                        }
539
                                } catch (JNCSException exc) {
540
                                }
541
                        }
542
                        return;
543
                }
544

    
545
                if(task.getEvent() != null)
546
                        task.manageEvent(task.getEvent());
547

    
548
                if(bandR >= 0){
549
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
550
                                try {
551
                                        file.readLineRGBA(pRGBArray);
552
                                        for(int col = 0; col < pRGBArray.length; col ++)
553
                                                rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
554
                                } catch (JNCSException exc) {
555
                                }
556
                        }
557
                        return;
558
                }
559

    
560
                if(task.getEvent() != null)
561
                        task.manageEvent(task.getEvent());
562

    
563
        }
564

    
565
        public void reProject(ICoordTrans rp) {
566
        }
567

    
568
        /*
569
         * (non-Javadoc)
570
         * @see org.gvsig.raster.dataset.RasterDataset#getBlockSize()
571
         */
572
        public int getBlockSize() {
573
                return 0;
574
        }
575

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

    
584
                Point2D begin = rasterToWorld(new Point2D.Double(0, line));
585
                Point2D end = rasterToWorld(new Point2D.Double(file.width, line + 1));
586
                int[] readBandsFromECW = new int[file.numBands];
587
                if(file.numBands <= 3) {
588
                        for(int i = 0; i < file.numBands; i++)
589
                                readBandsFromECW[i] = i;
590
                }else {
591
                        readBandsFromECW[0] = band;
592
                }
593

    
594
                Extent e = new ExtentImpl(begin.getX(), begin.getY(), end.getX(), end.getY());
595

    
596
                try {
597
                        int[] value = new int[file.width];
598
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, 1);
599
                        file.readLineRGBA(value);
600

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

    
634
                return null;
635
        }
636

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

    
646
                if((pos + blockHeight) > file.height)
647
                        blockHeight = Math.abs(file.height - pos);
648

    
649
                Point2D begin = rasterToWorld(new Point2D.Double(0, pos));
650
                Point2D end = rasterToWorld(new Point2D.Double(file.width, pos + blockHeight));
651
                int[] readBandsFromECW = new int[file.numBands];
652

    
653
                for(int i = 0; i < file.numBands; i++)
654
                        readBandsFromECW[i] = i;
655

    
656
                byte[][][] buf = new byte[file.numBands][blockHeight][file.width];
657
                Extent e = new ExtentImpl(begin.getX(), begin.getY(), end.getX(), end.getY());
658
                e = RasterUtilities.calculateAdjustedView(getExtent(), e);
659

    
660
                try {
661
                        int[] value = new int[file.width];
662
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, blockHeight);
663

    
664
                        if(file.numBands <= 3) {
665
                                for (int row = 0; row < blockHeight; row++) {
666
                                        file.readLineRGBA(value);
667
                                        switch(getDataType()[0]) {
668
                                        case Buffer.TYPE_BYTE:
669
                                                for(int col = 0; col < file.width; col ++) {
670
                                                        buf[0][row][col] = (byte)(((value[col] & 0x00ff0000) >> 16) & 0xff);
671
                                                        buf[1][row][col] = (byte)(((value[col] & 0x0000ff00) >> 8) & 0xff);
672
                                                        buf[2][row][col] = (byte)((value[col] & 0x000000ff) & 0xff);
673
                                                }
674
                                                break;
675
                                        }
676
                                }
677

    
678
                                if(task.getEvent() != null)
679
                                        task.manageEvent(task.getEvent());
680

    
681
                        } else {
682
                                //TODO: FUNCIONALIDAD: file.numBands > 3
683
                        }
684

    
685
                        //TODO: FUNCIONALIDAD: Ecw con otro tipo de dato != Byte
686
                } catch (JNCSFileNotOpenException e1) {
687
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
688
                } catch (JNCSInvalidSetViewException e1) {
689
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
690
                } catch (JNCSException e1) {
691
                        throw new RasterDriverException("Error la lectura de datos ecw");
692
                }
693

    
694
                return buf;
695
        }
696

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

    
705
                Point2D begin = new Point2D.Double(x, y);
706
                Point2D end = new Point2D.Double(x + 1, y + 1);
707

    
708
                ownTransformation.transform(begin, begin);
709
                ownTransformation.transform(end, end);
710

    
711
                int[] readBandsFromECW = new int[file.numBands];
712
                if(file.numBands <= 3){
713
                        for(int i = 0; i < file.numBands; i++)
714
                                readBandsFromECW[i] = i;
715
                }else{
716
                        readBandsFromECW[0] = band;
717
                }
718

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

    
741
        public void refreshUpdate(int arg0, int arg1, double arg2, double arg3, double arg4, double arg5) {
742
        }
743

    
744
        public void refreshUpdate(int arg0, int arg1, int arg2, int arg3, int arg4, int arg5) {
745
        }
746

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

    
767
        /**
768
         * Asigna el objeto que contiene que contiene la interpretaci?n de
769
         * color por banda
770
         * @param DatasetColorInterpretation
771
         */
772
        public void setColorInterpretation(DatasetColorInterpretation colorInterpretation){
773
                this.colorInterpretation = colorInterpretation;
774
        }
775

    
776
        /*
777
         * (non-Javadoc)
778
         * @see org.gvsig.raster.dataset.RasterDataset#getOverviewCount(int)
779
         */
780
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
781
                if(band >= getBandCount())
782
                        throw new BandAccessException("Wrong band");
783
                return 0;
784
        }
785

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

    
796
        /*
797
         * (non-Javadoc)
798
         * @see org.gvsig.raster.dataset.RasterDataset#getOverviewWidth(int, int)
799
         */
800
        public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
801
                if (band >= getBandCount())
802
                        throw new BandAccessException("Wrong band");
803
                return 0;
804
        }
805

    
806
        /*
807
         * (non-Javadoc)
808
         * @see org.gvsig.raster.dataset.RasterDataset#overviewsSupport()
809
         */
810
        public boolean overviewsSupport() {
811
                return false;
812
        }
813

    
814
        /*
815
         * (non-Javadoc)
816
         * @see org.gvsig.raster.driver.GeoData#getStringProjection()
817
         */
818
        public String getStringProjection() throws RasterDriverException{
819
                return file.projection;
820
        }
821

    
822
        /*
823
         * (non-Javadoc)
824
         * @see org.gvsig.raster.driver.GeoData#getStringProjection()
825
         */
826
        public String getWktProjection() {
827
                //System.err.println("======>" + file.projection);
828
                return null;
829
        }
830
}