#! /bin/bash
# Last edited on 2023-09-25 10:34:58 by stolfi

# Applies perspective mappings on selected images to get them all
# roughly aligned.  Uses four hand-picked ref points in each image.

pts_in_dir="$1"; shift   # Parent directory of ".ipts" files.
img_out_dir="$1"; shift  # Parent directory of output (rectified) files.

img_in_dir="images/full"  # Directory with full original images.
nhood_out_dir="${img_out_dir}/nhood" # Directory for feature neighborhood clips.

mkdir -p ${nhood_out_dir}

oUnit=0.500 # Output pixels per user unit.

# Some lists of input files:
img_in_files_all=( ${img_in_dir}/[a-z][a-z][0-9][0-9][0-9]*.jpg )
img_in_files_new=( ${img_in_dir}/nt[0-9][0-9][0-9]*.jpg )
img_in_files_some=( ${img_in_dir}/{ae023a,wc0{06,82}a,wc0{01,02,11}{a,b}}.jpg )
img_in_files_few=( ${img_in_dir}/{ae023a,wc051a,nt002b,wc071b}.jpg )

sides=( a b )

for side in ${sides[@]} ; do
  
  # File with user output coords of ref feature points:
  opts_in_file="${pts_in_dir}/ref${side}.pts" # Name of file with their user coords on {img_out_file}.
  if [[ ! ( -s ${opts_in_file} ) ]]; then
    echo "** ${opts_in_file} not found" 1>&2; exit 1
  fi
  
  # Map also the ref image as a consistency check:
  ref_img_in_file="${pts_in_dir}/ref${side}.jpg"
  ref_img_out_file="${img_out_dir}/ref${side}.png"
  
  # Map also the ref mask image as a consistency check:
  mask_img_in_file="${pts_in_dir}/ref${side}-mask.jpg"
  mask_img_out_file="${img_out_dir}/ref${side}-mask.png"
  
  # Edit the next line as appropriate:
  img_in_files=( \
    "${ref_img_in_file}" \
    "${mask_img_in_file}" \
    `ls "${img_in_files_all[@]}" | egrep -e "${side}[.]" | sort` \
  )
  
  # Decide if will show each image as computed:
  if [[ ${#img_in_files[@]} -le 12 ]]; then show=1; else show=0; fi

  for img_in_file in "${img_in_files[@]}" ; do 
    iname="${img_in_file##*/}"; iname="${iname/.*}" # Name of input image, sans the directory and extension.
    echo "=== ${iname} ======================================================================" 1>&2

    img_out_file="${img_out_dir}/${iname}.png"  # Filename of output image, with directory and extension.
    ipts_in_file="${pts_in_dir}/${iname}.ipts"   # Name of file with given coords of key features on {img_in_file}.
    
    opts_ump_in_file="${opts_in_file}"  # File with output point coords to be mapped back to input image.
    mpts_ump_out_file="${img_out_dir}/${iname}.mpts"  # Name of file with points mapped back to input image.

    nhood_pref="${img_out_dir}/nhood/${iname}"  # Filename prefix for neighborhood clips.

    if [[ -s ${ipts_in_file} ]]; then
      echo "=== ${img_in_file} ===" 1>&2
      persp_rectify.sh \
          ${img_in_file} \
          ${ipts_in_file} ${opts_in_file} \
          ${oUnit} \
          ${img_out_file} \
          ${opts_ump_in_file} ${mpts_ump_out_file} \
          ${nhood_pref} \
        < /dev/null
      if [[ -s ${img_out_file} ]] ; then 
        if [[ ${show} -ne 0 ]]; then 
          display -title '%f' ${img_out_file}
          display -title '%f' ${nhood_pref}-*.png
        fi
      else
        echo "** ${img_out_file} not generated" 1>&2
      fi
    else
      echo "** ${ipts_in_file} not found" 1>&2
    fi
  done

  # The "ref" file should simply be reduced by ${oUnit}. 
  # So let do it directly, to compare the results:
  oUnitPct=`echo "${oUnit}*100" | bc -lq`
  exp_ref_img_out_file="${img_out_dir}/ref${side}-exp.png"
  convert ${ref_img_in_file} -resize "${oUnitPct}%" -colorspace Gray ${exp_ref_img_out_file}

  if [[ $show -ne 0 ]]; then
    display -title '%f' ${ref_img_out_file} ${exp_ref_img_out_file}
  fi
done
