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

1    package ip.gui.frames; 
2     
3    import futils.WriterUtil; 
4    import ip.ppm.WritePPM; 
5    import j2d.ImageUtils; 
6    import j2d.ShortImageBean; 
7    import utils.Timer; 
8    import xml.Utils; 
9     
10   import java.awt.*; 
11   import java.awt.event.ActionEvent; 
12   import java.io.*; 
13   import java.util.zip.GZIPOutputStream; 
14   import java.util.zip.ZipEntry; 
15   import java.util.zip.ZipOutputStream; 
16    
17   public class SaveFrame extends NegateFrame { 
18       Menu saveMenu = getMenu("Save"); 
19    
20       private MenuItem saveAsJava_mi = 
21               addMenuItem(saveMenu, "[T-j]ava..."); 
22       private MenuItem saveAsCompactJava_mi = 
23               addMenuItem(saveMenu, "Compact Java..."); 
24       private MenuItem saveAsHexImage_mi = 
25               addMenuItem(saveMenu, "[T-h]ex image..."); 
26       private MenuItem printIcon_mi = 
27               addMenuItem(saveMenu, "binary icon..."); 
28    
29       private MenuItem saveAsGif_mi = 
30               addMenuItem(saveMenu, "[T-g]if...(not 24 bit!)"); 
31       private MenuItem saveAsPPM_mi = 
32               addMenuItem(saveMenu, "[T-p]pm..."); 
33       private MenuItem saveAsPPMgz_mi = 
34               addMenuItem(saveMenu, "[T-z]ppm.gz..."); 
35    
36       private MenuItem saveAsShortgz_mi = 
37               addMenuItem(saveMenu, "[T-s]short.gz..."); 
38       private MenuItem saveAsShortZip_mi = 
39               addMenuItem(saveMenu, "[T-i]saveAsShortZip..."); 
40       private MenuItem makeTocHtml_mi = 
41               addMenuItem(saveMenu, "makeTocHtml..."); 
42       private MenuItem print_mi = 
43               addMenuItem(saveMenu, "print..."); 
44    
45    
46       public void actionPerformed(ActionEvent e) { 
47           if (match(e,saveAsCompactJava_mi)){ 
48               saveAsCompactJava(); 
49               return; 
50           } 
51           if (match(e, saveAsHexImage_mi)) { 
52               saveAsHexImage(); 
53               return; 
54           } 
55           if (match(e, print_mi)) { 
56               ImageUtils.print(this); 
57               return; 
58           } 
59    
60           if (match(e, makeTocHtml_mi)) { 
61               makeTocHtml(); 
62               return; 
63           } 
64           if (match(e, saveAsShortZip_mi)) { 
65               saveAsShortZip(); 
66               return; 
67           } 
68           if (match(e, printIcon_mi)) { 
69               printIcon(); 
70               return; 
71           } 
72    
73           if (match(e, saveAsShortgz_mi)) { 
74               saveAsShortgz(); 
75               return; 
76           } 
77           if (match(e, saveAsPPMgz_mi)) { 
78               saveAsPPMgz(); 
79               return; 
80           } 
81           if (match(e, saveAsPPM_mi)) { 
82               saveAsPPM(); 
83               return; 
84           } 
85           if (match(e, saveAsJava_mi)) { 
86               saveAsJava(); 
87               return; 
88           } 
89           if (match(e, saveAsGif_mi)) { 
90               saveAsGif(); 
91               return; 
92           } 
93           super.actionPerformed(e); 
94    
95       } 
96    
97       SaveFrame(String title) { 
98           super(title); 
99           getFileMenu().add(saveMenu); 
100      } 
101      public void saveAsCompactJava() { 
102          ShortImageBean sib = getShortImageBean(); 
103          futils.WriterUtil.writeString(Utils.encodeImage(sib.getImage())); 
104   
105      } 
106   
107      public void saveAsHexImage() { 
108          j2d.ImageUtils.writeHexImage(getImageBean()); 
109      } 
110   
111      public static void main(String args[]) { 
112          System.out.println("Test"); 
113          SaveFrame f = new SaveFrame("save frame"); 
114          f.setVisible(true); 
115      } 
116   
117   
118      public void saveAsPPM() { 
119          String fn = WriterUtil.getSaveFileName("Save as PPM"); 
120          if (fn == null) return; 
121          saveAsPPM(fn); 
122   
123      } 
124   
125      public void saveAsPPM(String fn) { 
126          WritePPM.doIt(shortImageBean.getR(), 
127                  shortImageBean.getG(), 
128                  shortImageBean.getB(), 
129                  fn); 
130      } 
131   
132      public void saveAsPPMgz(String fn) { 
133          WritePPM wppm = new WritePPM(getImageWidth(), getImageHeight()); 
134          Timer t = new Timer(); 
135          t.start(); 
136          try { 
137              GZIPOutputStream 
138                      os = new GZIPOutputStream(new FileOutputStream(fn)); 
139              wppm.writeHeader(os); 
140              wppm.writeImage(os, shortImageBean.getR(), shortImageBean.getG(), shortImageBean.getB()); 
141              os.finish(); 
142              os.close(); 
143          } catch (Exception e) { 
144              System.out.println("Save PPM Exception - 2!"); 
145          } 
146          t.stop(); 
147          t.print(" saveAsPPMgz in "); 
148      } 
149   
150      public void saveAsPPMgz() { 
151          String fn = WriterUtil.getSaveFileName("Save as PPM.gz"); 
152          if (fn == null) return; 
153          saveAsPPMgz(fn); 
154      } 
155   
156   
157      public void saveAsShortZip() { 
158          String fn = WriterUtil.getSaveFileName("Save ZIP"); 
159          if (fn == null) return; 
160          saveAsShortZip(fn); 
161      } 
162   
163      public void saveAsShortZip(String fn) { 
164          Timer t = new Timer(); 
165          t.start(); 
166          try { 
167              FileOutputStream 
168                      fos = new 
169                              FileOutputStream(fn); 
170              ZipOutputStream 
171                      zos = new 
172                              ZipOutputStream(fos); 
173              ZipEntry zer = 
174                      new ZipEntry("r"); 
175              ZipEntry zeg = 
176                      new ZipEntry("g"); 
177              ZipEntry zeb = 
178                      new ZipEntry("b"); 
179              zer.setMethod(ZipEntry.DEFLATED); 
180              zeg.setMethod(ZipEntry.DEFLATED); 
181              zeb.setMethod(ZipEntry.DEFLATED); 
182              zos.putNextEntry(zer); 
183              ObjectOutputStream 
184                      oos = new 
185                              ObjectOutputStream(zos); 
186              oos.writeObject(shortImageBean.getR()); 
187              zos.putNextEntry(zeg); 
188              oos.writeObject(shortImageBean.getG()); 
189              zos.putNextEntry(zeb); 
190              oos.writeObject(shortImageBean.getB()); 
191              oos.close(); 
192              zos.finish(); 
193   
194          } catch (Exception e) { 
195              System.out.println("Save saveAsShortZip:" + e); 
196          } 
197          t.stop(); 
198          t.print(" saveAsOOSZip in "); 
199      } 
200   
201      public void saveAsShortZip2(String fn) { 
202          Timer t = new Timer(); 
203          t.start(); 
204          try { 
205              FileOutputStream fos = new FileOutputStream(fn); 
206              ZipOutputStream zos = new ZipOutputStream(fos); 
207              DataOutputStream dos = new DataOutputStream(zos); 
208              ZipEntry ze = new ZipEntry("quad0"); 
209              ze.setMethod(ZipEntry.DEFLATED); 
210              zos.putNextEntry(ze); 
211              writeHeader(dos); 
212              writeArray(shortImageBean.getR(), dos); 
213              writeArray(shortImageBean.getG(), dos); 
214              writeArray(shortImageBean.getB(), dos); 
215              zos.close(); 
216              zos.finish(); 
217   
218          } catch (Exception e) { 
219              System.out.println("Save saveAsShortZip:" + e); 
220          } 
221          t.stop(); 
222          t.print(" saveAsShortZip in "); 
223      } 
224   
225      public void writeArray(short a[][], DataOutputStream dos) 
226              throws IOException { 
227          for (int x = 0; x < getImageWidth(); x++) 
228              for (int y = 0; y < getImageHeight(); y++) 
229                  dos.writeShort(a[x][y]); 
230      } 
231   
232      public void writeHeader(DataOutputStream dos) 
233              throws IOException { 
234          dos.writeInt(getImageWidth()); 
235          dos.writeInt(getImageHeight()); 
236      } 
237   
238      public void saveAsShortgz() { 
239          String fn = WriterUtil.getSaveFileName("Save gz"); 
240          if (fn == null) return; 
241          saveAsShortgz(fn); 
242      } 
243   
244      public void saveAsShortgz(String fn) { 
245          Timer t = new Timer(); 
246          t.start(); 
247          try { 
248              FileOutputStream fos = new FileOutputStream(fn); 
249              GZIPOutputStream gos = new GZIPOutputStream(fos); 
250              ObjectOutputStream 
251                      oos = new 
252                              ObjectOutputStream(gos); 
253              oos.writeObject(shortImageBean.getR()); 
254              oos.writeObject(shortImageBean.getG()); 
255              oos.writeObject(shortImageBean.getB()); 
256              oos.close(); 
257              gos.finish(); 
258   
259          } catch (Exception e) { 
260              System.out.println("Save saveAsShortgz:" + e); 
261          } 
262          t.stop(); 
263          t.print(" saveAsShortgz in "); 
264      } 
265   
266      public void saveAsGif() { 
267          System.out.println("Saving as ip.gif..."); 
268          String fn = WriterUtil.getSaveFileName("Save as GIF"); 
269          if (fn == null) return; 
270          saveAsGif(fn); 
271      } 
272   
273      public void saveAsGif(String fn) { 
274          try { 
275              ip.vs.WriteGIF.DoIt(getImage(), 
276                      fn); 
277          } catch (Exception e) { 
278              e.printStackTrace(); 
279          } 
280      } 
281   
282      public void saveAsJava() { 
283          String fn = WriterUtil.getSaveFileName("Save as Java"); 
284          if (fn == null) return; 
285          saveAsJava(fn); 
286      } 
287   
288      public void saveAsxyz() { 
289          String fn = WriterUtil.getSaveFileName("Save as GIF"); 
290          if (fn == null) return; 
291          saveAsxyz(fn); 
292   
293      } 
294   
295      public void saveAsxyz(String fn) { 
296          try { 
297              FileWriter fos = new FileWriter(fn); 
298              PrintWriter pw = new PrintWriter(fos); 
299              saveAsxyz(pw); 
300              pw.flush(); 
301              fos.close(); 
302          } catch (Exception e) { 
303          } 
304      } 
305   
306      public FileOutputStream getFileOutputStream(String prompt) { 
307          FileOutputStream fos = null; 
308          try { 
309              fos = 
310                      new FileOutputStream(WriterUtil.getSaveFileName(prompt)); 
311          } catch (IOException e) { 
312              System.out.println("futil:Could not create file"); 
313          } 
314          return fos; 
315      } 
316   
317      public void makeTocHtml() { 
318          File dir = 
319                  new File(WriterUtil.getSaveDirectoryName("Enter input directory")); 
320   
321          String[] files = dir.list(new FileFilter()); 
322          System.out.println(files.length + " file(s):"); 
323          FileOutputStream fos = getFileOutputStream("Enter Save File"); 
324          PrintWriter pw = new PrintWriter(fos); 
325          pw.println("<HTML>"); 
326          pw.println("<BODY>"); 
327          pw.println("<ul>"); 
328          for (int i = 0; i < files.length; i++) 
329              pw.println("<P><IMG SRC=" + 
330                      files[i] + 
331                      "></P>" + 
332                      "<a>" + files[i] + "</a><P>"); 
333          //ps.println("<LI><a href = \"" + files[i]+"\">"+ 
334          //  files[i]+"</a><P>"); 
335          pw.println("</ul>"); 
336          pw.println("</BODY>"); 
337          pw.println("</HTML>"); 
338          try { 
339              fos.close(); 
340          } catch (Exception e) { 
341              System.out.println(e); 
342          } 
343      } 
344   
345      public void saveAsxyz(PrintWriter pw) { 
346          int y = 0; 
347          pw.println(0 + " " + 0 + " " + shortImageBean.getR()[0][0] + " 0"); 
348          for (int x = 1; x < getImageWidth(); x++) { 
349              for (y = 0; y < getImageHeight(); y++) { 
350                  pw.println(x + " " + y + " " + shortImageBean.getR()[x][y] + " 1"); 
351              } 
352              y = 0; 
353              pw.println(x + " " + y + " " + shortImageBean.getR()[x][y] + " 0"); 
354          } 
355      } 
356   
357      public void saveAsJava(String fn) { 
358          try { 
359              FileWriter fos = 
360                      new FileWriter(fn); 
361              PrintWriter pw = 
362                      new PrintWriter(fos); 
363              saveAsJava(pw); 
364              pw.flush(); 
365              fos.close(); 
366          } catch (IOException e) { 
367          } 
368      } 
369   
370      public void printIcon() { 
371          int g; 
372          System.out.println("public static byte iconName[][] = {\n"); 
373   
374          for (int y = 0; y < getImageHeight(); y++) { 
375              System.out.print("{"); 
376              for (int x = 0; x < getImageWidth() - 1; x++) { 
377                  if (shortImageBean.getR()[x][y] == 0) 
378                      g = 0; 
379                  else 
380                      g = 1; 
381                  System.out.print(g + ","); 
382              } 
383              if (shortImageBean.getR()[getImageWidth() - 1][y] == 0) 
384                  g = 0; 
385              else 
386                  g = 1; 
387              System.out.println(g + "},"); 
388   
389          } 
390          System.out.println("};"); 
391   
392      } 
393   
394      public void saveAsJava(PrintWriter pw) { 
395          int g; 
396   
397   
398          pw.println("package ip.gui;\n" 
399                  + 
400                  "\nclass NumImage {\n" 
401                  + "\tpublic static short gray[][] = {\n"); 
402   
403          for (int y = 0; y < getImageHeight(); y++) { 
404              pw.print("{"); 
405              for (int x = 0; x < getImageWidth() - 1; x++) { 
406                  g = shortImageBean.getR()[x][y]; 
407                  if (g < 10) 
408                      pw.print("  "); 
409                  else if (g < 100) pw.print(" "); 
410                  pw.print(g + ", "); 
411              } 
412              pw.println(shortImageBean.getR()[getImageWidth() - 1][y] + "},"); 
413   
414          } 
415          pw.println("};}"); 
416          System.out.println("Done writing image"); 
417   
418      } 
419   
420      public void print(String s) { 
421          infoFrame.print(s); 
422      } 
423   
424      public void println(String s) { 
425          infoFrame.println(s); 
426      } 
427   
428      class FileFilter implements FilenameFilter { 
429          public boolean accept(File dir, String name) { 
430              return new File(dir, name).isFile(); 
431          } 
432      } 
433   
434  }