#!/usr/bin/perl

use strict;
use warnings;

use Panotools::Script;
use File::Spec;

my @images = @ARGV;
die "usage\n  $0 front.tif right.tif back.tif left.tif up.tif down.tif equirectangular.tif\n" unless scalar @images == 7;

my $out = File::Spec->rel2abs ($ARGV[6]);

for my $index (0 .. 5)
{
    my ($volume, $directories, $file) = File::Spec->splitpath ($images[$index]);
    my $basedir = File::Spec->catpath ($volume, $directories, '');
    $images[$index] = $file;
    chdir $basedir;
}

my $pano = new Panotools::Script;

$pano->Panorama->Set (v => 360, f => 2, u => 0, w => 4000, h => 2000, n => '"TIF"');

$pano->Image->[0] = new Panotools::Script::Line::Image;
$pano->Image->[0]->Set (v => 90, r => 0, p => 0, y => 0, n => "\"$images[0]\"");

$pano->Image->[1] = new Panotools::Script::Line::Image;
$pano->Image->[1]->Set (v => 90, r => 0, p => 0, y => 90, n => "\"$images[1]\"");

$pano->Image->[2] = new Panotools::Script::Line::Image;
$pano->Image->[2]->Set (v => 90, r => 0, p => 0, y => 180, n => "\"$images[2]\"");

$pano->Image->[3] = new Panotools::Script::Line::Image;
$pano->Image->[3]->Set (v => 90, r => 0, p => 0, y => 270, n => "\"$images[3]\"");

$pano->Image->[4] = new Panotools::Script::Line::Image;
$pano->Image->[4]->Set (v => 90, r => 0, p => 90, y => 0, n => "\"$images[4]\"");

$pano->Image->[5] = new Panotools::Script::Line::Image;
$pano->Image->[5]->Set (v => 90, r => 0, p => -90, y => 0, n => "\"$images[5]\"");

$pano->Stitch ($out);

__END__

=head1 NAME

cubic2erect - Utility to convert six cube faces to equirectangular

=head1 Synopsis

  cubic2erect front.tif right.tif back.tif left.tif up.tif down.tif equirectangular.tif

=head1 DESCRIPTION

`cubic2erect' is a command-line utility program for converting six cube faces
to a single TIFF format equirectangular file suitable for viewing with
`panoglview'.

Input files must be JPEG, PPM or TIFF format, output is in PNG format.

=head1 Calling syntax

  cubic2erect <front> <right> <back> <left> <up> <down> <TIFF-file>

=head1 License

This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=head1 See Also

L<http://panotools.sourceforge.net/>

=head1 Author

Bruno Postle, E<lt>bruno (at) postle.netE<gt>

=cut
