662d87749a541ea3142f7aceafa0df76ad03903b
[dja/scandal.git] / scan2pages.sh
1 #!/bin/bash
2
3 function usage {
4 cat >&2 << __EOF__ 
5 Usage: $0 [-vfpsmutb] [-D DPI] [-d depth] [-F format] pdffile outdir
6 Convert pdffile - a pdf of scanned facing pages - to a set of images in outdir,
7 each with only one page.
8 OPTIONS:
9         -v: Be verbose.
10         -d depth: Use given depth. Default is 1 for black and white, 8 for colour.
11                 Unpaper can only handle up to 8.
12         -D dpi: Use given DPI. Colour scans on library scanners are usually 300, bw 400.
13         -F extension: use the output format signified by the extension. Default is png
14         -s: skip masking/trimming. Overrides -m.
15         -f: Forceably redo everything.
16         -p: Forceably redo pdf conversion. Implies options below, equivalent to -f.
17         -m: Forceably redo masking/trimming and other preprocessing. Impiles options below.
18         -u: Forceably redo unpaper processing. Implies option below. IGNORED
19         -t: Forceably redo final trimming and cleaning. IGNORED
20         -b: Optimize for BeBook One.
21 __EOF__
22 }
23
24 ## Setup and utilities ##
25 # Mac OS X doesn't have seq. It has jot instead.
26 Linux_seq="seq"
27 Darwin_seq="jot -"
28 if [[ $(uname) == "Darwin" ]]; then my_seq=$Darwin_seq;
29 else my_seq=$Linux_seq;
30 fi;
31
32
33 ## Process flags ##
34 forcepdf=
35 forcemask=
36 forceunpaper=
37 forceclean=
38 convertflags=
39 unpaperflags=
40 depthset=
41 depth=
42 dpi=
43 skipmask=
44 verbose=
45 extension="png"
46 bebook=
47 while getopts 'vd:D:sfpmucF:b' OPTION
48 do
49         case $OPTION in
50         v)      verbose=1
51                 convertflags="$convertflags -verbose"
52                 unpaperflags="$unpaperflags -v --time"
53                 ;;
54         d)      depth="$OPTARG"
55                 ;;
56         D)      dpi=$OPTARG
57                 ;;
58         s)      skipmask=1;
59                 ;;
60         f)      forcepdf=1; forcemask=1; forceunpaper=1; forceclean=1
61                 ;;
62         p)      forcepdf=1; forcemask=1; forceunpaper=1; forceclean=1
63                 ;;
64         m)      forcemask=1; forceunpaper=1; forceclean=1
65                 ;;
66         u)      forceunpaper=1; forceclean=1
67                 ;;
68         t)      forceclean=1
69                 ;;
70         F)      extension="$OPTARG"
71                 ;;
72         b)      bebook=1
73                 ;;
74         ?)      usage
75                 exit 2
76                 ;;
77         esac
78 done
79 shift $(($OPTIND - 1))
80
81 # check we have an input and output!
82 if [[ $# != 2 ]]; then
83         echo "Wrong number of parameters (2 required, $# given: [$@])" >&2
84         usage
85         exit 2
86 fi
87
88 file=$1
89 dir=$2
90
91 filedir=$(dirname $1)
92 base=$(basename $1 .pdf)
93
94 # make the output dir
95 mkdir -p $dir
96
97 # figure out the number of pages
98 dscname=$dir/${base}.dsc
99 pdf2dsc $file $dscname || exit 1
100 pages=$(awk '$1 ~ "%%Pages" {print $2}' $dscname)
101 echo "Got $pages page(s)."
102 rm $dscname
103
104 # guess at DPI, unless it's been set
105 # the scans do not seem to accurately reflect their resolution.
106 if [[ "$dpi" = "" ]]; then
107         # %k gives the number of colours/shades in the image.
108         # 2 for b/w, and the scanner seems to default to 400 dpi for that
109         # 256 for greyscale, which has to be selected manually (so I'm assuming 300 for no good reason)
110         # 31564 for colour, which defaults to 300
111         if [[ $( convert $file[0] -format "%k" info: || exit 1 ) == 2 ]]; then
112                 dpi=400;
113         else
114                 dpi=300
115         fi
116         if [[ $verbose == 1 ]]; then
117                 echo "DPI guessed at $dpi."
118         fi
119 fi
120
121 # guess at depth, unless it's been set
122 if [[ "$depth" = "" ]]; then
123         if [[ $( convert $file[0] -format "%k" info: || exit 1 ) == 2 ]]; then
124                 depth=1;
125         else
126                 depth=8
127         fi
128         if [[ $verbose == 1 ]]; then
129                 echo "Depth guessed at $depth."
130         fi
131 fi
132
133 # process pages 1 by 1 to avoid convert gobbling all the memory
134 for pg in `$my_seq 1 $pages`; do
135         echo "Processing page $pg."
136         
137         pgn=$(printf '%03d' $pg)
138         
139         # convert from pdf
140         origpnm=$dir/pg-${pgn}.pnm
141         if [[ ! $([ -e $origpnm ]) || $forcepdf ]]; then
142                 convert $convertflags -depth $depth -density $dpi $file[$(expr $pg - 1)] \
143                         $origpnm || exit 1
144         fi;
145         
146         # preprocess it!
147         preppnm=$dir/pg-pp-${pgn}.pnm
148         if [[ ! $skipmask && ( ! $([ -e $preppnm ]) || $forcemask ) ]]; then
149                 # create mask: 
150                 # ... downscale, blur,
151                 convert $convertflags -resize 25% -depth 8 -blur 10 $origpnm $dir/pg-mask-${pgn}.pnm ||exit 1
152
153                 # ... get crop co-ords. They're off by ~2 as I don't know how to
154                 # properly correct for the border.
155                 cropcords=$(convert -border 1x1 -bordercolor '#000' -resize 400% -trim -fuzz 90% -format "%wx%h%O" $dir/pg-mask-${pgn}.pnm info: || exit 1)
156                 
157                 # ... crop and despeckle? the final pre-prepared image
158                 convert $convertflags -crop $cropcords $origpnm $preppnm || exit 1
159         elif [[ $skipmask ]]; then
160                 cp $origpnm $preppnm
161         fi;
162         
163         #unpaper it
164         #names go a bit funny here
165         #also, ignore flags starting here
166         unppnm=$dir/upg-${pgn}-%01d.pnm
167         unpaper $unpaperflags --layout double --overwrite --no-blackfilter -ni 10 -op 2 $preppnm $unppnm || exit 1
168
169         #final convert and clean w/ bebook optimisation
170         if [[ $bebook ]]; then
171                 convert $convertflags $dir/upg-${pgn}-?.pnm -depth 4 -resize 600x800 $dir/final-${pgn}-%01d.${extension} || exit 1
172         else
173                 convert $convertflags $dir/upg-${pgn}-?.pnm $dir/final-${pgn}-%01d.${extension} || exit 1
174         fi
175         
176 done
177
178 mkdir $dir/pages
179 mv $dir/final-*.${extension} $dir/pages
180         

UCC git Repository :: git.ucc.asn.au