#!/bin/csh -f
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This is a csh script which can be used to send image data to the
#  Abekas A60.
#
#  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.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
@ NTSC	= 1
@ XL	= 2
@ XLL	= 3
@ SOL	= 4
@ PANDORA = 5
@ ICON  = 6
@ MICON = 7
@ USER  = 10
#
#  Convert an image to NTSC size (720x486).
#
@ xntsc = 720		#1.48
@ yntsc = 486
@ xxl 	= 2048		#1.33
@ yxl 	= 1536
@ xxll	= 2048		#1.00
@ yxll	= 2048
@ xsol	= 2048		#1.50
@ ysol	= 1366
@ xpandora = 600	#1.50
@ ypandora = 400
@ xicon  = 85	#1.27
@ yicon  = 67
@ xmicon  = 67	#1.33
@ ymicon  = 50
set image = rle
set mode = "scale"
set output = "undefined"
@ white = 0
@ omode = $NTSC
set CONVTMP = /usr/tmp/$$.sgi
set FULLTMP = /usr/tmp/full$$.sgi
set DONETMP = /usr/tmp/done$$.sgi
set BACKTMP = /usr/tmp/back$$.sgi
#
if ($#argv == 0) then
   echo "Usage: tontsc [-center|-fit|-fill|-scale|-tile]"
   echo "              [-sol|-pandora|-xl]"
   echo "              [-imf rle|rla|yuv|sgi|display] [-o output_name]"
   echo "              image_file_name"
   echo "   -center:center image on an NTSC sized window"
   echo "   -fit:   scale image indepently (distorting) in x and y to fill NTSC"
   echo "   -fill:  scale image uniformly to fill NTSC, clipping excess"
   echo "   -scale: scale image uniformly to fit onto NTSC without clipping any"
   echo "   -tile:  tile image to fill NTSC"
   echo "   image_file_name: name of image file (with extension)"
   echo " "
   echo "Recognized file types:  .scr"
   echo "			.scr.Z"
   echo "			.sr"
   echo "			.sr.Z"
   echo "			.gif"
   echo "			.rle"
   echo "			.rla"
   echo "			.sgi"
   echo "			.xbm"
   echo "			.ppm"
   echo "			.tiff"
   echo "			.jpg"
   echo " "
   echo "If you do not see your favorite file type in this list, see Wes."
   exit
endif
#
#  Test the command line args.
#
@ n = 1
while ($n <= $#argv)
   switch ("$argv[$n]")
      case -black
         @ white = 0
         breaksw
      case -white
         @ white = 1
         breaksw
      case -t
      case -tile
         set mode = "tile"
         breaksw
      case -s
      case -scale
         set mode = "scale"
         breaksw
      case -f
      case -fill
         set mode = "fill"
         breaksw
      case -fit
         set mode = "fit"
         breaksw
      case -c
      case -center
         set mode = "center"
         breaksw
      case -4x3t
      case -xlt
      case -xlprintt
      case -4x3
      case -xlprint
      case -xl
         @ white = 1
         @ omode = $XL
         set image = sgi
         breaksw
      case -xll
         @ white = 1
         @ omode = $XLL
         set image = sgi
         breaksw
      case -pan
      case -pandora
         @ omode = $PANDORA
         @ white = 1
         set image = sgi
         breaksw
      case -solt
         @ white = 1
      case -sol
         @ omode = $SOL
         breaksw
      case -icon
         @ omode = $ICON
         set image = sgi
         breaksw
      case -micon
         @ omode = $MICON
         set mode = "fill"
         set image = gif
         breaksw
      case -user
         @ n++
         @ xuser = $argv[$n]
         @ n++
         @ yuser = $argv[$n]
         @ omode = $USER
         breaksw
      case -imf
         @ n++
         set image = $argv[$n]
         breaksw
      case -o
         @ n++
         set output = $argv[$n]
         breaksw
      default
         set src = $argv[$n]
   endsw
   @ n++
end
set name = $src:t
if (-e $src) then
   echo "Converting $name to iris rgb format..."
else
   echo "I can't find $name"
   exit
endif
#
#  Convert it
#
if ($src:e == gif) then
   fromgif $src $CONVTMP
else if ($src:e == Z) then
   set junk = $src:r
   if ($junk:e == scr || $junk:e == sr) then
      uncompress $src
      fromsun $src:r $CONVTMP
      compress $src:r
   else if ($junk:e == rle) then
      uncompress $src
      fromutah $src:r $CONVTMP
      compress $src:r
   else
      echo Unknown compressed file type.
      exit
   endif
   set name = $src:t
   set name = $name:r
else if ($src:e == rle) then
   set nchan = `rlehdr -h $src | awk '/Saved/ {print $2}'`
   if ($nchan != 3) then
#
#  I believe that these are the only references to URT commands.
#
      set mapchan = `rlehdr -h $src | awk '/color map/ {print $1}'`
      if ($mapchan == 3) then
         echo "8 bit color mapped image -- hang on..."
         applymap $src | rletoiris -o $CONVTMP
      else
         echo "8 bit grayscale image -- hang on..."
         rleswap -f 0,0,0 $src | rletoiris -o $CONVTMP
      endif
   else
      fromutah $src $CONVTMP
   endif
else if ($src:e == xbm) then
   fromxbm $src $CONVTMP
else if ($src:e == ppm) then
   fromppm $src $CONVTMP
else if ($src:e == rla) then
   fromrla $src $CONVTMP
else if ($src:e == sgi || $src:e == rgb) then
   cp $src $CONVTMP
else if ($src:e == tiff) then
   fromtiff $src $CONVTMP
else if ($src:e == jpg) then
   djpeg $src >$$.ppm
   fromppm $$.ppm $CONVTMP
   rm $$.ppm
else
   echo "Unknown file extension. -- See Wes"
   exit
endif
#
#  Get the size.
#
@ xsize = `istat $CONVTMP | sed -e 1,1d | awk '{print $1}'`
@ ysize = `istat $CONVTMP | sed -e 1,1d | awk '{print $2}'`
echo "Your image size: $xsize $ysize"
if ($omode == $NTSC) then
   if ($xsize == $xntsc && $ysize == $yntsc) then
      set mode = easy
   endif
   @ xoutput = $xntsc
   @ youtput = $yntsc
   set add = "ntsc"
else if ($omode == $SOL) then
   if ($xsize == $xsol && $ysize == $ysol) then
      set mode = easy
   else if ($ysize > $xsize) then
      echo Rotating your image...
      iflip $CONVTMP $$.sgi 90
      mv $$.sgi $CONVTMP
      @ junk = $xsize
      @ xsize = $ysize
      @ ysize = $junk
   endif
   @ xoutput = $xsol
   @ youtput = $ysol
   set add = "sol"
else if ($omode == $PANDORA) then
   if ($xsize == $xpandora && $ysize == $ypandora) then
      set mode = easy
   endif
   @ xoutput = $xpandora
   @ youtput = $ypandora
   set add = "pan"
else if ($omode == $XL) then
   if ($xsize == $xxl && $ysize == $yxl) then
      set mode = easy
   else
      @ xsize = `istat $CONVTMP | sed -e 1,1d | awk '{print $1}'`
      @ ysize = `istat $CONVTMP | sed -e 1,1d | awk '{print $2}'`
      if ($ysize > $xsize) then
         echo Rotating your image...
         iflip $CONVTMP $$.sgi 90
         mv $$.sgi $CONVTMP
         @ junk = $xsize
         @ xsize = $ysize
         @ ysize = $junk
      endif
      @ aspect = `echo $xsize $ysize | nawk '{print int(($1/$2)*100)}'`
      @ diff = `echo $aspect | nawk '{print $1-133}'`
      if ($diff < -1) then
         if ($mode == fill) then
            @ xoutput = $xsize
            @ youtput = `echo $xsize | nawk '{print int($1/1.3333)}'`
            @ y1 = `echo $ysize $youtput | nawk '{print int(($1-$2)*0.5)}'`
            @ y2 = $y1 + $youtput - 1
            @ x1 = 0
            @ x2 = $xoutput - 1
         else
            @ xoutput = `echo $ysize | nawk '{print int($1*1.3333)}'`
            @ youtput = $ysize
         endif
      else if ($diff > 1) then
         if ($mode == fill) then
            @ xoutput = `echo $ysize | nawk '{print int($1*1.3333)}'`
            @ youtput = $ysize
            @ x1 = `echo $xsize $xoutput | nawk '{print int(($1-$2)*0.5)}'`
            @ x2 = $x1 + $xoutput - 1
            @ y1 = 0
            @ y2 = $youtput - 1
         else
            @ xoutput = $xsize
            @ youtput = `echo $xsize | nawk '{print int($1/1.3333)}'`
         endif
      else
         set mode = easy
      endif
   endif
   set add = "xl"
else if ($omode == $XLL) then
   if ($xsize == $xxll && $ysize == $yxll) then
      set mode = easy
   else
      @ xsize = `istat $CONVTMP | sed -e 1,1d | awk '{print $1}'`
      @ ysize = `istat $CONVTMP | sed -e 1,1d | awk '{print $2}'`
      @ aspect = `echo $xsize $ysize | nawk '{print int(($1/$2)*100)}'`
      if ($aspect < 100) then
         if ($mode == fill) then
            @ xoutput = $xsize
            @ youtput = $xsize
            @ x1 = 0
            @ x2 = $xoutput - 1
            @ y1 = `echo $ysize $youtput | nawk '{print int(($1-$2)*0.5)}'`
            @ y2 = $y1 + $youtput - 1
         else
            @ xoutput = $ysize
            @ youtput = $ysize
         endif
      else if ($aspect >= 100) then
         if ($mode == fill) then
            @ xoutput = $ysize
            @ youtput = $ysize
            @ x1 = `echo $xsize $xoutput | nawk '{print int(($1-$2)*0.5)}'`
            @ x2 = $x1 + $xoutput - 1
            @ y1 = 0
            @ y2 = $youtput - 1
         else
            @ xoutput = $xsize
            @ youtput = $xsize
         endif
      else
         set mode = easy
      endif
   endif
   set add = "xl"
else if ($omode == $ICON) then
   if ($xsize == $xicon && $ysize == $yicon) then
      set mode = easy
   endif
   @ xoutput = $xicon
   @ youtput = $yicon
   set add = "icon"
else if ($omode == $MICON) then
   if ($xsize == $xmicon && $ysize == $ymicon) then
      set mode = easy
   endif
   @ xoutput = $xmicon
   @ youtput = $ymicon
   set add = "icon"
else if ($omode == $USER) then
   if ($xsize == $xuser && $ysize == $yuser) then
      set mode = easy
   endif
   @ xoutput = $xuser
   @ youtput = $yuser
   set add = "user"
endif
#
#  Either tile, scale, or center the image.
#
switch ($mode)
   case scale
      if ($omode == $XL || $omode == $XLL) then
         cp $CONVTMP $FULLTMP
         echo "Desired image size: $xoutput $youtput"
      else
         set xscale = `echo $xoutput $xsize | awk '{print $1/$2}'`
         set yscale = `echo $youtput $ysize | awk '{print $1/$2}'`
         @ xlessy = `echo $xscale $yscale | awk '{print 1000*($1-$2)}' | awk -F. '{print $1}'`
#echo $xlessy
         @ xt1 = `echo $xscale | awk -F. '{print $1}'`
         @ yt1 = `echo $yscale | awk -F. '{print $1}'`
         echo "xscale: $xscale; yscale: $yscale"
         echo -n "Scaling image by "
         if ($xt1 > $yt1) then
            echo "$yscale to fit ${xoutput}x${youtput}..."
            izoom $CONVTMP $FULLTMP $yscale $yscale
         else if ($xt1 < $yt1) then
            echo "$xscale to fit ${xoutput}x${youtput}..."
            izoom $CONVTMP $FULLTMP $xscale $xscale
         else if ($xt1 == 0 && $yt1 == 0) then
#  If the image is being scaled down, use the bigger scale.
            if ($xlessy >= 0) then
               echo "$yscale to fit ${xoutput}x${youtput}..."
               izoom $CONVTMP $FULLTMP $yscale $yscale
            else
               echo "$xscale to fit ${xoutput}x${youtput}..."
               izoom $CONVTMP $FULLTMP $xscale $xscale
            endif
         else if ($xt1 > 0 && $yt1 > 0) then
#  If the image is being scaled up, use the smaller scale.
            if ($xlessy >= 0) then
               echo "$yscale to fit ${xoutput}x${youtput}..."
               izoom $CONVTMP $FULLTMP $yscale $yscale
            else
               echo "$xscale to fit ${xoutput}x${youtput}..."
               izoom $CONVTMP $FULLTMP $xscale $xscale
            endif
         else
            echo "I don't know how to scale this image..."
            rm -i $CONVTMP
            exit
         endif
         @ xsize = `istat $FULLTMP | sed -e 1,1d | awk '{print $1}'`
         @ ysize = `istat $FULLTMP | sed -e 1,1d | awk '{print $2}'`
         echo "Scaled image size: $xsize $ysize"
      endif
#
#  We have an image (full), check the size and center it on a background.
#
      if ($xsize == $xoutput && $ysize == $youtput) then
         mv $FULLTMP $DONETMP
      else
#
#  Center the image over the background.
#
         @ xpos = `echo $xoutput $xsize | awk '{print ($1-$2)/2}' | awk -F. '{print $1}'`
         @ ypos = `echo $youtput $ysize | awk '{print ($1-$2)/2}' | awk -F. '{print $1}'`
         echo "Final image origin: $xpos $ypos"
#
#  Make a background.
#
         if ($white == 0) then
            echo "Making a black backgound..."
            nullimg $BACKTMP $xoutput $youtput 3
         else
            echo "Making a white image backgound..."
            conimg $BACKTMP $xoutput $youtput 255 255 255
         endif
#
#  Composite the two...
#
         echo "Compositing your scaled image over the background..."
         over $BACKTMP $FULLTMP $DONETMP $xpos $ypos
         rm $FULLTMP $BACKTMP
      endif
      breaksw
   case fill
      if ($omode == $XL || $omode == $XLL) then
         subimg $CONVTMP $DONETMP $x1 $x2 $y1 $y2
         echo "Desired image size: $xoutput $youtput"
      else
         set xscale = `echo $xoutput $xsize | awk '{print $1/$2}'`
         set yscale = `echo $youtput $ysize | awk '{print $1/$2}'`
         @ xlessy = `echo $xscale $yscale | awk '{print 1000*($1-$2)}' | awk -F. '{print $1}'`
#echo $xlessy
         @ xt1 = `echo $xscale | awk -F. '{print $1}'`
         @ yt1 = `echo $yscale | awk -F. '{print $1}'`
         echo "xscale: $xscale; yscale: $yscale"
         echo -n "Scaling image by "
         if ($xt1 > $yt1) then
            echo "$xscale to fill ${xoutput}x${youtput}..."
            if ($xscale == 1) then
               cp $CONVTMP $FULLTMP
            else
               izoom $CONVTMP $FULLTMP $xscale $xscale
            endif
         else if ($xt1 < $yt1) then
            echo "$yscale to fill ${xoutput}x${youtput}..."
            izoom $CONVTMP $FULLTMP $yscale $yscale
         else if ($xt1 == 0 && $yt1 == 0) then
#  If the image is being scaled down, use the bigger scale.
            if ($xlessy >= 0) then
               echo "$xscale to fill ${xoutput}x${youtput}..."
               izoom $CONVTMP $FULLTMP $xscale $xscale
            else
               echo "$yscale to fill ${xoutput}x${youtput}..."
               izoom $CONVTMP $FULLTMP $yscale $yscale
            endif
         else if ($xt1 > 0 && $yt1 > 0) then
#  If the image is being scaled up, use the smaller scale.
            if ($xlessy >= 0) then
               echo "$xscale to fill ${xoutput}x${youtput}..."
               izoom $CONVTMP $FULLTMP $xscale $xscale
            else
               echo "$yscale to fill ${xoutput}x${youtput}..."
               izoom $CONVTMP $FULLTMP $yscale $yscale
            endif
         else
            echo "I don't know how to scale this image..."
            rm -i $CONVTMP
            exit
         endif
#
#  We have an image (full), check the size and center it on a background.
#
         @ xsize = `istat $FULLTMP | sed -e 1,1d | awk '{print $1}'`
         @ ysize = `istat $FULLTMP | sed -e 1,1d | awk '{print $2}'`
         echo "Scaled image size: $xsize $ysize"
         echo $xsize $xoutput $ysize $youtput
         if ($xsize == $xoutput && $ysize == $youtput) then
            mv $FULLTMP $DONETMP
         else
#
#  Cut out the center of the image.
#
            @ xpos = `echo $xsize $xoutput | awk '{print ($1-$2)/2}' | awk -F. '{print $1}'`
            @ ypos = `echo $ysize $youtput | awk '{print ($1-$2)/2}' | awk -F. '{print $1}'`
            @ x2 = $xpos + $xoutput - 1
            @ y2 = $ypos + $youtput - 1
            echo "Cutting out a ${xoutput}x${youtput} at x= $xpos y= $ypos..."
            subimg $FULLTMP $DONETMP $xpos $x2 $ypos $y2
            rm $FULLTMP
         endif
      endif
      breaksw
   case easy
      echo "Your image fits perfectly\!..."
      cp $CONVTMP $DONETMP
      breaksw
   case fit
      set xscale = `echo $xoutput $xsize | awk '{print $1/$2}'`
      set yscale = `echo $youtput $ysize | awk '{print $1/$2}'`
      echo "Scaling image by xscale: $xscale; yscale: $yscale..."
      izoom $CONVTMP $DONETMP $xscale $yscale
      breaksw
   case tile
      echo "Tiling the file into a large image..."
      tile $CONVTMP $DONETMP $xoutput $youtput
      breaksw
   case center
#
#  Center the image over the background.
#
      @ xpos = `echo $xoutput $xsize | awk '{print ($1-$2)/2}' | awk -F. '{print $1}'`
      @ ypos = `echo $youtput $ysize | awk '{print ($1-$2)/2}' | awk -F. '{print $1}'`
      echo "Final image origin: $xpos $ypos"
#
#  Make a background.
#
      if ($white == 0) then
         echo "Making a black backgound..."
         nullimg $BACKTMP $xoutput $youtput 3
      else
         echo "Making a white image backgound..."
         conimg $BACKTMP $xoutput $youtput 255 255 255
      endif
#
#  Composite the two...
#
      echo "Compositing your image over the background..."
      over $BACKTMP $CONVTMP $DONETMP $xpos $ypos
      rm $BACKTMP
      breaksw
endsw
#
#  Convert it to the final form.
#
if ($image == rle) then
   if ($output == undefined) then
      echo "Converting to ${name:r}-${add}.rle..."
      toutah $DONETMP ${name:r}-${add}.rle
   else
      echo "Converting to $output..."
      toutah $DONETMP $output
   endif
else if ($image == rla) then
   if ($output == undefined) then
      echo "Converting to ${name:r}-${add}.rla..."
      iristorle $DONETMP | rletorla -o ${name:r}-${add}.rla
   else
      echo "Converting to $output..."
      iristorle $DONETMP | rletorla -o $output
   endif
else if ($image == yuv) then
   if ($output == undefined) then
      echo "Converting to ${name:r}-${add}.yuv..."
      toyuv $DONETMP ${name:r}-${add}.yuv
   else
      echo "Converting to $output..."
      toyuv $DONETMP $output
   endif
else if ($image == sgi) then
   if ($output == undefined) then
      echo "Converting to ${name:r}-${add}.sgi..."
      cp $DONETMP ${name:r}-${add}.sgi
   else
      echo "Converting to $output..."
      cp $DONETMP $output
   endif
else if ($image == gif) then
   if ($output == undefined) then
      echo "Converting to ${name:r}-${add}.gif..."
      togif $DONETMP ${name:r}-${add}.gif
   else
      echo "Converting to $output..."
      togif $DONETMP $output
   endif
else if ($image == display) then
   echo "Displaying..."
   @ nvis = `xdpyinfo | awk '/number of visuals/ {print $4}'`
   if ($nvis == 4) then
      iristorle $DONETMP | xli stdin $
   else
      ipaste $DONETMP
   endif
endif
rm $DONETMP
rm $CONVTMP
