[16842] | 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 | use strict;
|
---|
| 16 | use vars qw($VERSION);
|
---|
| 17 | use Image::ExifTool qw(:DataAccess :Utils);
|
---|
| 18 |
|
---|
| 19 | $VERSION = '1.04';
|
---|
| 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 | {
|
---|
| 27 | my ($exifTool, $dirInfo) = @_;
|
---|
| 28 | my $raf = $$dirInfo{RAF};
|
---|
| 29 | my $outfile = $$dirInfo{OutFile};
|
---|
| 30 | my $verbose = $exifTool->Options('Verbose');
|
---|
| 31 | my $out = $exifTool->Options('TextOut');
|
---|
| 32 | my ($buff, $num, $type, %info);
|
---|
| 33 | #
|
---|
| 34 | # read as much of the image as necessary to extract the header and comments
|
---|
| 35 | #
|
---|
| 36 | for (;;) {
|
---|
| 37 | if (defined $buff) {
|
---|
| 38 | # need to read some more data
|
---|
| 39 | my $tmp;
|
---|
| 40 | return 0 unless $raf->Read($tmp, 1024);
|
---|
| 41 | $buff .= $tmp;
|
---|
| 42 | } else {
|
---|
| 43 | return 0 unless $raf->Read($buff, 1024);
|
---|
| 44 | }
|
---|
| 45 | # verify this is a valid PPM file
|
---|
| 46 | return 0 unless $buff =~ /^P([1-6])\s+/g;
|
---|
| 47 | $num = $1;
|
---|
| 48 | # note: may contain comments starting with '#'
|
---|
| 49 | if ($buff =~ /\G#/gc) {
|
---|
| 50 | # must read more if we are in the middle of a comment
|
---|
| 51 | next unless $buff =~ /\G ?(.*\n(#.*\n)*)\s*/g;
|
---|
| 52 | $info{Comment} = $1;
|
---|
| 53 | next if $buff =~ /\G#/gc;
|
---|
| 54 | } else {
|
---|
| 55 | delete $info{Comment};
|
---|
| 56 | }
|
---|
| 57 | next unless $buff =~ /\G(\S+)\s+(\S+)\s/g;
|
---|
| 58 | $info{ImageWidth} = $1;
|
---|
| 59 | $info{ImageHeight} = $2;
|
---|
| 60 | $type = [qw{PPM PBM PGM}]->[$num % 3];
|
---|
| 61 | last if $type eq 'PBM'; # (no MaxVal for PBM images)
|
---|
| 62 | if ($buff =~ /\G\s*#/gc) {
|
---|
| 63 | next unless $buff =~ /\G ?(.*\n(#.*\n)*)\s*/g;
|
---|
| 64 | $info{Comment} = '' unless exists $info{Comment};
|
---|
| 65 | $info{Comment} .= $1;
|
---|
| 66 | next if $buff =~ /\G#/gc;
|
---|
| 67 | }
|
---|
| 68 | next unless $buff =~ /\G(\S+)\s/g;
|
---|
| 69 | $info{MaxVal} = $1;
|
---|
| 70 | last;
|
---|
| 71 | }
|
---|
| 72 | # validate numerical values
|
---|
| 73 | foreach (keys %info) {
|
---|
| 74 | next if $_ eq 'Comment';
|
---|
| 75 | return 0 unless $info{$_} =~ /^\d+$/;
|
---|
| 76 | }
|
---|
| 77 | if (defined $info{Comment}) {
|
---|
| 78 | $info{Comment} =~ s/^# ?//mg; # remove "# " at the start of each line
|
---|
| 79 | $info{Comment} =~ s/\n$//; # remove trailing newline
|
---|
| 80 | }
|
---|
| 81 | $exifTool->SetFileType($type);
|
---|
| 82 | my $len = pos($buff);
|
---|
| 83 | #
|
---|
| 84 | # rewrite the file if requested
|
---|
| 85 | #
|
---|
| 86 | if ($outfile) {
|
---|
| 87 | my $newValueHash;
|
---|
| 88 | my $newComment = $exifTool->GetNewValues('Comment', \$newValueHash);
|
---|
| 89 | my $oldComment = $info{Comment};
|
---|
| 90 | if (Image::ExifTool::IsOverwriting($newValueHash, $oldComment)) {
|
---|
| 91 | ++$exifTool->{CHANGED};
|
---|
| 92 | if ($verbose > 1) {
|
---|
| 93 | print $out " - Comment = '$oldComment'\n" if defined $oldComment;
|
---|
| 94 | print $out " + Comment = '$newComment'\n" if defined $newComment;
|
---|
| 95 | }
|
---|
| 96 | } else {
|
---|
| 97 | $newComment = $oldComment; # use existing comment
|
---|
| 98 | }
|
---|
| 99 | my $hdr = "P$num\n";
|
---|
| 100 | if (defined $newComment) {
|
---|
| 101 | $newComment =~ s/\n/\n# /g;
|
---|
| 102 | $hdr .= "# $newComment\n";
|
---|
| 103 | }
|
---|
| 104 | $hdr .= "$info{ImageWidth} $info{ImageHeight}\n";
|
---|
| 105 | $hdr .= "$info{MaxVal}\n" if $type ne 'PBM';
|
---|
| 106 | # write header and start of image
|
---|
| 107 | Write($outfile, $hdr, substr($buff, $len)) or return -1;
|
---|
| 108 | # copy over the rest of the image
|
---|
| 109 | while ($raf->Read($buff, 0x10000)) {
|
---|
| 110 | Write($outfile, $buff) or return -1;
|
---|
| 111 | }
|
---|
| 112 | return 1;
|
---|
| 113 | }
|
---|
| 114 | #
|
---|
| 115 | # save extracted information
|
---|
| 116 | #
|
---|
| 117 | if ($verbose > 2) {
|
---|
| 118 | print $out "$type header ($len bytes):\n";
|
---|
| 119 | Image::ExifTool::HexDump(\$buff, $len, Out => $out);
|
---|
| 120 | }
|
---|
| 121 | my $tag;
|
---|
| 122 | foreach $tag (qw{Comment ImageWidth ImageHeight MaxVal}) {
|
---|
| 123 | $exifTool->FoundTag($tag, $info{$tag}) if defined $info{$tag};
|
---|
| 124 | }
|
---|
| 125 | return 1;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | 1; # end
|
---|
| 129 |
|
---|
| 130 | __END__
|
---|
| 131 |
|
---|
| 132 | =head1 NAME
|
---|
| 133 |
|
---|
| 134 | Image::ExifTool::PPM - Read and write PPM meta information
|
---|
| 135 |
|
---|
| 136 | =head1 SYNOPSIS
|
---|
| 137 |
|
---|
| 138 | This module is used by Image::ExifTool
|
---|
| 139 |
|
---|
| 140 | =head1 DESCRIPTION
|
---|
| 141 |
|
---|
| 142 | This module contains definitions required by Image::ExifTool to read and
|
---|
| 143 | write PPM (Portable Pixel Map), PGM (Portable Gray Map) and PBM (Portable
|
---|
| 144 | BitMap) images.
|
---|
| 145 |
|
---|
| 146 | =head1 AUTHOR
|
---|
| 147 |
|
---|
| 148 | Copyright 2003-2007, Phil Harvey (phil at owl.phy.queensu.ca)
|
---|
| 149 |
|
---|
| 150 | This library is free software; you can redistribute it and/or modify it
|
---|
| 151 | under the same terms as Perl itself.
|
---|
| 152 |
|
---|
| 153 | =head1 REFERENCES
|
---|
| 154 |
|
---|
| 155 | =over 4
|
---|
| 156 |
|
---|
| 157 | =item L<http://netpbm.sourceforge.net/doc/ppm.html>
|
---|
| 158 |
|
---|
| 159 | =item L<http://netpbm.sourceforge.net/doc/pgm.html>
|
---|
| 160 |
|
---|
| 161 | =item L<http://netpbm.sourceforge.net/doc/pbm.html>
|
---|
| 162 |
|
---|
| 163 | =back
|
---|
| 164 |
|
---|
| 165 | =head1 SEE ALSO
|
---|
| 166 |
|
---|
| 167 | L<Image::ExifTool::TagNames/PPM Tags>,
|
---|
| 168 | L<Image::ExifTool(3pm)|Image::ExifTool>
|
---|
| 169 |
|
---|
| 170 | =cut
|
---|
| 171 |
|
---|