Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1010 / libraries / libCq_CMS_praster / src / org / cresques / io / MemoryRasterDriver.java @ 12804

History | View | Annotate | Download (27.1 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 * 
4
 * Copyright (C) 2004-5. 
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 * 
22
 * cresques@gmail.com
23
 */
24
package org.cresques.io;
25

    
26
import java.awt.Image;
27
import java.awt.geom.Point2D;
28
import java.awt.image.BufferedImage;
29

    
30
import org.cresques.cts.ICoordTrans;
31
import org.cresques.io.data.BandList;
32
import org.cresques.io.data.RasterBuf;
33
import org.cresques.io.datastruct.Metadata;
34
import org.cresques.io.exceptions.NotSupportedExtensionException;
35
import org.cresques.io.exceptions.SupersamplingNotSupportedException;
36
import org.cresques.px.Extent;
37

    
38

    
39
/**
40
 * Driver to read data from memory. This can be created with a RasterBuf 
41
 * and its bounding box.
42
 * @author Nacho Brodin (nachobrodin@gmail.com)
43
 *
44
 */
45
public class MemoryRasterDriver extends GeoRasterFile {
46

    
47
        protected Extent v = null;
48
        protected RasterBuf buf = null;
49
        
50
        public MemoryRasterDriver(){
51
                super(null, null);
52
        }
53
        
54
        /**
55
         * Constructor
56
         * @param buf Data buffer
57
         * @param ext bounding box
58
         * @throws NotSupportedExtensionException
59
         */
60
        public MemoryRasterDriver(RasterBuf buf, Extent ext)throws NotSupportedExtensionException{
61
                super(null, null);
62
                this.extent = this.requestExtent = ext;
63
                this.buf = buf;
64
                this.name = "";
65
                load();
66
                bandCount = buf.getBandCount(); 
67
                if ( bandCount > 2) {
68
                        setBand(RED_BAND,   0);
69
                        setBand(GREEN_BAND, 1);
70
                        setBand(BLUE_BAND,  2);
71
                } else
72
                        setBand(RED_BAND|GREEN_BAND|BLUE_BAND, 0);
73

    
74
                //Obtenemos el tipo de dato de gdal y lo convertimos el de RasterBuf
75
                setDataType(org.cresques.util.Utilities.getRasterBufTypeFromGdalType(buf.getDataType()));
76
        }
77
        
78
        /**
79
         * 
80
         */
81
        public GeoFile load() {
82
                return this;
83
        }
84
        
85
        /**
86
         * 
87
         */
88
        public void close() {
89
                
90
        }
91
        
92
        /**
93
         * Asigna a cada banda R,G o B una banda de la imagen
94
         */
95
        public void setBand(int flag, int bandNr) {
96
                super.setBand(flag, bandNr);
97
        }
98
        
99
        /**
100
         * Asigna el extent de la vista actual. 
101
         */
102
        public void setView(Extent e) { 
103
                v = new Extent(e.minX(), e.minY(), e.maxX(), e.maxY());        
104
        }
105
                
106
         /**
107
         * Calcula la transformaci?n que se produce sobre la vista cuando la imagen tiene un fichero .rmf
108
         * asociado. En Gdal el origen de coordenadas en Y es el valor m?nimo y crece hasta el m?ximo. De la
109
         * misma forma calcula la matriz de transformaci?n de la cabecera del fichero o del world file asociado
110
         * @param originX Origen de la imagen en la coordenada X
111
         * @param originY Origen de la imagen en la coordenada Y
112
         */
113
        public void setExtentTransform(double originX, double originY, double psX, double psY) {                
114
                transformRMF.setToTranslation(originX, originY);
115
                transformRMF.scale(psX, psY);
116
        }
117
        
118
        /**
119
         * Obtiene extent de la vista actual
120
         */
121
        public Extent getView() { 
122
                return v; 
123
        }
124
        
125
        /**
126
         * Obtiene la anchura del fichero
127
         */
128
        public int getWidth() {        
129
                return buf.getWidth(); 
130
        }
131
        
132
        /**
133
         * Obtiene la altura del fichero
134
         */
135
        public int getHeight() { 
136
                return buf.getHeight();
137
        }
138

    
139
        /* (non-Javadoc)
140
         * @see org.cresques.io.GeoRasterFile#reProject(org.cresques.cts.ICoordTrans)
141
         */
142
        public void reProject(ICoordTrans rp) {
143
                // TODO Auto-generated method stub
144
        }
145
        
146
        /**
147
         * Obtiene la orientaci?n de la imagen a partir del signo del tama?o de pixel para poder
148
         * asignarlo en el setView. Esto es util para poder conocer como debe leerse la image, 
149
         * de abajo a arriba, de arriba a abajo, de izquierda a derecha o de derecha a izquierda. 
150
         * La posici?n habitual es la que el pixel size en X es positivo y en Y negativo leyendose 
151
         * en este caso las X de menor a mayor y las Y de mayor a menor. Los casos posibles son:
152
         * <UL>
153
         * <LI><B>X > 0; Y < 0;</B> {true, false}</LI>
154
         * <LI><B>X > 0; Y > 0;</B> {true, true}</LI>
155
         * <LI><B>X < 0; Y > 0;</B> {false, true}</LI>
156
         * <LI><B>X < 0; Y < 0;</B> {false, false}</LI>
157
         * </UL>
158
         *  
159
         * @return
160
         */
161
        private boolean[] getOrientation(){
162
                boolean[] orientation = {true, false};
163
                return orientation;
164
        }
165
        
166
        /* (non-Javadoc)
167
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans)
168
         */
169
        public Image updateImage(int width, int height, ICoordTrans rp) {
170
                return null;
171
        }
172
        
173
        public RasterBuf getRaster(int width, int height, ICoordTrans rp) {
174
                int line;
175
                RasterBuf raster = null;
176
                                        
177
                Point2D begin = worldToRaster(new Point2D.Double(v.minX(), v.maxY()));
178
                Point2D end = worldToRaster(new Point2D.Double(v.maxX(), v.minY()));
179
                
180
                //Comprobaciones
181
                if(begin.getX() < 0)
182
                        begin.setLocation(0, begin.getY());
183
                if(begin.getY() > buf.getHeight())
184
                        begin.setLocation(begin.getX(), buf.getHeight());
185
                if(end.getY() < 0)
186
                        end.setLocation(begin.getX(), 0);
187
                if(end.getX() > buf.getWidth())
188
                        begin.setLocation(buf.getWidth(), begin.getY());
189
                
190
                int alpha = (getAlpha() & 0xff) << 24;
191
                
192
                try {
193
                        //Esta funci?n se usa para la renderizaci?n, por eso se crean 4 bandas a pi?on fijo
194
                        raster = new RasterBuf(getDataType(), width, height, 4, true);
195
                
196
                        setBand(RED_BAND,   rBandNr);
197
                        setBand(GREEN_BAND, gBandNr);
198
                        setBand(BLUE_BAND,  bBandNr);
199
                        
200
                        double w = Math.abs(end.getX() - begin.getX());
201
                        double h = Math.abs(end.getY() - begin.getY());
202
                        double stepX = w / ((double)width);
203
                        double stepY = h / ((double)height);
204
                        int endLine = (((int)end.getY() + 1) < buf.getHeight()) ? ((int)end.getY() + 1) : buf.getHeight();
205
                        int endCol = (((int)end.getX() + 1) < buf.getWidth()) ? ((int)end.getX() + 1) : buf.getWidth();
206
                        double initY = begin.getY() - (int)begin.getY();
207
                        double initX = begin.getX() - (int)begin.getX(); 
208
                        
209
                        switch(getDataType()){
210
                        case RasterBuf.TYPE_BYTE: 
211
                                //writeRasterBufByte(begin, end, alpha, raster, width, height, w, h, stepX, stepY, endLine, endCol, initY, initX);
212
                                break;
213
                        case RasterBuf.TYPE_SHORT:;        
214
                                writeRasterBufShort(begin, end, alpha, raster, width, height, w, h, stepX, stepY, endLine, endCol, initY, initX);
215
                                break;
216
                        case RasterBuf.TYPE_INT:
217
                                writeRasterBufInt(begin, end, alpha, raster, width, height, w, h, stepX, stepY, endLine, endCol, initY, initX);
218
                                break;
219
                        case RasterBuf.TYPE_FLOAT:
220
                                writeRasterBufFloat(begin, end, alpha, raster, width, height, w, h, stepX, stepY, endLine, endCol, initY, initX);
221
                                break;
222
                        case RasterBuf.TYPE_DOUBLE:
223
                                writeRasterBufDouble(begin, end, alpha, raster, width, height, w, h, stepX, stepY, endLine, endCol, initY, initX);
224
                                break;
225
                        case RasterBuf.TYPE_UNDEFINED:break;
226
                        }
227
                        
228
                } catch (Exception e) {
229
                        e.printStackTrace();
230
                }
231
                
232
                return raster;
233
        }
234
        
235
        private void writeRasterBufShort(Point2D begin, Point2D end, int alpha, RasterBuf rb, int widthImg, int heightImg,
236
                        double w, double h, double stepX, double stepY, int endLine, int endCol, double l, double initX){
237
                short[][][] pRGBArray = new short[4][(int)Math.ceil(h) + 1][(int)Math.ceil(w) + 1];
238

    
239
                int x = 0, y = 0;
240
                for (int line = (int)begin.getY(); line < endLine ; line ++) {
241
                        x = 0;
242
                        for (int col = (int)begin.getX(); col < endCol; col ++){
243
                                pRGBArray[0][y][x] = buf.getElemShort(line, col, rBandNr);
244
                                pRGBArray[1][y][x] = buf.getElemShort(line, col, gBandNr);
245
                                pRGBArray[2][y][x] = buf.getElemShort(line, col, bBandNr);
246
                                pRGBArray[3][y][x] = (short)alpha;
247
                                x ++;
248
                        }
249
                        y ++;
250
                }
251
                for (int line = 0; line < heightImg; line ++) {
252
                        double c = initX;
253
                        for (int col = 0; col < widthImg; col ++){
254
                                try{ 
255
                                        for(int band = 0; band < 4; band ++)
256
                                                rb.setElemShort(line, col, band, pRGBArray[band][(int)l][(int)c]); 
257
                                }catch(ArrayIndexOutOfBoundsException e){}
258
                                c += stepX;
259
                        }
260
                        l += stepY;
261
                }
262
                
263
        }
264
        
265
        private void writeRasterBufInt(Point2D begin, Point2D end, int alpha, RasterBuf rb, int widthImg, int heightImg,
266
                        double w, double h, double stepX, double stepY, int endLine, int endCol, double l, double initX){
267
                int[][][] pRGBArray = new int[4][(int)Math.ceil(h) + 1][(int)Math.ceil(w) + 1];
268

    
269
                int x = 0, y = 0;
270
                for (int line = (int)begin.getY(); line < endLine ; line ++) {
271
                        x = 0;
272
                        for (int col = (int)begin.getX(); col < endCol; col ++){
273
                                pRGBArray[0][y][x] = buf.getElemInt(line, col, rBandNr);
274
                                pRGBArray[1][y][x] = buf.getElemInt(line, col, gBandNr);
275
                                pRGBArray[2][y][x] = buf.getElemInt(line, col, bBandNr);
276
                                pRGBArray[3][y][x] = (short)alpha;
277
                                x ++;
278
                        }
279
                        y ++;
280
                }
281
                for (int line = 0; line < heightImg; line ++) {
282
                        double c = initX;
283
                        for (int col = 0; col < widthImg; col ++){
284
                                try{ 
285
                                        for(int band = 0; band < 4; band ++)
286
                                                rb.setElemInt(line, col, band, pRGBArray[band][(int)l][(int)c]); 
287
                                }catch(ArrayIndexOutOfBoundsException e){}
288
                                c += stepX;
289
                        }
290
                        l += stepY;
291
                }
292
                
293
        }
294
        
295
        private void writeRasterBufFloat(Point2D begin, Point2D end, int alpha, RasterBuf rb, int widthImg, int heightImg,
296
                        double w, double h, double stepX, double stepY, int endLine, int endCol, double l, double initX){
297
                float[][][] pRGBArray = new float[4][(int)Math.ceil(h) + 1][(int)Math.ceil(w) + 1];
298

    
299
                int x = 0, y = 0;
300
                for (int line = (int)begin.getY(); line < endLine ; line ++) {
301
                        x = 0;
302
                        for (int col = (int)begin.getX(); col < endCol; col ++){
303
                                pRGBArray[0][y][x] = buf.getElemFloat(line, col, rBandNr);
304
                                pRGBArray[1][y][x] = buf.getElemFloat(line, col, gBandNr);
305
                                pRGBArray[2][y][x] = buf.getElemFloat(line, col, bBandNr);
306
                                pRGBArray[3][y][x] = (float)alpha;
307
                                x ++;
308
                        }
309
                        y ++;
310
                }
311
                for (int line = 0; line < heightImg; line ++) {
312
                        double c = initX;
313
                        for (int col = 0; col < widthImg; col ++){
314
                                try{ 
315
                                        for(int band = 0; band < 4; band ++)
316
                                                rb.setElemFloat(line, col, band, pRGBArray[band][(int)l][(int)c]); 
317
                                }catch(ArrayIndexOutOfBoundsException e){}
318
                                c += stepX;
319
                        }
320
                        l += stepY;
321
                }
322
                
323
        }
324
        
325
        private void writeRasterBufDouble(Point2D begin, Point2D end, int alpha, RasterBuf rb, int widthImg, int heightImg,
326
                        double w, double h, double stepX, double stepY, int endLine, int endCol, double l, double initX){
327
                double[][][] pRGBArray = new double[4][(int)Math.ceil(h) + 1][(int)Math.ceil(w) + 1];
328

    
329
                int x = 0, y = 0;
330
                for (int line = (int)begin.getY(); line < endLine ; line ++) {
331
                        x = 0;
332
                        for (int col = (int)begin.getX(); col < endCol; col ++){
333
                                pRGBArray[0][y][x] = buf.getElemDouble(line, col, rBandNr);
334
                                pRGBArray[1][y][x] = buf.getElemDouble(line, col, gBandNr);
335
                                pRGBArray[2][y][x] = buf.getElemDouble(line, col, bBandNr);
336
                                pRGBArray[3][y][x] = (float)alpha;
337
                                x ++;
338
                        }
339
                        y ++;
340
                }
341
                for (int line = 0; line < heightImg; line ++) {
342
                        double c = initX;
343
                        for (int col = 0; col < widthImg; col ++){
344
                                try{ 
345
                                        for(int band = 0; band < 4; band ++)
346
                                                rb.setElemDouble(line, col, band, pRGBArray[band][(int)l][(int)c]); 
347
                                }catch(ArrayIndexOutOfBoundsException e){}
348
                                c += stepX;
349
                        }
350
                        l += stepY;
351
                }
352
                
353
        }
354
        
355
        /**
356
         * Asigna al objeto Image los valores con los dato de la imagen contenidos en el 
357
         * vector de enteros.
358
         * @param image        imagen con los datos actuales
359
         * @param startX        inicio de la posici?n en X dentro de la imagen
360
         * @param startY        inicio de la posici?n en X dentro de la imagen
361
         * @param w        Ancho de la imagen
362
         * @param h        Alto de la imagen
363
         * @param rgbArray        vector que contiene la banda que se va a sustituir
364
         * @param offset        desplazamiento
365
         * @param scansize        tama?o de imagen recorrida por cada p
366
         */
367
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, 
368
                         int offset, int scansize) {
369
                image.setRGB(startX, startY, w, h, rgbArray, offset, scansize);
370
        }
371
        
372
        /**
373
         * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores 
374
         * con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
375
         * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
376
         * banda correspondiente a flags es sustituida por los datos del vector.
377
         * @param image        imagen con los datos actuales
378
         * @param startX        inicio de la posici?n en X dentro de la imagen
379
         * @param startY        inicio de la posici?n en X dentro de la imagen
380
         * @param w        Ancho de la imagen
381
         * @param h        Alto de la imagen
382
         * @param rgbArray        vector que contiene la banda que se va a sustituir
383
         * @param offset        desplazamiento
384
         * @param scansize        tama?o de imagen recorrida por cada paso
385
         * @param flags        banda que se va a sustituir (Ctes de GeoRasterFile)
386
         */
387
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, 
388
                         int offset, int scansize, int flags) {
389
                int [] line = new int[rgbArray.length]; 
390
                image.getRGB(startX, startY, w, h, line, offset, scansize);
391
                if (flags == GeoRasterFile.RED_BAND)
392
                        for (int i=0; i<line.length; i++)
393
                                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
394
                else if (flags == GeoRasterFile.GREEN_BAND)
395
                        for (int i=0; i<line.length; i++)
396
                                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
397
                else if (flags == GeoRasterFile.BLUE_BAND)
398
                        for (int i=0; i<line.length; i++)
399
                                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
400
                image.setRGB(startX, startY, w, h, line, offset, scansize);
401
        }
