Statistics
| Revision:

svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src-dvp / org / cresques / io / BsbFile.java @ 1928

History | View | Annotate | Download (9.01 KB)

1
/*
2
 * Created on 25-sep-2004
3
 *
4
 * TODO To change the template for this generated file go to
5
 * Window - Preferences - Java - Code Style - Code Templates
6
 */
7
package org.cresques.io;
8

    
9
import java.awt.Color;
10
import java.awt.Image;
11
import java.awt.geom.Point2D;
12
import java.awt.image.BufferedImage;
13
import java.io.IOException;
14
import java.util.Vector;
15

    
16
import org.cresques.cts.ICoordTrans;
17
import org.cresques.cts.IProjection;
18
import org.cresques.io.GeoFile;
19
import org.cresques.io.GeoRasterFile;
20
import org.cresques.px.Extent;
21

    
22
import es.gva.cit.jbsb.BsbException;
23

    
24
/**
25
 * Soporte 'nativo' para Bsb.
26
 * Este conjunto de funcionalidades est? tomado de manera casi literal
27
 * del soporte para ECW de ermapper.<br>
28
 * Probablemente esto deber?a formar parte del JNI que recubre a la
29
 * librer?a en C extraida de gdal.<br>
30
 * Lo pongo aqu? a manera de ejemplo de como atacar un formato binario
31
 * desde Java.<br><br>   
32
 * @author Luis W. Sevilla.
33
 */
