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

Last change on this file since 14953 was 14953, checked in by mdewsnip, 16 years ago

Changed convert() function to take a target file type instead of a target file path, in preparation for caching images.

  • Property svn:keywords set to Author Date Id Revision
File size: 14.7 KB
RevLine 
[1733]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
[10254]30use strict;
31no strict 'refs'; # allow filehandles to be variables and viceversa
32
[1733]33sub BEGIN {
[10254]34 @ImagePlug::ISA = ('BasPlug');
[1733]35}
36
[4744]37my $arguments =
38 [ { 'name' => "process_exp",
[4873]39 'desc' => "{BasPlug.process_exp}",
[6408]40 'type' => "regexp",
[4744]41 'deft' => &get_default_process_exp(),
42 'reqd' => "no" },
43 { 'name' => "noscaleup",
[4873]44 'desc' => "{ImagePlug.noscaleup}",
[4744]45 'type' => "flag",
46 'reqd' => "no" },
47 { 'name' => "thumbnailsize",
[4873]48 'desc' => "{ImagePlug.thumbnailsize}",
[4744]49 'type' => "int",
50 'deft' => "100",
[10218]51 'range' => "1,",
[4744]52 'reqd' => "no" },
53 { 'name' => "thumbnailtype",
[4873]54 'desc' => "{ImagePlug.thumbnailtype}",
[4744]55 'type' => "string",
56 'deft' => "gif",
57 'reqd' => "no" },
58 { 'name' => "screenviewsize",
[4873]59 'desc' => "{ImagePlug.screenviewsize}",
[4744]60 'type' => "int",
61 'deft' => "0",
[10218]62 'range' => "1,",
[4744]63 'reqd' => "no" },
64 { 'name' => "screenviewtype",
[4873]65 'desc' => "{ImagePlug.screenviewtype}",
[4744]66 'type' => "string",
67 'deft' => "jpg",
68 'reqd' => "no" },
69 { 'name' => "converttotype",
[4873]70 'desc' => "{ImagePlug.converttotype}",
[4744]71 'type' => "string",
72 'deft' => "",
73 'reqd' => "no" },
74 { 'name' => "minimumsize",
[4873]75 'desc' => "{ImagePlug.minimumsize}",
[4744]76 'type' => "int",
77 'deft' => "100",
[10218]78 'range' => "1,",
[4744]79 'reqd' => "no" } ];
[1758]80
[3540]81my $options = { 'name' => "ImagePlug",
[5680]82 'desc' => "{ImagePlug.desc}",
[6408]83 'abstract' => "no",
[3540]84 'inherits' => "yes",
85 'args' => $arguments };
86
87
[1744]88
[1733]89sub new {
[10218]90 my ($class) = shift (@_);
91 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
92 push(@$pluginlist, $class);
[4724]93
[10218]94 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
95 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
[2230]96
[12169]97 my $self = new BasPlug($pluginlist, $inputargs, $hashArgOptLists);
[14952]98 $self->{'tmp_file_paths'} = ();
[1744]99
[9574]100 # Check that ImageMagick is installed and available on the path (except for Windows 95/98)
101 if (!($ENV{'GSDLOS'} eq "windows" && !Win32::IsWinNT())) {
102 my $result = `identify 2>&1`;
103 if ($? == -1 || $? == 256) { # Linux and Windows return different values for "program not found"
104 $self->{'imagemagick_not_installed'} = 1;
105 }
[8145]106 }
107
[13243]108
[1733]109 return bless $self, $class;
110}
111
112sub get_default_process_exp {
113 my $self = shift (@_);
114
[1758]115 return q^(?i)(\.jpe?g|\.gif|\.png|\.bmp|\.xbm|\.tif?f)$^;
[1733]116}
117
[9067]118# this makes no sense for images
119sub block_cover_image
120{
121 my $self =shift (@_);
122 my ($filename) = @_;
[2230]123
[9067]124 return;
125}
[2230]126# Create the thumbnail and screenview images, and discover the Image's
127# size, width, and height using the convert utility.
128
[14950]129sub generate_images
130{
[1733]131 my $self = shift (@_);
[2230]132 my $filename = shift (@_); # filename with full path
133 my $file = shift (@_); # filename without path
[1733]134 my $doc_obj = shift (@_);
[1744]135 my $section = $doc_obj->get_top_section();
[2230]136
137 my $verbosity = $self->{'verbosity'};
138 my $outhandle = $self->{'outhandle'};
[1733]139
[2230]140 # check the filename is okay
141 return 0 if ($file eq "" || $filename eq "");
142
[3307]143# Code now extended to quote filenames in 'convert' commnads
144# Allows spaces in filenames, but note needs spaces to be escaped in URL as well
145# if ($filename =~ m/ /) {
146# print $outhandle "ImagePlug: \"$filename\" contains a space. choking.\n";
147# return undef;
148# }
[1744]149
[1758]150 my $minimumsize = $self->{'minimumsize'};
[2230]151 if (defined $minimumsize && (-s $filename < $minimumsize)) {
152 print $outhandle "ImagePlug: \"$filename\" too small, skipping\n"
153 if ($verbosity > 1);
154 }
155
[4724]156
[2230]157 # Convert the image to a new type (if required).
[1744]158 my $converttotype = $self->{'converttotype'};
[2230]159 my $originalfilename = ""; # only set if we do a conversion
[1733]160 my $type = "unknown";
[1758]161
[13879]162 if ($converttotype ne "" && $filename !~ m/$converttotype$/) {
[1744]163 $originalfilename = $filename;
[2230]164
[14953]165 my $result = $self->convert($originalfilename, $converttotype, "", "");
166 ($filename) = ($result =~ /=>(.*\.$converttotype)/);
[2230]167
[1744]168 $type = $converttotype;
[13879]169 $file =~ s/\..*$/\.$type/;
[1733]170 }
[1744]171
[4724]172
[3137]173 # Add the image metadata
[3307]174 my $url = $file;
[13542]175
176 ##not know why it is required at the first place, it seems all works fine without it, so I comment it out
177 ##$url =~ s/ /%20/g;
[3307]178
179 $doc_obj->add_metadata ($section, "Image", $url);
[3517]180
181 # Also want to set filename as 'Source' metadata to be
182 # consistent with other plugins
183 $doc_obj->add_metadata ($section, "Source", $url);
184
[3137]185 my ($image_type, $image_width, $image_height, $image_size)
186 = &identify($filename, $outhandle, $verbosity);
[2230]187
[8121]188 if ($image_type ne " ") {
189 $type = $image_type;
190 }
191
192 $doc_obj->add_metadata ($section, "FileFormat", $type);
[8166]193 $doc_obj->add_metadata ($section, "FileSize", $image_size);
[8121]194
[3137]195 $doc_obj->add_metadata ($section, "ImageType", $image_type);
196 $doc_obj->add_metadata ($section, "ImageWidth", $image_width);
197 $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
198 $doc_obj->add_metadata ($section, "ImageSize", $image_size);
[14117]199 $doc_obj->add_metadata ($section, "NoText", "1");
[3137]200
[4724]201 $doc_obj->add_metadata ($section, "srclink",
[11834]202 "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[Image]\">");
[4724]203 $doc_obj->add_metadata ($section, "/srclink", "</a>");
204
[11834]205 $doc_obj->add_metadata ($section, "srcicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[Image]\" width=100>");
[4724]206
207
[3137]208 # Add the image as an associated file
209 $doc_obj->associate_file($filename,$file,"image/$type",$section);
210
[4724]211
[2230]212 # Make the thumbnail image
[3137]213 my $thumbnailsize = $self->{'thumbnailsize'} || 100;
[2230]214 my $thumbnailtype = $self->{'thumbnailtype'} || 'gif';
[4724]215
[2230]216 # Generate the thumbnail with convert
[14953]217 my $result = $self->convert($filename, $thumbnailtype, "-geometry $thumbnailsize" . "x$thumbnailsize", "THUMB");
218 my ($thumbnailfile) = ($result =~ /=>(.*\.$thumbnailtype)/);
[2230]219
220 # Add the thumbnail as an associated file ...
[3137]221 if (-e "$thumbnailfile") {
222 $doc_obj->associate_file("$thumbnailfile", "thumbnail.$thumbnailtype",
[2230]223 "image/$thumbnailtype",$section);
224 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
225 $doc_obj->add_metadata ($section, "Thumb", "thumbnail.$thumbnailtype");
[4724]226
[11834]227 $doc_obj->add_metadata ($section, "thumbicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[Thumb]\" width=[ThumbWidth] height=[ThumbHeight]>");
[2230]228 }
229
[3137]230 # Extract Thumnail metadata from convert output
231 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
232 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
233 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
[1733]234 }
[1758]235
[2230]236 # Make a screen-sized version of the picture if requested
237 if ($self->{'screenviewsize'}) {
238
[3137]239 # To do: if the actual image smaller than the screenview size,
240 # we should use the original !
241
[2230]242 my $screenviewsize = $self->{'screenviewsize'};
243 my $screenviewtype = $self->{'screenviewtype'} || 'jpeg';
244
245 # make the screenview image
[14953]246 my $result = $self->convert($filename, $screenviewtype, "-geometry $screenviewsize" . "x$screenviewsize", "SCREEN");
247 my ($screenviewfilename) = ($result =~ /=>(.*\.$screenviewtype)/);
[2230]248
249 # get screenview dimensions, size and type
250 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
251 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
252 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
253 }
[5078]254 else {
255 $doc_obj->add_metadata ($section, "ScreenWidth", $image_width);
256 $doc_obj->add_metadata ($section, "ScreenHeight", $image_height);
257 }
258
[2230]259 #add the screenview as an associated file ...
260 if (-e "$screenviewfilename") {
261 $doc_obj->associate_file("$screenviewfilename", "screenview.$screenviewtype",
262 "image/$screenviewtype",$section);
263 $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
264 $doc_obj->add_metadata ($section, "Screen", "screenview.$screenviewtype");
[4724]265
[11834]266 $doc_obj->add_metadata ($section, "screenicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[Screen]\" width=[ScreenWidth] height=[ScreenHeight]>");
[2230]267 } else {
268 print $outhandle "ImagePlug: couldn't find \"$screenviewfilename\"\n";
269 }
270 }
271
[1733]272 return $type;
[4724]273
274
[3137]275}
[2230]276
277
[4724]278
[3137]279# Discover the characteristics of an image file with the ImageMagick
280# "identify" command.
281
282sub identify {
283 my ($image, $outhandle, $verbosity) = @_;
284
285 # Use the ImageMagick "identify" command to get the file specs
[5845]286 my $command = "identify \"$image\" 2>&1";
[3137]287 print $outhandle "$command\n" if ($verbosity > 2);
288 my $result = '';
289 $result = `$command`;
290 print $outhandle "$result\n" if ($verbosity > 3);
291
292 # Read the type, width, and height
293 my $type = 'unknown';
294 my $width = 'unknown';
295 my $height = 'unknown';
296
[4790]297 my $image_safe = quotemeta $image;
298 if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
[3137]299 $type = $1;
300 $width = $2;
301 $height = $3;
302 }
[5103]303
[3137]304 # Read the size
305 my $size = "unknown";
306 if ($result =~ m/^.* ([0-9]+)b/) {
307 $size = $1;
[5103]308 }
[5845]309 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?kb?/) {
[3137]310 $size = 1024 * $1;
[5103]311 if (defined($2)) {
312 $size = $size + (1024 * $2);
313 # Truncate size (it isn't going to be very accurate anyway)
314 $size = int($size);
315 }
[3137]316 }
317
318 print $outhandle "file: $image:\t $type, $width, $height, $size\n"
319 if ($verbosity > 2);
320
321 # Return the specs
322 return ($type, $width, $height, $size);
[1733]323}
324
325
[14951]326sub convert
327{
328 my $self = shift(@_);
329 my $source_file_path = shift(@_);
[14953]330 my $target_file_type = shift(@_);
[14951]331 my $convert_options = shift(@_) || "";
332 my $convert_type = shift(@_) || "";
333
334 my $outhandle = $self->{'outhandle'};
335 my $verbosity = $self->{'verbosity'};
336
[14953]337 # Determine the full name and path of the output file
338 my $target_file_path = &util::get_tmp_filename() . "." . $target_file_type;
339 push(@{$self->{'tmp_file_paths'}}, $target_file_path);
340
341 # Generate and run the convert command
[14951]342 my $convert_command = "convert -interlace plane -verbose $convert_options \"$source_file_path\" \"$target_file_path\"";
343 print $outhandle "$convert_type $convert_command\n" if ($verbosity > 2);
344 my $result = `$convert_command 2>&1`;
345 print $outhandle "$convert_type RESULT = $result\n" if ($verbosity > 2);
346
347 return $result;
348}
349
350
[11090]351# The ImagePlug read() function.
[1733]352# ImagePlug overrides read() because there is no need to read the actual
353# text of the file in, because the contents of the file is not text...
354#
355# Return number of files processed, undef if can't process
356# Note that $base_dir might be "" and that $file might
357# include directories
358
359sub read {
360 my $self = shift (@_);
[9853]361 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
[1733]362
[2230]363 my $outhandle = $self->{'outhandle'};
364
[11090]365 #check process and block exps, smart block, etc
366 my ($block_status,$filename) = $self->read_block(@_);
367 return $block_status if ((!defined $block_status) || ($block_status==0));
[6332]368
369 print STDERR "<Processing n='$file' p='ImagePlug'>\n" if ($gli);
[9960]370 print $outhandle "ImagePlug processing $file\n"
[1758]371 if $self->{'verbosity'} > 1;
372
[9585]373 # None of this works very well on Windows 95/98...
374 if ($ENV{'GSDLOS'} eq "windows" && !Win32::IsWinNT()) {
375 if ($gli) {
376 print STDERR "<ProcessingError n='$file' r='Windows 95/98 not supported'>\n";
377 }
[9703]378 print $outhandle "ImagePlug: Windows 95/98 not supported\n";
[9585]379 return -1;
380 }
381
382 # None of this is going to work very well without ImageMagick...
383 if ($self->{'imagemagick_not_installed'}) {
384 if ($gli) {
385 print STDERR "<ProcessingError n='$file' r='ImageMagick not installed'>\n";
386 }
[9703]387 print $outhandle "ImagePlug: ImageMagick not installed\n";
[9585]388 return -1;
389 }
390
[2230]391 #if there's a leading directory name, eat it...
392 $file =~ s/^.*[\/\\]//;
[1733]393
394 # create a new document
395 my $doc_obj = new doc ($filename, "indexed_doc");
[12270]396 $doc_obj->set_OIDtype ($processor->{'OIDtype'}, $processor->{'OIDmetadata'});
[7508]397 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
[2327]398
[1733]399 #run convert to get the thumbnail and extract size and type info
[14950]400 my $result = generate_images($self, $filename, $file, $doc_obj);
[1744]401
[1758]402 if (!defined $result)
[1744]403 {
[9585]404 if ($gli) {
405 print STDERR "<ProcessingError n='$file'>\n";
406 }
[9703]407 print $outhandle "ImagePlug: couldn't process \"$filename\"\n";
[7362]408 return -1; # error during processing
[1733]409 }
410
[13243]411
[13269]412 #create an empty text string so we don't break downstream plugins
413 my $text = &gsprintf::lookup_string("{BasPlug.dummy_text}",1);
414
[1733]415 # include any metadata passed in from previous plugins
416 # note that this metadata is associated with the top level section
[1744]417 my $section = $doc_obj->get_top_section();
[1733]418 $self->extra_metadata ($doc_obj, $section, $metadata);
419
420 # do plugin specific processing of doc_obj
[9585]421 unless (defined ($self->process(\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj))) {
422 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
423 return -1;
424 }
[1733]425
426 # do any automatic metadata extraction
427 $self->auto_extract_metadata ($doc_obj);
428
[8350]429 # if we haven't found any Title so far, assign one
430 # this was shifted to here from inside read()
431 $self->title_fallback($doc_obj,$section,$file);
[1733]432 # add an OID
433 $doc_obj->set_OID();
[13243]434 $doc_obj->add_utf8_text($section, $text);
[1733]435
436 # process the document
437 $processor->process($doc_obj);
438
[2230]439 # clean up temporary files - we do this here instead of in
[14950]440 # generate_images becuase associated files aren't actually copied
[2230]441 # until after process has been run.
[14952]442 foreach my $tmp_file_path (@{$self->{'tmp_file_paths'}})
443 {
444 if (-e $tmp_file_path)
445 {
446 &util::rm($tmp_file_path);
447 }
[1733]448 }
[13243]449
[3307]450 $self->{'num_processed'}++;
451
[2207]452 return 1;
[1733]453}
454
455# do plugin specific processing of doc_obj
456sub process {
457 my $self = shift (@_);
458 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
459 my $outhandle = $self->{'outhandle'};
460
461 return 1;
462}
463
4641;
[4724]465
466
467
468
469
470
471
472
473
474
475
Note: See TracBrowser for help on using the repository browser.