blib/lib/Imager/File/SIXEL.pm

filter: Statementstmt 100% 92 / 92 Branchbran 91.3% 42 / 46 Conditioncond 65.2% 15 / 23 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 100% 16 / 16 Podpod n/a totaltotal 90.5% 172 / 190 CC 34 SCAR 17.8
CC
34
Cov
93%
_isRgbArray
12.1
_paletteColor
7.0
_paletteOption
7.0
CRAP 5.9
↑ Summary SIXEL.xs »
1 package Imager::File::SIXEL;
2  
3 6 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use feature qw(signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶no warnings qw(experimental::signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Imager 1.013;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 our $VERSION = '1.001';
12  
13 6 XSLoader::load(__PACKAGE__, $VERSION);
14  
15 6 ▶use constant MAX_PALETTE_SIZE => 256;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use constant MAX_PAGE => 2147483647;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 88 ▶sub _readSingle($im, $io, %opts) {
Subroutine _readSingle: called CC=6 SCAR=17.9
19 88 ▶ my $page = $opts{page} // 0;
Condition100%2/2
$opts{"page"} // 0
l!l
583
MC/DC100%1/1
$opts{"page"} // 0
$opts{"page"}
Truth table100%2/2
A
A= $opts{"page"}
Aresult
11
00
20  
21 88 ▶ unless ($page =~ /\A[0-9]+\z/ && $page <= MAX_PAGE) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($page =~ /\A[0-9]+\z/u and $page <= 2147483647) 3 85 88
Condition100%3/3
$page =~ /\A[0-9]+\z/u and $page <= 2147483647
!ll&&!rl&&r
2185
MC/DC100%2/2
$page =~ /\A[0-9]+\z/u and $page <= 2147483647
$page =~ /\A[0-9]+\z/u $page <= 2147483647
Truth table100%3/3
A and B
A= $page =~ /\A[0-9]+\z/uB= $page <= 2147483647
ABresult
0X0
100
111
22 3 $im->_set_error('page must be a non-negative integer');
23 3 return;
24 }
25 85 ▶ $im->{IMG} = i_readsixel($io, $page, $opts{allow_incomplete} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$opts{'allow_incomplete'} ? : 1 84 85
26 85 ▶ unless ($im->{IMG}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($im->{"IMG"}) 11 74 85
27 11 $im->_set_error(Imager->_error_as_msg);
28 11 return;
29 }
30 74 return $im;
31 }
32  
33 7 ▶sub _readMultiple($io, %opts) {
Subroutine _readMultiple: called CC=3 SCAR=11.0
34 7 ▶ my @images = i_readsixel_multi($io, $opts{allow_incomplete} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$opts{'allow_incomplete'} ? : 1 6 7
35 7 ▶ unless (@images) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@images) 2 5 7
36 2 Imager->_set_error(Imager->_error_as_msg);
37 2 return;
38 }
39 5 return map { bless { IMG => $_, ERRSTR => undef }, 'Imager' } @images;
40 }
41  
42 # True for a reference to an array of red, green and blue values, with
43 # an optional alpha value, each an integer from 0 to 255.
44 2 ▶sub _isRgbArray($spec) {
Subroutine _isRgbArray: called CC=8 SCAR=24.9
45 2 ▶ return 0 unless $spec->@* == 3 || $spec->@* == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @$spec == 3 or @$spec == 4 0 2 2
Condition33%1/3
@$spec == 3 or @$spec == 4
l!l&&r!l&&!r
200
MC/DC0%0/2
@$spec == 3 or @$spec == 4
@$spec == 3 @$spec == 4
Truth table33%1/3
A or B
A= @$spec == 3B= @$spec == 4
ABresult
1X1
011
000
46 2 foreach my $value ($spec->@*) {
47 4 ▶ return 0 unless defined $value && !ref $value && $value =~ /\A[0-9]+\z/ && $value <= 255;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and $value =~ /\A[0-9]+\z/u and $value <= 255 1 3 4
Condition44%4/9
defined $value and not ref $value and $value =~ /\A[0-9]+\z/u and $value <= 255
!ll&&!rl&&r
013
defined $value and not ref $value and $value =~ /\A[0-9]+\z/u
!ll&&!rl&&r
004
defined $value and not ref $value
!ll&&!rl&&r
004
MC/DC25%1/4
defined $value and not ref $value and $value =~ /\A[0-9]+\z/u and $value <= 255
defined $value not ref $value $value =~ /\A[0-9]+\z/u $value <= 255
Truth table40%2/5
A and not(B) and C and D
A= defined $valueB= not ref $valueC= $value =~ /\A[0-9]+\z/uD= $value <= 255
ABCDresult
0XXX0
10XX0
110X0
11100
11111
48 }
49 1 return 1;
50 }
51  
52 # Converts one entry of the colors option into an Imager::Color, or
53 # returns undef.
54 12 ▶sub _paletteColor($spec) {
Subroutine _paletteColor: called CC=7 SCAR=19.5
55 12 ▶ return unless defined $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec 1 11 12
56 11 ▶ return $spec if blessed($spec) && $spec->isa('Imager::Color');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($spec) and $spec->isa("Imager::Color") 1 10 11
Condition66%2/3
builtin::blessed($spec) and $spec->isa("Imager::Color")
!ll&&!rl&&r
1001
MC/DC50%1/2
builtin::blessed($spec) and $spec->isa("Imager::Color")
builtin::blessed($spec) $spec->isa("Imager::Color")
Truth table66%2/3
A and B
A= builtin::blessed($spec)B= $spec->isa("Imager::Color")
ABresult
0X0
100
111
57 10 ▶ if (ref $spec eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec eq "ARRAY") 2 8 10
58 2 ▶ return unless _isRgbArray($spec);
Branch: 1 branch
BranchTrueFalseTotal
unless _isRgbArray($spec) 1 1 2
59 1 return Imager::Color->new($spec->@*);
60 }
61 8 ▶ return if ref $spec;
Branch: 1 branch
BranchTrueFalseTotal
if ref $spec 0 8 8
62 8 return Imager::Color->new($spec);
63 }
64  
65 # Converts the colors option into an array reference of Imager::Color
66 # objects. Returns an empty list after recording an error on $target.
67 70 ▶sub _paletteOption($target, $colors) {
Subroutine _paletteOption: called CC=7 SCAR=19.5
68 70 ▶ return (1, undef) unless defined $colors;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $colors 60 10 70
69  
70 10 ▶ unless (ref $colors eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $colors eq "ARRAY") 1 9 10
71 1 $target->_set_error('colors must be an array reference');
72 1 return;
73 }
74 9 ▶ unless ($colors->@* >= 1 && $colors->@* <= MAX_PALETTE_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$colors >= 1 and @$colors <= 256) 2 7 9
Condition100%3/3
@$colors >= 1 and @$colors <= 256
!ll&&!rl&&r
117
MC/DC100%2/2
@$colors >= 1 and @$colors <= 256
@$colors >= 1 @$colors <= 256
Truth table100%3/3
A and B
A= @$colors >= 1B= @$colors <= 256
ABresult
0X0
100
111
75 2 $target->_set_error('colors must hold from 1 to ' . MAX_PALETTE_SIZE . ' colors');
76 2 return;
77 }
78  
79 7 my @palette;
80 7 foreach my $index (0 .. $colors->$#*) {
81 12 my $color = _paletteColor($colors->[$index]);
82 12 ▶ unless ($color) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($color) 3 9 12
83 3 $target->_set_error("colors entry $index is not a valid color");
84 3 return;
85 }
86 9 push @palette, $color;
87 }
88 4 return (1, \@palette);
89 }
90  
91 68 ▶sub _writeSingle($im, $io, %opts) {
Subroutine _writeSingle: called CC=4 SCAR=13.9
92 68 my ($ok, $palette) = _paletteOption($im, $opts{colors});
93 68 ▶ return unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 6 62 68
94  
95 62 ▶ $im->_set_opts(\%opts, 'sixel_', $im)
Branch: 1 branch
BranchTrueFalseTotal
$im->_set_opts(\%opts, "sixel_", $im) or return 61 1 62
96 or return;
97  
98 61 ▶ unless (i_writesixel($io, $palette, $im->{IMG})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (i_writesixel($io, $palette, $im->{"IMG"})) 11 50 61
99 11 $im->_set_error(Imager->_error_as_msg);
100 11 return;
101 }
102 50 return $im;
103 }
104  
105 3 ▶sub _writeMultiple($class, $io, $opts, @images) {
Subroutine _writeMultiple: called CC=5 SCAR=16.2
106 3 ▶ unless (@images) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@images) 1 2 3
107 1 $class->_set_error('no images to write');
108 1 return;
109 }
110 2 my ($ok, $palette) = _paletteOption($class, $opts->{colors});
111 2 ▶ return unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 2 2
112  
113 2 ▶ $class->_set_opts($opts, 'sixel_', @images)
Branch: 1 branch
BranchTrueFalseTotal
$class->_set_opts($opts, "sixel_", @images) or return 2 0 2
114 or return;
115  
116 2 ▶ unless (i_writesixel_multi($io, $palette, map { $_->{IMG} } @images)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (i_writesixel_multi($io, $palette, map({$_->{"IMG"};} @images))) 1 1 2
117 1 $class->_set_error($class->_error_as_msg);
118 1 return;
119 }
120 1 return 1;
121 }
122  
123 6 Imager->register_reader(
124 type => 'sixel',
125 single => \&_readSingle,
126 multiple => \&_readMultiple,
127 );
128  
129 6 Imager->register_writer(
130 type => 'sixel',
131 single => \&_writeSingle,
132 multiple => \&_writeMultiple,
133 );
134  
135 6 Imager->add_type_extensions('sixel', 'six', 'sixel');
136 6 Imager->add_file_magic(name => 'sixel', bits => "\x1BP", mask => 'xx');
137  
138 6 1;
139  
140 __END__
↑ Summary SIXEL.xs »