#!/bin/sh
# Copyright Karl Palsson, July 2006
#
# magically combines two images (photos) that were exposed differently,
# ala http://www.gimpguru.org/Tutorials/BlendingExposures/ or
# http://www.luminous-landscape.com/tutorials/digital-blending.shtml
# only using imagemagick, so you can just feed some images at it, and script
# away.
#
# REQUIRES: ImageMagick

if [ $# != 3 ]; then
    echo "usage is $0 [light image] [dark image] [output image name]"
    echo "(output image will NOT be trampled!)"
    exit 1;
fi
LIGHT=$1
DARK=$2
OUTPUT=$3

if [ ! -e $LIGHT ]; then
    echo "Couldn't find light image: $LIGHT"
    exit 1;
fi

if [ ! -e $DARK ]; then
    echo "Couldn't find dark image: $DARK"
    exit 1;
fi

if [ -e $OUTPUT ]; then
    echo "$OUTPUT already exists! Will not trample"
    exit 1;
fi

#### now the meat....

# first, we need a grayscale, blurred copy of the light image
LIGHTBLUR=$LIGHT.blurtemp.$$
convert -type GrayScale -blur 40x20 $LIGHT $LIGHTBLUR
# then we stack!
composite $DARK $LIGHT $LIGHTBLUR -compose atop $OUTPUT
# cleanup
rm $LIGHTBLUR
