#! /bin/bash -eu
# Last edited on 2025-12-10 12:21:07 by stolfi

# Extracts a specified rectbagle from three multispectral images of a given page.
# Combines them as the R, G, B channels of a color PNG image "raw.png", with a 
# 60-pixel gray border.

MS="${HOME}/projects/voynich/MultiSpectral/davis"
WORK="${HOME}/projects/voynich/work"
BIN="${HOME}/bin"

page="$1"; shift
bandR="$1"; shift
bandG="$1"; shift
bandB="$1"; shift
crop="$1"; shift

# Create a composite image "raw.png"
bands=( ${bandR} ${bandG} ${bandB}  )

for band in ${bands[@]}; do
  convert \
    ${MS}/${page}/${band}/page.png \
    +repage \
    -crop "${crop}" \
    -normalize \
    .${band}.png 
done

ofile=combined.png
convert \
  .${bands[0]}.png \
  .${bands[1]}.png \
  .${bands[2]}.png \
  -combine \
  -bordercolor "rgb(50%,50%,50%)" \
  -border "60x60" \
  +repage \
  ${ofile}

rm -f raw.png 
ln -s ${ofile} raw.png
