/Users/lyon/j4p/src/ip/gui/ImageSequence.java

1    package ip.gui; 
2     
3    import futils.Futil; 
4    import futils.WriterUtil; 
5    import ip.transforms.Lifting; 
6     
7    import java.awt.*; 
8    import java.awt.image.ColorModel; 
9    import java.awt.image.MemoryImageSource; 
10   import java.awt.image.PixelGrabber; 
11   import java.io.*; 
12   import java.net.MalformedURLException; 
13   import java.net.URL; 
14   import java.util.Vector; 
15   import java.util.zip.GZIPInputStream; 
16   import java.util.zip.GZIPOutputStream; 
17    
18   public class ImageSequence { 
19       private int index = 0; 
20       private int width = 0; 
21       private int height = 0; 
22       private Vector imageVector = 
23               new Vector(); 
24    
25       public int getSize() { 
26           return imageVector.size(); 
27       } 
28    
29       public void setWidth(int w) { 
30           width = w; 
31       } 
32    
33       public void setHeight(int h) { 
34           height = h; 
35       } 
36    
37       public int getWidth() { 
38           return width; 
39       } 
40    
41       public int getHeight() { 
42           return height; 
43       } 
44    
45       public void setIndex(int i) { 
46           index = i; 
47       } 
48    
49       public void add(Image img) { 
50           imageVector.addElement(img); 
51       } 
52    
53       public static Image pels2Image( 
54               int pels[], int w, int h) { 
55           Toolkit tk = Toolkit.getDefaultToolkit(); 
56           return tk.createImage( 
57                   new MemoryImageSource( 
58                           w, 
59                           h, 
60                           ColorModel.getRGBdefault(), 
61                           pels, 0, 
62                           w)); 
63       } 
64    
65       public static int[] toPels(Image img) { 
66           int width = img.getWidth(null); 
67           int height = img.getHeight(null); 
68           int pels[] = new int[width * height]; 
69           PixelGrabber grabber = 
70                   new PixelGrabber( 
71                           img, 0, 0, 
72                           width, 
73                           height, 
74                           pels, 0, 
75                           width); 
76           try { 
77               grabber.grabPixels(); 
78           } catch (InterruptedException e) { 
79           } 
80           ; 
81           return pels; 
82       } 
83    
84       public void open() { 
85           open( 
86                   Futil.getReadFileName()); 
87       } 
88    
89       public void open(URL url) { 
90           try { 
91               open(url.openStream()); 
92           } catch (MalformedURLException e) { 
93               e.printStackTrace(); 
94           } catch (IOException e) { 
95               e.printStackTrace(); 
96           } catch (ClassNotFoundException e) { 
97               e.printStackTrace(); 
98           } 
99       } 
100   
101      public Image next() { 
102          Image img = (Image) 
103                  imageVector.elementAt(index++); 
104          if (index == 
105                  imageVector.size() - 1) 
106              index = 0; 
107          return img; 
108      } 
109   
110      public Image elementAt(int i) { 
111          return (Image) 
112                  imageVector.elementAt(i); 
113      } 
114   
115      public int[][] toPels() { 
116          int n = getSize(); 
117          int pels[][] = new int[n][]; 
118          for (int i = 0; i < n; i++) 
119              pels[i] = toPels(i); 
120          return pels; 
121      } 
122   
123      public int[][] forwardHaar() { 
124          int pels[][] = toPels(); 
125          for (int i = 0; i < pels.length; i++) 
126              Lifting.forwardHaar(pels[i], pels.length); 
127          resetImages(); 
128          for (int i = 0; i < pels.length; i++) 
129              add(pels2Image(pels[i], width, height)); 
130          return pels; 
131      } 
132   
133      public int[][] backwardHaar() { 
134          int pels[][] = toPels(); 
135          Lifting.backwardHaar(pels); 
136          resetImages(); 
137          for (int i = 0; i < pels.length; i++) 
138              add(pels2Image(pels[i], width, height)); 
139          return pels; 
140      } 
141   
142      public void resetImages() { 
143          imageVector = new Vector(); 
144          index = 0; 
145      } 
146   
147      public int[] toPels(int i) { 
148          return 
149                  toPels(elementAt(i)); 
150      } 
151   
152      public void save(String fn) { 
153          try { 
154              FileOutputStream fos = 
155                      new FileOutputStream(fn); 
156              GZIPOutputStream gos = 
157                      new GZIPOutputStream(fos); 
158              ObjectOutputStream oos = 
159                      new ObjectOutputStream(gos); 
160              save(oos); 
161              oos.close(); 
162              gos.finish(); 
163              gos.close(); 
164   
165          } catch (IOException e) { 
166              System.out.println(e); 
167          } 
168          System.out.println("done"); 
169      } 
170   
171      public void save( 
172              ObjectOutputStream oos) 
173              throws IOException { 
174          oos.writeInt(width); 
175          oos.writeInt(height); 
176          System.out.println(width); 
177          System.out.println(height); 
178          oos.writeInt(getSize()); 
179          for (int i = 0; i < getSize(); i++) 
180              oos.writeObject(toPels(i)); 
181      } 
182   
183      public void save() { 
184          save( 
185                  WriterUtil.getSaveFileName( 
186                          "*.imgs")); 
187      } 
188   
189      public void open(InputStream 
190              is) 
191              throws IOException, ClassNotFoundException { 
192          GZIPInputStream gis = 
193                  new GZIPInputStream(is); 
194          ObjectInputStream ois 
195                  = new ObjectInputStream(gis); 
196          open(ois); 
197          ois.close(); 
198      } 
199   
200      public void open(String fn) { 
201          imageVector = new Vector(); 
202          try { 
203              FileInputStream fis = new FileInputStream(fn); 
204              GZIPInputStream gis = new GZIPInputStream(fis); 
205              ObjectInputStream ois = new ObjectInputStream(gis); 
206              open(ois); 
207              ois.close(); 
208              //gis.finish(); 
209   
210          } catch (Exception e) { 
211              System.out.println(e); 
212          } 
213          System.out.println("done reading images"); 
214   
215      } 
216   
217      public void open( 
218              ObjectInputStream ois) 
219              throws IOException, 
220              ClassNotFoundException { 
221   
222          width = ois.readInt(); 
223          height = ois.readInt(); 
224          int numberOfImages = ois.readInt(); 
225          for (int i = 0; 
226               i < numberOfImages; 
227               i++) 
228              add( 
229                      pels2Image( 
230                              (int[]) 
231                      ois.readObject(), 
232                              width, height)); 
233      } 
234  }