
// AnimGIF.java
// Andrew Davison, ad@coe.psu.ac.th, April 2023

/* Load the images from the IMS_DIR directory,
   store them into a stack which can be animated,
   and save the stack as an animated GIF when its
   window is closed.

   The images come from the frames of an existing 
   animated GIF available at
   https://dribbble.com/shots/4690216-Human-walk-cycle
   which were extracted using the online GIF frame extractor 
   at https://ezgif.com/split

   Usage:
     > compile AnimGIF.java
     > run AnimGIF
*/

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import ij.*;
import ij.io.*;
import ij.gui.*;
import ij.plugin.*;
import ij.process.*;


public class AnimGIF
{
  private static final String IMS_DIR = "walking/";


  public static void main(String[] args)
  {

    ImagePlus[] imps = ImageUtils.loadImages(IMS_DIR);
    for (int i=0; i < imps.length; i++)
      IJ.run(imps[i], "RGB Color", "");  // so each frame is fully colored

    ImagePlus stack = ImagesToStack.run(imps);
    stack.show();

    IJ.run("Animation Options...",  "speed=20");  // set speed

    StackWindow stackWin = (StackWindow) stack.getWindow();
    stackWin.addWindowListener( new WindowAdapter() {
      public void windowClosing(WindowEvent e) 
      {
        System.out.println("Saving animation to AnimWalking.gif");
        IJ.saveAs(stack, "gif", "AnimWalking.gif");
        System.exit(0);
      }
    });

    System.out.println("Starting animation...");
    IJ.run("Start Animation", "");
  } /* main */


}  // end of AnimGIF class
