/Users/lyon/j4p/src/j2d/animation/GifUtils.java

1    package j2d.animation; 
2     
3    import futils.Futil; 
4    import ip.gif.stills.GifDecoder; 
5    import ip.gif.gifAnimation.Gif89Encoder; 
6    import j2d.ImageProcessorFactory; 
7    import j2d.ImageProcessorInterface; 
8    import j2d.ImageUtils; 
9     
10   import java.awt.AWTException; 
11   import java.awt.Image; 
12   import java.awt.image.BufferedImage; 
13   import java.io.*; 
14    
15   /** 
16    * Created by User: lyon Date: May 4, 2003 Time: 
17    * 11:35:37 AM 
18    */ 
19   public class GifUtils { 
20       /** 
21        * Save still_images to a gif animation file. 
22        * Annotation is used for the sequence. If 
23        * looped is true, the animation repeats. 
24        * 
25        * @param still_images      images to be written 
26        * @param annotation        text to be embedded 
27        *                          into the gif animation 
28        * @param looped            true means looped 
29        *                          on playback 
30        * @param frames_per_second an int 
31        * @param os                OutputStream to 
32        *                          send animation 
33        *                          to. 
34        * @throws IOException If you run out of disk 
35        *                     space 
36        */ 
37       public static void writeGif( 
38               Image[] still_images, 
39               String annotation, 
40               boolean looped, 
41               double frames_per_second, 
42               OutputStream os) throws IOException { 
43           Gif89Encoder ge = new Gif89Encoder(); 
44           for (int i = 0; i < still_images.length; ++i) 
45               ge.addFrame(still_images[i]); 
46           ge.setComments(annotation); 
47           ge.setLoopCount(looped ? 
48                           0 : 
49                           1); 
50           ge.setUniformDelay( 
51                   (int) Math.round( 
52                           100 / frames_per_second)); 
53           ge.encode(os); 
54       } 
55    
56       /** 
57        * Save imgs into a directory, one image per 
58        * file. Using GIF format 
59        * 
60        * @param imgs a sequence of images to be 
61        *             written 
62        */ 
63       public static void writeGifStills( 
64               Image imgs[]) { 
65           writeGifStills(imgs, 
66                          Futil.getReadDirFile( 
67                                  "select gif output dir")); 
68       } 
69    
70       /** 
71        * Save imgs array into a directory, fn, using 
72        * GIF form, one image per file. 
73        * 
74        * @param imgs     array of images to be 
75        *                 saved 
76        * @param fileName to be used as the root for 
77        *                 the output files 
78        */ 
79       public static void writeGifStills( 
80               Image imgs[], File fileName) { 
81           for (int i = 0; i < imgs.length; i++) { 
82               writeGif(imgs[i], 
83                        fileName.toString() + i + 
84                        ".gif"); 
85           } 
86    
87       } 
88    
89       /** 
90        * Saves a 256 color image to a file. 
91        * 
92        * @param img An image to be saved 
93        * @param fn  a string containing the file 
94        */ 
95       public static void writeGif(Image img, 
96                                   String fn) { 
97           try { 
98               ip.vs.WriteGIF.DoIt(img, 
99                                   fn); 
100          } catch (IOException e) { 
101   
102          } catch (AWTException e) { 
103   
104          } 
105      } 
106   
107      /** 
108       * save <code>imgs</code> out to a gif 
109       * animation file 
110       * 
111       * @param imgs  array of 256 color images 
112       * @param speed speed, in fps, to display gif 
113       *              animation 
114       */ 
115      public static void writeGif(Image imgs[], 
116                                  int speed) { 
117          try { 
118              FileOutputStream fos = 
119                      Futil.getFileOutputStream( 
120                              "output gif animation"); 
121              writeGif(imgs, 
122                       "A gif animation", 
123                       true, 
124                       speed, 
125                       fos); 
126              fos.close(); 
127          } catch (IOException e) { 
128              e.printStackTrace(); 
129          } 
130   
131      } 
132   
133      /** 
134       * Create a gif animation from an 
135       * <code>ImageProcessorFactory</code> 
136       * 
137       * @param sourceImage    the input image 
138       * @param ipf            the image processor 
139       *                       factory instance 
140       * @param numberOfImages 
141       * @param speed 
142       */ 
143      public static void writeGifs( 
144              Image sourceImage, 
145              ImageProcessorFactory ipf, 
146              int numberOfImages, 
147              int speed) { 
148          try { 
149              FileOutputStream fos = 
150                      Futil.getFileOutputStream( 
151                              "output gif animation"); 
152              Gif89Encoder ge = new Gif89Encoder(); 
153              for (int i = 1; i < numberOfImages; i++) { 
154                  ImageProcessorInterface ip = ipf.getProcessor( 
155                          i); 
156                  try { 
157                      Image img = ip.process( 
158                              sourceImage); 
159                      ge.addFrame(img); 
160                  } catch (Exception e) { 
161                      System.out.println( 
162                              "error on image:" + 
163                              i); 
164                      continue; 
165                  } 
166   
167                  ge.setUniformDelay( 
168                          Math.round(100 / speed)); 
169                  System.out.println( 
170                          "processed frame:" + i); 
171              } 
172              System.out.println("done!"); 
173   
174              ge.setLoopCount(true ? 
175                              0 : 
176                              1); 
177   
178              ge.encode(fos); 
179              fos.close(); 
180          } catch (IOException e) { 
181              e.printStackTrace(); 
182          } 
183      } 
184   
185      /** 
186       * Get a gif animation as an array of image. 
187       * 
188       * @return the image array 
189       */ 
190      public static Image[] getGifs() { 
191          GifDecoder gd = new GifDecoder(); 
192          FileInputStream fis = 
193                  Futil.getFileInputStream( 
194                          "select a gif animation file"); 
195          BufferedInputStream bis = 
196                  new BufferedInputStream(fis); 
197   
198          gd.read(bis); 
199          int nof = gd.getFrameCount(); 
200          Image[] ia = new Image[nof]; 
201          for (int i = 0; i < nof; i++) { 
202              BufferedImage bi = gd.getFrame(i); 
203              ia[i] = ImageUtils.getImage(bi); 
204              System.out.println("got image#" + i); 
205          } 
206          Futil.close(fis); 
207          return ia; 
208      } 
209  } 
210