+ for physpgnum in $($my_seq 1 2); do
+ echo Processing physical page ${physpgnum}.
+
+ physpg=${physpgbase}-${physpgnum}.pnm #$physpgbase has $outdir/ already
+
+ cleanphyspg=$outdir/physpg-clean-${scanpgnum}-${physpgnum}.pnm
+
+ if [ ! -e $cleanphyspg ]; then
+ # remask and retrim. FIXME: may throw off 2page detection
+ convert $convertflags -resize 10% -depth 8 -gamma 0.01 -median 2 $physpg $outdir/physpg-mask-${scanpgnum}-${physpgnum}.png ||exit 1
+
+ # Trim #-border 1x1 -bordercolor '#fff' -trim -fuzz 30%
+ cropcords=$(convert -trim -fuzz 90%\
+ -resize 1000% -format "%wx%h%O" $outdir/physpg-mask-${scanpgnum}-${physpgnum}.png info: || exit 1)
+
+ # ... crop and despeckle? the final pre-prepared image
+ convert $convertflags -crop $cropcords $physpg $cleanphyspg || exit 1
+ fi;
+
+ #detect if the page is 2-up
+
+ if [[ $logperphys == 2 ]] || ( [[ $logperphys != 1 ]] && $(dirname $0)/detect2pages.sh ${cleanphyspg} ${scanpgnum} ${physpgnum} ); then
+ if [[ $verbose == 1 ]]; then
+ echo "Resplitting physical page ${physpgnum}."
+ fi
+ unpaper $unpaperflags --pre-rotate 90 --layout double --overwrite \
+ -op 2 --no-blackfilter --no-grayfilter --no-noisefilter \
+ --no-blurfilter $cleanphyspg $outdir/logpg-${scanpgnum}-${physpgnum}-%01d.pnm || exit 1
+ else
+ cp $cleanphyspg $outdir/logpg-${scanpgnum}-${physpgnum}-1.pnm
+ fi;
+
+ #prepare for ocr
+ convert $convertflags $outdir/logpg-${scanpgnum}-${physpgnum}-?.pnm $outdir/logpg-${scanpgnum}-${physpgnum}-%01d.png || exit 1
+
+ #final convert and clean w/ bebook optimisation
+ if [[ $bebook ]]; then
+ convert $convertflags $outdir/logpg-${scanpgnum}-${physpgnum}-?.pnm -trim -fuzz 80% -resize 1200x1600 $outdir/final-${scanpgnum}-${physpgnum}-%01d.${extension} || exit 1
+ else
+ convert $convertflags $outdir/logpg-${scanpgnum}-${physpgnum}-?.pnm $outdir/final-${scanpgnum}-${physpgnum}-%01d.${extension} || exit 1
+ fi
+
+ done;
+