source: gsdl/trunk/perllib/plugins/ImageConverter.pm@ 16825

Last change on this file since 16825 was 16825, checked in by davidb, 16 years ago

Code for supporting cache merged back in with Katherine's restructured plugins

  • Property svn:executable set to *
File size: 15.6 KB
Line 
1###########################################################################
2#
3# ImageConverter - helper plugin that does image conversion using ImageMagick
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 2008 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26package ImageConverter;
27
28use MediaConverter;
29
30
31use strict;
32no strict 'refs'; # allow filehandles to be variables and viceversa
33
34use gsprintf 'gsprintf';
35
36BEGIN {
37 @ImageConverter::ISA = ('MediaConverter');
38}
39
40my $arguments = [
41 { 'name' => "create_thumbnail",
42 'desc' => "{ImageConverter.create_thumbnail}",
43 'type' => "enum",
44 'list' => [{'name' => "true", 'desc' => "{common.true}"},
45 {'name' => "false", 'desc' => "{common.false}"}],
46 'deft' => "true",
47 'reqd' => "no" },
48 { 'name' => "thumbnailsize",
49 'desc' => "{ImageConverter.thumbnailsize}",
50 'type' => "int",
51 'deft' => "100",
52 'range' => "1,",
53 'reqd' => "no" },
54 { 'name' => "thumbnailtype",
55 'desc' => "{ImageConverter.thumbnailtype}",
56 'type' => "string",
57 'deft' => "gif",
58 'reqd' => "no" },
59 { 'name' => "noscaleup",
60 'desc' => "{ImageConverter.noscaleup}",
61 'type' => "flag",
62 'reqd' => "no" },
63 { 'name' => "create_screenview",
64 'desc' => "{ImageConverter.create_screenview}",
65 'type' => "enum",
66 'list' => [{'name' => "true", 'desc' => "{common.true}"},
67 {'name' => "false", 'desc' => "{common.false}"}],
68 'deft' => "true",
69 'reqd' => "no" },
70 { 'name' => "screenviewsize",
71 'desc' => "{ImageConverter.screenviewsize}",
72 'type' => "int",
73 'deft' => "500",
74 'range' => "1,",
75 'reqd' => "no" },
76 { 'name' => "screenviewtype",
77 'desc' => "{ImageConverter.screenviewtype}",
78 'type' => "string",
79 'deft' => "jpg",
80 'reqd' => "no" },
81 { 'name' => "converttotype",
82 'desc' => "{ImageConverter.converttotype}",
83 'type' => "string",
84 'deft' => "",
85 'reqd' => "no" },
86 { 'name' => "minimumsize",
87 'desc' => "{ImageConverter.minimumsize}",
88 'type' => "int",
89 'deft' => "100",
90 'range' => "1,",
91 'reqd' => "no" },
92 { 'name' => "cache_generated_images",
93 'desc' => "{ImageConverter.cache_generated_images}",
94 'type' => "flag",
95 'reqd' => "no",
96 'hiddengli' => "yes" # not yet implemented
97 }
98 ];
99
100my $options = { 'name' => "ImageConverter",
101 'desc' => "{ImageConverter.desc}",
102 'abstract' => "yes",
103 'inherits' => "yes",
104 'args' => $arguments };
105
106sub new {
107 my ($class) = shift (@_);
108 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
109 push(@$pluginlist, $class);
110
111 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
112 push(@{$hashArgOptLists->{"OptList"}},$options);
113
114 my $self = new MediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
115
116
117 return bless $self, $class;
118
119}
120
121# needs to be called after BasePlugin init, so that outhandle is set up.
122sub init {
123 my $self = shift(@_);
124
125 $self->{'tmp_file_paths'} = ();
126
127 # Check that ImageMagick is installed and available on the path
128 my $image_conversion_available = 1;
129 my $no_image_conversion_reason = "";
130 # None of this works very well on Windows 95/98...
131 if ($ENV{'GSDLOS'} eq "windows" && !Win32::IsWinNT()) {
132 $image_conversion_available = 0;
133 $no_image_conversion_reason = "win95notsupported";
134 } else {
135 my $result = `identify 2>&1`;
136 if ($? == -1 || $? == 256) { # Linux and Windows return different values for "program not found"
137 $image_conversion_available = 0;
138 $no_image_conversion_reason = "imagemagicknotinstalled";
139 }
140 }
141 $self->{'image_conversion_available'} = $image_conversion_available;
142 $self->{'no_image_conversion_reason'} = $no_image_conversion_reason;
143
144 if ($self->{'image_conversion_available'} == 0) {
145 my $outhandle = $self->{'outhandle'};
146 &gsprintf($outhandle, "ImageConverter: {ImageConverter.noconversionavailable} ({ImageConverter.".$self->{'no_image_conversion_reason'}."})\n");
147 }
148
149}
150
151
152# convert image to new type if converttotype is set
153# generate thumbnails if required
154# generate screenview if required
155# discover image metadata
156# filename_no_path must be in utf8
157sub generate_images {
158 my $self = shift(@_);
159
160 my ($filename_full_path, $filename_no_path, $doc_obj, $section) = @_;
161
162 # check image magick status
163 return 0 if $self->{'image_conversion_available'} == 0;
164 # check the filenames
165 return 0 if ($filename_no_path eq "" || !-f $filename_full_path);
166
167 if ($self->{'cache_generated_images'}) {
168 $self->init_cache_for_file($filename_full_path);
169 }
170
171 my $verbosity = $self->{'verbosity'};
172 my $outhandle = $self->{'outhandle'};
173
174 # check the size of the image against minimum size if specified
175 my $minimumsize = $self->{'minimumsize'};
176 if (defined $minimumsize && (-s $filename_full_path < $minimumsize)) {
177 print $outhandle "ImageConverter: \"$filename_full_path\" too small, skipping\n"
178 if ($verbosity > 1);
179 return 0; # or is there a better return value??
180 }
181
182 my $filehead = $filename_no_path;
183 $filehead =~ s/\.([^\.]*)$//; # filename with no extension
184 my $assocfilemeta = "[assocfilepath]";
185 if ($section ne $doc_obj->get_top_section()) {
186 $assocfilemeta = "[parent(Top):assocfilepath]";
187 }
188
189 # Images that will get generated may contain percent signs in their src filenames
190 # Encode those percent signs themselves so that urls to the imgs refer to them correctly
191 my $url_to_filehead = $filehead;
192 $url_to_filehead =~ s/%/%25/g;
193 my $url_to_filename_no_path = $filename_no_path;
194 $url_to_filename_no_path =~ s/%/%25/g;
195
196 # Convert the image to a new type (if required).
197 my $converttotype = $self->{'converttotype'};
198 my $type = "unknown";
199
200 if ($converttotype ne "" && $filename_full_path !~ m/$converttotype$/) {
201 # $doc_obj->add_utf8_metadata($section, "Image", $utf8_filename_meta);
202
203 my ($result,$filename_full_path)
204 = $self->convert($filename_full_path, $converttotype, "", "CONVERTTYPE");
205
206 $type = $converttotype;
207 $filename_no_path = "$filehead.$type";
208 $url_to_filename_no_path = "$url_to_filehead.$type";
209 }
210
211 # add Image metadata
212 $doc_obj->add_utf8_metadata($section, "Image", $url_to_filename_no_path); # url to generated image
213
214 # here we overwrite the original with the potentially converted one
215 $doc_obj->set_utf8_metadata_element($section, "Source", $filename_no_path); # name of generated image
216
217 # use identify to get info about the (possibly converted) image
218 my ($image_type, $image_width, $image_height, $image_size)
219 = &identify($filename_full_path, $outhandle, $verbosity);
220
221 if ($image_type ne " ") {
222 $type = $image_type;
223 }
224
225 #overwrite the ones added in BasePlugin
226 $doc_obj->set_metadata_element ($section, "FileFormat", $type);
227 $doc_obj->set_metadata_element ($section, "FileSize", $image_size);
228
229 $doc_obj->add_metadata ($section, "ImageType", $image_type);
230 $doc_obj->add_metadata ($section, "ImageWidth", $image_width);
231 $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
232 $doc_obj->add_metadata ($section, "ImageSize", $image_size);
233
234 $doc_obj->add_metadata ($section, "srclink", "<a href=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Image]\">");
235 $doc_obj->add_metadata ($section, "/srclink", "</a>");
236 $doc_obj->add_metadata ($section, "srcicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Image]\" width=\"100\">");
237
238 # Add the image as an associated file
239 $doc_obj->associate_file($filename_full_path, $filename_no_path, "image/$type", $section);
240
241 if ($self->{'create_thumbnail'} eq "true") {
242 $self->create_thumbnail($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead);
243 }
244 if ($self->{'create_screenview'} eq "true") {
245 $self->create_screenview($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead);
246 }
247}
248
249sub create_thumbnail {
250 my $self = shift(@_);
251 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead) = @_;
252 $url_to_filehead = $filehead unless defined $url_to_filehead;
253
254 my $thumbnailsize = $self->{'thumbnailsize'};
255 my $thumbnailtype = $self->{'thumbnailtype'};
256
257 # Generate the thumbnail with convert
258 my ($result,$thumbnailfile)
259 = $self->convert($original_file, $thumbnailtype, "-geometry $thumbnailsize" . "x$thumbnailsize", "THUMB");
260
261 # Add the thumbnail as an associated file ...
262 if (-e "$thumbnailfile") {
263 $doc_obj->associate_file("$thumbnailfile", $filehead."_thumb.$thumbnailtype",
264 "image/$thumbnailtype",$section); # name of generated image
265 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
266 $doc_obj->add_utf8_metadata ($section, "Thumb", $url_to_filehead."_thumb.$thumbnailtype"); # url to generated image
267
268 $doc_obj->add_metadata ($section, "thumbicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Thumb]\" width=[ThumbWidth] height=[ThumbHeight]>");
269
270
271 # Extract Thumbnail metadata from convert output
272 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
273 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
274 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
275 }
276 } else {
277 my $outhandle = $self->{'outhandle'};
278 print $outhandle "Couldn't find thumbnail $thumbnailfile\n";
279
280 }
281}
282
283sub create_screenview {
284
285 my $self = shift(@_);
286 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead) = @_;
287 $url_to_filehead = $filehead unless defined $url_to_filehead;
288
289 # To do: if the actual image smaller than the screenview size,
290 # we should use the original !
291
292 my $screenviewsize = $self->{'screenviewsize'};
293 my $screenviewtype = $self->{'screenviewtype'};
294
295 # make the screenview image
296 my ($result,$screenviewfilename)
297 = $self->convert($original_file, $screenviewtype, "-geometry $screenviewsize" . "x$screenviewsize", "SCREEN");
298
299 #add the screenview as an associated file ...
300 if (-e "$screenviewfilename") {
301 $doc_obj->associate_file("$screenviewfilename", $filehead."_screen.$screenviewtype", "image/$screenviewtype",$section); # name of generated image
302 $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
303 $doc_obj->add_utf8_metadata ($section, "Screen", $url_to_filehead."_screen.$screenviewtype"); # url to generated image
304
305 $doc_obj->add_metadata ($section, "screenicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Screen]\" width=[ScreenWidth] height=[ScreenHeight]>");
306
307 # get screenview dimensions, size and type
308 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
309 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
310 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
311 } elsif ($result =~ m/([0-9]+)x([0-9]+)/) {
312 #if the image hasn't changed size, the previous regex doesn't match
313 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
314 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
315 }
316 } else {
317 my $outhandle = $self->{'outhandle'};
318 print $outhandle "Couldn't find screenview file $screenviewfilename\n";
319
320 }
321
322}
323
324
325
326sub convert {
327 my $self = shift(@_);
328 my $source_file_path = shift(@_);
329 my $target_file_type = shift(@_);
330 my $convert_options = shift(@_) || "";
331 my $convert_id = shift(@_) || "";
332
333 my $outhandle = $self->{'outhandle'};
334 my $verbosity = $self->{'verbosity'};
335
336 my $source_file_no_path = &File::Basename::basename($source_file_path);
337
338 # Determine the full name and path of the output file
339 my $target_file_path;
340 if ($self->{'cache_generated_images'}) {
341 my $cached_image_dir = $self->{'cached_dir'};
342 my $image_root = $self->{'cached_file_root'};
343 $image_root .= "_$convert_id" if ($convert_id ne "");
344 my $image_file = "$image_root.$target_file_type";
345 $target_file_path = &util::filename_cat($cached_image_dir,$image_file);
346 }
347 else {
348 $target_file_path = &util::get_tmp_filename($target_file_type);
349 push(@{$self->{'tmp_file_paths'}}, $target_file_path);
350
351 # Output filename used to be parsed from result line:
352 # my ($ofilename) = ($result =~ m/=>(.*\.$target_file_type)/);
353 # by the function that called 'convert'
354 # but this is no longer needed, as output filename is now
355 # explicitly passed back
356
357 }
358
359 # Generate and run the convert command
360 my $convert_command = "convert -interlace plane -verbose $convert_options \"$source_file_path\" \"$target_file_path\"";
361
362 my $print_info = { 'message_prefix' => $convert_id,
363 'message' => "Converting image $source_file_no_path to: $convert_id $target_file_type" };
364
365 my ($regenerated,$result,$had_error)
366 = $self->autorun_general_cmd($convert_command,$target_file_path,$print_info);
367
368 return ($result,$target_file_path);
369}
370
371
372# Discover the characteristics of an image file with the ImageMagick
373# "identify" command.
374
375sub identify {
376 my ($image, $outhandle, $verbosity) = @_;
377
378 # Use the ImageMagick "identify" command to get the file specs
379 my $command = "identify \"$image\" 2>&1";
380 print $outhandle "$command\n" if ($verbosity > 2);
381 my $result = '';
382 $result = `$command`;
383 print $outhandle "$result\n" if ($verbosity > 3);
384
385 # Read the type, width, and height
386 my $type = 'unknown';
387 my $width = 'unknown';
388 my $height = 'unknown';
389
390 my $image_safe = quotemeta $image;
391 if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
392 $type = $1;
393 $width = $2;
394 $height = $3;
395 }
396
397 # Read the size
398 my $size = "unknown";
399 if ($result =~ m/^.* ([0-9]+)b/) {
400 $size = $1;
401 }
402 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?kb?/) {
403 $size = 1024 * $1;
404 if (defined($2)) {
405 $size = $size + (1024 * $2);
406 # Truncate size (it isn't going to be very accurate anyway)
407 $size = int($size);
408 }
409 }
410 elsif ($result =~ m/^.* (([0-9]+)(\.([0-9]+))?e\+([0-9]+))(kb|b)?/) {
411 # Deals with file sizes on Linux of type "3.4e+02kb" where e+02 is 1*10^2.
412 # 3.4e+02 therefore evaluates to 3.4 x 1 x 10^2 = 340kb.
413 # Programming languages including Perl know how that 3.4e+02 is a number,
414 # so we don't need to do any calculations.
415 $size = $1*1; # turn the string into a number by multiplying it by 1
416 #if we did $size = $1; $size would be merely the string "3.4e+02"
417 $size = int($size); # truncate size
418 }
419 print $outhandle "file: $image:\t $type, $width, $height, $size\n"
420 if ($verbosity > 2);
421
422 # Return the specs
423 return ($type, $width, $height, $size);
424}
425
426sub clean_up_temporary_files {
427 my $self = shift(@_);
428
429 foreach my $tmp_file_path (@{$self->{'tmp_file_paths'}}) {
430 if (-e $tmp_file_path) {
431 &util::rm($tmp_file_path);
432 }
433 }
434
435}
436
4371;
Note: See TracBrowser for help on using the repository browser.