| 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
| |||||||||||||||||||||
| 38 | # need to read some more data | ||||||||||||||||||||
| 39 | 0 | my $tmp; | |||||||||||||||||||
| 40 | 0 | ▶ | return 0 unless $raf->Read($tmp, 1024); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 0 | $buff .= $tmp; | |||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||
| 43 | 3 | ▶ | return 0 unless $raf->Read($buff, 1024); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | # verify this is a valid PPM file | ||||||||||||||||||||
| 46 | 3 | ▶ | return 0 unless $buff =~ /^P([1-6])\s+/g; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 3 | $num = $1; | |||||||||||||||||||
| 48 | # note: may contain comments starting with '#' | ||||||||||||||||||||
| 49 | 3 | ▶ | if ($buff =~ /\G#/gc) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 52 | 3 | $info{Comment} = $1; | |||||||||||||||||||
| 53 | 3 | ▶ | next if $buff =~ /\G#/gc; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||
| 55 | 0 | delete $info{Comment}; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | 3 | ▶ | next unless $buff =~ /\G(\S+)\s+(\S+)\s+/g; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 62 | 3 | ▶ | if ($buff =~ /\G\s*#/gc) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 0 | ▶ | next unless $buff =~ /\G ?(.*[\n\r]+(#.*[\n\r]+)*)\s*/g; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 0 | ▶ | $info{Comment} = '' unless exists $info{Comment}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 0 | $info{Comment} .= $1; | |||||||||||||||||||
| 66 | 0 | ▶ | next if $buff =~ /\G#/gc; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | 3 | ▶ | next unless $buff =~ /\G(\S+)\s/g; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 75 | 9 | ▶ | return 0 unless $info{$_} =~ /^\d+$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | 3 | ▶ | if (defined $info{Comment}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 92 | 1 | ++$$et{CHANGED}; | |||||||||||||||||||
| 93 | 1 | ▶ | $et->VerboseValue('- Comment', $oldComment) if defined $oldComment; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 1 | ▶ | $et->VerboseValue('+ Comment', $newComment) if defined $newComment; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | ▶ | } elsif ($seal and $$et{DEL_GROUP}{SEAL}) { | |||||||||||||||||||
|
Condition0%0/3
$seal and $et->{"DEL_GROUP"}{"SEAL"}
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"}
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 109 | # write header and start of image | ||||||||||||||||||||
| 110 | 1 | ▶ | Write($outfile, $hdr, substr($buff, $len)) or return -1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | # copy over the rest of the image | ||||||||||||||||||||
| 112 | 1 | ▶ | while ($raf->Read($buff, 0x10000)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | 0 | ▶ | Write($outfile, $buff) or return -1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | 1 | return 1; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | # | ||||||||||||||||||||
| 118 | # save extracted information | ||||||||||||||||||||
| 119 | # | ||||||||||||||||||||
| 120 | 2 | ▶ | if ($verbose > 2) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 0 | print $out "$type header ($len bytes):\n"; | |||||||||||||||||||
| 122 | 0 | $et->VerboseDump(\$buff, Len => $len); | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | 2 | ▶ | if ($seal) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | 2 | return 1; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 1 | 1; # end | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||