lib/LaTeXML/Util/Image.pm

filter: Statementstmt 15.2% 51 / 334 Branchbran 1.1% 3 / 266 Conditioncond 0.0% 0 / 131 MC/DCmcdc 0.0% 0 / 77 Subroutinesub 41.3% 12 / 29 Podpod 0.0% 0 / 19 totaltotal 7.7% 66 / 856 CC 186 SCAR 78.0
CC
186
Cov
7%
image_graphicx_complex
5852.0
image_graphicx_parse
1722.0
image_graphicx_size
240.0
CRAP 2442.3
« lib/LaTeXML/Common/Config.pm ↑ Summary lib/LaTeXML/Post/MathML.pm »
1 # /=====================================================================\ #
2 # | LaTeXML::Util::Image | #
3 # | Image support for LaTeXML | #
4 # |=====================================================================| #
5 # | Part of LaTeXML: | #
6 # | Public domain software, produced as part of work done by the | #
7 # | United States Government & not subject to copyright in the US. | #
8 # |---------------------------------------------------------------------| #
9 # | Bruce Miller <bruce.miller@nist.gov> #_# | #
10 # | http://dlmf.nist.gov/LaTeXML/ (o o) | #
11 # \=========================================================ooo==U==ooo=/ #
12 package LaTeXML::Util::Image;
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use LaTeXML::Global;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use LaTeXML::Common::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use LaTeXML::Common::Dimension;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use LaTeXML::Util::Pathname;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Image::Size;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 our @EXPORT = (
24 qw( &image_candidates &image_type &image_size ),
25 qw( &image_classes &image_can_image &image_object ),
26 qw( &image_write ),
27 qw( &image_graphicx_parse &image_graphicx_is_trivial &image_graphicx_trivialize
28 &image_graphicx_size &image_graphicx_trivial &image_graphicx_complex),
29 qw( &image_graphicx_sizer),
30 );
31 # The initial idea here is to form a minimal interface to image operations
32 # and to shield LaTeXML from the 'unreliable' Image::Magick;
33 # Namely, it is hard to rely on as a dependency, since it is often
34 # difficult to port, or has mismatched versions or whatever.
35 # We do, at least, need to be able to get image size.....
36  
37 1 our $DPI = 100; # [CONSTANT]
38 1 our $BACKGROUND = "#FFFFFF"; # [CONSTANT]
39  
40 # Return cleaned-up path and list of candidate image files
41 # {We could, but dont, filter out non-image types, since extensions are so inconsistent.
42 # although we could query image_type which is more thorough)
43 0 ▶sub image_candidates {
Subroutine image_candidates: not called CC=4 SCAR=30.0
Pod undocumented
44 0 my ($path) = @_;
45 0 $path =~ s/^\s+//; $path =~ s/\s+$//;
46 0 $path =~ s/^("+)(.+)\g1$/$2/; # unwrap if in quotes
47 0 my $searchpaths = $STATE->lookupValue('GRAPHICSPATHS');
48 0 my @candidates = pathname_findall($path, types => ['*'], paths => $searchpaths);
49 0 ▶ if (!@candidates) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@candidates) 0 0 0
50 # if we have no candidates, also consult kpsewhich,
51 # e.g. for "example-image-a"
52 0 ▶ if (my $kpse_found = pathname_kpsewhich("$path.png", "$path.pdf")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $kpse_found = pathname_kpsewhich("$path.png", "$path.pdf")) 0 0 0
53 0 @candidates = ($kpse_found); } }
54 0 ▶ if (my $base = $STATE->lookupValue('SOURCEDIRECTORY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $base = $LaTeXML::Util::Image::STATE->lookupValue("SOURCEDIRECTORY")) 0 0 0
55 0 @candidates = map { pathname_relative($_, $base) } @candidates; }
56 0 return ($path, @candidates); }
57  
58 # These environment variables can be used to limit the amount
59 # of time & space used by ImageMagick. They are particularly useful
60 # when ghostscript becomes involved in converting postscript or pdf.
61 # HOWEVER, there are indications that the time limit (at least)
62 # is being measured against the whole latexml process, not just image processing.
63 # Thus they aren't really that useful here.
64 # They probably are useful in a server context, however, so I'll leave the comments.
65 # $ENV{MAGICK_DISK_LIMIT} = "2GiB" unless defined $ENV{MAGICK_DISK_LIMIT};
66 # $ENV{MAGICK_MEMORY_LIMIT} = "512MiB" unless defined $ENV{MAGICK_MEMORY_LIMIT};
67 # $ENV{MAGICK_MAP_LIMIT} = "1GiB" unless defined $ENV{MAGICK_MAP_LIMIT};
68 # $ENV{MAGICK_TIME_LIMIT} = "300" unless defined $ENV{MAGICK_TIME_LIMIT};
69  
70 # Note that Image::Size may, itself, use Image::Magick, if available,
71 # as a fallback for getting image size & type!!!
72 # However, it seems not to recognize file types with extension .EPS (uppercase), eg!
73 0 ▶sub image_type {
Subroutine image_type: not called CC=8 SCAR=42.8
Pod undocumented
74 0 my ($pathname) = @_;
75 0 my ($w, $h, $t) = imgsize($pathname);
76 # even though Image::Size CLAIMS to use Image::Magick as fallback... needs tickling?
77 0 ▶ if (!(defined $w) && !(defined $h) && image_can_image()) { # try harder!
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $w || defined $h and image_can_image()) 0 0 0
Condition0%0/6
not defined $w || defined $h and image_can_image()
!ll&&!rl&&r
000
defined $w || defined $h
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $w || defined $h and image_can_image()
defined $w defined $h image_can_image()
Truth table0%0/4
not(A || B) and C
A= defined $wB= defined $hC= image_can_image()
ABCresult
1XX0
01X0
0000
0011
78 0 ▶ my $image = image_read($pathname) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $image = image_read($pathname) or return 0 0 0
79 0 ($t) = image_getvalue($image, 'format'); }
80 # Note that Image::Magick (sometimes) returns "descriptive" types
81 # (but I can't find a list anywhere!)
82 0 ▶ $t = 'eps' if $t && $t =~ /postscript/i;
Branch: 1 branch
BranchTrueFalseTotal
if $t and $t =~ /postscript/i 0 0 0
Condition0%0/3
$t and $t =~ /postscript/i
!ll&&!rl&&r
000
MC/DC0%0/2
$t and $t =~ /postscript/i
$t $t =~ /postscript/i
Truth table0%0/3
A and B
A= $tB= $t =~ /postscript/i
ABresult
0X0
100
111
83 0 ▶ return (defined $t ? lc($t) : undef); }
Branch: 1 branch
BranchTrueFalseTotal
defined $t ? : 0 0 0
84  
85 0 ▶sub image_size {
Subroutine image_size: not called CC=8 SCAR=42.8
Pod undocumented
86 0 my ($pathname) = @_;
87 # Annoyingly, ImageMagick uses the MediaBox instead of CropBox (as does graphics.sty) for pdfs.
88 # Worse, imgsize delegates to ImageMagick, w/o ability to add options
89 0 ▶ if (($pathname =~ /\.pdf$/i) && image_can_image()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pathname =~ /\.pdf$/i and image_can_image()) 0 0 0
Condition0%0/3
$pathname =~ /\.pdf$/i and image_can_image()
!ll&&!rl&&r
000
MC/DC0%0/2
$pathname =~ /\.pdf$/i and image_can_image()
$pathname =~ /\.pdf$/i image_can_image()
Truth table0%0/3
A and B
A= $pathname =~ /\.pdf$/iB= image_can_image()
ABresult
0X0
100
111
90 0 ▶ my $image = image_read($pathname) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $image = image_read($pathname) or return 0 0 0
91 0 return image_getvalue($image, 'width', 'height'); }
92 0 my ($w, $h, $t) = imgsize($pathname);
93 0 ▶ return ($w, $h) if $w && $h;
Branch: 1 branch
BranchTrueFalseTotal
if $w and $h 0 0 0
Condition0%0/3
$w and $h
!ll&&!rl&&r
000
MC/DC0%0/2
$w and $h
$w $h
Truth table0%0/3
A and B
A= $wB= $h
ABresult
0X0
100
111
94 0 ▶ if (image_can_image()) { # try harder!
Branch: 1 branch
BranchTrueFalseTotal
if (image_can_image()) 0 0 0
95 0 ▶ my $image = image_read($pathname) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $image = image_read($pathname) or return 0 0 0
96 0 return image_getvalue($image, 'width', 'height'); } }
97  
98 # This will be set once we've found an Image processing library to use [Daemon safe]
99 1 our $IMAGECLASS; # cached class if we found one that works. [CONFIGURABLE?]
100 1 my @MagickClasses = (qw(Graphics::Magick Image::Magick)); # CONSTANT
101  
102 0 ▶sub image_classes {
Subroutine image_classes: not called CC=1 SCAR=6.9
Pod undocumented
103 0 return @MagickClasses; }
104  
105 1 ▶sub image_can_image {
Subroutine image_can_image: called CC=4 SCAR=14.8
Pod undocumented
106 1 my ($pathname) = @_;
107 1 ▶ if (!$IMAGECLASS) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LaTeXML::Util::Image::IMAGECLASS) 1 0 1
108 1 foreach my $class (@MagickClasses) {
109 2 my $module = $class . ".pm";
110 2 $module =~ s/::/\//g;
111 2 my $object = eval {
112 2 local $LaTeXML::IGNORE_ERRORS = 1;
113 0 require $module; $class->new(); };
114 2 ▶ if ($object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object) 0 2 2
115 0 $IMAGECLASS = $class;
116 0 last; } } }
117 1 return $IMAGECLASS; }
118  
119 # return an image object (into which you can read), if possible.
120 1 ▶sub image_object {
Subroutine image_object: called CC=2 SCAR=9.2
Pod undocumented
121 1 my (%properties) = @_;
122 1 ▶ return unless image_can_image();
Branch: 1 branch
BranchTrueFalseTotal
unless image_can_image() 1 0 1
123 0 my $image = $IMAGECLASS->new(%properties);
124 0 return $image; }
125  
126 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 # Working with Image transformation options from the graphic(s|x) packages
128 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 # Note that viewport is supposed to be relative to bounding box,
130 # and EITHER ONE can cause clipping, if clip=true.
131 # However, apparently the INTENT of bounding box is simply to
132 # supply one, if one can't be found, in order to determine the image size.
133 # In our case, we may end up getting a gif, jpeg, etc, whose origin is always 0,0,
134 # and whose size is clear; also postscript figures sizes will be determined
135 # by ghostview(?). Another use of setting a bounding box, when clip=false,
136 # is to make the image lay ontop of neighboring text. This isn't quite
137 # possible in HTML, other than possibly through some tricky CSS.
138 # Besides, I'd like to avoid reading the bb file, if I can.
139 # --- So, for all these reasons, we simply ignore bounding box here.
140  
141 0 ▶sub image_graphicx_parse {
Subroutine image_graphicx_parse: not called CC=41 SCAR=74.5
Pod undocumented
142 0 my ($transformstring, %options) = @_;
143 0 ▶ return [] unless $transformstring;
Branch: 1 branch
BranchTrueFalseTotal
unless $transformstring 0 0 0
144 0 local $_ = $_;
145 0 my ($v, $clip, $trim, $width, $height, $xscale, $yscale,
146 $aspect, $angle, $rotfirst, $mag, @bb, @vp,) = ('', '', '', 0, 0, 0, 0, '', 0, '', 1, 0);
147 0 my @unknown = ();
148 0 ▶ my @ignore = @{ $options{ignore_options} || [] };
Condition0%0/2
$options{'ignore_options'} || []
l!l
00
MC/DC0%0/1
$options{'ignore_options'} || []
$options{'ignore_options'}
Truth table0%0/2
A
A= $options{'ignore_options'}
Aresult
11
00
149 0 ▶ foreach (split(/(?<!\\),/, $transformstring || '')) {
Condition0%0/2
$transformstring || ''
l!l
00
MC/DC0%0/1
$transformstring || ''
$transformstring
Truth table0%0/2
A
A= $transformstring
Aresult
11
00
150 0 ▶ if (/^\s*(\w+)(?:=\s*(.*))?\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*(\w+)(?:=\s*(.*))?\s*$/) { } 0 0 0
151 0 ▶ $_ = $1; $v = $2 || ''; $v =~ s/\\,/,/g;
Condition0%0/2
$2 || ''
l!l
00
MC/DC0%0/1
$2 || ''
$2
Truth table0%0/2
A
A= $2
Aresult
11
00
152 0 my $op = $_;
153 0 ▶ if (grep { $op eq $_ } @ignore) { } # Ignore this option
Branch: 16 branches
BranchTrueFalseTotal
if (grep {$op eq $_;} @ignore) { } 0 0 0
elsif (/^bb$/) { } 0 0 0
elsif (/^bb(?:ll|ur)(?:x|y)$/) { } 0 0 0
elsif (/^nat(?:width|height)$/) { } 0 0 0
elsif (/^viewport$/) { } 0 0 0
elsif (/^trim$/) { } 0 0 0
elsif (/^clip$/) { } 0 0 0
elsif (/^keepaspectratio$/) { } 0 0 0
elsif (/^width$/) { } 0 0 0
elsif (/^(?:total)?height$/) { } 0 0 0
elsif (/^scale$/) { } 0 0 0
elsif (/^xscale$/) { } 0 0 0
elsif (/^yscale$/) { } 0 0 0
elsif (/^angle$/) { } 0 0 0
elsif (/^origin$/) { } 0 0 0
elsif (/^magnification$/) { } 0 0 0
154 0 elsif (/^bb$/) { @bb = map { to_bp($_) } split(' ', $v); }
155 0 elsif (/^bb(?:ll|ur)(?:x|y)$/) { $bb[2 * /ur/ + /y/] = to_bp($v); }
156 0 elsif (/^nat(?:width|height)$/) { $bb[2 + /width/] = to_bp($v); }
157 0 elsif (/^viewport$/) { @vp = map { to_bp($_) } split(' ', $v); $trim = 0; }
158 0 elsif (/^trim$/) { @vp = map { to_bp($_) } split(' ', $v); $trim = 1; }
159 0 elsif (/^clip$/) { $clip = !($v eq 'false'); }
160 0 elsif (/^keepaspectratio$/) { $aspect = !($v eq 'false'); }
161 0 elsif (/^width$/) { $width = to_bp($v); }
162 0 elsif (/^(?:total)?height$/) { $height = to_bp($v); }
163 0 elsif (/^scale$/) { $xscale = $yscale = $v; }
164 0 elsif (/^xscale$/) { $xscale = $v; }
165 0 elsif (/^yscale$/) { $yscale = $v; }
166 0 ▶ elsif (/^angle$/) { $angle = $v; $rotfirst = !($width || $height || $xscale || $yscale); }
Condition0%0/9
$width || $height || $xscale || $yscale
l!l&&r!l&&!r
000
$width or $height or $xscale
l!l&&r!l&&!r
000
$width or $height
l!l&&r!l&&!r
000
MC/DC0%0/4
$width || $height || $xscale || $yscale
$width $height $xscale $yscale
Truth table0%0/5
A or B or C || D
A= $widthB= $heightC= $xscaleD= $yscale
ABCDresult
1XXX1
01XX1
001X1
00011
00000
167 elsif (/^origin$/) { } # ??
168 # Non-standard option
169 0 elsif (/^magnification$/) { $mag = $v; }
170 0 else { push(@unknown, [$op, $v]); } }
171 else { } } # ?
172 # --------------------------------------------------
173 # Now, compile the options into a sequence of `transformations'.
174 # Note: the order of rotation & scaling is significant,
175 # but the order of various clipping options w.r.t rotation or scaling is not.
176 0 my @transform = ();
177 # If clip is set, viewport and trim will clip the image to that box,
178 # but if not, they should ONLY affect the apparent size of the image.
179 # Anything outside the box will *overlap* any adjacent material.
180 # That's tricky to do in html, but possible with some fancy, brittle, CSS ....
181 # For now, I guess we'll just clip in all cases.
182 0 ▶ push(@transform, [($trim ? 'trim' : 'clip'), @vp]) if @vp;
Branch: 2 branches
BranchTrueFalseTotal
if @vp 0 0 0
$trim ? : 0 0 0
183 0 ▶ push(@transform, ['rotate', $angle]) if ($rotfirst && $angle); # Rotate before scaling?
Branch: 1 branch
BranchTrueFalseTotal
if $rotfirst and $angle 0 0 0
Condition0%0/3
$rotfirst and $angle
!ll&&!rl&&r
000
MC/DC0%0/2
$rotfirst and $angle
$rotfirst $angle
Truth table0%0/3
A and B
A= $rotfirstB= $angle
ABresult
0X0
100
111
184 0 ▶ if ($width && $height) { push(@transform, ['scale-to', $mag * $width, $mag * $height, $aspect]); }
Branch: 7 branches
BranchTrueFalseTotal
if ($width and $height) { } 0 0 0
elsif ($width) { } 0 0 0
elsif ($height) { } 0 0 0
elsif ($xscale and $yscale) { } 0 0 0
elsif ($xscale) { } 0 0 0
elsif ($yscale) { } 0 0 0
elsif ($mag != 1) { } 0 0 0
Condition0%0/3
$width and $height
!ll&&!rl&&r
000
MC/DC0%0/2
$width and $height
$width $height
Truth table0%0/3
A and B
A= $widthB= $height
ABresult
0X0
100
111
185 0 elsif ($width) { push(@transform, ['scale-to', $mag * $width, 999999, 1]); }
186 0 elsif ($height) { push(@transform, ['scale-to', 999999, $mag * $height, 1]); }
187 0 ▶ elsif ($xscale && $yscale) { push(@transform, ['scale', $mag * $xscale, $mag * $yscale]); }
Condition0%0/3
$xscale and $yscale
!ll&&!rl&&r
000
MC/DC0%0/2
$xscale and $yscale
$xscale $yscale
Truth table0%0/3
A and B
A= $xscaleB= $yscale
ABresult
0X0
100
111
188 0 elsif ($xscale) { push(@transform, ['scale', $mag * $xscale, $mag]); }
189 0 elsif ($yscale) { push(@transform, ['scale', $mag, $mag * $yscale]); }
190 0 elsif ($mag != 1) { push(@transform, ['scale', $mag, $mag]); }
191 0 ▶ push(@transform, ['rotate', $angle]) if (!$rotfirst && $angle); # Rotate after scaling?
Branch: 1 branch
BranchTrueFalseTotal
if not $rotfirst and $angle 0 0 0
Condition0%0/3
not $rotfirst and $angle
!ll&&!rl&&r
000
MC/DC0%0/2
not $rotfirst and $angle
not $rotfirst $angle
Truth table0%0/3
not(A) and B
A= not $rotfirstB= $angle
ABresult
0X0
100
111
192 # ----------------------
193 0 return [@transform, @unknown]; }
194  
195 1 my %BP_conversions = ( # CONSTANT
196 pt => 72 / 72.27, pc => 12 / 72.27, in => 72, bp => 1,
197 cm => 72 / 2.54, mm => 72 / 25.4, dd => (72 / 72.27) * (1238 / 1157),
198 cc => 12 * (72 / 72.27) * (1238 / 1157), sp => 72 / 72.27 / 65536);
199  
200 0 ▶sub to_bp {
Subroutine to_bp: not called CC=4 SCAR=30.0
Pod undocumented
201 0 my ($x) = @_;
202 0 ▶ if ($x =~ /^\s*([+-]?[\d\.]+)(\w*)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x =~ /^\s*([+-]?[\d\.]+)(\w*)\s*$/) { } 0 0 0
203 0 my ($v, $u) = ($1, $2);
204 0 $u =~ s/^true//;
205 0 ▶ return $v * (($u && $BP_conversions{$u}) || 1); }
Condition0%0/5
$u && $BP_conversions{$u} || 1
l!l
00
$u and $BP_conversions{$u}
!ll&&!rl&&r
000
MC/DC0%0/2
$u && $BP_conversions{$u} || 1
$u $BP_conversions{$u}
Truth table0%0/3
A and B
A= $uB= $BP_conversions{$u}
ABresult
111
0X0
100
206 else {
207 0 return 1; } }
208  
209 #======================================================================
210 # Compute the effective size of a graphic transformed in graphicx style.
211 # [this is a simplification of image_graphicx_complex]
212 # Note: This is not (yet) using any of the magnify/upscale parameters; Should it?
213 0 ▶sub image_graphicx_size {
Subroutine image_graphicx_size: not called CC=15 SCAR=54.8
Pod undocumented
214 0 my ($source, $transform, %properties) = @_;
215 0 ▶ my $dppt = ($properties{DPI} || $DPI) / 72.27;
Condition0%0/3
$properties{'DPI'} || $LaTeXML::Util::Image::DPI
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties{'DPI'} || $LaTeXML::Util::Image::DPI
$properties{'DPI'} $LaTeXML::Util::Image::DPI
Truth table0%0/3
A || B
A= $properties{'DPI'}B= $LaTeXML::Util::Image::DPI
ABresult
1X1
011
000
216 0 my ($w, $h) = image_size($source);
217 0 ▶ return unless $w && $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $w and $h 0 0 0
Condition0%0/3
$w and $h
!ll&&!rl&&r
000
MC/DC0%0/2
$w and $h
$w $h
Truth table0%0/3
A and B
A= $wB= $h
ABresult
0X0
100
111
218 0 foreach my $trans (@$transform) {
219 0 my ($op, $a1, $a2, $a3, $a4) = @$trans;
220 0 ▶ if ($op eq 'scale') { # $a1 => scale
Branch: 5 branches
BranchTrueFalseTotal
if ($op eq 'scale') { } 0 0 0
elsif ($op eq 'scale-to') { } 0 0 0
elsif ($op eq 'rotate') { } 0 0 0
elsif ($op eq 'reflect') { } 0 0 0
elsif ($op eq 'trim' or $op eq 'clip') { } 0 0 0
221 0 ▶ ($w, $h) = (ceil($w * $a1), ceil($h * ($a2 || $a1))); }
Condition0%0/3
$a2 || $a1
l!l&&r!l&&!r
000
MC/DC0%0/2
$a2 || $a1
$a2 $a1
Truth table0%0/3
A || B
A= $a2B= $a1
ABresult
1X1
011
000
222 elsif ($op eq 'scale-to') {
223 # $a1 => width (pts), $a2 => height (pts), $a3 => preserve aspect ratio.
224 0 ▶ if ($a3) { # If keeping aspect ratio, ignore the most extreme request
Branch: 1 branch
BranchTrueFalseTotal
if ($a3) 0 0 0
225 0 ▶ if ($a1 / $w < $a2 / $h) { $a2 = $h * $a1 / $w; }
Branch: 1 branch
BranchTrueFalseTotal
if ($a1 / $w < $a2 / $h) { } 0 0 0
226 0 else { $a1 = $w * $a2 / $h; } }
227 0 ($w, $h) = (ceil($a1 * $dppt), ceil($a2 * $dppt)); }
228 elsif ($op eq 'rotate') {
229 0 my $rad = -$a1 * 3.1415926 / 180; # close enough
230 0 my $s = sin($rad);
231 0 my $c = cos($rad);
232 0 ($w, $h) = (abs($w * $c) + abs($h * $s), abs($w * $s) + abs($h * $c)); }
233 elsif ($op eq 'reflect') { }
234 # In the following two, note that TeX's coordinates are relative to lower left corner,
235 # but ImageMagick's coordinates are relative to upper left.
236 ▶ elsif (($op eq 'trim') || ($op eq 'clip')) {
Condition0%0/3
$op eq "trim" or $op eq "clip"
l!l&&r!l&&!r
000
MC/DC0%0/2
$op eq "trim" or $op eq "clip"
$op eq "trim" $op eq "clip"
Truth table0%0/3
A or B
A= $op eq "trim"B= $op eq "clip"
ABresult
1X1
011
000
237 0 my ($x0, $y0, $ww, $hh);
238 0 ▶ if ($op eq 'trim') { # Amount to trim: a1=left, a2=bottom, a3=right, a4=top
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq 'trim') { } 0 0 0
239 0 ($x0, $y0, $ww, $hh) = (floor($a1 * $dppt), floor($a4 * $dppt),
240 ceil($w - ($a1 + $a3) * $dppt), ceil($h - ($a4 + $a2) * $dppt)); }
241 else { # BBox: a1=left, a2=bottom, a3=right, a4=top
242 0 ($x0, $y0, $ww, $hh) = (floor($a1 * $dppt), floor($h - $a4 * $dppt),
243 ceil(($a3 - $a1) * $dppt), ceil(($a4 - $a2) * $dppt)); }
244 0 ($w, $h) = ($ww, $hh);
245 } }
246 0 return ($w, $h); }
247  
248 # Totally doesn't belong here, but want to share...
249 0 ▶sub image_graphicx_sizer {
Subroutine image_graphicx_sizer: not called CC=8 SCAR=42.8
Pod undocumented
250 0 my ($whatsit) = @_;
251 0 ▶ if (my $candidates = $whatsit->getProperty('candidates')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $candidates = $whatsit->getProperty("candidates")) 0 0 0
252 0 ▶ my $dpi = ($STATE && $STATE->lookupValue('DPI')) || $DPI;
Condition0%0/6
$LaTeXML::Util::Image::STATE && $LaTeXML::Util::Image::STATE->lookupValue('DPI') || $LaTeXML::Util::Image::DPI
l!l&&r!l&&!r
000
$LaTeXML::Util::Image::STATE and $LaTeXML::Util::Image::STATE->lookupValue("DPI")
!ll&&!rl&&r
000
MC/DC0%0/3
$LaTeXML::Util::Image::STATE && $LaTeXML::Util::Image::STATE->lookupValue('DPI') || $LaTeXML::Util::Image::DPI
$LaTeXML::Util::Image::STATE $LaTeXML::Util::Image::STATE->lookupValue("DPI") $LaTeXML::Util::Image::DPI
Truth table0%0/5
A and B || C
A= $LaTeXML::Util::Image::STATEB= $LaTeXML::Util::Image::STATE->lookupValue("DPI")C= $LaTeXML::Util::Image::DPI
ABCresult
11X1
0X11
1011
0X00
1000
253 0 foreach my $source (split(/,/, $candidates)) {
254 0 ▶ if (!pathname_is_absolute($source)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (pathname_is_absolute($source)) 0 0 0
255 0 ▶ if (my $base = $STATE->lookupValue('SOURCEDIRECTORY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $base = $LaTeXML::Util::Image::STATE->lookupValue("SOURCEDIRECTORY")) 0 0 0
256 0 $source = pathname_concat($base, $source); } }
257 # Skip anything that a lower level imgsize can't understand
258 0 my $options = $whatsit->getProperty('options');
259 0 local $LaTeXML::IGNORE_ERRORS = 1;
260 0 my ($w, $h) = image_graphicx_size($source, image_graphicx_parse($options), DPI => $dpi);
261 0 ▶ return (Dimension($w * 72.27 / $dpi . 'pt'), Dimension($h * 72.27 / $dpi . 'pt'), Dimension(0)) if $w; } }
Branch: 1 branch
BranchTrueFalseTotal
if $w 0 0 0
262 0 return (Dimension(0), Dimension(0), Dimension(0)); }
263  
264 #======================================================================
265 # Trivial scaling.
266 # When an image can be dealt with by simple scaling without "editting" the image.
267 # Compute the desired image size (width,height)
268 # No need to necessarily read the image!
269  
270 # Check if the transform (parsed from above) is trivial
271 # at most scaling; no rotations, reflectiosn or clipping.
272 0 ▶sub image_graphicx_is_trivial {
Subroutine image_graphicx_is_trivial: not called CC=1 SCAR=6.9
Pod undocumented
273 0 my ($transform) = @_;
274 0 return !grep { ($_->[0] =~ /^(?:rotate|reflect|trim|clip)$/) } @$transform; }
275  
276 # Make the transform (parsed from above) trivial
277 # by removing any non-scaling operations!
278 0 ▶sub image_graphicx_trivialize {
Subroutine image_graphicx_trivialize: not called CC=1 SCAR=6.9
Pod undocumented
279 0 my ($transform) = @_;
280 0 return [grep { ($_->[0] =~ /^scale/) } @$transform]; }
281  
282 # trivial_scaling: for transforms containing ONLY scale, scale-to
283 0 ▶sub image_graphicx_trivial {
Subroutine image_graphicx_trivial: not called CC=10 SCAR=47.0
Pod undocumented
284 0 my ($source, $transform, %properties) = @_;
285 0 my ($w, $h) = image_size($source);
286 0 ▶ return unless $w && $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $w and $h 0 0 0
Condition0%0/3
$w and $h
!ll&&!rl&&r
000
MC/DC0%0/2
$w and $h
$w $h
Truth table0%0/3
A and B
A= $wB= $h
ABresult
0X0
100
111
287 0 ▶ my $dppt = ($properties{DPI} || $DPI) / 72.27;
Condition0%0/3
$properties{'DPI'} || $LaTeXML::Util::Image::DPI
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties{'DPI'} || $LaTeXML::Util::Image::DPI
$properties{'DPI'} $LaTeXML::Util::Image::DPI
Truth table0%0/3
A || B
A= $properties{'DPI'}B= $LaTeXML::Util::Image::DPI
ABresult
1X1
011
000
288 0 foreach my $trans (@$transform) {
289 0 my ($op, $a1, $a2, $a3, $a4) = @$trans;
290 0 ▶ if ($op eq 'scale') { # $a1 => scale
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq 'scale') { } 0 0 0
elsif ($op eq 'scale-to') { } 0 0 0
291 0 ▶ ($w, $h) = (ceil($w * $a1), ceil($h * ($a2 || $a1))); }
Condition0%0/3
$a2 || $a1
l!l&&r!l&&!r
000
MC/DC0%0/2
$a2 || $a1
$a2 $a1
Truth table0%0/3
A || B
A= $a2B= $a1
ABresult
1X1
011
000
292 elsif ($op eq 'scale-to') { # $a1 => width, $a2 => height, $a3 => preserve aspect ratio.
293 0 ▶ if ($a3) { # If keeping aspect ratio, ignore the most extreme request
Branch: 1 branch
BranchTrueFalseTotal
if ($a3) 0 0 0
294 0 ▶ if ($a1 / $w < $a2 / $h) { $a2 = $h * $a1 / $w; }
Branch: 1 branch
BranchTrueFalseTotal
if ($a1 / $w < $a2 / $h) { } 0 0 0
295 0 else { $a1 = $w * $a2 / $h; } }
296 0 ($w, $h) = (ceil($a1 * $dppt), ceil($a2 * $dppt)); } }
297 0 return ($w, $h); }
298  
299 #======================================================================
300 # Transform the image, returning (image,width,height);
301 # complex transform for transforms containing ONLY
302 # scale, scale-to, rotate, reflect, trim, clip.
303 0 ▶sub image_graphicx_complex {
Subroutine image_graphicx_complex: not called CC=76 SCAR=86.7
Pod undocumented
304 0 my ($source, $transform, %properties) = @_;
305 0 ▶ my $dpi = ($properties{DPI} || $DPI);
Condition0%0/3
$properties{'DPI'} || $LaTeXML::Util::Image::DPI
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties{'DPI'} || $LaTeXML::Util::Image::DPI
$properties{'DPI'} $LaTeXML::Util::Image::DPI
Truth table0%0/3
A || B
A= $properties{'DPI'}B= $LaTeXML::Util::Image::DPI
ABresult
1X1
011
000
306 0 ▶ my $magnify = $properties{magnify} || 1;
Condition0%0/2
$properties{'magnify'} || 1
l!l
00
MC/DC0%0/1
$properties{'magnify'} || 1
$properties{'magnify'}
Truth table0%0/2
A
A= $properties{'magnify'}
Aresult
11
00
307 0 ▶ my $upsample = $properties{upsample} || 2;
Condition0%0/2
$properties{'upsample'} || 2
l!l
00
MC/DC0%0/1
$properties{'upsample'} || 2
$properties{'upsample'}
Truth table0%0/2
A
A= $properties{'upsample'}
Aresult
11
00
308 0 ▶ my $zoomout = $properties{zoomout} || 1;
Condition0%0/2
$properties{'zoomout'} || 1
l!l
00
MC/DC0%0/1
$properties{'zoomout'} || 1
$properties{'zoomout'}
Truth table0%0/2
A
A= $properties{'zoomout'}
Aresult
11
00
309 0 my ($xprescale, $yprescale) = (1, 1);
310  
311 # # Wastefully preread the image to get it's initial size
312 0 my ($w0, $h0) = image_size($source);
313 0 ▶ return unless $w0 && $h0;
Branch: 1 branch
BranchTrueFalseTotal
unless $w0 and $h0 0 0 0
Condition0%0/3
$w0 and $h0
!ll&&!rl&&r
000
MC/DC0%0/2
$w0 and $h0
$w0 $h0
Truth table0%0/3
A and B
A= $w0B= $h0
ABresult
0X0
100
111
314 Debug("Processing $source initially $w0 x $h0,"
315 0 ▶ . "w/ DPI=$dpi, magnify=$magnify, upsample=$upsample, zoomout=$zoomout") if $LaTeXML::DEBUG{images};
Branch: 1 branch
BranchTrueFalseTotal
if $LaTeXML::DEBUG{"images"} 0 0 0
316 0 my @transform = @$transform;
317 # Establish the scaling necessary to carry out the transformation w/o loosing resolution
318 # This is particularly for vector formats before rasterizing.
319 # If image is vector & transform starts with scaling, rasterize at at least desired size
320 # We REALLY should go through the ENTIRE transformation detecting the scaling,
321 # but it's tricky to account for the prescale when carrying out the actual transformation!
322 0 ▶ if ($properties{prescale}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($properties{"prescale"}) 0 0 0
323 0 ▶ while (@transform && ($transform[0]->[0] =~ /^scale/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@transform and $transform[0][0] =~ /^scale/) 0 0 0
Condition0%0/3
@transform and $transform[0][0] =~ /^scale/
!ll&&!rl&&r
000
MC/DC0%0/2
@transform and $transform[0][0] =~ /^scale/
@transform $transform[0][0] =~ /^scale/
Truth table0%0/3
A and B
A= @transformB= $transform[0][0] =~ /^scale/
ABresult
0X0
100
111
324 0 my ($op, $a1, $a2, $a3, $a4) = @{ shift(@transform) };
325 0 ▶ if ($op eq 'scale') { # $a1 => scale
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq 'scale') { } 0 0 0
elsif ($op eq 'scale-to') { } 0 0 0
326 0 ▶ $xprescale *= $a1; $yprescale *= ($a2 || $a1); }
Condition0%0/3
$a2 || $a1
l!l&&r!l&&!r
000
MC/DC0%0/2
$a2 || $a1
$a2 $a1
Truth table0%0/3
A || B
A= $a2B= $a1
ABresult
1X1
011
000
327 elsif ($op eq 'scale-to') {
328 # $a1 => width (pts), $a2 => height (pts), $a3 => preserve aspect ratio.
329 0 ▶ if ($a3) { # If keeping aspect ratio, ignore the most extreme request
Branch: 1 branch
BranchTrueFalseTotal
if ($a3) 0 0 0
330 0 ▶ if ($a1 / $w0 < $a2 / $h0) { $a2 = $h0 * $a1 / $w0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($a1 / $w0 < $a2 / $h0) { } 0 0 0
331 0 else { $a1 = $w0 * $a2 / $h0; } }
332 0 $xprescale *= $a1 * $dpi / $w0 / 72.27; $yprescale *= $a2 * $dpi / $h0 / 72.27; } }
333 0 ▶ Debug("Prescaling factors $xprescale x $yprescale") if $LaTeXML::DEBUG{images}; }
Branch: 1 branch
BranchTrueFalseTotal
if $LaTeXML::DEBUG{"images"} 0 0 0
334 # At this point, we conceivably could clamp the resolution to limit
335 # to a maximum (or minimum) size, either for display or to reduce needed resources.
336 # We'd presumably want to adjust (one of) the scaling factors.
337 0 $dpi *= $magnify * $upsample;
338 0 ▶ my $background = $properties{background} || $BACKGROUND;
Condition0%0/3
$properties{'background'} || $LaTeXML::Util::Image::BACKGROUND
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties{'background'} || $LaTeXML::Util::Image::BACKGROUND
$properties{'background'} $LaTeXML::Util::Image::BACKGROUND
Truth table0%0/3
A || B
A= $properties{'background'}B= $LaTeXML::Util::Image::BACKGROUND
ABresult
1X1
011
000
339 0 ▶ my $image = image_read($source, antialias => 1,
Branch: 1 branch
BranchTrueFalseTotal
my $image = image_read($source, "antialias", 1, "density", $dpi * $xprescale . 'x' . $dpi * $yprescale) or return 0 0 0
340 density => $dpi * $xprescale . 'x' . $dpi * $yprescale,
341 ) or return;
342 0 my ($w, $h) = image_getvalue($image, 'width', 'height');
343 # Get some defaults from the read-in image.
344 0 my ($imagedpi) = image_getvalue($image, 'x-resolution');
345 # image_setvalue($image,debug=>'exception');
346 0 my ($bg) = image_getvalue($image, 'mattecolor');
347 0 ▶ $background = "rgba($bg)" if $bg; # Use background from image, if any.
Branch: 1 branch
BranchTrueFalseTotal
if $bg 0 0 0
348 0 my ($hasalpha) = image_getvalue($image, 'matte');
349 0 ▶ Debug("Read $source to $w x $h") if $LaTeXML::DEBUG{images};
Branch: 1 branch
BranchTrueFalseTotal
if $LaTeXML::DEBUG{"images"} 0 0 0
350  
351 0 ▶ if ($properties{autocrop}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($properties{"autocrop"}) 0 0 0
352 0 ▶ image_internalop($image, 'Trim') or return;
Branch: 1 branch
BranchTrueFalseTotal
image_internalop($image, "Trim") or return 0 0 0
353 0 ($w, $h) = image_getvalue($image, 'width', 'height');
354 0 ▶ Debug(" Autocrop to $w x $h") if $LaTeXML::DEBUG{images}; }
Branch: 1 branch
BranchTrueFalseTotal
if $LaTeXML::DEBUG{"images"} 0 0 0
355  
356 0 my $orig_ncolors = image_getvalue($image, 'colors');
357  
358 0 ▶ if (!$orig_ncolors || !($w && $h)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($orig_ncolors and ($w and $h)) 0 0 0
Condition0%0/6
$orig_ncolors and ($w and $h)
!ll&&!rl&&r
000
$w and $h
!ll&&!rl&&r
000
MC/DC0%0/3
$orig_ncolors and ($w and $h)
$orig_ncolors $w $h
Truth table0%0/4
A and B and C
A= $orig_ncolorsB= $wC= $h
ABCresult
0XX0
10X0
1100
1111
359 0 ▶ Debug(" Image is now empty; skipping") if $LaTeXML::DEBUG{images};
Branch: 1 branch
BranchTrueFalseTotal
if $LaTeXML::DEBUG{"images"} 0 0 0
360 0 return; }
361  
362 0 foreach my $trans (@transform) {
363 0 my ($op, $a1, $a2, $a3, $a4) = @$trans;
364 0 ▶ return unless $w && $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $w and $h 0 0 0
Condition0%0/3
$w and $h
!ll&&!rl&&r
000
MC/DC0%0/2
$w and $h
$w $h
Truth table0%0/3
A and B
A= $wB= $h
ABresult
0X0
100
111
365 0 ▶ if ($op eq 'scale') { # $a1 => scale
Branch: 5 branches
BranchTrueFalseTotal
if ($op eq 'scale') { } 0 0 0
elsif ($op eq 'scale-to') { } 0 0 0
elsif ($op eq 'rotate') { } 0 0 0
elsif ($op eq 'reflect') { } 0 0 0
elsif ($op eq 'trim' or $op eq 'clip') { } 0 0 0
366 0 ▶ ($w, $h) = (ceil($w * $a1), ceil($h * ($a2 || $a1)));
Condition0%0/3
$a2 || $a1
l!l&&r!l&&!r
000
MC/DC0%0/2
$a2 || $a1
$a2 $a1
Truth table0%0/3
A || B
A= $a2B= $a1
ABresult
1X1
011
000
367 0 ▶ return unless $w && $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $w and $h 0 0 0
Condition0%0/3
$w and $h
!ll&&!rl&&r
000
MC/DC0%0/2
$w and $h
$w $h
Truth table0%0/3
A and B
A= $wB= $h
ABresult
0X0
100
111
368 0 ▶ image_internalop($image, 'Scale', width => $w, height => $h) or return;
Branch: 1 branch
BranchTrueFalseTotal
image_internalop($image, "Scale", "width", $w, "height", $h) or return 0 0 0
369 0 ▶ Debug(" Scale by $a1 " . ($a2 ? " x $a2" : '') . " => $w x $h") if $LaTeXML::DEBUG{images}; }
Branch: 2 branches
BranchTrueFalseTotal
if $LaTeXML::DEBUG{"images"} 0 0 0
$a2 ? : 0 0 0
370 elsif ($op eq 'scale-to') {
371 # $a1 => width (pts), $a2 => height (pts), $a3 => preserve aspect ratio.
372 0 ▶ if ($a3) { # If keeping aspect ratio, ignore the most extreme request
Branch: 1 branch
BranchTrueFalseTotal
if ($a3) 0 0 0
373 0 ▶ if ($a1 / $w < $a2 / $h) { $a2 = $h * $a1 / $w; }
Branch: 1 branch
BranchTrueFalseTotal
if ($a1 / $w < $a2 / $h) { } 0 0 0
374 0 else { $a1 = $w * $a2 / $h; } }
375 0 ($w, $h) = (ceil($a1 * $dpi / 72.27), ceil($a2 * $dpi / 72.27));
376 0 ▶ image_internalop($image, 'Scale', width => $w, height => $h) or return;
Branch: 1 branch
BranchTrueFalseTotal
image_internalop($image, "Scale", "width", $w, "height", $h) or return 0 0 0
377 0 ▶ Debug(" Scale to $w x $h") if $LaTeXML::DEBUG{images}; }
Branch: 1 branch
BranchTrueFalseTotal
if $LaTeXML::DEBUG{"images"} 0 0 0
378 elsif ($op eq 'rotate') {
379 0 ▶ image_internalop($image, 'Rotate', degrees => -$a1, background => $background) or return;
Branch: 1 branch
BranchTrueFalseTotal
image_internalop($image, "Rotate", "degrees", -$a1, "background", $background) or return 0 0 0
380 0 ($w, $h) = image_getvalue($image, 'width', 'height');
381 0 ▶ return unless $w && $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $w and $h 0 0 0
Condition0%0/3
$w and $h
!ll&&!rl&&r
000
MC/DC0%0/2
$w and $h
$w $h
Truth table0%0/3
A and B
A= $wB= $h
ABresult
0X0
100
111
382 0 ▶ Debug(" Rotate by $a1 => $w x $h") if $LaTeXML::DEBUG{images}; }
Branch: 1 branch
BranchTrueFalseTotal
if $LaTeXML::DEBUG{"images"} 0 0 0
383 elsif ($op eq 'reflect') {
384 0 ▶ image_internalop($image, 'Flop') or return;
Branch: 1 branch
BranchTrueFalseTotal
image_internalop($image, "Flop") or return 0 0 0
385 0 ▶ Debug(" Reflext image => $w x $h") if $LaTeXML::DEBUG{images}; }
Branch: 1 branch
BranchTrueFalseTotal
if $LaTeXML::DEBUG{"images"} 0 0 0
386 # In the following two, note that TeX's coordinates are relative to lower left corner,
387 # but ImageMagick's coordinates are relative to upper left.
388 ▶ elsif (($op eq 'trim') || ($op eq 'clip')) {
Condition0%0/3
$op eq "trim" or $op eq "clip"
l!l&&r!l&&!r
000
MC/DC0%0/2
$op eq "trim" or $op eq "clip"
$op eq "trim" $op eq "clip"
Truth table0%0/3
A or B
A= $op eq "trim"B= $op eq "clip"
ABresult
1X1
011
000
389 0 my ($x0, $y0, $ww, $hh);
390 # Use the image's dpi for trim & clip!
391 0 ▶ my $idppt = (defined $imagedpi ? $imagedpi : $dpi) / 72.27;
Branch: 1 branch
BranchTrueFalseTotal
defined $imagedpi ? : 0 0 0
392 0 ▶ if ($op eq 'trim') { # Amount to trim: a1=left, a2=bottom, a3=right, a4=top
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq 'trim') { } 0 0 0
393 0 ($x0, $y0, $ww, $hh) = (floor($a1 * $idppt), floor($a4 * $idppt),
394 ceil($w - ($a1 + $a3) * $idppt), ceil($h - ($a4 + $a2) * $idppt)); }
395 else { # BBox: a1=left, a2=bottom, a3=right, a4=top
396 0 ($x0, $y0, $ww, $hh) = (floor($a1 * $idppt), floor($h - $a4 * $idppt),
397 ceil(($a3 - $a1) * $idppt), ceil(($a4 - $a2) * $idppt)); }
398  
399 0 ▶ if (($x0 > 0) || ($y0 > 0) || ($x0 + $ww < $w) || ($y0 + $hh < $h)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x0 > 0 or $y0 > 0 or $x0 + $ww < $w or $y0 + $hh < $h) 0 0 0
Condition0%0/9
$x0 > 0 or $y0 > 0 or $x0 + $ww < $w or $y0 + $hh < $h
l!l&&r!l&&!r
000
$x0 > 0 or $y0 > 0 or $x0 + $ww < $w
l!l&&r!l&&!r
000
$x0 > 0 or $y0 > 0
l!l&&r!l&&!r
000
MC/DC0%0/4
$x0 > 0 or $y0 > 0 or $x0 + $ww < $w or $y0 + $hh < $h
$x0 > 0 $y0 > 0 $x0 + $ww < $w $y0 + $hh < $h
Truth table0%0/5
A or B or C or D
A= $x0 > 0B= $y0 > 0C= $x0 + $ww < $wD= $y0 + $hh < $h
ABCDresult
1XXX1
01XX1
001X1
00011
00000
400 0 my $x0p = max($x0, 0); $x0 = min($x0, 0);
401 0 my $y0p = max($y0, 0); $y0 = min($y0, 0);
402 0 ▶ image_internalop($image, 'Crop', x => $x0p, width => min($ww, $w - $x0p),
Branch: 1 branch
BranchTrueFalseTotal
image_internalop($image, "Crop", "x", $x0p, "width", &min($ww, $w - $x0p), "y", $y0p, "height", &min($hh, $h - $y0p)) or return 0 0 0
403 y => $y0p, height => min($hh, $h - $y0p)) or return;
404 0 $w = min($ww + $x0, $w - $x0p);
405 0 $h = min($hh + $y0, $h - $y0p);
406 0 ▶ Debug(" Trim/Clip to $w x $h @ $x0p, $y0p") if $LaTeXML::DEBUG{images}; }
Branch: 1 branch
BranchTrueFalseTotal
if $LaTeXML::DEBUG{"images"} 0 0 0
407 # No direct `padding' operation in ImageMagick
408 # BUT, trim & clip really shouldn't need to pad???
409 # And besides, this composition seems to mangle the colormap (depending on background)
410 # my $nimage = image_read("xc:$background", size => "$ww x $hh") or return;
411 # image_internalop($nimage, 'Composite', image => $image, compose => 'over',
412 # x => -$x0, y => -$y0) or return;
413 # $notes .= " compose to $ww x $hh at $x0,$y0";
414 # $image = $nimage;
415 # ($w, $h) = ($ww, $hh);
416 } }
417  
418 0 ▶ if ($properties{transparent} && !$hasalpha) {
Branch: 1 branch
BranchTrueFalseTotal
if ($properties{"transparent"} and not $hasalpha) 0 0 0
Condition0%0/3
$properties{"transparent"} and not $hasalpha
!ll&&!rl&&r
000
MC/DC0%0/2
$properties{"transparent"} and not $hasalpha
$properties{"transparent"} not $hasalpha
Truth table0%0/3
A and not(B)
A= $properties{"transparent"}B= not $hasalpha
ABresult
0X0
100
111
419 0 ▶ image_internalop($image, 'Transparent', $background) or return; }
Branch: 1 branch
BranchTrueFalseTotal
image_internalop($image, "Transparent", $background) or return 0 0 0
420  
421 0 ▶ if (my $curr_ncolors = image_getvalue($image, 'colors')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $curr_ncolors = image_getvalue($image, "colors")) 0 0 0
422 0 ▶ if (my $req_ncolors = $properties{ncolors}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $req_ncolors = $properties{"ncolors"}) 0 0 0
423 0 ▶ $req_ncolors = int($orig_ncolors * $1 / 100) if $req_ncolors =~ /^([\d]*)\%$/;
Branch: 1 branch
BranchTrueFalseTotal
if $req_ncolors =~ /^([\d]*)\%$/ 0 0 0
424 0 ▶ if ($req_ncolors < $curr_ncolors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_ncolors < $curr_ncolors) 0 0 0
425 0 ▶ image_internalop($image, 'Quantize', colors => $req_ncolors) or return; } } }
Branch: 1 branch
BranchTrueFalseTotal
image_internalop($image, "Quantize", "colors", $req_ncolors) or return 0 0 0
426  
427 0 ▶ if (my $quality = $properties{quality}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $quality = $properties{"quality"}) 0 0 0
428 0 ▶ image_setvalue($image, quality => $properties{quality}) or return; }
Branch: 1 branch
BranchTrueFalseTotal
image_setvalue($image, "quality", $properties{"quality"}) or return 0 0 0
429  
430 0 ▶ if ($properties{prescale} && ($upsample != 1)) { # Now downsample IF actually upsampled!
Branch: 1 branch
BranchTrueFalseTotal
if ($properties{"prescale"} and $upsample != 1) 0 0 0
Condition0%0/3
$properties{"prescale"} and $upsample != 1
!ll&&!rl&&r
000
MC/DC0%0/2
$properties{"prescale"} and $upsample != 1
$properties{"prescale"} $upsample != 1
Truth table0%0/3
A and B
A= $properties{"prescale"}B= $upsample != 1
ABresult
0X0
100
111
431 0 ▶ image_internalop($image, 'Scale', geometry => $w / $upsample . 'x' . $h / $upsample) or return;
Branch: 1 branch
BranchTrueFalseTotal
image_internalop($image, "Scale", "geometry", $w / $upsample . 'x' . $h / $upsample) or return 0 0 0
432 0 ($w, $h) = image_getvalue($image, 'width', 'height');
433 0 ▶ Debug(" Downsampled to $w x $h") if $LaTeXML::DEBUG{images}; }
Branch: 1 branch
BranchTrueFalseTotal
if $LaTeXML::DEBUG{"images"} 0 0 0
434  
435 0 my ($watt, $hatt) = ($w / $zoomout, $h = $h / $zoomout);
436 0 ▶ Debug("Transformed $source final size $w x $h, displayed as $watt x $hatt") if $LaTeXML::DEBUG{images};
Branch: 1 branch
BranchTrueFalseTotal
if $LaTeXML::DEBUG{"images"} 0 0 0
437 0 return ($image, $watt, $hatt); }
438  
439 # Wrap up ImageMagick's methods to give more useful & consistent error handling.
440 # These all return non-zero on success!
441 # so, you generally want to do image_internalop(...) or return;
442  
443 # This reads a new image, setting the given properties BEFORE ingesting the image data.
444 0 ▶sub image_read {
Subroutine image_read: not called CC=6 SCAR=37.4
Pod undocumented
445 0 my ($source, @args) = @_;
446 0 ▶ if (!$source) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($source) 0 0 0
447 0 Error('imageprocessing', 'read', undef, "No image source given"); return; }
448 0 ▶ return unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 0 0 0
449 0 my $image = image_object();
450 # Just in case this is pdf, set this option; ImageMagick defaults to MediaBox (Wrong!!!)
451 0 ▶ image_internalop($image, 'Set', option => 'pdf:use-cropbox=true') or return;
Branch: 1 branch
BranchTrueFalseTotal
image_internalop($image, "Set", "option", "pdf:use-cropbox=true") or return 0 0 0
452 0 ▶ image_internalop($image, 'Set', @args) or return;
Branch: 1 branch
BranchTrueFalseTotal
image_internalop($image, "Set", @args) or return 0 0 0
453 0 ▶ image_internalop($image, 'Read', $source) or return;
Branch: 1 branch
BranchTrueFalseTotal
image_internalop($image, "Read", $source) or return 0 0 0
454 0 return $image; }
455  
456 0 ▶sub image_write {
Subroutine image_write: not called CC=5 SCAR=34.0
Pod undocumented
457 0 my ($image, $destination) = @_;
458 0 ▶ if (!$image) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($image) 0 0 0
459 0 Error('imageprocessing', 'write', undef, "No image object!"); return; }
460 0 ▶ if (!$destination) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($destination) 0 0 0
461 0 Error('imageprocessing', 'write', undef, "No image destination!"); return; }
462 # In the perverse case that we've ended up with a sequence of images; flatten them.
463 0 ▶ if (@$image > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$image > 1) 0 0 0
464 0 my $fimage = $image->Flatten(); # Just in case we ended up with pieces!?!?!?
465 0 ▶ $image = $fimage if $fimage; }
Branch: 1 branch
BranchTrueFalseTotal
if $fimage 0 0 0
466 0 return image_internalop($image, 'Write', filename => $destination); }
467  
468 0 ▶sub image_getvalue {
Subroutine image_getvalue: not called CC=2 SCAR=17.9
Pod undocumented
469 0 my ($image, @args) = @_;
470 0 ▶ if (!$image) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($image) 0 0 0
471 0 Error('imageprocessing', 'getvalue', undef, "No image object!"); return; }
472 0 my @values = $image->Get(@args);
473 0 return @values; }
474  
475 0 ▶sub image_setvalue {
Subroutine image_setvalue: not called CC=2 SCAR=17.9
Pod undocumented
476 0 my ($image, @args) = @_;
477 0 ▶ if (!$image) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($image) 0 0 0
478 0 Error('imageprocessing', 'setvalue', undef, "No image object!"); return; }
479 0 return image_internalop($image, 'Set', @args); }
480  
481 # Apparently ignorable warnings from Image::Magick
482 1 our %ignorable = map { $_ => 1; } (
483 350, # profile 'icc' not permitted on grayscale PNG
484 );
485  
486 0 ▶sub image_internalop {
Subroutine image_internalop: not called CC=6 SCAR=37.4
Pod undocumented
487 0 my ($image, $operation, @args) = @_;
488 0 ▶ if (!$image) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($image) 0 0 0
489 0 Error('imageprocessing', 'internal', undef, "No image object!"); return; }
490 0 my $retval = $image->$operation(@args);
491 0 ▶ return 1 unless $retval;
Branch: 1 branch
BranchTrueFalseTotal
unless $retval 0 0 0
492 0 my $retcode = 999;
493 0 ▶ if ($retval =~ /(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval =~ /(\d+)/) 0 0 0
494 0 $retcode = $1; }
495 0 ▶ if ($retcode < 400) { # Warning
Branch: 1 branch
BranchTrueFalseTotal
if ($retcode < 400) { } 0 0 0
496 Warn('imageprocessing', $operation, undef,
497 "Image processing operation $operation (" . join(', ', @args) . ") returned $retval")
498 0 ▶ unless $ignorable{$retcode};
Branch: 1 branch
BranchTrueFalseTotal
unless $LaTeXML::Util::Image::ignorable{$retcode} 0 0 0
499 0 return 1; }
500 else { # Error
501 0 Error('imageprocessing', $operation, undef,
502 "Image processing operation $operation (" . join(', ', @args) . ") returned $retval");
503 0 return 0; } }
504  
505 #======================================================================
506 1 1;
« lib/LaTeXML/Common/Config.pm ↑ Summary lib/LaTeXML/Post/MathML.pm »