Statistics
| Revision:

svn-gvsig-desktop / tags / Root_piloto3d / libraries / libCq CMS for java.old / src / org / cresques / px / PxRaster.java @ 9537

History | View | Annotate | Download (53.1 KB)

1 2 luisw
/*
2 2809 nacho
 * 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 2 luisw
 */
24
package org.cresques.px;
25
26 4998 nacho
import java.awt.Color;
27
import java.awt.Component;
28
import java.awt.Graphics2D;
29
import java.awt.Image;
30 8765 jjdelcerro
import java.awt.geom.AffineTransform;
31 4998 nacho
import java.awt.geom.GeneralPath;
32 8765 jjdelcerro
import java.awt.geom.NoninvertibleTransformException;
33 4998 nacho
import java.awt.geom.Point2D;
34
import java.awt.image.BufferedImage;
35
import java.awt.image.DataBuffer;
36
import java.awt.image.ImageObserver;
37
import java.util.ArrayList;
38
import java.util.Date;
39
import java.util.Vector;
40
41 2809 nacho
import org.cresques.cts.ICoordTrans;
42
import org.cresques.cts.IProjection;
43 5978 nacho
import org.cresques.filter.RasterBuf;
44
import org.cresques.filter.RasterFilterStack;
45
import org.cresques.filter.RasterFilterStackManager;
46
import org.cresques.filter.RasterToImageFilter;
47 9056 nacho
import org.cresques.filter.enhancement.TransparencyRange;
48 2809 nacho
import org.cresques.geo.Projected;
49
import org.cresques.geo.ViewPortData;
50 5991 nacho
import org.cresques.io.EcwFile;
51
import org.cresques.io.GdalFile;
52 2809 nacho
import org.cresques.io.GeoRasterFile;
53 4998 nacho
import org.cresques.io.SupersamplingNotSupportedException;
54 9056 nacho
import org.cresques.io.datastruct.Statistic;
55 2809 nacho
56 2 luisw
57 2809 nacho
/**
58
 *
59
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
60
 * @author Nacho Brodin (brodin_ign@gva.es)
61
 *
62
 */
