#!/bin/csh -f
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This is a csh script that can be used to rotoscope over a series of
#  images using the SGI program: imp.
#
#
#  Author:     Wesley C. Barris
#              AHPCRC
#              Minnesota Supercomputer Center, Inc.
#  Date:       Feb. 17, 1992
#
#  Copyright @ 1992, 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.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  Here are some default thingies...
#
@ start         = 1             # start sending this file
@ end           = 30            # stop sending files after this one
@ dlast		= 0		# display the last frame?
@ cont		= 0		# continuous (one frame)?
set base_name   = 'comp'        # use this base name
set out_name    = 'roto'        # base name of rotoscoped files
set extension   = 'undefined'   # extension of files to look for
set image       = 'sgi'         # extension of rotoscoped files
#
#  Read command line arguments.
#
@ n = 1
while ($n <= $#argv)
   if ("$argv[$n]" == "-fs") then
      @ n++
      @ start = $argv[$n]
   else if ("$argv[$n]" == "-fe") then
      @ n++
      @ end = $argv[$n]
   else if ("$argv[$n]" == "-base") then
      @ n++
      set base_name = $argv[$n]
   else if ("$argv[$n]" == "-out") then
      @ n++
      set out_name = $argv[$n]
   else if ("$argv[$n]" == "-ext") then
      @ n++
      set extension = $argv[$n]
   else if ("$argv[$n]" == "-img") then
      @ n++
      set image = $argv[$n]
   else if ("$argv[$n]" == "-cont") then
      set cont = 1
   else if ("$argv[$n]" == "-dl") then
      @ dlast = 1
   else
      echo "Usage: rotoscope [-fs start] [-fe end] [-base basename]"
      echo "                 [-out roto_base] [-img output-format]"
      echo "If options are omitted, the following defaults will be used."
      echo "rotoscope -fs 1 -fe 30 -base comp -out roto -img sgi"
      exit
   endif
   @ n++
end
#
#  What kind of files do we see here?
#
if ($extension == undefined) then
   set file_name = `echo $base_name $start | awk '{printf "%s.%04d",$1,$2}'`
   if (-e $file_name.rle) then
      set extension = 'rle'
   else if (-e $file_name.sgi) then
      set extension = 'sgi'
   else if (-e $file_name.yuv) then
      set extension = 'yuv'
   else if (-e $file_name.qtl) then
      set extension = 'qtl'
   else if (-e $file_name.rla) then
      set extension = 'rla'
   else
      echo "I can't find any files to send???"
      exit
   endif
   echo "Found ${file_name}.$extension"
endif
#
#  Use imp to rotoscope each image.
#
@ i = $start
while ($i <= $end)
#
#  Convert the input file to SGI format.
#
   if ($cont == 0) then
      if ($extension == rle) then
         set file_name = `echo $base_name $i | awk '{printf "%s.%04d.rle",$1,$2}'`
         echo "Reading $file_name..."
         fromutah $file_name in.rgb
      else if ($extension == rla) then
         set file_name = `echo $base_name $i | awk '{printf "%s.%04d.rla",$1,$2}'`
         echo "Reading $file_name..."
         fromrla $file_name in.rgb
      else if ($extension == sgi) then
         set file_name = `echo $base_name $i | awk '{printf "%s.%04d.sgi",$1,$2}'`
         echo "Reading $file_name..."
         cp $file_name in.rgb
      else if ($extension == yuv) then
         set file_name = `echo $base_name $i | awk '{printf "%s.%04d.yuv",$1,$2}'`
         echo "Reading $file_name..."
         fromyuv $file_name in.rgb
      else if ($extension == qtl) then
         set file_name = `echo $base_name $i | awk '{printf "%s.%04d.qtl",$1,$2}'`
         echo "Reading $file_name..."
         fromyuv $file_name in.rgb
      endif
   endif
#
#  Display the previous image for reference.
#
   if (-e last-frame) then
      ipaste last-frame
      @ ippid = `ps | awk '/ipaste/ {print $1}'`
   endif
#
#  Loop here while imp is running.
#
   imp in.rgb
   set pid = `ps | awk '/imp/ {print $1}'`
   while ($pid)
      sleep 5
      @ pid = `ps | awk '/imp/ {print $1}'`
   end
#
#  imp occasionally dumps core, check for that here.
#
   if (-e core) then
      rm core
      echo "Opps.  We crashed."
      echo "To continue type: $argv[0] -fs $i ..."
      exit
   endif
#
#  Did we create a out.rgb file?
#
   if (-e out.rgb) then
#
#  Convert to desired output format.
#
   if ($?ippid) then
      kill -9 $ippid
   endif
      set file_name = `echo $out_name $i $image | awk '{printf "%s.%04d.%s",$1,$2,$3}'`
      echo "Writing $file_name..."
      if ($dlast != 0) then
         izoom out.rgb last-frame .5 .5
      endif
      if ($image == rle) then
         toutah out.rgb $file_name:r.rle
         rm out.rgb
      else if ($image == rla) then
         toutah out.rgb junk.rle
         rm out.rgb
         rletorla -o $file_name:r.rla junk.rle
      else if ($image == sgi) then
         mv out.rgb $file_name:r.sgi
      else if ($image == yuv) then
         toyuv out.rgb $file_name:r.yuv
         rm out.rgb
      else if ($image == qtl) then
         toyuv out.rgb $file_name:r.qtl
         rm out.rgb
      endif
      @ i++
   else
      echo "Woah\!  You did not save the file\! -- redoing..."
   endif
end
if ($dlast != 0) then
   rm last-frame
endif
