| 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')
can_run("identify") and can_run("convert")
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")
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 20 | 0 | my $cmd = join ' ', map shell_quote($_), qw(identify -format %Wx%H), $file; | |||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | ( `$cmd` =~ /^(\d+)x(\d+)$/ ) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$x or $y
MC/DC100%2/2
$x or $y
$x
$y
Truth table100%3/3
A or B
A= $xB= $y
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$x and not $y
MC/DC100%2/2
$x and not $y
$x
not $y
Truth table100%3/3
A and not(B)
A= $xB= not $y
| |||||||||||||||||||||||||||||||||||||
| 73 | 1 | push @args, '-resize', "${x}"; | |||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||
| 75 | ▶ | elsif ( !$x && $y ) { | |||||||||||||||||||||||||||||||||||
|
Condition66%2/3
not $x and $y
MC/DC50%1/2
not $x and $y
not $x
$y
Truth table66%2/3
not(A) and B
A= not $xB= $y
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 88 | 21 | ▶ | if ( my $degree = $req->{degree} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 89 | 2 | push @args, '-rotate', $degree; | |||||||||||||||||||||||||||||||||||
| 90 | 2 | ▶ | if ( $degree - 90 * int( $degree / 90 ) ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 91 | 1 | push @args, '-background', 'none'; | |||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||
| 95 | # apply quality | ||||||||||||||||||||||||||||||||||||
| 96 | 21 | ▶ | if ( $req->{quality} eq 'gray' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $in and $in ne ""
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 ""
| |||||||||||||||||||||||||||||||||||||
| 111 | 1 | ▶ | push @cmd, $out if defined $out and $out ne ''; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $out and $out ne ""
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 ""
| |||||||||||||||||||||||||||||||||||||
| 112 | 1 | ▶ | unshift @cmd, "magick" if can_run("magick"); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 128 | 3 | ▶ | if ( $arg !~ /\A[\w_+-]+\z/ ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||