#!/usr/bin/perl
#
# Create the animated GIF from individual PNG files
#

$debug = 1;


# first, the "direct" animation: light from source to observer
$output_file = "./reverb_direct_anim.gif";
$delay = 50;
$loops = -1;
$merge_cmd = "~/old/bin/gifmerge -${delay} ";
if ($loops != -1) {
  $merge_cmd .= " -l${loops} ";
}

@files = glob("reverb_direct_??.png");
foreach $file (@files) {
  # convert to GIF
  $gifname = $file;
  $gifname =~ s/.png/.gif/;
  $cmd = "convert $file -background white -flatten $gifname";
  $ret = exec_cmd($cmd);

  $merge_cmd .= " $gifname ";
}
$merge_cmd .= " > $output_file ";
$ret = exec_cmd($merge_cmd);



# next, the "bounce" animation: light from source to ring, then to observer
$output_file = "./reverb_bounce_anim.gif";
$delay = 50;
$loops = -1;
$merge_cmd = "~/old/bin/gifmerge -${delay} ";
if ($loops != -1) {
  $merge_cmd .= " -l${loops} ";
}

@files = glob("reverb_bounce_??.png");
foreach $file (@files) {
  # convert to GIF
  $gifname = $file;
  $gifname =~ s/.png/.gif/;
  $cmd = "convert $file -background white -flatten $gifname";
  $ret = exec_cmd($cmd);

  $merge_cmd .= " $gifname ";
}
$merge_cmd .= " > $output_file ";
$ret = exec_cmd($merge_cmd);






exit 0;




##############################################################################
# PROCEDURE: exec_cmd
#
# DESCRIPTION: Execute the given shell command line.  If the $debug flag
#              is set, we print to stdout the command line, and
#              also print out the result string.
#
# RETURNS:
#              the result of the command
#
#
sub exec_cmd {

  my($cmd, $ret);

  $cmd = $_[0];

  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }

  return($ret);
}


