#! /bin/sh

# Bourne shell script to cut slices of a JPG page image and make
# separate JPG files from each line. It used to convert to PNM to get the
# image size but this version assumes a whole uncropped page, so we give
# absolute coordinates for the starting-point. The master page is made
# by making a 1-page A4 PS file with dvips at 600dpi, then running GS on
# it with DEVICE=jpeggray and r=300. If you use other settings, open the
# JPG file in a graphics editor and work out initial values for LEFT, TOP,
# WIDTH, and SLICE by yourself.

# Peter Flynn, Silmaril Consultants, 2000/2001/2002/2003

if [ -z "$1" ]; then

    echo You must give a filename to slice up

    exit 1

else

    FILE=`basename $1 .jpg`

# Starting-point for TLC of first slice

#    LEFT=850
#    TOP=614

     LEFT=1000
     TOP=2300

#    WIDTH=`jpegtopnm $FILE.jpg | pnmfile | awk '{print $4}'`
#    WIDTH=$[WIDTH-LEFT]
#    WIDTH=1050

     WIDTH=1000

    HEIGHT=`jpegtopnm $FILE.jpg | pnmfile | awk '{print $6}'`
    
    SLICE=50

    BOTTOM=$SLICE

# we expect 36 lines of font samples
    
    while [ $BOTTOM -lt $HEIGHT ]; do
    
	BOTTOM=$[TOP+SLICE-1]
	
	COUNT=$[COUNT+1]
	
	if [ $COUNT -lt 10 ]; then
	    SEQ="0"$COUNT
	else
	    SEQ=$COUNT
	fi
	
	echo Cutting slice $COUNT from $TOP to $BOTTOM
	
	jpegtopnm $FILE.jpg | pnmcut $LEFT $TOP $WIDTH $SLICE | pnmcrop | ppmtojpeg >$FILE-$SEQ.jpg
	
	TOP=$[BOTTOM+1]

# additional spacing to skip over table subheadings

	if [ $COUNT = 21 ]; then

	    TOP=$[TOP + 136 - $SLICE]

	elif [ $COUNT = 31 ]; then

	    TOP=$[TOP + 136 - $SLICE]

	fi
	
    done

fi

exit 0