]> git.donarmstrong.com Git - bin.git/blob - make_djvu
add reset usb bus command
[bin.git] / make_djvu
1 #!/usr/bin/perl
2 # make_djvu converts images to a djvu document
3 # and is released under the terms of the GNU GPL version 3, or any
4 # later version, at your option. See the file README and COPYING for
5 # more information.
6 # Copyright 2014 by Don Armstrong <don@donarmstrong.com>.
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 =head1 NAME
16
17 make_djvu - converts images to a djvu document
18
19 =head1 SYNOPSIS
20
21 make_djvu [options] image1 [image2 ...]
22
23  Options:
24    --output, -o output file (Default out.djvu)
25    --debug, -d debugging level (Default 0)
26    --help, -h display this help
27    --man, -m display manual
28
29 =head1 OPTIONS
30
31 =over
32
33 =item B<--output, -o>
34
35 Output file (default out.djvu)
36
37 =item B<--debug, -d>
38
39 Debug verbosity. (Default 0)
40
41 =item B<--help, -h>
42
43 Display brief usage information.
44
45 =item B<--man, -m>
46
47 Display this manual.
48
49 =back
50
51 =head1 EXAMPLES
52
53 make_djvu
54
55 =cut
56
57
58 use vars qw($DEBUG);
59
60 my %options = (output          => 'out.djvu',
61                debug           => 0,
62                help            => 0,
63                man             => 0,
64               );
65
66 GetOptions(\%options,
67            'output|out|o=s',
68            'debug|d+','help|h|?','man|m');
69
70 pod2usage() if $options{help};
71 pod2usage({verbose=>2}) if $options{man};
72
73 $DEBUG = $options{debug};
74
75 my @USAGE_ERRORS;
76 if (not @ARGV) {
77     push @USAGE_ERRORS,"You must provide at least one image file";
78 }
79
80 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
81
82 sub is_bitonal {
83     my $file = shift;
84     open(my $fh,'-|','identify',$file) or die "Unable to execute identify $file: $!";
85     local $/;
86     my $res = <$fh>;
87     if ($res =~ /Bilevel/) {
88         return 1;
89     }
90     return 0;
91 }
92
93 my @djvu_files;
94 for my $tiff (@ARGV) {
95     my $base_name = $tiff;
96     $base_name =~ s/\.[^\.]+$//;
97     if (is_bitonal($tiff)) {
98         system('cjb2',$tiff,$base_name.'.djvu');
99     } else {
100         system('convert',$tiff,$base_name.'.ppm');
101         system('c44','-dpi',600,$base_name.'.ppm',$base_name.'.djvu');
102     }
103     system('tesseract',$tiff,$base_name.'.ocr');
104     my $fh = IO::File->new($base_name.'.ocr.txt','r');
105     my $wf = IO::File->new($base_name.'.txt','w');
106     print {$wf} "(page 0 0 1 1\n";
107     if (defined $fh) {
108         my $line;
109         while ($line=<$fh>) {
110             $line =~ s/\"/\\\"/g ;
111             print {$wf} "(line 0 0 1 1 \"$line\")\n" ;
112         }
113         close $fh;
114     }
115     print {$wf} ")\n";
116     close $wf;
117     unlink($base_name.'.ocr.txt') if -e $base_name.'.ocr.txt';
118     system('djvused',$base_name.'.djvu','-e','select 1; remove-txt','-s');
119     system('djvused',$base_name.'.djvu','-e','select 1; set-txt '.$base_name.'.txt','-s');
120     unlink($base_name.'.txt') if -e $base_name.'.txt';
121     push @djvu_files,$base_name.'.djvu';
122 }
123
124 system('djvm','-c',$options{output},@djvu_files);
125
126
127
128
129 __END__