#!/bin/bash 

NBFRAMES=60
INTERVAL=1
DEVICE=/dev/video0
GRABOPT="-S -o png -D 5  -i sif -d $DEVICE"
OUTDIR=tc8
ESPEAKOPT="-p 40 -s 130"

if [ $# == 1 ]
then
	JOB=$1
else
	JOB=essai
fi

export LANG=C



#
# new 7 fev 2014 - preparation du fond
#
negatif=/tmp/fond_$job.png
temptga=/tmp/$$.tga
insert=/tmp/insert_$job.tga
vgrabbj $GRABOPT > $negatif
pngtopnm $negatif | ppmtotga -rgb -norle > $temptga
tga_resize $temptga percent $temptga 17 0 1
tga_effects $temptga updown $temptga
tga_effects $temptga mirror $temptga
tga_effects $temptga gray $temptga
tga_cadre $temptga filet $temptga
cp $temptga $insert
# XXX display $temptga &

#
# sequence de capture
#
for frame in $(seq 1 $NBFRAMES)
do
	echo
	figlet -W "$NBFRAMES - $frame"
	negatif=/tmp/photo_${JOB}_$(printf %03d $frame).png
	vgrabbj $GRABOPT > $negatif
	sleep $INTERVAL
done

echo "Shoot Well Done" | espeak $ESPEAKOPT
 
# export TGA_OUTILS_VERBOSE=yes

for frame in $(seq 1 $NBFRAMES)
do
	echo
	photo=/tmp/photo_${JOB}_$(printf %03d $frame).tga
	negatif=/tmp/photo_${JOB}_$(printf %03d $frame).png
	pngtopnm $negatif | ppmtotga -rgb -norle > $photo
	echo Photo = $photo
	tga_effects $photo updown $photo
	tga_effects $photo mirror $photo
	tga_equalize $photo std $photo
	tga_resize $photo percent $photo 53 1 0
	
	# traitement de la photo
	dst=$(printf "/tmp/tga-$JOB-a-%03d.tga" $frame)
	tga_television $photo pix0 $dst 7 4 25
	tga_effects $dst 4bits $dst
	tga_cadre $dst rasta0 $dst 3

	dst=$(printf "/tmp/tga-$JOB-b-%03d.tga" $frame)
	tga_remap 0 $photo volcano.map $dst
	tga_equalize $dst square $dst
	tga_cadre $dst burp1 $dst 6 6

	dst=$(printf "/tmp/tga-$JOB-c-%03d.tga" $frame)
	tga_effects $photo water $dst 10
	tga_effects $dst 4bits $dst
	tga_filtres $dst smooth $dst
	tga_effects $dst gray $dst 
	tga_cadre $dst bruit $dst 0 50 100 8

	dst=$(printf "/tmp/tga-$JOB-d-%03d.tga" $frame)
	tga_filtres $photo sobel $dst 1
	tga_effects $dst 4bits $dst
	tga_incrust $dst $insert poke $dst 10 10
	tga_cadre $dst filet $dst

	dst=$(printf "/tmp/tga-$JOB-e-%03d.tga" $frame)
	tga_dither $photo bayer0 $dst 
	tga_cadre $dst zsnd0 $dst 10 10 10

	dst=$(printf "/tmp/tga-$JOB-f-%03d.tga" $frame)
	tga_dither $photo crude $dst 5
	tga_cadre $dst b84b $dst 10 10

	dst=$(printf "/tmp/tga-$JOB-g-%03d.tga" $frame)
	tga_television $photo proto $dst 10
	tga_cadre $dst burp1 $dst 8 16

	dst=$(printf "/tmp/tga-$JOB-h-%03d.tga" $frame)
	tga_effects $photo class0 $dst
	tga_cadre $dst cracra $dst 8 

	dst=$(printf "/tmp/tga-$JOB-i-%03d.tga" $frame)
	tga_television $photo oldstyle $dst 0 100 0
	tga_cadre $dst pattern $dst truchet.tga 16

	dst=$(printf "/tmp/tga-$JOB-j-%03d.tga" $frame)
	tga_applymap 1 $photo volcano.map $dst
	tga_incrust $dst $insert poke $dst 100 100
	tga_cadre $dst patt_degr $dst blurp.tga 18

	rm $photo
	echo "==============================================="
	
done

figlet  "almost done"
echo "$JOB almost done" | espeak $ESPEAKOPT

echo creating GIF, please wait...
# renice $$
set -v

convert -delay 10 /tmp/tga-${JOB}-a-*.tga $OUTDIR/_${JOB}_a.gif
convert -delay 10 /tmp/tga-${JOB}-b-*.tga $OUTDIR/_${JOB}_b.gif
convert -delay 10 /tmp/tga-${JOB}-c-*.tga $OUTDIR/_${JOB}_c.gif
convert -delay 10 /tmp/tga-${JOB}-d-*.tga $OUTDIR/_${JOB}_d.gif
convert -delay 10 /tmp/tga-${JOB}-e-*.tga $OUTDIR/_${JOB}_e.gif
convert -delay 10 /tmp/tga-${JOB}-f-*.tga $OUTDIR/_${JOB}_f.gif
convert -delay 10 /tmp/tga-${JOB}-g-*.tga $OUTDIR/_${JOB}_g.gif
convert -delay 10 /tmp/tga-${JOB}-h-*.tga $OUTDIR/_${JOB}_h.gif
convert -delay 10 /tmp/tga-${JOB}-i-*.tga $OUTDIR/_${JOB}_i.gif
convert -delay 10 /tmp/tga-${JOB}-j-*.tga $OUTDIR/_${JOB}_j.gif

echo "TADAM $JOB" | espeak  $ESPEAKOPT

