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

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

added license info

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