#!/bin/csh -f
#
# pcdindex - generate a single PPM file from a PCD overview file
#
# This script assumes that the PBMPLUS and hpcdtoppm software
# packages are installed, and that /tmp has enough space
# (worst case 150Kbyte per image).
# Based on pnmindex (PBMPLUS), which was written by Jef Poskanzer,
# this script makes also use of hpcdtoppm, written by Hadmut Danisch.
#
# A similar result can be achieved by using "hpcdtoppm -Overview"
# followed by "pnmindex -black" on the generated PPM images.
# This shell just makes it more convenient and transparent to
# convert from one PCD to one PPM overview file.
#
# Additional options (compared to pnmindex) are -maxwidth and
# -font <font>. See "man pbmtext" on how to create your own font.
#
# Pieter S. van der Meulen, 1992.

# You may want to change the default values in the next 6 lines:
set maxwidth=1152	# maximum width of the index image
set size=192		# make the images about this big
set across=6		# show this many images per row
set colors="noquant"	# maximum amount of colors or noquant (no quantization)
set back="-black"	# default background color
set font=" "		# default font or none (pbmtext's internal font)

# Parse the options
while ( 1 )
    switch ( "$1" )

	case -m*:
	if ( $#argv < 2 ) goto usage
	set maxwidth="$2"
	shift
	shift
	breaksw

	case -s*:
	if ( $#argv < 2 ) goto usage
	set size="$2"
	shift
	shift
	breaksw

	case -a*:
	if ( $#argv < 2 ) goto usage
	set across="$2"
	shift
	shift
	breaksw

	case -c*:
	set colors="$2"
	shift
	shift
	breaksw

	case -f*:
	set font="-font $2"
	shift
	shift
	breaksw

	case -b*:
	set back="-black"
	shift
	breaksw

	case -w*:
	set back="-white"
	shift
	breaksw

	case -*:
	echo "$0 : Unknown option $1"
	echo " "
	goto usage
	breaksw

	default:
	break
	breaksw

    endsw
end

if ( $#argv == 0 ) then
    goto usage
endif

set tmpfile=/tmp/pi.tmp.$$
rm -f $tmpfile
touch /tmp/img0001 # Avoid complaints about non matching
rm -f /tmp/img*

set rowfiles=()
set imagefiles=()
@ row = 1
@ col = 1
@ width = $size

# Convert the PCD overview file to many PPM images
if (-f $1) then
    hpcdtoppm -Overview $1 /tmp/img
else
    echo "$0 : Could not access $1"
    echo " "
    goto usage
endif

foreach i ( /tmp/img* )

if (-f $i) then
    set description=`pnmfile $i`
    if ( $description[4] <= $size && $description[6] <= $size ) then
	cat $i > $tmpfile
    else
	    if ( $colors =~ n* ) then
		pnmscale -quiet -xysize $size $size $i > $tmpfile
	    else
		pnmscale -quiet -xysize $size $size $i | ppmquant -quiet $colors > $tmpfile
	    endif
    endif
    set imagefile=/tmp/pi.${row}.${col}.$$
    rm -f $imagefile
    set ttext=$i:t
    if ( "$back" == "-white" ) then
	pbmtext $font "$ttext" | pnmcrop -quiet | pnmmargin -white 2| pnmcat $back -tb $tmpfile - > $imagefile
    else
	pbmtext $font "$ttext" | pnmcrop -quiet | pnmmargin -white 2 | pnminvert | pnmcat $back -tb $tmpfile - > $imagefile
    endif
    rm -f $tmpfile
    set description=`pnmfile $imagefile`
    @ width += $description[4]
    set imagefiles=( $imagefiles $imagefile )

    if (( $col >= $across ) || ( $width > $maxwidth)) then
	set rowfile=/tmp/pi.${row}.$$
	rm -f $rowfile
	if ( $colors =~ n* ) then
	    pnmcat $back -lr -jbottom $imagefiles > $rowfile
	else
	    pnmcat $back -lr -jbottom $imagefiles | ppmquant -quiet $colors > $rowfile
	endif
	rm -f $imagefiles
	set imagefiles=()
	set rowfiles=( $rowfiles $rowfile )
	@ col = 1
	@ row += 1
	@ width = $size
    else
	@ col += 1
    endif
endif

end

if ( $#imagefiles > 0 ) then
    set rowfile=/tmp/pi.${row}.$$
    rm -f $rowfile
    if ( $colors =~ n* ) then
	pnmcat $back -lr -jbottom $imagefiles > $rowfile
    else
	pnmcat $back -lr -jbottom $imagefiles | ppmquant -quiet $colors > $rowfile
    endif
    rm -f $imagefiles
    set rowfiles=( $rowfiles $rowfile )
endif

if ( $#rowfiles == 1 ) then
    cat $rowfiles
else
    if ( $colors =~ n* ) then
	pnmcat $back -tb $rowfiles
    else
	pnmcat $back -tb $rowfiles | ppmquant -quiet $colors
    endif
endif
rm -f $rowfiles
rm -f /tmp/img*

exit 0

usage:
    echo "Usage: $0 [-m W] [-s S] [-a A] [-c N|n] [-f F] [-b|-w] <overview.pcd>"
    echo " with"
    echo "	W = maximum width of the result image	(default: $maxwidth)"
    echo "	S = maximum size of each of the images	(default: $size)"
    echo "	A = maximum number of images across	(default: $across)"
    echo "	N = maximum number of colors or noquant	(default: $colors)"
    echo -n "	F = font to be used for annotation  	(default: "
    if ( "$font" == " " ) then
	echo "internal font)"
    else
	echo "$font)"
    endif
    echo "	-b/-w = black/white background color	(default: $back)"
    echo " "
    echo " e.g.: $0 -m 768 -s 96 -f smallfont.pbm overview.pcd > overview.ppm"
    echo " or  : $0 /cdrom/photo_cd/overview.pcd | cjpeg > overview.jpg"
exit 1

