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

Download this file

341 lines (274 with data), 8.0 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package Panotools::Script;
=head1 NAME
Panotools::Script - Panorama Tools scripting
=head1 SYNOPSIS
Read, write and manipulate hugin 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::Option;
use Panotools::Script::Line::Image;
use Panotools::Script::Line::ImageMetadata;
use Panotools::Script::Line::Output;
use Panotools::Script::Line::Control;
use Panotools::Script::Line::ControlMorph;
use Panotools::Script::Line::Variable;
use File::Temp qw/ tempdir /;
use File::Spec;
use Storable qw/ dclone /;
our $VERSION = 0.13;
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->{option} = new Panotools::Script::Line::Option;
$self->{variable} = new Panotools::Script::Line::Variable;
$self->{image} = [];
$self->{imagemetadata} = [];
$self->{output} = [];
$self->{control} = [];
$self->{controlmorph} = [];
$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)
{
$line =~ s/(\r|\n)//g;
$self->Mode->Parse ($line) if ($line =~ /^m /);
$self->Panorama->Parse ($line) if ($line =~ /^p /);
$self->Option->Parse ($line) if ($line =~ /^#hugin_/);
$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;
}
if ($line =~ /^#-hugin /)
{
# per-image metadata
my $imagemeta = new Panotools::Script::Line::ImageMetadata;
$imagemeta->Parse ($line);
push @{$self->ImageMetadata}, $imagemeta;
}
}
$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 $index (0 .. (scalar (@{$self->Image}) - 1))
{
print FILE $self->ImageMetadata->[$index]->Assemble if defined ($self->ImageMetadata->[$index]);
print FILE $self->Image->[$index]->Assemble if defined ($self->Image->[$index]);
}
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# option lines\n";
print FILE $self->Option->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
Clone a script object
$clone = $p->Clone;
=cut
sub Clone
{
my $self = shift;
dclone ($self);
}
=pod
Access various sections of the scriptfile:
$p->Mode; # a L<Panotools::Script::Line::Mode> object
$p->Panorama; # a L<Panotools::Script::Line::Panorama> object
$p->Variable; # a L<Panotools::Script::Line::Variable> object
=cut
sub Mode
{
my $self = shift;
$self->{mode};
}
sub Panorama
{
my $self = shift;
$self->{panorama};
}
sub Option
{
my $self = shift;
$self->{option};
}
sub Variable
{
my $self = shift;
$self->{variable};
}
=pod
$p->Image; # an array of L<Panotools::Script::Line::Image> objects
$p->Output; # an array of L<Panotools::Script::Line::Output> objects
$p->Control; # an array of L<Panotools::Script::Line::Control> objects
$p->ControlMorph; # an array of L<Panotools::Script::Line::ControlMorph> objects
=cut
sub Image
{
my $self = shift;
$self->{image};
}
sub ImageMetadata
{
my $self = shift;
$self->{imagemetadata};
}
sub Output
{
my $self = shift;
$self->{output};
}
sub Control
{
my $self = shift;
$self->{control};
}
sub ControlMorph
{
my $self = shift;
$self->{controlmorph};
}
=pod
Rotate transform all the images in a project, angles in degrees:
$p->Transform ($roll, $pitch, $yaw);
=cut
sub Transform
{
my $self = shift;
my ($roll, $pitch, $yaw) = @_;
for my $image (@{$self->Image})
{
$image->Transform ($roll, $pitch, $yaw);
}
$self->Image2Output;
}
=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};
}
}
}
}
1;

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks