]> git.sesse.net Git - pr0n/blobdiff - perl/Sesse/pr0n/Common.pm
Newer ImageMagick supports NEF via libraw-dev, and the old fallback is seemingly...
[pr0n] / perl / Sesse / pr0n / Common.pm
index f6cea5b98ab8e9de7721decb63f167707eea8e43..8a426987bf2fbb90614f7f36620aaf37cbbcaae2 100644 (file)
@@ -528,17 +528,9 @@ sub read_original_image {
        }
        my $err;
 
-       # ImageMagick can handle NEF files, but it does it by calling dcraw as a delegate.
-       # The delegate support is rather broken and causes very odd stuff to happen when
-       # more than one thread does this at the same time. Thus, we simply do it ourselves.
        if ($filename =~ /\.(nef|cr2)$/i) {
-               # this would suffice if ImageMagick gets to fix their handling
-               # $physical_fname = "NEF:$physical_fname";
-               
-               open DCRAW, "-|", "dcraw", "-w", "-c", $physical_fname
-                       or error("dcraw: $!");
-               $err = $magick->Read(file => \*DCRAW);
-               close(DCRAW);
+               $physical_fname = "NEF:$physical_fname";
+               $err = $magick->Read($physical_fname);
        } else {
                # We always want YCbCr JPEGs. Setting this explicitly here instead of using
                # RGB is slightly faster (no colorspace conversion needed) and works equally
@@ -573,16 +565,21 @@ sub read_original_image {
 }
 
 sub ensure_cached {
-       my ($r, $avif_ok, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres) = @_;
+       my ($r, $avif_ok, $jxl_ok, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres) = @_;
 
        my $fname = get_disk_location($r, $id);
        unless (defined($xres) && (!defined($dbwidth) || !defined($dbheight) || $xres < $dbwidth || $yres < $dbheight || $xres == -1)) {
                return ($fname, undef);
        }
 
-       # See if we have an up-to-date AVIF to serve.
+       # See if we have an up-to-date JPEG-XL or AVIF to serve.
        # (We never generate them on-the-fly, since they're so slow.)
-       my $cachename = get_cache_location($id, $xres, $yres, 'avif');
+       my $cachename = get_cache_location($id, $xres, $yres, 'jxl');
+       if ($jxl_ok && -r $cachename and (-M $cachename <= -M $fname)) {
+               return ($cachename, 'image/jxl');
+       }
+
+       $cachename = get_cache_location($id, $xres, $yres, 'avif');
        if ($avif_ok && -r $cachename and (-M $cachename <= -M $fname)) {
                return ($cachename, 'image/avif');
        }
@@ -677,6 +674,18 @@ sub make_cache {
                        unlink($raw_filename);
                        system('MP4Box', '-quiet', '-add-image', "$ivf_filename:primary", '-ab', 'avif', '-ab', 'miaf', '-new', $cachename);
                        unlink($ivf_filename);
+               } elsif ($format eq 'jxl') {
+                       # Similar, for JPEG-XL.
+                       (my $dirname = $cachename) =~ s,/[^/]*$,,;
+                       my ($fh, $raw_filename) = File::Temp::tempfile('tmp.XXXXXXXX', DIR => $dirname, SUFFIX => '.ppm');
+                       my %parms = (
+                               file => $fh,
+                               filename => $raw_filename
+                       );
+                       $cimg->write(%parms);
+                       close($fh);
+                       system('cjxl', '-p', $raw_filename, $cachename);
+                       unlink($raw_filename);
                } else {
                        die "Unknown format $format";
                }