lib/LaTeXML/Util/Image.pm untested

filter: Statementstmt 0.0% 0 / 340 Branchbran 0.0% 0 / 206 Conditioncond 0.0% 0 / 147 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 740 CC 104 SCAR 62.8
CC
104
Cov
0%
image_graphicx_complex
1190.0
image_graphicx_parse
930.0
image_graphicx_size
110.0
CRAP 534.5
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 use strict;
14 use warnings;
15 use LaTeXML::Global;
16 use LaTeXML::Common::Error;
17 use LaTeXML::Common::Dimension;
18 use LaTeXML::Util::Pathname;
19 use List::Util qw(min max);
20 use Image::Size;
21 use POSIX;
22 use base qw(Exporter);
23 our @EXPORT = (
24 qw( &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 our $DPI = 90; # [CONSTANT]
38 our $DOTS_PER_POINT = ($DPI / 72.0); # [CONSTANT] Dots per point.
39 our $BACKGROUND = "#FFFFFF"; # [CONSTANT]
40  
41 # These environment variables can be used to limit the amount
42 # of time & space used by ImageMagick. They are particularly useful
43 # when ghostscript becomes involved in converting postscript or pdf.
44 # HOWEVER, there are indications that the time limit (at least)
45 # is being measured against the whole latexml process, not just image processing.
46 # Thus they aren't really that useful here.
47 # They probably are useful in a server context, however, so I'll leave the comments.
48 # $ENV{MAGICK_DISK_LIMIT} = "2GiB" unless defined $ENV{MAGICK_DISK_LIMIT};
49 # $ENV{MAGICK_MEMORY_LIMIT} = "512MiB" unless defined $ENV{MAGICK_MEMORY_LIMIT};
50 # $ENV{MAGICK_MAP_LIMIT} = "1GiB" unless defined $ENV{MAGICK_MAP_LIMIT};
51 # $ENV{MAGICK_TIME_LIMIT} = "300" unless defined $ENV{MAGICK_TIME_LIMIT};
52  
53 # Note that Image::Size may, itself, use Image::Magick, if available,
54 # as a fallback for getting image size & type!!!
55 # However, it seems not to recognize file types with extension .EPS (uppercase), eg!
56 sub image_type {
57 my ($pathname) = @_;
58 my ($w, $h, $t) = imgsize($pathname);
59 # even though Image::Size CLAIMS to use Image::Magick as fallback... needs tickling?
60 if (!(defined $w) && !(defined $h) && image_can_image()) { # try harder!
61 my $image = image_read($pathname) or return;
62 ($t) = image_getvalue($image, 'format'); }
63 # Note that Image::Magick (sometimes) returns "descriptive" types
64 # (but I can't find a list anywhere!)
65 $t = 'eps' if $t && $t =~ /postscript/i;
66 return (defined $t ? lc($t) : undef); }
67  
68 sub image_size {
69 my ($pathname) = @_;
70 my ($w, $h, $t) = imgsize($pathname);
71 return ($w, $h) if $w && $h;
72 if (image_can_image()) { # try harder!
73 my $image = image_read($pathname) or return;
74 return image_getvalue($image, 'width', 'height'); } }
75  
76 # This will be set once we've found an Image processing library to use [Daemon safe]
77 our $IMAGECLASS; # cached class if we found one that works. [CONFIGURABLE?]
78 my @MagickClasses = (qw(Graphics::Magick Image::Magick)); # CONSTANT
79  
80 sub image_classes {
81 return @MagickClasses; }
82  
83 sub image_can_image {
84 my ($pathname) = @_;
85 if (!$IMAGECLASS) {
86 foreach my $class (@MagickClasses) {
87 my $module = $class . ".pm";
88 $module =~ s/::/\//g;
89 my $object = eval {
90 local $LaTeXML::IGNORE_ERRORS = 1;
91 require $module; $class->new(); };
92 if ($object) {
93 $IMAGECLASS = $class;
94 last; } } }
95 return $IMAGECLASS; }
96  
97 # return an image object (into which you can read), if possible.
98 sub image_object {
99 my (%properties) = @_;
100 return unless image_can_image();
101 my $image = $IMAGECLASS->new(%properties);
102 return $image; }
103  
104 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 # Working with Image transformation options from the graphic(s|x) packages
106 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 # Note that viewport is supposed to be relative to bounding box,
108 # and EITHER ONE can cause clipping, if clip=true.
109 # However, apparently the INTENT of bounding box is simply to
110 # supply one, if one can't be found, in order to determine the image size.
111 # In our case, we may end up getting a gif, jpeg, etc, whose origin is always 0,0,
112 # and whose size is clear; also postscript figures sizes will be determined
113 # by ghostview(?). Another use of setting a bounding box, when clip=false,
114 # is to make the image lay ontop of neighboring text. This isn't quite
115 # possible in HTML, other than possibly through some tricky CSS.
116 # Besides, I'd like to avoid reading the bb file, if I can.
117 # --- So, for all these reasons, we simply ignore bounding box here.
118  
119 sub image_graphicx_parse {
120 my ($transformstring, %options) = @_;
121 return [] unless $transformstring;
122 local $_ = $_;
123 my ($v, $clip, $trim, $width, $height, $xscale, $yscale,
124 $aspect, $angle, $rotfirst, $mag, @bb, @vp,) = ('', '', '', 0, 0, 0, 0, '', 0, '', 1, 0);
125 my @unknown = ();
126 my @ignore = @{ $options{ignore_options} || [] };
127 foreach (split(',', $transformstring || '')) {
128 if (/^\s*(\w+)(?:=\s*(.*))?\s*$/) {
129 $_ = $1; $v = $2 || '';
130 my $op = $_;
131 if (grep { $op eq $_ } @ignore) { } # Ignore this option
132 elsif (/^bb$/) { @bb = map { to_bp($_) } split(' ', $v); }
133 elsif (/^bb(?:ll|ur)(?:x|y)$/) { $bb[2 * /ur/ + /y/] = to_bp($v); }
134 elsif (/^nat(?:width|height)$/) { $bb[2 + /width/] = to_bp($v); }
135 elsif (/^viewport$/) { @vp = map { to_bp($_) } split(' ', $v); $trim = 0; }
136 elsif (/^trim$/) { @vp = map { to_bp($_) } split(' ', $v); $trim = 1; }
137 elsif (/^clip$/) { $clip = !($v eq 'false'); }
138 elsif (/^keepaspectratio$/) { $aspect = !($v eq 'false'); }
139 elsif (/^width$/) { $width = to_bp($v); }
140 elsif (/^(?:total)?height$/) { $height = to_bp($v); }
141 elsif (/^scale$/) { $xscale = $yscale = $v; }
142 elsif (/^xscale$/) { $xscale = $v; }
143 elsif (/^yscale$/) { $yscale = $v; }
144 elsif (/^angle$/) { $angle = $v; $rotfirst = !($width || $height || $xscale || $yscale); }
145 elsif (/^origin$/) { } # ??
146 # Non-standard option
147 elsif (/^magnification$/) { $mag = $v; }
148 else { push(@unknown, [$op, $v]); } }
149 else { } } # ?
150 # --------------------------------------------------
151 # Now, compile the options into a sequence of `transformations'.
152 # Note: the order of rotation & scaling is significant,
153 # but the order of various clipping options w.r.t rotation or scaling is not.
154 my @transform = ();
155 # We ignore viewport & trim if clip isn't set, since in that case we shouldn't
156 # actually remove anything from the image (and there's no way to have the image
157 # overlap neighboring text, etc, in current HTML).
158 push(@transform, [($trim ? 'trim' : 'clip'), @vp]) if (@vp && $clip);
159 push(@transform, ['rotate', $angle]) if ($rotfirst && $angle); # Rotate before scaling?
160 if ($width && $height) { push(@transform, ['scale-to', $mag * $width, $mag * $height, $aspect]); }
161 elsif ($width) { push(@transform, ['scale-to', $mag * $width, 999999, 1]); }
162 elsif ($height) { push(@transform, ['scale-to', 999999, $mag * $height, 1]); }
163 elsif ($xscale && $yscale) { push(@transform, ['scale', $mag * $xscale, $mag * $yscale]); }
164 elsif ($xscale) { push(@transform, ['scale', $mag * $xscale, $mag]); }
165 elsif ($yscale) { push(@transform, ['scale', $mag, $mag * $yscale]); }
166 elsif ($mag != 1) { push(@transform, ['scale', $mag, $mag]); }
167 push(@transform, ['rotate', $angle]) if (!$rotfirst && $angle); # Rotate after scaling?
168 # ----------------------
169 return [@transform, @unknown]; }
170  
171 my %BP_conversions = ( # CONSTANT
172 pt => 72 / 72.27, pc => 12 / 72.27, in => 72, bp => 1,
173 cm => 72 / 2.54, mm => 72 / 25.4, dd => (72 / 72.27) * (1238 / 1157),
174 cc => 12 * (72 / 72.27) * (1238 / 1157), sp => 72 / 72.27 / 65536);
175  
176 sub to_bp {
177 my ($x) = @_;
178 if ($x =~ /^\s*([+-]?[\d\.]+)(\w*)\s*$/) {
179 my ($v, $u) = ($1, $2);
180 $u =~ s/^true//;
181 return $v * ($u ? $BP_conversions{$u} : 1); }
182 else {
183 return 1; } }
184  
185 #======================================================================
186 # Compute the effective size of a graphic transformed in graphicx style.
187 # [this is a simplification of image_graphicx_complex]
188 sub image_graphicx_size {
189 my ($source, $transform, %properties) = @_;
190 my $dppt = $properties{dppt} || $DOTS_PER_POINT;
191 my ($w, $h) = image_size($source);
192 return unless $w && $h;
193 foreach my $trans (@$transform) {
194 my ($op, $a1, $a2, $a3, $a4) = @$trans;
195 if ($op eq 'scale') { # $a1 => scale
196 ($w, $h) = (ceil($w * $a1), ceil($h * ($a2 || $a1))); }
197 elsif ($op eq 'scale-to') {
198 # $a1 => width (pts), $a2 => height (pts), $a3 => preserve aspect ratio.
199 if ($a3) { # If keeping aspect ratio, ignore the most extreme request
200 if ($a1 / $w < $a2 / $h) { $a2 = $h * $a1 / $w; }
201 else { $a1 = $w * $a2 / $h; } }
202 ($w, $h) = (ceil($a1 * $dppt), ceil($a2 * $dppt)); }
203 elsif ($op eq 'rotate') {
204 my $rad = -$a1 * 3.1415926 / 180; # close enough
205 my $s = sin($rad);
206 my $c = cos($rad);
207 ($w, $h) = (abs($w * $c) + abs($h * $s), abs($w * $s) + abs($h * $c)); }
208 elsif ($op eq 'reflect') { }
209 # In the following two, note that TeX's coordinates are relative to lower left corner,
210 # but ImageMagick's coordinates are relative to upper left.
211 elsif (($op eq 'trim') || ($op eq 'clip')) {
212 my ($x0, $y0, $ww, $hh);
213 if ($op eq 'trim') { # Amount to trim: a1=left, a2=bottom, a3=right, a4=top
214 ($x0, $y0, $ww, $hh) = (floor($a1 * $dppt), floor($a4 * $dppt),
215 ceil($w - ($a1 + $a3) * $dppt), ceil($h - ($a4 + $a2) * $dppt)); }
216 else { # BBox: a1=left, a2=bottom, a3=right, a4=top
217 ($x0, $y0, $ww, $hh) = (floor($a1 * $dppt), floor($h - $a4 * $dppt),
218 ceil(($a3 - $a1) * $dppt), ceil(($a4 - $a2) * $dppt)); }
219 ($w, $h) = ($ww, $hh);
220 } }
221 return ($w, $h); }
222  
223 # Totally doesn't belong here, but want to share...
224 sub image_graphicx_sizer {
225 my ($whatsit) = @_;
226 if (my $candidates = $whatsit->getProperty('candidates')) {
227 foreach my $source (split(/,/, $candidates)) {
228 if (!pathname_is_absolute($source)) {
229 if (my $base = $STATE->lookupValue('SOURCEDIRECTORY')) {
230 $source = pathname_concat($base, $source); } }
231 # Skip anything that a lower level imgsize can't understand
232 my $options = $whatsit->getProperty('options');
233 local $LaTeXML::IGNORE_ERRORS = 1;
234 my ($w, $h) = image_graphicx_size($source, image_graphicx_parse($options));
235 return (Dimension($w / $DOTS_PER_POINT . 'pt'), Dimension($h / $DOTS_PER_POINT . 'pt'), Dimension(0)) if $w; } }
236 return (Dimension(0), Dimension(0), Dimension(0)); }
237  
238 #======================================================================
239 # Trivial scaling.
240 # When an image can be dealt with by simple scaling without "editting" the image.
241 # Compute the desired image size (width,height)
242 # No need to necessarily read the image!
243  
244 # Check if the transform (parsed from above) is trivial
245 sub image_graphicx_is_trivial {
246 my ($transform) = @_;
247 return !grep { !($_->[0] =~ /^scale/) } @$transform; }
248  
249 # Make the transform (parsed from above) trivial
250 # by removing any non-scaling operations!
251 sub image_graphicx_trivialize {
252 my ($transform) = @_;
253 return [grep { ($_->[0] =~ /^scale/) } @$transform]; }
254  
255 # sub trivial_scaling {
256 sub image_graphicx_trivial {
257 my ($source, $transform, %properties) = @_;
258 my ($w, $h) = image_size($source);
259 return unless $w && $h;
260 my $dppt = $properties{dppt} || $DOTS_PER_POINT;
261 foreach my $trans (@$transform) {
262 my ($op, $a1, $a2, $a3, $a4) = @$trans;
263 if ($op eq 'scale') { # $a1 => scale
264 ($w, $h) = (ceil($w * $a1), ceil($h * ($a2 || $a1))); }
265 elsif ($op eq 'scale-to') { # $a1 => width, $a2 => height, $a3 => preserve aspect ratio.
266 if ($a3) { # If keeping aspect ratio, ignore the most extreme request
267 if ($a1 / $w < $a2 / $h) { $a2 = $h * $a1 / $w; }
268 else { $a1 = $w * $a2 / $h; } }
269 ($w, $h) = (ceil($a1 * $dppt), ceil($a2 * $dppt)); } }
270 return ($w, $h); }
271  
272 #======================================================================
273 # Transform the image, returning (image,width,height);
274 # sub complex_transform {
275 sub image_graphicx_complex {
276 my ($source, $transform, %properties) = @_;
277 my $dppt = $properties{dppt} || $DOTS_PER_POINT;
278 my $background = $properties{background} || $BACKGROUND;
279 my $image = image_read($source, antialias => 1) or return;
280 # Get some defaults from the read-in image.
281 my ($imagedpi) = image_getvalue($image, 'x-resolution');
282 # image_setvalue($image,debug=>'exception');
283 my ($bg) = image_getvalue($image, 'transparent-color');
284 $background = "rgba($bg)" if $bg; # Use background from image, if any.
285 my ($hasalpha) = image_getvalue($image, 'matte');
286  
287 image_internalop($image, 'Trim') or return if $properties{autocrop};
288 my $orig_ncolors = image_getvalue($image, 'colors');
289 return unless $orig_ncolors;
290 my ($w, $h) = image_getvalue($image, 'width', 'height');
291 return unless $w && $h;
292 my @transform = @$transform;
293 # If native unit is points, we at least need to scale by dots/point.
294 # [tho' other scalings may override this]
295 if (($properties{unit} || 'pixel') eq 'point') {
296 push(@transform, ['scale', $dppt, $dppt]); }
297  
298 # For prescaling, compute the desired size and re-read the image into that size,
299 # with an appropriate density set. This will give much better anti-aliasing.
300 # Actually, we'll set the density & size up a further factor of $F, and then downscale.
301 if ($properties{prescale}) {
302 my ($w0, $h0) = ($w, $h);
303 while (@transform && ($transform[0]->[0] =~ /^scale/)) {
304 my ($op, $a1, $a2, $a3, $a4) = @{ shift(@transform) };
305 if ($op eq 'scale') { # $a1 => scale
306 ($w, $h) = (ceil($w * $a1), ceil($h * ($a2 || $a1))); }
307 elsif ($op eq 'scale-to') {
308 # $a1 => width (pts), $a2 => height (pts), $a3 => preserve aspect ratio.
309 if ($a3) { # If keeping aspect ratio, ignore the most extreme request
310 if ($a1 / $w < $a2 / $h) { $a2 = $h * $a1 / $w; }
311 else { $a1 = $w * $a2 / $h; } }
312 ($w, $h) = (ceil($a1 * $dppt), ceil($a2 * $dppt)); } }
313 my $X = 4; # Expansion factor
314 my ($dx, $dy) = (int($X * 72 * $w / $w0), int($X * 72 * $h / $h0));
315 Debug("reloading $source to desired size $w x $h (density = $dx x $dy)")
316 if $LaTeXML::DEBUG{images};
317 $image = image_read($source, antialias => 1, density => $dx . 'x' . $dy) or return;
318 image_internalop($image, 'Trim') or return if $properties{autocrop};
319 # image_setvalue($image, colorspace => 'RGB') or return;
320 image_internalop($image, 'Scale', geometry => int(100 / $X) . "%") or return; # Now downscale.
321 ($w, $h) = image_getvalue($image, 'width', 'height');
322 return unless $w && $h; }
323  
324 my $notes = '';
325 foreach my $trans (@transform) {
326 my ($op, $a1, $a2, $a3, $a4) = @$trans;
327 return unless $w && $h;
328 if ($op eq 'scale') { # $a1 => scale
329 ($w, $h) = (ceil($w * $a1), ceil($h * ($a2 || $a1)));
330 return unless $w && $h;
331 $notes .= " scale to $w x $h";
332 image_internalop($image, 'Scale', width => $w, height => $h) or return; }
333 elsif ($op eq 'scale-to') {
334 # $a1 => width (pts), $a2 => height (pts), $a3 => preserve aspect ratio.
335 if ($a3) { # If keeping aspect ratio, ignore the most extreme request
336 if ($a1 / $w < $a2 / $h) { $a2 = $h * $a1 / $w; }
337 else { $a1 = $w * $a2 / $h; } }
338 ($w, $h) = (ceil($a1 * $dppt), ceil($a2 * $dppt));
339 $notes .= " scale-to $w x $h";
340 image_internalop($image, 'Scale', width => $w, height => $h) or return; }
341 elsif ($op eq 'rotate') {
342 image_internalop($image, 'Rotate', degrees => -$a1, background => $background) or return;
343 ($w, $h) = image_getvalue($image, 'width', 'height');
344 return unless $w && $h;
345 $notes .= " rotate $a1 to $w x $h"; }
346 elsif ($op eq 'reflect') {
347 image_internalop($image, 'Flop') or return;
348 $notes .= " reflected"; }
349 # In the following two, note that TeX's coordinates are relative to lower left corner,
350 # but ImageMagick's coordinates are relative to upper left.
351 elsif (($op eq 'trim') || ($op eq 'clip')) {
352 my ($x0, $y0, $ww, $hh);
353 # Use the image's dpi for trim & clip!
354 my $idppt = (defined $imagedpi ? ($imagedpi / 72.0) : $dppt);
355 if ($op eq 'trim') { # Amount to trim: a1=left, a2=bottom, a3=right, a4=top
356 ($x0, $y0, $ww, $hh) = (floor($a1 * $idppt), floor($a4 * $idppt),
357 ceil($w - ($a1 + $a3) * $idppt), ceil($h - ($a4 + $a2) * $idppt));
358 $notes .= " trim to $ww x $hh @ $x0,$y0"; }
359 else { # BBox: a1=left, a2=bottom, a3=right, a4=top
360 ($x0, $y0, $ww, $hh) = (floor($a1 * $idppt), floor($h - $a4 * $idppt),
361 ceil(($a3 - $a1) * $idppt), ceil(($a4 - $a2) * $idppt));
362 $notes .= " clip to $ww x $hh @ $x0,$y0"; }
363  
364 if (($x0 > 0) || ($y0 > 0) || ($x0 + $ww < $w) || ($y0 + $hh < $h)) {
365 my $x0p = max($x0, 0); $x0 = min($x0, 0);
366 my $y0p = max($y0, 0); $y0 = min($y0, 0);
367 image_internalop($image, 'Crop', x => $x0p, width => min($ww, $w - $x0p),
368 y => $y0p, height => min($hh, $h - $y0p)) or return;
369 $w = min($ww + $x0, $w - $x0p);
370 $h = min($hh + $y0, $h - $y0p);
371 $notes .= " crop $w x $h @ $x0p,$y0p"; }
372 # No direct `padding' operation in ImageMagick
373 # BUT, trim & clip really shouldn't need to pad???
374 # And besides, this composition seems to mangle the colormap (depending on background)
375 # my $nimage = image_read("xc:$background", size => "$ww x $hh") or return;
376 # image_internalop($nimage, 'Composite', image => $image, compose => 'over',
377 # x => -$x0, y => -$y0) or return;
378 # $notes .= " compose to $ww x $hh at $x0,$y0";
379 # $image = $nimage;
380 # ($w, $h) = ($ww, $hh);
381 } }
382  
383 if ($properties{transparent} && !$hasalpha) {
384 $notes .= " transparent=$background";
385 image_internalop($image, 'Transparent', $background) or return; }
386  
387 if (my $curr_ncolors = image_getvalue($image, 'colors')) {
388 if (my $req_ncolors = $properties{ncolors}) {
389 $req_ncolors = int($orig_ncolors * $1 / 100) if $req_ncolors =~ /^([\d]*)\%$/;
390 if ($req_ncolors < $curr_ncolors) {
391 $notes .= " quantize $orig_ncolors => $req_ncolors";
392 image_internalop($image, 'Quantize', colors => $req_ncolors) or return; } } }
393  
394 if (my $quality = $properties{quality}) {
395 $notes .= " quality=$quality";
396 image_setvalue($image, quality => $properties{quality}) or return; }
397  
398 Debug("Transformed $source : $notes") if $notes && $LaTeXML::DEBUG{images};
399 return ($image, $w, $h); }
400  
401 # Wrap up ImageMagick's methods to give more useful & consistent error handling.
402 # These all return non-zero on success!
403 # so, you generally want to do image_internalop(...) or return;
404  
405 # This reads a new image, setting the given properties BEFORE ingesting the image data.
406 sub image_read {
407 my ($source, @args) = @_;
408 if (!$source) {
409 Error('imageprocessing', 'read', undef, "No image source given"); return; }
410 return unless $source;
411 my $image = image_object();
412 image_internalop($image, 'Set', @args) or return;
413 image_internalop($image, 'Read', $source) or return;
414 return $image; }
415  
416 sub image_write {
417 my ($image, $destination) = @_;
418 if (!$image) {
419 Error('imageprocessing', 'write', undef, "No image object!"); return; }
420 if (!$destination) {
421 Error('imageprocessing', 'write', undef, "No image destination!"); return; }
422 # In the perverse case that we've ended up with a sequence of images; flatten them.
423 if (@$image > 1) {
424 my $fimage = $image->Flatten(); # Just in case we ended up with pieces!?!?!?
425 $image = $fimage if $fimage; }
426 return image_internalop($image, 'Write', filename => $destination); }
427  
428 sub image_getvalue {
429 my ($image, @args) = @_;
430 if (!$image) {
431 Error('imageprocessing', 'getvalue', undef, "No image object!"); return; }
432 my @values = $image->Get(@args);
433 return @values; }
434  
435 sub image_setvalue {
436 my ($image, @args) = @_;
437 if (!$image) {
438 Error('imageprocessing', 'setvalue', undef, "No image object!"); return; }
439 return image_internalop($image, 'Set', @args); }
440  
441 # Apparently ignorable warnings from Image::Magick
442 our %ignorable = map { $_ => 1; } (
443 350, # profile 'icc' not permitted on grayscale PNG
444 );
445  
446 sub image_internalop {
447 my ($image, $operation, @args) = @_;
448 if (!$image) {
449 Error('imageprocessing', 'internal', undef, "No image object!"); return; }
450 my $retval = $image->$operation(@args);
451 return 1 unless $retval;
452 my $retcode = 999;
453 if ($retval =~ /(\d+)/) {
454 $retcode = $1; }
455 if ($retcode < 400) { # Warning
456 Warn('imageprocessing', $operation, undef,
457 "Image processing operation $operation (" . join(', ', @args) . ") returned $retval")
458 unless $ignorable{$retcode};
459 return 1; }
460 else { # Error
461 Error('imageprocessing', $operation, undef,
462 "Image processing operation $operation (" . join(', ', @args) . ") returned $retval");
463 return 0; } }
464  
465 #======================================================================
466 1;