402
        
403
        /**
404
         * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores 
405
         * con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
406
         * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
407
         * banda correspondiente a flags es sustituida por los datos del vector.
408
         * @param image        imagen con los datos actuales
409
         * @param startX        inicio de la posici?n en X dentro de la imagen
410
         * @param startY        inicio de la posici?n en X dentro de la imagen
411
         * @param w        Ancho de la imagen
412
         * @param h        Alto de la imagen
413
         * @param rgbArray        vector que contiene la banda que se va a sustituir
414
         * @param offset        desplazamiento
415
         * @param scansize        tama?o de imagen recorrida por cada paso
416
         * @param origBand        Banda origen del GeoRasterFile
417
         * @param destBandFlag        banda que se va a sustituir (Ctes de GeoRasterFile)
418
         */
419
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray, 
420
                         int offset, int scansize, int origBand, int destBandFlag) {
421
                int [] line = new int[rgbArray.length]; 
422
                image.getRGB(startX, startY, w, h, line, offset, scansize);
423
                if (origBand == 0 && destBandFlag == GeoRasterFile.RED_BAND)
424
                        for (int i=0; i<line.length; i++)
425
                                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
426
                else if (origBand == 1 && destBandFlag == GeoRasterFile.GREEN_BAND)
427
                        for (int i=0; i<line.length; i++)
428
                                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
429
                else if (origBand == 2 && destBandFlag == GeoRasterFile.BLUE_BAND)
430
                        for (int i=0; i<line.length; i++)
431
                                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
432
                
433
                else if (origBand == 0 && destBandFlag == GeoRasterFile.GREEN_BAND)
434
                        for (int i=0; i<line.length; i++)
435
                                line[i] = (line[i] & 0xffff00ff) | ((rgbArray[i] & 0x00ff0000) >> 8) ;
436
                else if (origBand == 0 && destBandFlag == GeoRasterFile.BLUE_BAND)
437
                        for (int i=0; i<line.length; i++)
438
                                line[i] = (line[i] & 0xffffff00) | ((rgbArray[i] & 0x00ff0000) >> 16);
439
                else if (origBand == 1 && destBandFlag == GeoRasterFile.RED_BAND)
440
                        for (int i=0; i<line.length; i++)
441
                                line[i] = (line[i] & 0xff00ffff) | ((rgbArray[i] & 0x0000ff00) << 8);
442
                
443
                else if (origBand == 1 && destBandFlag == GeoRasterFile.BLUE_BAND)
444
                        for (int i=0; i<line.length; i++)
445
                                line[i] = (line[i] & 0xffffff00) | ((rgbArray[i] & 0x0000ff00) >> 8);
446
                else if (origBand == 2 && destBandFlag == GeoRasterFile.RED_BAND)
447
                        for (int i=0; i<line.length; i++)
448
                                line[i] = (line[i] & 0xff00ffff) | ((rgbArray[i] & 0x000000ff) << 16);
449
                else if (origBand == 2 && destBandFlag == GeoRasterFile.GREEN_BAND)
450
                        for (int i=0; i<line.length; i++)
451
                                line[i] = (line[i] & 0xffff00ff) | ((rgbArray[i] & 0x000000ff) << 8);
452
                image.setRGB(startX, startY, w, h, line, offset, scansize);
453
        }
