Image/ExifTool/PPM.pm

filter: Statementstmt 80.7% 67 / 83 Branchbran 43.5% 27 / 62 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 1 totaltotal 63.2% 98 / 155 CC 35 SCAR 46.2
CC
35
Cov
64%
ProcessPPM
110.0
BEGIN
1.0
BEGIN
1.0
CRAP 101.4
« Image/ExifTool/WPG.pm ↑ Summary Image/ExifTool/PhaseOne.pm »
1 #------------------------------------------------------------------------------
2 # File: PPM.pm
3 #
4 # Description: Read and write PPM meta information
5 #
6 # Revisions: 09/03/2005 - P. Harvey Created
7 #
8 # References: 1) http://netpbm.sourceforge.net/doc/ppm.html
9 # 2) http://netpbm.sourceforge.net/doc/pgm.html
10 # 3) http://netpbm.sourceforge.net/doc/pbm.html
11 #------------------------------------------------------------------------------
12  
13 package Image::ExifTool::PPM;
14  
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Image::ExifTool qw(:DataAccess :Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 $VERSION = '1.11';
20  
21 #------------------------------------------------------------------------------
22 # Read or write information in a PPM/PGM/PBM image
23 # Inputs: 0) ExifTool object reference, 1) Directory information reference
24 # Returns: 1 on success, 0 if this wasn't a valid PPM file, -1 on write error
25 sub ProcessPPM($$)
26 3 ▶{
Subroutine ProcessPPM: called CC=35 SCAR=47.0
Pod undocumented
27 3 my ($et, $dirInfo) = @_;
28 3 my $raf = $$dirInfo{RAF};
29 3 my $outfile = $$dirInfo{OutFile};
30 3 my $verbose = $et->Options('Verbose');
31 3 my $out = $et->Options('TextOut');
32 3 my ($buff, $num, $type, %info, $seal);
33 #
34 # read as much of the image as necessary to extract the header and comments
35 #
36 3 for (;;) {
37 3 ▶ if (defined $buff) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $buff) { } 0 3 3
38 # need to read some more data
39 0 my $tmp;
40 0 ▶ return 0 unless $raf->Read($tmp, 1024);
Branch: 1 branch
BranchTrueFalseTotal
unless $raf->Read($tmp, 1024) 0 0 0
41 0 $buff .= $tmp;
42 } else {
43 3 ▶ return 0 unless $raf->Read($buff, 1024);
Branch: 1 branch
BranchTrueFalseTotal
unless $raf->Read($buff, 1024) 0 3 3
44 }
45 # verify this is a valid PPM file
46 3 ▶ return 0 unless $buff =~ /^P([1-6])\s+/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $buff =~ /^P([1-6])\s+/g 0 3 3
47 3 $num = $1;
48 # note: may contain comments starting with '#'
49 3 ▶ if ($buff =~ /\G#/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buff =~ /\G#/cg) { } 3 0 3
50 # must read more if we are in the middle of a comment
51 3 ▶ next unless $buff =~ /\G ?(.*[\n\r]+(#.*[\n\r]+)*)\s*/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $buff =~ /\G ?(.*[\n\r]+(#.*[\n\r]+)*)\s*/g 0 3 3
52 3 $info{Comment} = $1;
53 3 ▶ next if $buff =~ /\G#/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $buff =~ /\G#/cg 0 3 3
54 } else {
55 0 delete $info{Comment};
56 }
57 3 ▶ next unless $buff =~ /\G(\S+)\s+(\S+)\s+/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $buff =~ /\G(\S+)\s+(\S+)\s+/g 0 3 3
58 3 $info{ImageWidth} = $1;
59 3 $info{ImageHeight} = $2;
60 3 $type = [qw{PPM PBM PGM}]->[$num % 3];
61 3 ▶ last if $type eq 'PBM'; # (no MaxVal for PBM images)
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "PBM" 0 3 3
62 3 ▶ if ($buff =~ /\G\s*#/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buff =~ /\G\s*#/cg) 0 3 3
63 0 ▶ next unless $buff =~ /\G ?(.*[\n\r]+(#.*[\n\r]+)*)\s*/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $buff =~ /\G ?(.*[\n\r]+(#.*[\n\r]+)*)\s*/g 0 0 0
64 0 ▶ $info{Comment} = '' unless exists $info{Comment};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $info{"Comment"} 0 0 0
65 0 $info{Comment} .= $1;
66 0 ▶ next if $buff =~ /\G#/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $buff =~ /\G#/cg 0 0 0
67 }
68 3 ▶ next unless $buff =~ /\G(\S+)\s/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $buff =~ /\G(\S+)\s/g 0 3 3
69 3 $info{MaxVal} = $1;
70 3 last;
71 }
72 # validate numerical values
73 3 foreach (keys %info) {
74 12 ▶ next if $_ eq 'Comment';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "Comment" 3 9 12
75 9 ▶ return 0 unless $info{$_} =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $info{$_} =~ /^\d+$/ 0 9 9
76 }
77 3 ▶ if (defined $info{Comment}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info{"Comment"}) 3 0 3
78 3 $info{Comment} =~ s/^# ?//mg; # remove "# " at the start of each line
79 3 $info{Comment} =~ s/[\n\r]+$//; # remove trailing newline
80 3 ▶ $seal = 1 if $info{Comment} =~ /^<seal seal=/;
Branch: 1 branch
BranchTrueFalseTotal
if $info{"Comment"} =~ /^<seal seal=/ 0 3 3
81 }
82 3 $et->SetFileType($type);
83 3 my $len = pos($buff);
84 #
85 # rewrite the file if requested
86 #
87 3 ▶ if ($outfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile) 1 2 3
88 1 my $nvHash;
89 1 my $newComment = $et->GetNewValue('Comment', \$nvHash);
90 1 my $oldComment = $info{Comment};
91 1 ▶ if ($et->IsOverwriting($nvHash, $oldComment)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($et->IsOverwriting($nvHash, $oldComment)) { } 1 0 1
elsif ($seal and $et->{'DEL_GROUP'}{'SEAL'}) { } 0 0 0
92 1 ++$$et{CHANGED};
93 1 ▶ $et->VerboseValue('- Comment', $oldComment) if defined $oldComment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $oldComment 1 0 1
94 1 ▶ $et->VerboseValue('+ Comment', $newComment) if defined $newComment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $newComment 1 0 1
95 ▶ } elsif ($seal and $$et{DEL_GROUP}{SEAL}) {
Condition0%0/3
$seal and $et->{"DEL_GROUP"}{"SEAL"}
!ll&&!rl&&r
000
MC/DC0%0/2
$seal and $et->{"DEL_GROUP"}{"SEAL"}
$seal $et->{"DEL_GROUP"}{"SEAL"}
Truth table0%0/3
A and B
A= $sealB= $et->{"DEL_GROUP"}{"SEAL"}
ABresult
0X0
100
111
96 # delete SEAL comment
97 0 $et->VerboseValue('- Comment', $oldComment);
98 0 ++$$et{CHANGED};
99 } else {
100 0 $newComment = $oldComment; # use existing comment
101 }
102 1 my $hdr = "P$num\n";
103 1 ▶ if (defined $newComment) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newComment) 1 0 1
104 1 $newComment =~ s/\n/\n# /g;
105 1 $hdr .= "# $newComment\n";
106 }
107 1 $hdr .= "$info{ImageWidth} $info{ImageHeight}\n";
108 1 ▶ $hdr .= "$info{MaxVal}\n" if $type ne 'PBM';
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "PBM" 1 0 1
109 # write header and start of image
110 1 ▶ Write($outfile, $hdr, substr($buff, $len)) or return -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $hdr, substr($buff, $len)) or return -1 1 0 1
111 # copy over the rest of the image
112 1 ▶ while ($raf->Read($buff, 0x10000)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raf->Read($buff, 65536)) 0 1 1
113 0 ▶ Write($outfile, $buff) or return -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $buff) or return -1 0 0 0
114 }
115 1 return 1;
116 }
117 #
118 # save extracted information
119 #
120 2 ▶ if ($verbose > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 2) 0 2 2
121 0 print $out "$type header ($len bytes):\n";
122 0 $et->VerboseDump(\$buff, Len => $len);
123 }
124 2 ▶ if ($seal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seal) 0 2 2
125 0 $et->ProcessDirectory({ DataPt => \$info{Comment} }, GetTagTable('Image::ExifTool::XMP::SEAL'));
126 0 delete $info{Comment};
127 }
128 2 my $tag;
129 2 foreach $tag (qw{Comment ImageWidth ImageHeight MaxVal}) {
130 8 ▶ $et->FoundTag($tag, $info{$tag}) if defined $info{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info{$tag} 8 0 8
131 }
132 2 return 1;
133 }
134  
135 1 1; # end
136  
137 __END__
« Image/ExifTool/WPG.pm ↑ Summary Image/ExifTool/PhaseOne.pm »