#!/bin/csh -f
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This is a csh script that uses the SGI image tools to generate a
#  fade between a series of images.
#
#  Author:     Wesley C. Barris
#              AHPCRC
#              Minnesota Supercomputer Center, Inc.
#  Date:       November 18, 1991
#
#  Copyright @ 1991, Minnesota Supercomputer Center, Inc.
#
#  RESTRICTED RIGHTS LEGEND
#
#  Use, duplication, or disclosure of this software and its documentation
#  by the Government is subject to restrictions as set forth in subdivision
#  { (b) (3) (ii) } of the Rights in Technical Data and Computer Software
#  clause at 52.227-7013.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  Set some defaults.
#
@ nframes = 30			# total number of files created (1 sec)
set first_file = "undefined"
set last_file = "undefined"
set base_name = "fade"		# default base name of output files
set image = "sgi"		# default output storage format
#
#  Check those command line args.
#
@ n = 1
while ($n <= $#argv)
   if ("$argv[$n]" == "-n") then
      @ n++
      set nframes = $argv[$n]
   else if ("$argv[$n]" == "-f") then
      @ n++
      set first_file = $argv[$n]
      @ n++
      @ first_b = $argv[$n]
   else if ("$argv[$n]" == "-l") then
      @ n++
      set last_file = $argv[$n]
      @ n++
      @ last_b = $argv[$n]
   else if ("$argv[$n]" == "-base") then
      @ n++
      set base_name = $argv[$n]
   else if ("$argv[$n]" == "-imf") then
      @ n++
      set image = $argv[$n]
   else if ("$argv[$n]" == "-15") then
      @ nframes = 15
   else if ("$argv[$n]" == "-help" || "$argv[$n]" == "-h") then
      echo "Usage: movingfade [-f first_file start] [-l last_file start]"
      echo "            [-base_name base] [-imf rle|sgi|rla|yuv]"
      echo "Notes: Default base_name: fade"
      echo "       The resulting image files will be named fade.xxxx.sgi."
      echo "       Default image storage: sgi"
      exit
   else
      echo "Unknown argument: $argv[$n]"
      exit
   endif
   @ n++
end
#
#  Define a spline smoothly going from 0.0 to 1.0.
#
set spline30 = (0.0 0.0 0.0069 0.0214 0.0426 0.0697 0.1021 0.1389 0.1796 0.2236 0.2702 0.3191 0.3696 0.4212 0.4736 0.5263 0.5787 0.6303 0.6808 0.7297 0.7763 0.8203 0.8610 0.8978 0.9302 0.9573 0.9785 0.9930 1.0 1.0)
set spline15 = (0.0 0.0 0.0308 0.0917 0.1753 0.2749 0.3849 0.5 0.6150 0.7250 0.8246 0.9082 0.9691 1.0 1.0)
#
#  Just what are we supposed to do?
#
if ($first_file == "undefined") then
   echo -n "Fading from black to "
#   echo "The first file is undefined\!"
#   exit
else
   set junk = `echo $first_file $first_b | awk '{printf "%s.%04d", $1, $2}'`
   if (-e $junk.sgi) then
      set ext = sgi
      set args = `imgsize $junk.sgi`
   else if (-e $junk.rle) then
      set ext = rle
      @ xsize = `rlehdr -h -b $junk.rle | awk -F'[' '{print $3}' | awk -F, '{printf $1}'`
      @ ysize = `rlehdr -h -b $junk.rle | awk -F']' '{print $2}' | awk -F, '{printf $2}'`
      set args = `echo $xsize $ysize 3`
   else if (-e $junk.rla) then
      set ext = rla
      @ xsize = `rlatorle $junk.rla | rlehdr -h -b | awk -F'[' '{print $3}' | awk -F, '{printf $1}'`
      @ ysize = `rlatorle $junk.rla | rlehdr -h -b | awk -F']' '{print $2}' | awk -F, '{printf $2}'`
      set args = `echo $xsize $ysize 3`
   endif
   echo -n "Fading from $junk.$ext to "
endif
if ($last_file == undefined) then
   echo "black."
   set lf1 = black$$.sgi
   set lf2 = black$$.sgi
   nullimg $lf2 $args
#   echo "The last file is undefined\!"
#   exit
else
   set junk = `echo $last_file $last_b | awk '{printf "%s.%04d", $1, $2}'`
   if (-e $junk.sgi) then
      set ext = sgi
      set args = `imgsize $junk.sgi`
   else if (-e $junk.rle) then
      set ext = rle
      @ xsize = `rlehdr -h -b $junk.rle | awk -F'[' '{print $3}' | awk -F, '{printf $1}'`
      @ ysize = `rlehdr -h -b $junk.rle | awk -F']' '{print $2}' | awk -F, '{printf $2}'`
      set args = `echo $xsize $ysize 3`
   else if (-e $junk.rla) then
      set ext = rla
      @ xsize = `rlatorle $junk.rla | rlehdr -h -b | awk -F'[' '{print $3}' | awk -F, '{printf $1}'`
      @ ysize = `rlatorle $junk.rla | rlehdr -h -b | awk -F']' '{print $2}' | awk -F, '{printf $2}'`
      set args = `echo $xsize $ysize 3`
   endif
   set junk = `echo $last_file $last_b $nframes | awk '{printf "%s.%04d", $1, $2+$3-1}'`
   echo "$junk.$ext."
endif
if ($first_file == "undefined") then
   set ff1 = black$$.sgi
   set ff2 = black$$.sgi
   nullimg $ff2 $args
endif
#
#  Perform the fade.
#
@ i = 1
while ($i <= $nframes)
   if ($first_file != "undefined") then
      set ff1 = `echo $first_file $i $first_b $ext | awk '{printf "%s.%04d.%s", $1, $2+$3-1, $4}'`
      if ($ext == sgi) then
         set ff2 = $ff1
      else if ($ext == rle) then
         set ff2 = first$$.sgi
         fromutah $ff1 $ff2
      else if ($ext == rla) then
         set ff2 = first$$.sgi
         rlatorle $ff1 | rletoiris -o $ff2
      endif
   endif
   if ($last_file != "undefined") then
      set lf1 = `echo $last_file $i $last_b $ext | awk '{printf "%s.%04d.%s", $1, $2+$3-1, $4}'`
      if ($ext == sgi) then
         set lf2 = $lf1
      else if ($ext == rle) then
         set lf2 = last$$.sgi
         fromutah $lf1 $lf2
      else if ($ext == rla) then
         set lf2 = last$$.sgi
         rlatorle $lf1 | rletoiris -o $lf2
      endif
   endif
#
# Do the actual blending.
#
   echo "Creating $i of $nframes using $ff1 and $lf1..."
   if ($nframes == 30) then
      blend $ff2 $lf2 junk3.sgi $spline30[$i]
   else
      blend $ff2 $lf2 junk3.sgi $spline15[$i]
   endif
#
# Convert to the desired output format.
#
   set outfile = `echo $base_name $i | awk '{printf "%s.%04d", $1, $2}'`
   if ($image == sgi) then
      mv junk3.sgi $outfile.sgi
   else if ($image == rle) then
      toutah junk3.sgi $outfile.rle
      rm -f junk3.sgi
   else if ($image == rla) then
      toutah junk3.sgi junk.rle
      rletorla junk.rle >$outfile.rla
      rm -f junk3.sgi
   else if ($image == yuv) then
      toyuv junk3.sgi $outfile.yuv
      rm -f junk3.sgi
   endif
   @ i++
end
#
#  Clean up shop.
#
if ($first_file == "undefined") then
   rm -f $ff2 *$$.sgi
else if ($last_file == "undefined") then
   rm -f $lf2 *$$.sgi
endif
