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

Last change on this file since 3307 was 3307, checked in by davidb, 22 years ago

Some minor modifications to Image Plugin: filenames can now
include spaces, the images generated are interlaced (perhaps this
would be better controlled through an option) and the number of
file processes correctly stored through $self->{'num_processed'}

  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 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
34
35sub print_usage {
36 my ($plugin_name) = @_;
37
38 print STDERR "
39 usage: plugin ImagePlug [options]
40
41 -noscaleup Don't scale up small images when making thumbnails
42
43 -thumbnailsize n Make thumbnails of size nxn
44
45 -thumbnailtype s Make thumbnails in format 's'
46
47 -screenviewsize n If set, makes an image of size n for screen display
48 and sets Screen, ScreenSize, ScrrenWidth and Screeneight
49 metadata. By default it is not set.
50
51 -screenviewtype s If -screenviewsize is set, this sets the screen display
52 image type. Defaults to jpg.
53
54 -convertto s Convert main inage to (gif|png|jpg)
55
56 -minimumsize n Ignore images smaller than n bytes
57
58"
59}
60
61
62sub new {
63 my ($class) = @_;
64 my $plugin_name = shift (@_);
65 my $self = new BasPlug ("ImagePlug", @_);
66
67 if (!parsargv::parse(\@_,
68 q^noscaleup^, \$self->{'noscaleup'},
69 q^converttotype/.*/^, \$self->{'converttotype'},
70 q^minimumsize/[0-9]*/100^, \$self->{'minimumsize'},
71
72 q^thumbnailsize/[0-9]*/100^, \$self->{'thumbnailsize'},
73 q^thumbnailtype/.*/gif^, \$self->{'thumbnailtype'},
74 q^screenviewsize/[0-9]*/0^, \$self->{'screenviewsize'},
75 q^screenviewtype/.*/jpg^, \$self->{'screenviewtype'},
76 "allow_extra_options")) {
77
78 print STDERR "\nImagePlug uses an incorrect option.\n";
79 print STDERR "Check your collect.cfg configuration file.\n";
80 &print_usage($plugin_name);
81 die "\n";
82 }
83
84 return bless $self, $class;
85}
86
87
88sub get_default_process_exp {
89 my $self = shift (@_);
90
91 return q^(?i)(\.jpe?g|\.gif|\.png|\.bmp|\.xbm|\.tif?f)$^;
92}
93
94
95# Create the thumbnail and screenview images, and discover the Image's
96# size, width, and height using the convert utility.
97
98sub run_convert {
99 my $self = shift (@_);
100 my $filename = shift (@_); # filename with full path
101 my $file = shift (@_); # filename without path
102 my $doc_obj = shift (@_);
103 my $section = $doc_obj->get_top_section();
104
105 my $verbosity = $self->{'verbosity'};
106 my $outhandle = $self->{'outhandle'};
107
108 # check the filename is okay
109 return 0 if ($file eq "" || $filename eq "");
110
111# Code now extended to quote filenames in 'convert' commnads
112# Allows spaces in filenames, but note needs spaces to be escaped in URL as well
113# if ($filename =~ m/ /) {
114# print $outhandle "ImagePlug: \"$filename\" contains a space. choking.\n";
115# return undef;
116# }
117
118 my $minimumsize = $self->{'minimumsize'};
119 if (defined $minimumsize && (-s $filename < $minimumsize)) {
120 print $outhandle "ImagePlug: \"$filename\" too small, skipping\n"
121 if ($verbosity > 1);
122 }
123
124 # Convert the image to a new type (if required).
125 my $converttotype = $self->{'converttotype'};
126 my $originalfilename = ""; # only set if we do a conversion
127 my $type = "unknown";
128
129 if ($converttotype ne "" && $filename =~ m/$converttotype$/) {
130
131 $originalfilename = $filename;
132 $filename = &util::get_tmp_filename() . ".$converttotype";
133 $self->{'tmp_filename'} = $filename;
134
135 my $command = "convert -interlace plane -verbose \"$originalfilename\" \"$filename\"";
136 print $outhandle "$command\n" if ($verbosity > 2);
137 my $result = '';
138 $result = `$command`;
139 print $outhandle "$result\n" if ($verbosity > 3);
140
141 $type = $converttotype;
142 }
143
144 # Add the image metadata
145 my $url = $file;
146 $url =~ s/ /%20/g;
147
148 $doc_obj->add_metadata ($section, "Image", $url);
149 my ($image_type, $image_width, $image_height, $image_size)
150 = &identify($filename, $outhandle, $verbosity);
151
152 $doc_obj->add_metadata ($section, "ImageType", $image_type);
153 $doc_obj->add_metadata ($section, "ImageWidth", $image_width);
154 $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
155 $doc_obj->add_metadata ($section, "ImageSize", $image_size);
156
157 # Add the image as an associated file
158 $doc_obj->associate_file($filename,$file,"image/$type",$section);
159
160 # Make the thumbnail image
161 my $thumbnailsize = $self->{'thumbnailsize'} || 100;
162 my $thumbnailtype = $self->{'thumbnailtype'} || 'gif';
163 my $thumbnailfile = &util::get_tmp_filename() . ".$thumbnailtype";
164 $self->{'tmp_filename2'} = $thumbnailfile;
165
166 # Generate the thumbnail with convert
167 my $command = "convert -interlace plane -verbose -geometry $thumbnailsize"
168 . "x$thumbnailsize \"$filename\" \"$thumbnailfile\"";
169 print $outhandle "$command\n" if ($verbosity > 2);
170 my $result = '';
171 $result = `$command 2>&1` ;
172 print $outhandle "$result\n" if ($verbosity > 3);
173
174 # Add the thumbnail as an associated file ...
175 if (-e "$thumbnailfile") {
176 $doc_obj->associate_file("$thumbnailfile", "thumbnail.$thumbnailtype",
177 "image/$thumbnailtype",$section);
178 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
179 $doc_obj->add_metadata ($section, "Thumb", "thumbnail.$thumbnailtype");
180 }
181
182 # Extract Thumnail metadata from convert output
183 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
184 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
185 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
186 }
187
188 # Make a screen-sized version of the picture if requested
189 if ($self->{'screenviewsize'}) {
190
191 # To do: if the actual image smaller than the screenview size,
192 # we should use the original !
193
194 my $screenviewsize = $self->{'screenviewsize'};
195 my $screenviewtype = $self->{'screenviewtype'} || 'jpeg';
196 my $screenviewfilename = &util::get_tmp_filename() . ".$screenviewtype";
197 $self->{'tmp_filename3'} = $screenviewfilename;
198
199 # make the screenview image
200 my $command = "convert -interlace plane -verbose -geometry $screenviewsize"
201 . "x$screenviewsize \"$filename\" \"$screenviewfilename\"";
202 print $outhandle "$command\n" if ($verbosity > 2);
203 my $result = "";
204 $result = `$command 2>&1` ;
205 print $outhandle "$result\n" if ($verbosity > 3);
206
207 # get screenview dimensions, size and type
208 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
209 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
210 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
211 }
212
213 #add the screenview as an associated file ...
214 if (-e "$screenviewfilename") {
215 $doc_obj->associate_file("$screenviewfilename", "screenview.$screenviewtype",
216 "image/$screenviewtype",$section);
217 $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
218 $doc_obj->add_metadata ($section, "Screen", "screenview.$screenviewtype");
219 } else {
220 print $outhandle "ImagePlug: couldn't find \"$screenviewfilename\"\n";
221 }
222 }
223
224 return $type;
225}
226
227
228# Discover the characteristics of an image file with the ImageMagick
229# "identify" command.
230
231sub identify {
232 my ($image, $outhandle, $verbosity) = @_;
233
234 # Use the ImageMagick "identify" command to get the file specs
235 my $command = "identify $image 2>&1";
236 print $outhandle "$command\n" if ($verbosity > 2);
237 my $result = '';
238 $result = `$command`;
239 print $outhandle "$result\n" if ($verbosity > 3);
240
241 # Read the type, width, and height
242 my $type = 'unknown';
243 my $width = 'unknown';
244 my $height = 'unknown';
245
246 if ($result =~ /^$image (\w+) (\d+)x(\d+)/) {
247 $type = $1;
248 $width = $2;
249 $height = $3;
250 }
251
252 # Read the size
253 my $size = "unknown";
254 if ($result =~ m/^.* ([0-9]+)b/) {
255 $size = $1;
256 } elsif ($result =~ m/^.* ([0-9]+)kb/) {
257 $size = 1024 * $1;
258 }
259
260 print $outhandle "file: $image:\t $type, $width, $height, $size\n"
261 if ($verbosity > 2);
262
263 # Return the specs
264 return ($type, $width, $height, $size);
265}
266
267
268# The ImagePlug read() function. This function does all the right things
269# to make general options work for a given plugin. It calls the process()
270# function which does all the work specific to a plugin (like the old
271# read functions used to do). Most plugins should define their own
272# process() function and let this read() function keep control.
273#
274# ImagePlug overrides read() because there is no need to read the actual
275# text of the file in, because the contents of the file is not text...
276#
277# Return number of files processed, undef if can't process
278# Note that $base_dir might be "" and that $file might
279# include directories
280
281sub read {
282 my $self = shift (@_);
283 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
284
285 my $outhandle = $self->{'outhandle'};
286
287 my $filename = &util::filename_cat($base_dir, $file);
288 return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
289 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
290 return undef;
291 }
292 print $outhandle "ImagePlug processing \"$filename\"\n"
293 if $self->{'verbosity'} > 1;
294
295 #if there's a leading directory name, eat it...
296 $file =~ s/^.*[\/\\]//;
297
298 # create a new document
299 my $doc_obj = new doc ($filename, "indexed_doc");
300 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
301
302 #run convert to get the thumbnail and extract size and type info
303 my $result = run_convert($self, $filename, $file, $doc_obj);
304
305 if (!defined $result)
306 {
307 print "ImagePlug: couldn't process \"$filename\"\n";
308 return 0;
309 }
310
311 #create an empty text string so we don't break downstream plugins
312 my $text = "Dummy text to sidestep display bug.";
313
314 # include any metadata passed in from previous plugins
315 # note that this metadata is associated with the top level section
316 my $section = $doc_obj->get_top_section();
317 $self->extra_metadata ($doc_obj, $section, $metadata);
318
319 # do plugin specific processing of doc_obj
320 return undef unless defined ($self->process (\$text, $pluginfo, $base_dir,
321 $file, $metadata, $doc_obj));
322
323 # do any automatic metadata extraction
324 $self->auto_extract_metadata ($doc_obj);
325
326 # add an OID
327 $doc_obj->set_OID();
328 $doc_obj->add_text($section, $text);
329
330 # process the document
331 $processor->process($doc_obj);
332
333 # clean up temporary files - we do this here instead of in
334 # run_convert becuase associated files aren't actually copied
335 # until after process has been run.
336 if (defined $self->{'tmp_filename'} &&
337 -e $self->{'tmp_filename'}) {
338 &util::rm($self->{'tmp_filename'})
339 }
340 if (defined $self->{'tmp_filename2'} &&
341 -e $self->{'tmp_filename2'}) {
342 &util::rm($self->{'tmp_filename2'})
343 }
344 if (defined $self->{'tmp_filename3'} &&
345 -e $self->{'tmp_filename3'}) {
346 &util::rm($self->{'tmp_filename3'})
347 }
348
349 $self->{'num_processed'}++;
350
351 return 1;
352}
353
354# do plugin specific processing of doc_obj
355sub process {
356 my $self = shift (@_);
357 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
358 my $outhandle = $self->{'outhandle'};
359
360 return 1;
361}
362
3631;
Note: See TracBrowser for help on using the repository browser.