34

    
35
class BsbNative extends es.gva.cit.jbsb.Bsb {
36
        // Polilinea con extent
37
        class Contour extends Vector {
38
                public double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE;
39
                public double maxX = -Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
40
                public Contour() {
41
                        super();
42
                }
43
                public void add(Point2D pt) {
44
                        super.add(pt);
45
                        if (pt.getX() > maxX) maxX = pt.getX();
46
                        if (pt.getX() < minX) minX = pt.getX();
47
                        if (pt.getY() > maxY) maxY = pt.getY();
48
                        if (pt.getY() < minY) minY = pt.getY();
49
                }
50
        }
51
        private int [] paleta = null;
52
        /**
53
         * Contorno en coordenadas geogr?ficas. (y Extent del raster).
54
         */
55
        public Contour esq = new Contour();
56
        public int width = 0, height = 0;
57
        public double originX = 0D, originY = 0D;
58
        public String version = "";
59
        
60
        public BsbNative(String fName) throws BsbException {
61
                super();
62
                try {
63
                        init(fName);
64
                } catch (IOException e) {
65
                        // TODO Auto-generated catch block
66
                        e.printStackTrace();
67
                }
68
        }
69
        
70
        private void init(String fName) throws BsbException, IOException {
71
                bsbOpen(fName);
72
                width = g_psInfo.nXSize;
73
                height = g_psInfo.nYSize;
74
                Vector pal = new Vector();
75
                String buf = null;
76
                  for (int i=0; i<g_psInfo.papszHeader.length; i++) {
77
                          buf = g_psInfo.papszHeader[i];
78
                          if (buf.length()<1) continue;
79
                          if (buf.startsWith("RGB/")) {
80
                                  String [] dat = buf.split(",");
81
                                  pal.add(new Color(Integer.parseInt(dat[1]),
82
                                          Integer.parseInt(dat[2]),
83
                                        Integer.parseInt(dat[3])));
84
                          } else if (buf.startsWith("VER/")) {
85
                                  version = buf.substring(4);
86
                          } /*else if (buf.startsWith("PLY/")) { // Marco de hoja
87
                                  String [] dat = buf.split(",");
88
                                  esq.add(new Point2D.Double(Double.parseDouble(dat[2]),
89
                                                  Double.parseDouble(dat[1])));
90
                          }*/
91
                  }
92
                  if (true) { //version.startsWith("1")) {
93
                          esq.add(new Point2D.Double(0D,height));
94
                          esq.add(new Point2D.Double(0D,0D));
95
                          esq.add(new Point2D.Double(width,0D));
96
                          esq.add(new Point2D.Double(width,height));
97
                  }
98
                  paleta = new int[pal.size()];
99
                  for (int i=0; i<pal.size(); i++)
100
                          paleta[i] = ((Color) pal.get(i)).getRGB();
101

    
102
                  originX = esq.minX;
103
                  originY = esq.maxY;
104
        }
105
        
106
        double lastReadLine = -1;
107
        int currentViewWidth = -1;
108
        int currentViewHeight = -1;
109
        double currentViewX = 0D;
110
        double viewportScale = 0D;
111
        double step = 0D;
112
        
113
        public Point2D worldToRaster(Point2D pt) {
114
                double x = (((double) width)/(esq.maxX-esq.minX))*(pt.getX()-esq.minX);
115
                double y = (((double) height)/(esq.maxY-esq.minY))*(esq.maxY-pt.getY());
116
                Point2D ptRes = new Point2D.Double(x, y);
117
                return ptRes;
118
        }
119
        
120
        public int setView(double dWorldTLX, double dWorldTLY,
121
            double dWorldBRX, double dWorldBRY,
122
            int nWidth, int nHeight) {
123
                int err = 0;
124
                Point2D tl = worldToRaster(new Point2D.Double(dWorldTLX, dWorldTLY));
125
                Point2D br = worldToRaster(new Point2D.Double(dWorldBRX, dWorldBRY));
126
                // Calcula cual es la primera l?nea a leer;
127
                currentViewWidth = nWidth;
128
                currentViewHeight = nHeight;
129

    
130
                currentViewX = tl.getX();
131
                viewportScale = (double) currentViewWidth/(br.getX()-tl.getX());
132
                step = 1D/viewportScale;
133
                lastReadLine = tl.getY();
134

    
135
                System.out.println("BsbFile: TL=("+dWorldTLX+","+dWorldTLY+
136
                        "); BR=("+dWorldBRX+","+dWorldBRY+")\n"+
137
                        "BsbFile: escala="+viewportScale+"; lastReadLine="+lastReadLine);
138
                return err;
139
        }
140
        
141
        public int readLineRGBA(int [] line) throws BsbException {
142
                int err = 0;
143
                  bsbReadLine((int) lastReadLine);
144

    
145
                  lastReadLine += step;
146
                  
147
                  byte [] l = g_buffer.pabyScanLineBuf;
148
                  int white = Color.BLUE.getRGB();
149
                  float j =(float) currentViewX;
150
                  for (int i=0; i<currentViewWidth && j<l.length; i++, j+=step) {
151
                          line[i] = paleta[l[(int) j]-1];
152
                  }
153
                //for (int i=0; i<currentViewWidth; i++) line[i] = 128+128*256+128*256*256;
154

    
155
                return err;
156
        }
157

    
158
        void pintaInfo() {
159
                  System.out.println("Fichero BSB: Info ("+g_psInfo.papszHeader.length+" l?neas):");
160
                  for (int i=0; i<g_psInfo.papszHeader.length; i++) {
161
                          if (g_psInfo.papszHeader[i].length()<1) continue;
162
                          System.out.println("info["+i+"] = "+g_psInfo.papszHeader[i]);
163
                  }
164
        }
165
        
166
        void pintaPaleta() {
167
                  System.out.println("Fichero BSB: Paleta ("+paleta.length+" colores):");
168
                for (int i=0; i<paleta.length; i++)
169
                        System.out.println("color("+i+")="+paleta[i]);
170
        }
171
}
172

    
173
/**
174
 * @author Luis W. Sevilla
175
 */
