File Coverage

lib/Image/Info/PPM.pm
Criterion Covered Total %
statement 42 44 95.4
branch 22 26 84.6
condition 2 3 66.6
subroutine 2 2 100.0
pod 1 1 100.0
total 69 76 90.7


line stmt bran cond sub pod time code
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     4   4133 use strict;
  4         6  
  4         2756  
21              
22             our $VERSION = 0.05;
23              
24             sub process_file {
25 9     9 1 17 my($info, $fh) = @_;
26              
27 9         13 my @header;
28             my $type;
29 9         11 my $num_wanted = 3;
30 9         15 my $binary;
31             my $read_header;
32              
33 9         37 local($/, $_) = ("\n");
34 9         94 while (<$fh>) {
35 20 50       44 if (s/#\s*(.*)//) {
36 0         0 $info->push_info(0, "Comment", $1);
37             }
38 20         42 push(@header, split(' '));
39 20 100 66     66 if (!$type && @header) {
40 9         15 $type = shift(@header);
41 9 50       35 $type =~ s/^P// || die;
42 9 100       29 $binary++ if $type > 3;
43 9         20 $type = "p" . qw/p b g/[$type % 3] . "m";
44 9 100       21 $num_wanted = 2 if $type eq "pbm";
45             }
46              
47 20         28 for (@header) {
48 32 50       93 unless (/^\d+$/) {
49 0         0 die "Badly formatted $type file";
50             }
51 32         58 $_ += 0; # strip leading zeroes
52             }
53              
54 20 100       70 next unless @header >= $num_wanted;
55              
56             # Now we know everything there is to know...
57 8         11 $read_header = 1;
58 8         34 $info->push_info(0, "file_media_type" => "image/$type");
59 8         22 $info->push_info(0, "file_ext" => "$type");
60 8         16 $info->push_info(0, "width", shift @header);
61 8         16 $info->push_info(0, "height", shift @header);
62 8         34 $info->push_info(0, "resolution", "1/1");
63              
64 8 100       76 if ($type eq "ppm") {
65 3         5 my $MSV = shift @header;
66              
67 3         8 $info->push_info(0, "MaxSampleValue", $MSV);
68 3         6 $info->push_info(0, "color_type", "RGB");
69              
70 3         6 $info->push_info(0, "SamplesPerPixel", 3);
71 3 50       6 if ($binary) {
72 3         10 for (1..3) {
73 9         24 $info->push_info(0, "BitsPerSample", int(log($MSV + 1) / log(2) ) );
74             }
75             }
76             }
77             else {
78 5         9 $info->push_info(0, "color_type", "Gray");
79 5         16 $info->push_info(0, "SamplesPerPixel", 1);
80 5 100       15 $info->push_info(0, "BitsPerSample", ($type eq "pbm") ? 1 : 8)
    100          
81             if $binary;
82 5 100       11 $info->push_info(0, "MaxSampleValue", shift @header) if $type ne 'pbm';
83             }
84 8         12 last;
85             }
86              
87 9 100       37 if (!$read_header) {
88 1         6 $info->push_info(0, 'error' => 'Incomplete PBM/PGM/PPM header');
89             }
90             }
91              
92             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