454
                
455
        /* (non-Javadoc)
456
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans, java.awt.Image, int, int)
457
         */
458
        public Image updateImage(int width, int height, ICoordTrans rp, Image img, int origBand, int destBandFlag)throws SupersamplingNotSupportedException{
459
                int pRGBArray[] = null;
460

    
461
                if(width<=0)width=1;
462
                if(height<=0)height=1;
463
                Point2D begin = worldToRaster(new Point2D.Double(v.minX(), v.maxY()));
464
                Point2D end = worldToRaster(new Point2D.Double(v.maxX(), v.minY()));
465
                
466
                //Comprobaciones
467
                if(begin.getX() < 0)
468
                        begin.setLocation(0, begin.getY());
469
                if(begin.getY() > buf.getHeight())
470
                        begin.setLocation(begin.getX(), buf.getHeight());
471
                if(end.getY() < 0)
472
                        end.setLocation(begin.getX(), 0);
473
                if(end.getX() > buf.getWidth())
474
                        begin.setLocation(buf.getWidth(), begin.getY());
475
                
476
                int alpha = (getAlpha() & 0xff) << 24;
477
                                
478
                //TODO: Falta orientaci?n
479
                try {
480
                        setBand(RED_BAND,   rBandNr);
481
                        setBand(GREEN_BAND, gBandNr);
482
                        setBand(BLUE_BAND,  bBandNr);
483
                        if(img!=null){
484
                                writeImage(begin, end, alpha, origBand, destBandFlag, img, width, height, true);
485
                                return img;
486
                        }else{
487
                                Image image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
488
                                writeImage(begin, end, alpha, origBand, destBandFlag, image, width, height, false);
489
                                return image;
490
                        }
491
                } catch (Exception e) {
492
                        e.printStackTrace();
493
                }
494
                
495
                return img;
496
        }
