#!/bin/bash 

NBFRAMES=60
INTERVAL=1
DEVICE=/dev/video0
GRABOPT="-S -o png -D 5  -i sif -d $DEVICE"
OUTDIR=jpo
PREFIX=H
DELAY=10

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

export LANG=C

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

#
#	PRE-TRAITEMENT
#
for frame in $(seq 1 $NBFRAMES)
do
	photo=/tmp/photo_${JOB}_$(printf %03d $frame).tga
	mini=/tmp/mini_${JOB}_$(printf %03d $frame).tga
	echo
	echo preparation $photo
	negatif=/tmp/photo_${JOB}_$(printf %03d $frame).png
	pngtopnm $negatif | ppmtotga -rgb -norle > $photo
	tga_effects $photo updown $photo
	tga_effects $photo mirror $photo
	tga_extract $photo $photo 120 90 400 300 
	tga_equalize $photo std $photo
done

#
#	ASSEMBLAGE FINAL
#

for frame in $(seq 1 $NBFRAMES)
do
	echo
	photo=/tmp/photo_${JOB}_$(printf %03d $frame).tga

	p1=$frame
	p2=$(( 1 + ((frame + 4) % NBFRAMES) ))
	echo index = $p1 $p2 

	i1=$(printf "/tmp/photo_${JOB}_%03d.tga" $p1)
	i2=$(printf "/tmp/photo_${JOB}_%03d.tga" $p2)
	t1=$(printf "/tmp/t1_${JOB}_%03d.tga" $frame)
	tga_combine $i2 $i1 power $t1
	dst=$(printf "/tmp/${PREFIX}_${JOB}_a_%03d.tga" $frame)
	tga_effects $t1 4bits $dst

done

figlet  job ${JOB}

echo creating GIF, please wait...

convert -delay $DELAY /tmp/${PREFIX}_${JOB}_a_*.tga $OUTDIR/${PREFIX}_${JOB}_a.gif


