- #names go a bit funny here
- #also, ignore flags starting here
- unppnm=$dir/upg-${pgn}-%01d.pnm
- unpaper $unpaperflags --layout double --overwrite --no-blackfilter -ni 10 -op 2 $preppnm $unppnm || exit 1
-
- #detect if the page is 2-up
- for subpg in $($my_seq 1 2); do
- echo Processing subpg ${subpg}.
- if $(dirname $0)/detect2pages.sh $dir/upg-${pgn}-${subpg}.pnm ${pgn} ${subpg}; then
+ physpgbase=$outdir/physpg-${scanpgnum}
+ if [ ! -e ${physpgbase}-1.pnm ] || [ ! -e ${physpgbase}-2.pnm ]; then
+ # most of unpaper's processing is redundant given ocropus, and somewhat too agressive
+ unpaper $unpaperflags --layout double --overwrite -op 2 \
+ --no-processing \
+ $cleanscanpg ${physpgbase}-%01d.pnm || exit 1
+
+ fi;
+
+ 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%
+ # binarise is so effective, try something << 90%
+ cropcords=$(convert -trim -fuzz 50%\
+ -resize 1000% -format "%wx%h%O" $outdir/physpg-mask-${scanpgnum}-${physpgnum}.png info: || exit 1)
+
+ [[ $verbose ]] && echo Crop co-ords: $cropcords
+
+ # ... crop and despeckle? the final pre-prepared image
+ convert $convertflags -crop $cropcords $physpg $cleanphyspg || exit 1
+ fi;
+
+ # check it hasn't mostly disappeared, warn viciously if it has!
+ if [[ $(convert $cleanphyspg -format '%[fx:s.w*s.h>1000]' info:) = "0" ]]; then
+ echo "Warning: discarding physical pg ${scanpgnum}-${physpgnum}: not enough remains after masking."
+ continue;
+ fi;
+
+ #detect if the page is 2-up
+
+ if [[ $logperphys == 2 ]] || ( [[ $logperphys != 1 ]] && $(dirname $0)/detect2pages.sh ${cleanphyspg} ${scanpgnum} ${physpgnum} ); then