#!/bin/sh # Width and height of thumbnail page w=1024 h=768 # Width, height and margin of thumbnail tw=160 th=160 tm=10 # Vertical upwards adjustment to leave space for text label vadj=9 # Vertical spacing between thumbnail and text tspc=6 # JPEG quality quality=50 tm2=$(($tm * 2)) nt_h=$(($w / ($tw + $tm2))) nt_v=$(($h / ($th + $tm2))) tmadd_h=$((($w - (($tw + $tm2) * $nt_h)) / $nt_h)) tmadd_v=$((($h - (($th + $tm2) * $nt_v)) / $nt_v)) tm_h=$(($tm + ($tmadd_h / 2))) tm_v=$(($tm + ($tmadd_v / 2))) tw_tot=$(($tw + $tm2 + $tmadd_h)) th_tot=$(($th + $tm2 + $tmadd_v)) tmpfile1=$(mktemp ~/.mkthumb.XXXXXX) || exit 1 tmpfile2=$(mktemp ~/.mkthumb.XXXXXX) || exit 1 tmpfile3=$(mktemp ~/.mkthumb.XXXXXX) || exit 1 rm -f [0-9][0-9][0-9]thumb.??? exec 2>/dev/null x=0; y=0 n=1; tfile=$(printf "%03dthumb.ppm" $n) for f in *.*; do [ ! -f "$f" ] && continue case "$f" in *.[Tt][Ii][Ff]) cmd=tifftopnm;; *.[Jj][Pp][Gg]) cmd=djpeg;; *.[Pp][Nn][Gg]) cmd=pngtopnm;; *.[Gg][Ii][Ff]) cmd=giftoppm;; *.[Ee][Pp][Ss] | *.[Pp][Ss]) cmd="ps2ppm -r300 -dEPSFitPage" crop="| pnmcrop -white";; *.[Jj][Bb][Gg]) cmd=jbgtopbm;; *) continue;; esac eval $cmd \"$f\" $crop | pnmscale -xysize $tw $th >$tmpfile1 || continue res=$(pnmfile $tmpfile1 | nawk '{print $4 " " $6}') xres=${res% *}; yres=${res#* } pbmtext "$f" | pnmcrop -white | pnminvert >$tmpfile2 tres=$(pnmfile $tmpfile2 | nawk '{print $4 " " $6}') txres=${tres% *}; tyres=${tres#* } if [ $txres -gt $tw ]; then pnmcut 0 0 $tw $tyres $tmpfile2 >$tmpfile3 mv -f $tmpfile3 $tmpfile2 txres=$tw fi colfile=$(mktemp ~/.mkthumb.XXXXXX) || exit 1 ppmmake black $tw_tot $th_tot | \ pnmpaste $tmpfile1 $(($tm_h + (($tw - $xres) / 2))) \ $(($tm_v + (($th - $yres) / 2) - $vadj)) | \ pnmpaste $tmpfile2 $(($tm_h + (($tw - $txres) / 2))) \ $(($tm_v + (($th - $yres) / 2) + $yres - $vadj + $tspc)) >$colfile colfiles="$colfiles $colfile" x=$(($x + $tw_tot)) if [ $(($x + $tw_tot)) -gt $w ]; then rowfile=$(mktemp ~/.mkthumb.XXXXXX) || exit 1 pnmcat -lr $colfiles >$rowfile rowfiles="$rowfiles $rowfile" rm -f $colfiles colfiles= x=0; y=$(($y + $th_tot)) if [ $(($y + $th_tot)) -gt $h ]; then pnmcat -tb -jleft $rowfiles >$tfile rm -f $rowfiles rowfiles= y=0 n=$(($n + 1)); tfile=$(printf "%03dthumb.ppm" $n) fi fi done if [ -n "$colfiles" -o -n "$rowfiles" ]; then if [ -n "$colfiles" ]; then rowfile=$(mktemp ~/.mkthumb.XXXXXX) || exit 1 pnmcat -lr $colfiles >$rowfile rowfiles="$rowfiles $rowfile" fi pnmcat -tb -jleft $rowfiles >$tfile fi for f in [0-9][0-9][0-9]thumb.ppm; do if ! [ -f "$f" ]; then continue; fi cjpeg -quality $quality $f >${f%.ppm}.jpg rm -f $f done rm -f $tmpfile1 $tmpfile2 $tmpfile3 $colfiles $rowfiles .