#!/usr/bin/perl

use strict;
use warnings;
use File::Spec;

my @files = @ARGV;
@files = map File::Spec->rel2abs ($_), @files;

for my $pto (@files)
{
    my $stub = $pto;
    $stub =~ s/\.pto$//i;

    use Panotools::Script;

    my $p = new Panotools::Script;
    $p->Read ($pto);
    if (scalar @{$p->{image}} == 1)
    {
        $p->Panorama->Set (n => '"TIFF c:DEFLATE"');
    }
    else
    {
        $p->Panorama->Set (n => '"TIFF_m c:DEFLATE"');
    }
    my $pto_temp = "$stub.pto2tiff.$$.pto";
    $p->Write ($pto_temp);
    system ('nona-mask', '-o', $stub, $pto_temp);
    unlink $pto_temp;

    if (scalar @{$p->{image}} > 1)
    {
        my @temp_files;
        for my $index (0 .. scalar @{$p->{image}} - 1)
        {
             push @temp_files, ($stub . sprintf ('%.4d', $index) .'.tif');
        }

        my @args;
        push @args ,'-w' if $p->Panorama->{v} == 360;

        system ('enblend-mask', @args, '-o', "$stub.tif", @temp_files);
    }
    system ('convert', "$stub.tif", "$stub.jpg");

    print STDERR "Done: $stub.tif\n";
}

__END__

=head1 NAME

pto2tiff - Batch process hugin project files

=head1 Synopsis

  pto2tiff project1.pto [project2.pto ...]

=head1 DESCRIPTION

The nona stitcher can be used for batch stitching hugin pto projects.  This
tool is a simple wrapper that forces the output format into a suitable input
format for enblend, it then runs enblend (actually enblend-mask) with
appropriate arguments to generate a single blended output.

L<http://hugin.sourceforge.net/>
L<http://enblend.sourceforge.net/>

=head1 License

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

=head1 See Also

L<perl>, L<Panotools::Script>

=head1 Author

Bruno Postle <bruno AT postle.net>

