/Users/lyon/j4p/src/ip/hak/RecieveFrame.java

1    package ip.hak; 
2     
3    import java.awt.*; 
4    import java.awt.event.ActionEvent; 
5    import java.awt.event.ActionListener; 
6    import java.awt.image.ColorModel; 
7    import java.awt.image.MemoryImageSource; 
8    import java.io.FileInputStream; 
9    import java.io.IOException; 
10   import java.io.ObjectInputStream; 
11   import java.util.zip.ZipEntry; 
12   import java.util.zip.ZipInputStream; 
13    
14   public class RecieveFrame extends ip.gui.frames.ClosableFrame implements ActionListener { 
15       short r[][]; 
16       short g[][]; 
17       short b[][]; 
18       int w, h; 
19       MenuBar mb = new MenuBar(); 
20       Menu fMenu = new Menu("File"); 
21       MenuItem open_MI, quit_MI; 
22       Image img = null; 
23       Button ntButton, ldButton; 
24    
25       public RecieveFrame(String title, int wi, int he) { 
26           super(title); 
27           w = wi; 
28           h = he; 
29           init(); 
30           setLocation(400, 0); 
31           setVisible(true); 
32       } 
33    
34       public void initArrays() { 
35           for (int x = 0; x < w; x++) 
36               for (int y = 0; y < h; y++) { 
37                   r[x][y] = 0; 
38                   g[x][y] = 0; 
39                   b[x][y] = 0; 
40               } 
41       } 
42    
43       public void init() { 
44           setLayout(null); 
45           int c = 203; 
46           Color mc = new Color(c, c, c); 
47           setBackground(mc); 
48           setSize(380, 320); 
49           open_MI = new MenuItem("Open Image Zip File.."); 
50           fMenu.add(open_MI); 
51           open_MI.addActionListener(this); 
52           quit_MI = new MenuItem("Quit"); 
53           fMenu.add(quit_MI); 
54           quit_MI.addActionListener(this); 
55           mb.add(fMenu); 
56           setMenuBar(mb); 
57    
58           ldButton = new Button("Load"); 
59           ldButton.setSize(50, 20); 
60           ldButton.setLocation(300, 100); 
61           add(ldButton); 
62           ldButton.setEnabled(true); 
63           ldButton.addActionListener(this); 
64    
65           ntButton = new Button("Next"); 
66           ntButton.setSize(50, 20); 
67           ntButton.setLocation(300, 140); 
68           add(ntButton); 
69           ntButton.setEnabled(false); 
70           ntButton.addActionListener(this); 
71    
72           r = new short[w][h]; 
73           g = new short[w][h]; 
74           b = new short[w][h]; 
75           initArrays(); 
76    
77       } 
78    
79       public void sendData(short sr[][], short sg[][], short sb[][], int ei) { 
80           for (int y = 0; y < ei; y++) 
81               for (int x = 0; x < w; x++) { 
82                   r[x][y] = sr[x][y]; 
83                   g[x][y] = sg[x][y]; 
84                   b[x][y] = sb[x][y]; 
85               } 
86           ip.transforms.Lifting.backwardHaar(r); 
87           ip.transforms.Lifting.backwardHaar(g); 
88           ip.transforms.Lifting.backwardHaar(b); 
89    
90           img = short2Image(w, h); 
91           repaint(); 
92       } 
93    
94       public Image short2Image(int wi, int he) { 
95           Toolkit tk = Toolkit.getDefaultToolkit(); 
96           ColorModel cm = ColorModel.getRGBdefault(); 
97    
98           int pels[] = new int[wi * he]; 
99           for (int x = 0; x < wi; x++) 
100              for (int y = 0; y < he; y++) { 
101                  pels[x + y * wi] = 0xff000000 | (r[x][y] << 16) 
102                          | (g[x][y] << 8) 
103                          | b[x][y]; 
104              } 
105          Image i = tk.createImage(new MemoryImageSource(wi, he, cm, pels, 0, wi)); 
106          return i; 
107      } 
108   
109      public void paint(Graphics g) { 
110          Rectangle rec = getBounds(); 
111          if (img == null) { 
112              g.drawRect(20, rec.height - 276, 256, 256); 
113              return; 
114          } 
115   
116          g.drawImage(img, 20, rec.height - 276, 256, 256, this); 
117   
118      } 
119   
120      public void open() { 
121          FileDialog fd = new FileDialog(this, "Select LZC file", FileDialog.LOAD); 
122          fd.setFile("*.lzc"); 
123          fd.setVisible(true); 
124   
125          String fn = fd.getFile(); 
126          if (fn == null) 
127              return; 
128          String dir = fd.getDirectory(); 
129   
130          open(dir + fn); 
131      } 
132   
133      public void open(String fn) { 
134          try { 
135              FileInputStream fis = new FileInputStream(fn); 
136              open(fis); 
137              ntButton.setEnabled(true); 
138          } catch (IOException e) { 
139              System.out.println(e); 
140          } 
141      } 
142   
143      ObjectInputStream ois; 
144      ZipInputStream zis; 
145   
146      public void open(FileInputStream fis) { 
147          zis = new ZipInputStream(fis); 
148          initArrays(); 
149   
150          readNext(zis); 
151      } 
152   
153      int sind = 0; 
154      int eind; 
155   
156      public void readNext(ZipInputStream zis) { 
157          short rt[][], gt[][], bt[][]; 
158   
159          try { 
160              ZipEntry ze = zis.getNextEntry(); 
161              ois = new ObjectInputStream(zis); 
162              try { 
163                  rt = (short[][]) ois.readObject(); 
164                  gt = (short[][]) ois.readObject(); 
165                  bt = (short[][]) ois.readObject(); 
166                  int size = rt[0].length; 
167                  eind = sind + size; 
168                  if (eind >= h) { 
169                      ntButton.setEnabled(false); 
170                      ntButton.transferFocus(); 
171                  } 
172                  ip.transforms.Lifting.forwardHaar(r); 
173                  ip.transforms.Lifting.forwardHaar(g); 
174                  ip.transforms.Lifting.forwardHaar(b); 
175                  for (int y = sind,si = 0; y < eind; y++, si++) 
176                      for (int x = 0; x < w; x++) { 
177                          r[x][y] = rt[x][si]; 
178                          g[x][y] = gt[x][si]; 
179                          b[x][y] = bt[x][si]; 
180                      } 
181                  sind = eind; 
182                  ip.transforms.Lifting.backwardHaar(r); 
183                  ip.transforms.Lifting.backwardHaar(g); 
184                  ip.transforms.Lifting.backwardHaar(b); 
185                  img = short2Image(w, h); 
186                  repaint(); 
187              } catch (ClassNotFoundException e) { 
188                  System.out.println(e); 
189              } 
190          } catch (IOException e) { 
191              System.out.println(e); 
192          } 
193      } 
194   
195      public void actionPerformed(ActionEvent e) { 
196          if (e.getSource() == open_MI) { 
197              open(); 
198              return; 
199          } 
200   
201          if (e.getSource() == ldButton) { 
202              open(); 
203              return; 
204          } 
205   
206          if (e.getSource() == quit_MI) { 
207   
208              dispose(); 
209              return; 
210          } 
211   
212          if (e.getSource() == ntButton) { 
213              readNext(zis); 
214              return; 
215          } 
216      } 
217   
218  }