#!/bin/sh
#
#  @(#)fs2xbm 1.3 91/11/19
#
#  Copyright (c) Steve Kinzler - April 1991.
#
#  Permission is given to distribute these sources, as long as the
#  copyright messages are not removed, and no monies are exchanged.
#
#  No responsibility is taken for any errors on inaccuracies inherent
#  either to the comments or the code of this program, but if reported
#  to me, then an attempt will be made to fix them.

PATH=$PATH:/usr/bin/X11; export PATH

# fs2xbm - convert a Usenix FaceSaver image to a 48x48 X11 bitmap
# stdin/stdout filter
# requires some bitmap filters from the pbmplus package
# hacked up version of Chris Maltby's fs2ikon
# kludge by kinzler@cs.indiana.edu, apr 91

tmp1=/tmp/fs2xbm1.$$
tmp2=/tmp/fs2xbm2.$$
trap "rm -f $tmp1 $tmp2; exit" 0 1 2 13 15

fstopgm > $tmp1 2> /dev/null
set `sed -n -e 2p -e 2q $tmp1` X X
x=$1
y=$2
if test $x = X || test $y = X
then
	echo "$0: bad data format" 1>&2
	exit 1
fi

if test $x -ge $y
then
	ppmscale -width 48 $tmp1 | ppmtopgm | pgmtopbm > $tmp2
	set `sed -n -e 2p -e 2q $tmp2`
	yo=`expr '(' 48 - $2 ')' / 2`
	pbmmake 48 48 |
	    pnmpaste $tmp2 0 $yo |
	    pbmtoxbm 
else
	ppmscale -height 48 $tmp1 | ppmtopgm | pgmtopbm > $tmp2
	set `sed -n -e 2p -e 2q $tmp2`
	xo=`expr '(' 48 - $1 ')' / 2`
	pbmmake 48 48 |
	    pnmpaste $tmp2 $xo 0 |
	    pbmtoxbm
fi
