#!/bin/bash
#   pdf2mtiff
#
#     Rasterizes a PDF file, saving as a single multipage g4 compressed
#     tiff file
#
#     input:  PDF file
#             output file
#     output: ccitt-g4 compressed mulitipage tiff file
#
#  Note 1:  Requires ghostscript
#  Note 2:  If you give it images with bpp > 1, the result will be ugly.
#  Note 3:  A modern alternative to ghostcript is to use poplar:
#    If the pdf is composed of images that were orthographically generated:
#          pdftoppm -png <pdf-file> <pdf-root>    [output in png]
#    If the pdf is composed of images that were scanned:
#          pdftoppm -jpeg <pdf-file> <pdf-root>   [output in jpeg]

scriptname=${0##*/}

if test $# != 2
then
  echo "usage: " $scriptname " pdfin tiffout"
  exit -1
fi

pdfin=$1
tiffout=$2

# assert (input pdf filename ends in .pdf)
if test ${pdfin##*.} != pdf
then
  echo $scriptname ": " $pdfin "does not end in .pdf"
  exit -1
fi

# 'echo "0 neg 0 neg" translate' is the mysterious "primer"
rm /tmp/junkpdf*

# --------- Choose one of the two options below ---------

# output image size at 300 ppi
#echo "0 neg 0 neg" translate | /usr/bin/gs -sDEVICE=tiffg4 -sOutputFile=/tmp/junkpdf%03d.tif -r300x300 -q - ${pdfin}
#./writemtiff /tmp junkpdf ${tiffout}

# output image size at 600 ppi
echo "0 neg 0 neg" translate | /usr/bin/gs -sDEVICE=tiffg4 -sOutputFile=/tmp/junkpdf%03d.tif -r600x600 -q - ${pdfin}
./writemtiff /tmp junkpdf ${tiffout}

# ------------------------------------------------------

