#!/bin/bash

IMAGE=strip-tease.tga
MAP=/usr/share/xfractint/map/sweet.map
LARGEUR=456

if [ $# == 1 ]
then
	IMAGE="$1"
fi

SPOOL="/tmp"
SEED=42
DELAY=10
NOMBRE=34

# --------------------------------------------------------

function annotate()
{
convert 				\
	-fill black -pointsize 16	\
	-annotate +10+19 "$1"		\
	-fill white -pointsize 16 	\
	-annotate +12+21 "$1"		\
	$2 $2
}
# --------------------------------------------------------
function postprocess()
{
TMPA=/tmp/postprocessA_$$.ppm
convert $1 $TMPA
TMPB=/tmp/postprocessB_$$.ppm
< $TMPA ppmquant -nofs -mapfile map.ppm > $TMPB
convert $TMPB $2
}
# --------------------------------------------------------
function map2pnm ()
{
echo $1 $2

lignes=$(wc -l $1 | awk '{print $1}')
echo P3 > $2
echo $lignes 1 >> $2
echo 255 >> $2
awk '{print $1, $2, $3}' < $1 >> $2
}
#-----------------------------------------------------


# preparation de l'image de travail

SOURCE=$SPOOL/source.png
convert -resize $LARGEUR "$IMAGE" $SOURCE
display $SOURCE &

echo I gmic $IMAGE >> log.toto

# preparation de la colormap à partir de
# l'image d'origine

convert $IMAGE toto.png
pngtopnm toto.png | pnmcolormap 108 > map.ppm

# --------------------------------------------------------
> toto.dat
debut=$SECONDS

for i in $(seq 1 $NOMBRE)
do
	dest=$SPOOL/gmic_a_$(printf "%03d" $i).png
	# calcul d'une fonction (co)sinusoidale
	p=$( echo " scale=3 ;  c($i/5.1)/2.0 " | bc -l )
	
	echo $i $p >> toto.dat
	gmic $SOURCE -wave $i,$p -o $dest
	postprocess $dest $dest
	annotate "waves $i $p" $dest
done

gnuplot -e "set term gif;set output 'toto.gif';	\
		plot 'toto.dat' with lines"
# display toto.gif &
nice -12 convert -delay $DELAY $SPOOL/gmic_a*.png exp_gmic_a.gif &
echo T gmic_a $(( $SECONDS - $debut )) >> log.toto
sleep 1

# --------------------------------------------------------
debut=$SECONDS
for i in $(seq  1 $NOMBRE)
do
	dest=$SPOOL/gmic_b_$(printf "%03d" $i).png
	p=$(( $i + 1 ))
	gmic -srand $SEED $SOURCE -deform $p -o $dest
	postprocess $dest $dest
	annotate "deform $p" $dest
done
echo 

nice -17 convert -delay $DELAY $SPOOL/gmic_b*.png exp_gmic_b.gif &
echo T gmic_b $(( $SECONDS - $debut )) >> log.toto
sleep 1

# --------------------------------------------------------
debut=$SECONDS
for i in $(seq  1 $NOMBRE)
do
	dest=$SPOOL/gmic_c_$(printf "%03d" $i).png
	p=$(( ($i * 2) + 1 ))
	gmic $SOURCE -glow $p -o $dest
	postprocess $dest $dest
	annotate "glow $p" $dest 
done

figlet "C done"
nice -15 convert -delay $DELAY $SPOOL/gmic_c*.png exp_gmic_c.gif &
echo T gmic_c $(( $SECONDS - $debut )) >> log.toto
sleep 1

# --------------------------------------------------------
debut=$SECONDS
for i in $(seq 1 $NOMBRE)
do
	dest=$SPOOL/gmic_d_$(printf "%03d" $i).png
	echo $dest
	p=$(( $NOMBRE - $i + 1 ))
	echo gmic_d $i $p >> log.titi
	gmic $SOURCE -blur_radial $p -o $dest
	postprocess $dest $dest
	annotate "blur radial $p" $dest
done

echo 
nice -19 convert -delay $DELAY $SPOOL/gmic_d*.png exp_gmic_d.gif &
echo T gmic_c $(( $SECONDS - $debut )) >> log.toto
sleep 1

# --------------------------------------------------------
debut=$SECONDS
for i in $(seq 1 $NOMBRE)
do
	dest=$SPOOL/gmic_e_$(printf "%03d" $i).png
	echo $dest
	p=$(( ($NOMBRE+1) - $i ))
	gmic $SOURCE -blur $p -quantize $i -o $dest
	postprocess $dest $dest
	annotate "blur $p" $dest
done

echo 
nice -15 convert -delay $DELAY $SPOOL/gmic_e*.png exp_gmic_e.gif &
echo T gmic_e $(( $SECONDS - $debut )) >> log.toto
sleep 1

# --------------------------------------------------------

debut=$SECONDS
for i in $(seq 1 $NOMBRE)
do
	dest=$SPOOL/gmic_f_$(printf "%03d" $i).png
	echo $dest
	p=$(( $i + 1 ))
	gmic $SOURCE -blur 1 -tetris $p -o $dest
	postprocess $dest $dest

	annotate  "tetris $p" $dest
done

echo 
nice -13 convert -delay $DELAY $SPOOL/gmic_f*.png exp_gmic_f.gif &
echo T gmic_f $(( $SECONDS - $debut )) >> log.toto
sleep 1


# --------------------------------------------------------
debut=$SECONDS

for i in $(seq 1 $NOMBRE)
do
	dest=$SPOOL/gmic_g_$(printf "%03d" $i).png
	p=$(( $i ))
	gmic $SOURCE -blur_angular $p -o $dest
	postprocess $dest $dest

	annotate "blur angular $p" $dest 
done

echo 
nice -17 convert -delay $DELAY $SPOOL/gmic_g*.png exp_gmic_g.gif &
echo T gmic_g $(( $SECONDS - $debut )) >> log.toto
sleep 1

# --------------------------------------------------------
debut=$SECONDS

for i in $(seq 1 $NOMBRE)
do
	dest=$SPOOL/gmic_h_$(printf "%03d" $i).png
	p=$(( $i * 2 ))
	gmic $SOURCE -blur_linear 20,,$p -o $dest
	postprocess $dest $dest
	annotate "blur linear $p" $dest 
done

echo 
nice convert -delay $DELAY $SPOOL/gmic_h*.png exp_gmic_h.gif &
echo T gmic_h $(( $SECONDS - $debut )) >> log.toto
sleep 1

# --------------------------------------------------------

debut=$SECONDS

for i in $(seq 1 $NOMBRE)
do
	dest=$SPOOL/gmic_i_$(printf "%03d" $i).png
	p=$(( ($NOMBRE+1) - $i ))
	gmic $SOURCE -stencil $p,2,2 -blur 5 -o $dest
	postprocess $dest $dest
	annotate "stencil $p" $dest
done

echo 
nice convert -delay $DELAY $SPOOL/gmic_i*.png exp_gmic_i.gif &
echo T gmic_i $(( $SECONDS - $debut )) >> log.toto
sleep 1


# --------------------------------------------------------

debut=$SECONDS
 
for i in $(seq 1 $NOMBRE)
do
	dest=$SPOOL/gmic_j_$(printf "%03d" $i).png
	p=$(( $i  ))
	gmic -srand $SEED $SOURCE -hearts $p -o $dest
	postprocess $dest $dest
	annotate "hearts $p" $dest 
done

echo 
nice convert -delay $DELAY $SPOOL/gmic_j*.png exp_gmic_j.gif &
echo T gmic_j $(( $SECONDS - $debut )) >> log.toto
sleep 1

# --------------------------------------------------------
echo done
convert -delay 50 $SPOOL/gmic_*_010.png $SPOOL/gmic_*_020.png \
		gmic15.gif

# --------------------------------------------------------


