#!/bin/bash

set -u
set -e
trap onexit INT
trap onexit TERM
trap onexit EXIT

onexit()
{
	if [ -d $OUTDIR ]; then
		rm -rf $OUTDIR
	fi
}

runme()
{
	echo \*\*\* $*
	$*
}

EXT=bmp
IMAGES="vgl_5674_0098.${EXT} vgl_6434_0018a.${EXT} vgl_6548_0026a.${EXT} nightshot_iso_100.${EXT}"
IMGDIR=@srcdir@/testimages
OUTDIR=`mktemp -d /tmp/__tjbenchtest_output.XXXXXX`
EXEDIR=.
BMPARG=
ALLOC=0
ALLOCARG=
if [ "$EXT" = "bmp" ]; then BMPARG=-bmp; fi

if [ -d $OUTDIR ]; then
	rm -rf $OUTDIR
fi
mkdir -p $OUTDIR

exec >$EXEDIR/tjbenchtest.log

if [ $# -gt 0 ]; then
	if [ "$1" = "-alloc" ]; then
		ALLOCARG=-alloc
		ALLOC=1
	fi
fi

# Standard tests
for image in $IMAGES; do

	cp $IMGDIR/$image $OUTDIR
	basename=`basename $image .${EXT}`
	$EXEDIR/cjpeg -quality 95 -dct fast -grayscale $IMGDIR/${basename}.${EXT} >$OUTDIR/${basename}_GRAY_fast_cjpeg.jpg
	$EXEDIR/cjpeg -quality 95 -dct fast -sample 2x2 $IMGDIR/${basename}.${EXT} >$OUTDIR/${basename}_420_fast_cjpeg.jpg
	$EXEDIR/cjpeg -quality 95 -dct fast -sample 2x1 $IMGDIR/${basename}.${EXT} >$OUTDIR/${basename}_422_fast_cjpeg.jpg
	$EXEDIR/cjpeg -quality 95 -dct fast -sample 1x1 $IMGDIR/${basename}.${EXT} >$OUTDIR/${basename}_444_fast_cjpeg.jpg
	$EXEDIR/cjpeg -quality 95 -dct int -grayscale $IMGDIR/${basename}.${EXT} >$OUTDIR/${basename}_GRAY_accurate_cjpeg.jpg
	$EXEDIR/cjpeg -quality 95 -dct int -sample 2x2 $IMGDIR/${basename}.${EXT} >$OUTDIR/${basename}_420_accurate_cjpeg.jpg
	$EXEDIR/cjpeg -quality 95 -dct int -sample 2x1 $IMGDIR/${basename}.${EXT} >$OUTDIR/${basename}_422_accurate_cjpeg.jpg
	$EXEDIR/cjpeg -quality 95 -dct int -sample 1x1 $IMGDIR/${basename}.${EXT} >$OUTDIR/${basename}_444_accurate_cjpeg.jpg
	for samp in GRAY 420 422 444; do
		$EXEDIR/djpeg -rgb $BMPARG $OUTDIR/${basename}_${samp}_fast_cjpeg.jpg >$OUTDIR/${basename}_${samp}_default_djpeg.${EXT}
		$EXEDIR/djpeg -dct fast -rgb $BMPARG $OUTDIR/${basename}_${samp}_fast_cjpeg.jpg >$OUTDIR/${basename}_${samp}_fast_djpeg.${EXT}
		$EXEDIR/djpeg -dct int -rgb $BMPARG $OUTDIR/${basename}_${samp}_accurate_cjpeg.jpg >$OUTDIR/${basename}_${samp}_accurate_djpeg.${EXT}
	done
	for samp in 420 422; do
		$EXEDIR/djpeg -nosmooth $BMPARG $OUTDIR/${basename}_${samp}_fast_cjpeg.jpg >$OUTDIR/${basename}_${samp}_default_nosmooth_djpeg.${EXT}
		$EXEDIR/djpeg -dct fast -nosmooth $BMPARG $OUTDIR/${basename}_${samp}_fast_cjpeg.jpg >$OUTDIR/${basename}_${samp}_fast_nosmooth_djpeg.${EXT}
		$EXEDIR/djpeg -dct int -nosmooth $BMPARG $OUTDIR/${basename}_${samp}_accurate_cjpeg.jpg >$OUTDIR/${basename}_${samp}_accurate_nosmooth_djpeg.${EXT}
	done

	# Compression
	for dct in accurate fast; do
		runme $EXEDIR/tjbench $OUTDIR/$image 95 -rgb -quiet -benchtime 0.01 -${dct}dct $ALLOCARG
		for samp in GRAY 420 422 444; do
			runme cmp $OUTDIR/${basename}_${samp}_Q95.jpg $OUTDIR/${basename}_${samp}_${dct}_cjpeg.jpg
		done
	done

	for dct in fast accurate default; do
		dctarg=-${dct}dct
		if [ "${dct}" = "default" ]; then
			dctarg=
		fi

		# Tiled compression & decompression
		runme $EXEDIR/tjbench $OUTDIR/$image 95 -rgb -tile -quiet -benchtime 0.01 ${dctarg} $ALLOCARG
		for samp in GRAY 444; do
			if [ $ALLOC = 1 ]; then
				runme cmp $OUTDIR/${basename}_${samp}_Q95_full.${EXT} $OUTDIR/${basename}_${samp}_${dct}_djpeg.${EXT}
				rm $OUTDIR/${basename}_${samp}_Q95_full.${EXT}
			else
				for i in $OUTDIR/${basename}_${samp}_Q95_[0-9]*[0-9]x[0-9]*[0-9].${EXT} \
					$OUTDIR/${basename}_${samp}_Q95_full.${EXT}; do
					runme cmp $i $OUTDIR/${basename}_${samp}_${dct}_djpeg.${EXT}
					rm $i
				done
			fi
		done
		runme $EXEDIR/tjbench $OUTDIR/$image 95 -rgb -tile -quiet -benchtime 0.01 -fastupsample ${dctarg} $ALLOCARG
		for samp in 420 422; do
			if [ $ALLOC = 1 ]; then
				runme cmp $OUTDIR/${basename}_${samp}_Q95_full.${EXT} $OUTDIR/${basename}_${samp}_${dct}_nosmooth_djpeg.${EXT}
				rm $OUTDIR/${basename}_${samp}_Q95_full.${EXT}
			else
				for i in $OUTDIR/${basename}_${samp}_Q95_[0-9]*[0-9]x[0-9]*[0-9].${EXT} \
					$OUTDIR/${basename}_${samp}_Q95_full.${EXT}; do
					runme cmp $i $OUTDIR/${basename}_${samp}_${dct}_nosmooth_djpeg.${EXT}
					rm $i
				done
			fi
		done

		# Tiled decompression
		for samp in GRAY 444; do
			runme $EXEDIR/tjbench $OUTDIR/${basename}_${samp}_Q95.jpg $BMPARG -tile -quiet -benchtime 0.01 ${dctarg} $ALLOCARG
			if [ $ALLOC = 1 ]; then
				runme cmp $OUTDIR/${basename}_${samp}_Q95_full.${EXT} $OUTDIR/${basename}_${samp}_${dct}_djpeg.${EXT}
				rm $OUTDIR/${basename}_${samp}_Q95_full.${EXT}
			else
				for i in $OUTDIR/${basename}_${samp}_Q95_[0-9]*[0-9]x[0-9]*[0-9].${EXT} \
					$OUTDIR/${basename}_${samp}_Q95_full.${EXT}; do
					runme cmp $i $OUTDIR/${basename}_${samp}_${dct}_djpeg.${EXT}
					rm $i
				done
			fi
		done
		for samp in 420 422; do
			runme $EXEDIR/tjbench $OUTDIR/${basename}_${samp}_Q95.jpg $BMPARG -tile -quiet -benchtime 0.01 -fastupsample ${dctarg} $ALLOCARG
			if [ $ALLOC = 1 ]; then
				runme cmp $OUTDIR/${basename}_${samp}_Q95_full.${EXT} $OUTDIR/${basename}_${samp}_${dct}_nosmooth_djpeg.${EXT}
				rm $OUTDIR/${basename}_${samp}_Q95_full.${EXT}
			else
				for i in $OUTDIR/${basename}_${samp}_Q95_[0-9]*[0-9]x[0-9]*[0-9].${EXT} \
					$OUTDIR/${basename}_${samp}_Q95_full.${EXT}; do
					runme cmp $i $OUTDIR/${basename}_${samp}_${dct}_nosmooth_djpeg.${EXT}
					rm $i
				done
			fi
		done
	done

	# Scaled decompression
	for scale in 2_1 15_8 7_4 13_8 3_2 11_8 5_4 9_8 7_8 3_4 5_8 1_2 3_8 1_4 1_8; do
		scalearg=`echo $scale | sed s@_@/@g`
		for samp in GRAY 420 422 444; do
			$EXEDIR/djpeg -rgb -scale ${scalearg} $BMPARG $OUTDIR/${basename}_${samp}_fast_cjpeg.jpg >$OUTDIR/${basename}_${samp}_${scale}_djpeg.${EXT}
			runme $EXEDIR/tjbench $OUTDIR/${basename}_${samp}_Q95.jpg $BMPARG -scale ${scalearg} -quiet -benchtime 0.01 $ALLOCARG
			runme cmp $OUTDIR/${basename}_${samp}_Q95_${scale}.${EXT} $OUTDIR/${basename}_${samp}_${scale}_djpeg.${EXT}
			rm $OUTDIR/${basename}_${samp}_Q95_${scale}.${EXT}
		done
	done

	# Transforms
	for samp in GRAY 420 422 444; do
		$EXEDIR/jpegtran -flip horizontal -trim $OUTDIR/${basename}_${samp}_Q95.jpg >$OUTDIR/${basename}_${samp}_hflip_jpegtran.jpg
		$EXEDIR/jpegtran -flip vertical -trim $OUTDIR/${basename}_${samp}_Q95.jpg >$OUTDIR/${basename}_${samp}_vflip_jpegtran.jpg
		$EXEDIR/jpegtran -transpose -trim $OUTDIR/${basename}_${samp}_Q95.jpg >$OUTDIR/${basename}_${samp}_transpose_jpegtran.jpg
		$EXEDIR/jpegtran -transverse -trim $OUTDIR/${basename}_${samp}_Q95.jpg >$OUTDIR/${basename}_${samp}_transverse_jpegtran.jpg
		$EXEDIR/jpegtran -rotate 90 -trim $OUTDIR/${basename}_${samp}_Q95.jpg >$OUTDIR/${basename}_${samp}_rot90_jpegtran.jpg
		$EXEDIR/jpegtran -rotate 180 -trim $OUTDIR/${basename}_${samp}_Q95.jpg >$OUTDIR/${basename}_${samp}_rot180_jpegtran.jpg
		$EXEDIR/jpegtran -rotate 270 -trim $OUTDIR/${basename}_${samp}_Q95.jpg >$OUTDIR/${basename}_${samp}_rot270_jpegtran.jpg
	done
	for xform in hflip vflip transpose transverse rot90 rot180 rot270; do
		for samp in GRAY 444; do
			$EXEDIR/djpeg -rgb $BMPARG $OUTDIR/${basename}_${samp}_${xform}_jpegtran.jpg >$OUTDIR/${basename}_${samp}_${xform}_jpegtran.${EXT}
			runme $EXEDIR/tjbench $OUTDIR/${basename}_${samp}_Q95.jpg $BMPARG -$xform -tile -quiet -benchtime 0.01 $ALLOCARG
			if [ $ALLOC = 1 ]; then
				runme cmp $OUTDIR/${basename}_${samp}_Q95_full.${EXT} $OUTDIR/${basename}_${samp}_${xform}_jpegtran.${EXT}
				rm $OUTDIR/${basename}_${samp}_Q95_full.${EXT}
			else
				for i in $OUTDIR/${basename}_${samp}_Q95_[0-9]*[0-9]x[0-9]*[0-9].${EXT} \
					$OUTDIR/${basename}_${samp}_Q95_full.${EXT}; do
					runme cmp $i $OUTDIR/${basename}_${samp}_${xform}_jpegtran.${EXT}
					rm $i
				done
			fi
		done
		for samp in 420 422; do
			$EXEDIR/djpeg -nosmooth -rgb $BMPARG $OUTDIR/${basename}_${samp}_${xform}_jpegtran.jpg >$OUTDIR/${basename}_${samp}_${xform}_jpegtran.${EXT}
			runme $EXEDIR/tjbench $OUTDIR/${basename}_${samp}_Q95.jpg $BMPARG -$xform -tile -quiet -benchtime 0.01 -fastupsample $ALLOCARG
			if [ $ALLOC = 1 ]; then
				runme cmp $OUTDIR/${basename}_${samp}_Q95_full.${EXT} $OUTDIR/${basename}_${samp}_${xform}_jpegtran.${EXT}
				rm $OUTDIR/${basename}_${samp}_Q95_full.${EXT}
			else
				for i in $OUTDIR/${basename}_${samp}_Q95_[0-9]*[0-9]x[0-9]*[0-9].${EXT} \
					$OUTDIR/${basename}_${samp}_Q95_full.${EXT}; do
					runme cmp $i $OUTDIR/${basename}_${samp}_${xform}_jpegtran.${EXT}
					rm $i
				done
			fi
		done
	done

	# Grayscale transform
	for xform in hflip vflip transpose transverse rot90 rot180 rot270; do
		for samp in GRAY 444 422 420; do
			runme $EXEDIR/tjbench $OUTDIR/${basename}_${samp}_Q95.jpg $BMPARG -$xform -tile -quiet -benchtime 0.01 -grayscale $ALLOCARG
			if [ $ALLOC = 1 ]; then
				runme cmp $OUTDIR/${basename}_${samp}_Q95_full.${EXT} $OUTDIR/${basename}_GRAY_${xform}_jpegtran.${EXT}
				rm $OUTDIR/${basename}_${samp}_Q95_full.${EXT}
			else
				for i in $OUTDIR/${basename}_${samp}_Q95_[0-9]*[0-9]x[0-9]*[0-9].${EXT} \
					$OUTDIR/${basename}_${samp}_Q95_full.${EXT}; do
					runme cmp $i $OUTDIR/${basename}_GRAY_${xform}_jpegtran.${EXT}
					rm $i
				done
			fi
		done
	done

	# Transforms with scaling
	for xform in hflip vflip transpose transverse rot90 rot180 rot270; do
		for samp in GRAY 444 422 420; do
			for scale in 2_1 15_8 7_4 13_8 3_2 11_8 5_4 9_8 7_8 3_4 5_8 1_2 3_8 1_4 1_8; do
				scalearg=`echo $scale | sed s@_@/@g`
				$EXEDIR/djpeg -rgb -scale ${scalearg} $BMPARG $OUTDIR/${basename}_${samp}_${xform}_jpegtran.jpg >$OUTDIR/${basename}_${samp}_${xform}_${scale}_jpegtran.${EXT}
				runme $EXEDIR/tjbench $OUTDIR/${basename}_${samp}_Q95.jpg $BMPARG -$xform -scale ${scalearg} -quiet -benchtime 0.01 $ALLOCARG
				runme cmp $OUTDIR/${basename}_${samp}_Q95_${scale}.${EXT} $OUTDIR/${basename}_${samp}_${xform}_${scale}_jpegtran.${EXT}
				rm $OUTDIR/${basename}_${samp}_Q95_${scale}.${EXT}
			done
		done
	done

done

echo SUCCESS!
