blib/lib/EBook/Ishmael/ImageID.pm

filter: Statementstmt 42.2% 38 / 90 Branchbran 27.2% 12 / 44 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 62.5% 10 / 16 Podpod 100% 5 / 5 totaltotal 39.3% 65 / 165 CC 25 SCAR 26.8
CC
25
Cov
36%
__ANON__
30.0
image_id
20.2
__ANON__
20.0
CRAP 14.6
« blib/lib/EBook/Ishmael/EBook/Mobi.pm ↑ Summary blib/lib/EBook/Ishmael.pm »
1 package EBook::Ishmael::ImageID;
2 17 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 our $VERSION = '2.04';
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 our @EXPORT_OK = qw(
9 image_id image_size is_image_path mimetype_id image_path_id
10 );
11  
12 17 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # This function may not support many image formats as it was designed for
17 # getting image sizes for CHM files to determine the cover image. CHMs
18 # primarily use GIFs.
19 # TODO: Add tif support
20 # TODO: Add webp support (probably never)
21 my %SIZE = (
22 # size stored as two BE ushorts in the SOF0 marker, at offset 5.
23 'jpg' => sub {
24  
25 0 ▶ my $img = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
26  
27 0 my $len = length $$img;
28  
29 0 my $p = 2;
30  
31 0 my $sof = join ' ', 0xff, 0xc0;
32  
33 0 ▶ while ($p < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < $len) 0 0 0
34  
35 0 my $id = join ' ', unpack "CC", substr $$img, $p, 2;
36 0 $p += 2;
37 0 my $mlen = unpack "n", substr $$img, $p, 2;
38  
39 0 ▶ unless ($id eq $sof) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id eq $sof) 0 0 0
40 0 $p += $mlen;
41 0 next;
42 }
43  
44 0 my ($y, $x) = unpack "nn", substr $$img, $p + 3, 4;
45  
46 0 return [ $x, $y ];
47  
48 }
49  
50 0 return undef;
51  
52 },
53 # size stored as two BE ulongs at offset 16
54 'png' => sub {
55  
56 0 ▶ my $img = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
57  
58 0 ▶ return undef unless length $$img > 24;
Branch: 1 branch
BranchTrueFalseTotal
unless length $$img > 24 0 0 0
59  
60 0 my ($x, $y) = unpack "N N", substr $$img, 16, 8;
61  
62 0 return [ $x, $y ];
63  
64 },
65 # size stored as two LE ushorts at offset 6
66 'gif' => sub {
67  
68 0 ▶ my $img = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
69  
70 0 ▶ return undef unless length $$img > 10;
Branch: 1 branch
BranchTrueFalseTotal
unless length $$img > 10 0 0 0
71  
72 0 my ($x, $y) = unpack "v v", substr $$img, 6, 4;
73  
74 0 return [ $x, $y ];
75  
76 },
77 # size storage depends on header. For an OS header, two LE ushorts at
78 # offset 18. For Windows, two LE signed longs at offset 18.
79 'bmp' => sub {
80  
81 0 ▶ my $img = shift;
Subroutine __ANON__: not called CC=5 SCAR=34.0
82  
83 0 ▶ return undef unless length $$img > 24;
Branch: 1 branch
BranchTrueFalseTotal
unless length $$img > 24 0 0 0
84  
85 0 my $dbisize = unpack "V", substr $$img, 14, 4;
86  
87 0 my ($x, $y);
88  
89 # OS
90 0 ▶ if ($dbisize == 16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbisize == 16) { } 0 0 0
91 0 ($x, $y) = unpack "v v", substr $$img, 18, 4;
92 # Win
93 } else {
94 0 ($x, $y) = unpack "(ll)<", substr $$img, 18, 8;
95 0 ▶ return undef if $x < 0 or $y < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $x < 0 or $y < 0 0 0 0
Condition0%0/3
$x < 0 or $y < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$x < 0 or $y < 0
$x < 0 $y < 0
Truth table0%0/3
A or B
A= $x < 0B= $y < 0
ABresult
1X1
011
000
96 }
97  
98 0 return [ $x, $y ];
99  
100 },
101 # Get width and height attributes of root node.
102 'svg' => sub {
103  
104 0 ▶ my $img = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
105  
106 0 ▶ my $dom = eval { XML::LibXML->load_xml(string => $img) }
Branch: 1 branch
BranchTrueFalseTotal
my $dom = eval { do { "XML::LibXML"->load_xml("string", $img) } } or return undef 0 0 0
107 or return undef;
108  
109 0 my $svg = $dom->documentElement;
110  
111 0 ▶ my $x = $svg->getAttribute('width') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $x = $svg->getAttribute("width") or return undef 0 0 0
112 0 ▶ my $y = $svg->getAttribute('height') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $y = $svg->getAttribute("height") or return undef 0 0 0
113  
114 0 return [ $x, $y ];
115  
116 },
117 17 );
118  
119 17 my %MIME_TYPES = (
120 'image/png' => 'png',
121 'image/jpeg' => 'jpg',
122 'image/tiff' => 'tiff',
123 'image/tiff-fx' => 'tiff',
124 'image/gif' => 'gif',
125 'image/bmp' => 'bmp',
126 'image/x-bmp' => 'bmp',
127 'image/webp' => 'webp',
128 'image/svg+xml' => 'svg',
129 'image/jxl' => 'jxl',
130 'image/avif' => 'avif',
131 );
132  
133 17 my %IMAGE_SUFFIXES = (
134 'png' => 'png',
135 'jpg' => 'jpg',
136 'jpeg' => 'jpeg',
137 'tif' => 'tiff',
138 'tiff' => 'tiff',
139 'gif' => 'gif',
140 'bmp' => 'bmp',
141 'webp' => 'webp',
142 'svg' => 'jxl',
143 'avif' => 'avif',
144 );
145  
146 17 my $IMGRX = do {
147 17 my $s = sprintf "(%s)", join '|', keys %IMAGE_SUFFIXES;
148 17 qr/$s/;
149 };
150  
151 sub image_id {
152  
153 95 ▶ my $img = shift;
Subroutine image_id: called CC=10 SCAR=30.0
Pod documented
154  
155 95 ▶ if ($img =~ /^\xff\xd8\xff/) {
Branch: 9 branches
BranchTrueFalseTotal
if ($img =~ /^\xff\xd8\xff/u) { } 54 41 95
elsif ($img =~ /^\x89\x50\x4e\x47\x0d\x0a\x1a\x0a/u) { } 31 10 41
elsif ($img =~ /^GIF8[79]a/u) { } 0 10 10
elsif ($img =~ /^\x52\x49\x46\x46....\x57\x45\x42\x50\x56\x50\x38/u) { } 0 10 10
elsif ($img =~ /^BM/u) { } 0 10 10
elsif ($img =~ /^(\x49\x49\x2a\x00|\x4d\x4d\x00\x2a)/u) { } 0 10 10
elsif ($img =~ /\A....ftypavif/su) { } 0 10 10
elsif ($img =~ /^(\xff\x0a|\x00{3}\x0c\x4a\x58\x4c\x20\x0d\x0a\x87\x0a)/u) { } 0 10 10
elsif (substr($img, 0, 1024) =~ /<\s*svg[^<>]*>/u) { } 0 10 10
156 54 return 'jpg';
157 } elsif ($img =~ /^\x89\x50\x4e\x47\x0d\x0a\x1a\x0a/) {
158 31 return 'png';
159 } elsif ($img =~ /^GIF8[79]a/) {
160 0 return 'gif';
161 } elsif ($img =~ /^\x52\x49\x46\x46....\x57\x45\x42\x50\x56\x50\x38/) {
162 0 return 'webp';
163 } elsif ($img =~ /^BM/) {
164 0 return 'bmp';
165 } elsif ($img =~ /^(\x49\x49\x2a\x00|\x4d\x4d\x00\x2a)/) {
166 0 return 'tif';
167 } elsif ($img =~ /\A....ftypavif/s) {
168 0 return 'avif';
169 } elsif ($img =~ /^(\xff\x0a|\x00{3}\x0c\x4a\x58\x4c\x20\x0d\x0a\x87\x0a)/) {
170 0 return 'jxl';
171 } elsif (substr($img, 0, 1024) =~ /<\s*svg[^<>]*>/) {
172 0 return 'svg';
173 } else {
174 10 return undef;
175 }
176  
177 }
178  
179 sub image_size {
180  
181 0 ▶ my $img = shift;
Subroutine image_size: not called CC=4 SCAR=30.0
Pod documented
182 0 ▶ my $fmt = shift // image_id($img);
Condition0%0/3
shift() // image_id($img)
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() // image_id($img)
shift() image_id($img)
Truth table0%0/3
A // B
A= shift()B= image_id($img)
ABresult
1X1
011
000
183  
184 0 ▶ unless (defined $fmt) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fmt) 0 0 0
185 0 die "Could not determine image data format\n";
186 }
187  
188 0 ▶ unless (exists $SIZE{ $fmt }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $SIZE{$fmt}) 0 0 0
189 0 return undef;
190 }
191  
192 0 return $SIZE{ $fmt }->(\$img);
193  
194 }
195  
196 sub is_image_path {
197  
198 33 ▶ my $path = shift;
Subroutine is_image_path: called CC=1 SCAR=0.0
Pod documented
199  
200 33 return $path =~ /\.$IMGRX$/;
201  
202 }
203  
204 sub mimetype_id {
205  
206 110 ▶ my ($mime) = @_;
Subroutine mimetype_id: called CC=1 SCAR=0.0
Pod documented
207  
208 110 return $MIME_TYPES{ $mime };
209  
210 }
211  
212 sub image_path_id {
213  
214 291 ▶ my ($path) = @_;
Subroutine image_path_id: called CC=2 SCAR=7.1
Pod documented
215  
216 291 ▶ $path =~ /\.([^.]+)$/ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$path =~ /\.([^.]+)$/u or return undef 0 291 291
217 291 return $IMAGE_SUFFIXES{ lc $1 };
218  
219 }
220  
221 17 1;
222  
223 =head1 NAME
224  
225 EBook::Ishmael::ImageID - Identify image data format
226  
227 =head1 SYNOPSIS
228  
229 use EBook::Ishmael::ImageID;
230  
231 my $format = image_id($img);
232  
233 =head1 DESCRIPTION
234  
235 B<EBook::Ishmael::ImageID> is a module that provides the C<image_id()>
236 subroutine, which identifies the image format of a given buffer. This is
237 developer documentation, for L<ishmael> user documentation you should consult
238 its manual.
239  
240 Currently, the following formats are supported:
241  
242 =over 4
243  
244 =item jpg
245  
246 =item png
247  
248 =item gif
249  
250 =item webp
251  
252 =item bmp
253  
254 =item tif
255  
256 =item svg
257  
258 =item avif
259  
260 =item jxl
261  
262 =back
263  
264 =head1 SUBROUTINES
265  
266 =over 4
267  
268 =item $f = image_id($img)
269  
270 Returns a string of the image format of the given image buffer.
271 Returns C<undef> if the image's format could not be
272 identified.
273  
274 =item [$x, $y] = image_size($img, [$fmt])
275  
276 Returns an C<$x>/C<$y> pair representing the image data's size. C<$fmt> is an
277 optional argument specifying the format to use for the image data. If not
278 specified, C<image_size> will identify the format itself. If the image size
279 could not be determined, returns C<undef>.
280  
281 This subroutine does not support the following formats (yet):
282  
283 =over 4
284  
285 =item webp
286  
287 =item tif
288  
289 =item avif
290  
291 =item jxl
292  
293 =back
294  
295 =item $bool = is_image_path($path)
296  
297 Returns true if C<$path> looks like an image path name.
298  
299 =item $f = mimetype_id(($mime)
300  
301 Identifies the image format based on the given mimetype. Returns C<undef> if
302 C<$mime> is not recognized.
303  
304 =item $f = image_path_id($path)
305  
306 Identifies the image format based on the given file path based on its suffix.
307 Returns C<undef> if the file's format cannot be recognized.
308  
309 =back
310  
311 =head1 AUTHOR
312  
313 Written by Samuel Young, E<lt>samyoung12788@gmail.comE<gt>.
314  
315 This project's source can be found on its
316 L<Codeberg Page|https://codeberg.org/1-1sam/ishmael>. Comments and pull
317 requests are welcome!
318  
319 =head1 COPYRIGHT
320  
321 Copyright (C) 2025-2026 Samuel Young
322  
323 This program is free software: you can redistribute it and/or modify
324 it under the terms of the GNU General Public License as published by
325 the Free Software Foundation, either version 3 of the License, or
326 (at your option) any later version.
327  
328 =cut
« blib/lib/EBook/Ishmael/EBook/Mobi.pm ↑ Summary blib/lib/EBook/Ishmael.pm »