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 | sub new {
|
---|
35 | my ($class) = @_;
|
---|
36 | my $self = new BasPlug ("ImagePlug", @_);
|
---|
37 |
|
---|
38 | return bless $self, $class;
|
---|
39 | }
|
---|
40 |
|
---|
41 | sub get_default_process_exp {
|
---|
42 | my $self = shift (@_);
|
---|
43 |
|
---|
44 | return q^(?i)(\.jpe?g|\.gif|\.png|\.tiff?)$^;
|
---|
45 | }
|
---|
46 |
|
---|
47 | sub run_convert {
|
---|
48 | my $self = shift (@_);
|
---|
49 | my $filename = shift (@_);
|
---|
50 | my $file = shift (@_);
|
---|
51 | my $doc_obj = shift (@_);
|
---|
52 |
|
---|
53 | my $type = "unknown";
|
---|
54 |
|
---|
55 | if (-e "$filename.thumbnail.gif") {
|
---|
56 | print "ImagePlug: both \"$filename\" and \"$filename.thumbnail.gif\" exist.\n";
|
---|
57 | return undef;
|
---|
58 | }
|
---|
59 |
|
---|
60 | my $result = "";
|
---|
61 | $result = `convert -verbose -loop 1 -delay 1 -geometry 100x100 $filename $filename.thumbnail.gif` ;
|
---|
62 | my $section = $doc_obj->get_top_section();
|
---|
63 |
|
---|
64 | if ($result =~ m/=>.* ([0-9]+)x([0-9]+)=>([0-9]+)x([0-9]+)/) {
|
---|
65 | $doc_obj->add_metadata ($section, "ImageHeight", $1);
|
---|
66 | $doc_obj->add_metadata ($section, "ImageWidth", $2);
|
---|
67 | $doc_obj->add_metadata ($section, "ThumbHeight", $3);
|
---|
68 | $doc_obj->add_metadata ($section, "ThumbWidth", $4);
|
---|
69 | }
|
---|
70 | my $size = "";
|
---|
71 | if ($result =~ m/^[^\n]* ([0-9]+)b/) {
|
---|
72 | $size = $1;
|
---|
73 | }
|
---|
74 | if ($result =~ m/^[^\n]* ([0-9]+)kb/) {
|
---|
75 | $size = 1024 * $1;
|
---|
76 | }
|
---|
77 |
|
---|
78 | if ($result =~ m/^[^\n]*JPE?G/) {
|
---|
79 | $type = "jpeg";
|
---|
80 | }
|
---|
81 | if ($result =~ m/^[^\n]*GIF/) {
|
---|
82 | $type = "gif";
|
---|
83 | }
|
---|
84 | if ($result =~ m/^[^\n]*PNG/) {
|
---|
85 | $type = "png";
|
---|
86 | }
|
---|
87 | if ($result =~ m/^[^\n]*TIFF?/) {
|
---|
88 | $type = "tiff";
|
---|
89 | }
|
---|
90 |
|
---|
91 | if ($type ne "unknown") {
|
---|
92 | $doc_obj->add_metadata ($section, "ImageType", $type);
|
---|
93 | }
|
---|
94 | $doc_obj->add_metadata ($section, "ThumbType", "gif");
|
---|
95 | $doc_obj->add_metadata ($section, "Image", "$file");
|
---|
96 | $doc_obj->add_metadata ($section, "Thumb", "$file.thumbnail.gif");
|
---|
97 | if ($size != 0) {
|
---|
98 | $doc_obj->add_metadata ($section, "ImageSize", $size);
|
---|
99 | }
|
---|
100 | print $result;
|
---|
101 | return $type;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | # The ImagePlug read() function. This function does all the right things
|
---|
106 | # to make general options work for a given plugin. It calls the process()
|
---|
107 | # function which does all the work specific to a plugin (like the old
|
---|
108 | # read functions used to do). Most plugins should define their own
|
---|
109 | # process() function and let this read() function keep control.
|
---|
110 | #
|
---|
111 | # ImagePlug overrides read() because there is no need to read the actual
|
---|
112 | # text of the file in, because the contents of the file is not text...
|
---|
113 | #
|
---|
114 | # Return number of files processed, undef if can't process
|
---|
115 | # Note that $base_dir might be "" and that $file might
|
---|
116 | # include directories
|
---|
117 |
|
---|
118 | sub read {
|
---|
119 | my $self = shift (@_);
|
---|
120 | my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
|
---|
121 |
|
---|
122 | my $filename = &util::filename_cat($base_dir, $file);
|
---|
123 | return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
|
---|
124 | if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
|
---|
125 | return undef;
|
---|
126 | }
|
---|
127 | my $plugin_name = ref ($self);
|
---|
128 | $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
|
---|
129 |
|
---|
130 | # create a new document
|
---|
131 | my $doc_obj = new doc ($filename, "indexed_doc");
|
---|
132 |
|
---|
133 | #run convert to get the thumbnail and extract size and type info
|
---|
134 | my $mimetype = run_convert($self, $filename, $file, $doc_obj);
|
---|
135 |
|
---|
136 | #add the image as an associated file ...
|
---|
137 | if (-e "$filename.thumbnail.gif") {
|
---|
138 | $doc_obj->associate_file("$filename.thumbnail.gif","$file.thumbnail.gif","image/gif",$section);
|
---|
139 | } else {
|
---|
140 | print "ImagePlug: couldn't find \"$filename.thumbnail.gif\"\n";
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (defined $mimetype) {
|
---|
144 | $doc_obj->associate_file($filename,$file,"image/$mimetype",$section);
|
---|
145 | }
|
---|
146 |
|
---|
147 | #create an empty text string so we don't break downstream plugins
|
---|
148 | my $text = "";
|
---|
149 |
|
---|
150 | # include any metadata passed in from previous plugins
|
---|
151 | # note that this metadata is associated with the top level section
|
---|
152 | $self->extra_metadata ($doc_obj, $section, $metadata);
|
---|
153 |
|
---|
154 | # do plugin specific processing of doc_obj
|
---|
155 | return undef unless defined ($self->process (\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj));
|
---|
156 |
|
---|
157 | # do any automatic metadata extraction
|
---|
158 | $self->auto_extract_metadata ($doc_obj);
|
---|
159 |
|
---|
160 | # add an OID
|
---|
161 | $doc_obj->set_OID();
|
---|
162 |
|
---|
163 | # $doc_obj->add_text($section, "<pre>\n$text\n</pre>");
|
---|
164 |
|
---|
165 | # process the document
|
---|
166 | $processor->process($doc_obj);
|
---|
167 |
|
---|
168 | if (-e "$filename.thumbnail.gif") {
|
---|
169 | util::rm("$filename.thumbnail.gif");
|
---|
170 | }
|
---|
171 |
|
---|
172 | return 1; # processed the file
|
---|
173 | }
|
---|
174 |
|
---|
175 | # do plugin specific processing of doc_obj
|
---|
176 | sub process {
|
---|
177 | my $self = shift (@_);
|
---|
178 | my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
|
---|
179 | my $outhandle = $self->{'outhandle'};
|
---|
180 |
|
---|
181 | return 1;
|
---|
182 | }
|
---|
183 |
|
---|
184 | 1;
|
---|
185 |
|
---|
186 |
|
---|
187 |
|
---|
188 |
|
---|
189 |
|
---|
190 |
|
---|
191 |
|
---|
192 |
|
---|
193 |
|
---|
194 |
|
---|
195 |
|
---|