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