Statistics
| Revision:

root / trunk / libraries / libCq CMS for java.old / src / org / cresques / io / EcwFile.java @ 34

History | View | Annotate | Download (5.33 KB)

1
/*
2
 * EcwFile.java
3
 */
4
package org.cresques.io;
5

    
6
import java.awt.Image;
7
import java.awt.image.BufferedImage;
8

    
9
import com.ermapper.ecw.JNCSFile;
10
import com.ermapper.util.JNCSDatasetPoint;
11

    
12
import org.cresques.geo.ReProjection;
13
import org.cresques.px.*;
14

    
15
/**
16
 * Filtro para raster. Permite eliminar el fondo;
17
 * 
18
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
19
 */
20

    
21
class PixelFilter {
22
        boolean debug = false;
23
        int transparente = 0x10ffff00;
24
        int orColor = 0xff000000;
25
        int umbral = 0xf0f0f0;
26
        
27
        /**
28
         * Constructor para el Oleico
29
         */
30

    
31
        public PixelFilter() {
32
                transparente = 0x10ffff00;
33
                orColor = 0xff000000;
34
                umbral = 0xf0f0f0;
35
        }
36

    
37
        /**
38
         * Constructor.
39
         * 
40
         * @param trans
41
         * @param or
42
         * @param pxMax
43
         */
44
                
45
        public PixelFilter(int trans, int or, int pxMax) {
46
                transparente = trans;
47
                orColor = or;
48
                umbral = pxMax;
49
        }
50
        
51
        /**
52
         * Filtra una l?nea de pixeles.
53
         * 
54
         * @param pRGBArray
55
         */
56
        
57
        public void filterLine(int [] pRGBArray) {
58
                for (int i=0; i<pRGBArray.length; i++) {
59
                        if (debug)
60
                                System.out.print(""+i+":"+Integer.toHexString(pRGBArray[i])+",");
61
                        if (pRGBArray[i]  >= umbral)
62
                                pRGBArray[i] = transparente;
63
                        else
64
                                pRGBArray[i] |= orColor;
65
                }
66
                if (debug)
67
                        System.out.println("");
68
        }
69
}
70

    
71
/**
72
 * Soporte para los ficheros .ecw de ErMapper
73
 * 
74
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
75
 */
