DST?=moscow-6

CPUS?=4
MEM?=1536
DEPTH?=16
IMCOMP?=zip
ENCOMP?=DEFLATE
RGBPROFILE?=../../tools/profiles/RGB.icc
sRGBPROFILE?=../../tools/profiles/sRGB.icm

CONV?=convert -limit area 3GiB
REMAP?=eval nona -v -t ${CPUS} -m TIFF_m -r ldr -p UINT${DEPTH} -z ${ENCOMP} -o tmp-
FFUSE?=eval enfuse -v --compression=${ENCOMP} -m ${MEM} -b 16384 --exposure-weight=0 --saturation-weight=0  --contrast-weight=1 --hard-mask --fallback-profile='../../tools/profiles/RGB.icc' --ciecam
MERGE?=unset EXIFTOOL EXIFTAG; eval sh ../../tools/pfshdrcalibrate-wrapper -v -x -c none -b 16 -m 1 -f ${DST}.m -O
DEGHOST?=deghosting_mask -v -s 60 -c 0.8 -t 150 -i 8 -a mw
BLEND?=eval enblend -v --compression=${ENCOMP} -m ${MEM} -b 16384 --fine-mask --primary-seam-generator=nearest-feature-transform --mask-vectorize=12 -f 2112x3072+192+96

EXIFTFF?=exiftool -overwrite_original -P -TagsFromFile
EXIFTAG?=-EXIF:ExposureTime -EXIF:ShutterSpeedValue -EXIF:ApertureValue -EXIF:ISO

RM=echo
MV=mv
LN=ln -f

all: ${DST}.tiff

tmp-0000.tif: ${DST}.pto 00-e00-f0000.tiff
	@${REMAP} -i 0 -e 11.9657842161378 ${DST}.pto
tmpf00-e00-f0000.tiff: tmp-0000.tif
	@${LN} tmp-0000.tif tmpf00-e00-f0000.tiff
	@${EXIFTFF} 00-e00-f0000.tiff ${EXIFTAG} tmpf00-e00-f0000.tiff
	@${RM} tmp-0000.tif
tmpe00-e00.tiff: tmpf00-e00-f0000.tiff
	@${LN} tmpf00-e00-f0000.tiff tmpe00-e00.tiff
	@${EXIFTFF} tmpf00-e00-f0000.tiff ${EXIFTAG} tmpe00-e00.tiff
	@${RM} tmpf00-e00-f0000.tiff
tmp-0001.tif: ${DST}.pto 00-e01-f0000.tiff
	@${REMAP} -i 1 -e 13.2877124117962 ${DST}.pto
tmpf00-e01-f0000.tiff: tmp-0001.tif
	@${LN} tmp-0001.tif tmpf00-e01-f0000.tiff
	@${EXIFTFF} 00-e01-f0000.tiff ${EXIFTAG} tmpf00-e01-f0000.tiff
	@${RM} tmp-0001.tif
tmpe00-e01.tiff: tmpf00-e01-f0000.tiff
	@${LN} tmpf00-e01-f0000.tiff tmpe00-e01.tiff
	@${EXIFTFF} tmpf00-e01-f0000.tiff ${EXIFTAG} tmpe00-e01.tiff
	@${RM} tmpf00-e01-f0000.tiff
tmp-0002.tif: ${DST}.pto 00-e02-f0000.tiff
	@${REMAP} -i 2 -e 15.2585660661367 ${DST}.pto
tmpf00-e02-f0000.tiff: tmp-0002.tif
	@${LN} tmp-0002.tif tmpf00-e02-f0000.tiff
	@${EXIFTFF} 00-e02-f0000.tiff ${EXIFTAG} tmpf00-e02-f0000.tiff
	@${RM} tmp-0002.tif
tmpe00-e02.tiff: tmpf00-e02-f0000.tiff
	@${LN} tmpf00-e02-f0000.tiff tmpe00-e02.tiff
	@${EXIFTFF} tmpf00-e02-f0000.tiff ${EXIFTAG} tmpe00-e02.tiff
	@${RM} tmpf00-e02-f0000.tiff
tmpt00.tiff: tmpe00-e00.tiff tmpe00-e01.tiff tmpe00-e02.tiff
	@for n in tmpe00-e00.tiff tmpe00-e01.tiff tmpe00-e02.tiff; \
	 do \
		${CONV} $${n} -profile ../../tools/profiles/RGB.icc -profile ../../tools/profiles/sRGB.icm $${n}; \
	 done
	@${DEGHOST} tmpe00-e00.tiff tmpe00-e01.tiff tmpe00-e02.tiff
	@for n in tmpe00-e00.tiff tmpe00-e01.tiff tmpe00-e02.tiff; \
	 do \
		${CONV} $${n} -profile ../../tools/profiles/sRGB.icm -profile ../../tools/profiles/RGB.icc $${n}; \
	 done
	@for n in tmpe00-e00.tiff tmpe00-e01.tiff tmpe00-e02.tiff; \
	 do \
		${CONV} $${n} \( +clone -fx '1-s.o' +matte $${n%.tiff}_mask.tif -compose multiply -composite \) -compose copy-opacity -composite $${n}; \
		${RM} $${n%.tiff}_mask.tif; \
	 done
	@${MERGE} -o tmpt00.tiff tmpe00-e00.tiff tmpe00-e01.tiff tmpe00-e02.tiff
	@${RM} tmpe00-e00.tiff tmpe00-e01.tiff tmpe00-e02.tiff
tmp00.tiff: tmpt00.tiff
	@${LN} tmpt00.tiff tmp00.tiff
	@${RM} tmpt00.tiff

tmp.tiff: tmp00.tiff
	@${RM}  00-e00-f0000.tiff tmp-0000_gray.pgm 00-e01-f0000.tiff tmp-0001_gray.pgm 00-e02-f0000.tiff tmp-0002_gray.pgm
	@${BLEND} -o tmp.tiff tmp00.tiff
	@${RM} tmp00.tiff
${DST}.tiff: tmp.tiff 
	@pfsin tmp.tiff \
	 | pfsclamp --min 1e-20 --max 1e+20 \
	 | pfswb -v -r 1.000 -g 1.000 -b 1.000 \
	 | pfsgamma -m 1.01359946400860e-04 -g 1.00 \
	 | pfsclamp --min 1e-20 --max 1e+20 \
	 | pfstmo_reinhard05 -v -b 0.00 -c 0.75 -l 0.95 \
	 | pfsoutimgmagick -s -b 16 ${DST}.tiff
	@${RM} tmp.tiff
	@${CONV} ${DST}.tiff \
		+matte +repage  \
		-compress ${IMCOMP} ${DST}.tiff
