#!/bin/sh
#
#	Uses ghostscript to translate an Encapsulated PostScript file to
#	Portable Anymap format file(s).
#	pstopnm will create as many files as the number of pages in 
#	the Postscript document.  The name of the files will be 
#	psfile001.ppm, psfile002.ppm, etc.
#	The ouput files will contain the area inside the BoundingBox.
#	If BoundingBox parameters are not found in the PostScript
#	document, default values are used.
#
#
#       Usage: pstopnm [-forceplain] [-help] [-llx s] [-lly s] 
#		       [-urx s] [-ury s] [-nocrop] [-pbm|-pgm|-ppm] 
#		       [-verbose] [-xborder n] [-xmax n] [-xsize n] 
#		       [-yborder n] [-ymax n] [-ysize n] 
#		       [-portrait] [-landscape] psfile[.ps]
# 
# 	Copyright (C) 1992 by Alberto Accomazzi, Smithsonian Astrophysical
#	Observatory (alberto@cfa.harvard.edu).
#
# 	Permission to use, copy, modify, and distribute this software and its
#	documentation for any purpose and without fee is hereby granted, 
#	provided that the above copyright notice appear in all copies and 
#	that both that copyright notice and this permission notice appear 
#	in supporting documentation.  This software is provided "as is" 
#	without express or implied warranty.
#
set -o noglob

progname=`basename $0`
filtertail="raw"
filterhead="ppm"
xsize=0
ysize=0
xres=""
yres=""

# default values: max image x and y sizes
xmax=612
ymax=792
# default values: image area fits in a 8.5x11 sheet with 1 inch border
llx=72
lly=72
urx=540
ury=720
# default values: x and y borders are 10% of x and y size
xborder="0.1"
yborder="0.1"
# default values: orientation is unknown
orient=0

psfile=""
usage()
{
  cat << EOF
  Usage: $progname [-forceplain] [-help] [-llx s] [-lly s]
                   [-urx s] [-ury s] [-landscape] [-portrait]
                   [-nocrop] [-pbm|-pgm|-ppm] [-verbose] [-xborder s] [-xmax s]
                   [-xsize s] [-yborder s] [-ymax s] [-ysize s] psfile[.ps]
EOF
  exit 1
}

while [ $# -gt 0 ]; do
    case "$1" in
    -h*)   # -help
        usage
        ;;
    -p[bgp]m)
    	filterhead=`echo "$argv[1]" | sed "s/-//1"`
	;;
    -llx)
        shift
        [ $# -eq 0 ] && usage
    	llx=`(echo "scale=4";echo "$argv[1] * 72")|bc -l`
	nobb="t"
	;;
    -lly)
        shift
        [ $# -eq 0 ] && usage
	lly=`(echo "scale=4";echo "$argv[1] * 72")|bc -l`
	nobb="t"
	;;
    -urx)
        shift
        [ $# -eq 0 ] && usage
	urx=`(echo "scale=4";echo "$argv[1] * 72")|bc -l`
	nobb="t"
	;;
    -ury)
        shift
        [ $# -eq 0 ] && usage
	ury=`(echo "scale=4";echo "$argv[1] * 72")|bc -l`
	nobb="t"
	;;
    -no*)	# -nocrop
	nocrop="t"
	;;
    -xs*)	# -xsize
        shift
        [ $# -eq 0 ] && usage
	@ xsize=$argv[1]
	;;
    -ys*)	# -ysize
        shift
        [ $# -eq 0 ] && usage
	@ ysize=$argv[1]
	;;
    -xm*)	# -xmax
        shift
        [ $# -eq 0 ] && usage
	@ xmax=$argv[1]
	;;
    -ym*)	# -ymax
        shift
        [ $# -eq 0 ] && usage
	@ ymax=$argv[1]
	;;
    -xb*)	# -xborder
        shift
        [ $# -eq 0 ] && usage
	xborder=$argv[1]
	;;
    -yb*)	# -yborder
        shift
        [ $# -eq 0 ] && usage
	yborder=$argv[1]
	;;
    -f*)	# -forceplain
	filtertail=""
	;;
    -v*)	# -verbose
	verb="t"
	;;
    -po*)	# -portrait
	orient=1
	;;
    -la*)	# -landscape
	orient=2
	;;
    -*)
        echo "${progname}: Unknown option $argv[1]"
        usage
        ;;
    *)	# input file
	psfile=$1
	ppmfile=`basename ${1%.ps}`
        ;;
    esac
    shift
done

if [ -z "$psfile" ]; then
    usage
fi
if [ ! -f "$psfile" ]; then
    echo "${progname}: file $psfile not found"
    usage
fi

