#!/bin/sh
#
# flicker_cmp [options] image1 image2 ...
#
# Generate and magick display an animation of the images given with 1/2 second delay
# so that you can see and magick compare the very slight differences between the two
# images.  A label is added to the image so you know which image is which.
#
# Additional options allow you to resize scale a specific area of interest
# in the two images, so you can more closely check on slight color changes
# or look closely at much larger images.
#
# Any number of images (even just one, or a whole animation) may be given.
#
# OPTIONS
#    -d delay    the delay for each frame in centi-seconds (def = 50)
#    -l label    Set label handling to this. EG: '%l' or '%s'  (def = '%f')
#    -b color    label/background fill color
#
#    -r size     resize image to this size (pixels or percentage)
#    -s size     scale image to this size (pixels or percentage)
#    -c crop     crop/extent images at given offset prior to scaling
#    -j size     limit the jpeg reading (for really large images)
#
#    -o file     output to this image file, don't display
#
# Examples...
#
# Compare two enlarged images
#
#   flicker_cmp -s 300%  rose_1.png rose2.png
#
# Check a area where two images were merged (from "overlap" script)
#
#   flicker_cmp -c 200x200+550+550 -scale 300% map_orig.png merged_*.png
#
# Advanced pre-prepared label handling (broken? Why?)
#
#   magick -size 100x100 xc:red xc:blue -set color '%[pixel:u]' miff:- |\
#       flicker_cmp -l '%[color]' -d 100 -
#
###
#
# Last update:  20 March 2013  (new options added)
#
# Created by Anthony Thyssen   5 September 2007
#
PROGNAME=`type $0 | awk '{print $3}'`  # search for executable on path
PROGDIR=`dirname $PROGNAME`            # extract directory of program
PROGNAME=`basename $PROGNAME`          # base name of program
Usage() {                              # output the script comments as docs
  echo >&2 "$PROGNAME:" "$@"
  sed >&2 -n '/^###/q; /^#/!q; s/^#//; s/^ //; 3s/^/Usage: /; 2,$ p' \
          "$PROGDIR/$PROGNAME"
  exit 10;
}

resize=''
crop=''
color=skyblue
delay=50
output=''

while [  $# -gt 0 ]; do
  case "$1" in
  --help|--doc*) Usage ;;

  -b|-background)   shift; color="$1" ;;    # label/background fill color
  -l|-label)  shift; label="$1" ;;          # set displayed label to this string
  -d|-delay)  shift; delay="$1" ;;          # animation delay

  -c|-crop)   shift; crop="$1" ;;           # crop/extent images
  -r|-resize) shift; resize="-resize $1" ;; # resize image to this size
  -s|-scale)  shift; resize="-scale $1" ;;  # scale image to this size
  -j|-jpeg)   shift; jpeg="-define jpeg:size=$1" ;;

  -o|-output) shift; output="$1" ;;         # output to file, don't display

  -)  break ;;           # STDIN,  end of user options
  --) shift; break ;;    # end of user options
  -*) Usage "Unknown option \"$1\"" ;;
  *)  break ;;           # end of user options

  esac
  shift   # next option
done

# add crop/extent option before any resize
# Not this goes wrong with negative offsets
# but that does not make sense, so okay!
if [ "X$crop" != 'X' ]; then
  case "$crop" in
    *[+-]*)
      extent=`echo "$crop" | sed 's/[+-].*//'`
      ;;
    *)
      extent="$crop"
      crop="$crop+0+0"
      gravity="-gravity center"
      ;;
  esac
  resize="$gravity -crop $crop! -extent $extent $resize"
fi

# set labeling defaults, different for stdin vs filenames
if [ "X$label" = "X" ]; then
  case "$*" in
    -|*:-) label='%s:%l' ;;
    *)     label='%f' ;;
  esac
fi

#magick $jpegsize \
#magick "$@" -set label "$label" -background $color $resize miff:- |
# Do each image seperatally as they could be very large
for img in "$@"; do
  magick $jpeg "$img" -set label "$label" \
          -background $color $resize miff:-
done |
  magick montage - -geometry +0+0 -tile 1x1 -background $color miff:- |
    if [ "X$output" = "X" ]; then
      magick animate -delay "$delay" -dispose background -loop 0 -
    else
      magick - -delay "$delay" -dispose background -loop 0 "$output"
    fi

