BeginPackage["GMT`GMT`"]

(*  GifMovieTool.m allows to export                        *)
(*  your animated MATHEMATICA pictures as GIF 89 files     *)
(*  in UNIX, this is done directly from Mathematica        *)

(*  File name: GMT.m                                       *)
(*  Version_1.1: Jul 31 1996 by Oliver Knill               *)
(*  Version_1.2: Oct 20 1996 with Options by M. Rychlik in *)
(* /MathSource/Enhancements/Graphics/Animation/0208-358    *)
(*  Version_1.3: Jan 30 1997 version for Mathematica 3.0   *)

(*  Oliver Knill                                           *)
(*  Department of Mathematics                              *)
(*  University of Arizona, Tucson, AZ                      *)
(*  e-mail:  knill@math.arizona.edu                        *)
(*  http://www2.math.arizona.edu/~knill                    *)

(*  For the UNIX program GIFMerge                          *)
(*  http://www.iis.ee.ethz.ch/~kiwi/GIFMerge/              *)

(*  On a PC or Mac, you have to put the Gif files into the *)
(*  corresponding program like for example GIFBUILDER for  *)
(*  the Macintosh                                          *)
(*  Exporting movies directly from the Mac via Quicktime   *)
(*  and then translating into a GifMovie is also possible. *)

SaveGif      ::usage="SaveGif[PictureName,Graphics] exports
                      Graphics a gif file with name PictureName   "; 
GifFileName  ::usage="GetFileName[128] produces a string GMTfile128";
GifMerge     ::usage="GifMerge[MovieName] exports a Gif 89 file 
                      under the name  MovieName  using all gif files
                      beginning with the string GMT which are in the directory"; 
MakeGifMovie::usage="MakeGifMovie[P,Name,1,40] exports a movie with 
                     40 pictures using a procedure P which renders for 
                     every natural number a Mathematica Picture"; 
DevelopGifMovie::usage="DevelopGifMovie[GraphicRout,PictExe,Init] exports 
                     a movie while iterating a procedure"; 
Example1       ::usage="Example using MakeGifMovie."; 
Example2       ::usage="Example using DevelopeGifMovie]"; 

Options[SaveGif]=Options[MakeGifMovie]=Options[DevelopGifMovie]={
  ImageSize->144,
  ImageResolution->200}

Begin["`Private`"]

If[$VersionNumber<3.0,Print["This version is for Mathematica 3.0. 
  Please use the program GMT_2.0"];Quit[]];  

SaveGif[FileName_,PictName_,opt___]:=Display[FileName,PictName,"GIF",opt]; 

GetFileName[n_]:=Block[{u,v,w},
  u=IntegerDigits[n]+48; v=ToCharacterCode["GMTfile"];
  w=ToCharacterCode[".gif"];
  FromCharacterCode[Join[v,u,w]]];

GifMerge[MovieName_]:=Block[{},
  Run["gifmerge -l0 -1 GMTfile*.gif > GMTmovie.tmp"];
  DeleteFile[FileNames["GMTfile*.gif"]];
  CopyFile["GMTmovie.tmp",MovieName]; 
  DeleteFile["GMTmovie.tmp"]]; 

MakeGifMovie[Pict_,MovieName_,FirstN_,LastN_,opts___]:= Block[{},
  Do[SaveGif[GetFileName[1000+i],Pict[i],opts],{i,FirstN,LastN}];
  GifMerge[MovieName]];

DevelopGifMovie[PictExeP_,StepExeP_,InitPoint_,
                MovieName_,NumberOfIter_,opts___]:=Block[{S},
  S=InitPoint; 
  Do[SaveGif[GetFileName[1000+i],PictExeP[S],opts]; 
  S=StepExeP[S],{i,NumberOfIter}];
  GifMerge[MovieName]];

Example1:=Block[{}, P=N[Pi];
  WaveP[k_]:=Plot3D[Sin[3x-2P k/20] Cos[y-2P k/20],{x,0,5},{y,0,5},
    Boxed->False, Axes->False,PlotPoints->20,
    PlotRange->{-1,1},DisplayFunction->Identity];
  MakeGifMovie[WaveP,"BigMovie.gif",1,20,
    ImageSize->4.0*72,
    ImageResolution->50]]; 

Example2:=Block[{},InitP={0.211234234,0.4555456234234}; 
  PictExe1[S_]:=Show[Graphics[{Hue[S[[2]]],Disk[S,0.04]},
    DisplayFunction->Identity, Frame->True, Ticks->False,
    PlotRange->{{0,1},{0,1}},AspectRatio->1]]; 
  StepExe1[S_]:=Block[{},Mod[{{2,1},{1,1}}.S,1]];
  DevelopGifMovie[PictExe1,StepExe1,InitP,"Cat.gif",20,
    ImageSize->4.0*72,
    ImageResolution->50]]; 

End[]
EndPackage[]

