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

Last change on this file since 14657 was 14657, checked in by anna, 17 years ago

Updated Perl Module in Marathi. Many thanks to Shubhada Nagarkar.

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