/Users/lyon/j4p/src/ip/apurva/ZipFrame.java

1    package ip.apurva; 
2     
3    import futils.Futil; 
4    import futils.WriterUtil; 
5    import ip.gui.ImageSequence; 
6    import ip.gui.frames.TopFrame; 
7    import ip.transforms.Lifting; 
8     
9    import java.awt.Menu; 
10   import java.awt.MenuBar; 
11   import java.awt.MenuItem; 
12   import java.awt.event.ActionEvent; 
13   import java.awt.event.ActionListener; 
14   import java.io.FileInputStream; 
15   import java.io.FileOutputStream; 
16   import java.io.ObjectInputStream; 
17   import java.io.ObjectOutputStream; 
18   import java.util.Vector; 
19   import java.util.zip.ZipEntry; 
20   import java.util.zip.ZipInputStream; 
21   import java.util.zip.ZipOutputStream; 
22    
23   public class ZipFrame extends TopFrame 
24           implements ActionListener { 
25       ImageSequence imgsq = new ImageSequence(); 
26       int count = 1; 
27       Vector imgvecr = new Vector(); 
28       Vector imgvecg = new Vector(); 
29       Vector imgvecb = new Vector(); 
30    
31       MenuBar mb = getMenuBar(); 
32       Menu Zipmenu = new Menu("Wave Zip"); 
33       MenuItem next_mi = addMenuItem(Zipmenu, 
34                                      "Next Entry"); 
35       MenuItem ForLift_mi = addMenuItem(Zipmenu, 
36                                         "Forward Lifting"); 
37       MenuItem BackLift_mi = addMenuItem(Zipmenu, 
38                                          "BackWard Lifting"); 
39       MenuItem DHT_mi = addMenuItem(Zipmenu, 
40                                     "Split Radix for DHT"); 
41    
42    
43       Menu s_plus_p_menu = new Menu("S + P Xforms"); 
44       MenuItem S_plus_P_forward = addMenuItem( 
45               s_plus_p_menu, "S + P Forward"); 
46       MenuItem clearband1 = addMenuItem( 
47               s_plus_p_menu, "Clear 1/2 band"); 
48       MenuItem clearband2 = addMenuItem( 
49               s_plus_p_menu, "Clear 1/4 band"); 
50       MenuItem clearband3 = addMenuItem( 
51               s_plus_p_menu, "Clear 1/8 band"); 
52       MenuItem S_plus_P_inverse = addMenuItem( 
53               s_plus_p_menu, "S + P Inverse"); 
54    
55       public void actionPerformed(ActionEvent e) { 
56           if (match(e, DHT_mi)) { 
57               dHT_mi(); 
58           } 
59           if (match(e, S_plus_P_forward)) { 
60               forward(); 
61           } 
62           if (match(e, clearband1)) { 
63               clear_band1(); 
64           } 
65           if (match(e, clearband2)) { 
66               clear_band2(); 
67           } 
68           if (match(e, clearband3)) { 
69               clear_band3(); 
70           } 
71           if (match(e, S_plus_P_inverse)) { 
72               inverse(); 
73           } 
74           if (match(e, next_mi)) { 
75               nextentry(); 
76               return; 
77           } 
78           if (match(e, ForLift_mi)) { 
79               forwardzip(); 
80               return; 
81           } 
82           if (match(e, BackLift_mi)) { 
83               backwardzip(); 
84               return; 
85           } 
86           super.actionPerformed(e); 
87       } 
88    
89    
90       public void dHT_mi() { 
91           /*DHT_SR dht_mi = new DHT_SR(); 
92           dht_mi.initframe(r,g,b); 
93           short2Image();*/ 
94       } 
95    
96       public void forward() { 
97           Lifting.forwardHaar( 
98                   shortImageBean.getR()); 
99           Lifting.forwardHaar( 
100                  shortImageBean.getG()); 
101          Lifting.forwardHaar( 
102                  shortImageBean.getB()); 
103          SPxform.S_plus_P_Xform( 
104                  shortImageBean.getR()); 
105          SPxform.S_plus_P_Xform( 
106                  shortImageBean.getG()); 
107          SPxform.S_plus_P_Xform( 
108                  shortImageBean.getB()); 
109          short2Image(); 
110      } 
111   
112      public void clear_band1() { 
113          clear_band(0, 
114                     getImageHeight() / 2, 
115                     getImageWidth(), 
116                     getImageHeight()); 
117          short2Image(); 
118      } 
119   
120      public void clear_band2() { 
121          clear_band(0, 
122                     getImageHeight() / 4, 
123                     getImageWidth(), 
124                     getImageHeight()); 
125          short2Image(); 
126      } 
127   
128      public void clear_band3() { 
129          clear_band(0, 
130                     getImageHeight() / 8, 
131                     getImageWidth(), 
132                     getImageHeight()); 
133          short2Image(); 
134   
135      } 
136   
137      public void clear_band(int x, 
138                             int y, 
139                             int x1, 
140                             int y1) { 
141          for (int i = x; i < x1; i++) { 
142              for (int j = y; j < y1; j++) { 
143                  shortImageBean.getR()[i][j] = 0; 
144                  shortImageBean.getG()[i][j] = 0; 
145                  shortImageBean.getB()[i][j] = 0; 
146              } 
147          } 
148      } 
149   
150      public void inverse() { 
151          SPxform.inversXform( 
152                  shortImageBean.getR()); 
153          SPxform.inversXform( 
154                  shortImageBean.getG()); 
155          SPxform.inversXform( 
156                  shortImageBean.getB()); 
157          Lifting.backwardHaar( 
158                  shortImageBean.getR()); 
159          Lifting.backwardHaar( 
160                  shortImageBean.getG()); 
161          Lifting.backwardHaar( 
162                  shortImageBean.getB()); 
163          short2Image(); 
164      } 
165   
166      public void nextentry() { 
167          System.out.println("" + count); 
168          short[][] r = (short[][]) imgvecr.elementAt( 
169                  count); 
170          shortImageBean.setR(r); 
171          setG( 
172                  (short[][]) imgvecg.elementAt( 
173                          count)); 
174          setB( 
175                  (short[][]) imgvecb.elementAt( 
176                          count)); 
177          Lifting.backwardHaar( 
178                  shortImageBean.getR()); 
179          Lifting.backwardHaar( 
180                  shortImageBean.getB()); 
181          Lifting.backwardHaar( 
182                  shortImageBean.getG()); 
183          short2Image(); 
184          setImageWidth( 
185                  shortImageBean.getR().length); 
186          setImageHeight( 
187                  shortImageBean.getR()[0].length); 
188          setSize(getImageWidth(), 
189                  getImageHeight()); 
190          count--; 
191          if (count == -1) 
192              count = imgvecr.size() - 1; 
193      } 
194   
195      public void forwardzip() { 
196          String filename = WriterUtil.getSaveFileName( 
197                  "Save as Zip file"); 
198          try { 
199              //writing to a zip file 
200              FileOutputStream fos = new FileOutputStream( 
201                      filename); 
202              ZipOutputStream zos = new ZipOutputStream( 
203                      fos); 
204              Lifting.forwardHaar( 
205                      shortImageBean.getR()); 
206              Lifting.forwardHaar( 
207                      shortImageBean.getG()); 
208              Lifting.forwardHaar( 
209                      shortImageBean.getB()); 
210              int j, i; 
211              for ( 
212                      i = getImageHeight(), j = 0; 
213                      i > getImageHeight() / 64; i = 
214                                                 i / 
215                                                 2 
216                                                 , 
217                                                 j++) { 
218                  ZipEntry zer = new ZipEntry( 
219                          "r" + i + ""); 
220                  ZipEntry zeg = new ZipEntry( 
221                          "g" + i + ""); 
222                  ZipEntry zeb = new ZipEntry( 
223                          "b" + i + ""); 
224                  zer.setMethod(ZipEntry.DEFLATED); 
225                  zeg.setMethod(ZipEntry.DEFLATED); 
226                  zeb.setMethod(ZipEntry.DEFLATED); 
227                  zos.putNextEntry(zer); 
228                  ObjectOutputStream oos = new ObjectOutputStream( 
229                          zos); 
230                  //clearing the sub bands by the multiples of 2 
231                  clearQuad(0, 
232                            i, 
233                            getImageWidth(), 
234                            getImageHeight()); 
235                  //writing serialized object to the gzip file 
236                  oos.writeObject( 
237                          shortImageBean.getR()); 
238                  zos.putNextEntry(zeg); 
239                  oos.writeObject( 
240                          shortImageBean.getG()); 
241                  zos.putNextEntry(zeb); 
242                  oos.writeObject( 
243                          shortImageBean.getB()); 
244              } 
245              zos.finish(); 
246              //applying backward lifting xform to show the last image od the sequence 
247              Lifting.backwardHaar( 
248                      shortImageBean.getR()); 
249              Lifting.backwardHaar( 
250                      shortImageBean.getB()); 
251              Lifting.backwardHaar( 
252                      shortImageBean.getG()); 
253              short2Image(); 
254          } catch (Exception e) { 
255              System.out.println( 
256                      "Save as Zip file :" + e); 
257          } 
258      } 
259   
260   
261      public void backwardzip() { 
262          String filename = Futil.getReadFileName(); 
263   
264   
265          try { 
266              FileInputStream fos = new FileInputStream( 
267                      filename); 
268              ZipInputStream gos = new ZipInputStream( 
269                      fos); 
270              ZipEntry ze; 
271              for ( 
272                      int i = getImageHeight(); 
273                      i > getImageHeight() / 64; i = 
274                                                 i / 
275                                                 2) { 
276                  ze = gos.getNextEntry(); 
277                  ze.setMethod(ZipEntry.DEFLATED); 
278                  ObjectInputStream oos = new ObjectInputStream( 
279                          gos); 
280                  short[][] r = (short[][]) oos.readObject(); 
281                  shortImageBean.setR(r); 
282                  imgvecr.addElement( 
283                          shortImageBean.getR()); 
284                  ze = gos.getNextEntry(); 
285                  ze.setMethod(ZipEntry.DEFLATED); 
286                  setB( 
287                          (short[][]) oos.readObject()); 
288                  imgvecg.addElement( 
289                          shortImageBean.getG()); 
290                  ze = gos.getNextEntry(); 
291                  ze.setMethod(ZipEntry.DEFLATED); 
292                  setG( 
293                          (short[][]) oos.readObject()); 
294                  imgvecb.addElement( 
295                          shortImageBean.getB()); 
296                  setImageWidth( 
297                          shortImageBean.getR() 
298                          .length); 
299                  setImageHeight( 
300                          shortImageBean.getR()[0].length); 
301                  setSize(getImageWidth(), 
302                          getImageHeight()); 
303              } 
304              count = imgvecr.size() - 1; 
305              Lifting.backwardHaar( 
306                      shortImageBean.getR()); 
307              Lifting.backwardHaar( 
308                      shortImageBean.getB()); 
309              Lifting.backwardHaar( 
310                      shortImageBean.getG()); 
311              short2Image(); 
312              gos.close(); 
313          } catch (Exception e) { 
314              System.out.println( 
315                      "Open as Zip file :" + e); 
316          } 
317   
318      } 
319   
320      public void clearQuad(int x1, 
321                            int y1, 
322                            int x2, 
323                            int y2) { 
324          for (int x = x1; x < x2; x++) 
325              for (int y = y1; y < y2; y++) { 
326                  shortImageBean.getR()[x][y] = 0; 
327                  shortImageBean.getG()[x][y] = 0; 
328                  shortImageBean.getB()[x][y] = 0; 
329              } 
330      } 
331   
332      ZipFrame() { 
333          super("MyZipFile"); 
334          mb.add(Zipmenu); 
335          mb.add(s_plus_p_menu); 
336   
337   
338      } 
339   
340      public static void main(String args[]) { 
341          new ZipFrame(); 
342      } 
343   
344  }