176
public class BsbFile extends GeoRasterFile {
177
        private BsbNative file = null;
178

    
179
        private Extent v = null;
180
        
181
        
182
        static {
183
                GeoRasterFile.registerExtension("nos", BsbFile.class);
184
                GeoRasterFile.registerExtension("kap", BsbFile.class);
185
        }
186
        public BsbFile(IProjection proj, String fName) {
187
                super(proj, fName);
188
                extent = new Extent();
189
                try {
190
                        file = new BsbNative(fName);
191
                        showOnOpen();
192
                        load();
193
                } catch(BsbException e){
194
                          System.out.println("Error en BSBOpen");
195
                          e.printStackTrace();
196
                          file = null;
197
                }
198
        }
199
        
200
        public GeoFile load() {
201
                /*double minX, minY, maxX, maxY;
202
                minX = minY = 0D;
203
                maxX = (double) width;
204
                maxY = (double) height;*/
205
                extent = new Extent(file.esq.minX, file.esq.minY, file.esq.maxX, file.esq.maxY);
206
                return this;
207
        }
208
        public void setView(Extent e) { v = new Extent(e); }
209
        public Extent getView() { return v; }
210
        
211
        public int getWidth() {        return file.width; }
212
        public int getHeight() { return file.height;}
213

    
214
        /* (non-Javadoc)
215
         * @see org.cresques.io.GeoRasterFile#reProject(org.cresques.cts.ICoordTrans)
216
         */
217
        public void reProject(ICoordTrans rp) {
218
                // TODO Auto-generated method stub
219
                
220
        }
221
        /* (non-Javadoc)
222
         * @see org.cresques.io.GeoRasterFile#setTransparency(boolean)
223
         */
224
        public void setTransparency(boolean t) {
225
                // TODO Auto-generated method stub
226
        }
227
        public void setTransparency(int t) {
228
                // TODO Auto-generated method stub
229
        }
230
        /* (non-Javadoc)
231
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans)
232
         */
233
        public Image updateImage(int width, int height, ICoordTrans rp) {
234
                double dFileAspect, dWindowAspect;
235
                int line, pRGBArray[] = null;
236
                Image image = null;
237

    
238
                // Work out the correct aspect for the setView call.
239
                dFileAspect = (double)v.width()/(double)v.height();
240
                dWindowAspect = (double)width /(double)height;
241

    
242
                if (dFileAspect > dWindowAspect) {
243
                  height =(int)((double)width/dFileAspect);
244
                } else {
245
                  width = (int)((double)height*dFileAspect);
246
                }
247
                
248
                // Set the view
249
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),
250
                        width, height);
251
                
252
                image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
253
                pRGBArray = new int[width];
254
                try {
255
                        for (line=0; line < height; line++) {
256
                                file.readLineRGBA(pRGBArray);
257
                                ((BufferedImage)image).setRGB(0, line, width, 1, pRGBArray, 0, width);
258
                        }
259
                } catch (Exception e) {
260
                        // TODO Auto-generated catch block
261
                        e.printStackTrace();
262
                }
263
                
264
                return image;
265
        }
266
        
267
        public Image updateImage(int width, int height, ICoordTrans rp, Image img, int origBand, int destBand){
268
                
269
                return null;
270
        }
271
        
272
        private void showOnOpen() {
273
                  // Report en la apertura (quitar)
274
                  System.out.println("Fichero BSB '"+getName()+"' abierto.");
275
                  System.out.println("Version = "+file.version);
276
                  System.out.println("   Size = ("+file.width+","+file.height+")");
277
                  file.pintaInfo();
278
                  file.pintaPaleta();
279

    
280
        }
281

    
282
        /* (non-Javadoc)
283
         * @see org.cresques.io.GeoRasterFile#close()
284
         */
285
        public void close() {
286
        }
287

    
288
        /* (non-Javadoc)
289
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans, java.awt.Image, int)
290
         */
291
        public Image updateImage(int width, int height, ICoordTrans rp, Image img, int flags) {
292
                // TODO Auto-generated method stub
293
                return null;
294
        }
295

    
296
        /* (non-Javadoc)
297
         * @see org.cresques.io.GeoRasterFile#getData(int, int, int)
298
         */
299
        public Object getData(int x, int y, int band) {
300
                // TODO Auto-generated method stub
301
                return null;
302
        }
303
        
304
        /**
305
         * Devuelve los datos de una ventana solicitada
306
         * @param ulX        coordenada X superior izda.
307
         * @param ulY        coordenada Y superior derecha.
308
         * @param sizeX        tama?o en X de la ventana.
309
         * @param sizeY tama?o en Y de la ventana.
310
         * @param band        Banda solicitada.
311
         */
312
        public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band){
313

    
314
                return null;
315
        }
316
        
317
        /**
318
         * Devuelve el tama?o de bloque
319
         * @return Tama?o de bloque
320
         */
321
        public int getBlockSize(){
322
     //TODO Nacho: Implementar getBlockSize de EcwFile
323
          return 1;
324
        }
325
}
326

    
327