/Users/lyon/j4p/src/ip/gui/frames/OpenFrame.java

1    package ip.gui.frames; 
2     
3     
4    import futils.Futil; 
5    import futils.StreamSniffer; 
6    import j2d.ImageUtils; 
7    import utils.Timer; 
8    import utils.UByte; 
9     
10   import java.awt.*; 
11   import java.awt.event.ActionEvent; 
12   import java.io.*; 
13   import java.util.zip.GZIPInputStream; 
14   import java.util.zip.ZipInputStream; 
15    
16   public class OpenFrame extends SaveFrame { 
17    
18       private MenuItem openImage_mi = addMenuItem(getOpenMenu(), "[o]pen image..."); 
19       private MenuItem openPPM_mi = addMenuItem(getOpenMenu(), "open ppm..."); 
20       private MenuItem sniff_mi = addMenuItem(getOpenMenu(), "sniff..."); 
21       private MenuItem getAsShortgz_mi = addMenuItem(getOpenMenu(), 
22               "[z]openAsShortgz"); 
23       private MenuItem getScreenShot_mi = addMenuItem(getOpenMenu(), 
24               "screen shot"); 
25       private MenuItem getShortImageZip_mi = 
26               addMenuItem(getOpenMenu(), 
27                       "getShortImageZip"); 
28    
29       public void actionPerformed(ActionEvent e) { 
30           if (match (e, getScreenShot_mi) ){ 
31               getScreenShot(); 
32               return; 
33           } 
34           if (match(e, getShortImageZip_mi)) { 
35               getShortImageZip(); 
36               return; 
37           } 
38    
39           if (match(e, getAsShortgz_mi)) { 
40               getAsShortgz(); 
41               return; 
42           } 
43           if (match(e, openImage_mi)) { 
44               openImage(); 
45               return; 
46           } 
47           if (match(e, sniff_mi)) { 
48               System.out.println("sniffer!"); 
49               ImageUtils.openAndSniffFile(); 
50               return; 
51           } 
52           if (match(e, openPPM_mi)) { 
53               openPPM(); 
54               return; 
55           } 
56           super.actionPerformed(e); 
57    
58       } 
59    
60       private void getScreenShot() { 
61           try { 
62               setImage(ImageUtils.getImage(ImageUtils.captureWholeScreen())); 
63           } catch (AWTException e) { 
64               e.printStackTrace(); 
65    
66           } 
67       } 
68    
69       public OpenFrame(String title) { 
70           super(title); 
71       } 
72    
73       public void getAsShortgz() { 
74           String fn = Futil.getReadFileName(); 
75           if (fn == null) return; 
76           Timer t = new Timer(); 
77           t.start(); 
78           try { 
79               FileInputStream fis = new FileInputStream(fn); 
80               GZIPInputStream gis = new GZIPInputStream(fis); 
81               ObjectInputStream ois = new ObjectInputStream(gis); 
82               short[][] r = (short[][]) ois.readObject(); 
83               shortImageBean.setR(r); 
84               setG((short[][]) ois.readObject()); 
85               setB((short[][]) ois.readObject()); 
86               ois.close(); 
87    
88           } catch (Exception e) { 
89               System.out.println("Open getAsShortgz Exception:" + e); 
90           } 
91           t.stop(); 
92           t.print(" getAsShortgz in "); 
93           setImageWidth(shortImageBean.getR().length); 
94           setImageHeight(shortImageBean.getR()[0].length); 
95           setSize(getImageWidth(), getImageHeight()); 
96    
97           short2Image(); 
98       } 
99    
100      public void openImage() { 
101          String fn = Futil.getReadFileName(); 
102          if (fn == null) return; 
103          File f = new File(fn); 
104          if (!f.exists()) return; 
105          setFileName(fn); 
106          InputStream is = null; 
107          try { 
108              is = new FileInputStream(fn); 
109          } catch (FileNotFoundException e) { 
110              e.printStackTrace(); 
111          } 
112   
113          StreamSniffer ss = new StreamSniffer(is); 
114          int id = ss.classifyStream(); 
115          switch (id) { 
116              case StreamSniffer.PPM: 
117                  setFileName(fn); 
118                  openPPM(fn); 
119                  break; 
120              case StreamSniffer.PPM_RAWBITS: 
121                  setFileName(fn); 
122                  openPPM(fn); 
123                  break; 
124              case StreamSniffer.GIF87a: 
125                  openGif(fn); 
126                  break; 
127              case StreamSniffer.GIF89a: 
128                  openGif(fn); 
129                  break; 
130              case StreamSniffer.JPEG: 
131                  setImageResize( 
132                          Toolkit.getDefaultToolkit().getImage( 
133                                  fn)); 
134                  setFileName(fn); 
135   
136                  break; 
137              case StreamSniffer.GZIP: 
138                  openPPMgz(fn); 
139                  break; 
140              case StreamSniffer.ZIP_ARCHIVE: 
141                  getShortImageZip(fn); 
142                  break; 
143              default: 
144                  { 
145                      System.out.println("Can not open " + ss + " as image"); 
146                  } 
147   
148          } 
149      } 
150   
151   
152      public void getShortImageZip() { 
153          String fn = Futil.getReadFileName(); 
154          if (fn == null) return; 
155          getShortImageZip(fn); 
156      } 
157   
158      public void getShortImageZip(String fn) { 
159          Timer t = new Timer(); 
160          t.start(); 
161          try { 
162              FileInputStream fis = new FileInputStream(fn); 
163              ZipInputStream zis = new ZipInputStream(fis); 
164              zis.getNextEntry(); 
165              ObjectInputStream ois = 
166                      new ObjectInputStream(zis); 
167              short[][] r = (short[][]) 
168                      ois.readObject(); 
169              shortImageBean.setR(r); 
170              setG(shortImageBean.getR()); 
171              setB(shortImageBean.getR()); 
172              setImageWidth(shortImageBean.getR().length); 
173              setImageHeight(shortImageBean.getR()[0].length); 
174              setSize(getImageWidth(), getImageHeight()); 
175              short2Image(); 
176              zis.getNextEntry(); 
177              setG((short[][]) 
178                      ois.readObject()); 
179              zis.getNextEntry(); 
180              short2Image(); 
181              setB((short[][]) 
182                      ois.readObject()); 
183              zis.close(); 
184   
185          } catch (Exception e) { 
186              System.out.println("Open getShortImageZip:" + e); 
187          } 
188          t.stop(); 
189          t.print(" getShortObjectZip in "); 
190          short2Image(); 
191      } 
192   
193      public void getShortImageZip2(String fn) { 
194          Timer t = new Timer(); 
195          t.start(); 
196          try { 
197              FileInputStream fis = new FileInputStream(fn); 
198              ZipInputStream zis = new ZipInputStream(fis); 
199              DataInputStream dis = new DataInputStream(zis); 
200              zis.getNextEntry(); 
201              readHeader(dis); 
202              readArray(shortImageBean.getR(), dis); 
203              readArray(shortImageBean.getG(), dis); 
204              readArray(shortImageBean.getB(), dis); 
205              zis.close(); 
206   
207          } catch (Exception e) { 
208              System.out.println("Open getShortImageZip:" + e); 
209          } 
210          t.stop(); 
211          t.print(" getShortImageZip in "); 
212          short2Image(); 
213      } 
214   
215      public void readArray(short a[][], DataInputStream dis) 
216              throws IOException { 
217          for (int x = 0; x < getImageWidth(); x++) 
218              for (int y = 0; y < getImageHeight(); y++) 
219                  a[x][y] = dis.readShort(); 
220      } 
221   
222      public void readHeader(DataInputStream dis) 
223              throws IOException { 
224          int width1 = dis.readInt(); 
225          setImageWidth(width1); 
226          int height1 = dis.read(); 
227          setImageHeight(height1); 
228      } 
229   
230      public static void main(String args[]) { 
231          OpenFrame f = new OpenFrame("OpenFrame"); 
232          f.show(); 
233      } 
234   
235      public void openPPMgz(String fn) { 
236          setFileName(fn); 
237          Timer t = new Timer(); 
238          try { 
239              t.start(); 
240              readShortsGz(fn); 
241              setImageWidth(shortImageBean.getR().length); 
242              setImageHeight(shortImageBean.getR()[0].length); 
243              short2Image(); 
244              t.print("Time to read and display the ppm"); 
245          } catch (Exception e) { 
246              System.out.println("Read PPM Exception:" + e); 
247          } 
248   
249      } 
250   
251      public void openPPM(String fn) { 
252          Timer t = new Timer(); 
253          try { 
254              t.start(); 
255              readShorts(fn); 
256              setSize(getImageWidth(), getImageHeight()); 
257              short2Image(); 
258              t.print("Time to read and display the ppm"); 
259          } catch (Exception e) { 
260              System.out.println("Read PPM Exception:" + e); 
261          } 
262          repaint(); 
263   
264      } 
265   
266      private void readShorts(String fn) { 
267          Timer t = new Timer(); 
268          t.start(); 
269          InputStream in = null; 
270          try { 
271              in = new FileInputStream(fn); 
272          } catch (Exception e) { 
273              e.printStackTrace(); 
274          } 
275          getShortImage(in); 
276          t.print("End ReadShorts"); 
277      } 
278   
279      private void readShortsGz(String fn) { 
280          Timer t = new Timer(); 
281          t.start(); 
282          GZIPInputStream in = null; 
283   
284          try { 
285              in = new GZIPInputStream( 
286                      new FileInputStream(fn)); 
287          } catch (Exception e) { 
288              e.printStackTrace(); 
289          } 
290          getShortImageGz(in); 
291          t.print("End ReadShorts"); 
292      } 
293   
294   
295   
296   
297      public void getShortImage(InputStream in) { 
298          try { 
299              ImageUtils.readPpmImageHeader(in); 
300          } catch (Exception e) { 
301              e.printStackTrace(); 
302          } 
303          short[][] r = new short[getImageWidth()][getImageHeight()]; 
304          shortImageBean.setR(r); 
305          setG(new short[getImageWidth()][getImageHeight()]); 
306          setB(new short[getImageWidth()][getImageHeight()]); 
307          byte buf[] = new byte[getImageWidth() * getImageHeight() * 3]; 
308          int offset = 0; 
309          int count = buf.length; 
310   
311          try { 
312              in.read(buf, offset, count); 
313          } catch (IOException e) { 
314              e.printStackTrace(); 
315          } 
316          try { 
317   
318              int j = 0; 
319              for (int col = 0; col < getImageHeight(); col++) 
320                  for (int row = 0; row < getImageWidth(); row++) { 
321                      shortImageBean.getR()[row][col] = UByte.us(buf[j++]); 
322                      shortImageBean.getG()[row][col] = UByte.us(buf[j++]); 
323                      shortImageBean.getB()[row][col] = UByte.us(buf[j++]); 
324                  } 
325          } catch (Exception e) { 
326              e.printStackTrace(); 
327          } 
328   
329   
330      } 
331   
332      private void getShortImageGz(GZIPInputStream in) { 
333   
334          try { 
335              ImageUtils.readPpmImageHeader(in); 
336          } catch (Exception e) { 
337              e.printStackTrace(); 
338          } 
339          short[][] r = new short[getImageWidth()][getImageHeight()]; 
340          shortImageBean.setR(r); 
341          setG(new short[getImageWidth()][getImageHeight()]); 
342          setB(new short[getImageWidth()][getImageHeight()]); 
343          byte buf[] = new byte[getImageWidth() * getImageHeight() * 3]; 
344          int len = 0; 
345          byte bufgz[] = new byte[1024]; 
346   
347          int n = 0; 
348          try { 
349              while ((len = in.read(bufgz)) > 0) { 
350                  System.arraycopy(bufgz, 0, buf, n, len); 
351                  n = n + len; 
352              } 
353          } catch (IOException e) { 
354              e.printStackTrace(); 
355          } 
356   
357          try { 
358   
359              int j = 0; 
360              for (int col = 0; col < getImageHeight(); col++) 
361                  for (int row = 0; row < getImageWidth(); row++) { 
362                      shortImageBean.getR()[row][col] = UByte.us(buf[j++]); 
363                      shortImageBean.getG()[row][col] = UByte.us(buf[j++]); 
364                      shortImageBean.getB()[row][col] = UByte.us(buf[j++]); 
365                  } 
366          } catch (Exception e) { 
367              e.printStackTrace(); 
368          } 
369      } 
370      public void openPPM() { 
371          String fn = Futil.getReadFileName(); 
372          if (fn == null) return; 
373          File f = new File(fn); 
374          if (!f.exists()) return; 
375          setFileName(fn); 
376          openPPM(fn); 
377      } 
378  } 
379   
380