| 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.000'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$opts{"page"} // 0
$opts{"page"}
Truth table100%2/2
A
A= $opts{"page"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 88 | ▶ | unless ($page =~ /\A[0-9]+\z/ && $page <= MAX_PAGE) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$page =~ /\A[0-9]+\z/u and $page <= 2147483647
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 85 | ▶ | unless ($im->{IMG}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 7 | ▶ | unless (@images) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
@$spec == 3 or @$spec == 4
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition44%4/9
defined $value and not ref $value and $value =~ /\A[0-9]+\z/u and $value <= 255
defined $value and not ref $value and $value =~ /\A[0-9]+\z/u
defined $value and not ref $value
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 11 | ▶ | return $spec if blessed($spec) && $spec->isa('Imager::Color'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($spec) and $spec->isa("Imager::Color")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 10 | ▶ | if (ref $spec eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | ▶ | return unless _isRgbArray($spec); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | return Imager::Color->new($spec->@*); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 8 | ▶ | return if ref $spec; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 10 | ▶ | unless (ref $colors eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
@$colors >= 1 and @$colors <= 256
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 62 | ▶ | $im->_set_opts(\%opts, 'sixel_', $im) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 61 | ▶ | unless (i_writesixel($io, $palette, $im->{IMG})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 2 | ▶ | $class->_set_opts($opts, 'sixel_', @images) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | ▶ | unless (i_writesixel_multi($io, $palette, map { $_->{IMG} } @images)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||