lib/Image/Info/AVIF.pm

filter: Statementstmt 80.2% 118 / 147 Branchbran 45.9% 45 / 98 Conditioncond 33.3% 4 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 81.8% 9 / 11 Podpod 0.0% 0 / 5 totaltotal 62.9% 177 / 281 CC 54 SCAR 45.2
CC
54
Cov
65%
process_file
147.4
read_heif
11.5
read_block
4.2
CRAP 92.2
« blib/lib/Image/Info/BMP.pm ↑ Summary blib/lib/Image/Info/TIFF.pm »
1 package Image::Info::AVIF;
2  
3 2 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = "0.01";
8  
9 0 ▶sub die_for_info($) { die bless({ err=>$_[0] }, __PACKAGE__."::__ERROR__") }
Subroutine die_for_info: not called CC=1 SCAR=6.9
Pod undocumented
10  
11 BEGIN {
12 2 ▶ if("$]" >= 5.008) {
Subroutine BEGIN: called CC=2 SCAR=8.5
Branch: 1 branch
BranchTrueFalseTotal
if ("$]" >= 5.008) { } 2 0 2
13 2 ▶ *io_string = sub ($) { open(my $fh, "<", \$_[0]); $fh };
Subroutine __ANON__: called CC=1 SCAR=0.0
14 } else {
15 0 require IO::String;
16 0 ▶ *io_string = sub ($) { IO::String->new($_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
17 }
18 }
19  
20 sub read_block($$) {
21 105 ▶ my($fh, $len) = @_;
Subroutine read_block: called CC=4 SCAR=14.5
Pod undocumented
22 105 my $d = "";
23 105 while(1) {
24 210 my $dlen = length($d);
25 210 ▶ last if $dlen == $len;
Branch: 1 branch
BranchTrueFalseTotal
if $dlen == $len 105 105 210
26 105 my $n = read($fh, $d, $len - $dlen, $dlen);
27 105 ▶ if(!defined($n)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $n) { } 0 105 105
elsif ($n == 0) { } 0 105 105
28 0 die_for_info "read error: $!";
29 } elsif($n == 0) {
30 0 die_for_info "truncated file";
31 }
32 }
33 105 return $d;
34 }
35  
36 sub read_nulterm($) {
37 3 ▶ my($fh) = @_;
Subroutine read_nulterm: called CC=3 SCAR=11.4
Pod undocumented
38 3 my $d = do { local $/ = "\x00"; <$fh> };
39 3 ▶ defined($d) && $d =~ /\x00\z/ or die_for_info "truncated file";
Branch: 1 branch
BranchTrueFalseTotal
defined $d and $d =~ /\x00\z/ or &die_for_info("truncated file") 0 3 3
Condition33%1/3
defined $d and $d =~ /\x00\z/
!ll&&!rl&&r
003
MC/DC0%0/2
defined $d and $d =~ /\x00\z/
defined $d $d =~ /\x00\z/
Truth table33%1/3
A and B
A= defined $dB= $d =~ /\x00\z/
ABresult
0X0
100
111
40 3 chop $d;
41 3 return $d;
42 }
43  
44 sub read_heif($$) {
45 12 ▶ my($fh, $box_types_to_keep) = @_;
Subroutine read_heif: called CC=10 SCAR=24.4
Pod undocumented
46 12 my %boxes;
47 12 ▶ while(!eof($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 42 12 54
48 42 my($len, $type) = unpack("Na4", read_block($fh, 8));
49 42 my $pos = 8;
50 42 my $bufp;
51 42 ▶ if($type =~ $box_types_to_keep && !exists($boxes{$type})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /$box_types_to_keep/ and not exists $boxes{$type}) 24 18 42
Condition66%2/3
$type =~ /$box_types_to_keep/ and not exists $boxes{$type}
!ll&&!rl&&r
18024
MC/DC50%1/2
$type =~ /$box_types_to_keep/ and not exists $boxes{$type}
$type =~ /$box_types_to_keep/ not exists $boxes{$type}
Truth table66%2/3
A and not(B)
A= $type =~ /$box_types_to_keep/B= not exists $boxes{$type}
ABresult
0X0
100
111
52 24 $boxes{$type} = "";
53 24 $bufp = \$boxes{$type};
54 }
55 42 ▶ if($len == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 1) 0 42 42
56 0 my($lenhi, $lenlo) = unpack("NN", read_block($fh, 8));
57 0 $pos += 8;
58 0 $len = ($lenhi << 32) | $lenlo;
59 0 ▶ $len >> 32 == $lenhi or die_for_info "box size overflow";
Branch: 1 branch
BranchTrueFalseTotal
$len >> 32 == $lenhi or &die_for_info("box size overflow") 0 0 0
60 }
61 42 ▶ $len >= $pos or die_for_info "bad box length";
Branch: 1 branch
BranchTrueFalseTotal
$len >= $pos or &die_for_info("bad box length") 0 42 42
62 42 $len -= $pos;
63 42 ▶ while($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) 42 42 84
64 42 ▶ my $toread = $len < (1<<16) ? $len : (1<<16);
Branch: 1 branch
BranchTrueFalseTotal
$len < 65536 ? : 42 0 42
65 42 my $d = read_block($fh, $toread);
66 42 ▶ defined($bufp) and $$bufp .= $d;
Branch: 1 branch
BranchTrueFalseTotal
defined $bufp and $$bufp .= $d 24 18 42
67 42 $len -= $toread;
68 }
69 }
70 12 return \%boxes;
71 }
72  
73 2 my @primaries_type;
74 2 $primaries_type[$_] = "RGB" foreach 1, 4, 5, 6, 7, 9, 11, 22;
75 2 $primaries_type[10] = "CIEXYZ";
76  
77 sub process_file {
78 3 ▶ my($info, $source) = @_;
Subroutine process_file: called CC=39 SCAR=49.9
Pod undocumented
79 3 ▶ if(!eval { local $SIG{__DIE__};
Branch: 1 branch
BranchTrueFalseTotal
if (not eval { do { local $SIG{"__DIE__"}; my $boxes = &read_heif($source, qr/\A(?:ftyp|meta)\z/); my $ftyp = $boxes->{"ftyp"}; defined $ftyp or &die_for_info("no ftyp box"); length $ftyp >= 8 and not length $ftyp & 3 or &die_for_info("malformed ftyp box"); substr($ftyp, 0, 4) eq "avif" or &die_for_info("major brand is not \"avif\""); $info->replace_info(0, "file_media_type", "image/avif"); $info->replace_info(0, "file_ext", "avif"); my $mboxes; { my $meta = $boxes->{"meta"}; defined $meta or &die_for_info("no meta box"); my $metafh = &io_string($meta); &read_block($metafh, 1) eq "\0" or &die_for_info("malformed meta box"); &read_block($metafh, 3); $mboxes = &read_heif($metafh, qr/\A(?:hdlr|iprp)\z/); } ; { my $hdlr = $mboxes->{"hdlr"}; defined $hdlr or &die_for_info("no hdlr box"); my $hdlrfh = &io_string($hdlr); &read_block($hdlrfh, 1) eq "\0" or &die_for_info("malformed hdlr box"); &read_block($hdlrfh, 3); unpack("N", &read_block($hdlrfh, 4)) == 0 or &die_for_info("non-zero pre-defined value"); &read_block($hdlrfh, 4) eq "pict" or &die_for_info("handler type is not \"pict\""); &read_block($hdlrfh, 12); &read_nulterm($hdlrfh); } ; my $pboxes; { my $iprp = $mboxes->{"iprp"}; defined $iprp or &die_for_info("no iprp box"); my $iprpfh = &io_string($iprp); $pboxes = &read_heif($iprpfh, qr/\Aipco\z/); } ; my $cboxes; { my $ipco = $pboxes->{"ipco"}; defined $ipco or &die_for_info("no ipco box"); my $ipcofh = &io_string($ipco); $cboxes = &read_heif($ipcofh, qr/\A(?:irot|clap|ispe|pixi|colr|pasp)\z/); } ; my $rot = 0; if (defined(my $irot = $cboxes->{"irot"})) { length $irot >= 1 or &die_for_info("malformed irot box"); my($angle) = unpack("C", $irot); $angle & -4 and &die_for_info("malformed irot box"); $rot = 1 if $angle & 1; } ; if (defined(my $clap = $cboxes->{"clap"})) { length $clap >= 32 or &die_for_info("malformed clap box"); () = unpack("NNNN", $clap); $width_den != 0 and $height_den != 0 or &die_for_info("malformed clap box"); my $width = int $width_num / $width_den; my $height = int $height_num / $height_den; ($width, $height) = ($height, $width) if $rot; $info->replace_info(0, "width", $width); $info->replace_info(0, "height", $height); } elsif (defined(my $ispe = $cboxes->{"ispe"})) { length $ispe >= 12 or &die_for_info("malformed ispe box"); my($ver, undef, $width, $height) = unpack("Ca3NN", $ispe); $ver == 0 or &die_for_info("malformed ispe box"); () = ($height, $width) if $rot; $info->replace_info(0, "width", $width); $info->replace_info(0, "height", $height); } ; if (defined(my $pixi = $cboxes->{"pixi"})) { length $pixi >= 5 or &die_for_info("malformed pixi box"); my($ver, undef, $planes) = unpack("Ca3C", $pixi); $ver == 0 or &die_for_info("malformed pixi box"); length $pixi >= 5 + $planes or &die_for_info("malformed pixi box"); $info->replace_info(0, "SamplesPerPixel", $planes); $info->replace_info(0, "BitsPerSample", [map({unpack "C", substr($pixi, 5 + $_, 1);} 0 .. $planes - 1)]); } ; if (defined(my $colr = $cboxes->{"colr"})) { length $colr >= 4 or &die_for_info("malformed colr box"); my $type = substr($colr, 0, 4); if ($type eq "nclx") { length $colr >= 11 or &die_for_info("malformed colr box"); my($prim) = unpack("n", substr($colr, 4, 2)); if (defined(my $ctype = $primaries_type[$prim])) { $info->replace_info(0, "color_type", $ctype); } ; } ; } ; if (defined(my $pasp = $cboxes->{"pasp"})) { length $pasp >= 8 or &die_for_info("malformed pasp box"); () = unpack("NN", $pasp); $info->replace_info(0, "resolution", "$vspc/$hspc"); } ; 1 } }) 0 3 3
80 3 my $boxes = read_heif($source, qr/\A(?:ftyp|meta)\z/);
81 3 my $ftyp = $boxes->{ftyp};
82 3 ▶ defined $ftyp or die_for_info "no ftyp box";
Branch: 1 branch
BranchTrueFalseTotal
defined $ftyp or &die_for_info("no ftyp box") 0 3 3
83 3 ▶ length($ftyp) >= 8 && !(length($ftyp) & 3)
Branch: 1 branch
BranchTrueFalseTotal
length $ftyp >= 8 and not length $ftyp & 3 or &die_for_info("malformed ftyp box") 0 3 3
Condition33%1/3
length $ftyp >= 8 and not length $ftyp & 3
!ll&&!rl&&r
003
MC/DC0%0/2
length $ftyp >= 8 and not length $ftyp & 3
length $ftyp >= 8 not length $ftyp & 3
Truth table33%1/3
A and not(B)
A= length $ftyp >= 8B= not length $ftyp & 3
ABresult
0X0
100
111
84 or die_for_info "malformed ftyp box";
85 3 ▶ substr($ftyp, 0, 4) eq "avif"
Branch: 1 branch
BranchTrueFalseTotal
substr($ftyp, 0, 4) eq "avif" or &die_for_info("major brand is not \"avif\"") 0 3 3
86 or die_for_info "major brand is not \"avif\"";
87 3 $info->replace_info(0, file_media_type => "image/avif");
88 3 $info->replace_info(0, file_ext => "avif");
89 3 my $mboxes;
90 {
91 3 my $meta = $boxes->{meta};
92 3 ▶ defined $meta or die_for_info "no meta box";
Branch: 1 branch
BranchTrueFalseTotal
defined $meta or &die_for_info("no meta box") 0 3 3
93 3 my $metafh = io_string($meta);
94 3 ▶ read_block($metafh, 1) eq "\x00"
Branch: 1 branch
BranchTrueFalseTotal
&read_block($metafh, 1) eq "\0" or &die_for_info("malformed meta box") 0 3 3
95 or die_for_info "malformed meta box";
96 3 read_block($metafh, 3);
97 3 $mboxes = read_heif($metafh, qr/\A(?:hdlr|iprp)\z/);
98 }
99 {
100 3 my $hdlr = $mboxes->{hdlr};
101 3 ▶ defined $hdlr or die_for_info "no hdlr box";
Branch: 1 branch
BranchTrueFalseTotal
defined $hdlr or &die_for_info("no hdlr box") 0 3 3
102 3 my $hdlrfh = io_string($hdlr);
103 3 ▶ read_block($hdlrfh, 1) eq "\x00"
Branch: 1 branch
BranchTrueFalseTotal
&read_block($hdlrfh, 1) eq "\0" or &die_for_info("malformed hdlr box") 0 3 3
104 or die_for_info "malformed hdlr box";
105 3 read_block($hdlrfh, 3);
106 3 ▶ unpack("N", read_block($hdlrfh, 4)) == 0
Branch: 1 branch
BranchTrueFalseTotal
unpack("N", &read_block($hdlrfh, 4)) == 0 or &die_for_info("non-zero pre-defined value") 0 3 3
107 or die_for_info "non-zero pre-defined value";
108 3 ▶ read_block($hdlrfh, 4) eq "pict"
Branch: 1 branch
BranchTrueFalseTotal
&read_block($hdlrfh, 4) eq "pict" or &die_for_info("handler type is not \"pict\"") 0 3 3
109 or die_for_info "handler type is not \"pict\"";
110 3 read_block($hdlrfh, 12);
111 3 read_nulterm($hdlrfh);
112 }
113 3 my $pboxes;
114 {
115 3 my $iprp = $mboxes->{iprp};
116 3 ▶ defined $iprp or die_for_info "no iprp box";
Branch: 1 branch
BranchTrueFalseTotal
defined $iprp or &die_for_info("no iprp box") 0 3 3
117 3 my $iprpfh = io_string($iprp);
118 3 $pboxes = read_heif($iprpfh, qr/\Aipco\z/);
119 }
120 3 my $cboxes;
121 {
122 3 my $ipco = $pboxes->{ipco};
123 3 ▶ defined $ipco or die_for_info "no ipco box";
Branch: 1 branch
BranchTrueFalseTotal
defined $ipco or &die_for_info("no ipco box") 0 3 3
124 3 my $ipcofh = io_string($ipco);
125 3 $cboxes = read_heif($ipcofh,
126 qr/\A(?:irot|clap|ispe|pixi|colr|pasp)\z/);
127 }
128 3 my $rot = 0;
129 3 ▶ if(defined(my $irot = $cboxes->{irot})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $irot = $cboxes->{"irot"})) 0 3 3
130 0 ▶ length($irot) >= 1 or die_for_info "malformed irot box";
Branch: 1 branch
BranchTrueFalseTotal
length $irot >= 1 or &die_for_info("malformed irot box") 0 0 0
131 0 my($angle) = unpack("C", $irot);
132 0 ▶ !($angle & -4) or die_for_info "malformed irot box";
Branch: 1 branch
BranchTrueFalseTotal
$angle & -4 and &die_for_info("malformed irot box") 0 0 0
133 0 ▶ $rot = 1 if $angle & 1;
Branch: 1 branch
BranchTrueFalseTotal
if $angle & 1 0 0 0
134 }
135 3 ▶ if(defined(my $clap = $cboxes->{clap})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $clap = $cboxes->{'clap'})) { } 0 3 3
elsif (defined(my $ispe = $cboxes->{'ispe'})) { } 3 0 3
136 0 ▶ length($clap) >= 32 or die_for_info "malformed clap box";
Branch: 1 branch
BranchTrueFalseTotal
length $clap >= 32 or &die_for_info("malformed clap box") 0 0 0
137 0 my($width_num, $width_den, $height_num, $height_den) =
138 unpack("NNNN", $clap);
139 0 ▶ $width_den != 0 && $height_den != 0
Branch: 1 branch
BranchTrueFalseTotal
$width_den != 0 and $height_den != 0 or &die_for_info("malformed clap box") 0 0 0
Condition0%0/3
$width_den != 0 and $height_den != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$width_den != 0 and $height_den != 0
$width_den != 0 $height_den != 0
Truth table0%0/3
A and B
A= $width_den != 0B= $height_den != 0
ABresult
0X0
100
111
140 or die_for_info "malformed clap box";
141 0 my $width = int($width_num/$width_den);
142 0 my $height = int($height_num/$height_den);
143 0 ▶ ($width, $height) = ($height, $width) if $rot;
Branch: 1 branch
BranchTrueFalseTotal
if $rot 0 0 0
144 0 $info->replace_info(0, width => $width);
145 0 $info->replace_info(0, height => $height);
146 } elsif(defined(my $ispe = $cboxes->{ispe})) {
147 3 ▶ length($ispe) >= 12 or die_for_info "malformed ispe box";
Branch: 1 branch
BranchTrueFalseTotal
length $ispe >= 12 or &die_for_info("malformed ispe box") 0 3 3
148 3 my($ver, undef, $width, $height) = unpack("Ca3NN", $ispe);
149 3 ▶ $ver == 0 or die_for_info "malformed ispe box";
Branch: 1 branch
BranchTrueFalseTotal
$ver == 0 or &die_for_info("malformed ispe box") 0 3 3
150 3 ▶ ($width, $height) = ($height, $width) if $rot;
Branch: 1 branch
BranchTrueFalseTotal
if $rot 0 3 3
151 3 $info->replace_info(0, width => $width);
152 3 $info->replace_info(0, height => $height);
153 }
154 3 ▶ if(defined(my $pixi = $cboxes->{pixi})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $pixi = $cboxes->{"pixi"})) 3 0 3
155 3 ▶ length($pixi) >= 5 or die_for_info "malformed pixi box";
Branch: 1 branch
BranchTrueFalseTotal
length $pixi >= 5 or &die_for_info("malformed pixi box") 0 3 3
156 3 my($ver, undef, $planes) = unpack("Ca3C", $pixi);
157 3 ▶ $ver == 0 or die_for_info "malformed pixi box";
Branch: 1 branch
BranchTrueFalseTotal
$ver == 0 or &die_for_info("malformed pixi box") 0 3 3
158 3 ▶ length($pixi) >= 5+$planes or die_for_info "malformed pixi box";
Branch: 1 branch
BranchTrueFalseTotal
length $pixi >= 5 + $planes or &die_for_info("malformed pixi box") 0 3 3
159 3 $info->replace_info(0, SamplesPerPixel => $planes);
160 $info->replace_info(0, BitsPerSample =>
161 3 [ map { unpack(q(C), substr($pixi, 5+$_, 1)) } 0..$planes-1 ]);
162 }
163 3 ▶ if(defined(my $colr = $cboxes->{colr})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $colr = $cboxes->{"colr"})) 3 0 3
164 3 ▶ length($colr) >= 4 or die_for_info "malformed colr box";
Branch: 1 branch
BranchTrueFalseTotal
length $colr >= 4 or &die_for_info("malformed colr box") 0 3 3
165 3 my $type = substr($colr, 0, 4);
166 3 ▶ if($type eq "nclx") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "nclx") 3 0 3
167 3 ▶ length($colr) >= 11 or die_for_info "malformed colr box";
Branch: 1 branch
BranchTrueFalseTotal
length $colr >= 11 or &die_for_info("malformed colr box") 0 3 3
168 3 my($prim) = unpack("n", substr($colr, 4, 2));
169 3 ▶ if(defined(my $ctype = $primaries_type[$prim])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ctype = $primaries_type[$prim])) 3 0 3
170 3 $info->replace_info(0, color_type => $ctype);
171 }
172 }
173 }
174 3 ▶ if(defined(my $pasp = $cboxes->{pasp})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $pasp = $cboxes->{"pasp"})) 0 3 3
175 0 ▶ length($pasp) >= 8 or die_for_info "malformed pasp box";
Branch: 1 branch
BranchTrueFalseTotal
length $pasp >= 8 or &die_for_info("malformed pasp box") 0 0 0
176 0 my($hspc, $vspc) = unpack("NN", $pasp);
177 0 $info->replace_info(0, resolution => "$vspc/$hspc");
178 }
179 3 1;
180 }) {
181 0 my $err = $@;
182 0 ▶ if(ref($err) eq __PACKAGE__."::__ERROR__") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $err eq 'Image::Info::AVIF::__ERROR__') { } 0 0 0
183 0 $info->replace_info(0, error => $err->{err});
184 } else {
185 0 die $err;
186 }
187 }
188 }
189  
190 2 1;
191  
192 =begin register
193  
194 MAGIC: /\A....ftypavif/s
195  
196 Supports the basic standard info key names.
197  
198 =end register
199  
200 =head1 NAME
201  
202 Image::Info::AVIF - AV1 Image File Format support for Image::Info
203  
204 =head1 SYNOPSIS
205  
206 use Image::Info qw(image_info);
207  
208 $info = image_info("image.avif");
209 if($error = $info->{error}) {
210 die "Can't parse image info: $error\n";
211 }
212 $color = $info->{color_type};
213  
214 =head1 DESCRIPTION
215  
216 This module supplies information about AVIF files within the
217 L<Image::Info> system. It supports the basic standard info key names.
218  
219 =head1 SEE ALSO
220  
221 L<Image::Info>
222  
223 =head1 AUTHOR
224  
225 Andrew Main (Zefram) <zefram@fysh.org>
226  
227 =head1 COPYRIGHT
228  
229 Copyright (C) 2023 Andrew Main (Zefram) <zefram@fysh.org>
230  
231 =head1 LICENSE
232  
233 This module is free software; you can redistribute it and/or modify it
234 under the same terms as Perl itself.
235  
236 =head1 ACKNOWLEDGEMENT
237  
238 The development of this module was funded by
239 Preisvergleich Internet Services AG.
240  
241 =cut
« blib/lib/Image/Info/BMP.pm ↑ Summary blib/lib/Image/Info/TIFF.pm »