Statistics
| Revision:

root / branches / v02_desarrollo / libraries / libCq CMS for java.old / src / org / cresques / io / GeoRasterFile.java @ 1527

History | View | Annotate | Download (9.54 KB)

1
/*
2
 * Created on 26-abr-2004
3
 *
4
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
5
 */
6
package org.cresques.io;
7

    
8
import java.awt.Component;
9
import java.awt.Image;
10
import java.awt.image.DataBuffer;
11
import java.lang.reflect.Constructor;
12
import java.lang.reflect.InvocationTargetException;
13
import java.util.TreeMap;
14

    
15
import org.cresques.cts.ICoordTrans;
16
import org.cresques.cts.IProjection;
17
import org.cresques.px.Extent;
18
import org.cresques.px.IObjList;
19
import org.cresques.px.PxContour;
20
import org.cresques.px.PxObjList;
21

    
22
/**
23
 * Manejador de ficheros raster georeferenciados.
24
 * 
25
 * Esta clase abstracta es el ancestro de todas las clases que proporcionan
26
 * soporte para ficheros raster georeferenciados.<br>
27
 * Actua tambien como una 'Fabrica', ocultando al cliente la manera en que
28
 * se ha implementado ese manejo. Una clase nueva que soportara un nuevo
29
 * tipo de raster tendr?a que registrar su extensi?n o extensiones usando
30
 * el m?todo @see registerExtension.<br> 
31
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>*
32
 */