497
                        
498
        private void writeImage(Point2D begin, Point2D end, int alpha, int origBand, int destBandFlag, 
499
                        Image img, int widthImg, int heightImg, boolean existsImg){
500
                double w = Math.abs(end.getX() - begin.getX());
501
                double h = Math.abs(end.getY() - begin.getY());
502
                double stepX = w / ((double)widthImg);
503
                double stepY = h / ((double)heightImg);
504
                                
505
                int x = 0, y = 0;
506
                int[][] pRGBArray = new int[(int)Math.ceil(h) + 1][(int)Math.ceil(w) + 1];
507
                int endLine = (((int)end.getY() + 1) < buf.getHeight()) ? ((int)end.getY() + 1) : buf.getHeight();
508
                int endCol = (((int)end.getX() + 1) < buf.getWidth()) ? ((int)end.getX() + 1) : buf.getWidth();
509
                if(buf.getDataType() == RasterBuf.TYPE_BYTE){
510
                        for (int line = (int)begin.getY(); line < endLine ; line ++) {
511
                                x = 0;
512
                                for (int col = (int)begin.getX(); col < endCol; col ++){
513
                                        int value = (alpha + 
514
                                                                ((buf.getElemByte(line, col, rBandNr)& 0xff) << 16) +
515
                                                                ((buf.getElemByte(line, col, gBandNr)& 0xff) << 8) +
516
                                                                (buf.getElemByte(line, col, bBandNr)& 0xff));
517
                                        pRGBArray[y][x] = value;
518
                                        x++;
519
                                }
520
                                y++;
521
                        }
522
                        int[] rgbImgLine = new int[widthImg];
523
                        double l = begin.getY() - (int)begin.getY();
524
                        double initX = begin.getX() - (int)begin.getX(); 
525
                        for (int line = 0; line < heightImg; line ++) {
526
                                double c = initX;
527
                                for (int col = 0; col < widthImg; col ++){
528
                                        try{
529
                                                rgbImgLine[col] = pRGBArray[(int)l][(int)c];
530
                                        }catch(ArrayIndexOutOfBoundsException e){}
531
                                        c += stepX;
532
                                }
533
                                if(existsImg)
534
                                        setRGBLine((BufferedImage) img, 0, line, widthImg, 1, rgbImgLine, 0, widthImg, origBand, destBandFlag);
535
                                else
536
                                        setRGBLine((BufferedImage) img, 0, line, widthImg, 1, rgbImgLine, 0, widthImg);
537
                                l += stepY;
538
                        }
539
                        
540
                }
541
        }
