#!/bin/sh
#
# convt v0.9 by Andrew Costa, a bored taxi driver
# who can be reached as <seagull@osiris.isys.ca>
#
# This script assumes you have the pbmplus or NetPbm
# package installed someplace, along with c/djpeg.  
# Change the PBMROOT line to reflect where the utils
# are installed on your system.  If they're in the path,
# just set it to an empty value.
#
PBMROOT=/usr/bin/pbmplus/
if [ -f "$1" ]; then
File=$1
else
echo -n "File? "
read File
if [ ! -f $File ]; then
echo "Ain't There."
exit
elif [ "$File" = "" ]; then
echo "Be that way, then."
exit
fi
fi
Base=`echo $File | cut -f1 -d.`
Ext=`echo $File | cut -f2 -d.`
for i in gif jpeg jpg xbm xpm xwd img raw pbm ppm pnm txt text; do
if [ "$Ext" = "$i" ]; then
Type=$i
fi
done
if [ "$Type" = "" ]; then
echo -n "Image type? "
read Type
fi
for i in gif xpm img raw; do
if [ "$Type" = "$i" ]; then
InCmd="$PBMROOT""$Type"toppm
fi
done
if [ "$Type" = "jpeg" -o "$Type" = "jpg" ]; then
InCmd="djpeg -colors 256"
elif [ "$Type" = "xbm" ]; then
InCmd="$PBMROOT"xbmtopbm
elif [ "$Type" = "xwd" ]; then
InCmd="$PBMROOT"xwdtopnm
elif [ "$Type" = "txt" -o "$Type" = "text" ]; then
InCmd="$PBMROOT"pbmtext
elif [ "$Type" = "pbm" -o "$Type" = "ppm" -o "$Type" = "pnm" ]; then
InCmd=pnm
fi
if [ "$InCmd" = "" ]; then
echo "Image format unrecognised: ner :P"
exit
fi
if [ "$2" != "" ]; then
for i in gif jpeg jpg xbm xpm xwd pbm ppm pnm ascii; do
if [ "$i" = "$2" ]; then
OutType=$i
fi
done
fi
if [ "$OutType" = "" ]; then
echo -n "Output format? "
read OutType
fi
if [ "$OutType" = "jpeg" -o "$OutType" = "jpg" ]; then
OutCmd=cjpeg
elif [ "$OutType" = "gif" -o "$OutType" = "xpm" ]; then
OutCmd="$PBMROOT"ppmto"$OutType"
elif [ "$OutType" = "xbm" -o "$OutType" = "ascii" ]; then
OutCmd="$PBMROOT"pbmto"$OutType"
elif [ "$OutType" = "xwd" ]; then
OutCmd="$PBMROOT"pnmtoxwd
elif [ "$OutType" = "pbm" -o "$OutType" = "ppm" -o "$OutType" = "pnm" ]; then
OutCmd=pnm
else
echo "Output format unrecognised: ner :P"
exit
fi
if [ "$InCmd" = "$OutCmd" -o "$Type" = "$OutType" ]; then
echo "Nothing to do."
exit
fi
if [ -f $Base.$OutType ]; then
mv $Base.$OutType $Base.$$.$OutType
fi
if [ "$InCmd" != "pnm" -a "$OutCmd" != "pnm" ]; then
cat $File | $InCmd | $OutCmd > $Base.$OutType
elif [ "$InCmd" = "pnm" ]; then
cat $File | $OutCmd > $Base.$OutType
elif [ "$OutCmd" = "pnm" ]; then
cat $File | $InCmd > $Base.$OutType
fi

