#!/usr/bin/perl -w

# HDR panorama automator
# (c) 2009 daduke

# to install Photomatix using wine, do:
# wget http://kegel.com/wine/winetricks
# sh winetricks dotnet11 corefonts
# wine /path/to/PhotomatixPro303.exe

use strict;
use File::Basename;

my $PHOTOMATIX = 1;	#1 for PhotomatixPro, 0 for qtpfsgui

my @pix = `\\ls -1 *.jpg 2>/dev/null`;
my @dirs;

die "no tone mapping algorithm given! Choose detail or compressor.\n\n" if (!$ARGV[0]);
my $algo = $ARGV[0];
die "unknown mapping algorithm given! Choose detail or compressor. Usually compressor.\n\n" if ($algo ne 'detail' && $algo ne 'compressor');
die "no files found!\n\n" if (!scalar @pix);
die "number of files not multiple of 3, this cannot be an HDR panorama!\n\n" if (scalar @pix % 3);

if (scalar @pix == 3) {	#just one HDR shot
	print "\naligning HDR images, preserving EXIF data...\n";
	mkdir "aligned";
	my @hdrPix = `\\ls -1 *.jpg 2>/dev/null`;
	die "no pictures found!\n\n" if (!scalar @hdrPix);
	chomp @hdrPix;
	my $hdrPix = join ' ', @hdrPix;
	print "aligning pictures starting at ".$hdrPix[0]."...\n";
	system("/usr/bin/align_image_stack -a temp $hdrPix >/dev/null");

	print "converting and recovering EXIF data\n";
	my $n = 0;
	foreach my $file (@hdrPix) {
		my $basename = basename("$file", ".jpg");
		my $tempfile = "temp".substr("0000".$n, -4);

		system("convert -quality 0.99 $tempfile.tif aligned-$basename.jpg");
		system("exiftool -TagsFromFile $file -all:all aligned-$basename.jpg >/dev/null");
		unlink "$tempfile.tif";
		unlink "aligned-$basename.jpg_original";

		$n++;
	}

	mkdir "unaligned";
	foreach my $pic (@hdrPix) {
		system("mv $pic unaligned/");
	}

	my $exifSource = `\\ls -1 unaligned/*.jpg | head -1`;
	chomp $exifSource;

	system("mv aligned*.jpg aligned/");
	if ($PHOTOMATIX) {
		my @alignedPix = `\\ls -1 aligned/*.jpg 2>/dev/null`;
		chomp @alignedPix;
		my $alignedPix = join ' ', @alignedPix;
		print "\nstarting Photomatix to create HDR..\n\n";
#		system("wine ~/.wine/dosdevices/c\\:/Programme/Photomatix/PhotomatixCL.exe -3 -g 1 -t2 $alignedPix >/dev/null 2>&1");
#		system("exiftool -TagsFromFile $exifSource -all:all Result.jpg >/dev/null");
		if ($algo eq 'compressor') {
			system("wine ~/.wine/dosdevices/c\\:/Programme/Photomatix/PhotomatixCL.exe -3 -g 1 -t2 -d . -n 2 -q 1 -h remove $alignedPix >/dev/null 2>&1");
			system("exiftool -TagsFromFile $exifSource -all:all Set01Compressor.jpg >/dev/null");
		} else {
			system("wine ~/.wine/dosdevices/c\\:/Programme/Photomatix/PhotomatixCL.exe -3 -g 1 -t1 -d . -n 2 -q 1 -h remove $alignedPix >/dev/null 2>&1");
			system("exiftool -TagsFromFile $exifSource -all:all Set01Enhancer.jpg >/dev/null");
		}
		system("rm *.jpg_original");
		my $basename = basename("$exifSource", ".jpg");
#		system("mv Result.jpg $basename-hdr.jpg");
		if ($algo eq 'compressor') {
			system("mv Set01Compressor.jpg $basename-hdr.jpg");
		} else {
			system("mv Set01Enhancer.jpg $basename-hdr.jpg");
		}
	} else {
		print "creating HDR\n";
		system("qtpfsgui aligned/aligned-*.jpg -t reinhard05 -p brightness=-12:chroma=1:lightness=0 -s hdr.exr -o hdr.jpg");
	}
} elsif (!-d "hdr01") {	#HDR panorama
	print "creating HDR directories, moving file triplets in there...\n\n";
	my $dirNum = 1;
	while (scalar @pix) {
		my $targetDir = "hdr".substr("0".$dirNum++,-2);
		mkdir $targetDir;
		for my $pic (1..3) {
			my $pic = shift @pix;
			chomp $pic;
			system("mv $pic $targetDir");
		}
		push @dirs, $targetDir;
	}

	foreach my $dir (sort @dirs) {
		chdir $dir;
		system("hdr_create.pl $algo");	#call myself for single HDR pic in each folder
		system("mv *-hdr.jpg ../");
		chdir "..";
	}

	my $middle = $dirs[(scalar @dirs)/2];
	chomp $middle;
	my $exifSource = `\\ls -1 $middle/unaligned/*.jpg | head -1`;
	chomp $exifSource;

	print "invoking autopano for control points...\n";
	system("autopano-c-complete.sh -s 1500 -o pan.pto *-hdr.jpg");
	print "\nlaunching hugin...\n";
	system("hugin pan.pto");
	unlink "pan*.tif_original";
	my $basename = basename("$exifSource", ".jpg");
	system("mv pan*.tif $basename-hdrpan.tif");
	system("convert -quality 0.99 $basename-hdrpan.tif $basename-hdrpan.jpg");
	system("exiftool -TagsFromFile $exifSource -all:all $basename-hdrpan.jpg >/dev/null");
	unlink "$basename-hdrpan.tif";
} else {
	print "this directory has already been tampered with, bailing out...\n";
}
