source: main/trunk/greenstone2/perllib/plugins/ImageConverter.pm@ 24225

Last change on this file since 24225 was 24225, checked in by ak19, 13 years ago

Still on ticket 449. Now srclink_file metadata (contains an underscore that makes things difficult for GS3) is renamed to srclinkFile. Related commits are in GS2's runtime-src formattools.cpp and dublincore.cpp and GS3's default/transform/config_format.xsl and Action.java.

  • Property svn:executable set to *
File size: 18.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 BaseMediaConverter;
29
30
31use strict;
32no strict 'refs'; # allow filehandles to be variables and viceversa
33
34use gsprintf 'gsprintf';
35
36BEGIN {
37 @ImageConverter::ISA = ('BaseMediaConverter');
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 ];
93
94my $options = { 'name' => "ImageConverter",
95 'desc' => "{ImageConverter.desc}",
96 'abstract' => "yes",
97 'inherits' => "yes",
98 'args' => $arguments };
99
100sub new {
101 my ($class) = shift (@_);
102 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
103 push(@$pluginlist, $class);
104
105 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
106 push(@{$hashArgOptLists->{"OptList"}},$options);
107
108 my $self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
109
110
111 return bless $self, $class;
112
113}
114
115# needs to be called after BasePlugin init, so that outhandle is set up.
116sub init {
117 my $self = shift(@_);
118
119 $self->{'tmp_file_paths'} = ();
120
121 # Check that ImageMagick is installed and available on the path
122 my $image_conversion_available = 1;
123 my $no_image_conversion_reason = "";
124 # None of this works very well on Windows 95/98...
125 if ($ENV{'GSDLOS'} eq "windows" && !Win32::IsWinNT()) {
126 $image_conversion_available = 0;
127 $no_image_conversion_reason = "win95notsupported";
128 } else {
129 my $result = `identify -help 2>&1`;
130 my $return_value = $?;
131
132 if ( ($ENV{'GSDLOS'} eq "windows" && $return_value == 256) || $return_value == -1) { # Linux and Windows return different values for "program not found"
133 $image_conversion_available = 0;
134 $no_image_conversion_reason = "imagemagicknotinstalled";
135 }
136 }
137 $self->{'image_conversion_available'} = $image_conversion_available;
138 $self->{'no_image_conversion_reason'} = $no_image_conversion_reason;
139
140 if ($self->{'image_conversion_available'} == 0) {
141 my $outhandle = $self->{'outhandle'};
142 &gsprintf($outhandle, "ImageConverter: {ImageConverter.noconversionavailable} ({ImageConverter.".$self->{'no_image_conversion_reason'}."})\n");
143 }
144
145}
146
147
148# convert image to new type if converttotype is set
149# generate thumbnails if required
150# generate screenview if required
151# discover image metadata
152# filename_no_path must be in utf8 and url-encoded
153sub generate_images {
154 my $self = shift(@_);
155 my ($filename_full_path, $filename_encoded_full_path, $doc_obj, $section, $filename_encoding) = @_;
156
157 my ($unused_fefp,$filename_encoded_no_path)
158 = util::get_full_filenames("",$filename_encoded_full_path);
159
160 # The following is potentially very muddled thinking (but currently seems to work)
161 # generate_images currently called from ImagePlugin and PagedImagePlugin
162 my $filename_no_path = $filename_encoded_no_path;
163
164 # check image magick status
165 return 0 if $self->{'image_conversion_available'} == 0;
166
167 # check the filenames
168 return 0 if ($filename_no_path eq "" || !-f $filename_full_path);
169
170 if ($self->{'enable_cache'}) {
171 $self->init_cache_for_file($filename_full_path);
172 }
173 if ($self->{'store_file_paths'}) {
174 $self->{'orig_file'} = "";
175 $self->{'thumb_file'} = "";
176 $self->{'screen_file'} = "";
177 }
178 my $verbosity = $self->{'verbosity'};
179 my $outhandle = $self->{'outhandle'};
180
181 # check the size of the image against minimum size if specified
182 my $minimumsize = $self->{'minimumsize'};
183 if (defined $minimumsize && (-s $filename_full_path < $minimumsize)) {
184 print $outhandle "ImageConverter: \"$filename_full_path\" too small, skipping\n"
185 if ($verbosity > 1);
186 return 0; # or is there a better return value??
187 }
188
189 my $filehead = $filename_no_path;
190 $filehead =~ s/\.([^\.]*)$//; # filename with no extension
191 my $assocfilemeta = "[assocfilepath]";
192 if ($section ne $doc_obj->get_top_section()) {
193 $assocfilemeta = "[parent(Top):assocfilepath]";
194 }
195
196 # The images that will get generated may contain percent signs in their src filenames
197 # Encode those percent signs themselves so that urls to the imgs refer to them correctly
198 my $url_to_filehead = &unicode::filename_to_url($filehead);
199 my $url_to_filename_no_path = &unicode::filename_to_url($filename_no_path);
200
201 # Convert the image to a new type (if required).
202 my $converttotype = $self->{'converttotype'};
203 my $type = "unknown";
204
205 if ($converttotype ne "" && $filename_full_path !~ m/$converttotype$/) {
206# # $doc_obj->add_utf8_metadata($section, "Image", $utf8_filename_meta);
207
208 my ($result, $converted_filename_full_path)
209 = $self->convert($filename_full_path, $converttotype, "", "CONVERTTYPE");
210
211 $type = $converttotype;
212 $filename_full_path = $converted_filename_full_path;
213 $filename_no_path = "$filehead.$type";
214 $url_to_filename_no_path = "$url_to_filehead.$type";
215 if ($self->{'store_file_paths'}) {
216 $self->{'orig_file'} = $converted_filename_full_path;
217 }
218 }
219
220 # add Image metadata
221 $doc_obj->add_utf8_metadata($section, "Image", $url_to_filename_no_path); # url to generated image
222
223 # here we overwrite the original with the potentially converted one
224# $doc_obj->set_utf8_metadata_element($section, "Source", &unicode::url_decode($filename_no_path)); # displayname of generated image
225# $doc_obj->set_utf8_metadata_element($section, "SourceFile", $url_to_filename_no_path); # displayname of generated image
226
227# $self->set_Source_metadata($doc_obj,$url_to_filename_no_path,undef);
228
229 my $raw_filename_full_path = &unicode::url_decode($filename_encoded_full_path);
230 $self->set_Source_metadata($doc_obj,$raw_filename_full_path,
231 $filename_encoding, $section);
232
233
234 # use identify to get info about the (possibly converted) image
235 my ($image_type, $image_width, $image_height, $image_size)
236 = &identify($filename_full_path, $outhandle, $verbosity);
237
238 if ($image_type ne " ") {
239 $type = $self->correct_mime_type($image_type);
240 }
241
242 #overwrite the ones added in BasePlugin
243 $doc_obj->set_metadata_element ($section, "FileFormat", $type);
244 $doc_obj->set_metadata_element ($section, "FileSize", $image_size);
245
246 $doc_obj->add_metadata ($section, "ImageType", $image_type);
247 $doc_obj->add_metadata ($section, "ImageWidth", $image_width);
248 $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
249 $doc_obj->add_metadata ($section, "ImageSize", $image_size);
250
251 if ((defined $self->{'MaxImageWidth'})
252 && ($image_width > $self->{'MaxImageWidth'})) {
253 $self->{'MaxImageWidth'} = $image_width;
254 }
255 if ((defined $self->{'MaxImageHeight'})
256 && ($image_height > $self->{'MaxImageHeight'})) {
257 $self->{'MaxImageHeight'} = $image_height;
258 }
259
260 # srclink_file is now deprecated because of the "_" in the metadataname. Use srclinkFile
261 $doc_obj->add_metadata ($section, "srclink_file", $url_to_filename_no_path);
262 $doc_obj->add_metadata ($section, "srclinkFile", $url_to_filename_no_path);
263 $doc_obj->add_metadata ($section, "srcicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[srclinkFile]\" width=\"[ImageWidth]\" height=\"[ImageHeight]\">");
264
265 # Add the image as an associated file
266 $doc_obj->associate_file($filename_full_path, $filename_no_path, "image/$type", $section);
267
268 if ($self->{'create_thumbnail'} eq "true") {
269 $self->create_thumbnail($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead);
270 }
271 if ($self->{'create_screenview'} eq "true") {
272 $self->create_screenview($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead, $image_width, $image_height);
273 }
274
275 return 1;
276}
277
278sub create_thumbnail {
279 my $self = shift(@_);
280 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead) = @_;
281 $url_to_filehead = $filehead unless defined $url_to_filehead;
282
283 my $thumbnailsize = $self->{'thumbnailsize'};
284 my $thumbnailtype = $self->correct_mime_type($self->{'thumbnailtype'});
285
286 # Generate the thumbnail with convert
287 my ($result,$thumbnailfile)
288 = $self->convert($original_file, $thumbnailtype, "-geometry $thumbnailsize" . "x$thumbnailsize", "THUMB");
289
290 # Add the thumbnail as an associated file ...
291 if (-e "$thumbnailfile") {
292 $doc_obj->associate_file("$thumbnailfile", $filehead."_thumb.$thumbnailtype",
293 "image/$thumbnailtype",$section); # name of generated image
294 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
295 $doc_obj->add_utf8_metadata ($section, "Thumb", $url_to_filehead."_thumb.$thumbnailtype"); # url to generated image
296
297 $doc_obj->add_metadata ($section, "thumbicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Thumb]\" width=[ThumbWidth] height=[ThumbHeight]>");
298
299
300 # Extract Thumbnail metadata from convert output
301 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
302 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
303 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
304 }
305 if ($self->{'store_file_paths'}) {
306 $self->{'thumb_file'} = $thumbnailfile;
307 }
308
309 } else {
310 my $outhandle = $self->{'outhandle'};
311 print $outhandle "Couldn't find thumbnail $thumbnailfile\n";
312
313 }
314}
315
316sub create_screenview {
317
318 my $self = shift(@_);
319 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead, $image_width, $image_height) = @_;
320 $url_to_filehead = $filehead unless defined $url_to_filehead;
321
322 my $screenviewsize = $self->{'screenviewsize'};
323 my $screenviewtype = $self->correct_mime_type($self->{'screenviewtype'});
324
325 # Scale the image, unless the original image is smaller than the screenview size and -noscaleup is set
326 my $scale_option = "-geometry $screenviewsize" . "x$screenviewsize";
327 if ($self->{'noscaleup'} && $image_width < $screenviewsize && $image_height < $screenviewsize)
328 {
329 $scale_option = "";
330 }
331
332 # make the screenview image
333 my ($result,$screenviewfilename)
334 = $self->convert($original_file, $screenviewtype, $scale_option, "SCREEN");
335
336 #add the screenview as an associated file ...
337 if (-e "$screenviewfilename") {
338 $doc_obj->associate_file("$screenviewfilename", $filehead."_screen.$screenviewtype", "image/$screenviewtype",$section); # name of generated image
339 $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
340 $doc_obj->add_utf8_metadata ($section, "Screen", $url_to_filehead."_screen.$screenviewtype"); # url to generated image
341
342 $doc_obj->add_metadata ($section, "screenicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Screen]\" width=[ScreenWidth] height=[ScreenHeight]>");
343
344 # get screenview dimensions, size and type
345 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
346 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
347 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
348 } elsif ($result =~ m/([0-9]+)x([0-9]+)/) {
349 #if the image hasn't changed size, the previous regex doesn't match
350 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
351 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
352 }
353
354 if ($self->{'store_file_paths'}) {
355 $self->{'screen_file'} = $screenviewfilename;
356 }
357
358 } else {
359 my $outhandle = $self->{'outhandle'};
360 print $outhandle "Couldn't find screenview file $screenviewfilename\n";
361
362 }
363
364}
365
366
367
368sub convert {
369 my $self = shift(@_);
370 my $source_file_path = shift(@_);
371 my $target_file_type = shift(@_);
372 my $convert_options = shift(@_) || "";
373 my $convert_id = shift(@_) || "";
374 my $cache_mode = shift(@_) || "";
375
376 my $outhandle = $self->{'outhandle'};
377 my $verbosity = $self->{'verbosity'};
378
379 my $source_file_no_path = &File::Basename::basename($source_file_path);
380
381 # Determine the full name and path of the output file
382 my $target_file_path;
383 if ($self->{'enable_cache'}) {
384 my $cached_image_dir = $self->{'cached_dir'};
385 my $image_root = $self->{'cached_file_root'};
386 $image_root .= "_$convert_id" if ($convert_id ne "");
387 my $image_file = "$image_root.$target_file_type";
388 $target_file_path = &util::filename_cat($cached_image_dir,$image_file);
389 }
390 else {
391 $target_file_path = &util::get_tmp_filename($target_file_type);
392 push(@{$self->{'tmp_file_paths'}}, $target_file_path);
393
394 # Output filename used to be parsed from result line:
395 # my ($ofilename) = ($result =~ m/=>(.*\.$target_file_type)/);
396 # by the function that called 'convert'
397 # but this is no longer needed, as output filename is now
398 # explicitly passed back
399 }
400
401 # Generate and run the convert command
402 my $convert_command = "convert -interlace plane -verbose $convert_options \"$source_file_path\" \"$target_file_path\"";
403
404 my $print_info = { 'message_prefix' => $convert_id,
405 'message' => "Converting image $source_file_no_path to: $convert_id $target_file_type" };
406 $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
407
408 my ($regenerated,$result,$had_error)
409 = $self->autorun_general_cmd($convert_command,$source_file_path,$target_file_path,$print_info);
410
411 return ($result,$target_file_path);
412}
413
414sub convert_without_result {
415 my $self = shift(@_);
416
417 my $source_file_path = shift(@_);
418 my $target_file_type = shift(@_);
419 my $convert_options = shift(@_) || "";
420 my $convert_id = shift(@_) || "";
421
422 return $self->convert($source_file_path,$target_file_type,
423 $convert_options,$convert_id,"without_result");
424}
425
426
427# Discover the characteristics of an image file with the ImageMagick
428# "identify" command.
429
430sub identify {
431 my ($image, $outhandle, $verbosity) = @_;
432
433 # Use the ImageMagick "identify" command to get the file specs
434 my $command = "identify \"$image\" 2>&1";
435 print $outhandle "$command\n" if ($verbosity > 2);
436 my $result = '';
437 $result = `$command`;
438 print $outhandle "$result\n" if ($verbosity > 3);
439
440 # Read the type, width, and height
441 my $type = 'unknown';
442 my $width = 'unknown';
443 my $height = 'unknown';
444
445 my $image_safe = quotemeta $image;
446 if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
447 $type = $1;
448 $width = $2;
449 $height = $3;
450 }
451
452 # Read the size
453 my $size = "unknown";
454 if ($result =~ m/^.* ([0-9]+)b/) {
455 $size = $1;
456 }
457 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?kb?/) {
458 $size = 1024 * $1;
459 if (defined($2)) {
460 $size = $size + (1024 * $2);
461 # Truncate size (it isn't going to be very accurate anyway)
462 $size = int($size);
463 }
464 }
465 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?mb?/) {
466 $size = 1024 * 1024 * $1;
467 if (defined($2)) {
468 $size = $size + (1024 * 1024 * $2);
469 # Truncate size (it isn't going to be very accurate anyway)
470 $size = int($size);
471 }
472 }
473 elsif ($result =~ m/^.* (([0-9]+)(\.([0-9]+))?e\+([0-9]+))(kb|b)?/) {
474 # Deals with file sizes on Linux of type "3.4e+02kb" where e+02 is 1*10^2.
475 # 3.4e+02 therefore evaluates to 3.4 x 1 x 10^2 = 340kb.
476 # Programming languages including Perl know how that 3.4e+02 is a number,
477 # so we don't need to do any calculations.
478 $size = $1*1; # turn the string into a number by multiplying it by 1
479 #if we did $size = $1; $size would be merely the string "3.4e+02"
480 $size = int($size); # truncate size
481 }
482 print $outhandle "file: $image:\t $type, $width, $height, $size\n"
483 if ($verbosity > 2);
484
485 # Return the specs
486 return ($type, $width, $height, $size);
487}
488
489sub clean_up_temporary_files {
490 my $self = shift(@_);
491
492 foreach my $tmp_file_path (@{$self->{'tmp_file_paths'}}) {
493 if (-e $tmp_file_path) {
494 &util::rm($tmp_file_path);
495 }
496 }
497
498}
499
500# image/jpg is not a valid mime-type, it ought to be image/jpeg.
501# Sometimes JPEG is passed in also, want to keep things lowercase just in case.
502sub correct_mime_type {
503 my $self = shift(@_);
504 my ($file_extension) = @_;
505
506 $file_extension = lc($file_extension);
507 $file_extension =~ s/jpg/jpeg/s;
508
509 return $file_extension;
510}
511
5121;
Note: See TracBrowser for help on using the repository browser.