source: main/trunk/greenstone2/perllib/cpan/Image/ExifTool/PPM.pm@ 34921

Last change on this file since 34921 was 34921, checked in by anupama, 3 years ago

Committing the improvements to EmbeddedMetaPlugin's processing of Keywords vs other metadata fields. Keywords were literally stored as arrays of words rather than phrases in PDFs (at least in Diego's sample PDF), whereas other meta fields like Subjects and Creators stored them as arrays of phrases. To get both to work, Kathy updated EXIF to a newer version, to retrieve the actual EXIF values stored in the PDF. And Kathy and Dr Bainbridge came up with a new option that I added called apply_join_before_split_to_metafields that's a regex which can list the metadata fields to apply the join_before_split to and whcih previously always got applied to all metadata fields. Now it's applied to any *Keywords metafields by default, as that's the metafield we have experience of that behaves differently to the others, as it stores by word instead of phrases. Tested on Diego's sample PDF. Diego has double-checked it to works on his sample PDF too, setting the split char to ; and turning on the join_before_split and leaving apply_join_before_split_to_metafields at its default of .*Keywords. File changes are strings.properties for the tooltip, the plugin introducing the option and working with it and Kathy's EXIF updates affecting cpan/File and cpan/Image.

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.10';
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 ($et, $dirInfo) = @_;
28 my $raf = $$dirInfo{RAF};
29 my $outfile = $$dirInfo{OutFile};
30 my $verbose = $et->Options('Verbose');
31 my $out = $et->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\r]+(#.*[\n\r]+)*)\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\r]+(#.*[\n\r]+)*)\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\r]+$//; # remove trailing newline
80 }
81 $et->SetFileType($type);
82 my $len = pos($buff);
83#
84# rewrite the file if requested
85#
86 if ($outfile) {
87 my $nvHash;
88 my $newComment = $et->GetNewValue('Comment', \$nvHash);
89 my $oldComment = $info{Comment};
90 if ($et->IsOverwriting($nvHash, $oldComment)) {
91 ++$$et{CHANGED};
92 $et->VerboseValue('- Comment', $oldComment) if defined $oldComment;
93 $et->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 $et->VerboseDump(\$buff, Len => $len);
118 }
119 my $tag;
120 foreach $tag (qw{Comment ImageWidth ImageHeight MaxVal}) {
121 $et->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-2021, Phil Harvey (philharvey66 at gmail.com)
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.