brucemiller / LaTeXML

LaTeXML: a TeX and LaTeX to XML/HTML/ePub/MathML translator.
http://dlmf.nist.gov/LaTeXML/
Other
923 stars 98 forks source link

Graphic transform processing error #1983

Open Someonewow opened 1 year ago

Someonewow commented 1 year ago

Hello,

When I tried to use a program to convert [2210.09337], I found that I would calculate the wrong width and height when transforming the graph.

Warning:imageprocessing:Crop Image processing operation Crop (x, 0, width, 296, y, 719, height, -719) returned Exception 310: geometry does not contain image `/dev/shm/9vzL7W8bn6/figures/Maze_perturb/point5x11_bc.png' @ warning/transform.c/CropImage/668
 In Post::Graphics[@0x5633f2619d50] ->transformGraphic
Error:imageprocessing:Scale Image processing operation Scale (width, -2278580, height, 5534795) returned Exception 465: width or height exceeds limit `/dev/shm/9vzL7W8bn6/figures/Maze_perturb/point5x11_bc.png' @ error/cache.c/OpenPixelCache/3909
 In Post::Graphics[@0x5633f2619d50] ->transformGraphic

ar5iv html url: https://ar5iv.labs.arxiv.org/html/2210.09337 ar5iv log url: https://ar5iv.labs.arxiv.org/log/2210.09337

Problems seem to occur in the lib/LaTeXML/Util/Image.pm

line 332:


  my $image = image_read($source, antialias => 1,
    density => $dpi * $xprescale . 'x' . $dpi * $yprescale,
  ) or return;
  my ($w, $h) = image_getvalue($image, 'width', 'height');
  # Get some defaults from the read-in image.
  my ($imagedpi) = image_getvalue($image, 'x-resolution');

We pass in the DPI when we read the image file and think that the DPI will affect the width and height of the returned image object. But in some cases, the x-resolution value is set to DPI, but the width and height have no effect. Right?

So that in the subsequent trim transformation, the Amount to trim is multiplied by DPI, but the value of w or h is not affected by DPI, and the result ww or hh may be less than 0.

line 383:

 # Use the image's dpi for trim & clip!
 my $idppt = (defined $imagedpi ? $imagedpi : $dpi) / 72.27;
 if ($op eq 'trim') {    # Amount to trim: a1=left, a2=bottom, a3=right, a4=top
   ($x0, $y0, $ww, $hh) = (floor($a1 * $idppt), floor($a4 * $idppt),
     ceil($w - ($a1 + $a3) * $idppt), ceil($h - ($a4 + $a2) * $idppt)); }
 else {                  # BBox: a1=left, a2=bottom, a3=right, a4=top
   ($x0, $y0, $ww, $hh) = (floor($a1 * $idppt), floor($h - $a4 * $idppt),
     ceil(($a3 - $a1) * $idppt), ceil(($a4 - $a2) * $idppt)); }

Not sure if this is the cause of the problem.

dginev commented 1 year ago

Thank you for troubleshooting the problem so far and reporting it to us @Someonewow !

I am tempted to let @brucemiller get a first response in here, as these are his calculations, just wanted to send an early encouraging comment that it would indeed be great to get as precise as possible here.