lib/Panotools/Photos.pm

filter: Statementstmt 82.1% 120 / 146 Branchbran 56.4% 35 / 62 Conditioncond 33.3% 15 / 45 MC/DCmcdc 12.5% 3 / 24 Subroutinesub 83.3% 15 / 18 Podpod 92.3% 12 / 13 totaltotal 64.9% 200 / 308 CC 56 SCAR 30.6
CC
56
Cov
67%
Rotation
90.0
Eev
56.0
FocalLength
12.0
CRAP 21.4
« lib/Panotools/Script/Line/Panorama.pm ↑ Summary lib/Panotools/Script/Line/Mode.pm »
1 package Panotools::Photos;
2  
3 =head1 NAME
4  
5 Panotools::Photos - Photo sets
6  
7 =head1 SYNOPSIS
8  
9 Query sets of photos
10  
11 =head1 DESCRIPTION
12  
13 A collection of photos has possibilities, it could be one or more panoramas or a
14 bracketed set. This module provides some methods for describing groups of
15 photos based on available metadata
16  
17 =cut
18  
19 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Image::ExifTool;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 =head1 USAGE
26  
27 Create a new object like so:
28  
29 my $photos = new Panotools::Photos;
30  
31 Alternatively supply some filenames:
32  
33 my $photos = new Panotools::Photos ('DSC_0001.JPG', 'DSC_0002.JPG');
34  
35 =over
36  
37 =cut
38  
39 sub new
40 14 ▶{
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
41 14 my $class = shift;
42 14 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0140
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
43 14 my $self = bless [], $class;
44 14 $self->Paths (@_);
45 14 return $self;
46 }
47  
48 =item Paths
49  
50 Add to or get the list of image filenames:
51  
52 $photos->Paths ('DSC_0003.JPG', 'DSC_0004.JPG');
53 my @paths = $photos->Paths;
54  
55 =cut
56  
57 sub Paths
58 17 ▶{
Subroutine Paths: called CC=2 SCAR=6.9
Pod documented
59 17 my $self = shift;
60 17 for my $path (@_)
61 {
62 6 push @{$self}, {path => $path, exif => Image::ExifTool::ImageInfo ($path)};
63 }
64 17 return map ($_->{path}, @{$self});
65 }
66  
67 =item Stub
68  
69 Construct a stub filename from the names of the first and last images in the
70 list.
71  
72 my $stub = $photos->Stub;
73  
74 e.g. DSC_0001.JPG DSC_0002.JPG DSC_0003.JPG -> DSC_0001-DSC_0003
75  
76 =cut
77  
78 sub Stub
79 1 ▶{
Subroutine Stub: called CC=1 SCAR=0.0
Pod documented
80 1 my $self = shift;
81 1 my $path_a = $self->[0]->{path};
82 1 my $path_b = $self->[-1]->{path};
83 # strip any suffixes
84 1 $path_a =~ s/\.[[:alnum:]]+$//;
85 1 $path_b =~ s/\.[[:alnum:]]+$//;
86 # strip all but filename
87 1 $path_b =~ s/.*[\/\\]//;
88 1 return $path_a .'-'. $path_b;
89 }
90  
91 =item Bracketed
92  
93 Query to discover if this is a likely bracketed set. i.e. is the total number
94 of photos divisible by the number of different exposures:
95  
96 &do_stuff if ($photos->Bracketed);
97  
98 =cut
99  
100 sub Bracketed
101 10 ▶{
Subroutine Bracketed: called CC=8 SCAR=20.8
Pod documented
102 10 my $self = shift;
103 # bracketed photos are not shot on 'Auto Exposure'
104 10 for my $index (0 .. scalar @{$self} -1)
105 {
106 80 ▶ next unless defined $self->[$index]->{exif}->{ExposureMode};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[$index]{"exif"}{"ExposureMode"} 79 1 80
107 1 ▶ return 0 if $self->[$index]->{exif}->{ExposureMode} eq 'Auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->[$index]{"exif"}{"ExposureMode"} eq "Auto" 1 0 1
108 }
109 9 my $brackets = scalar (@{$self->Speeds});
110 # require equal numbers of each exposure level
111 9 ▶ return 0 if (scalar (@{$self}) % $brackets);
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@{$self;}) % $brackets 2 7 9
112 # require more than one exposure level
113 7 ▶ return 0 if ($brackets < 2);
Branch: 1 branch
BranchTrueFalseTotal
if $brackets < 2 0 7 7
114 # require bracketing order repeats: 1/50 1/100 1/200 1/50 1/100 1/200 etc...
115 7 for my $index ($brackets .. scalar @{$self} -1)
116 {
117 34 ▶ return 0 unless $self->[$index]->{exif}->{ExposureTime} eq $self->[$index - $brackets]->{exif}->{ExposureTime};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[$index]{"exif"}{"ExposureTime"} eq $$self[$index - $brackets]{"exif"}{"ExposureTime"} 4 30 34
118 }
119  
120 3 return 1;
121 }
122  
123 =item Layered
124  
125 Query to discover if this is a layered set, i.e. there is a large exposure
126 difference in the set, but it isn't bracketed.
127  
128 &do_stuff if ($photos->Layered);
129  
130 By default the threshold is 4, e.g. exposures varying between 2 and 1/2
131 seconds indicate layers. Vary this threshold like so:
132  
133 &do_stuff if ($photos->Layered (2));
134  
135 =cut
136  
137 sub Layered
138 5 ▶{
Subroutine Layered: called CC=9 SCAR=23.1
Pod documented
139 5 my $self = shift;
140 5 ▶ my $factor = shift || 4;
Condition100%2/2
shift() || 4
l!l
23
MC/DC100%1/1
shift() || 4
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
141 5 ▶ return 0 if $self->Bracketed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->Bracketed 1 4 5
142 4 my $longest = $self->Speeds->[0];
143 4 my $shortest = $self->Speeds->[-1];
144 0 ▶ if ($longest =~ /^1\/([0-9]+)$/) {$longest = 1 / $1};
Branch: 1 branch
BranchTrueFalseTotal
if ($longest =~ m[^1/([0-9]+)$]) 0 4 4
145 4 ▶ if ($shortest =~ /^1\/([0-9]+)$/) {$shortest = 1 / $1};
Branch: 1 branch
BranchTrueFalseTotal
if ($shortest =~ m[^1/([0-9]+)$]) 4 0 4
146 4 ▶ return 0 unless $longest or $shortest;
Branch: 1 branch
BranchTrueFalseTotal
unless $longest or $shortest 0 4 4
Condition33%1/3
$longest or $shortest
l!l&&r!l&&!r
400
MC/DC0%0/2
$longest or $shortest
$longest $shortest
Truth table33%1/3
A or B
A= $longestB= $shortest
ABresult
1X1
011
000
147 4 ▶ return 0 if $shortest == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $shortest == 0 0 4 4
148 4 ▶ return 0 if $longest / $shortest < $factor;
Branch: 1 branch
BranchTrueFalseTotal
if $longest / $shortest < $factor 1 3 4
149 3 return 1;
150 }
151  
152 =item Speeds
153  
154 Get a list of exposure times sorted with longest exposure first
155  
156 @speeds = @{$photos->Speeds};
157  
158 =cut
159  
160 sub Speeds
161 20 ▶{
Subroutine Speeds: called CC=5 SCAR=17.3
Pod documented
162 20 my $self = shift;
163 20 my $speeds = {};
164 20 for my $image (@{$self})
165 {
166 175 ▶ my $et = $image->{exif}->{ShutterSpeedValue} || $image->{exif}->{ExposureTime} || $image->{exif}->{ShutterSpeed} || 0;
Condition37%3/8
$image->{'exif'}{'ShutterSpeedValue'} || $image->{'exif'}{'ExposureTime'} || $image->{'exif'}{'ShutterSpeed'} || 0
l!l
1750
$image->{"exif"}{"ShutterSpeedValue"} or $image->{"exif"}{"ExposureTime"} or $image->{"exif"}{"ShutterSpeed"}
l!l&&r!l&&!r
17500
$image->{"exif"}{"ShutterSpeedValue"} or $image->{"exif"}{"ExposureTime"}
l!l&&r!l&&!r
01750
MC/DC0%0/3
$image->{'exif'}{'ShutterSpeedValue'} || $image->{'exif'}{'ExposureTime'} || $image->{'exif'}{'ShutterSpeed'} || 0
$image->{"exif"}{"ShutterSpeedValue"} $image->{"exif"}{"ExposureTime"} $image->{"exif"}{"ShutterSpeed"}
Truth table25%1/4
A or B or C
A= $image->{"exif"}{"ShutterSpeedValue"}B= $image->{"exif"}{"ExposureTime"}C= $image->{"exif"}{"ShutterSpeed"}
ABCresult
1XX1
01X1
0011
0000
167 175 $speeds->{$et} = 'TRUE';
168 }
169 20 return [sort {_normalise ($b) <=> _normalise ($a)} keys %{$speeds}];
170 }
171  
172 sub _normalise
173 101 ▶{
Subroutine _normalise: called CC=2 SCAR=6.9
174 101 my $number = shift;
175 101 ▶ if ($number =~ /^1\/([0-9]+)$/) {$number = 1 / $1};
Branch: 1 branch
BranchTrueFalseTotal
if ($number =~ m[^1/([0-9]+)$]) 71 30 101
176 101 return $number;
177 }
178  
179 =item SplitInterval
180  
181 Given a set of photos, split it into a one or more sets by looking at the
182 variation of time interval between shots. e.g. typically the interval between
183 shots in a panorama varies by less than 15 seconds. A variation greater than
184 that indicates the start of the next panorama:
185  
186 my @sets = $photos->SplitInterval (15);
187  
188 Sets with an average interval greater than 4x this variation are not considered
189 panoramas at all and discarded.
190  
191 =cut
192  
193 sub SplitInterval
194 6 ▶{
Subroutine SplitInterval: called CC=8 SCAR=20.9
Pod documented
195 6 my $self = shift;
196 6 ▶ my $d_inc = shift || 15;
Condition50%1/2
shift() || 15
l!l
60
MC/DC0%0/1
shift() || 15
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
197 6 my $max_inc = $d_inc * 4;
198 6 my @groups;
199
200 6 my $group_tmp = new Panotools::Photos;
201 6 my $previous_time;
202 6 my $previous_inc = 0;
203 6 for my $image (@{$self})
204 {
205 26 ▶ my $datetime = $image->{exif}->{'DateTimeOriginal'} || $image->{exif}->{'FileModifyDate'};
Condition66%2/3
$image->{'exif'}{'DateTimeOriginal'} || $image->{'exif'}{'FileModifyDate'}
l!l&&r!l&&!r
2510
MC/DC0%0/2
$image->{'exif'}{'DateTimeOriginal'} || $image->{'exif'}{'FileModifyDate'}
$image->{'exif'}{'DateTimeOriginal'} $image->{'exif'}{'FileModifyDate'}
Truth table66%2/3
A || B
A= $image->{'exif'}{'DateTimeOriginal'}B= $image->{'exif'}{'FileModifyDate'}
ABresult
1X1
011
000
206 26 my $time_unix = Image::ExifTool::GetUnixTime ($datetime);
207 26 ▶ $previous_time = $time_unix unless (defined $previous_time);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $previous_time 6 20 26
208 26 my $inc = $time_unix - $previous_time;
209
210 26 ▶ if (($inc - $previous_inc) > $d_inc)
Branch: 1 branch
BranchTrueFalseTotal
if ($inc - $previous_inc > $d_inc) 5 21 26
211 {
212 5 ▶ push @groups, $group_tmp if ($group_tmp->AverageInterval < $max_inc);
Branch: 1 branch
BranchTrueFalseTotal
if $group_tmp->AverageInterval < $max_inc 5 0 5
213 5 $group_tmp = new Panotools::Photos;
214 }
215 26 push @{$group_tmp}, $image;
216
217 26 $previous_time = $time_unix;
218 26 $previous_inc = $inc;
219 }
220 6 ▶ push @groups, $group_tmp if ($group_tmp->AverageInterval < $max_inc);
Branch: 1 branch
BranchTrueFalseTotal
if $group_tmp->AverageInterval < $max_inc 6 0 6
221 6 return @groups;
222 }
223  
224 =item AverageInterval
225  
226 Get the average time between shots:
227  
228 $average = $photos->AverageInterval;
229  
230 =cut
231  
232 sub AverageInterval
233 11 ▶{
Subroutine AverageInterval: called CC=4 SCAR=14.5
Pod documented
234 11 my $self = shift;
235 11 ▶ return 0 unless (scalar @{$self} > 1);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self;} > 1 6 5 11
236  
237 5 ▶ my $start = $self->[0]->{exif}->{'DateTimeOriginal'} || $self->[0]->{exif}->{'FileModifyDate'};
Condition33%1/3
$self->[0]{'exif'}{'DateTimeOriginal'} || $self->[0]{'exif'}{'FileModifyDate'}
l!l&&r!l&&!r
500
MC/DC0%0/2
$self->[0]{'exif'}{'DateTimeOriginal'} || $self->[0]{'exif'}{'FileModifyDate'}
$self->[0]{'exif'}{'DateTimeOriginal'} $self->[0]{'exif'}{'FileModifyDate'}
Truth table33%1/3
A || B
A= $self->[0]{'exif'}{'DateTimeOriginal'}B= $self->[0]{'exif'}{'FileModifyDate'}
ABresult
1X1
011
000
238 5 ▶ my $end = $self->[-1]->{exif}->{'DateTimeOriginal'} || $self->[-1]->{exif}->{'FileModifyDate'};
Condition33%1/3
$self->[-1]{'exif'}{'DateTimeOriginal'} || $self->[-1]{'exif'}{'FileModifyDate'}
l!l&&r!l&&!r
500
MC/DC0%0/2
$self->[-1]{'exif'}{'DateTimeOriginal'} || $self->[-1]{'exif'}{'FileModifyDate'}
$self->[-1]{'exif'}{'DateTimeOriginal'} $self->[-1]{'exif'}{'FileModifyDate'}
Truth table33%1/3
A || B
A= $self->[-1]{'exif'}{'DateTimeOriginal'}B= $self->[-1]{'exif'}{'FileModifyDate'}
ABresult
1X1
011
000
239  
240 5 my $totaltime = Image::ExifTool::GetUnixTime ($end) - Image::ExifTool::GetUnixTime ($start);
241 5 return $totaltime / (scalar @{$self} -1);
242 }
243  
244 =item FOV FocalLength Rotation
245  
246 Get the Angle of View in degrees of the first photo:
247  
248 $photos->FOV;
249  
250 ..or any other photo (-1 is last):
251  
252 $photos->FOV (123);
253  
254 Returns undef if the FOV can't be calculated.
255  
256 =cut
257  
258 sub FOV
259 4 ▶{
Subroutine FOV: called CC=3 SCAR=11.0
Pod documented
260 4 my $self = shift;
261 4 my $index = 0;
262 4 ▶ $index = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 2 4
263 4 my $fov = $self->[$index]->{exif}->{'FOV'};
264 4 ▶ $fov =~ s/ .*$// if defined $fov;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fov 3 1 4
265 4 return $fov;
266 }
267  
268 sub FocalLength
269 0 ▶{
Subroutine FocalLength: not called CC=3 SCAR=24.8
Pod documented
270 0 my $self = shift;
271 0 my $index = 0;
272 0 ▶ $index = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
273 0 my $fl = $self->[$index]->{exif}->{'FocalLengthIn35mmFormat'};
274 0 ▶ $fl =~ s/ .*$// if defined $fl;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fl 0 0 0
275 0 return $fl;
276 }
277  
278 sub Rotation
279 0 ▶{
Subroutine Rotation: not called CC=9 SCAR=45.0
Pod documented
280 0 my $self = shift;
281 0 ▶ my $index = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
282 0 ▶ my $rotation = $self->[$index]->{exif}->{'Rotation'} || undef;
Condition0%0/2
$self->[$index]{'exif'}{'Rotation'} || undef
l!l
00
MC/DC0%0/1
$self->[$index]{'exif'}{'Rotation'} || undef
$self->[$index]{'exif'}{'Rotation'}
Truth table0%0/2
A
A= $self->[$index]{'exif'}{'Rotation'}
Aresult
11
00
283 0 ▶ return 0 unless $rotation;
Branch: 1 branch
BranchTrueFalseTotal
unless $rotation 0 0 0
284 0 ▶ return 0 if $rotation =~ /Mirror/;
Branch: 1 branch
BranchTrueFalseTotal
if $rotation =~ /Mirror/ 0 0 0
285 return 0 if ($self->[$index]->{exif}->{'ImageWidth'}
286 0 ▶ < $self->[$index]->{exif}->{'ImageHeight'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->[$index]{"exif"}{"ImageWidth"} < $self->[$index]{"exif"}{"ImageHeight"} 0 0 0
287 0 ▶ return 90 if $rotation =~ /Rotate 90 CW/;
Branch: 1 branch
BranchTrueFalseTotal
if $rotation =~ /Rotate 90 CW/ 0 0 0
288 0 ▶ return 180 if $rotation =~ /Rotate 180/;
Branch: 1 branch
BranchTrueFalseTotal
if $rotation =~ /Rotate 180/ 0 0 0
289 0 ▶ return -90 if $rotation =~ /Rotate 270 CW/;
Branch: 1 branch
BranchTrueFalseTotal
if $rotation =~ /Rotate 270 CW/ 0 0 0
290 0 return 0;
291  
292 # 1 => 'Horizontal (normal)',
293 # 3 => 'Rotate 180',
294 # 6 => 'Rotate 90 CW',
295 # 8 => 'Rotate 270 CW',
296  
297 # 2 => 'Mirror horizontal',
298 # 4 => 'Mirror vertical',
299 # 5 => 'Mirror horizontal and rotate 270 CW',
300 # 7 => 'Mirror horizontal and rotate 90 CW',
301  
302 }
303  
304 =item Eev
305  
306 Get an EV value for a photo, this will be guessed from partial EXIF data:
307  
308 $photos->Eev ($index);
309  
310 =cut
311  
312 sub Eev
313 0 ▶{
Subroutine Eev: not called CC=7 SCAR=40.3
Pod documented
314 0 my $self = shift;
315 0 ▶ my $index = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
316 0 my $exif = $self->[$index]->{exif};
317 0 ▶ my $aperture = $exif->{Aperture} || 1.0;
Condition0%0/2
$exif->{'Aperture'} || 1.0
l!l
00
MC/DC0%0/1
$exif->{'Aperture'} || 1.0
$exif->{'Aperture'}
Truth table0%0/2
A
A= $exif->{'Aperture'}
Aresult
11
00
318 0 ▶ my $et = $exif->{ExposureTime} || $exif->{ShutterSpeed} || 1.0;
Condition0%0/5
$exif->{'ExposureTime'} || $exif->{'ShutterSpeed'} || 1.0
l!l
00
$exif->{"ExposureTime"} or $exif->{"ShutterSpeed"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$exif->{'ExposureTime'} || $exif->{'ShutterSpeed'} || 1.0
$exif->{"ExposureTime"} $exif->{"ShutterSpeed"}
Truth table0%0/3
A or B
A= $exif->{"ExposureTime"}B= $exif->{"ShutterSpeed"}
ABresult
1X1
011
000
319 0 ▶ if ($et =~ /^1\/([0-9]+)$/) {$et = 1 / $1};
Branch: 1 branch
BranchTrueFalseTotal
if ($et =~ m[^1/([0-9]+)$]) 0 0 0
320 0 ▶ my $iso = $exif->{ISO} || 100;
Condition0%0/2
$exif->{'ISO'} || 100
l!l
00
MC/DC0%0/1
$exif->{'ISO'} || 100
$exif->{'ISO'}
Truth table0%0/2
A
A= $exif->{'ISO'}
Aresult
11
00
321 # (A light value of 0 is defined as f/1.0 at 1 second with ISO 100)
322 0 return sprintf ('%.3f', (2*log ($aperture) - log($et) - log($iso/100)) / log(2));
323 }
324  
325 =item AverageRGB
326  
327 Find the mean average colour balance for all photos:
328  
329 ($red, $green, $blue) = $photos->AverageRGB;
330  
331 =back
332  
333 =cut
334  
335 sub AverageRGB
336 3 ▶{
Subroutine AverageRGB: called CC=6 SCAR=17.9
Pod documented
337 3 my $self = shift;
338 3 my $RedBalance = 0;
339 3 my $GreenBalance = 0;
340 3 my $BlueBalance = 0;
341 3 my $count = 0;
342 3 for my $image (@{$self})
343 {
344 24 ▶ next unless ($image->{exif}->{'RedBalance'} and $image->{exif}->{'BlueBalance'});
Branch: 1 branch
BranchTrueFalseTotal
unless $image->{"exif"}{"RedBalance"} and $image->{"exif"}{"BlueBalance"} 22 2 24
Condition100%3/3
$image->{"exif"}{"RedBalance"} and $image->{"exif"}{"BlueBalance"}
!ll&&!rl&&r
2022
MC/DC100%2/2
$image->{"exif"}{"RedBalance"} and $image->{"exif"}{"BlueBalance"}
$image->{"exif"}{"RedBalance"} $image->{"exif"}{"BlueBalance"}
Truth table100%3/3
A and B
A= $image->{"exif"}{"RedBalance"}B= $image->{"exif"}{"BlueBalance"}
ABresult
0X0
100
111
345 2 $RedBalance += $image->{exif}->{'RedBalance'};
346 2 ▶ $GreenBalance += $image->{exif}->{'GreenBalance'} if defined $image->{exif}->{'GreenBalance'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $image->{"exif"}{"GreenBalance"} 2 0 2
347 2 $BlueBalance += $image->{exif}->{'BlueBalance'};
348 2 $count++;
349 }
350 3 ▶ return (1,1,1) unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 2 1 3
351 1 return ($RedBalance / $count, $GreenBalance / $count, $BlueBalance / $count);
352 }
353  
354 1 1;
« lib/Panotools/Script/Line/Panorama.pm ↑ Summary lib/Panotools/Script/Line/Mode.pm »