63 30 luisw
public class PxRaster extends PxObj implements Projected {
64 2809 nacho
    protected GeoRasterFile[] geoFile = null;
65
    protected ImageObserver component = null;
66
    Vector pts = null;
67 2 luisw
68 2809 nacho
    // Soporte para n bandas, visibles de 3 en 3, en ficheros separados
69
    //protected GeoRasterFile [] colorBand = null;
70
    protected int rBand = 1;
71 2 luisw
72 2809 nacho
    // Soporte para n bandas, visibles de 3 en 3, en ficheros separados
73
    //protected GeoRasterFile [] colorBand = null;
74
    protected int gBand = 2;
75 2 luisw
76 2809 nacho
    // Soporte para n bandas, visibles de 3 en 3, en ficheros separados
77
    //protected GeoRasterFile [] colorBand = null;
78
    protected int bBand = 3;
79 9056 nacho
    public Statistic stats = new Statistic();
80 2809 nacho
    int transparente = 0x10ffff80;
81
    String vName = null;
82
    protected boolean pintaMarco = false; //true;
83
    IProjection proj = null;
84
    protected Extent extentOrig = null;
85
    ICoordTrans rp = null;
86
    public RasterFilterStack filterStack = new RasterFilterStack(stats);
87
    private BandSwitch bandSwitch = new BandSwitch();
88
    private boolean firstPxRaster = true;
89
    private final double percentFilterInit = 0.02;
90
    private RasterFilterStackManager stackManager = null;
91
    private Image geoImage = null;
92
    private ViewPortData lastViewPort = null;
93 8765 jjdelcerro
94
    /**
95
     * Variable usada por el draw para calcular el n?mero de pixeles a leer de un
96
     * raster rotado.
97
     */
98
    private double[] adjustedRotedExtent = null;
99 2 luisw
100 2809 nacho
    /**
101
     * Constructor.
102
     * @param component
103
     */
104
    public PxRaster(ImageObserver component) {
105
        this.component = component;
106
    }
107 2 luisw
108 2809 nacho
    /**
109
     * Contructor para un solo fichero
110
     * @param proj        Proyeccci?n
111
     * @param fname Nombre del fichero
112
     * @param component
113
     */
114
    public PxRaster(IProjection proj, String fname, ImageObserver component) {
115
        geoFile = new GeoRasterFile[1];
116
        geoFile[0] = GeoRasterFile.openFile(proj, fname); //loadECW(fname);
117
        geoFile[0].setUpdatable((Component) component);
118
        this.proj = proj;
119
        this.component = component;
120
        setExtent(geoFile[0].getExtent());
121 8765 jjdelcerro
        setExtentForRequest(geoFile[0].getExtentForRequest());
122 2809 nacho
        geoFile[0].setView(geoFile[0].getExtent());
123
        extentOrig = extent;
124
        bandSwitch.addFile(geoFile[0]);
125 2 luisw
126 2809 nacho
        if (geoFile[0].getBandCount() >= 3) {
127
            setBand(GeoRasterFile.RED_BAND, 0);
128
            setBand(GeoRasterFile.GREEN_BAND, 1);
129
            setBand(GeoRasterFile.BLUE_BAND, 2);
130
        } else if (geoFile[0].getBandCount() == 2) {
131
            setBand(GeoRasterFile.RED_BAND, 0);
132
            setBand(GeoRasterFile.GREEN_BAND, 1);
133
            setBand(GeoRasterFile.BLUE_BAND, 1);
134
        } else if (geoFile[0].getBandCount() == 1) {
135
            //setBand(GeoRasterFile.RED_BAND|GeoRasterFile.GREEN_BAND|GeoRasterFile.BLUE_BAND, 0);
136
            setBand(GeoRasterFile.RED_BAND, 0);
137
            setBand(GeoRasterFile.GREEN_BAND, 0);
138
            setBand(GeoRasterFile.BLUE_BAND, 0);
139
        }
140
    }
141 34 luisw
142 2809 nacho
    /**
143
     * Constructor para multiples ficheros
144
     */
145
    public PxRaster(IProjection proj, String[] fnames, ImageObserver component) {
146
        this.proj = proj;
147
        this.component = component;
148
        geoFile = new GeoRasterFile[fnames.length];
149 13 luisw
150 2809 nacho
        for (int i = 0; i < geoFile.length; i++) {
151
            geoFile[i] = GeoRasterFile.openFile(proj, fnames[i]); //loadECW(fname);
152
            geoFile[i].setUpdatable((Component) component);
153
            setExtent(geoFile[i].getExtent());
154 8765 jjdelcerro
            setExtentForRequest(geoFile[i].getExtentForRequest());
155 2809 nacho
            geoFile[i].setView(geoFile[i].getExtent());
156
            bandSwitch.addFile(geoFile[i]);
157
        }
158 30 luisw
159 2809 nacho
        //geoFile = geoFile[0];
160
        extentOrig = extent;
161 30 luisw
162 2809 nacho
        if ((fnames.length >= 3) || (geoFile[0].getBandCount() > 2)) {
163
            setBand(GeoRasterFile.RED_BAND, 0);
164
            setBand(GeoRasterFile.GREEN_BAND, 1);
165
            setBand(GeoRasterFile.BLUE_BAND, 2);
166
        } else {
167
            setBand(GeoRasterFile.RED_BAND | GeoRasterFile.GREEN_BAND |
168
                    GeoRasterFile.BLUE_BAND, 0);
169
        }
170
    }
171
172
    public PxRaster(GeoRasterFile eFile, ImageObserver component, Extent view) {
173
        geoFile = new GeoRasterFile[1];
174
        geoFile[0] = eFile; //loadECW(fname);
175
        geoFile[0].setUpdatable((Component) component);
176
        setProjection(geoFile[0].getProjection());
177
        this.component = component;
178
179
        setExtent(geoFile[0].getExtent());
180 8765 jjdelcerro
        setExtentForRequest(geoFile[0].getExtentForRequest());
181 3084 nacho
        if(view != null){
182
                geoFile[0].setView(view); //geoFile.getExtent());
183
                extentOrig = extent;
184
                bandSwitch.addFile(eFile);
185
186
                if (geoFile[0].getBandCount() >= 3) {
187
                    setBand(GeoRasterFile.RED_BAND, 0);
188
                    setBand(GeoRasterFile.GREEN_BAND, 1);
189
                    setBand(GeoRasterFile.BLUE_BAND, 2);
190
                } else if (geoFile[0].getBandCount() == 2) {
191
                    setBand(GeoRasterFile.RED_BAND, 0);
192
                    setBand(GeoRasterFile.GREEN_BAND, 1);
193
                    setBand(GeoRasterFile.BLUE_BAND, 1);
194
                } else if (geoFile[0].getBandCount() == 1) {
195
                    //setBand(GeoRasterFile.RED_BAND|GeoRasterFile.GREEN_BAND|GeoRasterFile.BLUE_BAND, 0);
196
                    setBand(GeoRasterFile.RED_BAND, 0);
197
                    setBand(GeoRasterFile.GREEN_BAND, 0);
198
                    setBand(GeoRasterFile.BLUE_BAND, 0);
199
                }
200 2809 nacho
        }
201
    }
202
203
    /**
204
     * A?ade un GeoRasterFile al PxRaster
205
     * @param fileName Nombre del fichero
206
     */
207 9056 nacho
    public GeoRasterFile addFile(String fileName) {
208 2809 nacho
        if (geoFile != null) {
209
            GeoRasterFile[] listFiles = new GeoRasterFile[geoFile.length + 1];
210
211
            for (int i = 0; i < geoFile.length; i++)
212
                listFiles[i] = geoFile[i];
213
214
            listFiles[geoFile.length] = GeoRasterFile.openFile(proj, fileName);
215
            listFiles[geoFile.length].setUpdatable((Component) component);
216
            setExtent(listFiles[geoFile.length].getExtent());
217 8765 jjdelcerro
            setExtentForRequest(listFiles[geoFile.length].getExtentForRequest());
218 2809 nacho
            listFiles[geoFile.length].setView(listFiles[geoFile.length].getExtent());
219
            bandSwitch.addFile(listFiles[geoFile.length]);
220
            geoFile = listFiles;
221 9056 nacho
            return listFiles[geoFile.length - 1];
222 2809 nacho
        } else {
223
            System.err.println("PxRaster.addFile(): Imagen no cargada.");
224 9056 nacho
            return null;
225 2809 nacho
        }
226
    }
227
228
    /**
229
     * Devuelve el Extent de un fichero son necesidad de a?adirlo
230
     * al PxRaster
231
     * @param fileName
232
     * @return
233
     */
234
    public Extent testExtentFile(String fileName) {
235
        GeoRasterFile grf = GeoRasterFile.openFile(proj, fileName);
236
237
        return grf.getExtent();
238
    }
239
240
    /**
241
     * Obtiene el valor del pixel del Image en la posici?n wcx,wcy
242
     * @param wcx Posici?n x
243
     * @param wcx Posici?n y
244
     * @return valor de pixel
245
     */
246
    public int[] getPixel(double wcx, double wcy) {
247
        if (geoImage != null) {
248
            int ptox = 0;
249
            int ptoy = 0;
250
251
            try {
252
                //Extent de la imagen completa
253
                Extent extOrtofoto = geoFile[0].getExtent();
254
255
                //Variables q definen el extent del objeto image
256
                double minx = 0;
257
258
                //Variables q definen el extent del objeto image
259
                double miny = 0;
260
261
                //Variables q definen el extent del objeto image
262
                double maxx = 0;
263
264
                //Variables q definen el extent del objeto image
265
                double maxy = 0;
266
267
                if (lastViewPort.getExtent().getMin().getX() < extOrtofoto.minX()) {
268
                    minx = extOrtofoto.minX();
269
                } else {
270
                    minx = lastViewPort.getExtent().getMin().getX();
271
                }
272
273
                if (lastViewPort.getExtent().getMax().getX() > extOrtofoto.maxX()) {
274
                    maxx = extOrtofoto.maxX();
275
                } else {
276
                    maxx = lastViewPort.getExtent().getMax().getX();
277
                }
278
279
                if (lastViewPort.getExtent().getMin().getY() < extOrtofoto.minY()) {
280
                    miny = extOrtofoto.minY();
281
                } else {
282
                    miny = lastViewPort.getExtent().getMin().getY();
283
                }
284
285
                if (lastViewPort.getExtent().getMax().getY() > extOrtofoto.maxY()) {
286
                    maxy = extOrtofoto.maxY();
287
                } else {
288
                    maxy = lastViewPort.getExtent().getMax().getY();
289
                }
290
291
                //Comprobamos que estemos dentro del extent del Image
292
                if ((wcx < minx) || (wcx > maxx) || (wcy < miny) ||
293
                        (wcy > maxy)) {
294
                    int[] res = { -1, -1, -1, -1 };
295
296
                    return res;
297
                }
298
299
                //Pasamos a coordenadas del Image las coordenadas del mundo real
300
                int w = ((BufferedImage) geoImage).getWidth();
301
                int h = ((BufferedImage) geoImage).getHeight();
302
                double wcw = maxx - minx;
303
                double wch = maxy - miny;
304
                ptox = (int) (((wcx - minx) * w) / wcw);
305
                ptoy = (int) (((wcy - miny) * h) / wch);
306
307
                //Obtenemos el pto seleccionado del Image y extraemos el RGB
308
                int px = ((BufferedImage) geoImage).getRGB(ptox, h - ptoy);
309
                int[] values = new int[4];
310
                values[0] = ((px & 0xff000000) >> 24);
311
                values[1] = ((px & 0x00ff0000) >> 16);
312
                values[2] = ((px & 0x0000ff00) >> 8);
313
                values[3] = (px & 0x000000ff);
314
315
                return values;
316
            } catch (ArrayIndexOutOfBoundsException e) {
317
                e.printStackTrace();
318
            }
319
        }
320
321
        return null;
322
    }
323
324
    /**
325
     * Elimina un GeoRasterFile al PxRaster
326
     * @param finaName Nombre del fichero
327
     */
328 9056 nacho
    public GeoRasterFile delFile(String fileName) {
329 2809 nacho
        if (geoFile != null) {
330
            Vector grfTemp = new Vector();
331
332 9056 nacho
            GeoRasterFile grfDelete = null;
333
334 2809 nacho
            for (int i = 0; i < geoFile.length; i++) {
335
                if (!fileName.endsWith(geoFile[i].getName())) {
336
                    grfTemp.add(geoFile[i]);
337 9056 nacho
                }else
338
                        grfDelete = geoFile[i];
339 2809 nacho
            }
340
341
            GeoRasterFile[] listFiles = new GeoRasterFile[grfTemp.size()];
342
343
            for (int i = 0; i < listFiles.length; i++)
344
                listFiles[i] = (GeoRasterFile) grfTemp.get(i);
345
346
            //Lo eliminamos del bandSwitch
347
            for (int i = 0; i < geoFile.length; i++) {
348
                if (fileName.endsWith(geoFile[i].getName())) {
349
                    bandSwitch.removeFile(geoFile[i]);
350
                }
351
            }
352
353
            geoFile = listFiles;
354 9056 nacho
            return grfDelete;
355 2809 nacho
        } else {
356
            System.err.println("PxRaster.addFile(): Imagen no cargada.");
357 9056 nacho
            return null;
358 2809 nacho
        }
359
    }
360
361
    /**
362
     * Obtiene el tama?o de bloque
363
     * @return
364
     */
365
    public int getBlockSize() {
366
        return geoFile[0].getBlockSize();
367
    }
368
369
    /**
370
     *
371
     * @return
372
     */
373
    public GeoRasterFile[] getGeoFiles() {
374
        return geoFile;
375
    }
376
377
    public GeoRasterFile getGeoFile() {
378
        return geoFile[0];
379
    }
380
381
    /**
382
     * Obtiene el n?mero de bandas del PxRaster. Si ls longitud de geoFile es 1 quiere
383
     * decir que hay un solo fichero que contiene todas las bandas. Si hay m?s de un geoFile
384
     * el n?mero de bandas ser? la suma de todas las bandas de las im?genes que forman
385
     * el geoFile
386
     * @return N?mero de bandas
387
     */
388
    public int getBandCount() {
389
        return bandSwitch.getBandCount();
390
    }
391
392
    /**
393
     * Obtiene el tipo de dato del primer GeoRasterFile
394
     * @return
395
     */
396
    public int getDataType() {
397
        if (geoFile != null) {
398
            return geoFile[0].getDataType();
399
        } else {
400
            System.err.println("PxRaster.getDataType(): Imagen no cargada.");
401
        }
402
403
        return 0;
404
    }
405
406
    /**
407
     * Obtiene el vector de GeoRasterFile que componen el PxRaster
408
     * @return vector GeoRasterFile
409
     */
410
    public GeoRasterFile[] getFiles() {
411
        return geoFile;
412
    }
413
414
    /**
415
     * Asocia un colorBand al rojo, verde o azul.
416
     * @param flag cual (o cuales) de las bandas.
417
     * @param nBand        que colorBand
418
     */
419
    public void setBand(int flag, int nBand) {
420
        bandSwitch.setBand(flag, nBand);
421
    }
422
423
    /**
424
     * Obtiene la posici?n del fichero asignado a la banda
425
     * que se le pasa por par?metro
426
     * @return
427
     */
428
    public int getPosFile(int flag) {
429
        if (flag == GeoRasterFile.RED_BAND) {
430
            return bandSwitch.getBandR().getPos();
431
        } else if (flag == GeoRasterFile.GREEN_BAND) {
432
            return bandSwitch.getBandG().getPos();
433
        } else if (flag == GeoRasterFile.BLUE_BAND) {
434
            return bandSwitch.getBandB().getPos();
435
        } else {
436
            return -1;
437
        }
438
    }
439
440
    /**
441
     * Devuelve el colorBand activo en la banda especificada.
442
     * @param flag banda.
443
     */
444
    public int getBand(int flag) {
445
        if (flag == GeoRasterFile.RED_BAND) {
446
            return bandSwitch.getBandR().getBand();
447
        } else if (flag == GeoRasterFile.GREEN_BAND) {
448
            return bandSwitch.getBandG().getBand();
449
        } else if (flag == GeoRasterFile.BLUE_BAND) {
450
            return bandSwitch.getBandB().getBand();
451
        } else {
452
            return -1;
453
        }
454
    }
455
456
    /**
457
     * @param pm
458
     */
459
    public void setDrawBorder(boolean pm) {
460
        pintaMarco = pm;
461
    }
462
463
    /**
464
     * Obtiene el nombre del fichero si solo hay uno
465
     * @return Nombre del fichero
466
     */
467
    public String getFName() {
468
        return geoFile[0].getName();
469
    }
470
471
    /**
472
     * Obtiene el nombre del fichero GeoRasterFile seleccionado
473
     * @param i        posici?n del GeoRasterFile
474
     * @return Nombre del fichero
475
     */
476
    public String getFName(int i) {
477
        if (geoFile != null) {
478
            if (i < geoFile.length) {
479
                return geoFile[i].getName();
480
            } else {
481
                return null;
482
            }
483
        } else {
484
            System.err.println("PxRaster.getFName(): Imagen no cargada.");
485
486
            return null;
487
        }
488
    }
489
490
    /**
491
     * Obtiene una lista de Strings con los nombres de todos los ficheros
492
     * que tiene el PxRaster
493
     * @return Lista de nombres
494
     */
495
    public String[] getLisName() {
496
        if (geoFile != null) {
497
            String[] list = new String[geoFile.length];
498
499
            for (int i = 0; i < geoFile.length; i++)
500
                list[i] = geoFile[i].getName();
501
502
            return list;
503
        } else {
504
            System.err.println("PxRaster.getListName(): Imagen no cargada.");
505
506
            return null;
507
        }
508
    }
509
510
    /**
511
     * Devuelve la anchura total del fichero, en pixeles.
512
     * @return ancho en pixeles
513
     */
514
    public int getFWidth() {
515
        if (geoFile != null) {
516
            return geoFile[0].getWidth();
517
        } else {
518
            System.err.println("PxRaster.getFWidth(): Imagen no cargada.");
519
520
            return 0;
521
        }
522
    }
523
524
    /**
525
     * Devuelve la anchura total del fichero, en pixeles.
526
     * @return ancho en pixeles
527
     */
528
    public int getFWidth(int i) {
529
        if (i < geoFile.length) {
530
            return geoFile[i].getWidth();
531
        } else {
532
            System.err.println("PxRaster.getFWidth(): Imagen no cargada.");
533
534
            return 0;
535
        }
536
    }
537
538
    /**
539
     * Devuelve la altura total del fichero, en pixeles.
540
     * @return alto en pixeles
541
     */
542
    public int getFHeight() {
543
        if (geoFile != null) {
544
            return geoFile[0].getHeight();
545
        } else {
546
            System.err.println("PxRaster.getFHeight(): Imagen no cargada.");
547
548
            return 0;
549
        }
550
    }
551
552
    /**
553
     * Devuelve la altura total del fichero, en pixeles.
554
     * @return alto en pixeles
555
     */
556
    public int getFHeight(int i) {
557
        if (i < geoFile.length) {
558
            return geoFile[i].getHeight();
559
        } else {
560
            System.err.println("PxRaster.getFHeight(): Imagen no cargada.");
561
562
            return 0;
563
        }
564
    }
565
566
    /**
567
     * Devuelve el n?mero de ficheros que componen el PxRaster
568
     * @return N?mero de ficheros
569
     */
570
    public int nFiles() {
571
        if (geoFile != null) {
572
            return geoFile.length;
573
        } else {
574
            return 0;
575
        }
576
    }
577
578
    /**
579
     * Activa o desactiva la transparencia de los ficheros que forman el PxRaster
580
     * @param t true o false para activar o desactivar transparencia
581
     */
582
    public void setTransparency(boolean t) {
583
        if (geoFile != null) {
584
            for (int i = 0; i < geoFile.length; i++) {
585
                geoFile[i].setTransparency(t);
586
            }
587
        } else {
588
            System.err.println("PxRaster.setTransparency(): Imagen no cargada.");
589
590
            return;
591
        }
592
    }
593
594
    /**
595
     * Pone la transparencia de los ficheros de la imagen a un valor
596
     * @param t Valor para la transparencia
597
     */
598
    public void setTransparency(int t) {
599
        if (geoFile != null) {
600
            for (int i = 0; i < geoFile.length; i++) {
601
                geoFile[i].setTransparency(t);
602
            }
603
        } else {
604
            System.err.println("PxRaster.setTransparency(): Imagen no cargada.");
605
606
            return;
607
        }
608
    }
609
610
    /**
611
     * Obtiene el alpha del primer fichero. Han de ser iguales en todos
612
     * los ficheros de la imagen.
613
     * @return alpha
614
     */
615
    public int getAlpha() {
616
        if (geoFile != null) {
617
            return geoFile[0].getAlpha();
618
        } else {
619
            System.err.println("PxRaster.getAlpha(): Imagen no cargada.");
620
621
            return 0;
622
        }
623
    }
624
625
    /**
626
     * Asigna el extent
627
     * @param e extent
628
     */
629
    public void setExtent(Extent e) {
630
        super.extent = e;
631 3084 nacho
        if(e != null){
632
                pts = new Vector();
633
                pts.add(proj.createPoint(e.minX(), e.minY()));
634
                pts.add(proj.createPoint(e.maxX(), e.minY()));
635
                pts.add(proj.createPoint(e.maxX(), e.maxY()));
636
                pts.add(proj.createPoint(e.minX(), e.maxY()));
637
        }
638 2809 nacho
    }
639
640
    /**
641 8765 jjdelcerro
     * Asigna el extent sobre el que se ajusta una petici?n para que esta no exceda el
642
     * extent m?ximo del raster. Para un raster sin rotar ser? igual al extent
643
     * pero para un raster rotado ser? igual al extent del raster como si no
644
     * tuviera rotaci?n. Esto ha de ser as? ya que la rotaci?n solo se hace sobre la
645
     * vista y las peticiones han de hacerse en coordenadas de la imagen sin shearing
646
     * aplicado.
647
     * @param Extent
648
     */
649
    public void setExtentForRequest(Extent e) {
650
        super.requestExtent = e;
651
        if(e != null){
652
                pts = new Vector();
653
                pts.add(proj.createPoint(e.minX(), e.minY()));
654
                pts.add(proj.createPoint(e.maxX(), e.minY()));
655
                pts.add(proj.createPoint(e.maxX(), e.maxY()));
656
                pts.add(proj.createPoint(e.minX(), e.maxY()));
657
        }
658
    }
659
660
    /**
661 2809 nacho
     * Cambia la vista (viewport) sobre el raster.
662
     *
663
     * @param v extent
664
     * @param vName nombre
665
     */
666
    public void setView(Extent v, String vName) {
667
        if (geoFile != null) {
668
            for (int i = 0; i < geoFile.length; i++) {
669
                geoFile[i].setView(v);
670
            }
671
        } else {
672
            System.err.println("PxRaster.setView(): Imagen no cargada.");
673
674
            return;
675
        }
676
677
        this.vName = vName;
678
    }
679
680
    /**
681
     * Obtiene la escala.
682
     *
683
     * @param width
684
     * @param height
685
     * @return
686
     */
687
    public double[] getScale(int width, int height) {
688
        double[] scale = new double[2];
689
690
        if (geoFile != null) {
691
            scale[0] = ((double) width) / geoFile[0].getView().width();
692
            scale[1] = ((double) height) / geoFile[0].getView().height();
693
694
            return scale;
695
        } else {
696
            System.err.println("PxRaster.getScale(): Imagen no cargada.");
697
698
            return null;
699
        }
700
    }
701
702
    /**
703 8765 jjdelcerro
     * Transforma la petici?n que est? en coordenadas de la imagen sin rotar a coordenadas de la imagen rotada
704
     * para que sea posible el calculo de la caja m?nima de inclusi?n. La coordenada superior izquierda de esta
705
     * ser? la que se use para posicionar la imagen sobre el graphics aplicandole la transformaci?n de la la vista.
706
     * @param v
707
     * @return
708
     */
709
    private Point2D coordULRotateRaster(double[] v){
710
        double vx = v[0];
711
        double vy = v[1];
712
        double vx2 = v[2];
713
        double vy2 = v[3];
714
            if (geoFile != null) {
715
                double[] transf = geoFile[0].getTransform();
716
717
                   if(transf != null && (transf[2] != 0 || transf[4] != 0)){
718
                           //La transformaci?n se hace en base a una esquina que varia con el signo del
719
                           //pixel size.
720
                           double ptoDesplX = (transf[1] > 0)?requestExtent.minX():requestExtent.maxX();
721
                    double ptoDesplY = (transf[5] < 0)?requestExtent.maxY():requestExtent.minY();
722
723
                    Point2D ul = new Point2D.Double(vx - ptoDesplX, vy2 - ptoDesplY);
724
                    Point2D ur = new Point2D.Double(vx2 - ptoDesplX, vy2 - ptoDesplY);
725
                    Point2D ll = new Point2D.Double(vx - ptoDesplX, vy - ptoDesplY);
726
                    Point2D lr = new Point2D.Double(vx2 - ptoDesplX, vy - ptoDesplY);
727
728
                           double shearX = 0;
729
                double shearY = 0;
730
731
                if(transf[5] != 0)
732
                        shearX = transf[2] / transf[5];
733
                else
734
                        shearX = transf[2];
735
                if(transf[1] != 0)
736
                        shearY = transf[4] / transf[1];
737
                else
738
                        shearY = transf[4];
739
740
741
                        AffineTransform at = new AffineTransform();
742
                        at.setToShear(shearX, shearY);
743
744
                        at.transform(ul, ul);
745
                        at.transform(ur, ur);
746
                        at.transform(ll, ll);
747
                        at.transform(lr, lr);
748
749
                        ul = new Point2D.Double(ul.getX() + ptoDesplX, ul.getY() + ptoDesplY);
750
                        ur = new Point2D.Double(ur.getX() + ptoDesplX, ur.getY() + ptoDesplY);
751
                        ll = new Point2D.Double(ll.getX() + ptoDesplX, ll.getY() + ptoDesplY);
752
                        lr = new Point2D.Double(lr.getX() + ptoDesplX, lr.getY() + ptoDesplY);
753
754
                        vx2 = Math.max(Math.max(ul.getX(), ur.getX()), Math.max(ll.getX(), lr.getX()));
755
                        vy2 = Math.max(Math.max(ul.getY(), ur.getY()), Math.max(ll.getY(), lr.getY()));
756
                        vx = Math.min(Math.min(ul.getX(), ur.getX()), Math.min(ll.getX(), lr.getX()));
757
                        vy = Math.min(Math.min(ul.getY(), ur.getY()), Math.min(ll.getY(), lr.getY()));
758
                        adjustedRotedExtent = new double[4];
759
                        adjustedRotedExtent[0] = vx;
760
                        adjustedRotedExtent[1] = vy;
761
                        adjustedRotedExtent[2] = vx2;
762
                        adjustedRotedExtent[3] = vy2;
763
                        return ul;
764
                }
765
766
        }
767
            return null;
768
769
    }
770
771
    /**
772 5155 nacho
     * Ajusta la extensi?n pasada por par?metro y que corresponde al extent de la vista donde
773
     * se va a dibujar a los valores m?ximos y m?nimos de la imagen. Esto sirve para que la
774
     * petici?n al driver nunca sobrepase los l?mites de la imagen tratada aunque la vista
775
     * donde se dibuje sea de mayor tama?o.
776
     *
777 8765 jjdelcerro
     * Antes de realizar este ajuste hay que transformar la petici?n que puede corresponder a
778
     * una imagen rotada a las coordenadas de la imagen sin rotar ya que las peticiones al
779
     * driver hay que hacerlas con estas coordenadas. Para esto trasladamos la petici?n al origen
780
     * de la imagen (esquina superior izquierda), aplicamos la transformaci?n inversa a las cuatro
781
     * esquinas obtenidas y volvemos a trasladar a su posici?n original.
782
     *
783
     * Se usa la transformaci?n inversa para trasladar un punto del raster rotado al mismo sin
784
     * rotar y la transformaci?n af?n normal para trasladar un punto sin rotar a uno rotado.
785
     *
786 5155 nacho
     * @param sz Extent completo de la vista donde se va a dibujar.
787 2809 nacho
     */
788 8765 jjdelcerro
    protected double[] calculateNewView(ViewPortData vp) {
789
            Extent sz = vp.getExtent();
790 2809 nacho
        double vx = sz.minX();
791
        double vy = sz.minY();
792
        double vx2 = sz.maxX();
793
        double vy2 = sz.maxY();
794 8765 jjdelcerro
795
        //Trasladamos la petici?n si est? rotada a su posici?n sin rotar
796
797
        if (geoFile != null) {
798
                double[] transf = geoFile[0].getTransform();
799
800
                   if(transf != null && (transf[2] != 0 || transf[4] != 0)){
801
802
                           //La transformaci?n se hace en base a una esquina que varia con el signo del
803
                           //pixel size.
804
                           double ptoDesplX = (transf[1] > 0)?requestExtent.minX():requestExtent.maxX();
805
                    double ptoDesplY = (transf[5] < 0)?requestExtent.maxY():requestExtent.minY();
806
807
                    Point2D ul = new Point2D.Double(vx - ptoDesplX, vy2 - ptoDesplY);
808
                    Point2D ur = new Point2D.Double(vx2 - ptoDesplX, vy2 - ptoDesplY);
809
                    Point2D ll = new Point2D.Double(vx - ptoDesplX, vy - ptoDesplY);
810
                    Point2D lr = new Point2D.Double(vx2 - ptoDesplX, vy - ptoDesplY);
811
812
                           double shearX = 0;
813
                double shearY = 0;
814
                if(transf[5] != 0)
815
                        shearX = transf[2] / transf[5];
816
                else
817
                        shearX = transf[2];
818
                if(transf[1] != 0)
819
                        shearY = transf[4] / transf[1];
820
                else
821
                        shearY = transf[4];
822
823
                        AffineTransform at = new AffineTransform();
824
                        at.setToShear(shearX, shearY);
825
826
                        try {
827
                                at.inverseTransform(ul, ul);
828
                                at.inverseTransform(ur, ur);
829
                                at.inverseTransform(ll, ll);
830
                                at.inverseTransform(lr, lr);
831
                                } catch (NoninvertibleTransformException e) {
832
                                        e.printStackTrace();
833
                                }
834
835
                        ul = new Point2D.Double(ul.getX() + ptoDesplX, ul.getY() + ptoDesplY);
836
                        ur = new Point2D.Double(ur.getX() + ptoDesplX, ur.getY() + ptoDesplY);
837
                        ll = new Point2D.Double(ll.getX() + ptoDesplX, ll.getY() + ptoDesplY);
838
                        lr = new Point2D.Double(lr.getX() + ptoDesplX, lr.getY() + ptoDesplY);
839
840
                        vx2 = Math.max(Math.max(ul.getX(), ur.getX()), Math.max(ll.getX(), lr.getX()));
841
                        vy2 = Math.max(Math.max(ul.getY(), ur.getY()), Math.max(ll.getY(), lr.getY()));
842
                        vx = Math.min(Math.min(ul.getX(), ur.getX()), Math.min(ll.getX(), lr.getX()));
843
                        vy = Math.min(Math.min(ul.getY(), ur.getY()), Math.min(ll.getY(), lr.getY()));
844
                }
845
        }
846 2809 nacho
847 8765 jjdelcerro
        if (vx < requestExtent.minX())
848
            vx = requestExtent.minX();
849 5185 nacho
850 8765 jjdelcerro
        if (vy < requestExtent.minY())
851
            vy = requestExtent.minY();
852 5185 nacho
853 8765 jjdelcerro
        if (vx2 > requestExtent.maxX())
854
            vx2 = requestExtent.maxX();
855 5185 nacho
856 8765 jjdelcerro
        if (vy2 > requestExtent.maxY())
857
            vy2 = requestExtent.maxY();
858
859 2809 nacho
        if (geoFile != null) {
860
            for (int i = 0; i < geoFile.length; i++)
861 8765 jjdelcerro
                    geoFile[i].setView(new Extent(vx, vy, vx2, vy2));
862 2809 nacho
        } else {
863
            System.err.println("PxRaster.calculateNewView(): Imagen no cargada.");
864
        }
865 5155 nacho
        double[] adjustedExtent = {vx, vy, vx2, vy2};
866
        return adjustedExtent;
867 2809 nacho
    }
868
869
    /**
870 5894 nacho
     * Aplica transparencia leyendo los metadatos
871
     */
872
    private void setTransparencyByPixel(){
873
            if(geoFile[0].getMetadata() != null){
874 5903 nacho
                    if (stackManager == null){
875 5912 nacho
                        TransparencyRange[] noData = geoFile[0].getMetadata().parserNodataInMetadata();
876 5903 nacho
                        if(noData != null){
877
                        ArrayList entries = new ArrayList();
878
                        for(int i = 0; i < noData.length; i++)
879
                                  entries.add(noData[i]);
880
                        stackManager = new RasterFilterStackManager(filterStack);
881
                        stackManager.addTransparencyFilter(        entries, 0x0, 0xff, 0xff, 0xff);
882
                        }
883 5912 nacho
                        TransparencyRange noDataValue = geoFile[0].getMetadata().parserNodataByBand();
884
                        if(noData == null  && noDataValue != null){
885
                        ArrayList entries = new ArrayList();
886
                        entries.add(noDataValue);
887
                        stackManager = new RasterFilterStackManager(filterStack);
888
                        stackManager.addTransparencyFilter(        entries, 0x0, 0xff, 0xff, 0xff);
889
                        }
890
891 5903 nacho
                    }
892 5894 nacho
        }
893
    }
894
895 8765 jjdelcerro
    /** Dibuja el raster sobre el Graphics. Para ello debemos de pasar el viewPort que corresponde a la
896
    * vista. Este viewPort es ajustado a los tama?os m?ximos y m?nimos de la imagen por la funci?n
897
    * calculateNewView. Esta funci?n tambi?n asignar? la vista a los drivers. Posteriormente se calcula
898
    * el alto y ancho de la imagen a dibujar (wImg, hImg), as? como el punto donde se va a pintar dentro
899
    * del graphics (pt). Finalmente se llama a updateImage del driver para que pinte y una vez dibujado
900
    * se pasa a trav?s de la funci?n renderizeRaster que es la encargada de aplicar la pila de filtros
901
    * sobre el Image que ha devuelto el driver.
902
    *
903
    * Para calcular en que coordenada pixel (pt) se empezar? a pintar el BufferedImage con el raster le?do
904
    * se aplica sobre la esquina superior izquierda de esta la matriz de transformaci?n del ViewPortData
905
    * pasado vp.mat.transform(pt, pt). Si el raster no est? rotado este punto es el resultante de la
906
    * funci?n calculateNewView que devuelve la petici?n ajustada al extent de la imagen (sin rotar). Si
907
    * el raster est? rotado necesitaremos para la transformaci?n el resultado de la funci?n coordULRotateRaster.
908
    * Lo que hace esta ?ltima es colocar la petici?n que ha sido puesta en coordenadas de la imagen sin rotar
909
    * (para pedir al driver de forma correcta) otra vez en coordenadas de la imagen rotada (para calcular su
910
    * posici?n de dibujado).
911
    *
912
    * Para dibujar sobre el Graphics2D el raster rotado aplicaremos la matriz de transformaci?n con los
913
    * par?metros de Shear sobre este Graphics de forma inversa. Como hemos movido el fondo tendremos que
914
    * recalcular ahora el punto donde se comienza a dibujar aplicandole la transformaci?n sobre este
915
    * at.inverseTransform(pt, pt);. Finalmente volcamos el BufferedImage sobre el Graphics volviendo a dejar
916
    * el Graphics en su posici?n original al acabar.
917
    *
918
    * @param g Graphics sobre el que se pinta
919
    * @param vp ViewPort de la extensi?n a dibujar
920
    */
921 3221 nacho
    public synchronized void draw(Graphics2D g, ViewPortData vp) {
922 2809 nacho
        geoImage = null;
923 8765 jjdelcerro
            double shearX = 0;
924
        double shearY = 0;
925
        double factor = 1;
926
927 2809 nacho
        long t2;
928
        long t1 = new Date().getTime();
929
        lastViewPort = vp;
930 5898 nacho
931 2809 nacho
        if ((vp.getExtent().minX() > extent.maxX()) ||
932
                (vp.getExtent().minY() > extent.maxY()) ||
933
                (vp.getExtent().maxX() < extent.minX()) ||
934
                (vp.getExtent().maxY() < extent.minY())) {
935
            return;
936
        }
937
938 8765 jjdelcerro
        double[] adjustedExtent = calculateNewView(vp);
939
        Point2D p2d = coordULRotateRaster(adjustedExtent);
940
941 2809 nacho
        Extent v = geoFile[0].getView();
942
        double x = v.minX();
943
        double y = v.minY();
944
        double w = v.width();
945
        double h = v.height();
946
947 5155 nacho
        double scalex = vp.mat.getScaleX();
948
        double scaley = vp.mat.getScaleY();
949
950 5185 nacho
        /*int wImg = (int) Math.round(Math.abs(w * scalex));
951
        int hImg = (int) Math.round(Math.abs(h * scaley));*/
952
        int wImg = (int) Math.round(Math.abs((adjustedExtent[2] - adjustedExtent[0]) * scalex));
953
        int hImg = (int) Math.round(Math.abs((adjustedExtent[3] - adjustedExtent[1]) * scaley));
954 8765 jjdelcerro
955 5155 nacho
        if ((wImg <= 0) || (hImg <= 0))
956 2809 nacho
            return;
957 8765 jjdelcerro
958 5155 nacho
        //Para la transformaci?n usamos el extent que ha ajustado la funci?n calculateNewView y no usamos
959
        //el getView porque el getView puede haber  sufrido una transformaci?n en caso de que exista
960
        //fichero .rmf. En caso de no existir este fichero ser?a lo mismo aplicar la funci?n:
961
        //Point2D.Double pt = new Point2D.Double(x, y + h);
962 8765 jjdelcerro
        int wI = wImg, hI = hImg;
963
        double[] transf = bandSwitch.getBandR().getGeoRasterFile().getTransform();
964
        Point2D.Double pt = null;
965
               if(transf != null && (transf[2] != 0 || transf[4] != 0)){ //Esta rotada
966
                       pt =  new Point2D.Double(p2d.getX(), p2d.getY());
967
                       wImg = (int) Math.round(Math.abs((adjustedRotedExtent[2] - adjustedRotedExtent[0]) * scalex));
968
            hImg = (int) Math.round(Math.abs((adjustedRotedExtent[3] - adjustedRotedExtent[1]) * scaley));
969
               }else{        //No est? rotada
970
                               pt = new Point2D.Double(adjustedExtent[0], adjustedExtent[3]);
971
               }
972 5155 nacho
973 2809 nacho
        try {
974
            vp.mat.transform(pt, pt);
975
976 5899 nacho
            setTransparencyByPixel();
977 5894 nacho
978 2809 nacho
            if ((geoFile != null) && geoFile[0] instanceof GdalFile &&
979
                    (geoFile[0].getDataType() != DataBuffer.TYPE_BYTE)) {
980
                //System.out.println("PxRaster: Has dado con un Raster de 16 bits");
981 5876 nacho
                RasterBuf raster = ((GdalFile) geoFile[0]).getRaster(wImg, hImg, rp);
982 2809 nacho
                t2 = new Date().getTime();
983
984 5903 nacho
                System.out.println("Dibujando PxRaster: " + ((t2 - t1) / 1000D) + ", secs. Filtrando/Renderizando");
985 2809 nacho
                t1 = t2;
986
987
                filterStack.setInitRasterBuf(raster);
988
989
                //Si la imagen es de 16 bits lleva un filtro de realce por defecto
990 5899 nacho
                if (stackManager == null){
991
                    stackManager = new RasterFilterStackManager(filterStack);
992 5912 nacho
                    stackManager.addEnhancedFilter(false, geoFile[0].getName());
993 5899 nacho
                    stackManager.removeFilter(stackManager.getTypeFilter("computeminmax"));
994 5912 nacho
                    stackManager.addTailFilter(this.percentFilterInit, 0D, true);
995 5899 nacho
                }
996 5898 nacho
997 3578 nacho
                geoImage = renderizeRaster(raster, vp, v);
998 5903 nacho
                g.drawImage(geoImage, (int)(pt.getX()), (int)(pt.getY()), component);
999 3578 nacho
            } else if((geoFile != null) && (geoFile.length > 1) &&
1000
                            (!bandSwitch.getBandR().getGeoRasterFile().getName().equals(bandSwitch.getBandG().getGeoRasterFile().getName()) ||
1001
                            !bandSwitch.getBandR().getGeoRasterFile().getName().equals(bandSwitch.getBandB().getGeoRasterFile().getName()) ||
1002
                            !bandSwitch.getBandG().getGeoRasterFile().getName().equals(bandSwitch.getBandB().getGeoRasterFile().getName()))
1003
                            ) { // multiFiles
1004 2809 nacho
                System.out.println("Dibujando PxRaster (Multifile) ... Bands " +
1005
                                   geoFile.length);
1006
1007
                if (bandSwitch.getBandR().getGeoRasterFile() instanceof EcwFile) {
1008
                    ((EcwFile) bandSwitch.getBandR().getGeoRasterFile()).setMultifile(true);
1009
                }
1010
1011
                if (bandSwitch.getBandG().getGeoRasterFile() instanceof EcwFile) {
1012
                    ((EcwFile) bandSwitch.getBandG().getGeoRasterFile()).setMultifile(true);
1013
                }
1014
1015
                if (bandSwitch.getBandB().getGeoRasterFile() instanceof EcwFile) {
1016
                    ((EcwFile) bandSwitch.getBandB().getGeoRasterFile()).setMultifile(true);
1017
                }
1018 3578 nacho
1019
                //TODO:Soluci?n para que no se pinte si hay sharpening. Esto hay que pensarlo mejor
1020 3594 nacho
                if (stackManager == null)
1021
                    stackManager = new RasterFilterStackManager(filterStack);
1022
                if(!filterStack.isActive(stackManager.getTypeFilter("sharpening"))){
1023 3578 nacho
                        geoImage = bandSwitch.getBandR().getGeoRasterFile().updateImage(wImg, hImg, rp, null, 0, 0);
1024
                        geoImage = bandSwitch.getBandG().getGeoRasterFile().updateImage(wImg, hImg, rp, geoImage,
1025
                                                                        bandSwitch.getBandG().getBand(), GeoRasterFile.GREEN_BAND);
1026
                        geoImage = bandSwitch.getBandB().getGeoRasterFile().updateImage(wImg, hImg, rp, geoImage,
1027
                                                                        bandSwitch.getBandB().getBand(), GeoRasterFile.BLUE_BAND);
1028
                }else{
1029
                        geoImage = new BufferedImage(wImg, hImg, BufferedImage.TYPE_INT_RGB);
1030
                }
1031
1032 2809 nacho
                filterStack.setInitRasterBuf(geoImage);
1033
1034 3578 nacho
                geoImage = renderizeRaster(geoImage, vp, v);
1035 4965 nacho
1036
                g.drawImage(geoImage, (int)(pt.getX()), (int)(pt.getY()), component);
1037 3578 nacho
            } else if ((geoFile != null) ) { // Una solo fichero
1038 4998 nacho
                    geoImage = bandSwitch.getBandR().getGeoRasterFile().updateImage(wImg, hImg, rp, null, 0, 0);
1039
1040 2809 nacho
                filterStack.setInitRasterBuf(geoImage);
1041
1042 3578 nacho
                geoImage = renderizeRaster(geoImage, vp, v);
1043 4965 nacho
1044 8765 jjdelcerro
                AffineTransform at = new AffineTransform();
1045
1046
                if(transf != null && (transf[2] != 0 || transf[4] != 0)){
1047
1048
                        //Obtenemos los par?metros de shearing
1049
                        if(transf[5] != 0)
1050
                                shearX = transf[2] / transf[5];
1051
                        else
1052
                                shearX = transf[2];
1053
                        if(transf[1] != 0)
1054
                                shearY = transf[4] / transf[1];
1055
                        else
1056
                                shearY = transf[4];
1057
1058
                        //Aplicamos el shear a la vista
1059
                                at.setToShear(-shearX, -shearY);
1060
1061
                                //Escalamos en pixeles la misma cantidad que hemos le?do de m?s.
1062
                                at.scale(((double)wI/(double)wImg), ((double)hI/(double)hImg));
1063
1064
                                g.transform(at);
1065
1066
                                //Aplicamos el shear inverso al punto donde se comienza a dibujar
1067
                                at.inverseTransform(pt, pt);
1068
1069
                                g.drawImage(geoImage, (int) (pt.getX()), (int) (pt.getY()), component);
1070
                                g.transform(at.createInverse());
1071
                }else
1072
                        g.drawImage(geoImage, (int) (pt.getX()), (int) (pt.getY()), component);
1073
1074
1075 2809 nacho
            } else { // no cargada
1076
                System.err.println("Dibujando PxRaster: Foto no cargada.");
1077
            }
1078 5903 nacho
1079
            t2 = new Date().getTime();
1080
            System.out.println("Dibujando PxRaster: " + ((t2 - t1) / 1000D) + ", secs.");
1081 4998 nacho
        }catch (SupersamplingNotSupportedException e) {
1082
            System.err.println("Supersampling not supported");
1083
            return;
1084
        }catch (Exception e) {
1085 2809 nacho
            e.printStackTrace();
1086
        }
1087 4231 nacho
1088 2809 nacho
        if (pintaMarco) {
1089
            drawMarco(g, vp);
1090
        }
1091
    }
1092
1093
    /**
1094
     * Aplica la pila de fitros sobre el RasterBuf pasado como par?metro
1095
     * y lo devuelve en Image
1096
     * @param raster        RasterBuf con la imagen sobre la que se aplicaran filtros
1097
     * @return        Image con la imagen con los filtros puestos
1098
     */
1099 3578 nacho
    public Image renderizeRaster(RasterBuf raster, ViewPortData vp, Extent e) {
1100 2809 nacho
        if (filterStack != null) {
1101 3578 nacho
                filterStack.setViewPortData(vp);
1102
                filterStack.setExtent(e);
1103 2809 nacho
            filterStack.execute(raster);
1104
        }
1105 3578 nacho
        System.out.println("Image renderizeRaster(RasterBuf raster, ViewPortData vp) ");
1106 2809 nacho
        //Aplicamos el filtro para convertir a Image
1107
        RasterToImageFilter rti = new RasterToImageFilter();
1108
        rti.addParam("raster", raster);
1109
        rti.addParam("alpha", new Integer(this.getAlpha()));
1110
        rti.execute();
1111
        raster = null;
1112
1113
        return (Image) rti.getResult("raster");
1114
    }
1115
1116
    /**
1117
     * Aplica la pila de filtros sobre el Image pasado como par?metro y lo devuelve.
1118
     * Si la salida del ?ltimo filtro es un RasterBuf lo convertir? a Image
1119
     * @param image Image con la imagen sobre la que se aplicaran filtros
1120
     * @return        Image con la imagen con los filtros puestos
1121
     */
1122 3578 nacho
    public Image renderizeRaster(Image image, ViewPortData vp, Extent e) {
1123 2809 nacho
        if (filterStack != null) {
1124 3578 nacho
                filterStack.setViewPortData(vp);
1125
                filterStack.setExtent(e);
1126 2809 nacho
            filterStack.execute(image);
1127
        }
1128 3578 nacho
1129 2809 nacho
        if (filterStack.getOutDataType() != RasterBuf.TYPE_IMAGE) {
1130
            //Aplicamos el filtro para convertir a Image
1131
            RasterToImageFilter rti = new RasterToImageFilter();
1132
            rti.addParam("raster", (RasterBuf) filterStack.getResult());
1133
            rti.addParam("alpha", new Integer(this.getAlpha()));
1134
            rti.execute();
1135 3578 nacho
1136 2809 nacho
            return (Image) rti.getResult("raster");
1137
        }
1138 3578 nacho
        return (Image) filterStack.getResult();
1139
       // return image;
1140 2809 nacho
    }
1141
1142
    /**
1143
     *
1144
     * @param g
1145
     * @param vp
1146
     */
1147
    public void drawMarco(Graphics2D g, ViewPortData vp) {
1148
        //                Color color = new Color(255,222,165,128), fillColor = new Color(255,214,132,128);
1149
        Color color = new Color(128, 128, 128);
1150
1151
        //                Color color = new Color(255,222,165,128), fillColor = new Color(255,214,132,128);
1152
        Color fillColor = new Color(255, 220, 220, 0x20);
1153
        GeneralPath gp = newGP(vp);
1154
        g.setColor(fillColor);
1155
        g.fill(gp);
1156
        g.setColor(color);
1157
        g.draw(gp);
1158
    }
1159
1160
    private GeneralPath newGP(ViewPortData vp) {
1161
        //if (gp != null) return;
1162
        GeneralPath gp = new GeneralPath();
1163
        Point2D.Double pt0 = new Point2D.Double(0.0, 0.0);
1164
        Point2D.Double pt1 = new Point2D.Double(0.0, 0.0);
1165
        Point2D.Double pt2 = new Point2D.Double(0.0, 0.0);
1166
        Point2D.Double pt3 = new Point2D.Double(0.0, 0.0);
1167
        vp.mat.transform((Point2D) pts.get(0), pt0);
1168
        vp.mat.transform((Point2D) pts.get(1), pt1);
1169
        vp.mat.transform((Point2D) pts.get(2), pt2);
1170
        vp.mat.transform((Point2D) pts.get(3), pt3);
1171
1172
        // Aspa desde el extent
1173
        gp.moveTo((float) pt0.getX(), (float) pt0.getY());
1174
        gp.lineTo((float) pt2.getX(), (float) pt2.getY());
1175
        gp.moveTo((float) pt1.getX(), (float) pt1.getY());
1176
        gp.lineTo((float) pt3.getX(), (float) pt3.getY());
1177
1178
        // Extent
1179
        gp.moveTo((float) pt0.getX(), (float) pt0.getY());
1180
        gp.lineTo((float) pt1.getX(), (float) pt1.getY());
1181
        gp.lineTo((float) pt2.getX(), (float) pt2.getY());
1182
        gp.lineTo((float) pt3.getX(), (float) pt3.getY());
1183
1184
        if (extentOrig != extent) {
1185
            gp.lineTo((float) pt0.getX(), (float) pt0.getY());
1186
1187
            Vector pts = new Vector();
1188
            pts.add(proj.createPoint(extentOrig.minX(), extentOrig.minY()));
1189
            pts.add(proj.createPoint(extentOrig.maxX(), extentOrig.minY()));
1190
            pts.add(proj.createPoint(extentOrig.maxX(), extentOrig.maxY()));
1191
            pts.add(proj.createPoint(extentOrig.minX(), extentOrig.maxY()));
1192
1193
            vp.mat.transform((Point2D) pts.get(0), pt0);
1194
            vp.mat.transform((Point2D) pts.get(1), pt1);
1195
            vp.mat.transform((Point2D) pts.get(2), pt2);
1196
            vp.mat.transform((Point2D) pts.get(3), pt3);
1197
            gp.moveTo((float) pt0.getX(), (float) pt0.getY());
1198
            gp.lineTo((float) pt1.getX(), (float) pt1.getY());
1199
            gp.lineTo((float) pt2.getX(), (float) pt2.getY());
1200
            gp.lineTo((float) pt3.getX(), (float) pt3.getY());
1201
        }
1202
1203
        gp.closePath();
1204
1205
        return gp;
1206
    }
1207
1208
    public IProjection getProjection() {
1209
        return proj;
1210
    }
1211
1212
    public void setProjection(IProjection p) {
1213
        proj = p;
1214
    }
1215
1216
    public void reProject(ICoordTrans rp) {
1217
        this.rp = rp.getInverted();
1218
        System.out.println("PxRaster: reProject()");
1219
1220
        //geoFile.reProject(rp);
1221
        Vector savePts = pts;
1222
1223
        pts = new Vector();
1224
        extent = new Extent();
1225
1226
        Point2D ptDest = null;
1227
1228
        for (int i = 0; i < savePts.size(); i++) {
1229
            ptDest = rp.getPDest().createPoint(0.0, 0.0);
1230
            ptDest = rp.convert((Point2D) savePts.get(i), ptDest);
1231
            pts.add(ptDest);
1232
            extent.add(ptDest);
1233
        }
1234
1235
        setProjection(rp.getPDest());
1236
    }
1237
1238
    /**
1239
     * Obtiene el Stack Manager
1240
     * @return
1241
     */
1242
    public RasterFilterStackManager getStackManager() {
1243
        return this.stackManager;
1244
    }
1245
1246
    /**
1247
     * Asigna el Stack Manager
1248
     * @return
1249
     */
1250
    public void setStackManager(RasterFilterStackManager sm) {
1251
        this.stackManager = sm;
1252
    }
1253
1254
    /**
1255
     * Estructura que representa la relaci?n entre un fichero y la banda que se
1256
     * utiliza de este.
1257
     * @author Nacho Brodin <brodin_ign@gva.es>
1258
     */
1259
    class FileBands {
1260
        GeoRasterFile grf; //Fichero
1261
        int band; //Banda asinada
1262
        int filePos; //posici?n del fichero
1263
1264
        /**
1265
         * @return Returns the band.
1266
         */
1267
        public int getBand() {
1268
            return band;
1269
        }
1270
1271
        /**
1272
         * @param band The band to set.
1273
         */
1274
        public void setBand(int band) {
1275
            this.band = band;
1276
        }
1277
1278
        /**
1279
         * @return Returns the band.
1280
         */
1281
        public int getPos() {
1282
            return filePos;
1283
        }
1284
1285
        /**
1286
         * @param band The band to set.
1287
         */
1288
        public void setPos(int pos) {
1289
            this.filePos = pos;
1290
        }
1291
1292
        /**
1293
         * @return Returns the grf.
1294
         */
1295
        public GeoRasterFile getGeoRasterFile() {
1296
            return grf;
1297
        }
1298
1299
        /**
1300
         * @param grf The grf to set.
1301
         */
1302
        public void setGeoRasterFile(GeoRasterFile grf) {
1303
            this.grf = grf;
1304
        }
1305
    }
1306
1307
    /**
1308
     * Clase que lleva la gesti?n entre la relaci?n de ficheros representados
1309
     * por un GeoRasterFile y el n?mero de bandas que contienen. Esto es necesario
1310
     * para la actualizaci?n de la vista ya que para un Image dado podemos tener
1311
     * multiples bandas que pueden ser leidas desde diferentes ficheros.
1312
     *
1313
     * @author Nacho Brodin <brodin_ign@gva.es>
1314
     */
1315
    class BandSwitch {
1316
        private ArrayList geoFiles = new ArrayList(); //Lista de GeoRasterFile con los ficheros raster cargados
1317
        private FileBands[] listBands = new FileBands[3];
1318
        private boolean debug = false;
1319
1320
        BandSwitch() {
1321
            for (int i = 0; i < listBands.length; i++)
1322
                listBands[i] = new FileBands();
1323
        }
1324
1325
        /**
1326
         * A?ade un fichero
1327
         * @param grf
1328
         */
1329
        public void addFile(GeoRasterFile grf) {
1330
            geoFiles.add(grf);
1331
1332
            if (debug) {
1333
                this.show("addFile");
1334
            }
1335
        }
1336
1337
        /**
1338
         * Elimina un fichero
1339
         * @param grf
1340
         */
1341
        public void removeFile(GeoRasterFile grf) {
1342
            for (int iFile = 0; iFile < geoFiles.size(); iFile++) {
1343
                if (grf.equals(geoFiles.get(iFile))) {
1344
                    geoFiles.remove(iFile);
1345
                    iFile--;
1346
                }
1347
            }
1348
1349
            if (debug) {
1350
                this.show("removeFile");
1351
            }
1352
        }
1353
1354
        /**
1355
         *
1356
         * @param flag
1357
         * @param grf
1358
         * @param nBand
1359
         */
1360
        public void setBand(int flag, GeoRasterFile grf, int nBand) {
1361
            if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) {
1362
                listBands[0].setBand(nBand);
1363
                listBands[0].setGeoRasterFile(grf);
1364
            } else if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) {
1365
                listBands[1].setBand(nBand);
1366
                listBands[1].setGeoRasterFile(grf);
1367
            } else if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) {
1368
                listBands[2].setBand(nBand);
1369
                listBands[2].setGeoRasterFile(grf);
1370
            } else {
1371
                return;
1372
            }
