blib/lib/IIIF/Request.pm

filter: Statementstmt 100% 110 / 110 Branchbran 93.2% 69 / 74 Conditioncond 85.9% 55 / 64 MC/DCmcdc 67.5% 25 / 37 Subroutinesub 100% 11 / 11 Podpod 83.3% 5 / 6 totaltotal 91.0% 275 / 302 CC 61 SCAR 32.8
CC
61
Cov
94%
new
31.1
canonical
30.5
as_string
2.0
CRAP 26.7
« blib/lib/IIIF/ImageAPI.pm ↑ Summary blib/lib/IIIF/Magick.pm »
1 package IIIF::Request;
2 4 ▶use 5.014001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 4 our $VERSION = "0.06";
5  
6 4 ▶use Plack::Util::Accessor qw(region size rotation quality format);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $XY = qr{[0-9]+}; # non-negative integer
11 4 our $WH = qr{[1-9][0-9]*}; # positive integer
12 4 our $NUM = qr{[0-9]*(\.[0-9]+)?}; # non-negative
13 4 our $REGION = qr{full|square|($XY,$XY,$WH,$WH)|pct:($NUM,$NUM,$NUM,$NUM)};
14 4 our $SIZE = qr{(\^)?(max|pct:($NUM)|($WH,)|(,$WH)|(!)?($WH,$WH))};
15 4 our $ROTATION = qr{([!])?($NUM)};
16 4 our $QUALITY = qr{color|gray|bitonal|default};
17 4 our $FORMAT = qr{[^.]+};
18  
19 4 ▶use overload '""' => \&as_string, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 sub new {
22 86 ▶ my $class = shift;
Subroutine new: called CC=31 SCAR=34.4
Pod documented
23 86 ▶ my $path = shift // "";
Condition100%2/2
shift() // ""
l!l
851
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
24  
25 my (
26 86 $rotation, $mirror, $degree, $quality, $format,
27 $region, $region_pct, $region_px, $size, $upscale,
28 $size_px, $size_pct, $ratio
29 );
30  
31 86 my @parts = split '/', $path;
32  
33 86 ▶ if ( @parts && $parts[0] =~ /^$REGION$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts and $parts[0] =~ /^$IIIF::Request::REGION$/u) 33 53 86
Condition100%3/3
@parts and $parts[0] =~ /^$IIIF::Request::REGION$/u
!ll&&!rl&&r
25133
MC/DC100%2/2
@parts and $parts[0] =~ /^$IIIF::Request::REGION$/u
@parts $parts[0] =~ /^$IIIF::Request::REGION$/u
Truth table100%3/3
A and B
A= @partsB= $parts[0] =~ /^$IIIF::Request::REGION$/u
ABresult
0X0
100
111
34 33 $region = shift @parts;
35 33 ▶ if ($1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($1) { } 12 21 33
elsif ($2) { } 8 13 21
36 12 $region_px = [ split ',', $1 ];
37 }
38 elsif ($2) {
39 8 $region_pct = [ map { 1 * $_ } split ',', $2 ];
40 error("disallowed percentage value")
41 if !$region_pct->[2]
42 || !$region_pct->[3]
43 8 ▶ || grep { $_ > 100 } @$region_pct;
Branch: 1 branch
BranchTrueFalseTotal
if not $region_pct->[2] && $region_pct->[3] or grep {$_ > 100;} @$region_pct 1 7 8
Condition50%3/6
not $region_pct->[2] && $region_pct->[3] or grep {$_ > 100;} @$region_pct
l!l&&r!l&&!r
017
$region_pct->[2] && $region_pct->[3]
!ll&&!rl&&r
008
MC/DC33%1/3
not $region_pct->[2] && $region_pct->[3] or grep {$_ > 100;} @$region_pct
$region_pct->[2] $region_pct->[3] grep {$_ > 100;} @$region_pct
Truth table50%2/4
not(A && B) or C
A= $region_pct->[2]B= $region_pct->[3]C= grep {$_ > 100;} @$region_pct
ABCresult
0XX1
10X1
1111
1100
44 }
45 }
46  
47 85 ▶ if ( @parts && $parts[0] =~ /^$SIZE$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts and $parts[0] =~ /^$IIIF::Request::SIZE$/u) 36 49 85
Condition100%3/3
@parts and $parts[0] =~ /^$IIIF::Request::SIZE$/u
!ll&&!rl&&r
321736
MC/DC100%2/2
@parts and $parts[0] =~ /^$IIIF::Request::SIZE$/u
@parts $parts[0] =~ /^$IIIF::Request::SIZE$/u
Truth table100%3/3
A and B
A= @partsB= $parts[0] =~ /^$IIIF::Request::SIZE$/u
ABresult
0X0
100
111
48 36 $size = shift @parts;
49 36 $upscale = $1;
50 36 $ratio = $7;
51 36 ▶ $size_px = [ split ',', $5 // $6 // $8 ] if $5 // $6 // $8;
Branch: 1 branch
BranchTrueFalseTotal
if ($5 // $6) // $8 23 13 36
Condition91%11/12
($5 // $6) // $8
l!l&&r!l&&!r
61712
$5 // $6
l!l&&r!l&&!r
4230
($5 // $6) // $8
l!l&&r!l&&!r
7160
$5 // $6
l!l&&r!l&&!r
5216
MC/DC100%3/3
($5 // $6) // $8
$5 $6 $8
MC/DC0%0/3
($5 // $6) // $8
$5 $6 $8
Truth table100%4/4
A // B // C
A= $5B= $6C= $8
ABCresult
1XX1
01X1
0011
0000
Truth table75%3/4
A // B // C
A= $5B= $6C= $8
ABCresult
1XX1
01X1
0011
0000
52  
53 36 ▶ if ( defined $3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $3) 11 25 36
54 11 $size_pct = 1 * $3;
55 11 ▶ if ($upscale) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upscale) { } 1 10 11
56 1 $size = "^pct:$size_pct";
57 }
58 else {
59 10 ▶ error("disallowed percentage value")
Branch: 1 branch
BranchTrueFalseTotal
if $size_pct == 0 or $size_pct > 100 2 8 10
Condition100%3/3
$size_pct == 0 or $size_pct > 100
l!l&&r!l&&!r
118
MC/DC100%2/2
$size_pct == 0 or $size_pct > 100
$size_pct == 0 $size_pct > 100
Truth table100%3/3
A or B
A= $size_pct == 0B= $size_pct > 100
ABresult
1X1
011
000
60 if $size_pct == 0.0 || $size_pct > 100.0;
61 8 $size = "pct:$size_pct";
62 }
63 }
64 }
65  
66 83 ▶ if ( @parts && $parts[0] =~ /^$ROTATION$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts and $parts[0] =~ /^$IIIF::Request::ROTATION$/u) 9 74 83
Condition100%3/3
@parts and $parts[0] =~ /^$IIIF::Request::ROTATION$/u
!ll&&!rl&&r
6599
MC/DC100%2/2
@parts and $parts[0] =~ /^$IIIF::Request::ROTATION$/u
@parts $parts[0] =~ /^$IIIF::Request::ROTATION$/u
Truth table100%3/3
A and B
A= @partsB= $parts[0] =~ /^$IIIF::Request::ROTATION$/u
ABresult
0X0
100
111
67 9 shift @parts;
68 9 $mirror = !!$1;
69  
70 # normalize to 0...<360 with up to 6 decimal points
71 9 $degree = 1 * sprintf( "%.6f", $2 - int( $2 / 360 ) * 360 );
72 9 ▶ $rotation = $mirror ? "!$degree" : "$degree";
Branch: 1 branch
BranchTrueFalseTotal
$mirror ? : 6 3 9
73 }
74  
75 83 ▶ if ( @parts && $parts[0] =~ /^(($QUALITY)([.]($FORMAT))?|[.]($FORMAT))$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts and $parts[0] =~ /^(($IIIF::Request::QUALITY)([.]($IIIF::Request::FORMAT))?|[.]($IIIF::Request::FORMAT))$/u) 7 76 83
Condition100%3/3
@parts and $parts[0] =~ /^(($IIIF::Request::QUALITY)([.]($IIIF::Request::FORMAT))?|[.]($IIIF::Request::FORMAT))$/u
!ll&&!rl&&r
7337
MC/DC100%2/2
@parts and $parts[0] =~ /^(($IIIF::Request::QUALITY)([.]($IIIF::Request::FORMAT))?|[.]($IIIF::Request::FORMAT))$/u
@parts $parts[0] =~ /^(($IIIF::Request::QUALITY)([.]($IIIF::Request::FORMAT))?|[.]($IIIF::Request::FORMAT))$/u
Truth table100%3/3
A and B
A= @partsB= $parts[0] =~ /^(($IIIF::Request::QUALITY)([.]($IIIF::Request::FORMAT))?|[.]($IIIF::Request::FORMAT))$/u
ABresult
0X0
100
111
76 7 $quality = $2;
77 7 ▶ $format = $4 // $5;
Condition66%2/3
$4 // $5
l!l&&r!l&&!r
016
MC/DC50%1/2
$4 // $5
$4 $5
Truth table66%2/3
A // B
A= $4B= $5
ABresult
1X1
011
000
78 7 shift @parts;
79 }
80  
81 83 ▶ error( "failed to parse '" . join( '/', '', @parts ) . "'" )
Branch: 1 branch
BranchTrueFalseTotal
if @parts 3 80 83
82 if @parts;
83  
84 80 ▶ bless {
Condition100%8/8
$region // "full"
l!l
3248
$size // "max"
l!l
3446
$rotation // 0
l!l
971
$quality // "default"
l!l
773
MC/DC100%1/1
$region // "full"
$region
MC/DC100%1/1
$size // "max"
$size
MC/DC100%1/1
$rotation // 0
$rotation
MC/DC100%1/1
$quality // "default"
$quality
Truth table100%2/2
A
A= $region
Aresult
11
00
Truth table100%2/2
A
A= $size
Aresult
11
00
Truth table100%2/2
A
A= $rotation
Aresult
11
00
Truth table100%2/2
A
A= $quality
Aresult
11
00
85 region => $region // 'full',
86 region_pct => $region_pct,
87 region_px => $region_px,
88 size => $size // 'max',
89 upscale => $upscale,
90 size_pct => $size_pct,
91 size_px => $size_px,
92 ratio => $ratio,
93 rotation => $rotation // '0',
94 mirror => $mirror,
95 degree => $degree,
96 quality => $quality // 'default',
97 format => $format
98 }, $class;
99 }
100  
101 sub error {
102 6 ▶ croak "Invalid IIIF Image API Request: $_[0]";
Subroutine error: called CC=1 SCAR=0.0
Pod documented
103 }
104  
105 sub canonical {
106 23 ▶ my ( $self, $width, $height ) = @_;
Subroutine canonical: called CC=30 SCAR=34.2
Pod documented
107  
108 # convert region to /full|x,y,w,h/
109 23 my $region = $self->{region};
110 23 ▶ if ( $self->{region} eq 'square' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'region'} eq 'square') { } 2 21 23
elsif ($self->{'region_pct'}) { } 3 18 21
elsif ($self->{'region_px'}) { } 5 13 18
111 2 my $size = min( $width, $height );
112 2 $region = "0,0,$size,$size";
113 }
114 elsif ( $self->{region_pct} ) {
115 3 my ( $x, $y, $w, $h ) = @{ $self->{region_pct} };
116 3 $x = pct2px( $x, $width );
117 3 $y = pct2px( $y, $height );
118 3 ▶ $w = pct2px( $w, $width ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$w = pct2px($w, $width) or return 1 2 3
119 2 ▶ $h = pct2px( $h, $height ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$h = pct2px($h, $height) or return 0 2 2
120 2 $region = "$x,$y,$w,$h";
121 }
122 elsif ( $self->{region_px} ) { # region outside of image dimensions?
123 5 my ( $x, $y, $w, $h ) = @{ $self->{region_px} };
124 5 ▶ return if $x >= $width && $y >= $height;
Branch: 1 branch
BranchTrueFalseTotal
if $x >= $width and $y >= $height 1 4 5
Condition66%2/3
$x >= $width and $y >= $height
!ll&&!rl&&r
401
MC/DC50%1/2
$x >= $width and $y >= $height
$x >= $width $y >= $height
Truth table66%2/3
A and B
A= $x >= $widthB= $y >= $height
ABresult
0X0
100
111
125 }
126 21 ▶ $region = 'full' if $region eq "0,0,$width,$height";
Branch: 1 branch
BranchTrueFalseTotal
if $region eq "0,0,$width,$height" 3 18 21
127  
128 # proceed with region size
129 21 ▶ if ( $region ne 'full' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($region ne "full") 5 16 21
130 5 ( undef, undef, $width, $height ) = split ',', $region;
131 }
132  
133 21 ▶ if ( $self->{size_pct} ) { # too small
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"size_pct"}) 5 16 21
134 return
135 if !pct2px( $self->{size_pct}, $width )
136 5 ▶ || !pct2px( $self->{size_pct}, $height );
Branch: 1 branch
BranchTrueFalseTotal
unless pct2px($self->{"size_pct"}, $width) and pct2px($self->{"size_pct"}, $height) 3 2 5
Condition100%3/3
pct2px($self->{"size_pct"}, $width) and pct2px($self->{"size_pct"}, $height)
!ll&&!rl&&r
212
MC/DC100%2/2
pct2px($self->{"size_pct"}, $width) and pct2px($self->{"size_pct"}, $height)
pct2px($self->{"size_pct"}, $width) pct2px($self->{"size_pct"}, $height)
Truth table100%3/3
A and B
A= pct2px($self->{"size_pct"}, $width)B= pct2px($self->{"size_pct"}, $height)
ABresult
0X0
100
111
137 }
138  
139 # convert size to /[^]?(max|w,h)/
140 18 my $size = $self->{size};
141 18 my $upscale = $self->{upscale};
142 18 ▶ if ( $size !~ /\^?max/ ) { # TODO: respect maxWidth, maxHeight, maxArea
Branch: 1 branch
BranchTrueFalseTotal
unless ($size =~ /\^?max/u) 10 8 18
143 10 ▶ if ( $self->{size_pct} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'size_pct'}) { } 2 8 10
144 $size = join ',',
145 2 map { pct2px( $self->{size_pct}, $_ ) } ( $width, $height );
146 2 ▶ $size = "^$size" if $upscale;
Branch: 1 branch
BranchTrueFalseTotal
if $upscale 0 2 2
147 }
148 else {
149 8 my ( $w, $h ) = @{ $self->{size_px} };
150 8 ▶ return if !$w && !$h;
Branch: 1 branch
BranchTrueFalseTotal
unless $w or $h 0 8 8
Condition66%2/3
$w or $h
l!l&&r!l&&!r
710
MC/DC0%0/2
$w or $h
$w $h
Truth table66%2/3
A or B
A= $wB= $h
ABresult
1X1
011
000
151 8 ▶ return if !$upscale && ( $h > $height || $w > $width );
Branch: 1 branch
BranchTrueFalseTotal
if not $upscale and $h > $height || $w > $width 1 7 8
Condition66%4/6
not $upscale and $h > $height || $w > $width
!ll&&!rl&&r
260
$h > $height or $w > $width
l!l&&r!l&&!r
105
MC/DC0%0/3
not $upscale and $h > $height || $w > $width
not $upscale $h > $height $w > $width
Truth table50%2/4
not(A) and B or C
A= not $upscaleB= $h > $heightC= $w > $width
ABCresult
0XX0
1000
11X1
1011
152  
153 7 ▶ if ( $w && $h ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($w and $h) { } 4 3 7
elsif ($w) { } 2 1 3
elsif ($h) { } 1 0 1
Condition100%3/3
$w and $h
!ll&&!rl&&r
124
MC/DC100%2/2
$w and $h
$w $h
Truth table100%3/3
A and B
A= $wB= $h
ABresult
0X0
100
111
154 4 ▶ if ( $self->{ratio} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ratio"}) 3 1 4
155 3 ▶ if ( $w / $h > $width / $height ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w / $h > $width / $height) { } 1 2 3
156 1 $w = pct2px( 100 * $width / $height, $h );
157 }
158 else {
159 2 $h = pct2px( 100 * $height / $width, $w );
160 }
161 }
162  
163 4 $size = "$w,$h";
164 }
165 elsif ($w) {
166 2 $size = "$w," . pct2px( 100 * $height / $width, $w );
167 }
168 elsif ($h) {
169 1 $size = pct2px( 100 * $width / $height, $h ) . ",$h";
170 }
171  
172 7 ▶ $size = "^$size" if $upscale;
Branch: 1 branch
BranchTrueFalseTotal
if $upscale 2 5 7
173 }
174  
175 9 ▶ $size = "max" if $size =~ /^\^?$width,$height$/;
Branch: 1 branch
BranchTrueFalseTotal
if $size =~ /^\^?$width,$height$/u 2 7 9
176 }
177  
178 17 my $str = join '/', $region, $size, $self->{rotation}, $self->{quality};
179 17 ▶ return defined $self->{format} ? "$str.$self->{format}" : $str;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'format'} ? : 0 17 17
180 }
181  
182 sub pct2px {
183 29 ▶ my ( $percent, $value ) = @_;
Subroutine pct2px: called CC=1 SCAR=0.0
Pod undocumented
184 29 return int( $percent * $value / 100 + 0.5 );
185 }
186  
187 sub is_default {
188 1 ▶ my ($self) = @_;
Subroutine is_default: called CC=1 SCAR=0.0
Pod documented
189  
190 1 return $self->as_string =~ qr{^full/max/0/default};
191 }
192  
193 sub as_string {
194 90 ▶ my ($self) = @_;
Subroutine as_string: called CC=2 SCAR=6.9
Pod documented
195  
196 90 my $str = join '/', map { $self->{$_} } qw(region size rotation quality);
197 90 ▶ return defined $self->{format} ? "$str.$self->{format}" : $str;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'format'} ? : 1 89 90
198 }
199  
200 4 1;
201 __END__
« blib/lib/IIIF/ImageAPI.pm ↑ Summary blib/lib/IIIF/Magick.pm »