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