#! /bin/bash
# Last edited on 2023-09-26 10:17:53 by stolfi

# Creates and displays a strip image with the extracted neighborhood of specified reference 
# points of a rectified image {simg} ("{img}{side}").
#
# The first row of the strip has all extracted neighborhoods "{simg}-{tag}.png" from a given
# {nhoodir} directory.  The second row of the strip displays the correponding neighborhoods
# "ref{side}-tag.png" from that same directory.  If an alternate directory {nhoodir2} is give,
# the files "{simg}-{tag}.png" from that directory are displayed in the third row.

simg="$1"; shift     # Image name "{img}{side}" where {side} is 'a' or 'b'.
nhoodir="$1"; shift  # Directory with extracted neighborhood images "{simg}-{tag}.png".
nhoodir2="$1"; shift # Directory with alternative extracted neighborhood images or 'NONE'.
tags="$@"; shift   # Point tags to check.

echo "simg = ${simg}" 1>&2
echo "nhoodir = ${nhoodir}" 1>&2
echo "nhoodir2 = ${nhoodir2}" 1>&2

side="${simg: -1:1}" # The space is essential -- what a crock!
echo "side = ${side}" 1>&2

if [[ ${#tags[@]} -eq 0 ]]; then
  echo "** ${0##*/}: no tags specified" 1>&2; exit 1
fi

tmp="/tmp/$$"

# Create the columns of the strip:
colimgs=()
for tag in ${tags[@]}; do  
  imgs=()
  img1="${nhoodir}/${simg}-${tag}.png"
  if [[ ! ( -s "${img1}" ) ]]; then
    echo "** ${0##*/}: snip image ${img1} not found" 1>&2; exit 1
  fi
  imgs+=( ${img1} )
  img2="${nhoodir}/ref${side}-${tag}.png"
  if [[ ! ( -s "${img2}" ) ]]; then
    echo "** ${0##*/}: snip image ${img2} not found" 1>&2; exit 1
  fi
  imgs+=( ${img2} )
  img3="${nhoodir2}/${simg}-${tag}.png"
  if [[ -s "${img3}" ]]; then
    imgs+=( ${img3} )
  fi
  colimg="${tmp}-${tag}.png"
  convert ${imgs[@]} -append ${colimg}
  colimgs+=( ${colimg} )
done

stripimg="${tmp}-strip.png"
convert ${colimgs[@]} +append ${stripimg}
if [[ -s ${stripimg} ]]; then 
  display -title "${simg}" -resize '50%' ${stripimg}
else
  echo "** strip image ${} not generated" 1>&2 
fi

rm ${tmp}-*
