#!/bin/sh

SRC='P1010906 P1010907 P1010908 P1010909 P1010910 P1010911 P1010912 P1010913 P1010914 P1010915 P1010916 P1010917 P1010918 P1010919 P1010920 P1010921 P1010922 P1010923 P1010924 P1010925 P1010926 P1010927 P1010928 P1010929 P1010930 P1010931 P1010932 P1010933 P1010934 P1010935 P1010936 P1010946 P1010945'
DST='spring-2-g'

NONA='nona -t 2 -o tmp-g-'
FGLB='fulla -t 2 -s -g 0.09:0.04:-0.8:1.63'
CCM='color_correct -E -l -p.03 -D64 -a-4096:4096+1024 -k24:40/32 -g28:36/32 -otmp-g.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'
INPNT='greycstoration -iter 1024 -bits 16 -alpha 1.2 -sigma 0.75 -inpaint'

LANG=C; export LANG

rm -rf tmp-g
mkdir -p tmp-g
N=0
for n in ${SRC}
do
    o=$(printf '%02d' ${N})
    N=$((${N}+1))
    [ -r ${o}.tiff ] && continue
    [ -r ${n}.JPG ] || exit 126
    cp ${n}.JPG tmp-g/${o}.jpeg
done
sh ../../tools/orient tmp-g
for n in $(find tmp-g -type f)
do
    o=$(basename -s .jpeg ${n})
    ${CONV} ${n} \
        -depth 16 -orient undefined \
        $([ ${o} = 00 ] && echo '-rotate 180') \
        -enhance -unsharp 0x1 -resize 50% -unsharp 0x0.5 \
        -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
rm -rf tmp-g
#exit

#${NONA} ${DST}.pto \
#&& echo rm [0-9][0-9].tiff \
#&& ${CCM} tmp-g-0030.tif tmp-g-0021.tif && mv tmp-g.tiff tmp-g-0021.tif \
#&& ${ENBL} -f 9216x9216 -o tmp-g.tiff tmp-g-0*.tif \
#&& echo rm tmp-g-0*.tif \
true \
&& for x in 'w=256;h=1440;xs=3886;ys=3724;xd=3582;yd=3892' 'w=800;h=224;xs=4190;ys=5802;xd=3836;yd=5544' 'w=1472;h=512;xs=3852;ys=3778;xd=3906;yd=3354' 'w=448;h=992;xs=5689;ys=3878;xd=5250;yd=3538' 'w=288;h=992;xs=5628;ys=4778;xd=5400;yd=4400'
   do
    unset w h xs ys xd yd
    eval ${x}
    printf "${w}x${h}+${xs}+${ys}"
    ${CONV} tmp-g.tiff -crop ${w}x${h}+${xs}+${ys} +repage -matte -gaussian 0x1 -background transparent -virtual-pixel background -channel A -gaussian 64x36 -negate -evaluate multiply 2.5 -negate -evaluate multiply 1.25 -compress zip patch-${DST}.tiff
    printf ' => '
    ${CONV} tmp-g.tiff patch-${DST}.tiff -geometry ${w}x${h}+${xd}+${yd} -compose src-over -composite -compress zip tmp-g.tiff
    printf "${w}x${h}+${xd}+${yd}\n"
    rm patch-${DST}.tiff
   done \
