#! /bin/sh
#
# mouse:~ppr/src/www/images/fig2png
# Last modified 23 January 2004.
#

#
# This script converts FIG vector drawings to PPM using fig2dev from the
# Trasnfig package (which converts them to PostScript and then passes
# them through Ghostscript).  The transparent parts of the drawing are 
# replaced with a color that is almost white.  The output is then scaled
# down using pnmscale which uses anti-aliasing to produce a much better
# looking image than Ghostscript would if asked to produce an image at 
# the final resolution.  In the final step, pnmtopng which converts
# the image to PNG format while replacing the almost white color with
# transparent pixels.
#

case $1 in
    --wiz )
	shift
	# Convert a 6.375" square FIG image to a 128 pixel square PNG image.
	fig2dev -L ppm -m 1.0 -g '#f0f0f0' $1 | pnmscale 0.250 | pnmtopng -transparent '#f0f0f0' > $2
	;;

    --half )
	shift
	# Convert to half size
	fig2dev -L ppm -m 2.0 -g '#f0f0f0' $1 | pnmscale 0.250 | pnmtopng -transparent '#f0f0f0' > $2
	;;

    --quarter )
	shift
	# Convert to half size
	fig2dev -L ppm -m 1.0 -g '#f0f0f0' $1 | pnmscale 0.250 | pnmtopng -transparent '#f0f0f0' > $2
	;;

    * )
	# Render the image at 4x and then scale it down with anti-aliasing.
	fig2dev -L ppm -m 4.0 -g '#f0f0f0' $1 | pnmscale 0.250 | pnmtopng -transparent '#f0f0f0' > $2
	;;
    esac

result=$?
if [ $result -ne 0 ]
    then
    echo "Image convsion failed, removing useless output file."
    rm -f $2
    fi

exit $result
