#!/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.
#
# Copyright (C) 2005,2006  Charles Bouveyron <charles.bouveyron@free.fr>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# Author : Charles Bouveyron <charles.bouveyron@free.fr>
#   Mathieu Vilaplana <mathieu@creationgif.com>
#       Raphaël Pinson <raphink@raphink.net>

test -n "$TDEHOME" || TDEHOME="`tde-config --localprefix`"; export TDEHOME
#KIMDIR=`mktemp -d "$TDEHOME"/tmp-"$USER"/kim.XXXXXXXX` || exit 1
KIMDIR=`mktemp -d "$TDEHOME"/tmp-"$HOSTNAME"/kim.XXXXXXXX` || exit 1

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
        ((compteur++))
        x=$((($j-1)*320));
        y=$((($i-1)*240));
        convert -crop 320x240+$x+$y "$1" "$KIMDIR"/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 "$KIMDIR"/imagette.*.jpg
rm -rf "$KIMDIR"

dcop "$dcopRef" close

