#! /bin/bash -eu
# Last edited on 2026-04-07 14:19:11 by stolfilocal

NCL_FILE="in/Tep_04_det.jpg"
NCL_CROP="460x140+009+009"

# Both lights
M07_FILE="in/365UV_007_page.png"
M07_CROP="2416x0860+0464+5260"
M07_MAXV=600

# Light from S
M29_FILE="in/365UV_029_page.png"
M29_CROP="2416x0860+0464+5260"
M29_MAXV=600

# Light from N
M37_FILE="in/365UV_037_page.png"
M37_CROP="2416x0860+0464+5260"
M37_MAXV=400

# MB365UV_007_F  365     0    95  255  1290 2125    25  753     2125
# MB365UV_029_F  365     1    63  175   885 1405    25  667     1405
# MB365UV_037_F  365     2    63  173   983 1555    27  687     1555
 
mkdir -p out

convert -crop ${NCL_CROP} ${NCL_FILE}  out/NCL.png

function msprocess() {
  page="$1"; shift
  crop="$1"; shift
  maxv="$1"; shift
  clip="$1"; shift
  
  space="RGB"
  options=( -strip -colorSpace ${space} -define png:color-type=0 -define png:bit-depth=16 )
  convert ${page} +repage -set colorspace ${space} -crop ${crop} -format %c histogram:info:- \
    | cleanup_convert_histogram.gawk \
    > ${clip/.png/.hist}
  convert ${page} +repage -set colorspace ${space} -crop ${crop} -level 0,${M07_MAXV} ${options[@]} ${clip}
}

msprocess "${M07_FILE}" "${M07_CROP}" "${M07_MAXV}" "out/M07.png"
msprocess "${M29_FILE}" "${M29_CROP}" "${M29_MAXV}" "out/M29.png"
msprocess "${M37_FILE}" "${M37_CROP}" "${M37_MAXV}" "out/M37.png"

display -title '%f' out/M*.png
