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