source: trunk/gsdl/perllib/plugins/ImagePlug.pm@ 1758

Last change on this file since 1758 was 1758, checked in by say1, 23 years ago

added minimum image size and a few bug fixes

  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 KB
Line 
1###########################################################################
2#
3# ImagePlug.pm -- simple text plugin
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package ImagePlug;
27
28use BasPlug;
29
30sub BEGIN {
31 @ISA = ('BasPlug');
32}
33
34use strict;
35
36sub print_general_usage {
37 my ($plugin_name) = @_;
38
39 print STDERR "\n usage: plugin $plugin_name [options]\n\n";
40 print STDERR " -noscaleup Don't scale up small images when making thumbnails\n";
41 print STDERR " -thumbnailtype s Make thumbnails in format 's'\n";
42 print STDERR " -thumbnailsize n Make thumbnails of size nxn\n";
43 print STDERR " -convertto s Convert main inage to (gif|png|jpg)\n";
44 print STDERR " -minimumsize n Ignore images smaller than n bytes\n";
45 print STDERR "\n";
46}
47
48sub new {
49 my ($class) = @_;
50 my $plugin_name = shift (@_);
51 my $self = new BasPlug ("ImagePlug", @_);
52 # general options available to all plugins
53
54 if (!parsargv::parse(\@_,
55 q^noscaleup^, \$self->{'noscaleup'},
56 q^thumbnailtype/.*/gif^, \$self->{'thumbnailtype'},
57 q^converttotype/.*/^, \$self->{'converttotype'},
58 q^thumbnailsize/[0-9]*/100^, \$self->{'thumbnailsize'},
59 q^minimumsize/[0-9]*/100^, \$self->{'minimumsize'},
60 "allow_extra_options")) {
61
62 print STDERR "\nThe $plugin_name plugin uses an incorrect general option (general options are those\n";
63 print STDERR "available to all plugins). Check your collect.cfg configuration file.\n";
64 &print_general_usage($plugin_name);
65 die "\n";
66 }
67
68 return bless $self, $class;
69}
70
71sub get_default_process_exp {
72 my $self = shift (@_);
73
74 return q^(?i)(\.jpe?g|\.gif|\.png|\.bmp|\.xbm|\.tif?f)$^;
75}
76
77sub run_convert {
78 my $self = shift (@_);
79 my $filename = shift (@_);
80 my $file = shift (@_);
81 my $doc_obj = shift (@_);
82 my $section = $doc_obj->get_top_section();
83
84 if ($file eq "" || $filename eq "") {
85 return "";
86 }
87# if ($filename =~ m/thumbnail/) {
88# return "";
89# }
90# if ($filename =~ m/converted/) {
91# return "";
92# }
93 if ($filename =~ m/ /) {
94 print STDERR "IamgePlug: \"$filename\" contains a space. choking.\n";
95 return "";
96 }
97
98 my $minimumsize = $self->{'minimumsize'};
99 my $thumbSize = $self->{'thumbnailsize'};
100 if ($thumbSize eq "") { $thumbSize = 100; };
101 my $converttotype = $self->{'converttotype'};
102 my $thumbnailtype = $self->{'thumbnailtype'};
103 if ($thumbnailtype eq "") { $thumbnailtype = "gif"; };
104 my $originalfilename = ""; # only set if we do a conversion
105 my $thumbnailfilename = "";
106
107 my $type = "unknown";
108
109 if (defined $minimumsize && (-s $filename < $minimumsize)) {
110 print STDERR "ImagePlug: \"$filename\" too small, skipping\n"
111 if $self->{'verbosity'} > 1;
112 }
113 #see if we need to convert ...
114 if ($converttotype ne "" && $filename =~ m/$converttotype$/) {
115 $originalfilename = $filename;
116 $filename = &util::get_tmp_filename();
117 $filename = "$filename.$converttotype";
118 $self->{'tmp_filename'} = $filename;
119 if (-e $filename) {
120 print STDERR "File names to convert from and to are the same. choking in Imageplug on \"$filename\"\n";
121 return "";
122 }
123 my $result = "";
124 my $command = "convert -verbose $originalfilename $filename";
125 $result = `$command`;
126 print STDERR "$command\n"
127 if $self->{'verbosity'} > 2;
128 print STDERR "$result\n"
129 if $self->{'verbosity'} > 3;
130 $type = $converttotype;
131 }
132
133 #check that the thumbnail doesn't already exist ...
134 $thumbnailfilename = &util::get_tmp_filename();
135 $thumbnailfilename = $thumbnailfilename . ".thumbnail.$thumbnailtype";
136 $self->{'tmp_filename2'} = $thumbnailfilename;
137
138 #make the thumbnail
139 my $result = "";
140 my $command = "convert -verbose -geometry $thumbSize" . "x$thumbSize $filename $thumbnailfilename";
141 $result = `$command` ;
142 print STDERR "$command\n"
143 if $self->{'verbosity'} > 2;
144 print STDERR "$result\n"
145 if $self->{'verbosity'} > 3;
146
147 if ($result =~ m/([0-9]+)x([0-9]+)=>([0-9]+)x([0-9]+)/) {
148 $doc_obj->add_metadata ($section, "ImageHeight", $1);
149 $doc_obj->add_metadata ($section, "ImageWidth", $2);
150 $doc_obj->add_metadata ($section, "ThumbHeight", $3);
151 $doc_obj->add_metadata ($section, "ThumbWidth", $4);
152 }
153
154 my $size = "unknown";
155 if ($result =~ m/^[^\n]* ([0-9]+)b/) {
156 $size = $1;
157 }
158 if ($result =~ m/^[^\n]* ([0-9]+)kb/) {
159 $size = 1024 * $1;
160 }
161
162 if ($result =~ m/^[^\n]*JPE?G/i) {
163 $type = "jpeg";
164 }
165 if ($result =~ m/^[^\n]*GIF/i) {
166 $type = "gif";
167 }
168 if ($result =~ m/^[^\n]*PNG/i) {
169 $type = "png";
170 }
171 if ($result =~ m/^[^\n]*TIF?F/i) {
172 $type = "tiff";
173 }
174 if ($result =~ m/^[^\n]*BMP/i) {
175 $type = "bmp";
176 }
177 if ($result =~ m/^[^\n]*XBM?F/i) {
178 $type = "xbm";
179 }
180
181 #if there's a leading directory name, eat it...
182 $file =~ s/^[^\/\\]*[\/\\]//;
183
184 $doc_obj->add_metadata ($section, "ImageType", $type);
185 $doc_obj->add_metadata ($section, "Image", "$file");
186 $doc_obj->add_metadata ($section, "ImageSize", $size);
187
188 #add the thumbnail as an associated file ...
189 if (-e "$thumbnailfilename") {
190 $doc_obj->associate_file("$thumbnailfilename","thumbnail.$thumbnailtype","image/$thumbnailtype",$section);
191 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
192 $doc_obj->add_metadata ($section, "Thumb", "thumbnail.$thumbnailtype");
193 } else {
194 print STDERR "ImagePlug: couldn't find \"$thumbnailfilename\"\n";
195 }
196
197 #add the image as an associated file ...
198 $doc_obj->associate_file($filename,$file,"image/$type",$section);
199
200 return $type;
201}
202
203
204# The ImagePlug read() function. This function does all the right things
205# to make general options work for a given plugin. It calls the process()
206# function which does all the work specific to a plugin (like the old
207# read functions used to do). Most plugins should define their own
208# process() function and let this read() function keep control.
209#
210# ImagePlug overrides read() because there is no need to read the actual
211# text of the file in, because the contents of the file is not text...
212#
213# Return number of files processed, undef if can't process
214# Note that $base_dir might be "" and that $file might
215# include directories
216
217sub read {
218 my $self = shift (@_);
219 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
220
221 my $filename = &util::filename_cat($base_dir, $file);
222 return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
223 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
224 return undef;
225 }
226 print STDERR "ImagePlug: called on \"$filename\"\n"
227 if $self->{'verbosity'} > 1;
228
229 $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
230
231 # create a new document
232 my $doc_obj = new doc ($filename, "indexed_doc");
233
234 #run convert to get the thumbnail and extract size and type info
235 my $result = run_convert($self, $filename, $file, $doc_obj);
236
237 if (!defined $result)
238 {
239 print "ImagePlug: couldn't process \"$filename\"\n";
240 return 0;
241 }
242
243 #create an empty text string so we don't break downstream plugins
244 my $text = "";
245
246 # include any metadata passed in from previous plugins
247 # note that this metadata is associated with the top level section
248 my $section = $doc_obj->get_top_section();
249 $self->extra_metadata ($doc_obj, $section, $metadata);
250
251 # do plugin specific processing of doc_obj
252 return undef unless defined ($self->process (\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj));
253
254 # do any automatic metadata extraction
255 $self->auto_extract_metadata ($doc_obj);
256
257 # add an OID
258 $doc_obj->set_OID();
259
260# $doc_obj->add_text($section, "<pre>\n$text\n</pre>");
261
262 # process the document
263 $processor->process($doc_obj);
264
265 if (defined $self->{'tmp_filename'} &&
266 -e $self->{'tmp_filename'}) {
267 util::rm($self->{'tmp_filename'})
268 }
269 if (defined $self->{'tmp_filename2'} &&
270 -e $self->{'tmp_filename2'}) {
271 util::rm($self->{'tmp_filename2'})
272 }
273
274 return $result; # processed the file
275}
276
277# do plugin specific processing of doc_obj
278sub process {
279 my $self = shift (@_);
280 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
281 my $outhandle = $self->{'outhandle'};
282
283 return 1;
284}
285
2861;
287
288
289
290
291
292
293
294
295
296
297
Note: See TracBrowser for help on using the repository browser.