&& ${CONV} tmp-g.tiff -resize x7168 -unsharp 0x0.5+0.6+0.04 -compress zip tmp-r.tiff \
&& echo rm tmp-g.tiff \
&& ${CONV} tmp-r.tiff -channel A -separate -fill black -stroke none -draw 'circle 3071,3071 1280,3071' +matte -compress zip alpha-${DST}.tiff \
&& ${CONV} alpha-${DST}.tiff -background white -virtual-pixel background -resize x512 -gaussian 12x16 -evaluate multiply 2.6 -negate -resize x7168 -compress zip alpha-${DST}.tiff \
&& ${CONV} alpha-${DST}.tiff -evaluate multiply 0.5 alpha-${DST}.tiff alpha-${DST}.tiff -combine +matte -compress zip alpha-${DST}.tiff \
&& ${FGLB} -o alpha-${DST}.tiff alpha-${DST}.tiff \
&& ${CONV} alpha-${DST}.tiff -channel G -separate -resize x6144 -compress zip alpha-${DST}.tiff \
&& ${CONV} tmp-r.tiff +matte -compress zip tmp-r.tiff \
&& ${FGLB} -o tmp-r.tiff tmp-r.tiff \
&& ${CONV} tmp-r.tiff -resize x6144 -unsharp 0x0.5+0.6+0.04 -compress zip ${DST}.tiff \
&& rm tmp-r.tiff \
&& ${CONV} ${DST}.tiff alpha-${DST}.tiff -compose copyopacity -composite -compress zip ${DST}.tiff \
&& rm alpha-${DST}.tiff \
&& for x in 128:8BA5CF:4:8:+992+896 128:96ACCF:4:8:+5320+1858
   do
    ifs="${IFS}"
    IFS=:
    set ${x}
    s=${1}
    c=${2}
    k=${3}
    g=${4}
    o=${5}
    IFS="${ifs}"
    x=$((${s}/2))
    y=$((${s}/8))
    k=$((${s}*${k}))
    ${CONV} -size ${s}x${s} xc:"#${c}00" -fill "#${c}" -stroke "#${c}" -draw "circle ${x},${x} ${x},${y}" -channel A -gaussian 0x${g} -resize ${k} -compress zip patch.tiff
    ${CONV} ${DST}.tiff patch.tiff -geometry ${k}x${k}${o} -compose src-over -composite -compress zip ${DST}.tiff
    rm patch.tiff
   done \
&& ${CONV} -size 6144x6144 xc:'#84A0CD' ${DST}.tiff -compose src-over -composite -compress zip ${DST}.tiff \
&& ${CONV} -size 257x257 xc:'#0B0323' -background '#0B0323' -virtual-pixel background -fill black -stroke black -draw 'circle 128,128 6,128' -gaussian 0x12 +matte -compress zip sky-${DST}.tiff \
&& ${CONV} -size 257x257 xc:white -background white -virtual-pixel background -fill black -stroke black -draw 'circle 128,128 6,128' -gaussian 0x12 +matte -compress zip sky-alpha-${DST}.tiff \
&& ${CONV} sky-${DST}.tiff sky-alpha-${DST}.tiff -compose copyopacity -composite -resize 6144x6144 -compress zip sky-${DST}.tiff \
&& rm sky-alpha-${DST}.tiff \
&& ${CONV} ${DST}.tiff sky-${DST}.tiff -compose src-over -composite -density 300 -compress zip ${DST}.tiff \
&& rm sky-${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x4096 -unsharp 0x0.5+0.6+0.04 -compress zip -density 300 ${DST}-prn.tiff \
&& ${CONV} ${DST}.tiff -resize x3072 -unsharp 0x0.5+0.6+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x1536 -unsharp 0x0.5+0.4+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.0 -quality 90 ${DST}-big.jpeg \
&& ${CONV} ${DST}.tiff -resize x768 -unsharp 0x0.5+0.4+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.0 -quality 90 ${DST}.jpeg \
&& ${CONV} ${DST}.tiff -resize x512 -unsharp 0x0.4+0.3+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x256 -unsharp 0x0.4+0.3+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.0 -quality 90 ${DST}-small.jpeg \
&& rm ${DST}.tiff
exit

&& for x in 'w=256;h=1440;xs=3886;ys=3724;xd=3582;yd=3892' 'w=800;h=224;xs=4190;ys=5802;xd=3836;yd=5544' 'w=1472;h=512;xs=3852;ys=3778;xd=3906;yd=3354' 'w=448;h=992;xs=5689;ys=3878;xd=5250;yd=3538' 'w=288;h=992;xs=5628;ys=4778;xd=5400;yd=4400' 'w=512;h=512;xs=1964;ys=1482;xd=1526;yd=1440' 'w=352;h=768;xs=7928;ys=2270;xd=8374;yd=3074'
&& for x in 0:192x672+2560+2720 1:448x192+2688+3456 2:832x928+2752+2496
   do
    ifs="${IFS}"
    IFS=:
    set ${x}
    n=${1}
    g=${2}
    IFS="${ifs}"
    #${CONV} ${DST}.tiff -crop ${g} +repage -quality 95 mask-${DST}-${n}.png
    [ -r mask-${DST}-${n}.png ] || continue
    ${CONV} ${DST}.tiff -crop ${g} +repage -compress zip ${DST}-${n}.tiff
    ${INPNT} ${DST}-${n}.tiff -m mask-${DST}-${n}.png -o ${DST}-${n}.tiff
    ${CONV} ${DST}.tiff ${DST}-${n}.tiff -geometry ${g} -compose src-over -composite -compress zip ${DST}.tiff
    rm ${DST}-${n}.tiff
   done \
