blib/lib/Image/Info/PPM.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 3 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 84 CC 13 SCAR 50.6
CC
13
Cov
0%
process_file
182.0
BEGIN
2.0
BEGIN
2.0
CRAP 158.0
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 use strict;
21 use vars qw/$VERSION/;
22  
23 $VERSION = 0.04;
24  
25 sub process_file {
26 my($info, $fh) = @_;
27  
28 my @header;
29 my $type;
30 my $num_wanted = 3;
31 my $binary;
32 my $read_header;
33  
34 local($/, $_) = ("\n");
35 while (<$fh>) {
36 if (s/#\s*(.*)//) {
37 $info->push_info(0, "Comment", $1);
38 }
39 push(@header, split(' '));
40 if (!$type && @header) {
41 $type = shift(@header);
42 $type =~ s/^P// || die;
43 $binary++ if $type > 3;
44 $type = "p" . qw/p b g/[$type % 3] . "m";
45 $num_wanted = 2 if $type eq "pbm";
46 }
47  
48 for (@header) {
49 unless (/^\d+$/) {
50 die "Badly formatted $type file";
51 }
52 $_ += 0; # strip leading zeroes
53 }
54  
55 next unless @header >= $num_wanted;
56  
57 # Now we know everything there is to know...
58 $read_header = 1;
59 $info->push_info(0, "file_media_type" => "image/$type");
60 $info->push_info(0, "file_ext" => "$type");
61 $info->push_info(0, "width", shift @header);
62 $info->push_info(0, "height", shift @header);
63 $info->push_info(0, "resolution", "1/1");
64  
65 if ($type eq "ppm") {
66 my $MSV = shift @header;
67  
68 $info->push_info(0, "MaxSampleValue", $MSV);
69 $info->push_info(0, "color_type", "RGB");
70  
71 $info->push_info(0, "SamplesPerPixel", 3);
72 if ($binary) {
73 for (1..3) {
74 $info->push_info(0, "BitsPerSample", int(log($MSV + 1) / log(2) ) );
75 }
76 }
77 }
78 else {
79 $info->push_info(0, "color_type", "Gray");
80 $info->push_info(0, "SamplesPerPixel", 1);
81 $info->push_info(0, "BitsPerSample", ($type eq "pbm") ? 1 : 8)
82 if $binary;
83 $info->push_info(0, "MaxSampleValue", shift @header) if $type ne 'pbm';
84 }
85 last;
86 }
87  
88 if (!$read_header) {
89 $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<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