#!/bin/ksh
#
# Used by Paul Chamberlain as
#	anyindex -size 75 -across 10 -black * > /tmp/index.ppm
#
# anyindex - build a visual index of a bunch of images
# Modified from pnmindex by Paul Chamberlain
#
# pnmindex was...
# Copyright (C) 1991 by Jef Poskanzer.
#
# 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.

size=100		# make the images about this big
across=6		# show this many images per row
colors=""		# quantize results to this many colors
back="-white"	# default background color

while :
do
    case "$1" in

	-s*)
	if test $# -lt 2
	then goto usage
	fi
	size="$2"
	shift
	shift
	;;

	-a*)
	if test $# -lt 2
	then goto usage
	fi
	across="$2"
	shift
	shift
	;;

	-c*)
	colors="$2"
	shift
	shift
	;;

	-b*)
	back="-black"
	shift
	;;

	-w*)
	back="-white"
	shift
	;;

	-*)
	goto usage
	;;

	*)
	break
	;;

    esac
done

if test $# -eq 0
then
    goto usage
fi

tmpfile=/tmp/pi.tmp.$$
TMP=/tmp/ai.$$
rm -f $tmpfile
maxformat=PPM

rowfiles=""
imagefiles=""
row=1
col=1
scale="pnmscale -quiet -xysize $size $size"

for i
do
	file=$i
	hfile $i >&2
	case $i in
	*.[gG][iI][fF])
		giftoppm $i | $scale > $tmpfile
		;;
	*.[jJ][pP][gG] | *.[jJ][pP][eE][gG] | *.[jJ][pP]*)
		djpeg $i | $scale > $tmpfile
		;;
	*.[tT][iI][fF] | *.[tT][iI][fF][fF])
		tifftopnm $i | $scale > $tmpfile
		;;
	*.[mM][pP][gG] | *.[mM][pP][eE][gG])
		mpeg_frame $i | $scale > $tmpfile
		;;
	*)
		pbmtext "Unknown" | $scale > $tmpfile
		;;
	esac

    if pnmcat -lr $tmpfile > /dev/null
    then :
    else
	pbmtext "Error" | $scale > $tmpfile
	echo "$file: Error" >&2
    fi

    imagefile=/tmp/pi.${row}.${col}.$$
    rm -f $imagefile
    if test "$back" = "-white"
    then
	pbmtext "$i" | pnmcat $back -tb $tmpfile - > $imagefile
    else
	pbmtext "$i" | pnminvert | pnmcat $back -tb $tmpfile - > $imagefile
    fi
    rm -f $tmpfile
    imagefiles="$imagefiles $imagefile"

    if test $col -ge $across
    then
	rowfile=/tmp/pi.${row}.$$
	rm -f $rowfile
	if test $maxformat != PPM
	then
	    pnmcat $back -lr -jbottom $imagefiles > $rowfile
	else
	    pnmcat $back -lr -jbottom $imagefiles > $rowfile
	fi
	rm -f $imagefiles
	imagefiles=""
	rowfiles="$rowfiles $rowfile"
	col=1
	row=`expr $row + 1`
    else
	col=`expr $col + 1`
    fi
    if test "$i" != "$file"
    then
	rm -f $file
    fi

done

if test -n "$imagefiles"
then
    rowfile=/tmp/pi.${row}.$$
    rm -f $rowfile
    pnmcat $back -lr -jbottom $imagefiles > $rowfile
    rm -f $imagefiles
    rowfiles="$rowfiles $rowfile"
fi

if test -n "$rowfiles"
then
    if test $maxformat = PPM -a -n "$colors"
    then
	pnmcat $back -tb $rowfiles | ppmquant -quiet $colors
    else
	pnmcat $back -tb $rowfiles
    fi
    rm -f $rowfiles
fi

exit 0

usage:
echo "usage: $0 [-size N] [-across N] [-colors N] [-black] imagefile ..." >&2
exit 1
