#!/bin/csh -f
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This is a csh script that uses the URT image tools to generate a
#  fade between two 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.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
@ FADE		= 1
@ IN		= 2
@ OUT		= 3
@ NOISE		= 4
@ SCALEUP	= 5
@ SCALEDOWN	= 6
@ ROLLUP	= 7
@ ROLLDOWN	= 8
@ ROLLRIGHT	= 9
@ ROLLLEFT	= 10
@ SHEAR		= 11
#
#  Set some defaults.
#
@ nframes = 30			# total number of files created (1 sec)
set base_name = fade		# basename of output rle files
set first_file = "undefined"
set last_file = "undefined"
set base_name = "fade"		# default base name of output files
set image = "rle"		# default output storage format
@ mode = $FADE			# default fading mode
#
#  Check those command line args.
#
if ("$#argv" == 0) then
   echo "Usage: urtfade [-f first_file] [-l last_file] [-in|-out]"
   echo "            [-base base] [-imf rle|sgi|rla|yuv]"
   echo "Notes: Default base_name: fade"
   echo "       The resulting image files will be named fade.xxxx.rle."
   echo "       Default image storage: rle"
   exit
endif
@ 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]" == "-imf") then
      @ n++
      set image = $argv[$n]
   else if ("$argv[$n]" == "-fade") then
      @ mode = $FADE
   else if ("$argv[$n]" == "-in") then
      @ mode = $IN
   else if ("$argv[$n]" == "-out") then
      @ mode = $OUT
   else if ("$argv[$n]" == "-noise") then
      @ mode = $NOISE
   else if ("$argv[$n]" == "-scaleup") then
      @ mode = $SCALEUP
   else if ("$argv[$n]" == "-scaledown") then
      @ mode = $SCALEDOWN
   else if ("$argv[$n]" == "-rollup") then
      @ mode = $ROLLUP
   else if ("$argv[$n]" == "-rolldown") then
      @ mode = $ROLLDOWN
   else if ("$argv[$n]" == "-rollright") then
      @ mode = $ROLLRIGHT
   else if ("$argv[$n]" == "-rollleft") then
      @ mode = $ROLLLEFT
   else if ("$argv[$n]" == "-shear") then
      @ mode = $SHEAR
   else if ("$argv[$n]" == "-15") then
      @ nframes = 15
   else if ("$argv[$n]" == "-help" || "$argv[$n]" == "-h") then
      echo "Usage: sgifade [-f first_file] [-l last_file]"
      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.rle."
      echo "       Default image storage: rle"
      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.0060 0.0187 0.0374 0.0613 0.0900 0.1228 0.1591 0.1986 0.2407 0.2849 0.3309 0.3782 0.4265 0.4754 0.5245 0.5734 0.6217 0.6690 0.7150 0.7593 0.8013 0.8408 0.8771 0.9099 0.9386 0.9625 0.9812 0.9939 1.0000)
#
#  Just what are we supposed to do?
#
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
#
#  Perform the fade.
#
echo "Generating the fade -- please wait..."
switch ($mode)
   case $FADE
      if (-e ${base_name}_000.rle) then
         echo "\n${base_name}_000.rle already exists\! -- aborting..."
         exit
      endif
      if ($first_file == "undefined") then
         rleinterp -n $nframes -o $base_name.rle "" $last_file >&/dev/null
      else if ($last_file == undefined) then
         rleinterp -n $nframes -o $base_name.rle $first_file >&/dev/null
      else
         rleinterp -n $nframes -o $base_name.rle $first_file $last_file >&/dev/null
      endif
      @ i = 1
      while ($i <= $nframes)
         echo "Creating $i of $nframes..."
         @ junk = $i - 1
         set oldname = `echo $base_name $junk | awk '{printf "%s_%03d.rle", $1, $2}'`
         set newname = `echo $base_name $i $image | awk '{printf "%s.%04d.%s", $1, $2, $3}'`
         if ($image == "rle") then
            mv $oldname $newname
         else if ($image == "sgi") then
            rletoiris -o $newname $oldname
            rm $oldname
         else if ($image == "rla") then
            rletorla -o $newname $oldname
            rm $oldname
         else if ($image == "yuv") then
            rletoyuv -o $newname $oldname
            rm $oldname
         endif
         @ i++
      end
      breaksw
   default
      echo "Sorry, that fade mode is not yet supported."
      breaksw
endsw
