- # preprocess it!
- preppnm=$dir/pg-pp-${pgn}.pnm
- if [[ ! $skipmask ]] && ( [ ! -e $preppnm ] || [[ $forcemask ]] ); then
+done;
+
+# do ocr binarise
+if [ -e $outdir/scanpgs ]; then
+ #assume (for the sake of speed in repeated runs) that
+ # if the last file exists, the process completed successfully
+ if [ -e $outdir/scanpgs/$(printf '%04d' $pages).bin.png ]; then
+ [[ $verbose ]] && echo Binarisation already complete
+ else
+ rm -r $outdir/scanpgs
+ fi
+fi
+[ -e $outdir/scanpgs ] || ocropus book2pages $outdir/scanpgs $outdir/scanpg-*.png || exit 1
+
+# process each binarised scan page
+for scanpgnum in `$my_seq 1 $pages`; do
+
+ binscanpg=$outdir/scanpgs/$(printf '%04d' $scanpgnum).bin.png
+ scanpgnum=$(printf '%03d' $scanpgnum)
+ scanpg=$outdir/scanpg-${scanpgnum}.png
+
+ # preprocess scanned page
+ cleanscanpg=$outdir/scanpg-clean-${scanpgnum}.pnm
+ if [ ! -e $cleanscanpg ]; then