blib/lib/Image/Info/AVIF.pm untested

filter: Statementstmt 0.0% 0 / 158 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 93 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 307 CC 21 SCAR 46.5
CC
21
Cov
0%
process_file
210.0
read_block
20.0
read_heif
20.0
CRAP 104.1
1 package Image::Info::AVIF;
2  
3 { use 5.006; }
4 use warnings;
5 use strict;
6  
7 our $VERSION = "0.01";
8  
9 sub die_for_info($) { die bless({ err=>$_[0] }, __PACKAGE__."::__ERROR__") }
10  
11 BEGIN {
12 if("$]" >= 5.008) {
13 *io_string = sub ($) { open(my $fh, "<", \$_[0]); $fh };
14 } else {
15 require IO::String;
16 *io_string = sub ($) { IO::String->new($_[0]) };
17 }
18 }
19  
20 sub read_block($$) {
21 my($fh, $len) = @_;
22 my $d = "";
23 while(1) {
24 my $dlen = length($d);
25 last if $dlen == $len;
26 my $n = read($fh, $d, $len - $dlen, $dlen);
27 if(!defined($n)) {
28 die_for_info "read error: $!";
29 } elsif($n == 0) {
30 die_for_info "truncated file";
31 }
32 }
33 return $d;
34 }
35  
36 sub read_nulterm($) {
37 my($fh) = @_;
38 my $d = do { local $/ = "\x00"; <$fh> };
39 defined($d) && $d =~ /\x00\z/ or die_for_info "truncated file";
40 chop $d;
41 return $d;
42 }
43  
44 sub read_heif($$) {
45 my($fh, $box_types_to_keep) = @_;
46 my %boxes;
47 while(!eof($fh)) {
48 my($len, $type) = unpack("Na4", read_block($fh, 8));
49 my $pos = 8;
50 my $bufp;
51 if($type =~ $box_types_to_keep && !exists($boxes{$type})) {
52 $boxes{$type} = "";
53 $bufp = \$boxes{$type};
54 }
55 if($len == 1) {
56 my($lenhi, $lenlo) = unpack("NN", read_block($fh, 8));
57 $pos += 8;
58 $len = ($lenhi << 32) | $lenlo;
59 $len >> 32 == $lenhi or die_for_info "box size overflow";
60 }
61 $len >= $pos or die_for_info "bad box length";
62 $len -= $pos;
63 while($len) {
64 my $toread = $len < (1<<16) ? $len : (1<<16);
65 my $d = read_block($fh, $toread);
66 defined($bufp) and $$bufp .= $d;
67 $len -= $toread;
68 }
69 }
70 return \%boxes;
71 }
72  
73 my @primaries_type;
74 $primaries_type[$_] = "RGB" foreach 1, 4, 5, 6, 7, 9, 11, 22;
75 $primaries_type[10] = "CIEXYZ";
76  
77 sub process_file {
78 my($info, $source) = @_;
79 if(!eval { local $SIG{__DIE__};
80 my $boxes = read_heif($source, qr/\A(?:ftyp|meta)\z/);
81 my $ftyp = $boxes->{ftyp};
82 defined $ftyp or die_for_info "no ftyp box";
83 length($ftyp) >= 8 && !(length($ftyp) & 3)
84 or die_for_info "malformed ftyp box";
85 substr($ftyp, 0, 4) eq "avif"
86 or die_for_info "major brand is not \"avif\"";
87 $info->replace_info(0, file_media_type => "image/avif");
88 $info->replace_info(0, file_ext => "avif");
89 my $mboxes;
90 {
91 my $meta = $boxes->{meta};
92 defined $meta or die_for_info "no meta box";
93 my $metafh = io_string($meta);
94 read_block($metafh, 1) eq "\x00"
95 or die_for_info "malformed meta box";
96 read_block($metafh, 3);
97 $mboxes = read_heif($metafh, qr/\A(?:hdlr|iprp)\z/);
98 }
99 {
100 my $hdlr = $mboxes->{hdlr};
101 defined $hdlr or die_for_info "no hdlr box";
102 my $hdlrfh = io_string($hdlr);
103 read_block($hdlrfh, 1) eq "\x00"
104 or die_for_info "malformed hdlr box";
105 read_block($hdlrfh, 3);
106 unpack("N", read_block($hdlrfh, 4)) == 0
107 or die_for_info "non-zero pre-defined value";
108 read_block($hdlrfh, 4) eq "pict"
109 or die_for_info "handler type is not \"pict\"";
110 read_block($hdlrfh, 12);
111 read_nulterm($hdlrfh);
112 }
113 my $pboxes;
114 {
115 my $iprp = $mboxes->{iprp};
116 defined $iprp or die_for_info "no iprp box";
117 my $iprpfh = io_string($iprp);
118 $pboxes = read_heif($iprpfh, qr/\Aipco\z/);
119 }
120 my $cboxes;
121 {
122 my $ipco = $pboxes->{ipco};
123 defined $ipco or die_for_info "no ipco box";
124 my $ipcofh = io_string($ipco);
125 $cboxes = read_heif($ipcofh,
126 qr/\A(?:irot|clap|ispe|pixi|colr|pasp)\z/);
127 }
128 my $rot = 0;
129 if(defined(my $irot = $cboxes->{irot})) {
130 length($irot) >= 1 or die_for_info "malformed irot box";
131 my($angle) = unpack("C", $irot);
132 !($angle & -4) or die_for_info "malformed irot box";
133 $rot = 1 if $angle & 1;
134 }
135 if(defined(my $clap = $cboxes->{clap})) {
136 length($clap) >= 32 or die_for_info "malformed clap box";
137 my($width_num, $width_den, $height_num, $height_den) =
138 unpack("NNNN", $clap);
139 $width_den != 0 && $height_den != 0
140 or die_for_info "malformed clap box";
141 my $width = int($width_num/$width_den);
142 my $height = int($height_num/$height_den);
143 ($width, $height) = ($height, $width) if $rot;
144 $info->replace_info(0, width => $width);
145 $info->replace_info(0, height => $height);
146 } elsif(defined(my $ispe = $cboxes->{ispe})) {
147 length($ispe) >= 12 or die_for_info "malformed ispe box";
148 my($ver, undef, $width, $height) = unpack("Ca3NN", $ispe);
149 $ver == 0 or die_for_info "malformed ispe box";
150 ($width, $height) = ($height, $width) if $rot;
151 $info->replace_info(0, width => $width);
152 $info->replace_info(0, height => $height);
153 }
154 if(defined(my $pixi = $cboxes->{pixi})) {
155 length($pixi) >= 5 or die_for_info "malformed pixi box";
156 my($ver, undef, $planes) = unpack("Ca3C", $pixi);
157 $ver == 0 or die_for_info "malformed pixi box";
158 length($pixi) >= 5+$planes or die_for_info "malformed pixi box";
159 $info->replace_info(0, SamplesPerPixel => $planes);
160 $info->replace_info(0, BitsPerSample =>
161 [ map { unpack(q(C), substr($pixi, 5+$_, 1)) } 0..$planes-1 ]);
162 }
163 if(defined(my $colr = $cboxes->{colr})) {
164 length($colr) >= 4 or die_for_info "malformed colr box";
165 my $type = substr($colr, 0, 4);
166 if($type eq "nclx") {
167 length($colr) >= 11 or die_for_info "malformed colr box";
168 my($prim) = unpack("n", substr($colr, 4, 2));
169 if(defined(my $ctype = $primaries_type[$prim])) {
170 $info->replace_info(0, color_type => $ctype);
171 }
172 }
173 }
174 if(defined(my $pasp = $cboxes->{pasp})) {
175 length($pasp) >= 8 or die_for_info "malformed pasp box";
176 my($hspc, $vspc) = unpack("NN", $pasp);
177 $info->replace_info(0, resolution => "$vspc/$hspc");
178 }
179 1;
180 }) {
181 my $err = $@;
182 if(ref($err) eq __PACKAGE__."::__ERROR__") {
183 $info->replace_info(0, error => $err->{err});
184 } else {
185 die $err;
186 }
187 }
188 }
189  
190 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