Help save net neutrality! Learn more.
Close

[04f7a6]: / lib / Panotools / Script.pm  Maximize  Restore  History

Download this file

265 lines (215 with data), 6.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package Panotools::Script;
=head1 NAME
Panotools::Script - Panorama Tools scripting
=head1 SYNOPSIS
Read, manipulate and write Panorama Tools script files.
=head1 DESCRIPTION
Panorama Tools script files are used by several tools, including PTStitcher,
PTOptimizer, autooptimiser, nona and PTmender.
There are GUI tools to help create them: hugin, PTGui, PTAssembler,
autopano-sift and PTMac.
=cut
use strict;
use warnings;
use Panotools::Script::Line::Mode;
use Panotools::Script::Line::Panorama;
use Panotools::Script::Line::Image;
use Panotools::Script::Line::Output;
use Panotools::Script::Line::Control;
use Panotools::Script::Line::ControlMorph;
use Panotools::Script::Line::Variable;
use File::Temp qw/ tempfile /;
use File::Spec;
our $VERSION = 0.01;
our $CLEANUP = 1;
$CLEANUP = 0 if defined $ENV{DEBUG};
=head1 USAGE
my $p = new Panotools::Script;
=cut
sub new
{
my $class = shift;
$class = ref $class || $class;
my $self = bless {}, $class;
$self->_defaults;
return $self;
}
sub _defaults
{
my $self = shift;
$self->{mode} = new Panotools::Script::Line::Mode;
$self->{panorama} = new Panotools::Script::Line::Panorama;
$self->{variable} = new Panotools::Script::Line::Variable;
$self->{image} = [];
$self->{output} = [];
$self->{control} = [];
$self->{controlmorph} = [];
$self->{stitcher} = 'PTmender'; # nona, PTmender, PTStitcher
$self->{optimiser} = 'PTOptimizer'; # PTOptimizer, autooptimiser
$self->{basedir} = File::Spec->rel2abs (File::Spec->curdir);
}
=pod
$p->Read ('/path/to/script.txt');
=cut
sub Read
{
my $self = shift;
my $path = shift || return 0;
open FILE, "<", $path or die "cannot read-open $path";
my @raw = <FILE>;
close FILE;
my ($volume, $directories, $file) = File::Spec->splitpath ($path);
$self->{basedir} = File::Spec->catpath ($volume, $directories, '');
$self->{basedir} = File::Spec->rel2abs ($self->{basedir});
for my $line (@raw)
{
$self->{mode}->Parse ($line) if ($line =~ /^m /);
$self->{panorama}->Parse ($line) if ($line =~ /^p /);
$self->{variable}->Parse ($line) if ($line =~ /^v /);
if ($line =~ /^i /)
{
my $image = new Panotools::Script::Line::Image;
$image->Parse ($line);
push @{$self->{image}}, $image;
}
if ($line =~ /^o /)
{
my $output = new Panotools::Script::Line::Output;
$output->Parse ($line);
push @{$self->{output}}, $output;
}
if ($line =~ /^c /)
{
my $control = new Panotools::Script::Line::Control;
$control->Parse ($line);
push @{$self->{control}}, $control;
}
if ($line =~ /^C /)
{
my $controlmorph = new Panotools::Script::Line::ControlMorph;
$controlmorph->Parse ($line);
push @{$self->{controlmorph}}, $controlmorph;
}
}
$self->Output2Image;
return 1;
}
=pod
$p->Write ('/path/to/script.txt');
File paths in a Panorama Tools script file are generally relative to the
directory containing the script. Modify this or otherwise prefix the filenames
by supplying an optional second argument:
$p->Write ('/path/to/script.txt', '../path/to/prefix/tofiles');
=cut
sub Write
{
my $self = shift;
my $path = shift || return 0;
my $vector = shift || '';
open FILE, ">", $path or die "cannot write-open $path";
print FILE "# Created by ". (ref $self) ." $VERSION\n\n";
print FILE $self->{panorama}->Assemble;
print FILE $self->{mode}->Assemble;
print FILE "\n# Image lines\n";
for my $image (@{$self->{image}})
{
print FILE $image->Assemble ($vector);
}
print FILE "\n# Variable lines\n";
print FILE $self->{variable}->Assemble;
print FILE "\n# Control point lines\n";
for my $control (@{$self->{control}})
{
print FILE $control->Assemble;
}
for my $controlmorph (@{$self->{controlmorph}})
{
print FILE $controlmorph->Assemble;
}
print FILE "\n*\n";
print FILE "\n# Output image lines\n";
for my $output (@{$self->{output}})
{
print FILE $output->Assemble ($vector);
}
close FILE;
}
=pod
Optimise image parameters in a project:
$p->Optimise;
=cut
sub Optimise
{
my $self = shift;
my ($fh, $tempfile) = tempfile (SUFFIX => '.txt', UNLINK => $CLEANUP);
$self->Write ($tempfile);
`$self->{optimiser} $tempfile`;
return 0 unless ($? == 0);
my $try = new Panotools::Script;
$try->Read ($tempfile) || return 0;
$self->{output} = $try->{output};
$self->Output2Image;
return 1;
}
=pod
'o' output lines are generated by PTOptimizer and contain stitching parameters
for each input image.
'i' image lines provide parameters for optimisation as well as stitching.
Update the 'image' lines based on 'output' lines and vice-versa like so:
$p->Output2Image;
$p->Image2Output;
=cut
sub Output2Image
{
my $self = shift;
for my $index (0 .. (@{$self->{output}} - 1))
{
for my $entry (keys %{$self->{output}->[$index]})
{
$self->{image}->[$index] = new Panotools::Script::Line::Image unless (defined $self->{image}->[$index]);
$self->{image}->[$index]->{$entry} = $self->{output}->[$index]->{$entry}
unless (defined $self->{image}->[$index]->{$entry} and $self->{image}->[$index]->{$entry} =~ /=/);
}
}
}
sub Image2Output
{
my $self = shift;
for my $index (0 .. (@{$self->{image}} - 1))
{
for my $entry (keys %{$self->{image}->[$index]})
{
$self->{output}->[$index] = new Panotools::Script::Line::Output unless (defined $self->{output}->[$index]);
unless ($self->{image}->[$index]->{$entry} =~ /=/)
{
$self->{output}->[$index]->{$entry} = $self->{image}->[$index]->{$entry};
}
else
{
my $base = $self->{image}->[$index]->{$entry};
$base =~ s/=//;
$self->{output}->[$index]->{$entry} = $self->{image}->[$base]->{$entry};
}
}
}
}
=pod
Stitch a project:
$p->Stitch ('/path/to/output.jpg');
=cut
sub Stitch
{
my $self = shift;
my $outfile = shift;
my ($fh, $tempfile) = tempfile (SUFFIX => '.txt', UNLINK => $CLEANUP);
$self->Image2Output;
my $vector = File::Spec->abs2rel ($self->{basedir}, File::Spec->tmpdir);
$self->Write ($tempfile, $vector);
my $cwd = File::Spec->curdir;
chdir (File::Spec->tmpdir);
`$self->{stitcher} $tempfile -o $outfile`;
chdir ($cwd);
return 0 unless ($? == 0);
return 1;
}
1;