bb=(`grep "%%BoundingBox" $psfile`)
if [ -z "$nobb" ] && [ ${#bb[*]} == 5 ]; then
    llx=$bb[2]
    lly=$bb[3]
    urx=$bb[4]
    ury=$bb[5]
else
    if [ -z "$nobb" ]; then
    	echo "${progname}: warning: BoundingBox not found in input file"
    fi
fi

tmpsx=`(echo "scale=4";echo "$urx - $llx")|bc -l`
tmpsy=`(echo "scale=4";echo "$ury - $lly")|bc -l`

# see if orientation was specified 
if [ $orient = 0 ]; then
    # no orientation was specified; compute default orientation
    tmpx=0
    tmpy=0
    tmpsx1=${tmpsx%.*}
    tmpsy1=${tmpsy%.*}
    # default is landscape mode
    orient=2
    if [ $xsize = 0 ] && [ $ysize = 0 ]; then
	tmpx=$xmax
	tmpy=$ymax
    else
	[ $xsize != 0 ] && tmpx=$xsize
	[ $ysize != 0 ] && tmpy=$ysize
    fi
    if [ $tmpx = 0 ] || [ $tmpy = 0 ]; then
	# only one size was specified
	[ $tmpsy1 -gt $tmpsx1 ] && orient=1
    else
	# no size or both sizes were specified
	[ $tmpsy1 -gt $tmpsx1 ] && [ $tmpy -gt $tmpx ] && orient=1
	[ $tmpsx1 -gt $tmpsy1 ] && [ $tmpx -gt $tmpy ] && orient=1
    fi
fi

# now reset BoundingBox llc and total size to take into account margin
llx=`(echo "scale=4";echo "$llx - $tmpsx * $xborder")|bc -l`
lly=`(echo "scale=4";echo "$lly - $tmpsy * $yborder")|bc -l`
urx=`(echo "scale=4";echo "$urx + $tmpsx * $xborder")|bc -l`
ury=`(echo "scale=4";echo "$ury + $tmpsy * $yborder")|bc -l`
# compute image area size 
sx=`(echo "scale=4";echo "$tmpsx + 2 * $xborder * $tmpsx")|bc -l`
sy=`(echo "scale=4";echo "$tmpsy + 2 * $yborder * $tmpsy")|bc -l`
    
if [ $orient != 1 ]; then
    # render image in landscape mode
    tmpsx=$sx
    sx=$sy
    sy=$tmpsx
fi

# if xsize or ysize was specified, compute resolution from them
if [ $xsize != 0 ]; then
  xres=`(echo "scale=4";echo "$xsize *72 / $sx")|bc -l`
fi
if [ $ysize != 0 ]; then
  yres=`(echo "scale=4";echo "$ysize *72 / $sy")|bc -l`
fi

if [ -z "$xres" ] && [ -n "$yres" ]; then
    # ysize was specified, xsize was not; compute xsize based on ysize
    xres=$yres 
    xsize=`(echo "scale=4";echo "$sx * $xres /72 + 0.5")|bc -l`
    xsize=${xsize%.*}
else 
    if [ -z "$yres" ] && [ -n "$xres" ]; then
	# xsize was specified, ysize was not; compute ysize based on xsize
        yres=$xres
    	ysize=`(echo "scale=4";echo "$sy * $yres /72 + 0.5")|bc -l`
    	ysize=${ysize%.*}
    else
	if [ -z "$xres$yres" ]; then
    	    # neither xsize nor ysize was specified; compute them from
	    # xmax and ymax
	    xres=`(echo "scale=4";echo "$xmax *72/$sx")|bc -l`
	    yres=`(echo "scale=4";echo "$ymax *72/$sy")|bc -l`
	    xres=`(echo "scale=4";echo "if($xres>$yres)$yres";echo "if($yres>$xres)$xres")|bc -l`
	    yres=$xres
	    if [ -n "$nocrop" ]; then
		# keep output file dimensions equal to xmax and ymax
		xsize=$xmax
		ysize=$ymax
	    else
    	    	xsize=`(echo "scale=4";echo "$sx * $xres /72+0.5")|bc -l`
    	    	ysize=`(echo "scale=4";echo "$sy * $yres /72+0.5")|bc -l`
	    fi
    	    xsize=${xsize%.*}
    	    ysize=${ysize%.*}
	fi
    fi
fi

# translate + rotate image, if necessary
if [ $orient = 1 ]; then
    # portrait mode
    # adjust offsets
    llx=`(echo "scale=4";echo "$llx - ($xsize *72/$xres - $sx)/2")|bc -l`
    lly=`(echo "scale=4";echo "$lly - ($ysize *72/$yres - $sy)/2")|bc -l`
    pstrans="$llx neg $lly neg translate"
else
    # landscape mode
    # adjust offsets
    llx=`(echo "scale=4";echo "$llx - ($ysize *72/$yres - $sy)/2")|bc -l`
    ury=`(echo "scale=4";echo "$ury + ($xsize *72/$xres - $sx)/2")|bc -l`
    pstrans="90 rotate $llx neg $ury neg translate"
fi

if [ -n "$verb" ]; then
    echo "sx=$sx"
    echo "sy=$sy"
    echo "xres=$xres"
    echo "yres=$yres"
    echo "xsize=$xsize"
    echo "ysize=$ysize"
    echo -n "orientation "
    if [ $orient = 1]; then 
	echo "portrait"
    else
	echo "landscape"
    fi
    echo "PS header: $pstrans"
fi

echo "${progname}: writing $filterhead file(s)"

echo $pstrans | \
	gs -sDEVICE=${filterhead}${filtertail} \
	   -sOutputFile=$ppmfile%03d.$filterhead \
	   -g${xsize}x${ysize} \
	   -r${xres}x${yres} \
	   -q - $psfile 



