#!/bin/csh -f
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This is a csh script that uses the SGI 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
@ ALPHA		= 12
@ SCROLL	= 13
#
#  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 = "sgi"		# default output storage format
@ mode = $FADE			# default fading mode

#
#  Check those command line args.
#
if ("$#argv" == 0) then
   echo "Usage: sgifade [-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.sgi."
   echo "       Default image storage: sgi"
   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]
      if (-e $first_file) then
      else
         echo I can\'t find $first_file
         exit
      endif
   else if ("$argv[$n]" == "-l") then
      @ n++
      set last_file = $argv[$n]
      if (-e $last_file) then
      else
         echo I can\'t find $last_file
         exit
      endif
   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]" == "-alpha") then
      @ mode = $ALPHA
   else if ("$argv[$n]" == "-scroll") then
      @ mode = $SCROLL
      @ nframes = 180
   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 spline60 = (0 0.0015 0.0048 0.0098 0.0164 0.0245 0.0340 0.0449 0.0570 0.0702 0.0846 0.1001 0.1165 0.1338 0.1519 0.1709 0.1906 0.2109 0.2318 0.2533 0.2753 0.2978 0.3206 0.3438 0.3673 0.3911 0.4151 0.4392 0.4635 0.4878 0.5121 0.5364 0.5607 0.5848 0.6088 0.6326 0.6561 0.6793 0.7021 0.7246 0.7466 0.7681 0.7890 0.8093 0.8290 0.8480 0.8661 0.8834 0.8998 0.9153 0.9297 0.9429 0.9550 0.9659 0.9754 0.9835 0.9901 0.9951 0.9984 1)

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)

