deri pushed a commit to branch master
in repository groff.

commit ead74ff837d34347a54144179b4adbf28ab2f08b
Author: Deri James <[email protected]>
AuthorDate: Sun Aug 25 16:37:09 2024 +0100

    Make gropdf compatible with IM 6(.9)
    
    Peter Schaffter discovered some problems with recent
    changes to gropdf and PDFPIC, they were not fully
    compatible with ImageMagick 6.9 which is what he was
    using. See here 
<https://lists.gnu.org/archive/html/groff/2024-08/msg00044.html>
    
    * src/devices/gropdf/gropdf.pl: One difference in IM 6 is
    that an alpha channel of all zeroes meant the image was opaque,
    but in 7 it is reversed and means the image is wholly
    transparent.
    
    This change is documented here:-
    
    <http://www.imagemagick.org/script/porting.php#alpha>
    
    "Alpha
    
    We support alpha now, previously opacity. With alpha, a value
    of 0 means that the pixel does not have any coverage information
    and is transparent; i.e. there was no color contribution from any
    geometry because the geometry did not overlap this pixel. A value
    of QuantumRange means that the pixel is opaque because the geometry
    completely overlapped the pixel. As a consequence, in version 7,
    the PixelInfo structure member alpha has replaced the previous
    opacity member. Another consequence is the alpha part of an sRGB
    value in hexadecimal notation is now reversed (e.g. #0000 is fully
    transparent)."
    
    This is now handled by gropdf.
    
    There was an undocumented (I thought it came as part of perlmagick,
    it doesn't) dependency on the module Image::ExifTool this is no
    longer used by gropdf - so not required.
    
    * tmac/pdfpic.tmac: Another issue was that identify sometimes
    reports sizes for pdfs which do not match the page size, (it seems
    to be attempting to report the size of the image embedded in the
    pdf). For this reason pdfinfo is now used first on files, which
    consistently reports the page size. There is a slight time penalty
    since non-pdf files are subject to a (failed) pdfinfo run.
---
 ChangeLog                    | 44 ++++++++++++++++++++++++++++++++++++++++++++
 src/devices/gropdf/gropdf.pl | 39 +++++++++++++++++++--------------------
 tmac/pdfpic.tmac             | 14 +++++++-------
 3 files changed, 70 insertions(+), 27 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 77049874a..640a8bdd7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,47 @@
+2024-08-24  Deri James  <[email protected]>
+
+       Make gropdf compatible with IM 6(.9)
+
+       Peter Schaffter discovered some problems with recent
+       changes to gropdf and PDFPIC, they were not fully
+       compatible with ImageMagick 6.9 which is what he was
+       using. See here 
<https://lists.gnu.org/archive/html/groff/2024-08/msg00044.html>
+
+       * src/devices/gropdf/gropdf.pl: One difference in IM 6 is
+       that an alpha channel of all zeroes meant the image was opaque,
+       but in 7 it is reversed and means the image is wholly
+       transparent.
+
+       This change is documented here:-
+
+       <http://www.imagemagick.org/script/porting.php#alpha>
+
+       "Alpha
+
+       We support alpha now, previously opacity. With alpha, a value
+       of 0 means that the pixel does not have any coverage information
+       and is transparent; i.e. there was no color contribution from any
+       geometry because the geometry did not overlap this pixel. A value
+       of QuantumRange means that the pixel is opaque because the geometry
+       completely overlapped the pixel. As a consequence, in version 7,
+       the PixelInfo structure member alpha has replaced the previous
+       opacity member. Another consequence is the alpha part of an sRGB
+       value in hexadecimal notation is now reversed (e.g. #0000 is fully
+       transparent)."
+
+       This is now handled by gropdf.
+
+       There was an undocumented (I thought it came as part of perlmagick,
+       it doesn't) dependency on the module Image::ExifTool this is no
+       longer used by gropdf - so not required.
+
+       * tmac/pdfpic.tmac: Another issue was that identify sometimes
+       reports sizes for pdfs which do not match the page size, (it seems
+       to be attempting to report the size of the image embedded in the
+       pdf). For this reason pdfinfo is now used first on files, which
+       consistently reports the page size. There is a slight time penalty
+       since non-pdf files are subject to a (failed) pdfinfo run.
+
 2024-08-24  Deri James  <[email protected]>
 
        Handle preconv better
diff --git a/src/devices/gropdf/gropdf.pl b/src/devices/gropdf/gropdf.pl
index ac0dcefd8..1102364b0 100644
--- a/src/devices/gropdf/gropdf.pl
+++ b/src/devices/gropdf/gropdf.pl
@@ -283,8 +283,8 @@ if($rc)
 
 $rc = eval
 {
-    require Image::ExifTool;
-    Image::ExifTool->import();
+#     require Image::ExifTool;
+#     Image::ExifTool->import();
     require Image::Magick;
     Image::Magick->import();
     1;
@@ -1674,6 +1674,7 @@ sub do_x
                my $mat=[1,0,0,1,0,0];
                my $imgtype='PDF';
                my $info;
+               my $image;
 
                my ($FD,$FDnm)=OpenInc($fil);
 
@@ -1687,10 +1688,16 @@ sub do_x
                {
                    if ($gotexif)
                    {
-                       my $exifTool = Image::ExifTool->new;
-                       $info = $exifTool->ImageInfo($FD);
-
-                       $imgtype=$info->{FileType}||'' if defined($info);
+                       binmode $FD;
+
+                       $image = Image::Magick->new;
+                       my $x = $image->Read(file => $FD);
+                       Warn("Image '$FDnm': $x"), return if "$x";
+                       $imgtype=$image->Get('magick');
+                       $info->{ImageWidth}=$image->Get('width');
+                       $info->{ImageHeight}=$image->Get('height');
+                       $info->{ColorComponents}=
+                           ($image->Get('colorspace') eq 'Gray')?1:3;
                    }
                    else
                    {
@@ -1730,7 +1737,7 @@ sub do_x
                    }
                    else
                    {
-                       $incfil{$fil}=LoadMagick($FD,$FDnm,$info);
+                       $incfil{$fil}=LoadMagick($image,$FDnm,$info);
                    }
 
                    return if !defined($incfil{$fil});
@@ -2683,20 +2690,10 @@ sub LoadJP2
 
 sub LoadMagick
 {
-    my $JP=shift;
+    my $image=shift;
     my $JPnm=shift;
     my $info=shift;
 
-    local $/=undef;
-
-    seek($JP,0,0);
-    binmode $JP;
-
-    my ($x, $image);
-
-    $image = Image::Magick->new;
-    $x = $image->Read(file => $JP);
-    Warn("Image '$JPnm': $x"), return if "$x";
 #     my $e=$image->Get('endian');
 #     print STDERR "En: '$JPnm' $e\n";
 #     $image->Set(endian => 'MSB') if $e eq 'Undefined';
@@ -2713,18 +2710,20 @@ sub LoadMagick
     {
        $alpha=$image->Clone();
        $alpha->Separate(channel => 'Alpha');
+       my $v=$alpha->Get('version');
+       $v=$1 if $v=~m/^ImageMagick (\d+\.\d+)/;
+       $alpha->Negate(channel => 'All') if $v < 7;
        $alpha->Set(magick => 'gray');
     }
 
     my $cs=$image->Get('colorspace');
     $cs='RGB' if $cs eq 'sRGB';
-    $x = $image->Set(alpha => 'off', magick => $cs);
+    my $x = $image->Set(alpha => 'off', magick => $cs);
     Warn("Image '$JPnm': $x"), return if "$x";
     my @blobs = $image->ImageToBlob();
     Warn("Image '$JPnm': More than 1 image") if $#blobs > 0;
     $blobs[0]=pack('v*', unpack('n*', $blobs[0])) if $BPC==16;
     $blobs[0]=pack('V*', unpack('N*', $blobs[0])) if $BPC==32;
-    close($JP);
 
     my $xobj=++$objct;
     my $xonm="XO$xobj";
diff --git a/tmac/pdfpic.tmac b/tmac/pdfpic.tmac
index 80b527474..ca889a268 100644
--- a/tmac/pdfpic.tmac
+++ b/tmac/pdfpic.tmac
@@ -74,17 +74,17 @@
 .\" when the string they populate is later interpolated.
 .eo
 .de pdfpic@get-image-dimensions
-.  ds pdfpic*command ( identify \$1 2>/dev/null || file \$1 ) \"
+.  ds pdfpic*command pdfinfo \$1 2>/dev/null \"
 .  eo
-.  as pdfpic*command  | perl -ne 'print qq(. \\\\R\@pdfpic*width $+{w}p@ 
\\\\R\@pdfpic*height $+{h}p\@),qq(\\n) if m/(?:(?:[,=A-Z]|JP2) 
(?<w>\d+)\s*x\s*(?<h>\d+))|(?:height=(?<h>\d+).+width=(?<w>\d+))/;'
+.  as pdfpic*command  | perl -ne 'print qq(. \\\\R\@pdfpic*width $+{w}p@ 
\\\\R\@pdfpic*height $+{h}p\@) if 
m/Page\s+size:\s+(?<w>[\d.]+)\sx\s(?<h>[\d.]+)/si;'
 .  ec
 .  pso \*[pdfpic*command]
 .  if !r pdfpic*width \{\
-.     ds pdfpic*command pdfinfo \$1 \"
-.     eo
-.     as pdfpic*command  | perl -ne 'print qq(. \\\\R\@pdfpic*width $+{w}p@ 
\\\\R\@pdfpic*height $+{h}p\@) if 
m/Page\s+size:\s+(?<w>[\d.]+)\sx\s(?<h>[\d.]+)/si;'
-.     ec
-.     pso \*[pdfpic*command]
+.    ds pdfpic*command ( identify \$1 2>/dev/null || file \$1 ) \"
+.    eo
+.    as pdfpic*command  | perl -ne 'print qq(. \\\\R\@pdfpic*width $+{w}p@ 
\\\\R\@pdfpic*height $+{h}p\@),qq(\\n) if m/(?:(?:[,=A-Z]|JP2) 
(?<w>\d+)\s*x\s*(?<h>\d+))|(?:height=(?<h>\d+).+width=(?<w>\d+))/;'
+.    ec
+.    pso \*[pdfpic*command]
 .  \}
 .  rm pdfpic*command
 ..

_______________________________________________
Groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit

Reply via email to