/usr/share/perl5/Gscan2pdf/Cuneiform.pm is in gscan2pdf 2.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | package Gscan2pdf::Cuneiform;
use 5.008005;
use strict;
use warnings;
use Carp;
use File::Temp; # To create temporary files
use Gscan2pdf::Document; # for slurp
use version;
use English qw( -no_match_vars ); # for $PROCESS_ID
our $VERSION = '2.1.0';
my $SPACE = q{ };
my $EMPTY = q{};
my ( %languages, $version, $setup, $logger );
# cuneiform language codes
my %iso639 = (
bul => { code => 'bul', name => 'Bulgarian' },
ces => { code => 'cze', name => 'Czech' },
dan => { code => 'dan', name => 'Danish' },
deu => { code => 'ger', name => 'German' },
eng => { code => 'eng', name => 'English' },
est => { code => 'est', name => 'Estonian' },
fra => { code => 'fra', name => 'French' },
hrv => { code => 'hrv', name => 'Croatian' },
hun => { code => 'hun', name => 'Hungarian' },
lav => { code => 'lav', name => 'Latvian' },
lit => { code => 'lit', name => 'Lithuanian' },
nld => { code => 'dut', name => 'Dutch' },
ita => { code => 'ita', name => 'Italian' },
pol => { code => 'pol', name => 'Polish' },
por => { code => 'por', name => 'Portuguese' },
ron => { code => 'rum', name => 'Romanian' },
rus => { code => 'rus', name => 'Russian' },
ruseng => { code => 'ruseng', name => 'Russian+English' },
slk => { code => 'slo', name => 'Slovak' },
slv => { code => 'slv', name => 'Slovenian' },
spa => { code => 'spa', name => 'Spanish' },
srp => { code => 'srp', name => 'Serbian' },
swe => { code => 'swe', name => 'Swedish' },
tur => { code => 'tur', name => 'Turkish' },
ukr => { code => 'ukr', name => 'Ukrainian' },
);
sub setup {
( my $class, $logger ) = @_;
return $version if $setup;
my ( undef, $out, $err ) =
Gscan2pdf::Document::exec_command( [ 'which', 'cuneiform' ] );
return if ( not defined $out or $out eq $EMPTY );
( undef, $out, $err ) = Gscan2pdf::Document::exec_command( ['cuneiform'] );
if ( $out =~ /^Cuneiform[ ]for[ ]Linux[ ]([\d.]+)/xsm ) { $version = $1 }
$setup = 1;
return $version;
}
sub languages {
if ( not %languages ) {
my %cunmap;
for my $key ( keys %iso639 ) {
$cunmap{ $iso639{$key}{code} } = $key;
}
# Dig out supported languages
my ( undef, $output ) =
Gscan2pdf::Document::exec_command( [ 'cuneiform', '-l' ] );
my $langs;
if ( $output =~ /Supported[ ]languages:[ ](.*)[.]/xsm ) {
$langs = $1;
for ( split $SPACE, $langs ) {
if ( defined $cunmap{$_} ) {
$languages{ $cunmap{$_} } = $iso639{ $cunmap{$_} }{name};
}
else {
$languages{$_} = $_;
}
}
}
else {
$logger->info("Unrecognised output from cuneiform: $output");
}
}
return \%languages;
}
sub hocr {
my ( $class, %options ) = @_;
my ($bmp);
if ( not $setup ) { Gscan2pdf::Cuneiform->setup( $options{logger} ) }
# Temporary filename for output
my $txt = File::Temp->new( SUFFIX => '.txt' );
if (
(
version->parse("v$version") < version->parse('v1.1.0')
and $options{file} !~ /[.]bmp$/xsm
)
or ( defined $options{threshold} and $options{threshold} )
)
{
# Temporary filename for new file
$bmp = File::Temp->new( SUFFIX => '.bmp' );
my $image = Image::Magick->new;
$image->Read( $options{file} );
my $x;
if ( defined $options{threshold} and $options{threshold} ) {
$logger->info("thresholding at $options{threshold} to $bmp");
$image->BlackThreshold( threshold => "$options{threshold}%" );
$image->WhiteThreshold( threshold => "$options{threshold}%" );
$x = $image->Quantize( colors => 2 );
$x = $image->Write( depth => 1, filename => $bmp );
}
else {
$logger->info("writing temporary image $bmp");
# Force TrueColor, as this produces DirectClass, which is what cuneiform expects.
# Without this, PseudoClass is often produced, for which cuneiform gives
# "PUMA_XFinalrecognition failed" warnings
$image->Write( filename => $bmp, type => 'TrueColor' );
}
if ("$x") { $logger->warn($x) }
}
else {
$bmp = $options{file};
}
# Map the iso639 language code back to an cuneiform code
if ( defined $iso639{ $options{language} } ) {
$options{language} = $iso639{ $options{language} }{code};
}
my $cmd = "cuneiform -l $options{language} -f hocr -o $txt $bmp";
$logger->info($cmd);
if ( defined $options{pidfile} ) {
system "echo $PROCESS_ID > $options{pidfile};$cmd";
}
else {
system $cmd;
}
return Gscan2pdf::Document::slurp($txt);
}
1;
__END__
|