set spline15 = (0.0 0.0233 0.0702 0.1356 0.2150 0.3045 0.4006 0.5000 0.5993 0.6954 0.7849 0.8643 0.9297 0.9766 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 "
   if ($first_file:e == sgi) then
      cp $first_file junk1$$.sgi
   else
      fromutah $first_file junk1$$.sgi
   endif
   set width = `istat junk1$$.sgi | sed -e 1,1d | awk '{print $1}'`
   set height = `istat junk1$$.sgi | sed -e 1,1d | awk '{print $2}'`
endif
if ($last_file == undefined) then
   echo "black."
   set args = `imgsize junk1$$.sgi`
   nullimg junk2$$.sgi $args
else
   echo "$last_file"
   if ($last_file:e == sgi) then
      cp $last_file junk2$$.sgi
   else
      fromutah $last_file junk2$$.sgi
   endif
   set width = `istat junk2$$.sgi | sed -e 1,1d | awk '{print $1}'`
   set height = `istat junk2$$.sgi | sed -e 1,1d | awk '{print $2}'`
endif
if ($first_file == "undefined") then
   set args = `imgsize junk2$$.sgi`
   nullimg junk1$$.sgi $args
endif
set w = `echo $width | awk '{print ($1-1)/2}'`
set h = `echo $height | awk '{print ($1-1)/2}'`
@ xmax = $width - 1
@ ymax = $height - 1
#
#  Perform the fade.
#
@ i = 1
while ($i <= $nframes)
   echo "Creating $i of $nframes..."
   switch ($mode)
      case $FADE
         if ($nframes == 15) then
            blend junk1$$.sgi junk2$$.sgi junk3$$.sgi $spline15[$i]
         else if ($nframes == 60) then
            blend junk1$$.sgi junk2$$.sgi junk3$$.sgi $spline60[$i]
         else
            blend junk1$$.sgi junk2$$.sgi junk3$$.sgi $spline30[$i]
         endif
         breaksw
      case $ALPHA
         imadjust -intensity 0-$spline30[$i] -set -alpha 0 junk1$$.sgi junk3$$.rle
         iristorle junk2$$.sgi | rlecomp junk3$$.rle over - | rletoiris -o junk3$$.sgi
         rm -f junk3$$.rle
         breaksw
      case $SCROLL
         set fract = `echo $i $nframes | awk '{print ($1 - 1)/($2 - 1)}'`
         @ yroll = `echo $height $fract | awk '{print $1*$2 + .5}' | awk -F'.' '{print $1}'`
         @ y1 = 1 + $ymax - $yroll
         if ($y1 >= $ymax) then
            cp junk1$$.sgi junk3$$.sgi
         else
            subimg junk2$$.sgi junk3$$.sgi 0 $xmax $y1 $ymax
            over junk1$$.sgi junk3$$.sgi temp.sgi 0 $y1
            iroll temp.sgi junk3$$.sgi 0 $yroll
         endif
         breaksw
      case $IN
         @ j = 31 - $i
         @ x1 = `echo $spline30[$j] $w | awk '{print $2 - $1*$2 + 0.5}' | awk -F'.' '{print $1}'`
         @ x2 = `echo $spline30[$j] $w | awk '{print $2 + $1*$2 + 0.5}' | awk -F'.' '{print $1}'`
         @ y1 = `echo $spline30[$j] $h | awk '{print $2 - $1*$2 + 0.5}' | awk -F'.' '{print $1}'`
         @ y2 = `echo $spline30[$j] $h | awk '{print $2 + $1*$2 + 0.5}' | awk -F'.' '{print $1}'`
         subimg junk1$$.sgi temp.sgi $x1 $x2 $y1 $y2
         over junk2$$.sgi temp.sgi junk3$$.sgi $x1 $y1
         breaksw
      case $OUT
         @ x1 = `echo $spline30[$i] $w | awk '{print $2 - $1*$2 + 0.5}' | awk -F'.' '{print $1}'`
         @ x2 = `echo $spline30[$i] $w | awk '{print $2 + $1*$2 + 0.5}' | awk -F'.' '{print $1}'`
         @ y1 = `echo $spline30[$i] $h | awk '{print $2 - $1*$2 + 0.5}' | awk -F'.' '{print $1}'`
         @ y2 = `echo $spline30[$i] $h | awk '{print $2 + $1*$2 + 0.5}' | awk -F'.' '{print $1}'`
         subimg junk2$$.sgi temp.sgi $x1 $x2 $y1 $y2
         over junk1$$.sgi temp.sgi junk3$$.sgi $x1 $y1
         breaksw
      case $NOISE
         addnoise junk1$$.sgi junk3$$.sgi junk2$$.sgi $spline30[$i]
         breaksw
      case $SCALEUP
         @ x1 = `echo $spline30[$i] $w | awk '{print $2 - $1*$2 + 0.5}' | awk -F'.' '{print $1}'`
         @ y1 = `echo $spline30[$i] $h | awk '{print $2 - $1*$2 + 0.5}' | awk -F'.' '{print $1}'`
         izoom junk2$$.sgi temp.sgi $spline30[$i] $spline30[$i] -i
         over junk1$$.sgi temp.sgi junk3$$.sgi $x1 $y1
         breaksw
      case $SCALEDOWN
         @ j = 31 - $i
         @ x1 = `echo $spline30[$j] $w | awk '{print $2 - $1*$2 + 0.5}' | awk -F'.' '{print $1}'`
         @ y1 = `echo $spline30[$j] $h | awk '{print $2 - $1*$2 + 0.5}' | awk -F'.' '{print $1}'`
         izoom junk1$$.sgi temp.sgi $spline30[$j] $spline30[$j] -i
         over junk2$$.sgi temp.sgi junk3$$.sgi $x1 $y1
         breaksw
      case $ROLLUP
         @ yroll = `echo $height $spline30[$i] | awk '{print $1*$2 + .5}' | awk -F'.' '{print $1}'`
         @ y1 = 1 + $ymax - $yroll
         if ($y1 >= $ymax) then
            cp junk1$$.sgi junk3$$.sgi
         else
            subimg junk2$$.sgi junk3$$.sgi 0 $xmax $y1 $ymax
            over junk1$$.sgi junk3$$.sgi temp.sgi 0 $y1
            iroll temp.sgi junk3$$.sgi 0 $yroll
         endif
         breaksw
      case $ROLLDOWN
         @ yroll = `echo $height $spline30[$i] | awk '{print $1*$2 + .5}' | awk -F'.' '{print $1}'`
         @ y1 = $yroll - 1
         if ($y1 <= 0) then
            cp junk1$$.sgi junk3$$.sgi
         else
            subimg junk2$$.sgi junk3$$.sgi 0 $xmax 0 $y1
            over junk1$$.sgi junk3$$.sgi temp.sgi 0 0
            iroll temp.sgi junk3$$.sgi 0 -$yroll
         endif
         breaksw
      case $ROLLRIGHT
         @ xroll = `echo $width $spline30[$i] | awk '{print $1*$2 + .5}' | awk -F'.' '{print $1}'`
         @ x1 = 1 + $xmax - $xroll
         if ($x1 >= $xmax) then
            cp junk1$$.sgi junk3$$.sgi
         else
            subimg junk2$$.sgi junk3$$.sgi $x1 $xmax 0 $ymax
            over junk1$$.sgi junk3$$.sgi temp.sgi $x1 0
            iroll temp.sgi junk3$$.sgi $xroll 0
         endif
         breaksw
      case $ROLLLEFT
         @ xroll = `echo $width $spline30[$i] | awk '{print $1*$2 + .5}' | awk -F'.' '{print $1}'`
         @ x1 = $xroll - 1
         if ($x1 <= 0) then
            cp junk1$$.sgi junk3$$.sgi
         else
            subimg junk2$$.sgi junk3$$.sgi 0 $x1 0 $ymax
            over junk1$$.sgi junk3$$.sgi temp.sgi 0 0
            iroll temp.sgi junk3$$.sgi -$xroll 0
         endif
         breaksw
      case $SHEAR
         @ nlevels = `echo $spline30[$i] | awk '{print $1*256 + .5}' | awk -F'.' '{print $1}'`
         shear junk1$$.sgi junk3$$.sgi $spline30[$i]
         breaksw
   endsw
   set outfile = `echo $base_name $i $image | awk '{printf "%s.%04d.%s", $1, $2, $3}'`
   if ($image == "rle") then
      toutah junk3$$.sgi $outfile
   else if ($image == "sgi") then
      mv junk3$$.sgi $outfile
   else if ($image == "rla") then
      toutah junk3$$.sgi junk4.rle
      rletorla -o $outfile junk4.rle
   else if ($image == "yuv") then
      toyuv junk3$$.sgi $outfile
   endif
   @ i++
end
#
#  Clean up shop.
#
rm junk*.sgi
if (-e temp.sgi) then
   rm temp.sgi
endif
