#!/bin/sh
#
# pnmindex - build a visual index of a bunch of anymaps
#
# 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=256		# quantize results to this many colors
back="-white"		# default background color

usage ()
{
  echo "usage: $0 [-size N] [-across N] [-colors N] [-black] pnmfile ..."
  exit 1
}

while :; do
    case "$1" in

    -s*)
	if [ $# -lt 2 ]; then usage; fi
	set size="$2"
	shift
	shift
	;;

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

    -c*)
	if [ $# -lt 2 ]; then usage; fi
	colors="$2"
	shift
	shift
	;;

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

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

    -*)
	usage
	;;

    *)
	break
	;;
    esac
done

if [ $# = 0 ]; then
    usage
fi

tmpfile=`tempfile -p pi -m 600`
maxformat=PBM

rowfiles=()
imagefiles=()
row=1
col=1

for i in "$@"; do

    description=`pnmfile $i`
    if [ $? != 0 ]; then
      echo pnmfile returned an error
      exit $?
    fi
    if [ "${description[4]}" -le $size ] && [ "${description[6]}" -le $size ]; then
	cat $i > $tmpfile
    else
	case "${description[2]}" in
	    PBM) pnmscale -quiet -xysize $size $size $i | pgmtopbm > $tmpfile;;

	    PGM) pnmscale -quiet -xysize $size $size $i > $tmpfile
	    if [ $maxformat = PBM ]; then
		maxformat=PGM
	    fi;;

	    *) pnmscale -quiet -xysize $size $size $i | ppmquant -quiet $colors > $tmpfile
	    maxformat=PPM;;
	esac
    fi
    imagefile=pi.${row}.${col}.$$
    rm -f $imagefile
    if [ "$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 [ $col -ge $across ]; then
	rowfile=pi.${row}.$$
	rm -f $rowfile
	if [ $maxformat != PPM ]; then
	    pnmcat $back -lr -jbottom $imagefiles > $rowfile
	else
	    pnmcat $back -lr -jbottom $imagefiles | ppmquant -quiet $colors > $rowfile
	fi
	rm -f $imagefiles
	imagefiles=()
	rowfiles=( ${rowfiles[*]} $rowfile )
	@ col = 1
	row=$(($row + 1))
    else
	col=$(($col + 1))
    fi
done

if [ ${#imagefiles[*]} -gt 0 ]; then
    rowfile=pi.${row}.$$
    rm -f $rowfile
    if [ $maxformat != PPM ]; then
	pnmcat $back -lr -jbottom $imagefiles > $rowfile
    else
	pnmcat $back -lr -jbottom $imagefiles | ppmquant -quiet $colors > $rowfile
    fi
    rm -f $imagefiles
    rowfiles=( ${rowfiles[*]} $rowfile )
endif

if [ ${#rowfiles[*]} == 1 ]; then
    cat $rowfiles
else
    if [ $maxformat != PPM ]; then
	pnmcat $back -tb $rowfiles
    else
	pnmcat $back -tb $rowfiles | ppmquant -quiet $colors
    fi
fi
rm -f $rowfiles

exit 0