542
        
543
        /* (non-Javadoc)
544
         * @see org.cresques.io.GeoRasterFile#getData(int, int, int)
545
         */
546
        public Object getData(int x, int y, int band) {
547
                if(buf.getDataType() == RasterBuf.TYPE_BYTE){
548
                        return new Byte(buf.getElemByte(y, x, band));
549
                }else if(buf.getDataType() == RasterBuf.TYPE_SHORT){
550
                        return new Short(buf.getElemShort(y, x, band));
551
                }else if(buf.getDataType() == RasterBuf.TYPE_INT){
552
                        return new Integer(buf.getElemInt(y, x, band));
553
                }else if(buf.getDataType() == RasterBuf.TYPE_FLOAT){
554
                        return new Float(buf.getElemFloat(y, x, band));
555
                }else if(buf.getDataType() == RasterBuf.TYPE_DOUBLE){
556
                        return new Double(buf.getElemDouble(y, x, band));
557
                }
558
                return null;
559
        }
560
        
561
        /**
562
         * Devuelve los datos de una ventana solicitada
563
         * @param ulX        coordenada X superior izda.
564
         * @param ulY        coordenada Y superior derecha.
565
         * @param sizeX        tama?o en X de la ventana.
566
         * @param sizeY tama?o en Y de la ventana.
567
         * @param band        Banda solicitada.
568
         */
