Index: gs2-extensions/ocr/trunk/perllib/plugins/OCRImageConverter.pm
===================================================================
--- gs2-extensions/ocr/trunk/perllib/plugins/OCRImageConverter.pm	(revision 30200)
+++ gs2-extensions/ocr/trunk/perllib/plugins/OCRImageConverter.pm	(revision 30201)
@@ -4,4 +4,5 @@
 
 use strict;
+use warnings;
 no strict 'refs'; # allow filehandles to be variables and viceversa
 no strict 'subs'; # allow barewords (eg STDERR) as function arguments
@@ -17,5 +18,5 @@
     @OCRImageConverter::ISA = ('BaseMediaConverter');
 
-    # Check that PDFBox is installed and available on the path 
+    # Check that Tesseract is installed and available on the path 
     $ocrimage_conversion_available = 1;
     
@@ -55,9 +56,60 @@
 	        } 
 	    }
-    }
-
-}
-
-my $arguments = [ ];
+
+        my $cpan = &FileUtils::filenameConcatenate($gextocr_home, "installed", "cmdline", "cpan", "lib64", "perl5");
+        &gsprintf(STDERR, "OCRImageConverter: CPAN directory $cpan\n");
+        if (! -d $cpan) {
+            $ocrimage_conversion_available = 0;
+            $no_ocrimage_conversion_reason = "gexthtmltokenotinstalled";
+        } else {
+            push(@INC, $cpan);
+        }
+    }
+}
+
+use HTML::TokeParser;
+
+my $layout_list = [
+    {
+        'name' => "none",
+        'desc' => "{OCRImageConverter.unpaper_layout.none}"
+    },
+    {
+        'name' => "single",
+        'desc' => "{OCRImageConverter.unpaper_layout.single}"
+    },
+    {
+        'name' => "double",
+        'desc' => "{OCRImageConverter.unpaper_layout.double}"
+    }
+];
+
+my $arguments = [
+    {
+        'name' => 'use_unpaper',
+        'desc' => "{OCRImageConverter.use_unpaper}",
+        'type' => "flag",
+        'deft' => 0,
+        'reqd' => "no"
+    },
+    {
+        'name' => 'unpaper_layout',
+        'desc' => "{OCRImageConverter.unpaper_layout}",
+        'type' => "enum",
+        'reqd' => "yes",
+        'list' => $layout_list,
+        'deft' => "none"
+    }
+];
+
+my $opt_diva_args = [
+    {
+        'name' => 'diva_support',
+        'desc' => "{OCRImageConverter.diva_support}",
+        'type' => "flag",
+        'deft' => "yes",
+        'reqd' => "no"
+    }
+];
 
 my $options = {
@@ -74,7 +126,10 @@
     push(@$pluginlist, $class);
 
+    if (defined $ENV{'GEXTDIVA'}) {
+        push(@$arguments, @$opt_diva_args);
+    }
+
     push(@{$hashArgOptLists->{"ArgList"}}, @{$arguments});
     push(@{$hashArgOptLists->{"OptList"}}, $options);
-
 
     my $self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists, $auxilary);
@@ -121,11 +176,14 @@
     my ($source_file_full_path) = @_;
     
-    print STDERR "***** WE ARE WORKING *****\n";
-    
     # TODO cache
     if (lc(substr($source_file_full_path, length($source_file_full_path) - 4)) eq ".gif") {
+        my $desired_extension = "tif";
+        if ($self->{'use_unpaper'}) {
+            $desired_extension = "pnm";
+        }
+        
         # need to convert to another format first
         #my $target_source = substr($source_file_full_path, length($source_file_full_path) - 4) . ".tif";
-        my $converted_file = &util::get_tmp_filename("tif");
+        my $converted_file = &util::get_tmp_filename($desired_extension);
         push(@{$self->{'ocrtmp_file_paths'}}, $converted_file);
         
@@ -134,4 +192,10 @@
         
         print STDERR "*** Magick command: $imagick_cmd\n";
+        
+        if ($self->{'use_unpaper'}) {
+            my $unpaper_cmd = "unpaper \"$converted_file\" \"$converted_file\" --overwrite --layout " . $self->{'unpaper_layout'};
+            system($unpaper_cmd);
+            print STDERR "*** unpaper command: $unpaper_cmd\n";
+        }
         
         $source_file_full_path = $converted_file;
@@ -175,4 +239,67 @@
     
     my ($regenerated, $result, $had_error) = $self->autorun_general_cmd($convert_cmd, $source_file_full_path, $target_file_path, $print_info);
+    $self->autorun_general_cmd($convert_cmd . " hocr", $source_file_full_path, $target_file_path, $print_info);
+    
+    # HOCR
+    my $hocr = substr($target_file_path, 0, length($target_file_path) - 4) . ".hocr";
+    
+    my $json = "[";
+
+    open(my $fh, $hocr) || die "Can't open HOCR file $hocr!";
+    #open(my $fh, $hocr);
+    my $parser = HTML::TokeParser->new($fh);
+    
+    my $state = "idle";
+    my $bbox = "";
+    my $text = "";
+    
+    while (my $token = $parser->get_token()) {
+        if ($state eq "idle" && $token->[0] eq "S" && $token->[1] eq "span") {
+            $state = "inSpan";
+            $bbox = $token->[2]{'title'};
+        }
+        
+        if ($state eq "inSpan" && $token->[0] eq "T") {
+#            $state = "gotText";
+            $text = $token->[1];
+            $state = "gotText";
+            chomp $text;
+        }
+        
+        if ($state eq "gotText" && $token->[0] eq "E") {
+            $state = "idle";
+            #next unless $text =~ /\w/;
+            
+            my @numbers = $bbox =~ /(\d+)/g;
+            $json .= "[\"$text\",[${numbers[0]},${numbers[1]},${numbers[2]},${numbers[3]}]],";
+        }
+    }
+
+    close($fh);
+
+    $json = substr($json, 0, length($json) - 1); # remove trailing comma
+    $json .= "]";
+
+    open(my $oh, '>', $hocr . '.json');
+    print $oh "$json";
+    close($oh);
+    
+    # Diva.js
+    #if (defined $EXT{'GEXTDIVA'}) {
+    #    if ($self->{'diva_support'}) {
+    #        my $webroot = &FileUtils::filenameConcatenate($ENV{'GSDL3HOME'}, '..', 'packages', 'tomcat', 'webapps', 'iipsrv', 'collect', '<HASH>');
+    #        my $processpy = &FileUtils::filenameConcatenate($ENV{'GEXTDIVA_INSTALLED'}, 'cmdline', 'python');
+    #        my $DS = &util::get_dirsep();
+    #        my $convert = `which convert`;
+    #        
+    #        $print_info = {
+    #            'message_prefix' => "Diva.js processing",
+    #            'message' => "Now processing images for use with Diva.js..."
+    #        };
+    #
+    #        my $process_cmd = "python \"$processpy\" \"<IN>\" \"$webroot${DS}img\" \"$webroot${DS}data\" -t tiff -i \"$convert\"";
+    #        $self->autorun_general_cmd($process_cmd, "<IN>", $print_info);
+    #    }
+    #}
     
     if ($had_error) {
@@ -181,4 +308,9 @@
         return (1, $result, $target_file_path);
     }
+}
+
+sub test {
+    my $self = shift(@_);
+    print STDERR "**** test working\n";
 }
 
@@ -210,3 +342,3 @@
 }
 
-1;	
+1;