33

    
34
public abstract class GeoRasterFile extends GeoFile {
35
        public static final int RED_BAND        = 0x01;
36
        public static final int GREEN_BAND        = 0x02;
37
        public static final int BLUE_BAND        = 0x04;
38

    
39
        /**
40
         * Filtro para raster.
41
         * Permite eliminar la franja inutil alrededor de un raster girado o de
42
         * un mosaico de borde irregular.
43
         * 
44
         * Funciona bien solo con raster en tonos de gris, porque se basa que
45
         * el valor del pixel no supere un determinado valor 'umbral' que se
46
         * le pasa al constructor.
47
         * 
48
         * Desarrollado para 'limpiar' los bordes de los mosaicos del SIG
49
         * Oleicola. Para ese caso los par?metros del constructo son:
50
         * PixelFilter(0x10ffff00, 0xff000000, 0xf0f0f0);
51
         * 
52
         * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
53
         */
54

    
55
        class PixelFilter {
56
                boolean debug = false;
57
                int transparente = 0x10ffff00;
58
                int orColor = 0xff000000;
59
                int umbral = 0xf0f0f0;
60
                private int alpha = 0xff;
61
                
62
                /**
63
                 * Constructor para el Oleico
64
                 */
65

    
66
                public PixelFilter(int alpha) {
67
                        transparente = 0x10ffff00;
68
                        setAlpha(alpha);
69
                        //orColor = 0xff000000 | (alpha * 0x1000000);
70
                        umbral = 0xf0f0f0;
71
                }
72

    
73
                /**
74
                 * Constructor.
75
                 * 
76
                 * @param trans Color que sustituye a los pixeles filtrados
77
                 * @param or        M?scara que se aplica a los pixeles restantes
78
                 * @param pxMax        Umbral que determina los pixeles.
79
                 */
80
                        
81
                public PixelFilter(int trans, int or, int pxMax) {
82
                        transparente = trans;
83
                        setAlpha(orColor / 0x1000000);
84
                        orColor = or;
85
                        umbral = pxMax;
86
                }
87
                
88
                public void setAlpha(int alpha) {
89
                        this.alpha = alpha;
90
                        orColor = alpha * 0x1000000;
91
                }
92
                public int getAlpha() { return alpha; }
93
                
94
                /**
95
                 * Filtra una l?nea de pixeles.
96
                 * 
97
                 * @param pRGBArray
98
                 */
99
                
100
                public void filterLine(int [] pRGBArray) {
101
                        for (int i=0; i<pRGBArray.length; i++) {
102
                                if (debug)
103
                                        System.out.print(""+i+":"+Integer.toHexString(pRGBArray[i])+",");
104
                                if (pRGBArray[i]  >= umbral)
105
                                        pRGBArray[i] = transparente;
106
                                else
107
                                        pRGBArray[i] |= orColor;
108
                        }
109
                        if (debug)
110
                                System.out.println("");
111
                }
112
        }
113

    
114
        class SimplePixelFilter extends PixelFilter {
115
                public SimplePixelFilter(int alpha) {
116
                        super(alpha);
117
                }
118
                public void filterLine(int [] pRGBArray) {
119
                        for (int i=0; i<pRGBArray.length; i++) {
120
                                if (debug)
121
                                        System.out.print(""+i+":"+Integer.toHexString(pRGBArray[i])+",");
122
                                pRGBArray[i] |= orColor;
123
                        }
124
                        if (debug)
125
                                System.out.println("");
126
                }
127
        }
128

    
129
        private static TreeMap supportedExtensions = null;
130
        protected Component updatable = null;
131
        protected boolean doTransparency = false;
132
        //protected int alpha = 0;
133
        protected PixelFilter tFilter = null;
134

    
135
        protected int rBandNr = 1, gBandNr = 1, bBandNr = 1;
136
        protected int bandCount = 1;
137
        private int dataType = DataBuffer.TYPE_BYTE;
138

    
139
        static {
140
                supportedExtensions = new TreeMap();
141
                if (System.getProperty("os.name").toUpperCase().startsWith("WIN")) {
142
                        supportedExtensions.put("ecw",  EcwFile.class);
143
                }
144
                supportedExtensions.put("tif",  TifGeoRefFile.class);
145
                supportedExtensions.put("tiff", TifGeoRefFile.class);
146
                supportedExtensions.put("jpg",  TifGeoRefFile.class);
147
                supportedExtensions.put("png",  TifGeoRefFile.class);
148
                supportedExtensions.put("sid",  MrSidFile.class);
149
        }
150
        
151
        /**
152
         * Factoria para abrir distintos tipos de raster.
153
         * 
154
         * @param proj Proyecci?n en la que est? el raster.
155
         * @param fName Nombre del fichero.
156
         * @return GeoRasterFile, o null si hay problemas.
157
         */
158
        public static GeoRasterFile openFile(IProjection proj, String fName) {
159
                String ext = fName.toLowerCase().substring(fName.lastIndexOf('.')+1);
160
                GeoRasterFile grf = null;
161
                // TODO NotSupportedExtensionException
162
                if (!supportedExtensions.containsKey(ext)) return grf;
163
                /**/
164
                Class clase = (Class) supportedExtensions.get(ext);
165
                Class [] args = {IProjection.class, String.class};
166
                try {
167
                        Constructor hazNuevo = clase.getConstructor(args);
168
                        Object [] args2 = {proj, fName};
169
                        grf = (GeoRasterFile) hazNuevo.newInstance(args2);
170
                } catch (SecurityException e) {
171
                        // TODO Auto-generated catch block
172
                        e.printStackTrace();
173
                } catch (NoSuchMethodException e) {
174
                        // TODO Auto-generated catch block
175
                        e.printStackTrace();
176
                } catch (IllegalArgumentException e) {
177
                        // TODO Auto-generated catch block
178
                        e.printStackTrace();
179
                } catch (InstantiationException e) {
180
                        // TODO Auto-generated catch block
181
                        e.printStackTrace();
182
                } catch (IllegalAccessException e) {
183
                        // TODO Auto-generated catch block
184
                        e.printStackTrace();
185
                } catch (InvocationTargetException e) {
186
                        // TODO Auto-generated catch block
187
                        e.printStackTrace();
188
                }/**/
189
                 
190
                /* * /
191
                if (ext.compareTo("ecw") == 0) {
192
                        grf = new EcwFile(proj, fName);
193
                } else if (ext.compareTo("tif") == 0 || ext.compareTo("tiff") == 0 || ext.compareTo("jpg") == 0  || ext.compareTo("png") == 0 ) {
194
                        grf = new TifGeoRefFile(proj, fName);
195
                }/ * */
196

    
197
                return grf;
198
        }
199
        
200
        /**
201
         * Registra una clase que soporta una extensi?n raster.
202
         * @param ext extensi?n soportada.
203
         * @param clase clase que la soporta.
204
         */
205
        public static void registerExtension(String ext, Class clase) {
206
                ext = ext.toLowerCase();
207
                System.out.println("RASTER: extension '"+ext+"' supported.");
208
                supportedExtensions.put(ext, clase);
209
        }
210
        
211
        /**
212
         * Tipo de fichero soportado.
213
         * Devuelve true si el tipo de fichero (extension) est? soportado, si no
214
         * devuelve false.
215
         * 
216
         * @param fName Fichero raster
217
         * @return  true si est? soportado, si no false.
218
          */
219
        public static boolean fileIsSupported(String fName) {
220
                String ext = fName.toLowerCase().substring(fName.lastIndexOf('.')+1);
221
                return supportedExtensions.containsKey(ext);
222
        }
223
        
224
        public GeoRasterFile(IProjection proj, String name) {
225
                super(proj, name);
226
        }
227
        
228
        abstract public GeoFile load();
229
        
230
        abstract public void close();
231
        
232
        public static PxContour getContour(String fName, String name, IProjection proj) {
233
                PxContour contour = null;
234
                return contour;
235
        }
236
        
237
        abstract public int getWidth();
238
        abstract public int getHeight();
239

    
240
        abstract public void reProject(ICoordTrans rp);
241

    
242
        abstract public void setView(Extent e);
243
        abstract public Extent getView();
244
        
245
        // TRANSPARENCIA. Versi?n de pruebas. Hay que verificar que se puede
246
        // tener filtros y valores alpha, que no es lento, y que no hay una manera
247
        // mejor de hacerlo.
248
        //abstract public void setTransparency(boolean t);
249
        //abstract public void setTransparency(int t);
250
        public void setTransparency(boolean t) {
251
                doTransparency = t;
252
                tFilter = new PixelFilter(255);
253
        }
254
        public void setTransparency(int t ) {
255
                doTransparency = true;
256
                tFilter = new SimplePixelFilter(255 - t);
257
        }
258
        public boolean getTransparency() { return doTransparency; }
259
        public void setAlpha(int alpha) {
260
                if (!doTransparency) setTransparency(255 - alpha);
261
                else tFilter.setAlpha(alpha);
262
        }
263
        public int getAlpha() {
264
                if (tFilter == null)
265
                        return 255;
266
                return tFilter.getAlpha();
267
        }
268
        
269
        public void setUpdatable(Component c) { updatable = c; }
270
        
271
        abstract public Image updateImage(int width, int height, ICoordTrans rp);
272

    
273
        /**
274
         * Obtiene el valor del raster en la coordenada que se le pasa.
275
         * El valor ser? Double, Int, Byte, etc. dependiendo del tipo de
276
         * raster.
277
         * @param x        coordenada X
278
         * @param y coordenada Y
279
         * @return
280
         */
281
        abstract public Object getData(int x, int y, int band);
282

    
283
        /**
284
         * Actualiza la/s banda/s especificadas en la imagen.
285
         * @param width                ancho
286
         * @param height        alto
287
         * @param rp                reproyecci?n
288
         * @param img                imagen
289
         * @param flags                que bandas [ RED_BAND | GREEN_BAND | BLUE_BAND ]
290
         * @return                img
291
         */
292
        abstract public Image updateImage(int width, int height, ICoordTrans rp, Image img, int flags);
293

    
294
        public int getBandCount() { return bandCount; }
295
        
296
        /**
297
         * Asocia un colorBand al rojo, verde o azul.
298
         * @param flag cual (o cuales) de las bandas.
299
         * @param nBand        que colorBand
300
         */
301
        
302
        public void setBand(int flag, int bandNr) {
303
                if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) rBandNr = bandNr;
304
                if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) gBandNr = bandNr;
305
                if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) bBandNr = bandNr;
306
        }
307

    
308
        /**
309
         * Devuelve el colorBand activo en la banda especificada.
310
         * @param flag banda.
311
         */
312
        
313
        public int getBand(int flag) {
314
                if (flag == GeoRasterFile.RED_BAND) return rBandNr;
315
                if (flag == GeoRasterFile.GREEN_BAND) return gBandNr;
316
                if (flag == GeoRasterFile.BLUE_BAND) return bBandNr;
317
                return -1;
318
        }
319
        
320
        /**
321
         * @return Returns the dataType.
322
         */
323
        public int getDataType() {
324
                return dataType;
325
        }
326
        /**
327
         * @param dataType The dataType to set.
328
         */
329
        public void setDataType(int dataType) {
330
                this.dataType = dataType;
331
        }
332

    
333
        public IObjList getObjects() {
334
                // TODO hay que a?adir el raster a la lista de objetos
335
                IObjList oList = new PxObjList(proj);
336
                return oList;
337
        }
338
}