#!/bin/csh -f
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This is a csh script which is used to fade from one image to another.
#  It uses the Wavefront compositor: imf_comp.  For usage information
#  see the "Usage" line below.
#
#
#  Author:     Wesley C. Barris
#              AHPCRC
#              Minnesota Supercomputer Center, Inc.
#  Date:       Sept. 13, 1990
#
#  Copyright @ 1990, 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
set first_file = "undefined"
set last_file = "undefined"
set base_name = "fade"
set format = "qtl_ntsc"
set image = "rla"
@ rle = 0
@ cleanup = 0
set COMP = imf_comp
set PV = pv
#
#  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]
   else if ("$argv[$n]" == "-l") then
      @ n++
      set last_file = $argv[$n]
   else if ("$argv[$n]" == "-base") then
      @ n++
      set base_name = $argv[$n]
   else if ("$argv[$n]" == "-fmt") then
      @ n++
      set format = $argv[$n]
   else if ("$argv[$n]" == "-imf") then
      @ n++
      set image = $argv[$n]
   else if ("$argv[$n]" == "-help" || "$argv[$n]" == "-h") then
      echo "Usage: fade [-n nframes] [-f first_file] [-l last_file]"
      echo "            [-base base_name] [-fmt qtl_ntsc|ntsc_4d] [-imf rla|qtl]"
      echo "Notes: Nframes must be >= 4, 30 is reasonable (and the default)."
      echo "       Default base_name: fade"
      echo "       The resulting image files will be named fade.xxxx.rla."
      echo "       Default format: qtl_ntsc"
      echo "       Default image storage: rla"
      exit
   else
      echo "Unknown argument: $argv[$n]"
      exit
   endif
   @ n++
end
#
#  Does the scene composition directory exist?
#
if ($?WF_SCMP_DIR) then
   if (-d $WF_SCMP_DIR) then
   else
      setenv WF_SCMP_DIR .
      @ cleanup = 1
   endif
else
   setenv WF_SCMP_DIR .
   setenv WF_AV_DIR /usr/wave/TAV2.11 
   set COMP = /usr/wave/TAV2.11/bin/imf_comp
   set PV = /usr/wave/TAV2.11/bin/pv
endif
if ($?WF_CMD_DIR) then
else
   setenv WF_CMD_DIR /usr/wave/lbin
endif
#
#  Let's make sure that these two image files exist.
#
if ($first_file == undefined && $last_file == undefined) then
   echo "At least one file must be specified."
   exit
endif
if ($first_file == undefined) then
   echo -n "Fading from black to "
else
   echo -n "Fading from $first_file to "
endif
if ($last_file == undefined) then
   echo "black."
else
   echo "$last_file"
endif
#
#  Are we using Utah RLE files?
#
if ($first_file != undefined) then
   if ($first_file:e == rle) then
      @ rle = 1
      echo "Converting $first_file to temp1.rla..."
      rletorla $first_file -o temp1.rla
      set first_file = temp1.rla
   endif
endif
if ($last_file != undefined) then
   if ($last_file:e == rle) then
      @ rle = 1
      echo "Converting $last_file to temp2.rla..."
      rletorla $last_file -o temp2.rla
      set last_file = temp2.rla
   endif
endif
#
#  First we've got to build a .pv file to provide the fading channels.
#
@ nfless1 = $nframes - 1
$PV <<! >&/dev/null
init fade.pv $WF_CMD_DIR/PV/safe_small.set $nframes
o 1/1
f 1 2
m = 0
f $nfless1 $nframes
m = 1
o 1/2
f 1 2
m = 1
f $nfless1 $nframes
m = 0
o 1
f 1 2 $nfless1 $nframes
cr -s
seto -d on
po 1
exit
!
#
#  Ok, here we go.  Composite all these frames while running in verbose mode.
#
if ($last_file == undefined) then
   $COMP <<EO1
   init -fs 1 -fe $nframes -i $base_name -scmp $WF_SCMP_DIR/fade.pv -v -fmt $format -imf $image
   out = file -t \$c1/1 -nf $first_file
EO1
else if ($first_file == undefined) then
   $COMP <<EO2
   init -fs 1 -fe $nframes -i $base_name -scmp $WF_SCMP_DIR/fade.pv -v -fmt $format -imf $image
   out = file -t \$c1/2 -nf $last_file
EO2
else
   $COMP <<EO3
   init -fs 1 -fe $nframes -i $base_name -scmp $WF_SCMP_DIR/fade.pv -v -fmt $format -imf $image
   a = file -t \$c1/1 -nf $first_file
   b = file -t \$c1/2 -nf $last_file
   out = a over b
EO3
endif
rm $WF_SCMP_DIR/*fade.pv
#
#  Were these rle files?
#
if ($rle == 1) then
   echo "Converting from Wavefront to rle files..."
   @ i = 1
   while ($i <= $nframes)
      set file = `echo $base_name $i | awk '{printf "%s.%04d", $1, $2}'`
      echo "Converting $file.rla to $file.rle..."
      rlatorle $file.rla >$file.rle
      rm $file.rla
      @ i++
   end
   if ($first_file != undefined) then
      rm $first_file
   endif
   if ($last_file != undefined) then
      rm $last_file
   endif
endif
if ($cleanup == 1) then
   rm *pvhist
endif
