Index: trunk/gsdl/perllib/plugins/ImagePlug.pm
===================================================================
--- trunk/gsdl/perllib/plugins/ImagePlug.pm	(revision 2229)
+++ trunk/gsdl/perllib/plugins/ImagePlug.pm	(revision 2230)
@@ -32,5 +32,4 @@
 }
 
-use strict;
 
 sub print_usage {
@@ -40,13 +39,20 @@
   usage: plugin ImagePlug [options]
 
-   -noscaleup       Don't scale up small images when making thumbnails
-
-   -thumbnailtype s Make thumbnails in format 's'
-
-   -thumbnailsize n Make thumbnails of size nxn
-
-   -convertto s     Convert main inage to (gif|png|jpg)
-
-   -minimumsize n   Ignore images smaller than n bytes
+   -noscaleup         Don't scale up small images when making thumbnails
+
+   -thumbnailsize n   Make thumbnails of size nxn
+
+   -thumbnailtype s   Make thumbnails in format 's'
+
+   -screenviewsize n  If set, makes an image of size n for screen display
+                      and sets Screen, ScreenSize, ScrrenWidth and Screeneight
+                      metadata.  By default it is not set.
+
+   -screenviewtype s  If -screenviewsize is set, this sets the screen display
+                      image type.  Defaults to jpg.
+
+   -convertto s       Convert main inage to (gif|png|jpg)
+
+   -minimumsize n     Ignore images smaller than n bytes
 
 "
@@ -60,8 +66,11 @@
     if (!parsargv::parse(\@_,
 			 q^noscaleup^, \$self->{'noscaleup'},
+			 q^converttotype/.*/^, \$self->{'converttotype'},
+			 q^minimumsize/[0-9]*/100^, \$self->{'minimumsize'},
+
+			 q^thumbnailsize/[0-9]*/100^, \$self->{'thumbnailsize'},
 			 q^thumbnailtype/.*/gif^, \$self->{'thumbnailtype'},
-			 q^converttotype/.*/^, \$self->{'converttotype'},
-			 q^thumbnailsize/[0-9]*/100^, \$self->{'thumbnailsize'},
-			 q^minimumsize/[0-9]*/100^, \$self->{'minimumsize'},
+			 q^screenviewsize/[0-9]*/0^, \$self->{'screenviewsize'},
+			 q^screenviewtype/.*/jpg^, \$self->{'screenviewtype'},
 			 "allow_extra_options")) {
 	
@@ -81,74 +90,79 @@
 }
 
+
+# Create the thumbnail and screenview images, and discover the Image's
+# size, width, and height using the convert utility.
+
 sub run_convert {
     my $self = shift (@_);
-    my $filename = shift (@_);
-    my $file = shift (@_);
+    my $filename = shift (@_);   # filename with full path
+    my $file = shift (@_);       # filename without path
     my $doc_obj = shift (@_);
     my $section = $doc_obj->get_top_section();
-
-    if ($file eq "" || $filename eq "") {
-	return "";
-    }
-#    if ($filename =~ m/thumbnail/) {
-#	return "";
-#    }
-#    if ($filename =~ m/converted/) {
-#	return "";
-#    }
+    
+    my $verbosity = $self->{'verbosity'};
+    my $outhandle = $self->{'outhandle'};
+
+    # check the filename is okay
+    return 0 if ($file eq "" || $filename eq "");
+
     if ($filename =~ m/ /) {
-	print STDERR "IamgePlug: \"$filename\" contains a space. choking.\n";
-	return "";
+	print $outhandle "ImagePlug: \"$filename\" contains a space. choking.\n";
+	return undef;
     }
 
     my $minimumsize = $self->{'minimumsize'};
-    my $thumbSize = $self->{'thumbnailsize'};
-    if ($thumbSize eq "") { $thumbSize = 100; };
+    if (defined $minimumsize && (-s $filename < $minimumsize)) {
+        print $outhandle "ImagePlug: \"$filename\" too small, skipping\n"
+	    if ($verbosity > 1);
+    }
+
+
+    # Convert the image to a new type (if required).
     my $converttotype = $self->{'converttotype'};
-    my $thumbnailtype = $self->{'thumbnailtype'};
-    if ($thumbnailtype eq "") { $thumbnailtype = "gif"; };
-    my $originalfilename = ""; # only set if we do a conversion
-    my $thumbnailfilename = "";
+    my $originalfilename = "";  # only set if we do a conversion
+    my $type = "unknown";
+
+    if ($converttotype ne "" && $filename =~ m/$converttotype$/) {
+
+	$originalfilename = $filename;
+	$filename = &util::get_tmp_filename() . ".$converttotype";
+	$self->{'tmp_filename'} = $filename;
+
+	my $command = "convert -verbose $originalfilename $filename";
+	print $outhandle "$command\n" if ($verbosity > 2);
+	my $result = '';
+	$result = `$command`;
+	print $outhandle "$result\n" if ($verbosity > 3);
+
+	$type = $converttotype;
+    }
+    
+
+    # Make the thumbnail image
+    my $thumbnailsize = $self->{'thumbnailsize'}  || 100;
+    my $thumbnailtype = $self->{'thumbnailtype'} || 'gif';
 	
-    my $type = "unknown";
-
-    if (defined $minimumsize && (-s $filename < $minimumsize)) {
-        print STDERR "ImagePlug: \"$filename\" too small, skipping\n"
-	    if $self->{'verbosity'} > 1;
-    }
-    #see if we need to convert ...
-    if ($converttotype ne "" && $filename =~ m/$converttotype$/) {
-	$originalfilename = $filename;
-	$filename = &util::get_tmp_filename();
-	$filename = "$filename.$converttotype";
-	$self->{'tmp_filename'} = $filename;
-	if (-e $filename) {
-	    print STDERR "File names to convert from and to are the same. choking in Imageplug on \"$filename\"\n";
-	    return "";
-	    }
-	my $result = "";
-	my $command = "convert -verbose $originalfilename $filename";
-	$result = `$command`;
-	print STDERR "$command\n"
-	    if $self->{'verbosity'} > 2;
-	print STDERR "$result\n"
-	    if $self->{'verbosity'} > 3;
-	$type = $converttotype;
-    }
-    
-    #check that the thumbnail doesn't already exist ...
-    $thumbnailfilename = &util::get_tmp_filename();
-    $thumbnailfilename = $thumbnailfilename . ".thumbnail.$thumbnailtype";
+    my $thumbnailfilename = &util::get_tmp_filename() . ".$thumbnailtype";
     $self->{'tmp_filename2'} = $thumbnailfilename;
 
-    #make the thumbnail
-    my $result = "";
-    my $command = "convert -verbose -geometry $thumbSize" . "x$thumbSize $filename $thumbnailfilename";
+    # Generate the thumbnail with convert
+    my $command = "convert -verbose -geometry $thumbnailsize" 
+	. "x$thumbnailsize $filename $thumbnailfilename";
+    print $outhandle "$command\n" if ($verbosity > 2);
+    my $result = '';
     $result = `$command` ;
-    print STDERR "$command\n"
-	    if $self->{'verbosity'} > 2;
-    print STDERR "$result\n"
-	    if $self->{'verbosity'} > 3;
-    
+    print $outhandle "$result\n" if ($verbosity > 3);
+
+    # Add the thumbnail as an associated file ...
+    if (-e "$thumbnailfilename") { 
+	$doc_obj->associate_file("$thumbnailfilename", "thumbnail.$thumbnailtype", 
+				 "image/$thumbnailtype",$section);
+	$doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
+	$doc_obj->add_metadata ($section, "Thumb", "thumbnail.$thumbnailtype");
+    }
+
+
+    # Extract Image metadata from convert output
     if ($result =~ m/([0-9]+)x([0-9]+)=>([0-9]+)x([0-9]+)/) {
 	$doc_obj->add_metadata ($section, "ImageWidth", $1);
@@ -161,6 +175,5 @@
     if ($result =~ m/^[^\n]* ([0-9]+)b/) {
 	$size = $1;
-    }
-    if ($result =~ m/^[^\n]* ([0-9]+)kb/) {
+    } elsif ($result =~ m/^[^\n]* ([0-9]+)kb/) {
 	$size = 1024 * $1;
     }
@@ -168,41 +181,60 @@
     if ($result =~ m/^[^\n]*JPE?G/i) {
 	$type = "jpeg";
-    }
-    if ($result =~ m/^[^\n]*GIF/i) {
+    } elsif ($result =~ m/^[^\n]*GIF/i) {
 	$type = "gif";
-    }
-    if ($result =~ m/^[^\n]*PNG/i) {
+    } elsif ($result =~ m/^[^\n]*PNG/i) {
 	$type = "png";
-    }
-    if ($result =~ m/^[^\n]*TIF?F/i) {
+    } elsif ($result =~ m/^[^\n]*TIF?F/i) {
 	$type = "tiff";
-    }
-    if ($result =~ m/^[^\n]*BMP/i) {
+    } elsif ($result =~ m/^[^\n]*BMP/i) {
 	$type = "bmp";
-    }
-    if ($result =~ m/^[^\n]*XBM?F/i) {
+    } elsif ($result =~ m/^[^\n]*XBM?F/i) {
 	$type = "xbm";
     }
 
-    #if there's a leading directory name, eat it...
-    $file =~ s/^.*[\/\\]//;
-    
     $doc_obj->add_metadata ($section, "ImageType", $type);
     $doc_obj->add_metadata ($section, "Image", "$file");
     $doc_obj->add_metadata ($section, "ImageSize", $size);
     
-    #add the thumbnail as an associated file ...
-    if (-e "$thumbnailfilename") { 
-	$doc_obj->associate_file("$thumbnailfilename","thumbnail.$thumbnailtype","image/$thumbnailtype",$section);
-	$doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
-	$doc_obj->add_metadata ($section, "Thumb", "thumbnail.$thumbnailtype");
-    } else {
-	print STDERR "ImagePlug: couldn't find \"$thumbnailfilename\"\n";
-    }
-    
-    #add the image as an associated file ...
+    # Add the image as an associated file ...
     $doc_obj->associate_file($filename,$file,"image/$type",$section);
         
+
+    # Make a screen-sized version of the picture if requested
+    if ($self->{'screenviewsize'}) {
+
+	my $screenviewsize = $self->{'screenviewsize'};
+	my $screenviewtype = $self->{'screenviewtype'} || 'jpeg';
+	my $screenviewfilename = &util::get_tmp_filename() . ".$screenviewtype";
+	$self->{'tmp_filename3'} = $screenviewfilename;
+
+	# make the screenview image
+	my $command = "convert -verbose -geometry $screenviewsize" 
+	    . "x$screenviewsize $filename $screenviewfilename";
+	print $outhandle "$command\n" if ($verbosity > 2);
+	my $result = "";
+	$result = `$command` ;
+	print $outhandle "$result\n" if ($verbosity > 3);
+
+	# get screenview dimensions, size and type
+        if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
+	    $doc_obj->add_metadata ($section, "ScreenWidth", $1);
+	    $doc_obj->add_metadata ($section, "ScreenHeight", $2);
+	}
+    
+	#add the screenview as an associated file ...
+	if (-e "$screenviewfilename") { 
+	    $doc_obj->associate_file("$screenviewfilename", "screenview.$screenviewtype",
+				     "image/$screenviewtype",$section);
+	    $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
+	    $doc_obj->add_metadata ($section, "Screen", "screenview.$screenviewtype");
+	} else {
+	    print $outhandle "ImagePlug: couldn't find \"$screenviewfilename\"\n";
+	}
+    }
+
     return $type;
+
+
 }
 
@@ -225,4 +257,6 @@
     my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
 
+    my $outhandle = $self->{'outhandle'};
+
     my $filename = &util::filename_cat($base_dir, $file);
     return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
@@ -230,8 +264,9 @@
 	return undef;
     }
-    print STDERR "ImagePlug processing \"$filename\"\n"
+    print $outhandle "ImagePlug processing \"$filename\"\n"
 	    if $self->{'verbosity'} > 1;
 
-    $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
+    #if there's a leading directory name, eat it...
+    $file =~ s/^.*[\/\\]//;
     
     # create a new document
@@ -269,11 +304,18 @@
     $processor->process($doc_obj);
 
+    # clean up temporary files - we do this here instead of in  
+    # run_convert becuase associated files aren't actually copied 
+    # until after process has been run.
     if (defined $self->{'tmp_filename'} && 
 	-e $self->{'tmp_filename'}) {
-      util::rm($self->{'tmp_filename'})
+      &util::rm($self->{'tmp_filename'})
     }
     if (defined $self->{'tmp_filename2'} && 
 	-e $self->{'tmp_filename2'}) {
-      util::rm($self->{'tmp_filename2'})
+      &util::rm($self->{'tmp_filename2'})
+    }
+    if (defined $self->{'tmp_filename3'} && 
+	-e $self->{'tmp_filename3'}) {
+      &util::rm($self->{'tmp_filename3'})
     }
 
