#!/bin/sh

[ -r .conf-right ] || exit 125

NONA='nona -t 2 -o tmp-'
CCM='color_correct -E -l -p.03 -D64 -a-4096:4096+1024 -k24:40/32 -g28:36/32 -otmp.tiff'
ENBL='enblend -w -z -m 896 --fine-mask'
#CONV='convert -limit memory 512 -limit map 256'
CONV='convert'
INPNT='greycstoration -alpha 0.9 -sigma 0.2 -p 5 -a 1 -dt 20 -iter 5 -bits 16 -m mask.png -inpaint'

LANG=C; export LANG

. .conf-right

[ -n "${DEPTH}" ] || DEPTH="${2}"
[ -n "${DEPTH}" ] || DEPTH=16

N=0
for n in ${SRC}
do
    o=$(printf '%02d' ${N})
    N=$((${N}+1))
    [ -r ${o}.tiff ] && continue
    [ -r ${n}.JPG ] || exit 126
    ${CONV} ${n}.JPG \
        -depth ${DEPTH} -orient undefined \
        -unsharp 0x0.3 \
        -compress zip ${o}.tiff
    [ -r mask-${o}.png ] \
    && ${CONV} ${o}.tiff -matte \
        mask-${o}.png -compose multiply -composite \
        mask-${o}.png -channel A -fx 'v.r' \
        -compress zip ${o}.tiff
done
[ "${1}" = prep ] && exit

f=$(awk '/^p /{match($0,/w[0-9]+/);w=substr($0,RSTART+1,RLENGTH-1);match($0,/h[0-9]+/);h=substr($0,RSTART+1,RLENGTH-1);print w"x"h;nextfile}' ${DST}.pto)

final () {
    s=${1}
    shift
    pfsinimgmagick --linear ${DST}.tiff \
    | pfsgamma -m 1 -g 1.20 \
    | pfstmo_reinhard02 -v -k 0.09 \
    | pfsgamma -m 1 -g 2.00 \
    | pfsoutimgmagick ${DST}${s}.tiff \
    && ${CONV} ${DST}${s}.tiff \
        -gamma 1.10,1.10,1.00 \
        -modulate 100,110,102 \
        ${@} -quality 90 ${DST}${s}.jpeg
    r=${?}
    rm -f ${DST}${s}.tiff
    return ${r}
}

${NONA} ${DST}.pto \
&& rm [0-9][0-9].tiff \
&& ${ENBL} -f ${f} -o tmp.tiff tmp-0*.tif \
&& rm tmp-0*.tif \
&& ${CONV} tmp.tiff -crop 6464x4992+800+528 +repage +matte -compress zip ${DST}.tiff \
&& rm tmp.tiff \
&& ${CONV} ${DST}.tiff -resize x3072 -unsharp 0x0.55+0.6+0.04 -compress zip ${DST}.tiff \
&& final -huge \
&& ${CONV} ${DST}.tiff -resize x1536 -unsharp 0x0.55+0.5+0.04 -compress zip ${DST}.tiff \
&& final -big \
&& ${CONV} ${DST}.tiff -resize x768 -unsharp 0x0.55+0.4+0.04 -compress zip ${DST}.tiff \
&& final -middle \
&& ${CONV} ${DST}.tiff -resize x384 -unsharp 0x0.45+0.4+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x256 -unsharp 0x0.45+0.4+0.03 -compress zip ${DST}.tiff \
&& final -small -gamma 1.05 \
&& rm ${DST}.tiff
exit

&& ${CONV} ${DST}.tiff -resize x3072 -unsharp 0x0.55+0.6+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -negate -gamma 1.20 -negate -gamma 1.20,1.20,1.14 -modulate 100,115 -quality 90 ${DST}-huge.jpeg \
&& ${CONV} ${DST}.tiff -resize x1536 -unsharp 0x0.55+0.5+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -negate -gamma 1.20 -negate -gamma 1.20,1.20,1.14 -modulate 100,115 -quality 90 ${DST}-big.jpeg \
&& ${CONV} ${DST}.tiff -resize x768 -unsharp 0x0.55+0.4+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -negate -gamma 1.20 -negate -gamma 1.20,1.20,1.14 -modulate 100,115 -quality 90 ${DST}.jpeg \
&& ${CONV} ${DST}.tiff -resize x384 -unsharp 0x0.45+0.4+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x256 -unsharp 0x0.45+0.4+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -negate -gamma 1.25 -negate -gamma 1.20,1.20,1.19 -modulate 100,115 -quality 90 ${DST}-small.jpeg \
