#!/bin/csh -f
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#  This is a csh script that uses the Utah Raster Toolkit to generate an
#  image of a color map.
#
#  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.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
#
#  Set a few default values.
#
set xscale = 1.0
set yscale = 1.0
set cmap   = undefined
set TMPDIR = /usr/tmp
#
#  Read command line arguments.
#
@ n = 1
while ($n <= $#argv)
   if ("$argv[$n]" == "-cmap") then
      @ n++
      set format = $argv[$n]
   else if ("$argv[$n]" == "-s") then
      @ n++
      set xscale = $argv[$n]
      @ n++
      set yscale = $argv[$n]
   else if ("$argv[$n]" == "-p") then
      @ n++
      set xscale = `echo $argv[$n] | awk '{print $1/100.0}'`
      @ n++
      set yscale = `echo $argv[$n] | awk '{print $1/256.0}'`
   else if ("$argv[$n]" == "-help") then
      echo "Usage: cmaptorle [-s xscale yscale| -p xsize ysize] cmap"
      echo " "
      echo "If options are omitted, the following defaults will be used."
      echo "-fmt rle        #the output format will be URT"
      echo " "
      echo "NOTE:  The filename should not include the extension."
      exit
   else
      set cmap = $argv[$n]
   endif
   @ n++
end
if ($cmap == undefined) then
   echo "No color map specified."
   exit
endif
if (`wc -l $cmap | awk '{print $1}'` != 256) then
   echo "This color map file does not have 256 entries."
   exit
endif
#
#  Make and image 1 pixel by 256 pixels.
#
rlebg -s 100 256 255 255 255 >$TMPDIR/0.$$.rle
rlebg -s 100 256 -l -v 0.0 1.0 255 255 255 \
	| rleswap -f 0 \
	| rleldmap -t $cmap \
	| applymap >$TMPDIR/3.$$.rle
mergechan -a $TMPDIR/0.$$.rle $TMPDIR/3.$$.rle $TMPDIR/3.$$.rle $TMPDIR/3.$$.rle | into $TMPDIR/3.$$.rle
if ($xscale == 1.0 && $yscale == 1.0) then
   cat $TMPDIR/3.$$.rle
else
   fant -p 0 0 -s $xscale $yscale $TMPDIR/3.$$.rle
endif
rm $TMPDIR/0.$$.rle $TMPDIR/3.$$.rle
