#!/bin/sh

SRC='P1010727 P1010726 P1010725 P1010724 P1010736 P1010735 P1010734 P1010733 P1010732 P1010731 P1010730 P1010729 P1010728'
CONF='P1010724'
DST='kazan-3'
DST2='kazan-3-360'

UFRAW='ufraw-batch --interpolation=ahd --out-type=tiff16 --overwrite --zip --wavelet-denoising-threshold=250'
NONA='nona -t 2 -o tmp-'
FULLA='fulla -t 2'
CCM='color_correct -E -c -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'
#INPNT='greycstoration -alpha 0.9 -sigma 0.2 -iter 512 -bits 16 -inpaint'
INPNT='greycstoration -iter 1024 -bits 16 -inpaint'

LANG=C; export LANG

if false
then

N=0
for n in ${SRC}
do
    o=$(printf '%02d.tiff' ${N})
    N=$((${N}+1))
    [ -r ${o} ] && continue
    [ -r ${n}.RAW.bz2 ] && bzcat ${n}.RAW.bz2 >${n}.RAW
    [ -r ${n}.RAW.gz ] && zcat ${n}.RAW.gz >${n}.RAW
    [ -r ${n}.RAW ] || exit 126
    fl=$(dcraw -i -v ${n}.RAW|awk '/^Focal length: /{print $3}')
    conf=${n}.ufraw
    [ -z "${CONF}" ] || conf=${CONF}.ufraw
    [ -r ${conf} ] || exit 125
    ${UFRAW} --conf=${conf} --output=${n}.tiff ${n}.RAW
    [ -r ${n}.RAW.bz2 -o -r ${n}.RAW.gz ] && rm -f ${n}.RAW
    tca="$( ( cd ../../tools/tca && echo "disp(tca('tca.db','fz50',${fl},1,0))"|octave -q|cat|head -1 ) )"
    ${FULLA} ${tca} -o ${n}.tiff ${n}.tiff
    ${CONV} ${n}.tiff \
        -enhance -unsharp 0x1.25 \
        -background transparent \
        -rotate 2.85 \
        -compress zip ${o}
    rm -f ${n}.tiff
done
#exit

fi

#${NONA} ${DST2}.pto \
#&& rm [0-9][0-9].tiff \
#&& ${ENBL} -f 24342x4192 -o tmp.tiff tmp-0*.tif \
#&& echo rm tmp-0*.tif \
true && for x in 2:416x192+7090+2056 1:480x192+9932+2048 0:416x192+16842+2086
   do
    ifs="${IFS}"
    IFS=:
    set ${x}
    n=${1}
    g=${2}
    IFS="${ifs}"
    #${CONV} tmp.tiff -crop ${g} +repage -quality 95 mask-${DST}-${n}.png; continue
    [ -r mask-${DST}-${n}.png ] || continue
    ${CONV} tmp.tiff -crop ${g} +repage -compress LZW tmp-${n}.tiff
    ${INPNT} tmp-${n}.tiff -m mask-${DST}-${n}.png -o tmp-${n}.tiff
    ${CONV} tmp.tiff tmp-${n}.tiff -geometry ${g} -compose src-over -composite -compress zip tmp.tiff
    rm tmp-${n}.tiff
   done \
&& ${CONV} tmp.tiff \
    -crop 10336x3392+8212+292 +repage \
    -compress zip ${DST}.tiff \
&& ${CONV} tmp.tiff \
    -crop 24342x3280+0+292 +repage \
    -compress zip ${DST2}.tiff \
&& echo rm tmp.tiff \
&& ${CONV} ${DST}.tiff -resize x2416 -unsharp 0x0.75+0.8+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x1536 -unsharp 0x0.75+0.6+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.2 -quality 90 ${DST}-big.jpeg \
&& ${CONV} ${DST}.tiff -resize x768 -unsharp 0x0.75+0.6+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.2 -quality 90 ${DST}.jpeg \
&& ${CONV} ${DST}.tiff -resize x384 -unsharp 0x0.65+0.6+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x256 -unsharp 0x0.65+0.6+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.3 -quality 90 ${DST}-small.jpeg \
&& ${CONV} ${DST}.tiff -resize 640 -unsharp 0x0.6+0.5+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.3 -quality 90 ${DST}-tiny.jpeg \
&& rm ${DST}.tiff \
&& ${CONV} ${DST2}.tiff -resize x2416 -unsharp 0x0.75+0.8+0.04 -compress zip ${DST2}.tiff \
&& ${CONV} ${DST2}.tiff -resize x1536 -unsharp 0x0.75+0.6+0.04 -compress zip ${DST2}.tiff \
&& ${CONV} ${DST2}.tiff -gamma 1.2 -quality 90 ${DST2}-big.jpeg \
&& ${CONV} ${DST2}.tiff -resize x768 -unsharp 0x0.75+0.6+0.04 -compress zip ${DST2}.tiff \
&& ${CONV} ${DST2}.tiff -gamma 1.2 -quality 90 ${DST2}.jpeg \
&& ${CONV} ${DST2}.tiff -resize x384 -unsharp 0x0.65+0.6+0.03 -compress zip ${DST2}.tiff \
&& ${CONV} ${DST2}.tiff -resize 1024 -unsharp 0x0.65+0.6+0.03 -compress zip ${DST2}.tiff \
&& ${CONV} ${DST2}.tiff -gamma 1.3 -quality 90 ${DST2}-small.jpeg \
&& ${CONV} ${DST2}.tiff -resize 640 -unsharp 0x0.6+0.5+0.03 -compress zip ${DST2}.tiff \
&& ${CONV} ${DST2}.tiff -gamma 1.3 -quality 90 ${DST2}-tiny.jpeg \
&& rm ${DST2}.tiff
exit
