#!/bin/sh
# Create a focus stacked photo
# Usage: $0 result component-images
# $Id: focusstack,v 1.1 2016/03/08 02:01:16 grog Exp $
ECHO=
if [ $# = 0 ]; then
  echo Usage: $0 result component-images
  exit 1
fi
if [ "$1" = "-g" ]; then      # group by...
  GROUPBY=$2
  shift
  shift
  RESULT=$1
  shift
  while [ "$1" != "" ]; do
    THISGROUP=$GROUPBY
    INPUTS=
    OUTPUT=
    while [ $THISGROUP -gt 0 ]; do
      INPUTS="$INPUTS $1"
      OUTPUT="$OUTPUT$1"
      shift
      THISGROUP=`expr $THISGROUP - 1`
    done
  echo HDR $OUTPUT $INPUTS
  SRC="$SRC $OUTPUT"
  done | by 3
  exit
else
  RESULT=$1
fi
if [ -e $RESULT ]; then
  echo $RESULT already exists
  exit 1
fi
TMP=$RESULT.tmp
shift
# On Olympus cameras at any rate, this is in the middle of the exposure range.
ANCHOR=
while [ $# -gt 0 ]; do
  if [ -e $1 ]; then
    FILES="$FILES $1"
    if [ "$ANCHOR" = "" ]; then
      ANCHOR=$1
    fi
  else
    echo $1 not found
  fi
  shift
done
echo Creating $RESULT from $FILES
xtset Creating $RESULT
$ECHO nice align_image_stack -m -a $TMP $FILES
$ECHO nice enfuse --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 \
   --hard-mask  -o $RESULT $TMP*
$ECHO exifcopy $ANCHOR $RESULT
$ECHO rm $TMP*
