File Coverage

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