#!/bin/sh -e
# Binary von
#         http://www-sop.inria.fr/odyssee/team/David.Tschumperle

if [ _"$1" = _"" ] ; then
	echo "Welches Input-Bild?"
	exit
fi
if [ ! -s "$1" ] ; then
	echo "Input-Bild "$1" nicht gefunden."
fi

QUALITY=""
if file "$1" | grep -q -i -w "JPEG" ; then
    QUALITY="-quality 85"
fi

ext=`basename "$1" | sed 's/.*\(\.[a-zA-Z]\{3,4\}\)$/\1/'`

if [ _"$2" = _"" ] ; then
	out=`basename "$1" $ext`.ppm
else
	out="$2"
fi

vecdenoise -i "$1" -o $out -iter 45 -scale 9 -dt 20 -sigma 5 -init 5 -visu 0
# vecdenoise -i $1 -o $out -iter 40 -scale 9 -dt 20 -sigma 5 -init 5 -visu 0
# vecdenoise -i $1 -o $out -iter 35 -scale 9 -dt 20 -sigma 5 -init 5 -visu 0 
# vecdenoise -i $1 -o $out -iter 30 -scale 9 -dt 20 -sigma 5 -init 5 -visu 0 
# vecdenoise -i $1 -o $out -iter 25 -scale 9 -dt 20 -sigma 5 -init 5 -visu 0 
# vecdenoise -i $1 -o $out -iter 50 -scale 9 -dt 20 -sigma 5 -init 5 -visu 0 
# vecdenoise -i $1 -o $out -iter 100 -scale 9 -dt 20 -sigma 5 -init 5 -visu 0

if [ "$out" != "$2" ] ; then
    mv "$1" "$1"~
    convert "$out" $QUALITY "$1"
    rm "$out"
fi