569
        public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band){
570
                
571
                return null;
572
        }
573
        
574
        public RasterBuf getWindowRaster(double x, double y, double w, double h, BandList bandList, RasterBuf rasterBuf) {                
575
                Extent selectedExtent = new Extent(x, y, x + w, y - h);
576
                setView(selectedExtent);
577
                
578
                int width = 0;
579
                int height = 0;
580
                if(getTransform() != null){
581
                        width = (int)Math.abs(selectedExtent.width() / getTransform()[1]);//(int)(selectedExtent.width() * file.width) / extent.width();
582
                        height = (int)Math.abs(selectedExtent.height() / getTransform()[5]);
583
                }else{
584
                        width = (int)Math.abs(selectedExtent.width());
585
                        height = (int)Math.abs(selectedExtent.height());
586
                }
587
                                
588
                /*try {
589
                        file.readWindow(rasterBuf, bandList, x, y, width, height);
590
                } catch (Exception e) {
591
                        e.printStackTrace();
592
                }*/
593
                
594
                return rasterBuf;
595
        }
596
        
597
        /*
598
         *  (non-Javadoc)
599
         * @see org.gvsig.fmap.driver.GeoRasterFile#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer)
600
         */
601
        public RasterBuf getWindowRaster(double minX, double minY, double maxX, double maxY, int bufWidth, int bufHeight, BandList bandList, RasterBuf rasterBuf) {                
602
                Extent selectedExtent = new Extent(minX, minY, maxX, maxY);
603
                setView(selectedExtent);
604
                
605
                double width = 0;
606
                double height = 0;
607
                if(getTransform() != null){
608
                        width = (double)(Math.abs(selectedExtent.width() / getTransform()[1]));//(int)(selectedExtent.width() * file.width) / extent.width();
609
                        height = (double)(Math.abs(selectedExtent.height() / getTransform()[5]));
610
                }else{
611
                        width = (double)Math.abs(selectedExtent.width());
612
                        height = (double)Math.abs(selectedExtent.height());
613
                }
614
                                
615
                /*try {
616
                        file.readWindow(rasterBuf, bandList, minX, maxY, maxX, minY, width, height, bufWidth, bufHeight);
617
                } catch (Exception e) {
618
                        e.printStackTrace();
619
                }*/
620
                
621
                return rasterBuf;
622
        }
