blib/lib/IIIF/Magick.pm

filter: Statementstmt 85.2% 58 / 68 Branchbran 77.2% 34 / 44 Conditioncond 57.1% 12 / 21 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 80.0% 8 / 10 Podpod 83.3% 5 / 6 totaltotal 75.3% 122 / 162 CC 30 SCAR 24.4
CC
30
Cov
78%
convert_args
18.0
info
12.0
convert_command
8.1
CRAP 11.5
« blib/lib/IIIF/Request.pm ↑ Summary blib/lib/IIIF.pm »
1 package IIIF::Magick;
2 3 ▶use 5.014001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 our $VERSION = "0.06";
5  
6 3 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 our @EXPORT = qw(info available convert convert_command convert_args);
8  
9 3 ▶use IPC::Cmd qw(can_run);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub available {
13 2 ▶ return can_run("magick") || ( can_run("identify") && can_run("convert") );
Subroutine available: called CC=3 SCAR=15.4
Pod documented
Condition33%2/6
can_run('magick') || can_run('identify') && can_run('convert')
l!l&&r!l&&!r
002
can_run("identify") and can_run("convert")
!ll&&!rl&&r
200
MC/DC0%0/3
can_run('magick') || can_run('identify') && can_run('convert')
can_run('magick') can_run("identify") can_run("convert")
Truth table25%1/4
A || B and C
A= can_run('magick')B= can_run("identify")C= can_run("convert")
ABCresult
1XX1
0111
00X0
0100
14 }
15  
16 sub info {
17 0 ▶ my $file = shift;
Subroutine info: not called CC=3 SCAR=24.8
Pod documented
18  
19 0 ▶ -f $file or die "$file: No such file\n";
Branch: 1 branch
BranchTrueFalseTotal
-f $file or die "${file}: No such file\n" 0 0 0
20 0 my $cmd = join ' ', map shell_quote($_), qw(identify -format %Wx%H), $file;
21  
22 0 ▶ ( `$cmd` =~ /^(\d+)x(\d+)$/ )
Branch: 1 branch
BranchTrueFalseTotal
`$cmd` =~ /^(\d+)x(\d+)$/u or die "${file}: Failed to get image dimensions" 0 0 0
23 or die "$file: Failed to get image dimensions";
24  
25 return {
26 0 '@context' => 'http://iiif.io/api/image/3/context.json',
27 type => 'ImageService3',
28 protocol => 'http://iiif.io/api/image',
29 width => 1 * $1,
30 height => 1 * $2,
31 @_
32 };
33 }
34  
35 sub convert_args {
36 21 ▶ my $req = shift;
Subroutine convert_args: called CC=18 SCAR=28.9
Pod documented
37 21 my @args;
38  
39 # apply region
40 21 ▶ if ( $req->{region} eq 'square' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($req->{'region'} eq 'square') { } 1 20 21
elsif (my $region_px = $req->{'region_px'}) { } 1 19 20
elsif (my $region_pct = $req->{'region_pct'}) { } 3 16 19
41  
42 # could be simpler in ImageMagick 7:
43 # push @args, qw(-gravity center -crop), "%[fx:w>h?h:w]x%[fx:w>h?h:w]+0+0";
44 1 push @args, qw(-set option:distort:viewport
45 %[fx:w>h?h:w]x%[fx:w>h?h:w]+%[fx:w>h?(w-h)/2:0]+%[fx:w>h?0:(h-w)/2]
46 -filter point -distort SRT 0 +repage);
47 }
48 elsif ( my $region_px = $req->{region_px} ) {
49 1 my ( $x, $y, $w, $h ) = @$region_px;
50 1 @args = ( '-crop', "${w}x$h+$x+$y" );
51 }
52 elsif ( my $region_pct = $req->{region_pct} ) {
53 3 my ( $x, $y, $w, $h ) = @$region_pct;
54  
55 3 ▶ if ( $x || $y ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x or $y) 2 1 3
Condition100%3/3
$x or $y
l!l&&r!l&&!r
111
MC/DC100%2/2
$x or $y
$x $y
Truth table100%3/3
A or B
A= $xB= $y
ABresult
1X1
011
000
56 2 my $px = $x / 100;
57 2 my $py = $y / 100;
58 2 push @args, '-set', 'page', "-%[fx:w*$px]-%[fx:h*$py]";
59 }
60  
61 # could also be simpler in ImageMagick 7
62 3 push @args, '-crop', "${w}x$h%+0+0";
63 }
64  
65 # apply size
66 21 ▶ if ( $req->{size_pct} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($req->{'size_pct'}) { } 2 19 21
elsif ($req->{'size_px'}) { } 6 13 19
67 2 push @args, '-resize', $req->{size_pct} . '%';
68 }
69 elsif ( $req->{size_px} ) {
70 6 my ( $x, $y ) = @{ $req->{size_px} };
71  
72 6 ▶ if ( $x && !$y ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($x and not $y) { } 1 5 6
elsif (not $x and $y) { } 1 4 5
elsif ($req->{'ratio'}) { } 2 2 4
Condition100%3/3
$x and not $y
!ll&&!rl&&r
141
MC/DC100%2/2
$x and not $y
$x not $y
Truth table100%3/3
A and not(B)
A= $xB= not $y
ABresult
0X0
100
111
73 1 push @args, '-resize', "${x}";
74 }
75 ▶ elsif ( !$x && $y ) {
Condition66%2/3
not $x and $y
!ll&&!rl&&r
401
MC/DC50%1/2
not $x and $y
not $x $y
Truth table66%2/3
not(A) and B
A= not $xB= $y
ABresult
0X0
100
111
76 1 push @args, '-resize', "x${y}";
77 }
78 elsif ( $req->{ratio} ) {
79 2 push @args, '-resize', "${x}x$y";
80 }
81 else {
82 2 push @args, '-resize', "${x}x$y!";
83 }
84 }
85  
86 # apply rotation
87 21 ▶ push @args, '-flop' if $req->{mirror};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"mirror"} 2 19 21
88 21 ▶ if ( my $degree = $req->{degree} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $degree = $req->{"degree"}) 2 19 21
89 2 push @args, '-rotate', $degree;
90 2 ▶ if ( $degree - 90 * int( $degree / 90 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($degree - 90 * int($degree / 90)) 1 1 2
91 1 push @args, '-background', 'none';
92 }
93 }
94  
95 # apply quality
96 21 ▶ if ( $req->{quality} eq 'gray' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($req->{'quality'} eq 'gray') { } 1 20 21
elsif ($req->{'quality'} eq 'bitonal') { } 1 19 20
97 1 push @args, qw(-colorspace Gray);
98 }
99 elsif ( $req->{quality} eq 'bitonal' ) {
100 1 push @args, qw(-monochrome -colors 2);
101 }
102  
103 21 return @args;
104 }
105  
106 sub convert_command {
107 1 ▶ my ( $req, $in, $out ) = splice @_, 0, 3;
Subroutine convert_command: called CC=6 SCAR=20.9
Pod documented
108  
109 1 my @cmd = ( 'convert', @_, convert_args($req) );
110 1 ▶ push @cmd, $in if defined $in and $in ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $in and $in ne "" 0 1 1
Condition33%1/3
defined $in and $in ne ""
!ll&&!rl&&r
100
MC/DC0%0/2
defined $in and $in ne ""
defined $in $in ne ""
Truth table33%1/3
A and B
A= defined $inB= $in ne ""
ABresult
0X0
100
111
111 1 ▶ push @cmd, $out if defined $out and $out ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $out and $out ne "" 0 1 1
Condition33%1/3
defined $out and $out ne ""
!ll&&!rl&&r
100
MC/DC0%0/2
defined $out and $out ne ""
defined $out $out ne ""
Truth table33%1/3
A and B
A= defined $outB= $out ne ""
ABresult
0X0
100
111
112 1 ▶ unshift @cmd, "magick" if can_run("magick");
Branch: 1 branch
BranchTrueFalseTotal
if can_run("magick") 0 1 1
113  
114 1 return join ' ', map shell_quote($_), @cmd;
115 }
116  
117 sub convert {
118 0 ▶ my $command = convert_command(@_);
Subroutine convert: not called CC=1 SCAR=6.9
Pod documented
119 0 qx{$command};
120 0 return !$?;
121 }
122  
123 # adopted from <https://metacpan.org/release/ShellQuote-Any-Tiny>
124 sub shell_quote {
125 3 ▶ my $arg = shift;
Subroutine shell_quote: called CC=4 SCAR=15.2
Pod undocumented
126  
127 3 ▶ if ( $^O eq 'MSWin32' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 3 0 3
elsif (not $arg =~ qr"\A[\w,_+/.-]+\z"u) { } 0 0 0
128 3 ▶ if ( $arg !~ /\A[\w_+-]+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg =~ /\A[\w_+-]+\z/u) 1 2 3
129 1 $arg =~ s/\\(?=\\*(?:"|$))/\\\\/g;
130 1 $arg =~ s/"/\\"/g;
131 1 return qq("$arg");
132 }
133 }
134 elsif ( $arg !~ qr{\A[\w,_+/.-]+\z} ) {
135 0 $arg =~ s/'/'"'"'/g;
136 0 return "'$arg'";
137 }
138  
139 2 return $arg;
140 }
141  
142 3 1;
143 __END__
« blib/lib/IIIF/Request.pm ↑ Summary blib/lib/IIIF.pm »