lib/Image/Info/PPM.pm

filter: Statementstmt 95.6% 44 / 46 Branchbran 85.7% 24 / 28 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 2 / 2 Podpod 100% 1 / 1 totaltotal 90.2% 74 / 82 CC 18 SCAR 28.6
CC
18
Cov
91%
process_file
18.3
BEGIN
1.0
CRAP 17.4
« lib/Image/Info/SVG/XMLLibXMLReader.pm ↑ Summary lib/Image/Info/WEBP.pm »
1 package Image::Info::PPM;
2  
3 # Copyright 2000, Gisle Aas.
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the same terms as Perl itself.
7  
8 =begin register
9  
10 MAGIC: /^P[1-6]/
11  
12 =item PBM/PGM/PPM
13  
14 All information available is extracted.
15  
16 =end register
17  
18 =cut
19  
20 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 our $VERSION = 0.05;
23  
24 9 ▶sub process_file {
Subroutine process_file: called CC=18 SCAR=29.1
Pod documented
25 9 my($info, $fh) = @_;
26  
27 9 my @header;
28 my $type;
29 9 my $num_wanted = 3;
30 9 my $binary;
31 my $read_header;
32  
33 9 local($/, $_) = ("\n");
34 9 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 20 1 21
35 20 ▶ if (s/#\s*(.*)//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/#\s*(.*)//) 0 20 20
36 0 $info->push_info(0, "Comment", $1);
37 }
38 20 push(@header, split(' '));
39 20 ▶ if (!$type && @header) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $type and @header) 9 11 20
Condition66%2/3
not $type and @header
!ll&&!rl&&r
1109
MC/DC50%1/2
not $type and @header
not $type @header
Truth table66%2/3
not(A) and B
A= not $typeB= @header
ABresult
0X0
100
111
40 9 $type = shift(@header);
41 9 ▶ $type =~ s/^P// || die;
Branch: 1 branch
BranchTrueFalseTotal
$type =~ s/^P// or die 9 0 9
42 9 ▶ $binary++ if $type > 3;
Branch: 1 branch
BranchTrueFalseTotal
if $type > 3 7 2 9
43 9 $type = "p" . qw/p b g/[$type % 3] . "m";
44 9 ▶ $num_wanted = 2 if $type eq "pbm";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "pbm" 3 6 9
45 }
46  
47 20 for (@header) {
48 32 ▶ unless (/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\d+$/) 0 32 32
49 0 die "Badly formatted $type file";
50 }
51 32 $_ += 0; # strip leading zeroes
52 }
53  
54 20 ▶ next unless @header >= $num_wanted;
Branch: 1 branch
BranchTrueFalseTotal
unless @header >= $num_wanted 12 8 20
55  
56 # Now we know everything there is to know...
57 8 $read_header = 1;
58 8 $info->push_info(0, "file_media_type" => "image/$type");
59 8 $info->push_info(0, "file_ext" => "$type");
60 8 $info->push_info(0, "width", shift @header);
61 8 $info->push_info(0, "height", shift @header);
62 8 $info->push_info(0, "resolution", "1/1");
63  
64 8 ▶ if ($type eq "ppm") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'ppm') { } 3 5 8
65 3 my $MSV = shift @header;
66  
67 3 $info->push_info(0, "MaxSampleValue", $MSV);
68 3 $info->push_info(0, "color_type", "RGB");
69  
70 3 $info->push_info(0, "SamplesPerPixel", 3);
71 3 ▶ if ($binary) {
Branch: 1 branch
BranchTrueFalseTotal
if ($binary) 3 0 3
72 3 for (1..3) {
73 9 $info->push_info(0, "BitsPerSample", int(log($MSV + 1) / log(2) ) );
74 }
75 }
76 }
77 else {
78 5 $info->push_info(0, "color_type", "Gray");
79 5 $info->push_info(0, "SamplesPerPixel", 1);
80 5 ▶ $info->push_info(0, "BitsPerSample", ($type eq "pbm") ? 1 : 8)
Branch: 2 branches
BranchTrueFalseTotal
if $binary 4 1 5
$type eq 'pbm' ? : 2 2 4
81 if $binary;
82 5 ▶ $info->push_info(0, "MaxSampleValue", shift @header) if $type ne 'pbm';
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "pbm" 3 2 5
83 }
84 8 last;
85 }
86  
87 9 ▶ if (!$read_header) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $read_header) 1 8 9
88 1 $info->push_info(0, 'error' => 'Incomplete PBM/PGM/PPM header');
89 }
90 }
91  
92 4 1;
93  
94 =pod
95  
96 =head1 NAME
97  
98 Image::Info::PPM - PPM support Image::Info
99  
100 =head1 SYNOPSIS
101  
102 use Image::Info qw(image_info dim);
103  
104 my $info = image_info("image.ppm");
105 if (my $error = $info->{error}) {
106 die "Can't parse image info: $error\n";
107 }
108 my($w, $h) = dim($info);
109  
110 =head1 DESCRIPTION
111  
112 This modules adds ppm support to L<Image::Info>.
113  
114 It is loaded and used automatically.
115  
116 =head1 METHODS
117  
118 =head2 process_file()
119
120 $info->process_file($source, $options);
121  
122 Processes one file and sets the found info fields in the C<$info> object.
123  
124 =head1 AUTHOR
125  
126 Gisle Aas.
127  
128 =head1 LICENSE
129  
130 This library is free software; you can redistribute it and/or
131 modify it under the same terms as Perl itself.
132  
133 =cut
« lib/Image/Info/SVG/XMLLibXMLReader.pm ↑ Summary lib/Image/Info/WEBP.pm »