623
        
624
        public RasterBuf getWindowRaster(int x, int y, int w, int h, BandList bandList, RasterBuf rasterBuf) {
625
                for(int iBand = 0; iBand < buf.getBandCount(); iBand ++){
626
                        int[] drawableBands = bandList.getBufferBandToDraw(null, iBand);
627
                        if(drawableBands == null || (drawableBands.length == 1 && drawableBands[0] == -1))
628
                                continue;        
629
                        if(buf.getDataType() == RasterBuf.TYPE_BYTE){
630
                                for(int drawBands = 0; drawBands < drawableBands.length; drawBands++){
631
                                        for(int line = y; line < (y + h); line ++)
632
                                                for(int col = x; col < (x + w); col ++)
633
                                                        rasterBuf.setElemByte((line - y), (col - x), drawableBands[drawBands], buf.getElemByte(line, col, drawableBands[drawBands]));
634
                                }
635
                        }else if(buf.getDataType() == RasterBuf.TYPE_SHORT){
636
                                for(int drawBands = 0; drawBands < drawableBands.length; drawBands++){
637
                                        for(int line = y; line < (y + h); line ++)
638
                                                for(int col = x; col < (x + w); col ++)
639
                                                        rasterBuf.setElemShort((line - y), (col - x), drawableBands[drawBands], buf.getElemShort(line, col, drawableBands[drawBands]));
640
                                }
641
                        }else if(buf.getDataType() == RasterBuf.TYPE_INT){
642
                                for(int drawBands = 0; drawBands < drawableBands.length; drawBands++){
643
                                        for(int line = y; line < (y + h); line ++)
644
                                                for(int col = x; col < (x + w); col ++)
645
                                                        rasterBuf.setElemInt((line - y), (col - x), drawableBands[drawBands], buf.getElemInt(line, col, drawableBands[drawBands]));
646
                                }
647
                        }else if(buf.getDataType() == RasterBuf.TYPE_FLOAT){
648
                                for(int drawBands = 0; drawBands < drawableBands.length; drawBands++){
649
                                        for(int line = y; line < (y + h); line ++)
650
                                                for(int col = x; col < (x + w); col ++)
651
                                                        rasterBuf.setElemFloat((line - y), (col - x), drawableBands[drawBands], buf.getElemFloat(line, col, drawableBands[drawBands]));
652
                                }
653
                        }else if(buf.getDataType() == RasterBuf.TYPE_DOUBLE){
654
                                for(int drawBands = 0; drawBands < drawableBands.length; drawBands++){
655
                                        for(int line = y; line < (y + h); line ++)
656
                                                for(int col = x; col < (x + w); col ++)
657
                                                        rasterBuf.setElemDouble((line - y), (col - x), drawableBands[drawBands], buf.getElemDouble(line, col, drawableBands[drawBands]));
658
                                }
659
                        }
660
                }
661
                return rasterBuf;
662
        }
