Statistics
| Revision:

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

History | View | Annotate | Download (29.6 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.raster.dataset.io;
20

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

    
27
import org.cresques.cts.ICoordTrans;
28
import org.cresques.cts.IProjection;
29
import org.gvsig.raster.dataset.BandAccessException;
30
import org.gvsig.raster.dataset.BandList;
31
import org.gvsig.raster.dataset.FileNotOpenException;
32
import org.gvsig.raster.dataset.GeoInfo;
33
import org.gvsig.raster.dataset.IBuffer;
34
import org.gvsig.raster.dataset.InvalidSetViewException;
35
import org.gvsig.raster.dataset.NotSupportedExtensionException;
36
import org.gvsig.raster.dataset.RasterDataset;
37
import org.gvsig.raster.dataset.io.rmf.ParsingException;
38
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation;
39
import org.gvsig.raster.datastruct.Extent;
40
import org.gvsig.raster.datastruct.Transparency;
41
import org.gvsig.raster.process.RasterTask;
42
import org.gvsig.raster.process.RasterTaskQueue;
43
import org.gvsig.raster.util.RasterUtilities;
44
import org.gvsig.raster.util.extensionPoints.ExtensionPoint;
45

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

    
58
        private JNCSFile                                       file = null;
59

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

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

    
72
        public static void register() {
73
                ExtensionPoint point = ExtensionPoint.getExtensionPoint("RasterReader");
74
                point.register("ecw", ErmapperDriver.class);
75
                point.register("jp2", ErmapperDriver.class);
76
        }
77

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
298
                return rasterBuf;
299
        }
300

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

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

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

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

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

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

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

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

    
433

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

    
439
                //********* caso especial que resuelve Bug#1 **********************
440
                if(file.numBands == 3 && bandList.getDrawableBandsCount() < 3) {
441
                        for(int i = 0; i < 3; i ++) {
442
                                int[] b = bandList.getBand(i).getBufferBandListToDraw();
443
                                if(b != null){
444
                                        bandG = 1; bandR = 0; bandB = 2;
445
                                }
446
                        }
447
                }
448
                if(file.numBands == 3 && bandR == bandG && bandG == bandB) { //caso especial que resuelve Bug#1
449
                        for(int i = 0; i < 3; i ++) {
450
                                int[] b = bandList.getBand(i).getBufferBandListToDraw();
451
                                if(b != null) {
452
                                        if(i == 0) {
453
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
454
                                                        try {
455
                                                                file.readLineRGBA(pRGBArray);
456
                                                                for(int col = 0; col < pRGBArray.length; col ++){
457
                                                                        rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
458
                                                                        rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
459
                                                                        rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
460
                                                                }
461
                                                        } catch (JNCSException exc) {
462
                                                                System.err.println("Error: fuera de rango en la petici?n de l?nea");
463
                                                        }
464
                                                }
465
                                                return;
466
                                        }
467
                                        if(i == 1) {
468
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
469
                                                        try {
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
                                                        } catch (JNCSException exc) {
477
                                                                System.err.println("Error: fuera de rango en la petici?n de l?nea");
478
                                                        }
479
                                                }
480
                                                return;
481
                                        }
482
                                        if(i == 2) {
483
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
484
                                                        try {
485
                                                                file.readLineRGBA(pRGBArray);
486
                                                                for(int col = 0; col < pRGBArray.length; col ++){
487
                                                                        rasterBuf.setElem(line, col, bandR, (byte)(pRGBArray[col] & 0x000000ff));
488
                                                                        rasterBuf.setElem(line, col, bandG, (byte)(pRGBArray[col] & 0x000000ff));
489
                                                                        rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
490
                                                                }
491
                                                        } catch (JNCSException exc) {
492
                                                                System.err.println("Error: fuera de rango en la petici?n de l?nea");
493
                                                        }
494
                                                }
495
                                                return;
496
                                        }
497
                                }
498
                                if(task.getEvent() != null)
499
                                        task.manageEvent(task.getEvent());
500
                        }
501

    
502
                }
503
                //********* END caso especial que resuelve Bug#1 **********************
504

    
505
                if(bandR >= 0 && bandG >= 0 && bandB >= 0) {
506
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
507
                                try {
508
                                        file.readLineRGBA(pRGBArray);
509
                                        for(int col = 0; col < pRGBArray.length; col ++){
510
                                                rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
511
                                                rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
512
                                                rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
513
                                        }
514
                                } catch (JNCSException exc) {
515
                                        exc.printStackTrace();
516
                                }
517
                        }
518
                        return;
519
                }
520

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

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

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

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

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

    
556
        }
557

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

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

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

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

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

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

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

    
627
                return null;
628
        }
629

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

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

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

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

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

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

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

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

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

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

    
689
                return buf;
690
        }
691

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

    
700
                Point2D begin = new Point2D.Double(x, y);
701
                Point2D end = new Point2D.Double(x + 1, y + 1);
702
                
703
                ownTransformation.transform(begin, begin);
704
                ownTransformation.transform(end, end);
705
                
706
                int[] readBandsFromECW = new int[file.numBands];
707
                if(file.numBands <= 3) {
708
                        for(int i = 0; i < file.numBands; i++)
709
                                readBandsFromECW[i] = i;
710
                } else {
711
                        readBandsFromECW[0] = band;
712
                }
713

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

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

    
739
        public void refreshUpdate(int arg0, int arg1, int arg2, int arg3, int arg4, int arg5) {
740
        }
741
        
742
        /**
743
         * Obtiene el objeto que contiene que contiene la interpretaci?n de 
744
         * color por banda
745
         * @return
746
         */
747
        public DatasetColorInterpretation getColorInterpretation(){
748
                if(colorInterpr == null) {
749
                        colorInterpr = new DatasetColorInterpretation();
750
                        colorInterpr.initColorInterpretation(getBandCount());
751
                        if(getBandCount() == 1)
752
                                colorInterpr.setColorInterpValue(0, DatasetColorInterpretation.GRAY_BAND);
753
                        if(getBandCount() >= 3) {
754
                                colorInterpr.setColorInterpValue(0, DatasetColorInterpretation.RED_BAND);
755
                                colorInterpr.setColorInterpValue(1, DatasetColorInterpretation.GREEN_BAND);
756
                                colorInterpr.setColorInterpValue(2, DatasetColorInterpretation.BLUE_BAND);
757
                        }
758
                }
759
                return colorInterpr;
760
        }
761
        
762
        /**
763
         * Asigna el objeto que contiene que contiene la interpretaci?n de 
764
         * color por banda
765
         * @param DatasetColorInterpretation
766
         */
767
        public void setColorInterpretation(DatasetColorInterpretation colorInterpretation){
768
                this.colorInterpretation = colorInterpretation;
769
        }
770
        
771
        /*
772
         * (non-Javadoc)
773
         * @see org.gvsig.raster.dataset.RasterDataset#getOverviewCount(int)
774
         */
775
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
776
                if(band >= getBandCount())
777
                        throw new BandAccessException("Wrong band");
778
                return 0;
779
        }
780
        
781
        /*
782
         * (non-Javadoc)
783
         * @see org.gvsig.raster.dataset.RasterDataset#getOverviewWidth(int, int)
784
         */
785
        public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
786
                if (band >= getBandCount())
787
                        throw new BandAccessException("Wrong band");
788
                return 0;
789
        }
790

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

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

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