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

Last change on this file since 5845 was 5845, checked in by mdewsnip, 20 years ago

David's fixes for running ImagePlug under Windows.

  • Property svn:keywords set to Author Date Id Revision
File size: 14.2 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' => "string",
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 'inherits' => "yes",
78 'args' => $arguments };
79
80
81# sub print_usage {
82# my ($plugin_name) = @_;
83
84# print STDERR "
85# usage: plugin ImagePlug [options]
86
87# -noscaleup Don't scale up small images when making thumbnails
88
89# -thumbnailsize n Make thumbnails of size nxn
90
91# -thumbnailtype s Make thumbnails in format 's'
92
93# -screenviewsize n If set, makes an image of size n for screen display
94# and sets Screen, ScreenSize, ScrrenWidth and Screeneight
95# metadata. By default it is not set.
96
97# -screenviewtype s If -screenviewsize is set, this sets the screen display
98# image type. Defaults to jpg.
99
100# -convertto s Convert main inage to (gif|png|jpg)
101
102# -minimumsize n Ignore images smaller than n bytes
103
104# "
105# }
106
107sub new {
108 my ($class) = @_;
109 my $plugin_name = shift (@_);
110 my $self = new BasPlug ("ImagePlug", @_);
111
112 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
113 my $option_list = $self->{'option_list'};
114 push( @{$option_list}, $options );
115
116 if (!parsargv::parse(\@_,
117 q^noscaleup^, \$self->{'noscaleup'},
118 q^converttotype/.*/^, \$self->{'converttotype'},
119 q^minimumsize/[0-9]*/100^, \$self->{'minimumsize'},
120
121 q^thumbnailsize/[0-9]*/100^, \$self->{'thumbnailsize'},
122 q^thumbnailtype/.*/gif^, \$self->{'thumbnailtype'},
123 q^screenviewsize/[0-9]*/0^, \$self->{'screenviewsize'},
124 q^screenviewtype/.*/jpg^, \$self->{'screenviewtype'},
125 "allow_extra_options")) {
126
127 print STDERR "\nImagePlug uses an incorrect option.\n";
128 print STDERR "Check your collect.cfg configuration file.\n";
129 $self->print_txt_usage(""); # Use default resource bundle
130 die "\n";
131 }
132
133 return bless $self, $class;
134}
135
136sub get_default_process_exp {
137 my $self = shift (@_);
138
139 return q^(?i)(\.jpe?g|\.gif|\.png|\.bmp|\.xbm|\.tif?f)$^;
140}
141
142
143# Create the thumbnail and screenview images, and discover the Image's
144# size, width, and height using the convert utility.
145
146sub run_convert {
147 my $self = shift (@_);
148 my $filename = shift (@_); # filename with full path
149 my $file = shift (@_); # filename without path
150 my $doc_obj = shift (@_);
151 my $section = $doc_obj->get_top_section();
152
153 my $verbosity = $self->{'verbosity'};
154 my $outhandle = $self->{'outhandle'};
155
156 # check the filename is okay
157 return 0 if ($file eq "" || $filename eq "");
158
159# Code now extended to quote filenames in 'convert' commnads
160# Allows spaces in filenames, but note needs spaces to be escaped in URL as well
161# if ($filename =~ m/ /) {
162# print $outhandle "ImagePlug: \"$filename\" contains a space. choking.\n";
163# return undef;
164# }
165
166 my $minimumsize = $self->{'minimumsize'};
167 if (defined $minimumsize && (-s $filename < $minimumsize)) {
168 print $outhandle "ImagePlug: \"$filename\" too small, skipping\n"
169 if ($verbosity > 1);
170 }
171
172
173 # Convert the image to a new type (if required).
174 my $converttotype = $self->{'converttotype'};
175 my $originalfilename = ""; # only set if we do a conversion
176 my $type = "unknown";
177
178 if ($converttotype ne "" && $filename =~ m/$converttotype$/) {
179
180 $originalfilename = $filename;
181 $filename = &util::get_tmp_filename() . ".$converttotype";
182 $self->{'tmp_filename'} = $filename;
183
184 my $command = "convert -interlace plane -verbose \"$originalfilename\" \"$filename\"";
185 print $outhandle "$command\n" if ($verbosity > 2);
186 my $result = '';
187 $result = `$command`;
188 print $outhandle "RESULT = $result\n" if ($verbosity > 2);
189
190 $type = $converttotype;
191 }
192
193
194 # Add the image metadata
195 my $url = $file;
196 $url =~ s/ /%20/g;
197
198 $doc_obj->add_metadata ($section, "Image", $url);
199
200 # Also want to set filename as 'Source' metadata to be
201 # consistent with other plugins
202 $doc_obj->add_metadata ($section, "Source", $url);
203
204 my ($image_type, $image_width, $image_height, $image_size)
205 = &identify($filename, $outhandle, $verbosity);
206
207 $doc_obj->add_metadata ($section, "ImageType", $image_type);
208 $doc_obj->add_metadata ($section, "ImageWidth", $image_width);
209 $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
210 $doc_obj->add_metadata ($section, "ImageSize", $image_size);
211
212
213 $doc_obj->add_metadata ($section, "srclink",
214 "<a href=_httpcollection_/index/assoc/[assocfilepath]/[Image]>");
215 $doc_obj->add_metadata ($section, "/srclink", "</a>");
216
217 $doc_obj->add_metadata ($section, "srcicon", "<img src=_httpcollection_/index/assoc/[assocfilepath]/[Image] width=100>");
218
219
220 # Add the image as an associated file
221 $doc_obj->associate_file($filename,$file,"image/$type",$section);
222
223
224 # Make the thumbnail image
225 my $thumbnailsize = $self->{'thumbnailsize'} || 100;
226 my $thumbnailtype = $self->{'thumbnailtype'} || 'gif';
227
228 my $thumbnailfile = &util::get_tmp_filename() . ".$thumbnailtype";
229 $self->{'tmp_filename2'} = $thumbnailfile;
230
231 # Generate the thumbnail with convert
232 my $command = "convert -interlace plane -verbose -geometry $thumbnailsize"
233 . "x$thumbnailsize \"$filename\" \"$thumbnailfile\"";
234 print $outhandle "THUMBNAIL: $command\n" if ($verbosity > 2);
235 my $result = '';
236 $result = `$command 2>&1` ;
237 print $outhandle "THUMB RESULT: $result\n" if ($verbosity > 2);
238
239 # Add the thumbnail as an associated file ...
240 if (-e "$thumbnailfile") {
241 $doc_obj->associate_file("$thumbnailfile", "thumbnail.$thumbnailtype",
242 "image/$thumbnailtype",$section);
243 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
244 $doc_obj->add_metadata ($section, "Thumb", "thumbnail.$thumbnailtype");
245
246 $doc_obj->add_metadata ($section, "thumbicon", "<img src=_httpcollection_/index/assoc/[assocfilepath]/[Thumb] width=[ThumbWidth] height=[ThumbHeight]>");
247 }
248
249 # Extract Thumnail metadata from convert output
250 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
251 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
252 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
253 }
254
255 # Make a screen-sized version of the picture if requested
256 if ($self->{'screenviewsize'}) {
257
258 # To do: if the actual image smaller than the screenview size,
259 # we should use the original !
260
261 my $screenviewsize = $self->{'screenviewsize'};
262 my $screenviewtype = $self->{'screenviewtype'} || 'jpeg';
263 my $screenviewfilename = &util::get_tmp_filename() . ".$screenviewtype";
264 $self->{'tmp_filename3'} = $screenviewfilename;
265
266 # make the screenview image
267 my $command = "convert -interlace plane -verbose -geometry $screenviewsize"
268 . "x$screenviewsize \"$filename\" \"$screenviewfilename\"";
269 print $outhandle "$command\n" if ($verbosity > 2);
270 my $result = "";
271 $result = `$command 2>&1` ;
272 print $outhandle "$result\n" if ($verbosity > 3);
273
274 # get screenview dimensions, size and type
275 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
276 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
277 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
278 }
279 else {
280 $doc_obj->add_metadata ($section, "ScreenWidth", $image_width);
281 $doc_obj->add_metadata ($section, "ScreenHeight", $image_height);
282 }
283
284 #add the screenview as an associated file ...
285 if (-e "$screenviewfilename") {
286 $doc_obj->associate_file("$screenviewfilename", "screenview.$screenviewtype",
287 "image/$screenviewtype",$section);
288 $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
289 $doc_obj->add_metadata ($section, "Screen", "screenview.$screenviewtype");
290
291 $doc_obj->add_metadata ($section, "screenicon", "<img src=_httpcollection_/index/assoc/[assocfilepath]/[Screen] width=[ScreenWidth] height=[ScreenHeight]>");
292 } else {
293 print $outhandle "ImagePlug: couldn't find \"$screenviewfilename\"\n";
294 }
295 }
296
297 return $type;
298
299
300}
301
302
303
304# Discover the characteristics of an image file with the ImageMagick
305# "identify" command.
306
307sub identify {
308 my ($image, $outhandle, $verbosity) = @_;
309
310 # Use the ImageMagick "identify" command to get the file specs
311 my $command = "identify \"$image\" 2>&1";
312 print $outhandle "$command\n" if ($verbosity > 2);
313 my $result = '';
314 $result = `$command`;
315 print $outhandle "$result\n" if ($verbosity > 3);
316
317 # Read the type, width, and height
318 my $type = 'unknown';
319 my $width = 'unknown';
320 my $height = 'unknown';
321
322 my $image_safe = quotemeta $image;
323 if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
324 $type = $1;
325 $width = $2;
326 $height = $3;
327 }
328
329 # Read the size
330 my $size = "unknown";
331 if ($result =~ m/^.* ([0-9]+)b/) {
332 $size = $1;
333 }
334 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?kb?/) {
335 $size = 1024 * $1;
336 if (defined($2)) {
337 $size = $size + (1024 * $2);
338 # Truncate size (it isn't going to be very accurate anyway)
339 $size = int($size);
340 }
341 }
342
343 print $outhandle "file: $image:\t $type, $width, $height, $size\n"
344 if ($verbosity > 2);
345
346 # Return the specs
347 return ($type, $width, $height, $size);
348}
349
350
351# The ImagePlug read() function. This function does all the right things
352# to make general options work for a given plugin. It calls the process()
353# function which does all the work specific to a plugin (like the old
354# read functions used to do). Most plugins should define their own
355# process() function and let this read() function keep control.
356#
357# ImagePlug overrides read() because there is no need to read the actual
358# text of the file in, because the contents of the file is not text...
359#
360# Return number of files processed, undef if can't process
361# Note that $base_dir might be "" and that $file might
362# include directories
363
364sub read {
365 my $self = shift (@_);
366 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
367
368 my $outhandle = $self->{'outhandle'};
369
370 my $filename = &util::filename_cat($base_dir, $file);
371 return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
372 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
373 return undef;
374 }
375 print $outhandle "ImagePlug processing \"$filename\"\n"
376 if $self->{'verbosity'} > 1;
377
378 #if there's a leading directory name, eat it...
379 $file =~ s/^.*[\/\\]//;
380
381 # create a new document
382 my $doc_obj = new doc ($filename, "indexed_doc");
383 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
384
385 #run convert to get the thumbnail and extract size and type info
386 my $result = run_convert($self, $filename, $file, $doc_obj);
387
388 if (!defined $result)
389 {
390 print "ImagePlug: couldn't process \"$filename\"\n";
391 return 0;
392 }
393
394 #create an empty text string so we don't break downstream plugins
395 my $text = "Dummy text to sidestep display bug.";
396
397 # include any metadata passed in from previous plugins
398 # note that this metadata is associated with the top level section
399 my $section = $doc_obj->get_top_section();
400 $self->extra_metadata ($doc_obj, $section, $metadata);
401
402 # do plugin specific processing of doc_obj
403 return undef unless defined ($self->process (\$text, $pluginfo, $base_dir,
404 $file, $metadata, $doc_obj));
405
406 # do any automatic metadata extraction
407 $self->auto_extract_metadata ($doc_obj);
408
409 # add an OID
410 $doc_obj->set_OID();
411 $doc_obj->add_text($section, $text);
412
413 # process the document
414 $processor->process($doc_obj);
415
416 # clean up temporary files - we do this here instead of in
417 # run_convert becuase associated files aren't actually copied
418 # until after process has been run.
419 if (defined $self->{'tmp_filename'} &&
420 -e $self->{'tmp_filename'}) {
421 &util::rm($self->{'tmp_filename'})
422 }
423 if (defined $self->{'tmp_filename2'} &&
424 -e $self->{'tmp_filename2'}) {
425 &util::rm($self->{'tmp_filename2'})
426 }
427 if (defined $self->{'tmp_filename3'} &&
428 -e $self->{'tmp_filename3'}) {
429 &util::rm($self->{'tmp_filename3'})
430 }
431
432 $self->{'num_processed'}++;
433
434 return 1;
435}
436
437# do plugin specific processing of doc_obj
438sub process {
439 my $self = shift (@_);
440 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
441 my $outhandle = $self->{'outhandle'};
442
443 return 1;
444}
445
4461;
447
448
449
450
451
452
453
454
455
456
457
Note: See TracBrowser for help on using the repository browser.