663
        
664
        public RasterBuf getWindowRasterWithNoData(double x, double y, double w, double h, BandList bandList, RasterBuf rasterBuf) {
665
                return null;
666
        }
667
        
668
        /**
669
         * Obtiene la zona (Norte / Sur)
670
         * @return true si la zona es norte y false si es sur
671
         */
672
        
673
        public boolean getZone(){
674
                return false;
675
        }
676
        
677
        /**
678
         *Devuelve el n?mero de zona UTM
679
         *@return N?mero de zona 
680
         */
681
        
682
        public int getUTM(){
683
                return 0;        
684
        }
685
        
686
        /**
687
         * Obtiene el sistema de coordenadas geograficas
688
         * @return Sistema de coordenadas geogr?ficas
689
         */
690
        public String getGeogCS(){
691
                return new String("");        
692
        }
693
        
694
        /**
695
         * Devuelve el tama?o de bloque
696
         * @return Tama?o de bloque
697
         */
698
        public int getBlockSize(){
699
                return 0;
700
        }
701
        
702
        /**
703
         * Obtiene el objeto que contiene los metadatos
704
         */
705
        public Metadata getMetadata() {
706
                return null;
707
        }
708
        
709
        /**
710
         * Obtiene el flag que dice si la imagen est? o no georreferenciada
711
         * @return true si est? georreferenciada y false si no lo est?.
712
         */
713
        public boolean isGeoreferenced() {
714
                return (this.extent != null);
715
        }
716
    
717
        /**
718
         * Informa de si el driver ha supersampleado en el ?ltimo dibujado. Es el driver el que colocar?
719
         * el valor de esta variable cada vez que dibuja. 
720
         * @return true si se ha supersampleado y false si no se ha hecho.
721
         */
722
        public boolean isSupersampling() {
723
                return false;
724
        }
725
        
726
        /**
727
         * Obtiene los par?metros de la transformaci?n af?n que corresponde con los elementos de
728
         * un fichero tfw.
729
         * <UL> 
730
         * <LI>[1]tama?o de pixel en X</LI>
731
         * <LI>[2]rotaci?n en X</LI>
732
         * <LI>[4]rotaci?n en Y</LI>
733
         * <LI>[5]tama?o de pixel en Y</LI>
734
         * <LI>[0]origen en X</LI>
735
         * <LI>[3]origen en Y</LI>
736
         * </UL>
737
         * Este m?todo debe ser reimplementado por el driver si tiene esta informaci?n. En principio
738
         * Gdal es capaz de proporcionarla de esta forma.
739
         * 
740
         * En caso de que exista fichero .rmf asociado al raster pasaremos de la informaci?n de georreferenciaci?n
741
         * del .tfw y devolveremos la que est? asociada al rmf
742
         * @return vector de double con los elementos de la transformaci?n af?n.
743
         */
744
        public double[] getTransform(){
745
                        return null;
746
        }
747
        
748
        /*
749
         *  (non-Javadoc)
750
         * @see org.gvsig.fmap.driver.GeoRasterFile#rasterToWorld(java.awt.geom.Point2D)
751
         */
752
        public Point2D rasterToWorld(Point2D pt) {
753
                double x = extent.minX() + ((pt.getX() * (extent.maxX() - extent.minX())) / ((double) buf.getWidth()));
754
                double y = extent.maxY() - ((pt.getY() * (extent.maxY() - extent.minY())) / ((double) buf.getHeight()));
755
                Point2D ptRes = new Point2D.Double(x, y);
756
                return ptRes;
757
        }
758
        
759
        /*
760
         *  (non-Javadoc)
761
         * @see org.gvsig.fmap.driver.GeoRasterFile#worldToRaster(java.awt.geom.Point2D)
762
         */
763
        public Point2D worldToRaster(Point2D pt) {
764
                double x = (((double) buf.getWidth()) / (extent.maxX() - extent.minX())) * (pt.getX() - extent.minX());
765
                double y = (((double) buf.getHeight()) / (extent.maxY() - extent.minY())) * (extent.maxY() - pt.getY());
766
                Point2D ptRes = new Point2D.Double(x, y);
767
                return ptRes;
768
        }
769
        
770
        /**
771
         * @return Returns the dataType.
772
         */
773
        public int getDataType() {
774
                return buf.getDataType();
775
        }
776
}
777

    
778

    
779