#!/bin/bash
#
# A small script to convert Sony Cybershot multiburst images into an animated GIF. The script requires the 
# imagemagick command line tools to be installed.
#
# Author : Charles Bouveyron <charles.bouveyron@free.fr>
#	Mathieu Vilaplana <mathieu@creationgif.com>

let "nbfiles = $# -1"
dcopRef=`kdialog --progressbar "Kim - Initialising compression" $nbfiles`
dcop $dcopRef showCancelButton true

compteur=0

# Extract all 16 subpictures
for ((i=1;i<=4;i++))
do
	for ((j=1;j<=4;j++))
	do
		#test if cancel button has been pushed
		if test "true" = `dcop $dcopRef wasCancelled`;then
			dcop $dcopRef close
			exit 1
		fi
		let "compteur +=1"
		x=$((($j-1)*320));
		y=$((($i-1)*240));
		convert -crop 320x240+$x+$y "$1" imagette.$i.$j.jpg
		dcop $dcopRef setLabel "Kim - Compressing file `basename "$FILE"`"
 		dcop $dcopRef setProgress $compteur
	done
done

# Convert in gif animation
convert -adjoin -delay 5 imagette.*.jpg animation.gif

# Remove subpictures (?!)
rm imagette.*.jpg

dcop $dcopRef close

