Synchronize.pm

filter: Statementstmt 96.4% 55 / 57 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 19 / 19 Podpod n/a totaltotal 97.3% 74 / 76 CC 1 SCAR 0.2
CC
1
Cov
96%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Synchronize/Timestamp.pm ↑ Summary
1 package Image::Synchronize;
2
3 1 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 =head1 NAME
6
7 Image::Synchronize - a module for synchronizing filesystem
8 modification timestamps of images, movies, and related files.
9
10 =head1 SYNOPSIS
11
12 use Image::Synchronize;
13
14 $ims = Image::Synchronize->new(%options);
15 $ims->process(@pathpatterns);
16
17 =head1 DESCRIPTION
18
19 This module is the backend of L<imsync> and was not designed to be
20 used outside of that context.
21
22 =head1 SEE ALSO
23
24 See the documentation of L<imsync> for more details.
25
26 =head1 AUTHOR
27
28 Louis Strous, E<lt>imsync@quae.nl<gt>
29
30 =head1 COPYRIGHT AND LICENSE
31
32 Copyright (C) 2018-2025 by Louis Strous
33
34 This library is free software; you can redistribute it and/or modify
35 it under the same terms as Perl itself, either Perl version 5.26.2 or,
36 at your option, any later version of Perl 5 you may have available.
37
38 =cut
39
40 1 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 our @EXPORT_OK = qw(normalize_options);
42
43 1 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
44
45 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use File::Copy qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
48 copy
49 move
50 1 );
51 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use Image::ExifTool 10.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use Image::Synchronize::CameraOffsets;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use Image::Synchronize::GpsPositionCollection;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use Image::Synchronize::GroupedInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use Image::Synchronize::Logger qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
57 default_logger
58 log_error
59 log_message
60 log_warn
61 1 );
62 1 ▶use Image::Synchronize::ProgressBar;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use Path::Class qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
64 dir
65 file
66 1 );
67 1 ▶use Path::Iterator::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use POSIX qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
69 floor
70 1 );
71 1 ▶use Scalar::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
72 blessed
73 looks_like_number
74 1 );
75 1 ▶use Text::Glob qw(match_glob);
Subroutine BEGIN: called CC=1 SCAR=0.0
76 0 ▶use XML::Twig;
Subroutine BEGIN: called CC=1 SCAR=2.6
77 use YAML::Any qw(
78 Dump
79 DumpFile
80 LoadFile
81 );
82
83 BEGIN {
84 if ($^O eq 'MSWin32'
85 and (!$^V or $^V lt v5.33.5)
86 and eval 'require Win32::UTCFileTime')
87 {
88 import Win32::UTCFileTime;
89 }
90 }
91
92 # always use x.yyy version numbering, so that string comparison and
93 # numeric comparison give the same ordering, to avoid trouble due to
94 # different ways of interpreting version numbers.
95 our $VERSION = '2.015';
96
97 # TODO: check each folder for an .imsync-cameraoffsets.yaml file
98 # TODO: allow timezone specification on --time
99
100 my $CASE_TOLERANT;
101 my $PIRname; # for 'name' or 'iname' as appropriate for
102 # case-sensitiveness of the operating system
103 my $PIRnotname; # likewise for 'not_name' or 'not_iname'
104
105 # capture and log warnings and errors, so they end up in the log file
106 # as well as being printed to standard output
107 BEGIN {
108 $SIG{__WARN__} = sub {
109 log_warn( $_[0] );
110 };
111 $SIG{__DIE__} = sub {
112 log_error( Carp::longmess( $_[0] ) ) unless $^S;
113 };
114 $CASE_TOLERANT = File::Spec->case_tolerant();
115 $PIRname = $CASE_TOLERANT? 'iname': 'name';
116 $PIRnotname = 'not_' . $PIRname;
117 }
118
119 my @gps_location_tags = qw(GPSLatitude GPSLongitude GPSAltitude);
120 my %gps_location_tags = map { $_ => 1 } @gps_location_tags;
121
122 # The ExiftimeVersion is here temporarily. It is associated with a
123 # predecessor of the current application that was not released to the
124 # general public but was extensively used by the author. He wants to
125 # know if he's processing files using the current application that
126 # already contain tags embedded by the predecessor.
127 my @own_xmp_tags = qw(
128 CameraID
129 ImsyncVersion
130 TimeSource
131 ExiftimeVersion
132 );
133
134 my @all_tags;
135 {
136 my %tags = map { $_ => 1 }
137 (
138 'CreateDate',
139 'DateTimeOriginal',
140 'Duration',
141 'FileModifyDate',
142 'GPSAltitude',
143 'GPSAltitudeRef',
144 'GPSDateTime',
145 'GPSLatitude',
146 'GPSLatitudeRef',
147 'GPSLongitude',
148 'GPSLongitudeRef',
149 'ImageWidth',
150 'Make',
151 'MIMEType',
152 'Model',
153 'QuickTime:CreationDate',
154 'QuickTime:AndroidManufacturer',
155 'QuickTime:AndroidModel',
156 'SerialNumber',
157 map { "XMP:$_" } @own_xmp_tags,
158 );
159 @all_tags = sort keys %tags;
160 }
161
162 my %all_tags = map { $_ => 1 } @all_tags;
163
164 my %time_tags;
165
166 # non-UTC time tags
167 $time_tags{$_} = 1 foreach qw(FileModifyDate CreateDate DateTimeOriginal);
168
169 # UTC time tags
170 $time_tags{$_} = 0 foreach qw(GPSDateTime QuickTime:CreationDate);
171
172 =head1 METHODS
173
174 =head2 new
175
176 $ims = Image::Synchronize->new(%options);
177
178 Construct and return a new instance of the class. The following
179 C<%options> keys are recognized:
180
181 =over
182
183 =item logfile
184
185 Specifies the file to write the log messages to (in addition to
186 printing them to standard output). If not set, then I<imsync.log>
187 in the current working directory is used.
188
189 =back
190
191 =cut
192
193 sub new {
194 my ( $class, %options ) = @_;
195
196 my $self = bless {
197 backend => Image::ExifTool->new,
198 gps_offsets => Image::Synchronize::CameraOffsets->new(
199 log_callback => sub { log_message( 16, @_ ) }
200 ),
201 gps_positions => Image::Synchronize::GpsPositionCollection->new,
202 options => \%options,
203 }, $class;
204
205 # write XMP tags without padding.
206 $self->{backend}->Options( Compact => 1, );
207
208 return $self;
209 }
210
211 =head2 process
212
213 $ims->process(@arguments);
214
215 Processes the specified C<@arguments>, of which the elements are
216 interpreted as path name patterns matching files and/or directories.
217
218 Returns C<$ims>.
219
220 =cut
221
222 sub process {
223 my ( $self, @arguments ) = @_;
224 $self->set_working_directory->initialize_logging->report_program_and_options;
225
226 my $done = 0;
227 my $result;
228
229 if ( $self->option('restoreoriginals') ) {
230 $result = $self->restore_original_files(@arguments);
231 $done = 1;
232 }
233
234 if ( $self->option('removebackups') ) {
235 $result = $self->delete_backups(@arguments);
236 $done = 1;
237 }
238
239 my @files;
240 if ( not($done) or $self->option('removeourtags') ) {
241 log_message("Seeking files.\n");
242 @files = resolve_files( { recurse => $self->option('recurse') },
243 Path::Iterator::Rule->new->$PIRnotname('*_original')->file, @arguments );
244 log_message( 'Found ' . scalar(@files) . ' file(s).' . "\n" );
245
246 $self->process_follow(@files) # must happen before the first
247 # file-specific message gets printed
248 ->initialize_own_xmp_namespace; # must happen before using any
249 # Image::Exiftool functionality
250
251 if ( $self->option('removeourtags') ) {
252 $result = $self->remove_our_tags(@files);
253 $done = 1;
254 }
255 }
256
257 if ( not $done ) {
258 $self->inspect_files(@files);
259
260 # must happen before determining new values
261
262 $self->import_camera_offsets->process_user_camera_ids->process_user_times
263 ->process_user_locations->process_summer_wintertime->read_gpx_tracks;
264
265 $self->determine_new_values_for_all_files->modify_files->report->exportgpx(
266 $self->option('exportgpx') );
267
268 $self->export_camera_offsets if $self->option('modify');
269 }
270
271 log_message( "\nResults logged to file '"
272 . file( $self->{logfile} )->absolute
273 . "'.\n" );
274
275 # clean up the logging to file
276 default_logger()->clear_printer('file');
277 close $self->{logfh};
278
279 if ( ( $self->option('removebackups') and $self->option('clearlog') )
280 or $self->option('unsafe') )
281 {
282 print "Removing logfile.\n";
283 unlink( $self->{logfile} );
284 }
285
286 return 0;
287 }
288
289 # below here, specify subs in alphabetical order
290
291 # output a key-value pair only if the value is defined.
292 sub add_maybe {
293 if ( @_ == 2 ) {
294 my ( $key, $value ) = @_;
295 return ( $key => $value ) if defined $value;
296 }
297 elsif ( @_ == 1 ) {
298 my ($key) = @_;
299 return defined($key) ? $key : ();
300 }
301 return ();
302 }
303
304 # ($quotient, $remainder) = split_number($value, $divisor);
305 # $quotient = split_number($value, $divisor);
306 #
307 # Divides two numbers and returns their quotient and (in list mode)
308 # non-negative remainder.
309 sub split_number {
310 my ($value, $period) = @_;
311 $period = abs($period);
312 my $q = sprintf('%d', $value/$period)*$period;
313 my $r = $value - $q;
314 if ($r < 0) {
315 $r += $period;
316 $q -= $period;;
317 } elsif ($r >= $period) {
318 $r -= $period;
319 $q += $period;
320 }
321 return wantarray? ($q, $r): $q;
322 }
323
324 # $corrected_time = apply_camera_offset($time, $offset);
325 #
326 # Applies camera offset C<$offset> (in seconds) to a clone of
327 # C<$time>, which must be a Synchronize::Timestamp. Returns the
328 # clone.
329 sub apply_camera_offset {
330 my ( $t, $offset ) = @_;
331 $t = $t->clone; # don't modify object that argument refers to
332
333 # In v2.0.0, we processed as follows:
334 # - remove the timezone (if any) from $t
335 # - set the timezone offset of $t to $offset, which was a plain number
336 # - move $t to the local timezone
337
338 if (Image::Synchronize::Timestamp->istypeof($offset)) {
339 # the timezone offset has a time part and perhaps a timezone part
340 $offset = $offset->clone; # don't modify object that argument
341 # refers to
342 } else {
343 # deprecated: offset is a number; support for backward
344 # compatibility
345 $t->set_timezone_offset($offset)
346 ->adjust_to_local_timezone;
347 }
348
349 # If $offset has no timezone offset, then split its time part into
350 # two parts: (1) the whole number of hours nearest its value, which
351 # becomes the timezone part of $offset; (2) the remainder, which
352 # becomes the new time part of $offset.
353 #
354 # For example, if $offset is +02:11:03 then it gets changed to
355 # +0:11:03+02:00.
356 if (not $offset->has_timezone_offset) {
357 my ($q, $r) = split_number($offset->time_local, 3600);
358 if ($r >= 1800) { # the offset is nearer the next higher hour than
359 # the current hour
360 $r -= 3600;
361 $q += 3600;
362 }
363 $offset = Image::Synchronize::Timestamp
364 ->new($r)
365 ->adjust_timezone_offset($q)
366 ->adjust_nodate;
367 }
368
369 # Add the time part of $offset to $t and then adjust the timezone
370 # of $t to the timezone of $offset. For example, if $t is
371 # 12:40:00+02:00 and $offset is +00:05+03:00, then the result is
372 # 13:45:00+03:00.
373 $t += $offset->time_local;
374 $t->adjust_timezone_offset($offset->timezone_offset);
375
376 return $t;
377 }
378
379 # $backend = $ims->backend;
380 #
381 # Returns the L<Image::ExifTool> backend that is used to interact with
382 # the image files.
383 sub backend {
384 my ($self) = @_;
385 return $self->{backend};
386 }
387
388 # $base_camera_id = base_camera_id($camera_id)
389 #
390 # Returns the camera ID without the "always UTC" suffix "|U", if any.
391 sub base_camera_id {
392 my ($camera_id) = @_;
393
394 # Camera IDs that we construct are like Make|Model|SerialNumber or,
395 # if the file's CreateDate is always in UTC,
396 # Make|Model|SerialNumber|U. However, camera IDs that the user
397 # supplies through --cameraid can have any format, and perhaps we
398 # appended the "|U" suffix.
399 return $1 if defined($camera_id) and $camera_id =~ /^(.*?)\|U$/;
400 return $camera_id;
401 }
402
403 # $pattern = basename_pattern($path);
404 #
405 # Returns text identifying the structure of the base name of the
406 # C<$path>, suitable for use as a camera ID if no explicit camera ID is
407 # known for the file.
408 #
409 # The returned text consists of the base name of the C<$path>, omitting
410 # the last period and any following text, and replacing any sequence of
411 # digits with the length of that sequence.
412 sub basename_pattern {
413 my ($path) = @_;
414 my $name = file($path)->basename;
415 $name =~ s/\.[^.]+$//; # remove file extension
416 $name =~ s/(\d+)/length($1)/eg;
417 return $name;
418 }
419
420 my %camera_id_tags
421 = (
422 Make => [qw(Make QuickTime:AndroidManufacturer)],
423 Model => [qw(Model QuickTime:AndroidModel)],
424 SerialNumber => [qw(SerialNumber)],
425 );
426
427 # $camera_id = camera_id($info);
428 #
429 # returns the ID of the camera, based on information extracted from an
430 # image file through Image::ExifTool::ImageInfo. For some but not all
431 # types of camera, the camera ID includes a serial number that is
432 # unique to the camera.
433 sub camera_id {
434 my ( $info ) = @_;
435 my @values;
436 foreach my $maintag (qw(Make Model SerialNumber)) {
437 my $value;
438 foreach my $tagtry (@{$camera_id_tags{$maintag}}) {
439 my $v = $info->get(split /:/, $tagtry);
440 if (defined $v) {
441 $value = $v;
442 last;
443 }
444 }
445 push @values, $value // '';
446 }
447 if (not($values[0])
448 and $values[1]) {
449 # the manufacturer (the make) is not filled in but the model is.
450 # Assume that the first word of the model is the make.
451 ($values[0]) = $values[1] =~ /(\w+)/;
452 }
453 my $id = join( '|', @values );
454 if ($id =~ /^\|+$/) {
455 $id = undef;
456 } elsif ($info->get('supposedly_utc')) {
457 $id .= '|U';
458 }
459 return $id;
460 }
461
462 # $camera_id = $ims->camera_id_from_fallback($fallback_camera_id);
463 #
464 # Returns the regular camera ID of a camera that has a fallback camera
465 # ID equal to C<$fallback_camera_id>, or C<$fallback_camera_id> if
466 # there is no such camera.
467 sub camera_id_from_fallback {
468 my ( $self, $fallback_camera_id ) = @_;
469 my $r = $self->{fallback_to_camera_id}->{$fallback_camera_id};
470 if ( defined $r ) {
471 my @candidates =
472 sort { ( $r->{$b} <=> $r->{$a} ) or ( $a cmp $b ) } keys %{$r};
473 return $candidates[0];
474 } else {
475 return $fallback_camera_id;
476 }
477 }
478
479 # $camera_offsets_path = $ims->camera_offsets_path
480 #
481 # Returns the camera offsets path, which is taken from the
482 # C<offsetspath> option if defined. If C<offsetspath> is not defined,
483 # then uses C<'.imsync-cameraoffsets.yaml'> if that file exists in the
484 # current working directory or if the C<HOME> environment variable
485 # isn't set, and otherwise uses that file name in the directory
486 # defined by the C<HOME> environment variable.
487 sub camera_offsets_path {
488 my ( $self ) = @_;
489 my $path = $self->option('offsetspath');
490 if ( not defined $path ) {
491 $path = '.imsync-cameraoffsets.yaml';
492 if ( not(-e $path) and exists $ENV{HOME}) {
493 $path = file( $ENV{HOME}, $path );
494 }
495 }
496 return $path;
497 }
498
499 # $ims->cleanup_progressbar($progressbar);
500 #
501 # Cleans up and removes the specified $progressbar.
502 sub cleanup_progressbar {
503 my ( $self, $progressbar ) = @_;
504 $progressbar->done;
505
506 # Restore the original logger for logging to the screen, now that
507 # the progress bar has disappeared.
508 default_logger()->set_printer(
509 {
510 bitflags => ( $self->option('verbose') // 1 ),
511 action => sub { print @_; }
512 }
513 );
514 return $self;
515 }
516
517 # $prefix = common_prefix($separator, @values);
518 #
519 # Determines the common prefix of a collection of text values. The
520 # returned common prefix ends with the specified separator, unless the
521 # common prefix is empty (or the separator is empty).
522 #
523 # For example, if (':', 'foo:bar:fie', 'foo:bar:zoo') are passed as
524 # arguments, then 'foo:bar:' is returned.
525 sub common_prefix {
526 my ( $separator, @values ) = @_;
527 return '' unless @values;
528 my $value = shift @values;
529 my @prefix = split /($separator)/, $value;
530 my $shorter;
531 foreach $value (@values) {
532 my @next = split /($separator)/, $value;
533 for my $i ( 0 .. $#prefix ) {
534 if ( not( defined( $next[$i] ) ) or $next[$i] ne $prefix[$i] ) {
535 @prefix = @prefix[ 0 .. $i - 1 ];
536 $shorter = 1;
537 last;
538 }
539 }
540 }
541 pop @prefix if not($shorter) and $separator;
542 return '' unless @prefix;
543 my $result = join( '', @prefix );
544 return $result;
545 }
546
547 # $prefix = common_prefix_path(@paths);
548 #
549 # Determines the common prefix of a collection of file paths. The
550 # returned common prefix ends with a path name separator, unless the
551 # common prefix is empty. The returned path is in Unix style, with
552 # forward slashes as path separators.
553 #
554 # For example, if 'foo/bar/fie' and 'foo/bar/zoo' are passed as
555 # arguments, then 'foo/bar/' is returned.
556 sub common_prefix_path {
557 my @args = map { file($_)->as_foreign('Unix')->stringify } @_;
558 return common_prefix( '/', @_ );
559 }
560
561 # This is a callback function
562 sub convert_to_xmp {
563 my ( $tag, $in, $out ) = @_;
564 $out->{ 'XMP:' . $tag } = $in;
565 }
566
567 # $offset = deduce_camera_offset($camera_time, $target_time);
568 #
569 # deduce the camera offset from the C<$camera_time> and the
570 # C<$target_time>. The $target_time must have a timezone offset. The
571 # $camera_time may or may not have a timezone offset.
572 #
573 # The deduced camera offset has a time part and a timezone part. The
574 # time part says how much to add to the instant expressed by the
575 # camera time to get the instant expressed by the target time (if both
576 # are converted to the same timezone). The timezone part says how
577 # much to add to UTC to get the nominal timezone offset of the camera.
578 # The nominal timezone is the timezone that the camera's clock was
579 # intended to follow. The time part of the offset says how much the
580 # "true" camera timezone differed from the nominal one, perhaps due to
581 # misconfiguration or clock drift.
582 #
583 # If the camera time is 2020-07-30T08:22:30+02:00 and the target time
584 # is 2020-07-30T09:23:05+03:00 then the deduced camera offset is
585 # +00:00:35+02:00.
586 #
587 # TODO: what timezone should we assume for the target time if it has
588 # none, to get backward compatibility with versions of imsync
589 # predating the introduction of the timezone part of the camera
590 # offset?
591 #
592 # In the past the camera offset did not have a timezone part yet.
593 # Then the camera offset was added to the camera time (which was
594 # assumed to have no timezone offset) and the resulting timestamp was
595 # assumed to be given relative to the system's timezone for that
596 # instant.
597 #
598 # If the camera time was 2020-07-30T08:22:30 and the target time was
599 # 2020-07-30T09:23:05+03:00 (with the system's timezone offset
600 # UTC+03:00) then the deduced camera offset was +01:00:35.
601 sub deduce_camera_offset {
602 my ( $camera_time, $target_time ) = @_;
603
604 croak "Target time must have a timezone offset\n"
605 unless $target_time->has_timezone_offset;
606
607 my $d = Image::Synchronize::Timestamp->new($target_time - $camera_time)
608 ->adjust_nodate;
609
610 $d->adjust_timezone_offset($target_time->timezone_offset);
611 return $d;
612 }
613
614 # $ims->delete_backups(@file_patterns);
615 #
616 # Deletes any backup files matching the C<@file_patterns>. Backup
617 # files have a name that ends in '_original'.
618 sub delete_backups {
619 my ( $self, @arguments ) = @_;
620 my @files = resolve_files( { recurse => $self->option('recurse') },
621 Path::Iterator::Rule->new->$PIRname('*_original')->file, @arguments );
622
623 log_message( "Removing " . scalar(@files) . " backup file(s).\n" );
624 my $progressbar;
625 if ( scalar(@files) ) {
626 $progressbar = $self->setup_progressbar( scalar(@files), 'delete' );
627 foreach my $file (@files) {
628 unlink($file);
629 $progressbar->add;
630 }
631 $self->cleanup_progressbar($progressbar);
632 }
633 log_message("Done removing backup file(s).\n");
634 $self;
635 }
636
637 # @best_scorers = best_scorers(sub { ... }, @targets)
638 #
639 # Return the best-scoring items. The code reference, when called for
640 # a target, should return a numerical score. The highest score among
641 # the @targets is determined, and all elements of @targets that have
642 # that highest score are returned.
643 sub best_scorers {
644 my ($code, @targets) = @_;
645 my %score = map { $code->($_) } @targets;
646 my @order = sort { $score{$b} <=> $score{$a} } @targets;
647 my $bestscore = $score{ $order[0] };
648 return grep { $score{$_} == $bestscore } keys %score;
649 }
650
651 sub potential_donors {
652 my ($self, $file, $info) = @_;
653 my $number = $info->get('image_number');
654 my @targets;
655 if ( defined($number)
656 && defined( $self->{files_for_image_numbers}->{$number} ) ) {
657 my $target;
658 @targets = grep { $_ ne $file }
659 @{ $self->{files_for_image_numbers}->{$number} };
660 if (@targets > 1) {
661 # multiple files with the same number. Arrange them in
662 # descending order of priority; metadata that is missing from
663 # the current file will be copied from the first target file
664 # that has it.
665
666 # descending priority order:
667 # 1. current file name with '.yaml' or '.json' appended
668 # 2. greatest directory prefix in common with current file
669 # 3. metadata file
670 # 4. greatest basename pattern prefix in common with current file
671 # 5. lexicographic order
672 my $t1 = qr/^${file}\.(yaml|json)$/;
673 my $t3 = basename_pattern($file);
674 @targets = map { $_->[0] }
675 sort {
676 $b->[1] <=> $a->[1] # with .yaml suffix
677 or $b->[2] <=> $a->[2] # common directory prefix
678 or $b->[3] <=> $a->[3] # metadata file
679 or $b->[4] <=> $a->[4] # common base name pattern
680 or $a->[0] cmp $b->[0] # fallback: lexicographic order
681 }
682 map { [
683 $_,
684 ($_ =~ $t1)? 1: 0,
685 length_of_common_directory_prefix($_, $file),
686 ($self->{original_info}->{$_}->get('is_metadata') // 0),
687 length_of_common_prefix(basename_pattern($_), $t3)
688 ] } @targets;
689 }
690 }
691 return @targets;
692 }
693
694 # $ims->determine_new_values_for_all_files;
695 #
696 # Determines the proposed final values (target timestamp, location, and
697 # so on) for the files, as far as possible.
698 #
699 # Returns C<$ims>.
700 sub determine_new_values_for_all_files {
701 my ($self) = @_;
702
703 my @files = sort keys %{ $self->{original_info} };
704
705 my $progressbar = $self->setup_progressbar( scalar(@files), 'Determine' );
706
707 # Process the files
708 my @files_handle_by_number;
709 my $count_modified = 0;
710 my %count_needs_force;
711 foreach my $file (@files) {
712 my $info = $self->{original_info}->{$file};
713
714 if ( $self->has_useful_timestamp($file, $info) ) {
715
716 # remember which files with embedded timestamps have which image
717 # number
718 push @{ $self->{files_for_image_numbers}->{ $info->get('image_number') } }, $file
719 if defined $info->get('image_number');
720
721 my @potential_donors = $self->potential_donors($file, $info);
722 my $modification_type = $self->determine_new_values_for_file($file, \@potential_donors);
723
724 ++$count_modified if $modification_type == 1;
725
726 my $extra_info = $self->{extra_info}->{$file};
727 if (defined($extra_info)) {
728 my $f = $extra_info->get('min_force_for_change');
729 ++$count_needs_force{$f} if $f;
730 }
731 }
732 elsif ( defined $info->get('image_number') ) {
733
734 # mark but otherwise skip files that aren't image or movie files
735 # but that have a potential image number; we can hopefully copy
736 # the target time from another image or movie file with the same
737 # number later, so we must make sure to process those
738 push @files_handle_by_number, $file;
739 next;
740 }
741 else {
742 log_message(
743 8,
744 { name => $file },
745 sub {
746 <<EOD; } );
747
748 Processing '$file'.
749 No relevant embedded timestamps or image number found -- skipped.
750 EOD
751 }
752 $progressbar->add;
753 }
754
755 # now process files with a file number but no embedded timestamp;
756 # maybe we can copy the target timestamp from a file processed
757 # earlier that has the same image number
758 foreach my $file (@files_handle_by_number) {
759 my $info = $self->{original_info}->{$file};
760 my $number = $info->get('image_number');
761 if ( defined( $self->{files_for_image_numbers}->{$number} ) ) {
762 my @potential_donors = $self->potential_donors($file, $info);
763 $count_modified +=
764 ($self->determine_new_values_for_file( $file, \@potential_donors ) == 1);
765 my $extra_info = $self->{extra_info}->{$file};
766 if (defined($extra_info)) {
767 my $f = $extra_info->get('min_force_for_change');
768 ++$count_needs_force{$f} if $f;
769 }
770 }
771 else {
772 log_message(
773 8,
774 { name => $file },
775 sub {
776 <<EOD; } );
777
778 Processing '$file'.
779 No file from which to copy a target timestamp -- skipped.
780 EOD
781 }
782
783 $progressbar->add;
784 }
785
786 $self->cleanup_progressbar($progressbar);
787
788 log_message("$count_modified file(s) need modification.\n");
789 log_message($count_needs_force{1} . " file(s) need --force for modification.\n")
790 if $count_needs_force{1} && $self->option('force', 0) < 1;
791 log_message($count_needs_force{2} . " file(s) need --force --force for modification.\n")
792 if $count_needs_force{2} && $self->option('force', 0) < 2;
793 return $self;
794 }
795
796 sub get_from_targets {
797 my ($self, $tag, $target_files) = @_;
798 foreach my $tf (@{$target_files}) {
799 my $v = $self->{new_info}->{$tf}->get($tag);
800 if (defined $v) {
801 return wantarray? ($v, $tf): $v;
802 }
803 }
804 return;
805 }
806
807 # Compares $info and $new_info (which must be of type
808 # Image::Synchronize::GroupedInfo) to determine which tags need to be
809 # changed.
810 #
811 # $messages, if defined, is a reference to an array to which relevant
812 # progress messages are appended.
813 #
814 # $changes, if defined, is a reference to a hash to which key-value
815 # pairs are added in which the keys are the tags for which a change is
816 # needed and the values are the amount of force that is needed for the
817 # change.
818 #
819 # Returns 0 if no changes are needed. If changes are needed, then
820 # returns 1 plus the minimum amount of force that is needed for any of
821 # the changes.
822 sub get_changes {
823 my ($self, $file, $info, $new_info, $extra_info, $messages, $changes) = @_;
824
825 $messages //= [];
826 $changes //= {};
827
828 my $can_change = is_image_or_movie($info);
829
830 # In general, the file needs modification if FileModifyDate has
831 # changed or if the GPS location and timestamp are newly added
832 # or if the GPS location or timestamp have changed and
833 # TimeSource was set but not equal to 'GPS'.
834 #
835 # If the user specified --force or --force 1, then the file also
836 # needs modification if CameraID, DateTimeOriginal, or
837 # TimeSource have changed or are newly added.
838 #
839 # If the user specified --force 2, then the file also needs
840 # modification if the GPS location or timestamp have changed and
841 # TimeSource was not set or was equal to 'GPS'.
842
843 my $gps_location_tags_count;
844 my $min_force_for_change = 99;
845 if ($can_change) {
846 # not a metadata file
847 foreach my $tag (
848 qw(
849 CameraID
850 DateTimeOriginal
851 ImsyncVersion
852 FileModifyDate
853 GPSAltitude
854 GPSLatitude
855 GPSLongitude
856 TimeSource
857 )
858 ) {
859 my $new = $new_info->get($tag);
860 my $old = $info->get($tag);
861 my $change = 1;
862
863 if ( (defined($gps_location_tags{$tag}))
864 and (defined($old) or defined($new)) ) {
865 ++$gps_location_tags_count;
866 next; # handle these separately
867 }
868
869 if ( defined $old ) {
870 if ( defined $new ) {
871 if ( $new ne $old ) {
872 push @{$messages}, " $tag has changed.";
873 } else {
874 $change = 0;
875 }
876 } else { # old but not new
877 push @{$messages}, " $tag has disappeared,";
878 }
879 } elsif ( defined $new ) { # new but not old
880 push @{$messages}, " $tag is new.";
881 } else { # neither new nor old
882 $change = 0;
883 }
884 if ($change) {
885 state $force_1_tags = {
886 map { $_ => 1 }
887 qw(
888 CameraID
889 DateTimeOriginal
890 ImsyncVersion
891 TimeSource
892 )
893 };
894 if ( $tag eq 'FileModifyDate' ) {
895 $min_force_for_change = 0
896 if $min_force_for_change > 0;
897 $changes->{$tag} = 0;
898 } elsif ( exists $force_1_tags->{$tag} ) {
899 $min_force_for_change = 1
900 if $min_force_for_change > 1;
901 $changes->{$tag} = 1;
902 }
903 }
904 }
905 } else {
906 # a non-image file; cannot change any embedded tags, only file
907 # modification timestamp
908 my $tag = 'FileModifyDate';
909 my $new = $new_info->get($tag);
910 my $old = $info->get($tag);
911 my $change = 1;
912
913 if ( defined $old ) {
914 if ( defined $new ) {
915 if ( $new ne $old ) {
916 push @{$messages}, " $tag has changed.";
917 } else {
918 $change = 0;
919 }
920 } else { # old but not new
921 push @{$messages}, " $tag has disappeared,";
922 }
923 } elsif ( defined $new ) { # new but not old
924 push @{$messages}, " $tag is new.";
925 } else { # neither new nor old
926 $change = 0;
927 }
928 if ($change) {
929 $min_force_for_change = 0
930 if $min_force_for_change > 0;
931 $changes->{$tag} = 0;
932 }
933 }
934
935 if ($gps_location_tags_count) {
936 # Some cameras, for example the LG-H870, can record a GPS altitude
937 # by itself, without a GPS latitude and longitude, when the camera
938 # doesn't know its location. We treat those cases as if the GPS
939 # altitude wasn't there, but if the file gets modified anyway and
940 # get no new GPS position then we remove the troublesome and
941 # to us useless GPS altitude.
942 my $change = 1;
943 my $old_pos_count = 0;
944 my @old_pos = map {
945 my $v = $info->get($_);
946 ++$old_pos_count
947 if defined $v
948 and $_ ne 'GPSAltitude';
949 $v
950 } @gps_location_tags;
951 my $new_pos_count = 0;
952 my @new_pos = map {
953 my $v = $new_info->get($_);
954 ++$new_pos_count
955 if defined $v
956 and $_ ne 'GPSAltitude';
957 $v
958 } @gps_location_tags;
959 my $distance = geo_distance( \@old_pos, \@new_pos );
960 if ( defined $distance ) { # the GPS location was complete in old
961 # and new
962
963 # The precision of the updated or new coordinates as written in
964 # the image file may differ from the precision provided by the
965 # source of the coordinates. Then later runs of this
966 # application with the same inputs and configuration may report
967 # a non-zero distance. To avoid this problem we ignore
968 # distances less than 1 cm.
969 if ( $distance >= 0.01 ) {
970 my ( $value, $prefix ) = si_prefix($distance);
971 push @{$messages}, " GPS position has changed by $value ${prefix}m.";
972 $extra_info->set( 'position_change', $distance );
973 if ( $distance > ( $self->{max_gps_distance} // 0 ) ) {
974 $self->{max_gps_distance} = $distance;
975 $self->{max_gps_file} = $file;
976 }
977 }
978 else {
979 $change = 0;
980 }
981 }
982 elsif ( $old_pos_count == 2 ) {
983 push @{$messages}, " GPS position has disappeared,";
984 $new_info->delete($_) foreach ( @gps_location_tags, 'GPSDateTime' );
985 }
986 elsif ( $new_pos_count ) {
987 push @{$messages}, " GPS position is new.";
988 } elsif ($old_pos_count == 0) {
989 # no old location and no new location and yet we got here. Must
990 # be the case where the file contains a solitary GPS altitude.
991 push @{$messages}, ' Solitary GPS altitude.';
992 $new_info->delete('GPSAltitude');
993 } else {
994 push @{$messages}, ' GPS position is incomplete.';
995 $new_info->delete($_) foreach ( @gps_location_tags, 'GPSDateTime' );
996 }
997 if ($change) {
998
999 # if the GPS position already existed and the TimeSource is
1000 # absent or equal to GPS, then we assume the GPS information was
1001 # embedded by a GPS device and should only be modified if the
1002 # --force level is at least 2. Otherwise we assume that the old
1003 # GPS position (if any) was added by us in an earlier run; then
1004 # we can already modify it even if no --force is specified.
1005 if ( ( $info->get('TimeSource') // 'GPS' ) eq 'GPS' )
1006 {
1007 $min_force_for_change = 2
1008 if $min_force_for_change > 2;
1009 foreach my $tag (@gps_location_tags, 'GPSDateTime') {
1010 $changes->{$tag} = 2
1011 if stringify($info->get($tag)) ne stringify($new_info->get($tag));
1012 }
1013 }
1014 else {
1015 $min_force_for_change = 0
1016 if $min_force_for_change > 0;
1017 foreach my $tag (@gps_location_tags, 'GPSDateTime') {
1018 $changes->{$tag} = 0
1019 if stringify($info->get($tag)) ne stringify($new_info->get($tag));
1020 }
1021 }
1022 }
1023 }
1024
1025 push @{$messages}, $new_info->stringify(' ');
1026
1027 if (scalar(keys %{$changes}) == 1
1028 and exists $changes->{ImsyncVersion}
1029 and defined $info->get('ImsyncVersion')) {
1030 # The only thing that changed is the ImsyncVersion value: the file
1031 # already has that tag but the current version of imsync is newer
1032 # than the one that modified the file last. This is not a good
1033 # reason to change the file, so we suppress that change.
1034 %{$changes} = ();
1035 $min_force_for_change = 99;
1036 $new_info->set('ImsyncVersion', $info->get('ImsyncVersion'));
1037 push @{$messages}, ' Only ImsyncVersion has changed -- suppressing.';
1038 }
1039
1040 $extra_info->set( 'min_force_for_change', $min_force_for_change );
1041
1042 my $result = 0;
1043 if ( $min_force_for_change < 99 ) { # some changes
1044 if ( ( $extra_info->get('explicit_change') // 0 ) > 0
1045 or $self->option( 'force', 0 ) >= $min_force_for_change )
1046 {
1047 $extra_info->set( 'needs_modification', 1 );
1048 push @{$messages}, ' Modification of this file is indicated.';
1049 $extra_info->set( 'changes', [ sort keys %{$changes} ] )
1050 if keys %{$changes};
1051 $result = 1;
1052 }
1053 else {
1054 push @{$messages}, ' Modification of this file is suppressed, needs --force'
1055 . ( $min_force_for_change > 1 ? ' --force' : '' ) . '.';
1056 $result = $min_force_for_change + 1;
1057 }
1058 }
1059 else {
1060 push @{$messages}, ' No changes.';
1061 }
1062
1063 return $result;
1064 }
1065
1066 # returns 0 if the file needs no modification, 1 if the file needs
1067 # modification (for the current level of force), or 1 + minimum needed
1068 # force if the file needs more force if it is to be modified.
1069 sub determine_new_values_for_file {
1070 my ( $self, $file, $donator_files ) = @_;
1071
1072 # The user may have chosen to get progress messages printed only
1073 # when the file needs modification, but we can't tell yet if that is
1074 # the case. Save up the messages until we can tell if we need them.
1075 my @messages;
1076 push @messages, "\nProcessing '$file'";
1077
1078 my $info = $self->{original_info}->{$file};
1079 my $new_info = $self->{new_info}->{$file} =
1080 new Image::Synchronize::GroupedInfo;
1081 my $extra_info = $self->{extra_info}->{$file} =
1082 new Image::Synchronize::GroupedInfo;
1083
1084 # copy preferred tags to final list
1085 my @tagstoget = qw(
1086 CameraID
1087 CreateDate
1088 DateTimeOriginal
1089 FileModifyDate
1090 GPSAltitude
1091 GPSDateTime
1092 GPSLatitude
1093 GPSLongitude
1094 TimeSource
1095 );
1096
1097 foreach my $tag (@tagstoget)
1098 {
1099 my $v = $info->get($tag);
1100 if (blessed $v) {
1101 # create a clone; we don't want to accidentally adjust the $info
1102 # member by adjusting the $new_info member
1103 $v = clone($v);
1104 }
1105 $new_info->set( $tag, $v ) if defined $v;
1106 }
1107
1108 # TODO: only report donators when sufficiently verbose
1109 if (defined($donator_files) and @{$donator_files})
1110 {
1111 push @messages, " Possible donators in order of descending priority:";
1112 push @messages, " $_" foreach @{$donator_files};
1113 }
1114
1115 # for files that aren't image or movie files, we suppress changes of
1116 # any tags except FileModifyDate.
1117 my $can_change = is_image_or_movie($info);
1118
1119 # For image and movie files, if there is at least one donator file
1120 # and if the first of them is a metadata file then we prefer the
1121 # metadata from the donator file.
1122 if ($can_change
1123 and defined($donator_files)
1124 and @{$donator_files}) {
1125 my $first_donator_info = $self->{original_info}->{$donator_files->[0]};
1126 if ($first_donator_info->get('is_metadata')) {
1127 foreach my $tag (@tagstoget) {
1128 my $v = $first_donator_info->get($tag);
1129 if (blessed $v) {
1130 # create a clone; we don't want to accidentally adjust the $info
1131 # member by adjusting the $new_info member
1132 $v = clone($v);
1133 }
1134 my $current = $new_info->get($tag);
1135 if (defined $current
1136 and (not(defined $v)
1137 or $v ne $current)) {
1138 $new_info->set( $tag, $v );
1139 }
1140 }
1141 }
1142 }
1143
1144 # determine final camera ID.
1145
1146 # user-specified camera ID?
1147 my $camera_id = $self->user_camera_id($file);
1148 if ( defined $camera_id ) {
1149 push @messages, ' Camera ID set by user (--cameraid).';
1150 $extra_info->set( 'explicit_change', 1 ) if $can_change;
1151 } else {
1152 $camera_id = $info->get('camera_id');
1153
1154 if ( not( defined $camera_id ) ) {
1155 ($camera_id, my $donator) =
1156 $self->get_from_targets('CameraID', $donator_files);
1157 if ( defined($camera_id) ) {
1158 push @messages, " Camera ID copied from '$donator'.";
1159 } else {
1160 my $fallback_camera_id = $info->get('fallback_camera_id')
1161 // fallback_camera_id($file);
1162 $camera_id = $self->camera_id_from_fallback($fallback_camera_id);
1163 if ( defined $camera_id ) {
1164 push @messages, ' Got camera ID via fallback.';
1165 }
1166 }
1167 }
1168 }
1169 $new_info->set( 'CameraID', $camera_id );
1170
1171 # get creation timestamp if available
1172
1173 my $timesource; # value for TimeSource tag
1174 my $timesource_letter; # letter to identify time source in report
1175
1176 my $create_time = $new_info->get('CreateDate');
1177 if (not defined $create_time) {
1178 ($create_time, my $donator) =
1179 $self->get_from_targets('CreateDate', $donator_files);
1180 if (defined $create_time) {
1181 push @messages, " Creation timestamp copied from '$donator'.";
1182 $timesource_letter = 'n';
1183 $timesource = 'Other';
1184 }
1185 }
1186
1187 # determine the target time
1188
1189 my $target_time = $self->repository('user_times')->{$file};
1190 if (defined $target_time) {
1191 push @messages, " Target time is set by user.";
1192 $extra_info->set( 'explicit_change', 1 ) if $can_change;
1193 $timesource_letter //= 't'; # source is user's --time
1194 $timesource //= 'User';
1195 } elsif (defined($camera_id) and defined($create_time)) {
1196 # An image file may or may not have a timezone offset specified
1197 # for its CreateDate. How do we handle both kinds of timestamps
1198 # when applying a camera offset?
1199 #
1200 # A CreateDate timestamp is schematically T+Z (e.g.,
1201 # 2020-11-23T14:23+02:00) where T is the timestamp
1202 # (2020-11-23T14:23) and Z is the timezone offset (+02:00), in
1203 # such a way that T − Z = U aims for UTC (2020-11-23T12:23Z) -- if
1204 # a timezone offset is known. If no timezone offset is known,
1205 # then the timestamp is just T.
1206 #
1207 # The camera offset is schematically t+z where t is the timestamp
1208 # part and z is the timezone part, similar to T and Z.
1209 #
1210 # Camera offsets written by versions < 2.001 don't have a timezone
1211 # part z. They get treated as if the specified value were t + z
1212 # where z is the timezone offset that the current system would
1213 # have had on the indicated date in the current location.
1214 #
1215 # The target timestamp is schematically τ+ζ where τ is the
1216 # timestamp part and ζ is the timezone part, similar to T and Z.
1217 # The corresponding UTC time is τ − ζ = υ.
1218 #
1219 # If we don't know Z, then we want the target timestamp to be
1220 # calculated as
1221 #
1222 # τ = T + t
1223 # ζ = z
1224 #
1225 # i.e., t is how much to add to T, and z is the timezone to assume
1226 # for the result.
1227 #
1228 # If we do know Z, then we still want t to indicate how far off
1229 # the CreateDate was, so
1230 #
1231 # υ = U + t ⇔ τ − ζ = T − Z + t ⇔ τ = t + ζ + T − Z
1232 #
1233 # And we still want to end up in timezone z, so
1234 #
1235 # ζ = z
1236 # τ = t + z + T − Z
1237 #
1238 # If we compare the with-Z result with the no-Z result then we see
1239 # that they are equivalent if z = Z, i.e., equivalent to the
1240 # assumption that the CreateDate timezone is equal to the camera
1241 # offset timezone.
1242
1243 my $cto = $self->gps_offsets->get($camera_id, $create_time);
1244 if ( defined $cto) {
1245 push @messages, " Target time is based on camera timezone offsets.";
1246 $timesource_letter //= 's'; # source is CreateDate plus other images
1247 $timesource //= 'Other';
1248
1249 unless ($cto->has_timezone_offset) {
1250 # timezone offset for local system at given instant
1251 my $tz = $cto->local_timezone_offset;
1252 # $cto_before->time_local
1253 # == $cto_after->time_local + $cto_after->timezone_offset
1254 # and $cto_after->timezone_offset = $tz
1255 # so $cto_after->time_local = $cto_before->time_local - $tz
1256 $cto = ($cto - $tz)->set_timezone_offset($tz);
1257 }
1258
1259 $target_time = apply_camera_offset( $create_time, $cto );
1260 }
1261 }
1262 if (not defined $target_time) {
1263 $target_time = $new_info->get('DateTimeOriginal');
1264 if (defined $target_time) {
1265 push @messages,
1266 " Target time is embedded original time (DateTimeOriginal).";
1267 $timesource_letter //= 'o'; # source is DateTimeOriginal
1268 } else {
1269 ($target_time, my $donator) =
1270 $self->get_from_targets('DateTimeOriginal', $donator_files);
1271 if (defined $target_time) {
1272 push @messages, " Target timestamp copied from '$donator' "
1273 . "(DateTimeOriginal).";
1274 $timesource_letter //= 'n'; # source is other file
1275 }
1276 }
1277 if (not defined $target_time) {
1278 $target_time = $new_info->get('CreateDate');
1279 if (defined $target_time) {
1280 push @messages, " Target time is embedded creation time (CreateDate).";
1281 $timesource_letter //= 'c'; # source is CreateDate
1282 }
1283 }
1284 }
1285 if (not defined $target_time) {
1286 ($target_time, my $donator) =
1287 $self->get_from_targets('FileModifyDate', $donator_files);
1288 if (defined $target_time) {
1289 push @messages, " Target timestamp copied from '$donator' "
1290 . "(FileModifyDate).";
1291 $timesource_letter //= 'n'; # source is other file
1292 }
1293 }
1294 if (not(defined $target_time)
1295 && $info->get('is_metadata')) {
1296 $target_time = $info->get('TargetFileModifyDate');
1297 if (defined $target_time) {
1298 push @messages, " Target timestamp is metadata target file's "
1299 . "FileModifyDate.";
1300 $timesource_letter //= 'm'; # source is metadata target
1301 }
1302 }
1303
1304 if (defined($target_time)) {
1305 $timesource //= 'Other';
1306
1307 # the timezone part of the camera offset is the target timezone of
1308 # the target time
1309 my $cto = $self->gps_offsets->get($camera_id, $create_time // $target_time);
1310
1311 if (defined $cto) {
1312 unless ($cto->has_timezone_offset) {
1313 # timezone offset for local system at given instant
1314 my $tz = $cto->local_timezone_offset;
1315 # $cto_before->time_local
1316 # == $cto_after->time_local + $cto_after->timezone_offset
1317 # and $cto_after->timezone_offset = $tz
1318 # so $cto_after->time_local = $cto_before->time_local - $tz
1319 $cto = ($cto - $tz)->set_timezone_offset($tz);
1320 }
1321 $target_time->adjust_timezone_offset($cto->timezone_offset);
1322 } else {
1323 $target_time->adjust_to_local_timezone;
1324 }
1325
1326 if ( defined( my $j = $self->repository('jump')->{$file} ) ) {
1327 $target_time += $j * 3600;
1328 push @messages, " Clock time of camera jumped by $j hours.";
1329 }
1330 }
1331
1332 if ($can_change) {
1333 $new_info->set('DateTimeOriginal', $target_time);
1334 $new_info->set( 'TimeSource', $timesource );
1335 }
1336 $extra_info->set( 'timesource_letter', $timesource_letter );
1337
1338 # the File Modification Time may be in a different timezone than the
1339 # target time, because of --relativetime
1340 # TODO: suppress if target time is donated FileModifyDate?
1341 $new_info->set('FileModifyDate', $self->file_from_dto($target_time));
1342
1343 push @messages, " Target time is $target_time.";
1344
1345 # determine the camera offset
1346
1347 if ($timesource_letter ne 'n'
1348 and defined($create_time)
1349 and defined($target_time)
1350 and defined ($camera_id)) {
1351 my $camera_timezone_offset =
1352 deduce_camera_offset( $create_time, $target_time );
1353
1354 push @messages, ' Timezone offset for camera is '
1355 . $camera_timezone_offset->display_time . '.';
1356
1357 # record the offset so it can be used for later files
1358 $self->gps_offsets->set($camera_id, $create_time, $camera_timezone_offset);
1359 }
1360
1361 # determine position
1362
1363 if ( defined($target_time) and $can_change) {
1364 my $position = $self->repository('user_locations')->{$file};
1365 if ( defined $position ) {
1366 $extra_info->set( 'explicit_change', 1 );
1367 } elsif (my $ul = $self->repository('user_locations')->{''}) {
1368 foreach my $tp (@$ul) {
1369 if ($tp->[0]->contains_local($target_time)) {
1370 $position = $tp->[1];
1371 $extra_info->set('explicit_change', 1);
1372 }
1373 }
1374 } elsif (
1375 not( $info->get('GPSDateTime') ) # none yet
1376 or (
1377 ( $info->get('TimeSource') // 'GPS' ) ne 'GPS' # not original
1378 or $self->option( 'force', 0 ) >= 2 # or force is at least 2
1379 )
1380 ) {
1381 # presumably able to store a GPS position; deduce one if
1382 # possible.
1383 #
1384 # Do we have a target file with GPS information?
1385 (undef, my $donator) = $self->get_from_targets('GPSDateTime', $donator_files);
1386 if (defined $donator) {
1387 my $donator_info = $self->{new_info}->{$donator};
1388 # TODO: store in $positions
1389 # push @messages, " GPS information copied from '$donator'.";
1390 # foreach my $tag (@gps_location_tags, 'GPSDateTime') {
1391 # $new_info->set($tag, $donator_info->get($tag));
1392 # }
1393 }
1394 #
1395 # If the file already had a GPS position, then should we
1396 # update it? If TimeSource was absent or equal to 'GPS', then
1397 # the existing GPS position was embedded in the file when the
1398 # file was created, presumably directly from an attached GPS
1399 # device.
1400 #
1401 # In that case we should be very reluctant to update that
1402 # position, only updating the GPS position if --force has at
1403 # least a value of 2. If --force is less than 2, then we
1404 # should propose to update the TimeSource to 'GPS' if it was
1405 # empty.
1406 #
1407 # If a GPS position and TimeSource were already present but
1408 # TimeSource is not equal to 'GPS', then the GPS position is
1409 # assumed to have been added by an earlier run of an
1410 # application like this one, and then it is OK to update it.
1411 my @gps_positions =
1412 $self->gps_positions->position_for_time( $target_time,
1413 scope => scope_for_file($file) );
1414 if (@gps_positions) {
1415 my $l = length( $gps_positions[0]->{scope} );
1416
1417 # ignore positions from other than the first (longest) scope
1418 @gps_positions = grep { length( $_->{scope} ) == $l } @gps_positions;
1419
1420 if ( @gps_positions > 1 ) {
1421
1422 # sort by track ID
1423 @gps_positions =
1424 sort { $a->{track} cmp $b->{track} } @gps_positions;
1425 @gps_positions = ( $gps_positions[0] );
1426 }
1427 $position = $gps_positions[0]->{position};
1428 }
1429 }
1430 if ( defined $position ) {
1431 if (@{$position}) {
1432 foreach my $ix (0..$#gps_location_tags) {
1433 my $v = $info->get($gps_location_tags[$ix]);
1434 $new_info->set( $gps_location_tags[$ix], $position->[$ix] )
1435 if defined($position->[$ix]) and (not(defined $v) or
1436 $position->[$ix] != $v);
1437 }
1438 my $v = $info->get('GPSDateTime');
1439 $new_info->set( 'GPSDateTime', $target_time )
1440 if not(defined $v) or $target_time != $v;
1441 croak "Expected new TimeSource to have been set already\n"
1442 unless defined $new_info->get('TimeSource');
1443 } else { # remove position
1444 foreach my $ix (0..$#gps_location_tags) {
1445 $new_info->delete( $gps_location_tags[$ix] );
1446 }
1447 $new_info->delete( 'GPSDateTime' );
1448 }
1449 }
1450 }
1451
1452 # If the current image is acquiring an embedded GPSDateTime,
1453 # then it is vital that it also ends up with an embedded
1454 # TimeSource, because imsync interprets a GPSDateTime without
1455 # a TimeSource as having been embedded in the image by the
1456 # original recording device (such as a smartphone), for which
1457 # the GPS fix may differ a few seconds from the image recording
1458 # time (with the difference not necessarily being the same from
1459 # one image to the next), which may cause imsync to round the
1460 # camera offset.
1461 #
1462 # However, if the new GPSDateTime is embedded by imsync
1463 # (together with a location), then the camera offset is deemed
1464 # to be exact and must not be rounded. So, such a new
1465 # GPSDateTime must be accompanied by a suitable TimeSource, to
1466 # avoid it being interpreted later as an original GPSDateTime
1467 # for which the camera offset may get rounded, thus differing
1468 # from the previous camera offset and leading to the target time
1469 # being modified again (and erroneously).
1470 #
1471 # A TimeSource may be deduced during the Inspect phase even if
1472 # the image has no embedded TimeSource, so if the final
1473 # TimeSource (deduced during this Determine phase) is equal to
1474 # the TimeSource from the Inspect phase, then the above
1475 # $register call for XMP:TimeSource won't have registered a new
1476 # value for XMP:TimeSource, but if a GPSDateTime is being added
1477 # then we must also have a TimeSource.
1478
1479 if ( $can_change ) {
1480 $new_info->set( 'DateTimeOriginal', $new_info->get('FileModifyDate') )
1481 unless defined $new_info->get('DateTimeOriginal');
1482 $new_info->set( 'ImsyncVersion', $VERSION );
1483
1484 # our XMP tags must go in the XMP namespace
1485 foreach my $tag (@own_xmp_tags) {
1486 my $value = $new_info->get($tag);
1487 if ( defined $value ) {
1488 $new_info->delete($tag); # clear old value (not in a group)
1489 $new_info->set( 'XMP', $tag, $value );
1490 }
1491 }
1492
1493 # We want to store DateTimeOriginal with a timezone, which means
1494 # we must put it in the XMP group, because DateTimeOriginal in the
1495 # default Exif group likely has no room for a timezone.
1496 {
1497 my $dto = $new_info->get('DateTimeOriginal');
1498 if (defined $dto) {
1499 $new_info->set( 'XMP', 'DateTimeOriginal', $dto );
1500 my $o = $info->get('DateTimeOriginal');
1501 if (defined($o) && not($o->has_timezone_offset)) {
1502 # original DateTimeOriginal has no timezone offset, so the
1503 # new one (for the Exif group) can't have one
1504 $new_info->set('DateTimeOriginal', $dto->clone->remove_timezone);
1505 }
1506 }
1507 }
1508 }
1509
1510 my %changes;
1511
1512 my $result = $self->get_changes($file, $info, $new_info, $extra_info,
1513 \@messages, \%changes);
1514
1515 {
1516 my $camera_id = $new_info->get('CameraID');
1517 $self->{camera_ids}->{$camera_id} = 1 if defined $camera_id;
1518 }
1519
1520 # if verbose & 4 then only print if needs_modification
1521 # if verbose & 8 then print even if not needs_modification
1522 # otherwise don't print
1523 my $bitflag = 8;
1524 $bitflag |= 4 if $extra_info->get('needs_modification');
1525 log_message( $bitflag, { name => $file }, join( "\n", @messages ) . "\n" );
1526
1527 return $result;
1528 }
1529
1530 # $text = display_offset($offset, $maxlength);
1531 #
1532 # Returns a text representation of the time $offset measured in
1533 # seconds, that is at most $maxlength characters wide. The text
1534 # representation uses 'y' for years, 'd' for days, 'h' for hours, 'm'
1535 # for minutes, and 's' for seconds. If the allowed width is too
1536 # narrow to fit the entire representation, then only as many of the
1537 # largest components are returned as will fit, and a period '.' is
1538 # appended to indicate the truncation. If $maxlength is undefined
1539 # then the length is unrestricted.
1540 sub display_offset {
1541 use integer;
1542 my ( $offset, $maxlength ) = @_;
1543 $maxlength //= 80;
1544 return '0' unless $offset;
1545 my $a = abs($offset);
1546 my @steps = ( 60, 60, 24, 365 );
1547 my @symbols = ( 's', 'm', 'h', 'd', 'y' );
1548 my @values;
1549
1550 foreach my $step (@steps) {
1551 push @values, $a % $step;
1552 $a /= $step;
1553 last unless $a;
1554 }
1555 push @values, $a if $a;
1556 my @result;
1557 for my $i ( 0 .. $#values ) {
1558 push @result, $values[$i] . $symbols[$i] if $values[$i];
1559 }
1560 my $result = ( $offset > 0 ) ? '+' : '-';
1561 while ( my $value = pop @result ) {
1562 my $new_result = $result . $value;
1563 if ( length($new_result) > $maxlength ) {
1564 $result = $result . '.';
1565 last;
1566 }
1567 if ( length($new_result) == $maxlength
1568 and @result )
1569 {
1570 $result = $result . '.';
1571 last;
1572 }
1573 $result = $new_result;
1574 }
1575 return $result;
1576 }
1577
1578 # $regex = end_glob_to_regex($pattern);
1579 #
1580 # return a regular expression corresponding to glob C<$pattern>.
1581 sub end_glob_to_regex {
1582 my ($pattern) = @_;
1583 $pattern = perlish_path($pattern);
1584 $pattern =~ s/([.+()])/\\$1/g; # lose special meaning
1585 $pattern =~ s|\*|[^/]*|g; # match zero or more characters not equal to /
1586 $pattern =~ s|\?|[^/]|g; # match one character not equal to /
1587 my $case = $CASE_TOLERANT ? '(?i)' : '';
1588 return qr/${case}${pattern}$/;
1589 }
1590
1591 # $success = $ims->ensure_backup($file);
1592 #
1593 # Ensures that there is a backup of the $file. The name of the backup
1594 # is equal to $file with '_original' appended. If such a file does
1595 # not yet exist, then a copy of $file is made with that name.
1596 #
1597 # Returns a true value upon success, and otherwise a false value.
1598 sub ensure_backup {
1599 my ( $self, $file ) = @_;
1600 my $copy = "${file}_original";
1601 my $short = file($copy)->basename;
1602 if ( -e $copy ) {
1603 log_message(
1604 4,
1605 { name => $file },
1606 sub { "Backup '$short' of '$file' already exists.\n" }
1607 );
1608 return 1;
1609 }
1610 if ( copy( $file, $copy ) ) {
1611 log_message(
1612 4,
1613 { name => $file },
1614 sub { "Created backup '$short' of '$file'.\n" }
1615 );
1616 return 1;
1617 }
1618 else {
1619 log_error("Creating backup '$short' of '$file' failed; reason: $!\n");
1620 return;
1621 }
1622 }
1623
1624 # $ims->export_camera_offsets;
1625 #
1626 # Export the camera offsets. Returns C<$ims>.
1627 sub export_camera_offsets {
1628 my ($self) = @_;
1629 my $path = $self->camera_offsets_path;
1630 if ($path) {
1631 my $data = $self->gps_offsets->export_data;
1632 log_message("Exporting camera offsets to '$path'.\n");
1633 DumpFile( $path, $data );
1634 } else {
1635 log_message("Not exporting camera offsets because --offsetspath value was empty.\n");
1636 }
1637 $self;
1638 }
1639
1640 # $ims->exportgpx($exportfile);
1641 #
1642 # Exports the positions to GPX file C<$exportfile>. Returns C<$ims>.
1643 sub exportgpx {
1644 my ( $self, $exportfile ) = @_;
1645
1646 return $self unless defined $exportfile;
1647
1648 $exportfile = 'export.gpx' unless $exportfile;
1649
1650 my ( $xmlroot, $minlat, $maxlat, $minlon, $maxlon, $count );
1651 foreach my $file ( sort keys %{ $self->{new_info} } ) {
1652 my $new_info = $self->{new_info}->{$file};
1653 next unless defined( my $longitude = $new_info->get('GPSLongitude') );
1654 # get GPSDateTime, not FileModifyDate, because of GPS fix lag
1655 next unless defined( my $time = $new_info->get('GPSDateTime') );
1656
1657 ++$count;
1658
1659 my $latitude = $new_info->get('GPSLatitude');
1660 my $altitude = $new_info->get('GPSAltitude');
1661 $time = $time->display_utc;
1662
1663 $minlat = $latitude
1664 if not( defined $minlat )
1665 or $latitude < $minlat;
1666 $maxlat = $latitude
1667 if not( defined $maxlat )
1668 or $latitude > $minlat;
1669 $minlon = $longitude
1670 if not( defined $minlon )
1671 or $longitude < $minlon;
1672 $maxlon = $longitude
1673 if not( defined $maxlon )
1674 or $longitude > $minlon;
1675
1676 $xmlroot //= XML::Twig::Elt->new(
1677 'gpx',
1678 {
1679 'xmlns' => 'http://www.topografix.com/GPX/1/1',
1680 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
1681 version => '1.1',
1682 'xsi:schemaLocation' =>
1683 'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd',
1684 creator => 'Exiftime',
1685 }
1686 );
1687
1688 # latitude must be between -90 and +90, inclusive
1689 # longitude must be between -180 and +180, inclusive
1690 my $wpt = $xmlroot->insert_new_elt(
1691 'last_child',
1692 wpt => {
1693 lat => $latitude,
1694 lon => $longitude
1695 }
1696 );
1697 $wpt->insert_new_elt( 'name', file($file)->basename );
1698 $wpt->insert_new_elt( 'time', $time ) if defined $time;
1699 $wpt->insert_new_elt( 'ele', $altitude ) if defined $altitude;
1700 }
1701
1702 if ( defined $xmlroot ) {
1703 my $metadata = $xmlroot->insert_new_elt('metadata');
1704 $metadata->insert_new_elt(
1705 bounds => {
1706 maxlat => $maxlat,
1707 maxlon => $maxlon,
1708 minlat => $minlat,
1709 minlon => $minlon
1710 }
1711 );
1712
1713 $xmlroot->set_pretty_print('indented');
1714 $xmlroot->set_empty_tag_style('html');
1715 $xmlroot->print_to_file($exportfile);
1716 log_message(
1717 sub {
1718 "\nExported "
1719 . ( $self->option('modify') ? '' : '(intended) ' )
1720 . "GPS tags of "
1721 . ( $count // 0 )
1722 . " images to '$exportfile'.\n";
1723 }
1724 );
1725 }
1726 else {
1727 log_message( sub { "\nNo GPS tags to export to '$exportfile'.\n" } );
1728 }
1729 return $self;
1730 }
1731
1732 # $fallback_camera_id = fallback_camera_id($path);
1733 #
1734 # Returns the fall-back camera ID, to be used in case the usual
1735 # information from which to deduce a camera ID is not available.
1736 sub fallback_camera_id {
1737 my ( $path ) = @_;
1738 return '?' . basename_pattern($path);
1739 }
1740
1741 # @targets = $ims->find_info_targets($t);
1742 #
1743 # Returns the files that match C<$t>, which must be a
1744 # Synchronize::Timestamp or a Synchronize::Timerange.
1745 sub find_info_targets {
1746 my ( $self, $t ) = @_;
1747 my @targets;
1748 foreach my $file ( keys %{ $self->{original_info} } ) {
1749 my $create_date = $self->{original_info}->{$file}->get('CreateDate');
1750 next unless defined $create_date;
1751 push @targets, $file
1752 if $t->contains_local($create_date);
1753 }
1754 return @targets;
1755 }
1756
1757 # $r = geo_distance([$latitude1, $longitude1, $altitude1],
1758 # [$latitude2, $longitude2, $altitude2]);
1759 #
1760 # Calculate the approximate distance in meters between the two
1761 # positions. The latitude and longitude are measured in degrees and
1762 # the optional altitudes in meters.
1763 sub geo_distance {
1764 my ( $pos1, $pos2 ) = @_;
1765 return unless ref $pos1 eq 'ARRAY' and ref $pos2 eq 'ARRAY';
1766 return unless scalar( @{$pos1} ) >= 2 and scalar( @{$pos2} ) >= 2;
1767 foreach my $p ( $pos1, $pos2 ) {
1768 foreach my $i ( 0 .. 1 ) {
1769 return unless defined $p->[$i];
1770 }
1771 }
1772
1773 # Image::Exiftool stores longitude and latitude with a resolution of
1774 # 0.01 seconds of arc, and altitude with a resolution of 0.01 m, so
1775 # it is not useful to report a difference that is smaller than that
1776 # resolution.
1777
1778 my @pos1_r = @{$pos1};
1779 @pos1_r[ 0 .. 1 ] = map { floor( $_ * 360000 + 0.5 ) } @pos1_r[ 0 .. 1 ];
1780 $pos1_r[2] = floor( $pos1_r[2] * 100 + 0.5 ) if defined $pos1_r[2];
1781 my @pos2_r = @{$pos2};
1782 @pos2_r[ 0 .. 1 ] = map { floor( $_ * 360000 + 0.5 ) } @pos2_r[ 0 .. 1 ];
1783 $pos2_r[2] = floor( $pos2_r[2] * 100 + 0.5 ) if defined $pos2_r[2];
1784
1785 return 0
1786 if $pos1_r[0] == $pos2_r[0]
1787 && $pos1_r[1] == $pos2_r[1]
1788 && ( $pos1_r[2] // 'X' ) eq ( $pos2_r[2] // 'X' );
1789
1790 use constant DEG => 45 / atan2( 1, 1 );
1791 use constant R => 6378000;
1792
1793 my ( $lat1, $lon1, $alt1 ) =
1794 ( $pos1->[0] / DEG, $pos1->[1] / DEG, ( $pos1->[2] // 0 ) );
1795 my ( $lat2, $lon2, $alt2 ) =
1796 ( $pos2->[0] / DEG, $pos2->[1] / DEG, ( $pos2->[2] // 0 ) );
1797 my $x1 = ( R + $alt1 ) * cos($lon1) * cos($lat1);
1798 my $y1 = ( R + $alt1 ) * sin($lon1) * cos($lat1);
1799 my $z1 = ( R + $alt1 ) * sin($lat1);
1800 my $x2 = ( R + $alt2 ) * cos($lon2) * cos($lat2);
1801 my $y2 = ( R + $alt2 ) * sin($lon2) * cos($lat2);
1802 my $z2 = ( R + $alt2 ) * sin($lat2);
1803 my $dx = $x1 - $x2;
1804 my $dy = $y1 - $y2;
1805 my $dz = $z1 - $z2;
1806 my $rr = $dx * $dx + $dy * $dy + $dz * $dz;
1807 return sqrt($rr);
1808 }
1809
1810 =head2 get_image_info
1811
1812 $et->get_image_info($file);
1813
1814 Extract relevant information from the C<$file>, using
1815 L<Image::ExifTool>, and returns a reference to a hash map containing
1816 the extracted information. The extracted tags are:
1817
1818 =over
1819
1820 =item CreateDate
1821
1822 =item DateTimeOriginal
1823
1824 =item Duration
1825
1826 =item FileModifyDate
1827
1828 =item GPSAltitude
1829
1830 =item GPSAltitudeRef
1831
1832 =item GPSDateTime
1833
1834 =item GPSLatitude
1835
1836 =item GPSLatitudeRef
1837
1838 =item GPSLongitude
1839
1840 =item GPSLongitudeRef
1841
1842 =item ImageWidth
1843
1844 =item Make
1845
1846 =item MIMEType
1847
1848 =item Model
1849
1850 =item QuickTime:CreationDate
1851
1852 =item SerialNumber
1853
1854 =item XMP:CameraID
1855
1856 =item XMP:ImsyncVersion
1857
1858 =item XMP:TimeSource
1859
1860 =item XMP:ExiftimeVersion
1861
1862 =back
1863
1864 Some of the requested tags may occur in the embedded information
1865 multiple times (for example, once as an EXIF tag and once as an XMP
1866 tag). We store all occurrences in the returned map, with keys that
1867 consist of the requested tag name with the tag group (= source) name
1868 prefixed, separated by a colon (e.g., C<"Exif:DateTimeOriginal">).
1869 Additionally, the preferred occurrence is stored in the map with a key
1870 that has only the tag name without the group name (e.g.,
1871 C<"DateTimeOriginal">).
1872
1873 If the ImageWidth tag is present, then the file is considered to be an
1874 image, and then the returned hash map also includes the effective
1875 camera ID, as the value for key C<effective_camera_id>. The
1876 ImageWidth tag is omitted from the hash map.
1877
1878 QuickTime images/movies (recognized by there being a
1879 C<QuickTime:CreateDate> tag) get '|U' appended to their effective
1880 camera ID, and the C<QuickTime::CreateDate> tag is omitted from the
1881 hash map.
1882
1883 Each value is stored for the Image::ExifTool tag including the group
1884 name, and also for the tag without the group name. If a value occurs
1885 in multiple groups (for example EXIF and XMP), then the preferred
1886 value is stored for the tag without the group name.
1887
1888 =cut
1889
1890 sub get_image_info {
1891 my ( $self, $file ) = @_;
1892
1893 my $info = new Image::Synchronize::GroupedInfo;
1894
1895 {
1896 my $image_info = $self->backend->ImageInfo
1897 (
1898 $file,
1899 @all_tags,
1900 {
1901 PrintConv => 0,
1902 defined( $self->option('fastscan') )
1903 ? ( FastScan => $self->option('fastscan') )
1904 : (),
1905 }
1906 );
1907
1908 if ( ( $image_info->{MIMEType} // '' ) =~ m|^image/| ) {
1909 $info->set( 'file_type', 'image' );
1910 }
1911 elsif ( ( $image_info->{MIMEType} // '' ) =~ m|^video/| ) {
1912 $info->set( 'file_type', 'video' );
1913 }
1914 elsif ( $image_info->{ImageWidth} ) {
1915 $info->set( 'file_type', $image_info->{Duration} ? 'video' : 'image' );
1916 }
1917 remove_tag( $image_info, 'ImageWidth', 'Duration' ); # no longer needed
1918
1919 # There are some problems with the hash map returned by
1920 # Image::ExifTool::ImageInfo:
1921 #
1922 # The keys in the hash map may be different from the requested tag
1923 # names -- even if only a single instance of the requested
1924 # information occurs in the file. The letter case of the keys may
1925 # be different from that of the requested tags. The keys do not
1926 # include a tag group, even if the requested tag did include a tag
1927 # group. If there are multiple values for a requested tag, then
1928 # each key gets a parenthesized 1-based index number appended to
1929 # it (e.g., "DateTimeOriginal (1)" and "DateTimeOriginal (2)". In
1930 # such a case we want to know what the source (e.g., Exif, XMP) of
1931 # each of the values is, otherwise we cannot pick the preferred
1932 # value if the values are not identical.
1933 #
1934 # In the case of timestamps, some of them include timezone
1935 # information, but others do not.
1936
1937 my $t = $self->backend->GetInfo("File:FileModifyDate");
1938 if ( $t and scalar keys %{$t} ) {
1939
1940 # $t is a hash reference with one element. We cannot predict
1941 # its tag name exactly; It lacks the 'File:' group name prefix
1942 # but might have a parenthesized instance number affixed.
1943 # Image::ExifTool::GetTagName merely removes the instance
1944 # number, if any, so is no help here.
1945 my $ts =
1946 Image::Synchronize::Timestamp->new( ( values %{$t} )[0] );
1947 if ($ts) {
1948 $info->set( 'File', 'FileModifyDate', $ts );
1949 remove_tag( $image_info, 'FileModifyDate' );
1950 }
1951 }
1952
1953 # now treat the other tags
1954 foreach my $tag ( sort keys %{$image_info} ) {
1955 my $group = $self->backend->GetGroup($tag);
1956 my $bare_tag = $tag =~ s/ \(\d+\)$//r; # omit index number if any
1957
1958 my $value = $image_info->{$tag};
1959
1960 if ( exists $time_tags{$bare_tag}
1961 or exists $time_tags{"$group:$bare_tag"} )
1962 { # it expresses a time
1963 $value = Image::Synchronize::Timestamp->new($value);
1964 }
1965
1966 if (defined $value) {
1967 $info->set( $group, $bare_tag, $value );
1968 } # otherwise $value was undefined, for example because a
1969 # timestamp tag's value wasn't valid.
1970 }
1971
1972 # The GPS tags need special treatment. The sign of the
1973 # longitude/latitude/altitude is generally stored separately from
1974 # the value, and we need to combine them. Exiftool may provide
1975 # already combined values in the 'Composite' and 'XMP' groups,
1976 # except that XMP:GPSAltitude appears not to include a sign yet.
1977 foreach my $tag (@gps_location_tags) {
1978 foreach my $composite_group (qw(Composite XMP)) {
1979 my $composite = $info->get($composite_group, $tag);
1980 if (defined($composite)
1981 && ($tag ne 'GPSAltitude'
1982 || $composite_group ne 'XMP')) {
1983 # We have a composite value, store in the group with the
1984 # empty name ('') so it is the preferred value
1985 $info->set('', $tag, $composite);
1986 last;
1987 }
1988 }
1989 if (not defined $info->get('', $tag)) {
1990 # no composite value yet, so try to construct one from the
1991 # separate parts
1992 foreach my $group ($info->groups($tag)) {
1993 my $value = $info->get($group, $tag);
1994 my $ref = $info->get($group, $tag . 'Ref');
1995 if (defined($value) and defined($ref)) {
1996 if (( $tag eq 'GPSLongitude' and $ref eq 'W')
1997 || ($tag eq 'GPSLatitude' and $ref eq 'S')
1998 || ($tag eq 'GPSAltitude' and $ref eq '1')) {
1999 $value = -$value;
2000 }
2001 $info->set('', $tag, $value);
2002 last;
2003 }
2004 }
2005 }
2006 }
2007 }
2008
2009 if ( is_supposedly_utc($info) ) {
2010
2011 if ( defined $info->get( 'QuickTime', 'CreationDate' )
2012 and $info->get( 'QuickTime', 'CreationDate' )->has_timezone_offset )
2013 {
2014 # We expect that this tag represents absolute time, and allows
2015 # UTC to be deduced. We rename the tag from CreationDate to
2016 # CreateDate so it has the same name as for the other sources.
2017 $info->set( 'QuickTime', 'CreateDate',
2018 $info->get( 'QuickTime', 'CreationDate' ) );
2019 $info->delete( 'QuickTime', 'CreationDate' );
2020 }
2021
2022 if ( defined $info->get('GPSLongitude')
2023 and not( defined $info->get('GPSDateTime') ) )
2024 {
2025 # Some QuickTime files with a GPS position embedded in the
2026 # QuickTime section have no explicit GPSDateTime defined,
2027 # because some other timestamp serves the same purpose.
2028 # (E.g., for Apple iPod Touch.) The remainder of this program
2029 # assumes that if GPS information is present then it includes
2030 # at least longitude, latitude, and time, so copy a suitable
2031 # timestamp to GPSDateTime
2032 $info->set( 'GPSDateTime', $info->get( 'QuickTime', 'CreateDate' ) );
2033 $info->get('GPSDateTime')->adjust_to_utc; # just in case
2034 }
2035 }
2036
2037 if (not(defined $info->get('CreateDate'))) {
2038 # can we get a CreateDate from another tag?
2039 my $ct = $info->get('RIFF', 'DateTimeOriginal');
2040 if (defined $ct) {
2041 $info->set('CreateDate', $ct);
2042 }
2043 }
2044
2045 # for synchronizing the times of non-image files with those of
2046 # related image files, we determine the "image number" of each file.
2047 {
2048 my $number = get_image_number($file);
2049 $info->set( 'image_number', $number ) if defined $number;
2050 }
2051
2052 return $info;
2053 }
2054
2055 # read image/movie metadata from a text file. The metadata is assumed
2056 # to be in JSON or YAML format and to contain tags as produced by
2057 # "exiftool -n -j", optionally (and preferably) also with the "-G"
2058 # option.
2059 sub get_image_info_from_metadata_file {
2060 my ($file) = @_;
2061
2062 my $info;
2063
2064 my $metadata = LoadFile($file);
2065
2066 if (ref($metadata) eq 'ARRAY' && scalar(@{$metadata}) == 1) {
2067 # single-element array, uncover the single element
2068 $metadata = $metadata->[0];
2069 }
2070 if (ref($metadata) eq 'HASH') {
2071 $info = new Image::Synchronize::GroupedInfo;
2072
2073 foreach my $k (keys %{$metadata}) {
2074 my ($group, $tag) = $k =~ /^(?:([^:]+):)?(.*)$/;
2075 if (defined $tag) {
2076 if (exists($all_tags{$k}) || exists($all_tags{$tag})) {
2077 # a tag that interests us
2078 $info->set($group, $tag, $metadata->{$k});
2079 }
2080 }
2081 }
2082 if ($info->tags_count > 0) {
2083 # mark that this information is metadata about another file
2084 $info->set('is_metadata', 1);
2085 }
2086 }
2087 return $info;
2088 }
2089
2090 # extract the image number from the C<$file> name. The image number
2091 # is found as follows: (1) remove the directory part and the file name
2092 # extension from the file name, where the file name extension begins
2093 # at the last period '.'; (2) remove the part after the last digit;
2094 # (3) remove the part up to and including the last letter; (4)
2095 # concatenate the digits that remain. Some examples that yield image
2096 # number 1234: IMG_1234.JPG IMG_1234-extra.txt foo6x1.2_3.4-y.mov
2097 sub get_image_number {
2098 my ($file) = @_;
2099 my $file2 = file($file)->basename; # remove directory part, if any
2100 $file2 =~ s/\.([^.]*)$//; # remove file extension, if any
2101 $file2 =~ s/(\d)\D*$/$1/; # (2)
2102 $file2 =~ s/^.*?[[:alpha:]]([^[:alpha:]]*)$/$1/; # (3)
2103 my $number = join('', split /\D+/, $file2);
2104 return unless defined $number;
2105 return if $number eq '';
2106 return $number + 0;
2107 }
2108
2109 # $gps_offsets = $ims->gps_offsets;
2110 #
2111 # Returns a reference to the offsets of GPS times relative to file
2112 # creation times.
2113 sub gps_offsets {
2114 my ($self) = @_;
2115 return $self->{gps_offsets};
2116 }
2117
2118 # $gpc = $ims->gps_positions;
2119 #
2120 # Returns an C<Image::Synchronize::GpsPositionCollection> object
2121 # representing GPS positions as a function of time, obtained from GPX
2122 # files.
2123 sub gps_positions {
2124 my ($self) = @_;
2125 return $self->{gps_positions};
2126 }
2127
2128 sub has_embedded_timestamp {
2129 my ($image_info) = @_;
2130 foreach my $tag (qw(CreateDate DateTimeOriginal GPSDateTime)) {
2131 my ( $group, $value ) = $image_info->get_context($tag);
2132 return 1 if defined($value) and $group ne 'File' and $group ne '';
2133 }
2134 return 0;
2135 }
2136
2137 sub has_useful_timestamp {
2138 my ($self, $file, $image_info) = @_;
2139 return ( has_embedded_timestamp($image_info)
2140 || defined($self->repository('user_times')->{$file})
2141 || defined($image_info->get('TargetFileModifyDate')));
2142 }
2143
2144 # identify @files that match the $pattern. The match is case
2145 # sensitive, except on case-insensitive operating systems.
2146 sub identify_files {
2147 my ( $pattern, @files ) = @_;
2148 my %original;
2149 my @tfiles;
2150 if ($CASE_TOLERANT) {
2151 # lowercase the file names and pattern if the operating system
2152 # uses case insensitive path names so the match becomes case
2153 # insensitive. Remember the original path names so we can return
2154 # those (the matching ones) instead of the lowercased versions.
2155 $pattern = lc $pattern;
2156 @tfiles = map { lc =~ s|^\./||r } @files;
2157 } else {
2158 @tfiles = map { s|^\./||r } @files;
2159 }
2160 foreach my $i (0..$#files) {
2161 $original{$tfiles[$i]} = $files[$i];
2162 }
2163 $pattern = '*' . $pattern unless $pattern =~ /^\*/;
2164 return map { $original{$_} } match_glob($pattern, @tfiles);
2165 }
2166
2167 # $ims->import_camera_offsets;
2168 #
2169 # Imports camera offsets from the camera offsets file. Returns
2170 # C<$ims>.
2171 sub import_camera_offsets {
2172 my ($self) = @_;
2173 my $path = $self->camera_offsets_path;
2174 if ($path) {
2175 if ( -e $path ) {
2176 log_message("Importing camera offsets from '$path'.\n");
2177 my $data = LoadFile($path);
2178 $self->gps_offsets->parse($data)
2179 or croak "Imported camera offsets are invalid: $@\n";
2180 log_message(
2181 16,
2182 sub {
2183 "Camera Offsets:\n", Dump( $self->gps_offsets->export_data ), "\n";
2184 }
2185 );
2186 } else {
2187 log_message("No camera offsets file found to import from.\n");
2188 }
2189 } else {
2190 log_message("Not importing camera offsets because --offsetspath has an empty value.\n");
2191 }
2192 $self;
2193 }
2194
2195 # initialize_logging
2196 #
2197 # $ims->initialize_logging
2198 #
2199 # Set up logging to a file. Returns <$ims>.
2200 sub initialize_logging {
2201 my ($self) = @_;
2202
2203 # configure logging to standard output
2204 Image::Synchronize::Logger->new(
2205 {
2206 bitflags => $self->option( 'verbose', 1 ),
2207 action => sub { print @_; }
2208 }
2209 )->set_as_default;
2210
2211 # configure logging to the log file
2212 my $logfile = $self->option( 'logfile', 'imsync.log' );
2213 my $ofh;
2214 if ( $self->option('clearlog') ) {
2215 open $ofh, '>', $logfile
2216 or croak "Cannot open '$logfile' for writing: $^E\n";
2217 }
2218 else {
2219 open $ofh, '>>', $logfile
2220 or croak "Cannot open '$logfile' for appending: $^E\n";
2221 }
2222 $ofh->autoflush; # we may be watching the file
2223 $self->{logfh} = $ofh;
2224 $self->{logfile} = $logfile;
2225
2226 default_logger()->set_printer(
2227 {
2228 name => 'file',
2229 bitflags => $self->option( 'verbose', 1 ),
2230 action => sub { print $ofh @_; }
2231 }
2232 );
2233
2234 $self;
2235 }
2236
2237 # initialize_own_xmp_namespace
2238 #
2239 # $ims = $ims->initialize_own_xmp_namespace;
2240 #
2241 # Initialize our own XMP namespace for embedding our own tags into
2242 # image files through Image::ExifTool. Returns C<$ims>.
2243 sub initialize_own_xmp_namespace {
2244 my ($self) = @_;
2245
2246 # to delete all XMP tags using exiftool, use exiftool -XMP:all=
2247 # <files>
2248
2249 {
2250 no warnings; # suppress warning about ..::imsync being used only once
2251
2252 %Image::ExifTool::UserDefined::imsync = (
2253 GROUPS => { 0 => 'XMP', 1 => 'XMP', 2 => 'Image' },
2254 NAMESPACE => { 'imsync' => 'http://imsync.quae.nl/' },
2255 WRITABLE => 'string',
2256 map { $_ => {} } @own_xmp_tags
2257 );
2258
2259 %Image::ExifTool::UserDefined = (
2260
2261 # add new XMP namespace to the Main XMP table
2262 'Image::ExifTool::XMP::Main' => {
2263 imsync => {
2264 SubDirectory => {
2265 TagTable => 'Image::ExifTool::UserDefined::imsync'
2266 },
2267 },
2268 }
2269 );
2270 }
2271
2272 $self;
2273 }
2274
2275 sub enhance_image_info {
2276 my ($self, $file, $info) = @_;
2277
2278 # TODO: remove this when the author no longer needs it to transition
2279 # from exiftime to imsync
2280 if ( defined $info->get('ExiftimeVersion') ) {
2281 log_warn("ExiftimeVersion found in $file.\n");
2282 }
2283
2284 my $count_essential_gps_tags = 0;
2285 foreach (qw(GPSDateTime GPSLongitude GPSLatitude)) {
2286 ++$count_essential_gps_tags if defined $info->get($_);
2287 }
2288 if ( $count_essential_gps_tags
2289 and $count_essential_gps_tags < 3 ) {
2290 log_warn(<<EOD);
2291 File '$file' has some but not all of GPSDateTime,
2292 GPSLongitude, GPSLatitude. Ignoring its GPS information.
2293 EOD
2294 $info->delete($_)
2295 foreach qw(GPSLatitude GPSLongitude GPSAltitude GPSDateTime);
2296 }
2297
2298 $info->set( 'supposedly_utc', 1 ) if is_supposedly_utc($info);
2299
2300 # determine the camera ID.
2301
2302 my $camera_id;
2303 {
2304 my $embedded_camera_id = $info->get('CameraID');
2305
2306 if (defined $embedded_camera_id
2307 and not $self->option('force', 0)) {
2308 # no --force was applied, so just reuse the embedded
2309 # camera ID
2310 $camera_id = $embedded_camera_id;
2311 } else {
2312 # construct a camera ID from the embedded information
2313 $camera_id = camera_id($info);
2314 }
2315 }
2316
2317 # there may not be sufficient information to deduce a camera
2318 # ID in the preferred way; also deduce a fallback camera ID
2319 # that is always available
2320 my $fallback_camera_id = fallback_camera_id( $file );
2321
2322 if (defined $camera_id
2323 and $camera_id eq $fallback_camera_id) {
2324 $camera_id = undef;
2325 }
2326
2327 if ( defined $camera_id ) {
2328 $info->set( 'camera_id', $camera_id );
2329
2330 # remember for later, so we can hopefully substitute real
2331 # camera IDs for some fallback ones.
2332 ++$self->{fallback_to_camera_id}->{$fallback_camera_id}->{$camera_id};
2333 }
2334 $info->set( 'fallback_camera_id', $fallback_camera_id );
2335 }
2336
2337 # $ims->inspect_files(@files);
2338 #
2339 # Inspects the @files. The files are inspected but not modified,
2340 # because we may be able to use information from a later image to
2341 # synchronize the time of an earlier image, but we can't tell until
2342 # we've inspected all files.
2343 #
2344 # Relevant properties are remembered, and files that provide too
2345 # little information (or are found to not be image or movie files) are
2346 # omitted from further consideration.
2347 #
2348 # Returns the object.
2349 sub inspect_files {
2350 my ( $self, @files ) = @_;
2351
2352 my $count_image_files = 0;
2353 my $count_gps_times = 0;
2354 my $count_gps_track_files = 0;
2355
2356 # We display a progress bar to show how far we are in processing all
2357 # files.
2358 my $progressbar = $self->setup_progressbar( scalar(@files), 'Inspect' );
2359
2360 my %gpx_files;
2361
2362 $self->{fallback_to_camera_id} = {};
2363
2364 # Now process the files.
2365 foreach my $file (@files) {
2366
2367 log_message( 2, { name => $file }, sub { "\nInspecting '$file'\n" } );
2368
2369 if ( is_gpx_track($file) ) { # a GPX track
2370 ++$count_gps_track_files;
2371 push @{ $self->{gpx_tracks} }, $file;
2372 $self->{gpx_total_size} += -s $file;
2373 log_message( 2, { name => $file }, " Is a GPX file.\n" );
2374 }
2375 else {
2376 my $info =
2377 $self->get_image_info( $file );
2378
2379 # if we have the creation timestamp only from the file system
2380 # and not from an embedded tag, then we cannot deduce the target
2381 # timestamp from the creation timestamp (because the file
2382 # apparently wasn't created by a camera). We need to be able to
2383 # detect this.
2384 my ( $group, $createdate ) = $info->get_context('CreateDate');
2385 $info->set( 'createdate_was_embedded', 1 )
2386 if defined($createdate)
2387 and $group ne 'File';
2388
2389 if (is_image_or_movie($info)) {
2390 $self->enhance_image_info($file, $info);
2391 ++$count_image_files;
2392 ++$count_gps_times if defined $info->{GPSDateTime};
2393 } else {
2394 my $type;
2395 if (($info->get('MIMEType') // '') =~ m'application/(json|yaml)') {
2396 # the MIMEType is detected based on the file's contents,
2397 # regardless of its file name extension
2398 $type = uc($1);
2399 } elsif ($file =~ /\.ya?ml$/i) {
2400 # but at the time of writing this there is no YAML-specific
2401 # MIMEType yet, so we can also detect YAML files based on
2402 # the file name extension
2403 $type = 'YAML';
2404 }
2405 if ($type) {
2406 my $metainfo = get_image_info_from_metadata_file($file);
2407 if (defined $metainfo) {
2408 $self->enhance_image_info($file, $metainfo);
2409 my @tags = $metainfo->tags;
2410 if (@tags) {
2411 log_message(2, { name => $file },
2412 " Is a $type metadata file.\n" );
2413 # $info->set('is_metadata', 1);
2414 foreach my $tag ($metainfo->tags) {
2415 my ($group, $value) = $metainfo->get_context($tag);
2416 if (not defined($info->get($group, $tag))) {
2417 # this group/tag is not in $info yet
2418 if (exists $time_tags{$tag}) {
2419 $value = Image::Synchronize::Timestamp->new($value);
2420 }
2421 $info->set($group, $tag, $value);
2422 } elsif ($tag eq 'FileModifyDate') {
2423 # for a metadata file, not just its own file
2424 # modification date is of interest (because it may
2425 # need synchronization) but also the file
2426 # modification date that is written inside the
2427 # metadata file, because the latter timestamp may be
2428 # the timestamp of last resort for the target time
2429 # of the metadata file and the file that it applies
2430 # to.
2431 $info->set('TargetFileModifyDate',
2432 Image::Synchronize::Timestamp->new($value));
2433 }
2434 }
2435 ++$count_gps_times if defined $info->{GPSDateTime};
2436 }
2437 }
2438 }
2439 }
2440
2441 $self->{original_info}->{$file} = $info;
2442 log_message( 2, { name => $file }, $info->stringify(' Found ') . "\n" );
2443 }
2444
2445 $progressbar->add;
2446 }
2447 $self->cleanup_progressbar($progressbar);
2448
2449 log_message("\n");
2450 log_message("Found $count_gps_track_files GPS track file(s).\n")
2451 if $count_gps_track_files;
2452 log_message("Found $count_image_files image file(s).\n");
2453 log_message("$count_gps_times file(s) have a GPS timestamp.\n");
2454 return $self;
2455 }
2456
2457 # $ok = is_gpx_track($file);
2458 #
2459 # determines whether the named file is a GPX file, by looking for
2460 # "<gpx " in its first line. This is much faster than trying to parse
2461 # the entire file using an XML parser.
2462 sub is_gpx_track {
2463 my ($file) = @_;
2464 if ( open my $ifh, '<', $file ) {
2465 my $line;
2466 do {
2467 $line = <$ifh>;
2468 } until eof($ifh) or $line =~ /<[^?]/;
2469 close $ifh;
2470 return 1 if defined($line) and $line =~ /<gpx /;
2471 }
2472 else {
2473 log_message( { name => $file },
2474 sub { "Cannot open file '$file' for reading: $^E\n" } );
2475 }
2476 return 0;
2477 }
2478
2479 # $ok = is_image_or_movie($image_info);
2480 #
2481 # Queries whether the C<$image_info> indicates that the source was an
2482 # image or movie file. Returns a true value if yes, a false value if
2483 # no.
2484 sub is_image_or_movie {
2485 my ($image_info) = @_;
2486 return ( $image_info->get('file_type') // '' ) =~ /^image|video$/;
2487 }
2488
2489 # $ok = is_supposedly_utc($image_info);
2490 #
2491 # Queries whether the C<$image_info> indicates an image source whose
2492 # CreateDate is supposedly in UTC instead of in the local timezone.
2493 # Returns a true value if yes, a false value if no.
2494 #
2495 # Such timestamps that are supposed to be in UTC sometimes are not,
2496 # for instance when the image source doesn't know what timezone it is
2497 # in.
2498 sub is_supposedly_utc {
2499 my ($image_info) = @_;
2500
2501 # For a QuickTime file, QuickTime:CreateDate does not include an
2502 # explicit timezone. It is supposed to be in UTC, but sometimes
2503 # isn't. (For example, if the camera doesn't know what timezone it
2504 # is in.) Sometimes QuickTime:CreateDate is in neither UTC nor the
2505 # camera's local timezone.
2506
2507 return defined( $image_info->get( 'QuickTime', 'CreateDate' ) );
2508 }
2509
2510 sub length_of_common_prefix {
2511 my ( $a, $b ) = @_;
2512 my @a = split //, $a;
2513 my @b = split //, $b;
2514 splice @a, scalar(@b);
2515 foreach my $i (0..$#a) {
2516 return $i if $a[$i] ne $b[$i];
2517 }
2518 return scalar(@a);
2519 }
2520
2521 sub length_of_common_directory_prefix {
2522 my ($a, $b) = @_;
2523 my $a2 = file($a)->parent->stringify;
2524 my $b2 = file($b)->parent->stringify;
2525 return length_of_common_prefix($a2, $b2);
2526 }
2527
2528 my %convert_for_writing = (
2529 GPSLatitude => sub {
2530 my ( $tag, $in, $out ) = @_;
2531
2532 # don't set coordinates to abs($in). If the coordinate is
2533 # writting to XMP rather than EXIF, then there is no GPSxRef so
2534 # then the sign on GPSx is needed.
2535 $out->{"${tag}#"} = $in;
2536 $out->{"${tag}Ref#"} = ( $in > 0 ) ? 'N' : 'S';
2537 },
2538 GPSLongitude => sub {
2539 my ( $tag, $in, $out ) = @_;
2540 $out->{"${tag}#"} = $in;
2541 $out->{"${tag}Ref#"} = ( $in > 0 ) ? 'E' : 'W';
2542 },
2543 GPSAltitude => sub {
2544 my ( $tag, $in, $out ) = @_;
2545 $out->{"${tag}#"} = $in;
2546 $out->{"${tag}Ref#"} = ( $in > 0 ) ? 0 : 1;
2547 },
2548 GPSDateTime => sub {
2549 my ( $tag, $in, $out ) = @_;
2550 my $t = Image::Synchronize::Timestamp->new($in)->adjust_to_utc;
2551
2552 # GPSDateStamp and GPSTimeStamp are needed for EXIF.
2553 $out->{GPSDateStamp} = $t->date;
2554 $out->{GPSTimeStamp} = $t->time;
2555
2556 # GPSDateTime is for XMP, in case the GPS coordinates ended up in
2557 # XMP.
2558 $out->{$tag} = "$t";
2559 },
2560 DateTimeOriginal => sub {
2561 my ( $tag, $in, $out ) = @_;
2562
2563 # make sure to also write in XMP
2564 $out->{$tag} = $in;
2565 $out->{ 'XMP:' . $tag } = $in;
2566 },
2567 ImsyncVersion => \&convert_to_xmp,
2568 CameraID => \&convert_to_xmp,
2569 TimeSource => \&convert_to_xmp,
2570 );
2571
2572 sub set_file_modification_time {
2573 my ($self, $file, $time_utc) = @_;
2574 utime 0, $time_utc, $file;
2575 # return $self->backend->SetFileTime($file, undef, $time_utc);
2576 }
2577
2578 # $et->modify_file($file);
2579 #
2580 # Modify the $file as indicated in the information gathered in
2581 # C<$self>. Ensures that there is a backup of the file before
2582 # modifying the original -- and refuses to modify the original
2583 # otherwise.
2584 #
2585 # Returns 1 if the file was modified OK, 2 if the file was written but
2586 # no changes were made, 3 if the file was not modified because no
2587 # backup could be created for it, 4 if there was an error writing the
2588 # file but the metadata could be written to a separate file, 5 if
2589 # there were errors writing the file and writing the separate metadata
2590 # file, and 0 if there were no changes to make.
2591 sub modify_file {
2592 my ( $self, $file ) = @_;
2593
2594 my $new_info = $self->{new_info}->{$file};
2595 my $extra_info = $self->{extra_info}->{$file};
2596 my $changes = $extra_info->get('changes');
2597 return 0 unless defined($changes) and scalar( @{$changes} );
2598
2599 my $status;
2600 my $b = $self->backend;
2601
2602 {
2603 my $size = -s $file;
2604 log_message(
2605 2,
2606 { name => $file },
2607 sub { "Modifying '$file' ($size bytes)\n" }
2608 );
2609 }
2610 if ( $self->option('unsafe') || $self->ensure_backup($file) ) {
2611
2612 $b->SaveNewValues(); # so we can return to a clean slate
2613
2614 # then we treat all requested tags
2615 $b->ExtractInfo($file);
2616
2617 my $changed;
2618 my $found_filemodifydate_tag;
2619 foreach my $tag ( @{$changes} ) {
2620 $found_filemodifydate_tag = 1, next
2621 if $tag eq 'FileModifyDate'; # done separately
2622 my $value = $new_info->get($tag);
2623 my %values;
2624 if ( defined $value ) {
2625 $value = "$value"; # stringify
2626 if ( $convert_for_writing{$tag} ) {
2627 $convert_for_writing{$tag}->( $tag, $value, \%values );
2628 }
2629 else {
2630 $values{$tag} = $value;
2631 }
2632 }
2633 else {
2634 # delete this tag
2635 if ( $convert_for_writing{$tag} ) {
2636
2637 # figure out which tags we need to adjust
2638 # use "dummy" value 0
2639 $convert_for_writing{$tag}->( $tag, 0, \%values );
2640
2641 # set value to undef so tag will get deleted by
2642 # SetNewValue
2643 $values{$_} = undef foreach keys %values;
2644 }
2645 else {
2646 $values{$tag} = undef;
2647 }
2648 }
2649 foreach my $t ( sort keys %values ) {
2650 my ( $success, $error ) = $b->SetNewValue( $t, $values{$t} );
2651 if ($success) {
2652 log_message(
2653 4,
2654 {
2655 name => $file
2656 },
2657 sub {
2658 defined( $values{$t} )
2659 ? " Set $t of '$file' to '" . $values{$t} . "'\n"
2660 : " Removed $t from '$file'\n";
2661 }
2662 );
2663 $changed += $success;
2664 }
2665 else {
2666 log_warn(
2667 defined( $values{$t} )
2668 ? " ExifTool error setting $t of '$file' to '" . $values{$t} . "': $error\n"
2669 : " ExifTool error removing $t from '$file': $error\n"
2670 );
2671 }
2672 }
2673 }
2674
2675 if ( $changed ) {
2676
2677 $status = $b->WriteInfo($file); # write to file
2678 if ( $status == 2 ) {
2679 log_warn(" '$file' written but no changes made.\n");
2680 }
2681 if ( $status == 1 ) {
2682 my $warning = $b->GetValue('Warning');
2683 if ($warning) {
2684
2685 # We suppress warnings about the FileName encoding not being
2686 # specified. We don't invent or modify the byte values of the
2687 # file name, so the encoding should be unimportant as long as
2688 # Image::ExifTool doesn't modify any file name byte values,
2689 # either.
2690 my @warnings = split /\n/, $warning;
2691 @warnings = grep { !/
2692 FileName[ ]encoding[ ]not[ ]specified
2693 |Maker[ ]notes[ ]could[ ]not[ ]be[ ]parsed
2694 /x } @warnings;
2695 if (@warnings) {
2696 $warning = join( "\n", @warnings );
2697 log_warn(" Writing '$file' succeeded with ExifTool warning: $warning\n");
2698 }
2699 }
2700 }
2701 else {
2702 # Writing via Image::ExifTool failed. We write the tags to a
2703 # corresponding metadata file instead. If the writing failed
2704 # because Image::ExifTool does not support the file type
2705 # (yet), then we report that problem only for the first file
2706 # of that type -- to avoid numerous otherwise unavoidable
2707 # warnings.
2708
2709 my @messages;
2710 my $error = $b->GetValue('Error');
2711 if ($error =~ /(Writing of (?:.*?) files is not yet supported)/) {
2712 state %suppress;
2713 if (not $suppress{$1}) {
2714 push @messages,
2715 " Writing '$file' failed, ExifTool reports:\n $error\n",
2716 " This message is suppressed for similar following files.\n";
2717 # mark that we reported this problem already
2718 $suppress{$1} = 1;
2719 }
2720 } else {
2721 push @messages,
2722 join( ', ',
2723 " Writing '$file' failed",
2724 ( $error ? "ExifTool reports:\n $error" : () ))
2725 . "\n"
2726 ;
2727 }
2728
2729 # write the tags to a metadata file instead, if any of the
2730 # tags have changed relative to what is in the metadata file
2731 # already
2732 my $metafile = "${file}.yaml";
2733 my $old_metainfo;
2734 if (-r $metafile) {
2735 $old_metainfo = get_image_info_from_metadata_file($metafile);
2736 } else {
2737 $old_metainfo = new Image::Synchronize::GroupedInfo;
2738 }
2739 my %changes;
2740 $changed = $self->get_changes($file, $old_metainfo, $new_info, $extra_info,
2741 undef, \%changes);
2742 if ($changed) {
2743 if (exists $changes{FileModifyDate}
2744 and scalar keys %changes == 1) {
2745 # only FileModifyDate needs to be changed
2746 $self->set_file_modification_time($metafile,
2747 $new_info->get('FileModifyDate')->time_utc);
2748 $status = 4;
2749 } else {
2750 # the contents of the metadata file needs changes
2751 my %values_for_export;
2752 foreach my $tag ($new_info->tags) {
2753 foreach my $group ($new_info->groups($tag)) {
2754 my $v = $new_info->get($group, $tag);
2755 $values_for_export{$group? "$group:$tag": $tag} = "$v";
2756 }
2757 }
2758 if (DumpFile($metafile, \%values_for_export)) {
2759 push @messages, " Wrote metadata to '$metafile'.\n";
2760 $self->set_file_modification_time($metafile,
2761 $new_info->get('FileModifyDate')->time_utc);
2762 $status = 4;
2763 } else {
2764 push @messages, " Writing metadata to '${file}.yaml' failed: $@\n";
2765 $status = 5;
2766 }
2767 }
2768 } else {
2769 $changed = 0; # no changes (perhaps excluding FileModifyDate) after all
2770 }
2771 if (@messages) {
2772 log_message(join("\n", @messages) . "\n");
2773 }
2774 }
2775 }
2776
2777 if ( $changed or $found_filemodifydate_tag ) {
2778 # Updating the FileModifyDate (the file system modification
2779 # timestamp) via WriteInfo doesn't always work; for example,
2780 # ExifTool 10.63 does not support modifying AVI files.
2781 #
2782 # Unfortunately, on Microsoft Windows setting the file
2783 # modification time via utime() is problematic, because the zero
2784 # point depends on the timzone settings (at least it depends on
2785 # whether Daylight Savings Time is in effect when this code is
2786 # executed).
2787 #
2788 # The author of Image::ExifTool faced and overcame the same
2789 # problem, and using the resulting Image::ExifTool functionality
2790 # worked for me on Windows 10 but no longer on Windows 11.
2791 #
2792 # Then I found Win32::UTCFileTime, which allows overriding utime
2793 # with a version that hopefully works on Windows 11.
2794
2795 my $cur_mt = -M $file;
2796 my $fmt = $new_info->get('FileModifyDate');
2797 my $success = $self->set_file_modification_time($file, $fmt->time_utc);
2798
2799 if ($success) {
2800 my $new_mt = -M $file;
2801 if ($new_mt == $cur_mt) {
2802 log_message( 4, { name => $file },
2803 sub { " Setting FileModifyDate of '$file' to '$fmt' FAILED\n" } );
2804 $success = 0;
2805 }
2806 }
2807 if ($success) {
2808 log_message( 4, { name => $file },
2809 sub { " Set FileModifyDate of '$file' to '$fmt'\n" } );
2810 $changed += $success;
2811 } else {
2812 log_warn( " Error setting FileModifyDate of '$file' to '$fmt'\n");
2813 }
2814 } else {
2815 # TODO: don't report any changes for this file, either
2816 }
2817 $b->RestoreNewValues(); # return to clean slate
2818 }
2819 else {
2820 log_error(" Could not create backup file for '$file': not modified.\n");
2821 $status = 3;
2822 }
2823 return $status;
2824 }
2825
2826 # $et->modify_files;
2827 #
2828 # Makes another pass over all of the files of interest left over from
2829 # the previous pass. During this pass, the target timestamp for each
2830 # file is applied to that file, if the 'modify' option is true.
2831 sub modify_files {
2832 my ($self) = @_;
2833 return $self unless $self->option('modify'); # nothing to do
2834 my $totalsize = 0;
2835 my @files = grep {
2836 my $n = $self->{extra_info}->{$_}->get('needs_modification');
2837 $totalsize += -s $_ if $n;
2838 $n;
2839 } keys %{ $self->{extra_info} };
2840 return $self unless @files;
2841
2842 @files = sort @files;
2843
2844 if (@files) {
2845 my $count_modified = 0;
2846
2847 my $progressbar = $self->setup_progressbar( $totalsize, 'Modify' );
2848
2849 foreach my $file (@files) {
2850 my $ok = $self->modify_file($file);
2851 $progressbar->add( -s $file );
2852 ++$count_modified if $ok;
2853 }
2854 $self->cleanup_progressbar($progressbar);
2855 if ($count_modified) {
2856 log_message("$count_modified file(s) changed.\n");
2857 }
2858 $self->cleanup_progressbar($progressbar);
2859 }
2860 return $self;
2861 }
2862
2863 # $value = $ims->option($name);
2864 # $value = $ims->option($name, $default);
2865 #
2866 # Retrieves the value of the option with the specified C<$name>.
2867 # Accesses the options specified in the constructor and through
2868 # L</set_option>. If the option is not defined, then returns
2869 # C<$default> if specified, and C<undef> otherwise.
2870 sub option {
2871 my ( $self, $option, $default ) = @_;
2872 return $self->{options}->{$option} // $default;
2873 }
2874
2875 sub parse_coordinate {
2876 my ($value) = @_;
2877 return unless defined $value;
2878 my $result;
2879 if ( looks_like_number($value) ) {
2880 $result = $value;
2881 }
2882 else {
2883 my @components;
2884 my $rest = $value;
2885
2886 # may be signed
2887 my $sign = +1;
2888 if ( $rest =~ /^([-+])(.*)$/ ) {
2889 $sign = -1 if $1 eq '-';
2890 $rest = $2;
2891 }
2892
2893 # a possibly fractional number at the end
2894 if ( $rest =~ /^(.*?)(\d+(?:\.\d+)?)\D*$/ ) {
2895 unshift @components, $2;
2896 $rest = $1;
2897 }
2898 else { # not valid input
2899 return undef;
2900 }
2901 if ($rest) {
2902 unshift @components, split /\D+/, $rest;
2903 }
2904 $result = 0;
2905 while (@components) {
2906 my $next_component = pop(@components);
2907 next unless defined $next_component;
2908 $result = $result / 60 + $next_component;
2909 }
2910 $result *= $sign;
2911 }
2912 return $result;
2913 }
2914
2915 # convert the path to Perl style
2916 sub perlish_path {
2917 my ($path) = @_;
2918 return file($path)->as_foreign('Unix')->stringify;
2919 }
2920
2921 # %display_cameras = $ims->prepare_display_cameras;
2922 #
2923 # Determines a two-letter abbreviation for all of the camera IDs seen.
2924 # The abbreviation consists of the first two characters of the camera
2925 # ID, unless that conflicts with an abbreviation assigned earlier.
2926 # Progressively more distant abbreviations are tried until one is
2927 # found that isn't used yet. If all else fails, then '**' is used, so
2928 # that is the only abbreviation that might be assigned to multiple
2929 # cameras.
2930
2931 sub prepare_display_cameras {
2932 my ($self) = @_;
2933 my %display_cameras;
2934 my %used;
2935 foreach my $camera ( sort keys %{ $self->{camera_ids} } ) {
2936 my $candidate = uc( substr( $camera, 0, 2 ) );
2937 $candidate .= ' ' x ( 2 - length($candidate) ) if length($candidate) < 2;
2938 if ( $used{$candidate} ) { # try first letter with digit suffix
2939 my $prefix = uc( substr( $camera, 0, 1 ) );
2940 for my $i ( 0 .. 9 ) {
2941 $candidate = $prefix . $i;
2942 last unless $used{$candidate};
2943 }
2944 }
2945 if ( $used{$candidate} ) { # try first letter with lowercase letter suffix
2946 my $prefix = uc( substr( $camera, 0, 1 ) );
2947 for my $a ( 'a' .. 'z' ) {
2948 $candidate = $prefix . $a;
2949 last unless $used{$candidate};
2950 }
2951 }
2952 if ( $used{$candidate} ) { # try first letter with uppercase letter suffix
2953 my $prefix = uc( substr( $camera, 0, 1 ) );
2954 for my $a ( 'A' .. 'Z' ) {
2955 $candidate = $prefix . $a;
2956 last unless $used{$candidate};
2957 }
2958 }
2959 if ( $used{$candidate} ) { # try any two lowercase letters
2960 $candidate = 'aa';
2961 do {
2962 last unless $used{$candidate};
2963 ++$candidate;
2964 } while length($candidate) == 2;
2965 }
2966 if ( $used{$candidate} ) { # give up
2967 $candidate = '**';
2968 }
2969 $display_cameras{$camera} = $candidate;
2970 $used{$candidate} = 1;
2971 }
2972 return %display_cameras;
2973 }
2974
2975 sub process_gpx_track {
2976 my ( $self, $file ) = @_;
2977 my $gps_point_count;
2978 my $time_parse_problem_count;
2979 my $gpc = $self->gps_positions;
2980 my ( $mintime, $maxtime );
2981 my $track_count = 0;
2982 my $twig = XML::Twig->new(
2983 twig_handlers => {
2984 trkseg => sub {
2985 my ( $twig, $section ) = @_;
2986 ++$track_count;
2987 my $id = "$file-$track_count";
2988 foreach my $point ( $section->children('trkpt') ) {
2989 my $lat = $point->att('lat');
2990 my $lon = $point->att('lon');
2991 my $time = $point->first_child_text('time');
2992 next unless $time;
2993 $time = Image::Synchronize::Timestamp->new($time);
2994 unless ( defined $time ) {
2995 ++$time_parse_problem_count;
2996 next;
2997 }
2998 $mintime = $time
2999 if not( defined $mintime )
3000 or $time < $mintime;
3001 $maxtime = $time
3002 if not( defined $maxtime )
3003 or $time > $maxtime;
3004 my $ele = $point->first_child_text('ele');
3005 ++$gps_point_count;
3006 $gpc->add( $time->time_utc, $lat, $lon, $ele, $id,
3007 scope_for_file($file) )
3008 if $time;
3009 }
3010 }
3011 }
3012 );
3013 log_message( 2, { name => $file }, "Reading '$file'.\n" );
3014 if ( $twig->safe_parsefile($file) ) {
3015 if ( defined $gps_point_count ) {
3016 log_message(
3017 2,
3018 { name => $file },
3019 sub {
3020 my $min = $mintime->display_utc;
3021 my $max = $maxtime->display_utc;
3022 my $common_prefix = common_prefix( '[-T]', $min, $max );
3023 $max =~ s/^\Q$common_prefix\E//;
3024 " Read $gps_point_count positions ($min/$max).\n";
3025 }
3026 );
3027 }
3028 log_message(
3029 "Could not parse $time_parse_problem_count times from GPX file '$file'\n"
3030 ) if defined $time_parse_problem_count;
3031 return 1;
3032 }
3033 else {
3034 # some error
3035 log_message("Problem parsing GPX tracks from $file: $@\n");
3036 return 0;
3037 }
3038 }
3039
3040 sub process_summer_wintertime {
3041 my ($self) = @_;
3042 my %jumps = ( summertime => +1, wintertime => -1 );
3043 my @files = keys %{ $self->{original_info} };
3044 foreach my $jump ( keys %jumps ) {
3045 my $target = $self->option($jump);
3046 my $match;
3047 if ($target) {
3048 my @matches = identify_files( $target, @files );
3049 if (@matches) {
3050 $self->repository('jump')->{ $matches[0] } = $jumps{$jump};
3051 }
3052 else {
3053 croak "--$jump pattern matches no files.\n";
3054 }
3055 }
3056 }
3057 return $self;
3058 }
3059
3060 # (FILE_END|TIMESTAMP(/TIMESTAMP)?)=CAMERAID
3061 sub process_user_camera_ids {
3062 my ( $self, @files ) = @_;
3063 my $action_cref = sub {
3064 my ($rhs) = @_;
3065
3066 # strip surrounding whitespace
3067 $rhs = $1 if $rhs =~ /^\s*(.*?)\s*$/;
3068 if ($rhs) {
3069 return $rhs;
3070 }
3071 else {
3072 log_warn("None or an empty camera ID was specified; ignored.\n");
3073 return;
3074 }
3075 };
3076 return $self->process_user_items( 'cameraid', 'user_camera_ids',
3077 $action_cref, 0, @files );
3078 }
3079
3080 # t a r g e t = s o u r c e
3081 # (FILE_END|TIMESTAMP(/TIMESTAMP)?)=(LOCATION|FILE_END)
3082 sub process_user_locations {
3083 my ($self) = @_;
3084 my $action_cref = sub {
3085 my ( $rhs, $file ) = @_;
3086 my @sources;
3087 if ($rhs) {
3088 @sources = identify_files( $rhs, keys %{ $self->{original_info} } );
3089 if ( @sources > 1 ) {
3090 log_warn("--location RHS '$rhs' matches more than one file; "
3091 . "ignored.\n");
3092 return;
3093 }
3094 elsif ( @sources == 0 ) { # is it a location?
3095 my ( $latitude, $longitude, $altitude ) = split /,/, $rhs;
3096 $latitude = parse_coordinate($latitude);
3097 $longitude = parse_coordinate($longitude);
3098 if ( defined($latitude) and defined($longitude) ) {
3099 return [ $latitude, $longitude, add_maybe($altitude) ];
3100 }
3101 }
3102 if ( @sources == 1 ) {
3103 my $source_info = $self->{original_info}->{ $sources[0] };
3104 if ( defined $source_info ) {
3105 return [
3106 $source_info->get('GPSLatitude'),
3107 $source_info->get('GPSLongitude'),
3108 add_maybe( $source_info->get('GPSAltitude') )
3109 ];
3110 }
3111 }
3112 } else { # empty rhs -- reset position
3113 return [];
3114 }
3115 log_warn("Could not resolve --location RHS '$rhs'.\n");
3116 return;
3117 };
3118 return $self->process_user_items( 'location', 'user_locations',
3119 $action_cref, 1 );
3120 }
3121
3122 # (FILE_END|TIMESTAMP(/TIMESTAMP)?)=(TIME|OFFSET|FILE_END)
3123 sub process_user_times {
3124 my ($self) = @_;
3125 my $action_cref = sub {
3126 my ( $rhs, $file, $info ) = @_;
3127 my $value;
3128 if ( looks_like_number($rhs) ) { # offset in seconds
3129 my $createdate = $info->get('CreateDate');
3130 if ( defined $createdate ) {
3131 $value = $createdate->clone->adjust_to_local_timezone + $rhs;
3132 }
3133 else {
3134 # no CreateDate; cannot apply offset
3135 log_warn(
3136 "Cannot apply --time offset for $file because it lacks an embedded CreateDate.\n"
3137 );
3138 }
3139 }
3140 elsif (
3141 $rhs
3142 and $rhs =~ /^
3143 (?<sign>[-+])?
3144 (?:(?<year>\d+)y)?
3145 (?:(?<day>\d+)d)?
3146 (?:(?<hour>\d+)h)?
3147 (?:(?<minute>\d+)m)?
3148 (?:(?<second>[\d.]+)s)?
3149 $/x
3150 )
3151 { # multi-unit offset
3152 # TODO: support timezone offset
3153 $value = ( $+{year} // 0 ) * 365;
3154 $value = ( $value + ( $+{day} // 0 ) ) * 24;
3155 $value = ( $value + ( $+{hour} // 0 ) ) * 60;
3156 $value = ( $value + ( $+{minute} // 0 ) ) * 60;
3157 $value += ( $+{second} // 0 );
3158 $value = -$value if ( $+{sign} // '+' ) eq '-';
3159 my $createdate = $info->get('CreateDate');
3160 if ( defined $createdate ) {
3161 $value = $createdate->clone->adjust_to_local_timezone + $value;
3162 }
3163 else {
3164 # no CreateDate; cannot apply offset
3165 log_warn(
3166 "Cannot apply --time offset for $file because it lacks an embedded CreateDate.\n"
3167 );
3168 }
3169 }
3170 elsif ( $rhs =~ /^(\d+:\d+(?::\d+)?)$/ ) { # clock time
3171 # TODO: support timezone offset
3172 my $createdate = $info->get('CreateDate');
3173 if ( defined $createdate ) {
3174 $value = Image::Synchronize::Timestamp->new( $rhs, $createdate );
3175 }
3176 else {
3177 # no CreateDate; cannot apply offset
3178 log_warn(
3179 "Cannot apply --time clock time for $file because it lacks an embedded CreateDate.\n"
3180 );
3181 }
3182 }
3183 elsif ( $rhs =~ /^(\d+-\d+-\d+\D\d+:\d+(?::\d+)?)$/
3184 && defined($value = Image::Synchronize::Timestamp->new( $rhs )) ) {
3185 # clock date/time
3186 }
3187 else { # path end
3188 my @files = keys %{ $self->{original_info} };
3189 @files = identify_files( $rhs, @files );
3190 if (@files) {
3191 if ( @files > 1 ) {
3192 croak "--time RHS value matched by multiple files: "
3193 . join( ', ', sort @files ) . "\n";
3194 }
3195 else {
3196 $value = $files[0];
3197 $value = $self->{original_info}->{$value}->get('DateTimeOriginal');
3198 croak "--time RHS file '$value' had no DateTimeOriginal.\n"
3199 unless defined $value;
3200 }
3201 }
3202 else { # timestamp
3203 $value = Image::Synchronize::Timestamp->new($rhs);
3204 if ( defined $value ) {
3205 $value->adjust_to_local_timezone
3206 unless $value->has_timezone_offset;
3207 }
3208 else {
3209 log_error(
3210 "--time value $rhs does not have a valid format (for $file).\n");
3211 }
3212 }
3213 }
3214 return $value;
3215 };
3216 return $self->process_user_items( 'time', 'user_times', $action_cref, 1 );
3217 }
3218
3219 # process the 'follow' option.
3220 #
3221 # Returns the object.
3222 sub process_follow {
3223 my ( $self, @files ) = @_;
3224 my $count = 0;
3225 my $action_cref = sub {
3226 my ($item) = @_;
3227 $self->{follow}->{$item} = 1;
3228 ++$count;
3229 return undef; # don't need to remember anything else
3230 };
3231 $self->process_user_items( 'follow', undef, $action_cref, 1, @files );
3232 if ($count) {
3233 default_logger()
3234 ->set_printer_condition( '', 'follow',
3235 sub { $_ && $self->{follow}->{ $_->{name} } } );
3236 }
3237 return $self;
3238 }
3239
3240 # $self->process_user_items($option, $repository, $action,
3241 # $defer_time_based, @files);
3242 #
3243 # Processes the structured value of a command-line option.
3244 #
3245 # C<$option> names the command-line option whose value to process. The
3246 # value identifies files and the data to associate with them.
3247 #
3248 # C<$repository> names the repository to which to append the results of
3249 # processing the command-line option value. The repository must be a
3250 # hash, and is created if it does not yet exist.
3251 #
3252 # C<$action> is a CODE reference through which the command-line option
3253 # value is processed.
3254 #
3255 # C<$defer_time_based> says whether to defer processing of time-based
3256 # filters (i.e., timestamps or time ranges, see below). If true, then
3257 # the C<$action> is performed on the hash value and the timestamp or
3258 # time range and the outcome of the action are stored in the
3259 # repository for later use. If false, then the time-based filter is
3260 # applied to the C<CreateDate> of the files and the C<$action> is
3261 # performed on the matching files.
3262 #
3263 # C<@files> are the files to search through. If none are specified,
3264 # then the files to search through are those for which information has
3265 # been gathered through L</inspect_files>.
3266 #
3267 # The value of command-line option C<$option> may be a HASH or an ARRAY
3268 # reference. The hash keys or array elements are scalars that identify
3269 # the files with which to associate the results.
3270 #
3271 # If any of the files to search through have a path that ends with the
3272 # hash key or array element, then those files are the targets for that
3273 # hash key or array element.
3274 #
3275 # Otherwise, if the hash key or array element can be parsed as a
3276 # timestamp (L<Image::Synchronize::Timestamp>) or time range
3277 # (L<ImsyncTimerange>), and if C<$defer_time_based> is false, then the
3278 # files whose C<CreateDate> is equal to the timestamp or falls within
3279 # the time range are the targets.
3280 #
3281 # If the value of the command-line option is a HASH reference, then
3282 # the results are obtained through
3283 #
3284 # $result = $action->($value, $file, $info);
3285 #
3286 # for each target C<$file>, with C<$value> the hash value and C<$info>
3287 # the image information of the target file.
3288 #
3289 # If the value of the command-line option is an ARRAY reference, then
3290 # the results are obtained through
3291 #
3292 # $result = $action->($file, $info);
3293 #
3294 # for each target C<$file>.
3295 #
3296 # In both cases, if the C<$result> is defined, then it is stored in the
3297 # repository hash as the value for the key equal to C<$file>.
3298 #
3299 # Returns C<$self>.
3300 sub process_user_items {
3301 my ( $self, $option, $repository, $action, $defer_time_based, @files ) = @_;
3302 my $in_spec = $self->option($option);
3303 my $out_repository = $self->repository($repository) if defined $repository;
3304 if ($in_spec) {
3305 my @keys;
3306 if ( ref $in_spec eq 'HASH' ) {
3307 @keys = keys %{$in_spec};
3308 }
3309 elsif ( ref $in_spec eq 'ARRAY' ) {
3310 @keys = @{$in_spec};
3311 }
3312 else {
3313 croak "Argument should be array ref or hash ref but is '" . ref($in_spec)
3314 . "\n";
3315 }
3316 unless (@files) {
3317 @files = sort keys %{ $self->{original_info} };
3318 }
3319 foreach my $key (@keys) {
3320
3321 # $key is FILE_END or TIMESTAMP or TIMESTAMP/TIMESTAMP. First
3322 # see if the end of the path name of any file matches the key.
3323 my @targets = identify_files( $key, @files );
3324 unless (@targets) {
3325
3326 # then check if the key is a timestamp or time range
3327 my $t;
3328 if ( $key =~ m|/| ) {
3329 $t = Image::Synchronize::Timerange->new($key);
3330 }
3331 else {
3332 $t = Image::Synchronize::Timestamp->new($key);
3333 }
3334 if ( defined $t ) {
3335 if ($defer_time_based) {
3336 # remember for later use, when the target times are known
3337 my $rhs = $action->($in_spec->{$key});
3338 push @{$out_repository->{''}}, [$t, $rhs];
3339 } else {
3340 @targets = $self->find_info_targets($t);
3341 }
3342 }
3343 }
3344 unless (@targets) {
3345 log_warn("Target '$key' does not match any of the "
3346 . "specified files; ignored.\n") unless $defer_time_based;
3347 next;
3348 }
3349 foreach my $target (@targets) {
3350 my $rhs;
3351 if ( ref $in_spec eq 'HASH' ) {
3352 $rhs = $action->(
3353 $in_spec->{$key}, $target, $self->{original_info}->{$target}
3354 );
3355 }
3356 else {
3357 $rhs = $action->( $target, $self->{original_info}->{$target} );
3358 }
3359 if ( defined($out_repository) && defined($rhs) ) {
3360 $out_repository->{$target} = $rhs;
3361 } # end if defined($out_repository) && defined($rhs)
3362 } # end foreach my $target
3363 } # end foreach my $key
3364 } # end if $in_spec
3365 return $self;
3366 }
3367
3368 sub read_gpx_tracks {
3369 my ($self) = @_;
3370 if ( $self->{gpx_total_size} ) {
3371 my $progressbar =
3372 $self->setup_progressbar( $self->{gpx_total_size}, 'GPX' );
3373 foreach my $file ( @{ $self->{gpx_tracks} } ) {
3374 $self->process_gpx_track($file);
3375 $progressbar->add( -s $file );
3376 }
3377 $self->cleanup_progressbar($progressbar);
3378 }
3379 return $self;
3380 }
3381
3382 # remove the tags that this application added
3383 sub remove_our_tags {
3384 my ( $self, @files ) = @_;
3385
3386 my $b = $self->backend;
3387 my $progressbar =
3388 $self->setup_progressbar( scalar(@files), 'Remove Own Tags' );
3389 my $count = 0;
3390 foreach my $file (@files) {
3391 my $image_info = $self->get_image_info($file);
3392 if ( has_embedded_timestamp($image_info) ) {
3393 $b->ExtractInfo($file);
3394 $b->SaveNewValues(); # so we can return to a clean slate
3395
3396 # We don't include XMP:DateTimeOriginal even though the current
3397 # application may have written it, because it might also have been
3398 # written by some other application. We invented the following
3399 # tags, but not DateTimeOriginal.
3400 my @tags_to_remove = map { "XMP:$_" } @own_xmp_tags;
3401
3402 # if TimeSource is not equal to GPS, then we can remove the GPS tags
3403 my $timesource = $image_info->get('TimeSource');
3404 if ( not( defined $timesource ) or $timesource ne 'GPS' ) {
3405 # GPSDateTime is in this list because Image::ExifTool writes
3406 # it in the XMP namespace when it writes GPSDateStamp and
3407 # GPSTimeStamp in the EXIF namespace
3408 push @tags_to_remove, qw(
3409 GPSAltitude
3410 GPSAltitudeRef
3411 GPSDateStamp
3412 GPSDateTime
3413 GPSLatitude
3414 GPSLatitudeRef
3415 GPSLongitude
3416 GPSLongitudeRef
3417 GPSTimeStamp
3418 );
3419 }
3420
3421 my $changed = 0;
3422 foreach my $tag (@tags_to_remove) {
3423 {
3424 my $v = $b->GetValue($tag);
3425 next if not defined $v;
3426 next if ref($v) eq 'HASH' and not( scalar keys %{$v} );
3427 next if ref($v) eq 'ARRAY' and not( scalar @{$v} );
3428 }
3429 my ( $success, $error ) = $b->SetNewValue( $tag, undef );
3430 if ($success) {
3431 log_message(
3432 4,
3433 {
3434 name => $file
3435 },
3436 sub { "Removed $tag from '$file'\n"; }
3437 );
3438 $changed += $success;
3439 }
3440 else {
3441 log_warn(" Error removing $tag from '$file': $error\n");
3442 }
3443 }
3444 if ($changed) {
3445
3446 # Also update the FileModifyDate (the file system modification
3447 # timestamp), because updating any embedded information changes
3448 # that timestamp to the current time.
3449 my $value = $image_info->get('FileModifyDate');
3450 $value = "$value"; # stringify
3451 my ( $success, $error ) =
3452 $b->SetNewValue( 'FileModifyDate', $value, Protected => 1 );
3453 if ($success) {
3454 log_message(
3455 4,
3456 {
3457 name => $file
3458 },
3459 sub { " Set FileModifyDate of '$file' to '$value'\n" }
3460 );
3461 $changed += $success;
3462 }
3463 else {
3464 log_warn(
3465 " Error setting FileModifyDate of '$file' to '$value': $error\n");
3466 }
3467
3468 my $status = $b->WriteInfo($file);
3469 log_warn(" '$file' written but no changes made.\n") if $status == 2;
3470 if ( $status == 1 ) {
3471 my $warning = $b->GetValue('Warning');
3472 if ($warning) {
3473
3474 # We suppress warnings about the FileName encoding not being
3475 # specified. We don't invent or modify the byte values of the
3476 # file name, so the encoding should be unimportant as long as
3477 # Image::ExifTool doesn't modify any file name byte values,
3478 # either.
3479 my @warnings = split /\n/, $warning;
3480 @warnings = grep { !/FileName encoding not specified/ } @warnings;
3481 if (@warnings) {
3482 $warning = join( "\n", @warnings );
3483 log_warn(" Writing '$file' succeeded with warning: $warning\n");
3484 }
3485 }
3486 ++$count;
3487 }
3488 else {
3489 my $error = $b->GetValue('Error');
3490 log_warn(
3491 join( ', ',
3492 " Writing '$file' failed",
3493 ( $error ? "Error: $error" : () ) )
3494 . "\n"
3495 );
3496 $status = 4;
3497 }
3498 }
3499 $b->RestoreNewValues(); # return to clean slate
3500 }
3501 $progressbar->add;
3502 }
3503 log_message("Removed our own tags from $count file(s).\n");
3504 $self->cleanup_progressbar($progressbar);
3505 return $self;
3506 }
3507
3508 # removes tags related to C<@tags> from C<$image_info>.
3509 #
3510 # Image::ExifTool::ImageInfo returns a hash map that may contain
3511 # multiple keys for a requested tag, depending on how many instances
3512 # of the requested information were included in the target file. If
3513 # there are multiple keys, then some or all of them end with a
3514 # parenthesized index number. This sub removes keys equal to the
3515 # specified @tags, and also keys equal to the tag followed by a
3516 # parenthesized index number.
3517 sub remove_tag {
3518 my ( $image_info, @tags ) = @_;
3519 foreach my $tag (@tags) {
3520 delete $image_info->{$_}
3521 foreach grep /^\Q$tag\E(?: \(\d+\))?$/, keys %{$image_info};
3522 }
3523 return $image_info;
3524 }
3525
3526 # $ims->report;
3527 #
3528 # Prints a report about the processed files. Returns C<$ims>.
3529 sub report {
3530 my ($self) = @_;
3531
3532 my $reportlevel = $self->option( 'reportlevel', 1 );
3533 return $self unless $reportlevel;
3534
3535 my @report_these = keys %{ $self->{extra_info} };
3536 @report_these =
3537 grep { $self->{extra_info}->{$_}->get('needs_modification') } @report_these
3538 if $reportlevel == 1;
3539 return $self unless @report_these;
3540
3541 my %display_cameras = $self->prepare_display_cameras;
3542
3543 my $common_prefix_path;
3544 $common_prefix_path = common_prefix_path(@report_these);
3545
3546 my $modify = $self->option('modify');
3547 my $modality = $modify ? '' : 'would be ';
3548 log_message(<<EOD);
3549
3550 REPORT:
3551 G: embedded GPS timestamp (UTC)
3552 E: embedded original timestamp
3553 O: other embedded tags
3554 M: file modification time
3555 P: embedded GPS position
3556 F: minimum --force for modification
3557 =: ${modality}unchanged
3558 *: ${modality}modified
3559 +: ${modality}added
3560 !: ${modality}removed
3561 -: absent
3562
3563 Cm: camera code from following table
3564 Cm Camera ID
3565 --|-------------------------
3566 EOD
3567 my %cameras = reverse %display_cameras;
3568 foreach my $id ( sort keys %cameras ) {
3569 my $name = $cameras{$id} eq '?' ? 'UNKNOWN' : $cameras{$id};
3570 log_message(" $id $name\n");
3571 }
3572 log_message("\n");
3573 log_message( <<EOD );
3574 M Offset source:
3575 g: embedded GPS timestamp
3576 s: embedded creation timestamp & other images
3577 o: embedded original timestamp
3578 c: embedded creation timestamp
3579 t: --time
3580 m: metadata target file modification timestamp
3581 n: other file with same number
3582
3583 EOD
3584
3585 log_message(
3586 sprintf(
3587 "GEOMPF Cm %-25s %-10s %-8s %s\n",
3588 'Target Time', 'M Offset', 'c Offset', 'File'
3589 )
3590 );
3591 log_message( '------|--|'
3592 . ( '-' x 25 ) . '|'
3593 . ( '-' x 10 ) . '|'
3594 . ( '-' x 8 ) . '|'
3595 . ( '-' x 8 )
3596 . "\n" );
3597
3598 my $min_offset;
3599 my $max_offset;
3600 my $count_nonzero_offset = 0;
3601 foreach my $file (
3602 sort {
3603 timestamp_for_sorting( $self->{original_info}->{$a},
3604 $self->{new_info}->{$a} )
3605 <=> timestamp_for_sorting( $self->{original_info}->{$a},
3606 $self->{new_info}->{$b} )
3607 or ( $self->{original_info}->{$a}->get('image_number') // -1 )
3608 <=> ( $self->{original_info}->{$b}->get('image_number') // -1 )
3609 } @report_these
3610 )
3611 {
3612 my $info = $self->{original_info}->{$file};
3613 my $new_info = $self->{new_info}->{$file};
3614 my $extra_info = $self->{extra_info}->{$file};
3615 my $short;
3616 ( $short = $file ) =~ s/^(\Q$common_prefix_path\E)//o;
3617 $short =~ s{^\./}{};
3618
3619 my $is_metainfo_file = $info->get('is_metadata');
3620
3621 # G
3622 log_message( report_letter( 'GPSDateTime', $info, $new_info ) );
3623
3624 # E
3625 log_message( report_letter( 'DateTimeOriginal', $info, $new_info ) );
3626
3627 # O
3628 if ($is_metainfo_file) {
3629 log_message('=');
3630 } else {
3631 my @letters = map { report_letter( $_, $info, $new_info ) }
3632 ( 'XMP:DateTimeOriginal', map { "XMP:$_" } @own_xmp_tags );
3633 my $combined = $letters[0];
3634 foreach my $i ( 1 .. $#letters ) {
3635 if ( $letters[$i] ne $combined ) {
3636 if ( $combined eq '=' ) {
3637 $combined = $letters[$i];
3638 }
3639 else {
3640 $combined = '*';
3641 }
3642 }
3643 }
3644 log_message($combined);
3645 }
3646
3647 # M
3648 log_message( report_letter( 'FileModifyDate', $info, $new_info ) );
3649
3650 # P
3651 {
3652 my $letter;
3653 if ($extra_info->get('position_change')) {
3654 # position was present in old and new, and the new position is
3655 # sufficiently different that the difference shows up in the
3656 # EXIF tags
3657 $letter = '*';
3658 } else {
3659 # a position wasn't present in old or new, or it was present
3660 # in both and hasn't changed significantly. The same then
3661 # goes for any one of the components.
3662 $letter = report_letter( 'GPSLatitude', $info, $new_info );
3663 if ($letter eq '*') {
3664 # the latitude is different but not by enough to show up in
3665 # the EXIF tag after writing. Mark as the same.
3666 $letter = '=';
3667 }
3668 }
3669 log_message($letter);
3670 }
3671
3672 {
3673 my $m = $extra_info->get('min_force_for_change');
3674 log_message( {}, $m < 99 ? $m : ' ' );
3675 }
3676
3677 my $timestamp = timestamp_for_sorting( $info, $new_info );
3678 my $new_fmd = $new_info->get('FileModifyDate');
3679 my $time_change = $new_fmd - $info->get('FileModifyDate');
3680 if ( defined $timestamp ) {
3681 my $create_date = $new_info->get('CreateDate');
3682 my $camera_id = $new_info->get('CameraID');
3683 $camera_id = $display_cameras{$camera_id} if defined $camera_id;
3684 log_message(
3685 sprintf(
3686 " %2s %-25.25s %1s %8s %8s %s\n",
3687 ( $camera_id // '' ),
3688 $timestamp->display_iso,
3689 $extra_info->get('timesource_letter') // ' ',
3690 display_offset( $time_change, 8 ),
3691 (
3692 defined($create_date)
3693 ? display_offset( $new_fmd - $create_date, 8 )
3694 : ' ' x 8
3695 ),
3696 $short
3697 )
3698 );
3699 }
3700 else {
3701 log_message(
3702 sprintf(
3703 " %2s %-25.25s %1s %8s %8s %s\n",
3704 display_camera( $new_info->get('CameraID') ),
3705 'unknown', '?', 'unknown', 'unknown', $short
3706 )
3707 );
3708 }
3709 if ($time_change) {
3710 ++$count_nonzero_offset;
3711 $min_offset = $time_change
3712 if not( defined $min_offset )
3713 or $time_change < $min_offset;
3714 $max_offset = $time_change
3715 if not( defined $max_offset )
3716 or $time_change > $max_offset;
3717 }
3718 }
3719 log_message("\n");
3720 $min_offset //= 0;
3721 $max_offset //= 0;
3722 if ( $min_offset == $max_offset ) {
3723 if ($min_offset) { # not equal to zero
3724 log_message( "All $count_nonzero_offset non-zero offsets are equal to ",
3725 display_offset($min_offset), ".\n\n" );
3726 }
3727 else {
3728 log_message("All offsets are equal to 0 seconds.\n");
3729 }
3730 }
3731 else {
3732 log_message(
3733 "$count_nonzero_offset non-zero offsets are between ",
3734 display_offset($min_offset),
3735 " and ", display_offset($max_offset), ".\n"
3736 );
3737 }
3738
3739 if ( $self->{max_gps_distance} ) {
3740 log_message(
3741 sub {
3742 my ( $value, $prefix ) = si_prefix( $self->{max_gps_distance} );
3743 "Maximum shift of GPS position is $value ${prefix}m for $self->{max_gps_file}\n";
3744 }
3745 );
3746 }
3747
3748 if ( $self->gps_offsets->camera_count ) {
3749 if ( $self->option( 'verbose', 1 ) & 16 ) {
3750 log_message(
3751 sub {
3752 "\nCamera Offsets:\n", Dump( $self->gps_offsets->to_display ), "\n";
3753 }
3754 );
3755 }
3756 else {
3757 log_message(
3758 sub {
3759 "\nCamera offsets for the time range:\n",
3760 Dump( $self->gps_offsets->relevant_part ),
3761 "\n";
3762 }
3763 );
3764 }
3765 }
3766 else {
3767 log_message("\nNo camera offsets are known.\n");
3768 }
3769
3770 return $self;
3771 }
3772
3773 sub report_letter {
3774 my ( $tag, $info, $new_info ) = @_;
3775 ( my $group, $tag ) = $tag =~ /^(?:(\w+):)?(\w+)$/;
3776 $group //= '';
3777 my $old = $info->get($tag);
3778 my $new = $new_info->get( $group, $tag );
3779 if ( defined $new ) {
3780 if ( defined $old ) {
3781 if ( $new ne $old ) {
3782 return '*'; # modified
3783 }
3784 else {
3785 return '='; # unchanged
3786 }
3787 }
3788 else { # new but not old
3789 return '+'; # added
3790 }
3791 }
3792 else { # not new
3793 if ( defined $old ) {
3794 return '!'; # removed
3795 }
3796 else {
3797 return '-'; # absent
3798 }
3799 }
3800 }
3801
3802 # report program and options
3803 sub report_program_and_options {
3804 my ($self) = @_;
3805 my $program_name = file($0)->basename;
3806 my %defaults = (
3807 fastscan => 1,
3808 recurse => 1,
3809 );
3810 my %no = map { $_ => 1 } qw(modify recurse);
3811 log_message(
3812 "Running $program_name " . join(
3813 ' ',
3814 grep { defined($_) } map( {
3815 my $key = $_;
3816 my $value = $self->option($key);
3817 my $text;
3818 if ( ref $value eq 'HASH' ) {
3819 $text = "--$key "
3820 . join( " --$key ",
3821 map { "\"$_\"=\"$value->{$_}\"" } sort keys %{$value} );
3822 }
3823 elsif ( ref $value eq 'ARRAY' ) {
3824 $text = "--$key " . join( " --$key ", @{$value} );
3825 }
3826 elsif ( not( defined $defaults{$key} )
3827 || $value ne $defaults{$key} )
3828 {
3829 if ( $no{$key} ) {
3830 $text = '--' . ( $value ? '' : 'no-' ) . $key;
3831 }
3832 else {
3833 $text = "--$key";
3834 if ( looks_like_number($value) ) {
3835 if ( $value != 1 ) {
3836 $text .= " $value";
3837 }
3838 }
3839 else {
3840 $text .=
3841 ' ' . ( $value =~ /\s/ ? q(") . $value . q(") : $value );
3842 }
3843 }
3844 }
3845 $text;
3846 } grep !/^working-directory$/,
3847 sort keys %{ $self->{options} } ),
3848 map { /\s/ ? q(") . $_ . q(") : $_ } @ARGV
3849 )
3850 . "\n version $VERSION"
3851 . "\n in "
3852 . dir()->absolute . ".\n\n"
3853 );
3854 $self;
3855 }
3856
3857 # returns a reference to the named repository, which gets created if
3858 # it did not exist yet
3859 sub repository {
3860 my ( $self, $name ) = @_;
3861 $self->{repository}->{$name} //= {};
3862 return $self->{repository}->{$name};
3863 }
3864
3865 # @files = resolve_files($rule, @items);
3866 # @files = resolve_files({ recurse => 0 }, $rule, @items); # don't recurse
3867 #
3868 # Resolves a list of path name patterns to a list of files. C<$rule> is
3869 # expected to be a L<Path::Iterator::Rule> that identifies files of
3870 # interest. C<@items> is a list of path name patterns or references to
3871 # lists of path name patterns. If the first argument is a hash
3872 # reference, then it provides additional options. The C<recurse> option
3873 # says whether or not to recurse into subdirectories. It defaults to 1,
3874 # and can be set to 0 through this option.
3875 #
3876 # If an item is an existing directory, then the rule is applied to all
3877 # of its elements, and the matching elements are appended to the list to
3878 # be returned.
3879 #
3880 # If an item is an existing file, then it is appended to the list to be
3881 # returned.
3882 #
3883 # Otherwise, the item is interpreted as a path name pattern, and files
3884 # that match that pattern and also the rule are appended to the list to
3885 # be returned.
3886 sub resolve_files {
3887 my ( $options, $rule, @items );
3888 if ( ref $_[0] eq 'HASH' ) {
3889 ( $options, $rule, @items ) = @_;
3890 }
3891 else {
3892 ( $rule, @items ) = @_;
3893 }
3894 @items = map { ref($_) eq 'ARRAY' ? @{$_} : $_ } @items;
3895 return () unless @items;
3896 $options //= {};
3897 if ( not( $options->{recurse} ) ) {
3898 $rule = Path::Iterator::Rule->new
3899 ->max_depth(1) # no recursion
3900 ->and($rule);
3901 }
3902 my @files;
3903 foreach my $item (@items) {
3904 my @extra;
3905 if ( -d $item ) {
3906 @extra = $rule->all($item);
3907 log_message("No matches for $item.\n") unless @extra;
3908 }
3909 elsif ( -f $item ) {
3910 my $f = file($item);
3911 @extra = ($f);
3912 }
3913 else {
3914 # the item was not found as a literal name; maybe it is a file
3915 # name pattern
3916
3917 my $f = file($item);
3918
3919 # seek directories matching the pattern
3920 my @subdirs =
3921 Path::Iterator::Rule->new
3922 ->max_depth(1)
3923 ->$PIRname( $f->basename )
3924 ->directory
3925 ->all( $f->parent );
3926 @extra = $rule->all(@subdirs) if @subdirs;
3927
3928 # seek files matching the pattern
3929 push @extra,
3930 Path::Iterator::Rule->new
3931 ->max_depth(1)
3932 ->$PIRname( $f->basename )
3933 ->and( $rule )
3934 ->all( $f->parent );
3935
3936 log_message("No matches for $item.\n") unless @extra;
3937
3938 # remove initial './' because it makes later pattern matching
3939 # more difficult
3940 @extra = map { s|^./||; $_ } @extra;
3941 }
3942 push @files, @extra;
3943 }
3944
3945 # remove duplicates, and convert to Perl-style paths
3946 my %files;
3947 $files{ perlish_path($_) } = 1 foreach @files;
3948 return sort keys %files;
3949 }
3950
3951 # restore original files from backup files
3952 sub restore_original_files {
3953 my ( $self, @arguments ) = @_;
3954
3955 # restore original files
3956 my @files = resolve_files( { recurse => $self->option('recurse') },
3957 Path::Iterator::Rule->new->file->$PIRname('*_original'), @arguments );
3958 my $progressbar = $self->setup_progressbar( scalar(@files), 'restore' );
3959 my $count = 0;
3960 my $exit_status = 0;
3961 foreach my $file (@files) {
3962 my $original;
3963 my $backup;
3964 if ( $file =~ /^(.*?)_original$/ ) {
3965 $original = $1;
3966 $backup = $file;
3967 }
3968 else {
3969 $original = $file;
3970 $backup = "${file}_original";
3971 }
3972 if ( -e $backup ) {
3973 if ( -e $original ) {
3974 log_message( 4, { name => $file }, "Removing '$original'.\n" );
3975 unlink($original);
3976 }
3977 else {
3978 log_message(
3979 4,
3980 { name => $file },
3981 "'$original' does not exist; no need to delete.\n"
3982 );
3983 }
3984 log_message( 4, { name => $file },
3985 "Renaming '$backup' to '$original'\n" );
3986 move( $backup, $original )
3987 or $self->log_message("Failed to restore '$original': $!\n");
3988 }
3989 else {
3990 log_message( 4,
3991 "Backup '$backup' does not exist; not restoring '$original'.\n" );
3992 }
3993 ++$count;
3994 $progressbar->add;
3995 }
3996 log_message("Done restoring $count original file(s).\n");
3997 $self->cleanup_progressbar($progressbar);
3998 $self;
3999 }
4000
4001 sub scope_for_file {
4002 my ($file) = @_;
4003 my $scope = file($file)->dir->as_foreign('Unix')->stringify;
4004 $scope = '' if $scope eq '.';
4005 return $scope;
4006 }
4007
4008 # $ims->set_option($name, $value);
4009 # $ims->set_option($name); # undefine
4010 #
4011 # Assigns the C<$value> to the option with the given C<$name>. If the
4012 # C<$value> is not defined or equal to C<undef>, then the option is
4013 # removed.
4014 #
4015 # Returns C<$ims>.
4016 sub set_option {
4017 my ( $self, $option, $value ) = @_;
4018 if ( defined $value ) {
4019 $self->{options}->{$option} = $value;
4020 }
4021 else {
4022 delete $self->{options}->{$option};
4023 }
4024 return $self;
4025 }
4026
4027 # set_working_directory
4028 #
4029 # $ims->set_working_directory;
4030 #
4031 # Sets the working directory based on the C<working-directory> option.
4032 # Must be done before calling L</initialize_logging> because the log
4033 # file by default is in the working directory. So cannot make use of
4034 # the logging.
4035 #
4036 # Returns C<$ims>.
4037 sub set_working_directory {
4038 my ($self) = @_;
4039 my $wd = $self->option( 'workingdirectory', '.' );
4040 if ( $wd ne '.' ) {
4041 chdir($wd)
4042 or croak "Couldn't change to working directory '$wd': $@\n";
4043 }
4044 $self;
4045 }
4046
4047 # $progressbar = $ims->setup_progressbar($count, $title);
4048 #
4049 # Displays an Image::Synchronize::ProgressBar with the specified
4050 # $title that shows progress toward the goal equal to $count.
4051 # Progress messages are passed through the default
4052 # Image::Synchronize::Logger.
4053 sub setup_progressbar {
4054 my ( $self, $count, $title ) = @_;
4055
4056 # We display a progress bar to show how far we are in processing all
4057 # files.
4058 my $progressbar = Image::Synchronize::ProgressBar->new(
4059 {
4060 count => $count,
4061 name => $title,
4062 }
4063 );
4064
4065 # Arrange for the progress bar to handle output of log messages to
4066 # screen, so those messages and the progress bar don't mess up each
4067 # other.
4068 default_logger()->set_printer(
4069 {
4070 bitflags => ( $self->option('verbose') // 1 ),
4071 action => sub { $progressbar->message( join( '', @_ ) ); }
4072 }
4073 );
4074 log_message("Starting phase '$title'.\n");
4075 return $progressbar;
4076 }
4077
4078 sub si_prefix {
4079 my ( $value, $significant ) = @_;
4080 $significant //= 3;
4081 my $sign;
4082 if ( $value < 0 ) {
4083 $sign = -1;
4084 $value = -$value;
4085 }
4086 elsif ( $value > 0 ) {
4087 $sign = 1;
4088 }
4089 else {
4090 return wantarray ? ( 0, '' ) : 0;
4091 }
4092 my @prefixes = ( 'μ', 'm', '', 'k', 'M' );
4093 my $l = POSIX::floor( log($value) / log(1000) + 1 / 3 + 2 );
4094 $l = 0 if $l < 0;
4095 $l = $#prefixes if $l > $#prefixes;
4096 my $prefix = $prefixes[$l];
4097 my $factor = 10**( 3 * ( 2 - $l ) );
4098 my $numeral = sprintf( '%.*g', $significant, $sign * $value * $factor );
4099 return wantarray
4100 ? ( $numeral, $prefix )
4101 : $numeral . ( $prefix ? " $prefix" : '' );
4102 }
4103
4104 sub stringify {
4105 my ($thing) = @_;
4106 return "$thing" if defined $thing;
4107 return '';
4108 }
4109
4110 # $timestamp = timestamp_for_sorting($info, $new_info);
4111 #
4112 # Returns the "best available" timestamp for sorting, based on
4113 # information contained in $info and $new_info, which must have the
4114 # expected format. The order of preference is: (1) new
4115 # FileModifyDate, (2) new DateTimeOriginal, (3) new GPSDateTime, (4)
4116 # old FileModifyDate, (5) old DateTimeOriginal, (6) old GPSDateTime
4117 sub timestamp_for_sorting {
4118 my ( $info, $new_info ) = @_;
4119 my @tags = qw(FileModifyDate DateTimeOriginal GPSDateTime);
4120 my $value;
4121 foreach my $tag (@tags) {
4122 $value = $new_info->get($tag);
4123 return $value if defined $value;
4124 }
4125 foreach my $tag (@tags) {
4126 $value = $info->get($tag);
4127 return $value if defined $value;
4128 }
4129 return;
4130 }
4131
4132 # $user_camera_id = $ims->user_camera_id($path);
4133 #
4134 # Returns the user-defined (through option C<camera>) camera ID for
4135 # the file with the given C<$path>, or C<undef> if there isn't one.
4136 sub user_camera_id {
4137 my ( $self, $path ) = @_;
4138 return $self->repository('user_camera_ids')->{$path};
4139 }
4140
4141 # Calculates a file modification timestamp from a DateTimeOriginal.
4142 #
4143 # The file modification timestamp is expressed in the local timezone.
4144 # If 'relativefiletime' is set, then the file modification timestamp
4145 # has the same clock time as the DateTimeOriginal. Otherwise, the
4146 # file modification timestamp represents the same instant of universal
4147 # time as the DateTimeOriginal.
4148 sub file_from_dto {
4149 my ($self, $dto_timestamp) = @_;
4150 if ($self->option('relativefiletime')) {
4151 my $ltzo = $dto_timestamp->local_timezone_offset;
4152 return $dto_timestamp->clone->set_timezone_offset($ltzo);
4153 } else {
4154 return $dto_timestamp;
4155 }
4156 }
4157
4158 sub normalize_options {
4159 my @args;
4160 foreach (@_) {
4161 if (my ($prefix, $opt, $suffix) = /^(-+)([-_\w]+)(.*)/) {
4162 $opt =~ tr/-_//d;
4163 push @args, "$prefix$opt$suffix";
4164 } else {
4165 push @args, $_;
4166 }
4167 }
4168 return @args;
4169 }
4170
4171 1;
4172
4173 __END__
« Synchronize/Timestamp.pm ↑ Summary