mason/.perl-lib/TrivGal.pm (Image): Factour out EXIF metadata processing.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 21 Jun 2023 09:24:41 +0000 (10:24 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 21 Jun 2023 09:24:41 +0000 (10:24 +0100)
Pull out the EXIF scanning into a new private method `_getexif'.  This
now sets `rot' and `flip' slots so that `scale' can fix up the image
properly, but it also sets `wd' and `ht' correctly for the use of
external clients.

mason/.perl-lib/TrivGal.pm

index e6bfcf4..3346c1a 100644 (file)
@@ -217,6 +217,8 @@ package TrivGal::Image {
       path => $path, imgpath => $imgpath,
       mtime => $st->mtime,
       img => undef,
+      rot => undef, flip => undef,
+      wd => undef, ht => undef,
       _wd => undef, _ht => undef,
       sz => undef
     }, $cls;
@@ -232,7 +234,24 @@ package TrivGal::Image {
     @$me{"_wd", "_ht", "sz"} = ($wd, $ht, $sz);
   }
 
+  sub _getexif ($) {
+    my ($me) = @_;
+    return if defined $me->{wd};
+
+    $me->_getsz;
+    my $exif = new Image::ExifTool; $exif->ExtractInfo($me->{imgpath});
+    my $orient = $exif->GetValue("Orientation", "ValueConv");
+    my ($wd, $ht) = @$me{"_wd", "_ht"};
+    my ($rot, $flip);
+    if (defined $orient) { ($rot, $flip) = $ORIENT{$orient}->@*; }
+    else { ($rot, $flip) = (0, 0); }
+    if ($rot%2) { ($wd, $ht) = ($ht, $wd); }
+    @$me{"rot", "flip", "wd", "ht"} = ($rot, $flip, $wd, $ht);
+  }
+
   sub sz ($) { my ($me) = @_; $me->_getsz; return $me->{sz}; }
+  sub wd ($) { my ($me) = @_; $me->_getexif; return $me->{wd}; }
+  sub ht ($) { my ($me) = @_; $me->_getexif; return $me->{ht}; }
 
   sub scale ($$;$) {
     my ($me, $scale, $forcep) = @_;
@@ -259,15 +278,10 @@ package TrivGal::Image {
       } else {
        my $img = $me->{img};
        unless (defined $img) {
-         my $exif = new Image::ExifTool;
-         $exif->ExtractInfo($me->{imgpath});
-         my $orient = $exif->GetValue("Orientation", "ValueConv");
+         $me->_getexif;
          $img = $me->{img} = Image::Imlib2->load($me->{imgpath});
-         if (defined $orient) {
-           my ($rot, $flip) = @{$ORIENT{$orient}};
-           if ($rot) { $img->image_orientate($rot); }
-           if ($flip) { $img->flip_horizontal(); }
-         }
+         if ($me->{rot}) { $img->image_orientate($me->{rot}); }
+         if ($me->{flip}) { $img->flip_horizontal(); }
        }
 
        my ($dir, undef, $ext) = TrivGal::split_path $thumb;