6d232f5ddc14b42f69cf9ca015b4e05d91fb0c70
[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/gray.
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.
19         -t: Forceably redo final trimming and cleaning. IGNORED
20         -b: Optimize for BeBook One.
21 __EOF__
22 }
23
24 ## Setup and utilities ##
25 source $(dirname $0)/my_seq.sh
26
27 ## Process flags ##
28 forcepdf=
29 forcemask=
30 forceunpaper=
31 forceclean=
32 convertflags=
33 unpaperflags=
34 depthset=
35 depth=
36 dpi=
37 skipmask=
38 verbose=
39 extension="png"
40 bebook=
41 while getopts 'vd:D:sfpmucF:b' OPTION
42 do
43         case $OPTION in
44         v)      verbose=1
45                 convertflags="$convertflags -verbose"
46                 unpaperflags="$unpaperflags -v --time"
47                 ;;
48         d)      depth="$OPTARG"
49                 ;;
50         D)      dpi=$OPTARG
51                 ;;
52         s)      skipmask=1;
53                 ;;
54         f)      forcepdf=1; forcemask=1; forceunpaper=1; forceclean=1
55                 ;;
56         p)      forcepdf=1; forcemask=1; forceunpaper=1; forceclean=1
57                 ;;
58         m)      forcemask=1; forceunpaper=1; forceclean=1
59                 ;;
60         u)      forceunpaper=1; forceclean=1
61                 ;;
62         t)      forceclean=1
63                 ;;
64         F)      extension="$OPTARG"
65                 ;;
66         b)      bebook=1
67                 ;;
68         ?)      usage
69                 exit 2
70                 ;;
71         esac
72 done
73 shift $(($OPTIND - 1))
74
75 # check we have an input and output!
76 if [[ $# != 2 ]]; then
77         echo "Wrong number of parameters (2 required, $# given: [$@])" >&2
78         usage
79         exit 2
80 fi
81
82 file=$1
83 dir=$2
84
85 filedir=$(dirname $1)
86 base=$(basename $1 .pdf)
87
88 # make the output dir
89 mkdir -p $dir
90
91 # figure out the number of pages
92 dscname=$dir/${base}.dsc
93 pdf2dsc $file $dscname || exit 1
94 pages=$(awk '$1 ~ "%%Pages" {print $2}' $dscname)
95 echo "Got $pages page(s)."
96 rm $dscname
97
98 # guess at DPI, unless it's been set
99 # the scans do not seem to accurately reflect their resolution.
100 if [[ "$dpi" = "" ]]; then
101         # %k gives the number of colours/shades in the image.
102         # 2 for b/w, and the scanner seems to default to 400 dpi for that
103         # 256 for greyscale, which has to be selected manually (so I'm assuming 300 for no good reason)
104         # 31564 for colour, which defaults to 300
105         if [[ $( convert $file[0] -format "%k" info: || exit 1 ) == 2 ]]; then
106                 dpi=400;
107         else
108                 dpi=300
109         fi
110         if [[ $verbose == 1 ]]; then
111                 echo "DPI guessed at $dpi."
112         fi
113 fi
114
115 # guess at depth, unless it's been set
116 if [[ "$depth" = "" ]]; then
117         if [[ $( convert $file[0] -format "%k" info: || exit 1 ) == 2 ]]; then
118                 depth=1;
119         else
120                 depth=8
121         fi
122         if [[ $verbose == 1 ]]; then
123                 echo "Depth guessed at $depth."
124         fi
125 fi
126
127 # process pages 1 by 1 to avoid convert gobbling all the memory
128 for pg in `$my_seq 1 $pages`; do
129         echo "Processing page $pg."
130         
131         pgn=$(printf '%03d' $pg)
132         
133         # convert from pdf
134         origfile=$dir/pg-${pgn}.png
135         if [ ! -e $origpnm ] || [[ $forcepdf ]]; then
136                 convert $convertflags -depth $depth -density $dpi $file[$(expr $pg - 1)] \
137                         $origfile || exit 1
138         fi;
139         
140         # preprocess it!
141         preppnm=$dir/pg-pp-${pgn}.pnm
142         if [[ ! $skipmask ]] && ( [ ! -e $preppnm ] || [[ $forcemask ]] ); then
143                 # create mask: 
144                 # ... downscale, blur,
145                 convert $convertflags -resize 10% -depth 8 -blur 10 -median 2 $origfile $dir/pg-mask-${pgn}.png ||exit 1
146
147                 # ... get crop co-ords. They're off by ~2 as I don't know how to
148                 # properly correct for the border.
149                 cropcords=$(convert -border 1x1 -bordercolor '#000' -resize 1000% -trim -fuzz 90% -format "%wx%h%O" $dir/pg-mask-${pgn}.png info: || exit 1)
150                 
151                 # ... crop and despeckle? the final pre-prepared image
152                 convert $convertflags -crop $cropcords $origpnm $preppnm || exit 1
153         elif [[ $skipmask ]]; then
154                 cp $origpnm $preppnm
155         fi;
156         
157         # check it hasn't mostly disappeared - e.g. if the scan was all black
158         # (e.g. forgot to put the book down when you first hit scan)
159         if [[ $(convert  $preppnm -format '%[fx:s.w*s.h>1000]' info:) = "0" ]]; then
160                 [[ $verbose == 1 ]] && echo "Discarding pg ${pgn}: not enough remains after masking."
161                 continue; 
162         fi;
163         
164         #unpaper it
165         #names go a bit funny here
166         unppnmbase=$dir/upg-${pgn}
167         if [ ! -e ${unppnmbase}-1.pnm ] || [ ! -e ${unppnmbase}-2.pnm ] || [[ $forceunpaper ]]; then
168                 unpaper $unpaperflags --layout double --overwrite -ni 10 -op 2 $preppnm ${unppnmbase}-%01d.pnm || exit 1
169         fi;
170         
171         #detect if the page is 2-up
172         for subpg in $($my_seq 1 2); do
173                 echo Processing subpg ${subpg}.
174                 if $(dirname $0)/detect2pages.sh $dir/upg-${pgn}-${subpg}.pnm ${pgn} ${subpg}; then
175                         if [[ $verbose == 1 ]]; then
176                                 echo "Resplitting subpg ${subpg}."
177                         fi
178                         unpaper $unpaperflags --pre-rotate 90 --layout double --overwrite -op 2 $dir/upg-${pgn}-${subpg}.pnm $dir/upg-${pgn}-${subpg}-%01d.pnm
179                 else
180                         cp $dir/upg-${pgn}-${subpg}.pnm $dir/upg-${pgn}-${subpg}-1.pnm
181                 fi;
182
183         done;
184
185         #final convert and clean w/ bebook optimisation
186         if [[ $bebook ]]; then
187                 convert $convertflags -colorspace Gray -median 1 $dir/upg-${pgn}-1-?.pnm -trim -resize 1800x2400 $dir/final-${pgn}-1-%01d.${extension} || exit 1
188                 convert $convertflags -colorspace Gray -median 1 $dir/upg-${pgn}-2-?.pnm -trim -resize 1800x2400 $dir/final-${pgn}-2-%01d.${extension} || exit 1
189         else
190                 convert $convertflags $dir/upg-${pgn}-1-?.pnm $dir/final-${pgn}-1-%01d.${extension} || exit 1
191                 convert $convertflags $dir/upg-${pgn}-2-?.pnm $dir/final-${pgn}-2-%01d.${extension} || exit 1
192         fi
193         
194 done
195
196 mkdir -p $dir/pages
197 mv $dir/final-*.${extension} $dir/pages
198         

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