source: trunk/gsdl/perllib/plugins/ImagePlug.pm@ 7497

Last change on this file since 7497 was 7362, checked in by kjdon, 20 years ago

plugin read functions now return 'undef' - didn't recognise, '-1' - recognised but had an error during processing, '0' - blocked or didn't process but don't pass it on to other plugins, or 'num_docs' processed. this emables a bit better error reporting about what has happened to each file

  • Property svn:keywords set to Author Date Id Revision
File size: 13.7 KB
Line 
1###########################################################################
2#
3# ImagePlug.pm -- simple text plugin
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package ImagePlug;
27
28use BasPlug;
29
30sub BEGIN {
31 @ISA = ('BasPlug');
32}
33
34my $arguments =
35 [ { 'name' => "process_exp",
36 'desc' => "{BasPlug.process_exp}",
37 'type' => "regexp",
38 'deft' => &get_default_process_exp(),
39 'reqd' => "no" },
40 { 'name' => "noscaleup",
41 'desc' => "{ImagePlug.noscaleup}",
42 'type' => "flag",
43 'reqd' => "no" },
44 { 'name' => "thumbnailsize",
45 'desc' => "{ImagePlug.thumbnailsize}",
46 'type' => "int",
47 'deft' => "100",
48 'reqd' => "no" },
49 { 'name' => "thumbnailtype",
50 'desc' => "{ImagePlug.thumbnailtype}",
51 'type' => "string",
52 'deft' => "gif",
53 'reqd' => "no" },
54 { 'name' => "screenviewsize",
55 'desc' => "{ImagePlug.screenviewsize}",
56 'type' => "int",
57 'deft' => "0",
58 'reqd' => "no" },
59 { 'name' => "screenviewtype",
60 'desc' => "{ImagePlug.screenviewtype}",
61 'type' => "string",
62 'deft' => "jpg",
63 'reqd' => "no" },
64 { 'name' => "converttotype",
65 'desc' => "{ImagePlug.converttotype}",
66 'type' => "string",
67 'deft' => "",
68 'reqd' => "no" },
69 { 'name' => "minimumsize",
70 'desc' => "{ImagePlug.minimumsize}",
71 'type' => "int",
72 'deft' => "100",
73 'reqd' => "no" } ];
74
75my $options = { 'name' => "ImagePlug",
76 'desc' => "{ImagePlug.desc}",
77 'abstract' => "no",
78 'inherits' => "yes",
79 'args' => $arguments };
80
81
82
83sub new {
84 my ($class) = @_;
85 my $plugin_name = shift (@_);
86 my $self = new BasPlug ("ImagePlug", @_);
87 $self->{'plugin_type'} = "ImagePlug";
88 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
89 my $option_list = $self->{'option_list'};
90 push( @{$option_list}, $options );
91
92 if (!parsargv::parse(\@_,
93 q^noscaleup^, \$self->{'noscaleup'},
94 q^converttotype/.*/^, \$self->{'converttotype'},
95 q^minimumsize/[0-9]*/100^, \$self->{'minimumsize'},
96
97 q^thumbnailsize/[0-9]*/100^, \$self->{'thumbnailsize'},
98 q^thumbnailtype/.*/gif^, \$self->{'thumbnailtype'},
99 q^screenviewsize/[0-9]*/0^, \$self->{'screenviewsize'},
100 q^screenviewtype/.*/jpg^, \$self->{'screenviewtype'},
101 "allow_extra_options")) {
102
103 print STDERR "\nImagePlug uses an incorrect option.\n";
104 print STDERR "Check your collect.cfg configuration file.\n";
105 $self->print_txt_usage(""); # Use default resource bundle
106 die "\n";
107 }
108
109 return bless $self, $class;
110}
111
112sub get_default_process_exp {
113 my $self = shift (@_);
114
115 return q^(?i)(\.jpe?g|\.gif|\.png|\.bmp|\.xbm|\.tif?f)$^;
116}
117
118
119# Create the thumbnail and screenview images, and discover the Image's
120# size, width, and height using the convert utility.
121
122sub run_convert {
123 my $self = shift (@_);
124 my $filename = shift (@_); # filename with full path
125 my $file = shift (@_); # filename without path
126 my $doc_obj = shift (@_);
127 my $section = $doc_obj->get_top_section();
128
129 my $verbosity = $self->{'verbosity'};
130 my $outhandle = $self->{'outhandle'};
131
132 # check the filename is okay
133 return 0 if ($file eq "" || $filename eq "");
134
135# Code now extended to quote filenames in 'convert' commnads
136# Allows spaces in filenames, but note needs spaces to be escaped in URL as well
137# if ($filename =~ m/ /) {
138# print $outhandle "ImagePlug: \"$filename\" contains a space. choking.\n";
139# return undef;
140# }
141
142 my $minimumsize = $self->{'minimumsize'};
143 if (defined $minimumsize && (-s $filename < $minimumsize)) {
144 print $outhandle "ImagePlug: \"$filename\" too small, skipping\n"
145 if ($verbosity > 1);
146 }
147
148
149 # Convert the image to a new type (if required).
150 my $converttotype = $self->{'converttotype'};
151 my $originalfilename = ""; # only set if we do a conversion
152 my $type = "unknown";
153
154 if ($converttotype ne "" && $filename =~ m/$converttotype$/) {
155
156 $originalfilename = $filename;
157 $filename = &util::get_tmp_filename() . ".$converttotype";
158 $self->{'tmp_filename'} = $filename;
159
160 my $command = "convert -interlace plane -verbose \"$originalfilename\" \"$filename\"";
161 print $outhandle "$command\n" if ($verbosity > 2);
162 my $result = '';
163 $result = `$command`;
164 print $outhandle "RESULT = $result\n" if ($verbosity > 2);
165
166 $type = $converttotype;
167 }
168
169
170 # Add the image metadata
171 my $url = $file;
172 $url =~ s/ /%20/g;
173
174 $doc_obj->add_metadata ($section, "Image", $url);
175
176 # Also want to set filename as 'Source' metadata to be
177 # consistent with other plugins
178 $doc_obj->add_metadata ($section, "Source", $url);
179
180 my ($image_type, $image_width, $image_height, $image_size)
181 = &identify($filename, $outhandle, $verbosity);
182
183 $doc_obj->add_metadata ($section, "ImageType", $image_type);
184 $doc_obj->add_metadata ($section, "ImageWidth", $image_width);
185 $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
186 $doc_obj->add_metadata ($section, "ImageSize", $image_size);
187
188
189 $doc_obj->add_metadata ($section, "srclink",
190 "<a href=_httpcollection_/index/assoc/[assocfilepath]/[Image]>");
191 $doc_obj->add_metadata ($section, "/srclink", "</a>");
192
193 $doc_obj->add_metadata ($section, "srcicon", "<img src=_httpcollection_/index/assoc/[assocfilepath]/[Image] width=100>");
194
195
196 # Add the image as an associated file
197 $doc_obj->associate_file($filename,$file,"image/$type",$section);
198
199
200 # Make the thumbnail image
201 my $thumbnailsize = $self->{'thumbnailsize'} || 100;
202 my $thumbnailtype = $self->{'thumbnailtype'} || 'gif';
203
204 my $thumbnailfile = &util::get_tmp_filename() . ".$thumbnailtype";
205 $self->{'tmp_filename2'} = $thumbnailfile;
206
207 # Generate the thumbnail with convert
208 my $command = "convert -interlace plane -verbose -geometry $thumbnailsize"
209 . "x$thumbnailsize \"$filename\" \"$thumbnailfile\"";
210 print $outhandle "THUMBNAIL: $command\n" if ($verbosity > 2);
211 my $result = '';
212 $result = `$command 2>&1` ;
213 print $outhandle "THUMB RESULT: $result\n" if ($verbosity > 2);
214
215 # Add the thumbnail as an associated file ...
216 if (-e "$thumbnailfile") {
217 $doc_obj->associate_file("$thumbnailfile", "thumbnail.$thumbnailtype",
218 "image/$thumbnailtype",$section);
219 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
220 $doc_obj->add_metadata ($section, "Thumb", "thumbnail.$thumbnailtype");
221
222 $doc_obj->add_metadata ($section, "thumbicon", "<img src=_httpcollection_/index/assoc/[assocfilepath]/[Thumb] width=[ThumbWidth] height=[ThumbHeight]>");
223 }
224
225 # Extract Thumnail metadata from convert output
226 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
227 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
228 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
229 }
230
231 # Make a screen-sized version of the picture if requested
232 if ($self->{'screenviewsize'}) {
233
234 # To do: if the actual image smaller than the screenview size,
235 # we should use the original !
236
237 my $screenviewsize = $self->{'screenviewsize'};
238 my $screenviewtype = $self->{'screenviewtype'} || 'jpeg';
239 my $screenviewfilename = &util::get_tmp_filename() . ".$screenviewtype";
240 $self->{'tmp_filename3'} = $screenviewfilename;
241
242 # make the screenview image
243 my $command = "convert -interlace plane -verbose -geometry $screenviewsize"
244 . "x$screenviewsize \"$filename\" \"$screenviewfilename\"";
245 print $outhandle "$command\n" if ($verbosity > 2);
246 my $result = "";
247 $result = `$command 2>&1` ;
248 print $outhandle "$result\n" if ($verbosity > 3);
249
250 # get screenview dimensions, size and type
251 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
252 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
253 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
254 }
255 else {
256 $doc_obj->add_metadata ($section, "ScreenWidth", $image_width);
257 $doc_obj->add_metadata ($section, "ScreenHeight", $image_height);
258 }
259
260 #add the screenview as an associated file ...
261 if (-e "$screenviewfilename") {
262 $doc_obj->associate_file("$screenviewfilename", "screenview.$screenviewtype",
263 "image/$screenviewtype",$section);
264 $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
265 $doc_obj->add_metadata ($section, "Screen", "screenview.$screenviewtype");
266
267 $doc_obj->add_metadata ($section, "screenicon", "<img src=_httpcollection_/index/assoc/[assocfilepath]/[Screen] width=[ScreenWidth] height=[ScreenHeight]>");
268 } else {
269 print $outhandle "ImagePlug: couldn't find \"$screenviewfilename\"\n";
270 }
271 }
272
273 return $type;
274
275
276}
277
278
279
280# Discover the characteristics of an image file with the ImageMagick
281# "identify" command.
282
283sub identify {
284 my ($image, $outhandle, $verbosity) = @_;
285
286 # Use the ImageMagick "identify" command to get the file specs
287 my $command = "identify \"$image\" 2>&1";
288 print $outhandle "$command\n" if ($verbosity > 2);
289 my $result = '';
290 $result = `$command`;
291 print $outhandle "$result\n" if ($verbosity > 3);
292
293 # Read the type, width, and height
294 my $type = 'unknown';
295 my $width = 'unknown';
296 my $height = 'unknown';
297
298 my $image_safe = quotemeta $image;
299 if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
300 $type = $1;
301 $width = $2;
302 $height = $3;
303 }
304
305 # Read the size
306 my $size = "unknown";
307 if ($result =~ m/^.* ([0-9]+)b/) {
308 $size = $1;
309 }
310 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?kb?/) {
311 $size = 1024 * $1;
312 if (defined($2)) {
313 $size = $size + (1024 * $2);
314 # Truncate size (it isn't going to be very accurate anyway)
315 $size = int($size);
316 }
317 }
318
319 print $outhandle "file: $image:\t $type, $width, $height, $size\n"
320 if ($verbosity > 2);
321
322 # Return the specs
323 return ($type, $width, $height, $size);
324}
325
326
327# The ImagePlug read() function. This function does all the right things
328# to make general options work for a given plugin. It calls the process()
329# function which does all the work specific to a plugin (like the old
330# read functions used to do). Most plugins should define their own
331# process() function and let this read() function keep control.
332#
333# ImagePlug overrides read() because there is no need to read the actual
334# text of the file in, because the contents of the file is not text...
335#
336# Return number of files processed, undef if can't process
337# Note that $base_dir might be "" and that $file might
338# include directories
339
340sub read {
341 my $self = shift (@_);
342 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $gli) = @_;
343
344 my $outhandle = $self->{'outhandle'};
345
346 my $filename = &util::filename_cat($base_dir, $file);
347 return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
348 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
349 return undef;
350 }
351
352 print STDERR "<Processing n='$file' p='ImagePlug'>\n" if ($gli);
353 print $outhandle "ImagePlug processing \"$filename\"\n"
354 if $self->{'verbosity'} > 1;
355
356 #if there's a leading directory name, eat it...
357 $file =~ s/^.*[\/\\]//;
358
359 # create a new document
360 my $doc_obj = new doc ($filename, "indexed_doc");
361 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
362 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "$self->{'plugin_type'}", "1");
363
364 #run convert to get the thumbnail and extract size and type info
365 my $result = run_convert($self, $filename, $file, $doc_obj);
366
367 if (!defined $result)
368 {
369 print "ImagePlug: couldn't process \"$filename\"\n";
370 return -1; # error during processing
371 }
372
373 #create an empty text string so we don't break downstream plugins
374 my $text = "Dummy text to sidestep display bug.";
375
376 # include any metadata passed in from previous plugins
377 # note that this metadata is associated with the top level section
378 my $section = $doc_obj->get_top_section();
379 $self->extra_metadata ($doc_obj, $section, $metadata);
380
381 # do plugin specific processing of doc_obj
382 return -1 unless defined ($self->process (\$text, $pluginfo, $base_dir,
383 $file, $metadata, $doc_obj));
384
385 # do any automatic metadata extraction
386 $self->auto_extract_metadata ($doc_obj);
387
388 # add an OID
389 $doc_obj->set_OID();
390 $doc_obj->add_text($section, $text);
391
392 # process the document
393 $processor->process($doc_obj);
394
395 # clean up temporary files - we do this here instead of in
396 # run_convert becuase associated files aren't actually copied
397 # until after process has been run.
398 if (defined $self->{'tmp_filename'} &&
399 -e $self->{'tmp_filename'}) {
400 &util::rm($self->{'tmp_filename'})
401 }
402 if (defined $self->{'tmp_filename2'} &&
403 -e $self->{'tmp_filename2'}) {
404 &util::rm($self->{'tmp_filename2'})
405 }
406 if (defined $self->{'tmp_filename3'} &&
407 -e $self->{'tmp_filename3'}) {
408 &util::rm($self->{'tmp_filename3'})
409 }
410
411 $self->{'num_processed'}++;
412
413 return 1;
414}
415
416# do plugin specific processing of doc_obj
417sub process {
418 my $self = shift (@_);
419 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
420 my $outhandle = $self->{'outhandle'};
421
422 return 1;
423}
424
4251;
426
427
428
429
430
431
432
433
434
435
436
Note: See TracBrowser for help on using the repository browser.