#! /bin/csh -f
# Last edited on 2004-09-28 19:54:21 by stolfi

set usage = "$0 < PSCOMMANDS > PPMFILE"

# Converts a seqence of Postscript drawing commands
# to a ppm image.
#
# The PSCOMMANDS must include a line containing
# the single statement "WIDTH HEIGHT setsize"
# where WIDTH and HEIGHT are integer literals (NOT variables or formulas).

if ( $#argv != 0 ) then
  echo "bad args" > /dev/stderr;
  echo "usage = ${usage}" > /dev/stderr; exit 1
endif

set tooldir = "${STOLFIHOME}/posters/tools"
set pbmdir = "${STOLFIHOME}/pkg/netpbm-1mar1994-1/PUB/${PLATFORM}/bin"

set tmp = "/tmp/$$"

cat > ${tmp}.psc

set dims = ( `egrep '^ *[0-9]+ [0-9]+ setsize *$' ${tmp}.psc` )

if ( $#dims != 3 ) then
  echo 'bad or missing "setsize" command' > /dev/stderr; exit 1
endif

set wd = "${dims[1]}"
set ht = "${dims[2]}"

cat ${tooldir}/ps-to-ppm.psh \
  | sed \
      -e 's:\$(WIDTH):'"${wd}"':g' \
      -e 's:\$(HEIGHT):'"${ht}"':g' \
      -e '/\$(TEXT)/{' \
      -e   's:\$(TEXT)::g' \
      -e   'r '"${tmp}.psc" \
      -e '}' \
  > ${tmp}.eps

@ wdx = ${wd} * 3
@ htx = ${ht} * 3

gs \
    -q \
    -dNOPLATFONTS -dQUIET -dNOPAUSE \
    -sDEVICE=ppmraw \
    -r216 -g${wdx}x${htx} \
    -sOutputFile=${tmp}.ppm-x3 \
    ${tmp}.eps \
  < /dev/null \
  > /dev/null
  
cat ${tmp}.ppm-x3 \
  | ${pbmdir}/pnmscale 0.33333 \
  | ${pbmdir}/pnmdepth 255

/bin/rm -f ${tmp}.psc ${tmp}.eps ${tmp}.ppm-x3

exit 0
