/Users/lyon/j4p/src/math/MatFloat.java

1    package math; 
2     
3    import futils.Futil; 
4     
5    import java.io.FileInputStream; 
6    import java.io.FileOutputStream; 
7    import java.io.ObjectInputStream; 
8    import java.io.ObjectOutputStream; 
9    import java.util.zip.GZIPInputStream; 
10   import java.util.zip.GZIPOutputStream; 
11    
12   public class MatFloat extends 
13           Mat2 { 
14       public float f[][]; 
15    
16       public MatFloat(float flt[][]) { 
17           f = flt; 
18       } 
19    
20    
21    
22    
23       public static void main(String args[]) { 
24           System.out.println("Test"); 
25           float flt[][] = 
26                   { 
27                       {1.0f, 3.0f, 4.0f}, 
28                       {1.0f, 3.0f, 4.0f}, 
29                       {1.0f, 3.0f, 4.0f} 
30                   }; 
31           MatFloat mf = new MatFloat(flt); 
32           print(flt); 
33           String fn = Futil.getWriteFile("flt.gz file").toString(); 
34           mf.saveAsgz(fn); 
35           mf.readAsgz(fn); 
36           print(mf.f); 
37       } 
38    
39       public void saveAsgz() { 
40           saveAsgz( 
41                   Futil.getWriteFile("flt.gz file").toString()); 
42       } 
43    
44       public void saveAsgz(String fn) { 
45           try { 
46               FileOutputStream fos = new FileOutputStream(fn); 
47               GZIPOutputStream gos = new GZIPOutputStream(fos); 
48               ObjectOutputStream oos = new ObjectOutputStream(gos); 
49               oos.writeInt(f.length); 
50               oos.writeInt(f[0].length); 
51               for (int x = 0; x < f.length; x++) 
52                   for (int y = 0; y < f[0].length; y++) 
53                       oos.writeFloat(f[x][y]); 
54               oos.close(); 
55               gos.finish(); 
56    
57           } catch (Exception e) { 
58               System.out.println("Save saveAsFloatgz:" + e); 
59           } 
60       } 
61    
62       public void readAsgz(String fn) { 
63           try { 
64               FileInputStream fis = new FileInputStream(fn); 
65               GZIPInputStream gis = new GZIPInputStream(fis); 
66               ObjectInputStream ois = new ObjectInputStream(gis); 
67               int w = ois.readInt(); 
68               int h = ois.readInt(); 
69               f = new float[w][h]; 
70               for (int x = 0; x < w; x++) 
71                   for (int y = 0; y < h; y++) 
72                       f[x][y] = ois.readFloat(); 
73               ois.close(); 
74           } catch (Exception e) { 
75               System.out.println("readAsgz:" + e); 
76           } 
77       } 
78    
79       public static double getMax(double a[]) { 
80           double max = -1; 
81           for (int i = 0; i < a.length; i++) { 
82               if (a[i] > max) max = a[i]; 
83           } 
84           return max; 
85       } 
86   }