source: gs2-extensions/parallel-building/trunk/src/perllib/cpan/Image/ExifTool/PPM.pm@ 24626

Last change on this file since 24626 was 24626, checked in by jmt12, 13 years ago

An (almost) complete copy of the perllib directory from a (circa SEP2011) head checkout from Greenstone 2 trunk - in order to try and make merging in this extension a little easier later on (as there have been some major changes to buildcol.pl commited in the main trunk but not in the x64 branch)

File size: 5.0 KB
Line 
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
13package Image::ExifTool::PPM;
14
15use strict;
16use vars qw($VERSION);
17use Image::ExifTool qw(:DataAccess :Utils);
18
19$VERSION = '1.05';
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
25sub 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 $nvHash;
88 my $newComment = $exifTool->GetNewValues('Comment', \$nvHash);
89 my $oldComment = $info{Comment};
90 if (Image::ExifTool::IsOverwriting($nvHash, $oldComment)) {
91 ++$exifTool->{CHANGED};
92 $exifTool->VerboseValue('- Comment', $oldComment) if defined $oldComment;
93 $exifTool->VerboseValue('+ Comment', $newComment) if defined $newComment;
94 } else {
95 $newComment = $oldComment; # use existing comment
96 }
97 my $hdr = "P$num\n";
98 if (defined $newComment) {
99 $newComment =~ s/\n/\n# /g;
100 $hdr .= "# $newComment\n";
101 }
102 $hdr .= "$info{ImageWidth} $info{ImageHeight}\n";
103 $hdr .= "$info{MaxVal}\n" if $type ne 'PBM';
104 # write header and start of image
105 Write($outfile, $hdr, substr($buff, $len)) or return -1;
106 # copy over the rest of the image
107 while ($raf->Read($buff, 0x10000)) {
108 Write($outfile, $buff) or return -1;
109 }
110 return 1;
111 }
112#
113# save extracted information
114#
115 if ($verbose > 2) {
116 print $out "$type header ($len bytes):\n";
117 Image::ExifTool::HexDump(\$buff, $len, Out => $out);
118 }
119 my $tag;
120 foreach $tag (qw{Comment ImageWidth ImageHeight MaxVal}) {
121 $exifTool->FoundTag($tag, $info{$tag}) if defined $info{$tag};
122 }
123 return 1;
124}
125
1261; # end
127
128__END__
129
130=head1 NAME
131
132Image::ExifTool::PPM - Read and write PPM meta information
133
134=head1 SYNOPSIS
135
136This module is used by Image::ExifTool
137
138=head1 DESCRIPTION
139
140This module contains definitions required by Image::ExifTool to read and
141write PPM (Portable Pixel Map), PGM (Portable Gray Map) and PBM (Portable
142BitMap) images.
143
144=head1 AUTHOR
145
146Copyright 2003-2011, Phil Harvey (phil at owl.phy.queensu.ca)
147
148This library is free software; you can redistribute it and/or modify it
149under the same terms as Perl itself.
150
151=head1 REFERENCES
152
153=over 4
154
155=item L<http://netpbm.sourceforge.net/doc/ppm.html>
156
157=item L<http://netpbm.sourceforge.net/doc/pgm.html>
158
159=item L<http://netpbm.sourceforge.net/doc/pbm.html>
160
161=back
162
163=head1 SEE ALSO
164
165L<Image::ExifTool::TagNames/PPM Tags>,
166L<Image::ExifTool(3pm)|Image::ExifTool>
167
168=cut
169
Note: See TracBrowser for help on using the repository browser.