1373
1374
            grf.setBand(flag, nBand);
1375
        }
1376
1377
        /**
1378
         * Asigna las bandas
1379
         * @param flag
1380
         * @param nBand
1381
         */
1382
        public void setBand(int flag, int nBand) {
1383
            int cont = 0;
1384
1385
            for (int iGrf = 0; iGrf < geoFiles.size(); iGrf++) {
1386
                for (int iBand = 0;
1387
                         iBand < ((GeoRasterFile) geoFiles.get(iGrf)).getBandCount();
1388
                         iBand++) {
1389
                    if (((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) &&
1390
                            (cont == nBand)) {
1391
                        listBands[0].setGeoRasterFile(((GeoRasterFile) geoFiles.get(iGrf)));
1392
                        listBands[0].setBand(iBand);
1393
                        listBands[0].setPos(iGrf);
1394
                        ((GeoRasterFile) geoFiles.get(iGrf)).setBand(flag, iBand);
1395
1396
                        //System.out.println("==>Asignando banda R FILE="+iGrf+" BANDA="+iBand);
1397
                    } else if (((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) &&
1398
                                   (cont == nBand)) {
1399
                        listBands[1].setGeoRasterFile(((GeoRasterFile) geoFiles.get(iGrf)));
1400
                        listBands[1].setBand(iBand);
1401
                        listBands[1].setPos(iGrf);
1402
                        ((GeoRasterFile) geoFiles.get(iGrf)).setBand(flag, iBand);
1403
1404
                        //System.out.println("==>Asignando banda G FILE="+iGrf+" BANDA="+iBand);
1405
                    } else if (((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) &&
1406
                                   (cont == nBand)) {
1407
                        listBands[2].setGeoRasterFile(((GeoRasterFile) geoFiles.get(iGrf)));
1408
                        listBands[2].setBand(iBand);
1409
                        listBands[2].setPos(iGrf);
1410
                        ((GeoRasterFile) geoFiles.get(iGrf)).setBand(flag, iBand);
1411
1412
                        //System.out.println("==>Asignando banda B FILE="+iGrf+" BANDA="+iBand);
1413
                    }
1414
1415
                    cont++;
1416
                }
1417
            }
1418
1419
            if (debug) {
1420
                this.show("setBand");
1421
            }
1422
        }
1423
1424
        /**
1425
         * Obtiene el n?mero de bandas
1426
         * @return
1427
         */
1428
        public int getBandCount() {
1429
            int nbandas = 0;
1430
1431
            for (int iGrf = 0; iGrf < geoFiles.size(); iGrf++)
1432
                nbandas += ((GeoRasterFile) geoFiles.get(iGrf)).getBandCount();
1433
1434
            return nbandas;
1435
        }
1436
1437
        /**
1438
         * Obtiene el GeoRasterFile que hay que leer para cargar la banda del rojo
1439
         * y la banda de este fichero que se utiliza para esta asignaci?n.
1440
         * @return        Estructura FileBand con la relaci?n fichero-banda
1441
         */
1442
        public FileBands getBandR() {
1443
            return listBands[0];
1444
        }
1445
1446
        /**
1447
         * Obtiene el GeoRasterFile que hay que leer para cargar la banda del verde
1448
         * y la banda de este fichero que se utiliza para esta asignaci?n.
1449
         * @return        Estructura FileBand con la relaci?n fichero-banda
1450
         */
1451
        public FileBands getBandG() {
1452
            return listBands[1];
1453
        }
1454
1455
        /**
1456
         * Obtiene el GeoRasterFile que hay que leer para cargar la banda del azul
1457
         * y la banda de este fichero que se utiliza para esta asignaci?n.
1458
         * @return        Estructura FileBand con la relaci?n fichero-banda
1459
         */
1460
        public FileBands getBandB() {
1461
            return listBands[2];
1462
        }
1463
1464
        public void show(String op) {
1465
            String banda = null;
1466
            System.out.println("** " + op + " **");
1467
1468
            for (int i = 0; i < 3; i++) {
1469
                if (i == 0) {
1470
                    banda = new String("Rojo");
1471
                } else if (i == 1) {
1472
                    banda = new String("Verde");
1473
                } else if (i == 2) {
1474
                    banda = new String("Azul");
1475
                }
1476 4231 nacho
1477
                System.out.println("** BANDA IMAGE=" + banda + " FILEPOS=" +
1478 2809 nacho
                                   listBands[i].getBand() +
1479
                                   " BANDA DEL FICHERO=" +
1480 4231 nacho
                                   listBands[i].getBand());
1481 2809 nacho
            }
1482
        }
1483
    }
1484 3084 nacho
        /**
1485
         * @return Returns the vName.
1486
         */
1487
        public String getVName() {
1488
                return vName;
1489
        }
1490
        /**
1491
         * @param name The vName to set.
1492
         */
1493
        public void setVName(String name) {
1494
                vName = name;
1495
        }
1496 2 luisw
}