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

Last change on this file since 16382 was 16382, checked in by kjdon, 16 years ago

filename_no_path arg to generate_images must now be in utf8, and then the filename metadata is added using set_utf8_metadata instead of set_metadata

  • Property svn:executable set to *
File size: 14.0 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 PrintInfo;
29
30use strict;
31no strict 'refs'; # allow filehandles to be variables and viceversa
32
33use gsprintf 'gsprintf';
34
35BEGIN {
36 @ImageConverter::ISA = ('PrintInfo');
37}
38
39my $arguments = [
40 { 'name' => "create_thumbnail",
41 'desc' => "{ImageConverter.create_thumbnail}",
42 'type' => "enum",
43 'list' => [{'name' => "true", 'desc' => "{common.true}"},
44 {'name' => "false", 'desc' => "{common.false}"}],
45 'deft' => "true",
46 'reqd' => "no" },
47 { 'name' => "thumbnailsize",
48 'desc' => "{ImageConverter.thumbnailsize}",
49 'type' => "int",
50 'deft' => "100",
51 'range' => "1,",
52 'reqd' => "no" },
53 { 'name' => "thumbnailtype",
54 'desc' => "{ImageConverter.thumbnailtype}",
55 'type' => "string",
56 'deft' => "gif",
57 'reqd' => "no" },
58 { 'name' => "noscaleup",
59 'desc' => "{ImageConverter.noscaleup}",
60 'type' => "flag",
61 'reqd' => "no" },
62 { 'name' => "create_screenview",
63 'desc' => "{ImageConverter.create_screenview}",
64 'type' => "enum",
65 'list' => [{'name' => "true", 'desc' => "{common.true}"},
66 {'name' => "false", 'desc' => "{common.false}"}],
67 'deft' => "true",
68 'reqd' => "no" },
69 { 'name' => "screenviewsize",
70 'desc' => "{ImageConverter.screenviewsize}",
71 'type' => "int",
72 'deft' => "500",
73 'range' => "1,",
74 'reqd' => "no" },
75 { 'name' => "screenviewtype",
76 'desc' => "{ImageConverter.screenviewtype}",
77 'type' => "string",
78 'deft' => "jpg",
79 'reqd' => "no" },
80 { 'name' => "converttotype",
81 'desc' => "{ImageConverter.converttotype}",
82 'type' => "string",
83 'deft' => "",
84 'reqd' => "no" },
85 { 'name' => "minimumsize",
86 'desc' => "{ImageConverter.minimumsize}",
87 'type' => "int",
88 'deft' => "100",
89 'range' => "1,",
90 'reqd' => "no" },
91 { 'name' => "cache_generated_images",
92 'desc' => "{ImageConverter.cache_generated_images}",
93 'type' => "flag",
94 'reqd' => "no",
95 'hiddengli' => "yes" # not yet implemented
96 }
97 ];
98
99my $options = { 'name' => "ImageConverter",
100 'desc' => "{ImageConverter.desc}",
101 'abstract' => "yes",
102 'inherits' => "yes",
103 'args' => $arguments };
104
105sub new {
106 my ($class) = shift (@_);
107 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
108 push(@$pluginlist, $class);
109
110 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
111 push(@{$hashArgOptLists->{"OptList"}},$options);
112
113 my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists, 1);
114
115 return bless $self, $class;
116
117}
118
119# needs to be called after BasePlugin init, so that outhandle is set up.
120sub init {
121 my $self = shift(@_);
122
123 $self->{'tmp_file_paths'} = ();
124
125 # Check that ImageMagick is installed and available on the path
126 my $image_conversion_available = 1;
127 my $no_image_conversion_reason = "";
128 # None of this works very well on Windows 95/98...
129 if ($ENV{'GSDLOS'} eq "windows" && !Win32::IsWinNT()) {
130 $image_conversion_available = 0;
131 $no_image_conversion_reason = "win95notsupported";
132 } else {
133 my $result = `identify 2>&1`;
134 if ($? == -1 || $? == 256) { # Linux and Windows return different values for "program not found"
135 $image_conversion_available = 0;
136 $no_image_conversion_reason = "imagemagicknotinstalled";
137 }
138 }
139 $self->{'image_conversion_available'} = $image_conversion_available;
140 $self->{'no_image_conversion_reason'} = $no_image_conversion_reason;
141
142 if ($self->{'image_conversion_available'} == 0) {
143 my $outhandle = $self->{'outhandle'};
144 &gsprintf($outhandle, "ImageConverter: {ImageConverter.noconversionavailable} ({ImageConverter.".$self->{'no_image_conversion_reason'}."})\n");
145 }
146
147}
148
149
150# convert image to new type if converttotype is set
151# generate thumbnails if required
152# generate screenview if required
153# discover image metadata
154# filename_no_path must be in utf8
155sub generate_images {
156 my $self = shift(@_);
157
158 my ($filename_full_path, $filename_no_path, $doc_obj, $section) = @_;
159
160 # check image magick status
161 return 0 if $self->{'image_conversion_available'} == 0;
162 # check the filenames
163 return 0 if ($filename_no_path eq "" || !-f $filename_full_path);
164
165 my $verbosity = $self->{'verbosity'};
166 my $outhandle = $self->{'outhandle'};
167
168 # check the size of the image against minimum size if specified
169 my $minimumsize = $self->{'minimumsize'};
170 if (defined $minimumsize && (-s $filename_full_path < $minimumsize)) {
171 print $outhandle "ImageConverter: \"$filename_full_path\" too small, skipping\n"
172 if ($verbosity > 1);
173 return 0; # or is there a better return value??
174 }
175
176 my $filehead = $filename_no_path;
177 $filehead =~ s/\.([^\.]*)$//; # filename with no extension
178 my $assocfilemeta = "[assocfilepath]";
179 if ($section ne $doc_obj->get_top_section()) {
180 $assocfilemeta = "[parent(Top):assocfilepath]";
181 }
182
183 # Convert the image to a new type (if required).
184 my $converttotype = $self->{'converttotype'};
185 my $type = "unknown";
186
187 if ($converttotype ne "" && $filename_full_path !~ m/$converttotype$/) {
188
189 my $result = $self->convert($filename_full_path, $converttotype, "", "");
190 ($filename_full_path) = ($result =~ /=>(.*\.$converttotype)/);
191
192 $type = $converttotype;
193 $filename_no_path = "$filehead.$type";
194 }
195
196 # add Image metadata
197 $doc_obj->add_utf8_metadata($section, "Image", $filename_no_path);
198
199 # here we overwrite the original with the potentially converted one
200 $doc_obj->set_utf8_metadata_element($section, "Source", $filename_no_path);
201
202 # use identify to get info about the (possibly converted) image
203 my ($image_type, $image_width, $image_height, $image_size)
204 = &identify($filename_full_path, $outhandle, $verbosity);
205
206 if ($image_type ne " ") {
207 $type = $image_type;
208 }
209
210 #overwrite the ones added in BasePlugin
211 $doc_obj->set_metadata_element ($section, "FileFormat", $type);
212 $doc_obj->set_metadata_element ($section, "FileSize", $image_size);
213
214 $doc_obj->add_metadata ($section, "ImageType", $image_type);
215 $doc_obj->add_metadata ($section, "ImageWidth", $image_width);
216 $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
217 $doc_obj->add_metadata ($section, "ImageSize", $image_size);
218
219 $doc_obj->add_metadata ($section, "srclink", "<a href=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Image]\">");
220 $doc_obj->add_metadata ($section, "/srclink", "</a>");
221 $doc_obj->add_metadata ($section, "srcicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Image]\" width=\"100\">");
222
223 # Add the image as an associated file
224 $doc_obj->associate_file($filename_full_path, $filename_no_path, "image/$type", $section);
225
226 if ($self->{'create_thumbnail'} eq "true") {
227 $self->create_thumbnail($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta);
228 }
229 if ($self->{'create_screenview'} eq "true") {
230 $self->create_screenview($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta);
231 }
232}
233
234sub create_thumbnail {
235 my $self = shift(@_);
236 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta) = @_;
237
238 my $thumbnailsize = $self->{'thumbnailsize'};
239 my $thumbnailtype = $self->{'thumbnailtype'};
240
241 # Generate the thumbnail with convert
242 my $result = $self->convert($original_file, $thumbnailtype, "-geometry $thumbnailsize" . "x$thumbnailsize", "THUMB");
243 my ($thumbnailfile) = ($result =~ /=>(.*\.$thumbnailtype)/);
244
245 # Add the thumbnail as an associated file ...
246 if (-e "$thumbnailfile") {
247 $doc_obj->associate_file("$thumbnailfile", $filehead."_thumb.$thumbnailtype",
248 "image/$thumbnailtype",$section);
249 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
250 $doc_obj->add_utf8_metadata ($section, "Thumb", $filehead."_thumb.$thumbnailtype");
251
252 $doc_obj->add_metadata ($section, "thumbicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Thumb]\" width=[ThumbWidth] height=[ThumbHeight]>");
253
254
255 # Extract Thumbnail metadata from convert output
256 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
257 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
258 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
259 }
260 } else {
261 my $outhandle = $self->{'outhandle'};
262 print $outhandle "Couldn't find thumbnail $thumbnailfile\n";
263
264 }
265}
266
267sub create_screenview {
268
269 my $self = shift(@_);
270 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta) = @_;
271
272 # To do: if the actual image smaller than the screenview size,
273 # we should use the original !
274
275 my $screenviewsize = $self->{'screenviewsize'};
276 my $screenviewtype = $self->{'screenviewtype'};
277
278 # make the screenview image
279 my $result = $self->convert($original_file, $screenviewtype, "-geometry $screenviewsize" . "x$screenviewsize", "SCREEN");
280 my ($screenviewfilename) = ($result =~ /=>(.*\.$screenviewtype)/);
281
282
283 #add the screenview as an associated file ...
284 if (-e "$screenviewfilename") {
285 $doc_obj->associate_file("$screenviewfilename", $filehead."_screen.$screenviewtype", "image/$screenviewtype",$section);
286 $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
287 $doc_obj->add_utf8_metadata ($section, "Screen", $filehead."_screen.$screenviewtype");
288
289 $doc_obj->add_metadata ($section, "screenicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Screen]\" width=[ScreenWidth] height=[ScreenHeight]>");
290
291 # get screenview dimensions, size and type
292 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
293 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
294 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
295 } elsif ($result =~ m/([0-9]+)x([0-9]+)/) {
296 #if the image hasn't changed size, the previous regex doesn't match
297 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
298 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
299 }
300 } else {
301 my $outhandle = $self->{'outhandle'};
302 print $outhandle "Couldn't find screenview file $screenviewfilename\n";
303
304 }
305
306}
307
308
309
310sub convert {
311 my $self = shift(@_);
312 my $source_file_path = shift(@_);
313 my $target_file_type = shift(@_);
314 my $convert_options = shift(@_) || "";
315 my $convert_type = shift(@_) || "";
316
317 my $outhandle = $self->{'outhandle'};
318 my $verbosity = $self->{'verbosity'};
319
320 # Determine the full name and path of the output file
321 my $filehead = &util::get_tmp_filename();
322 my $target_file_path = $filehead . "." . $target_file_type;
323 push(@{$self->{'tmp_file_paths'}}, $target_file_path);
324
325 # Generate and run the convert command
326 my $convert_command = "convert -interlace plane -verbose $convert_options \"$source_file_path\" \"$target_file_path\"";
327 print $outhandle "$convert_type $convert_command\n" if ($verbosity > 2);
328 my $result = `$convert_command 2>&1`;
329 print $outhandle "$convert_type RESULT = $result\n" if ($verbosity > 2);
330
331 return $result;
332}
333
334
335# Discover the characteristics of an image file with the ImageMagick
336# "identify" command.
337
338sub identify {
339 my ($image, $outhandle, $verbosity) = @_;
340
341 # Use the ImageMagick "identify" command to get the file specs
342 my $command = "identify \"$image\" 2>&1";
343 print $outhandle "$command\n" if ($verbosity > 2);
344 my $result = '';
345 $result = `$command`;
346 print $outhandle "$result\n" if ($verbosity > 3);
347
348 # Read the type, width, and height
349 my $type = 'unknown';
350 my $width = 'unknown';
351 my $height = 'unknown';
352
353 my $image_safe = quotemeta $image;
354 if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
355 $type = $1;
356 $width = $2;
357 $height = $3;
358 }
359
360 # Read the size
361 my $size = "unknown";
362 if ($result =~ m/^.* ([0-9]+)b/) {
363 $size = $1;
364 }
365 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?kb?/) {
366 $size = 1024 * $1;
367 if (defined($2)) {
368 $size = $size + (1024 * $2);
369 # Truncate size (it isn't going to be very accurate anyway)
370 $size = int($size);
371 }
372 }
373 elsif ($result =~ m/^.* (([0-9]+)(\.([0-9]+))?e\+([0-9]+))(kb|b)?/) {
374 # Deals with file sizes on Linux of type "3.4e+02kb" where e+02 is 1*10^2.
375 # 3.4e+02 therefore evaluates to 3.4 x 1 x 10^2 = 340kb.
376 # Programming languages including Perl know how that 3.4e+02 is a number,
377 # so we don't need to do any calculations.
378 $size = $1*1; # turn the string into a number by multiplying it by 1
379 #if we did $size = $1; $size would be merely the string "3.4e+02"
380 $size = int($size); # truncate size
381 }
382 print $outhandle "file: $image:\t $type, $width, $height, $size\n"
383 if ($verbosity > 2);
384
385 # Return the specs
386 return ($type, $width, $height, $size);
387}
388
389sub clean_up_temporary_files {
390 my $self = shift(@_);
391
392 foreach my $tmp_file_path (@{$self->{'tmp_file_paths'}}) {
393 if (-e $tmp_file_path) {
394 &util::rm($tmp_file_path);
395 }
396 }
397
398}
399
4001;
Note: See TracBrowser for help on using the repository browser.