#!/bin/sh

# Attempt to convert any image file to XPM format. Requires the netpbm
# package for all formats except XPM itself. JPEG also requires the
# djpeg program.

# Compressed files are automatically decompressed before converting.
# Requires gzip and uncompress.

# For unknown extensions, the script falls back to anytopnm which is
# part of netpbm.

# echo "Converting $1" 1>&2

convert_image()
{
	case "$1" in
	*.gz )
		name=`basename $1 .gz`
		tmpfile=/tmp/DeCoDe_${name}
		cp $1 ${tmpfile}.gz
		gzip -df ${tmpfile}.gz
		convert_image $tmpfile
		rm ${tmpfile}
		;;
	*.Z )
		name=`basename $1 .Z`
		tmpfile=/tmp/DeCoDe_${name}
		cp $1 ${tmpfile}.Z
		uncompress -f ${tmpfile}.Z
		convert_image ${tmpfile}
		rm ${tmpfile}
		;;
	*.gif | *.GIF )
		giftopnm $1 | ppmtoxpm
		;;
	*.jpg | *.jpeg | *.JPG | *.JPEG )
		djpeg -colors 128 $1 | ppmtoxpm
		;;
	*.tif | *.tiff | *.TIF | *.TIFF )
		tifftopnm $1 | ppmtoxpm
		;;
	*.png | *.PNG )
		pngtopnm $1 | ppmtoxpm
		;;
	*.bmp | *.BMP )
		bmptoppm $1 | ppmtoxpm
		;;
	*.xpm | *.XPM )
		cat $1
		;;
	* )
		anytopnm $1 | ppmtoxpm
		;;
	esac
}

convert_image $1