76

    
77
public class EcwFile extends GeoRasterFile {
78
        JNCSFile file = null;
79
        boolean bErrorOnOpen = false;
80
        String errorMessage = null;
81

    
82
        Extent v = null;
83
        boolean doTransparency = false;
84
        PixelFilter tFilter = null;
85
        
86
        public EcwFile(String fname) {
87
                super(null, null); // TODO aqui van las proyecciones
88
                fname = DataSource.normalize(fname);
89
                super.setName(fname);
90
                extent = new Extent();
91
                try {
92
                        System.err.println("Abriendo "+fname);
93
                        file = new JNCSFile(fname, false);
94
                        load();
95
                } catch(Exception e) {
96
                  bErrorOnOpen = true;
97
                  errorMessage = e.getMessage();
98
//                  g.drawString(errorMessage, 0, 50);
99
                  System.err.println(errorMessage);
100
                  e.printStackTrace();
101
                }
102
        }
103
                
104
        public void setExtent(Extent e) { extent = new Extent(e); }
105
        public Extent getExtent() { return extent; }
106
        
107
        public void setView(Extent e) { v = new Extent(e); }
108
        public Extent getView() { return v; }
109
        
110
        public void setTransparency(boolean t) {
111
                doTransparency = t;
112
                tFilter = new PixelFilter();
113
        }
114
        public boolean getTransparency() { return doTransparency; }
115
        
116
        /**
117
         * Carga un ECW.
118
         * 
119
         * @param fname
120
         */
121
        
122
        public GeoFile load() {
123
                double minX, minY, maxX, maxY;
124

    
125
                System.out.println("ECW size = ("+file.width+","+file.height+")\n"+
126
                        " inc=("+file.cellIncrementX+","+file.cellIncrementY+")\n"+
127
                        " datum='"+file.datum+"', proyeccion='"+file.projection+"'");
128
                minX = file.originX;
129
                maxY = file.originY;
130
                maxX = file.originX + (double)(file.width-1)*file.cellIncrementX;
131
                minY = file.originY + (double)(file.height-1)*file.cellIncrementY;
132
                extent = new Extent(minX, minY, maxX, maxY);
133
                        System.out.println(extent);
134
                
135
                return this;
136
        }
137
        
138
        public void close() {
139
                file.close(true);
140
                file = null;
141
        }
142

    
143
        /**
144
         * Obtiene un trozo de imagen (determinado por la vista y los par?metros.
145
         * 
146
         * @param width
147
         * @param height
148
         */
149
        
150
        public Image updateImage(int width, int height, ReProjection rp) {
151
                // TODO reproyectar
152
                BufferedImage ecwImage = null;
153
                if (file != null) {
154
                  try {
155
                        double dFileAspect, dWindowAspect;
156
                        //double dWorldTLX, dWorldTLY, dWorldBRX, dWorldBRY;
157
                        int bandlist[];
158
                        int line, pRGBArray[] = null;
159
                
160
                        // Work out the correct aspect for the setView call.
161
                        dFileAspect = (double)v.width()/(double)v.height();
162
                        dWindowAspect = (double)width /(double)height;
163

    
164
                        if (dFileAspect > dWindowAspect) {
165
                          height =(int)((double)width/dFileAspect);
166
                        } else {
167
                          width = (int)((double)height*dFileAspect);
168
                        }
169

    
170
                        // Create an image of the ecw file.
171
                        if (doTransparency)
172
                                ecwImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
173
                        else
174
                                ecwImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
175
                        pRGBArray = new int[width];
176

    
177
                        // Setup the view parameters for the ecw file.
178
                        bandlist = new int[file.numBands];
179
                        for (int i=0; i< file.numBands; i++) {
180
                          bandlist[i] = i;
181
                        }
182

    
183
                        // Set the view
184
                        try {
185
                                JNCSDatasetPoint ptMin = file.convertWorldToDataset(v.minX(), v.minY());
186
                                JNCSDatasetPoint ptMax = file.convertWorldToDataset(v.maxX(), v.maxY());
187
                                System.out.println("Dataset coords Width = "+(ptMax.x-ptMin.x)+", px width ="+width);
188
                                file.setView(file.numBands, bandlist, 
189
                                        v.minX(), v.maxY(), v.maxX(), v.minY(),
190
                                        width, height);
191
                        } catch (com.ermapper.ecw.JNCSInvalidSetViewException e) {
192
                                System.err.println(errorMessage);
193
                                e.printStackTrace();
194
                        }
195

    
196
                        // Read the scan lines
197
                        for (line=0; line < height; line++) {
198
                          file.readLineRGBA(pRGBArray);
199
                          // Prueba de sustituci?n de color transparente
200
                          if (doTransparency) {
201
                                        if (line == 0) tFilter.debug = true;
202
                                          tFilter.filterLine(pRGBArray);
203
                                        tFilter.debug = false; 
204
                                        
205
                          }
206
                          ecwImage.setRGB(0, line, width, 1, pRGBArray, 0, width);
207
                        }
208
                  } catch(Exception e) {
209
                        bErrorOnOpen = true;
210
                        errorMessage = e.getMessage();
211
//                        g.drawString(errorMessage, 0, 50);
212
                        System.err.println(errorMessage);
213
                        e.printStackTrace();
214
                  }
215
                }
216
                return ecwImage;
217
        }
218

    
219
        
220
        /**
221
         * Cambia el raster de proyecci?n.
222
         */
223
        
224
        public void reProject(ReProjection rp) {
225
                // TODO metodo reProject pendiente de implementar
226
        }
227

    
228
}