lib/Image/Info/PPM.pm

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