Commits

Sergey Astanin committed 5ae7be7

pdf-trim-to-djvu: split multi-column layouts in many pages (--columns)

Comments (0)

Files changed (1)

 DPI=150
 MARGIN=10
 MODE=gray # mono or gray or color
+COLUMNS=1
 
 ### End of settings
 
 Usage: $me [options] document.pdf
 
 Options:
- -f <int>   the fist page to process [default: 1]
- -t <int>   the last page to process
- -d <int>   resolution in DPI [default: $dpidefault]
- --mono     bitonal compression (black and white only) $monodefault
- --gray     DjVuPhoto compression (shades of gray images) $graydefault
- --color    DjVuPhoto compression (color images) $colordefault
- -h|--help  print this message
+ -f <int>            the first page to process [default: 1]
+ -t <int>            the last page to process
+ -d <int>            resolution in DPI [default: $dpidefault]
+ -c|--columns <int>  multi-column mode [default: $COLUMNS]
+ --mono              bitonal compression (black and white only) $monodefault
+ --gray              DjVuPhoto compression (shades of gray images) $graydefault
+ --color             DjVuPhoto compression (color images) $colordefault
+ -h|--help           print this message
 
 END
 }
 
-opts=`getopt -l "help,mono,gray,grey,color,colour" "hf:t:d:" "$@"` && eval set -- "$opts"
+opts=`getopt -l "help,mono,gray,grey,color,colour,columns:" \
+      "hf:t:d:c:" "$@"` && eval set -- "$opts"
 while true ; do
   case "$1" in
     -h|--help) usage ; exit 0 ;;
     --mono) MODE=mono ; shift ;;
     --color|--colour) MODE=color ; shift ;;
     --gray|--grey) MODE=gray ; shift ;;
+    --columns|-c) COLUMNS=$2 ; shift 2 ;;
     --) shift ; break ;;
   esac
 done
       mogrify -quiet -trim +repage "$fname" && \
       ( # skip empty pages
         if [ `identify -format "%k" "$fname"` -gt 1 ] ; then
-           mogrify -bordercolor white -border $MARGIN "$fname"
-           $compress -dpi $DPI "$fname" `printf "%06d.djvu" $i`
-           printf "\b "
+	   if [ $COLUMNS -ne 1 ] ; then  # cut page in COLUMNS x n pieces
+	     w=$(($(identify -format "%w" "$fname")/COLUMNS))  # crop width
+             h=$(((2*MARGIN+w)*4/3-2*MARGIN))  # crop height, aspect 4:3
+	     pageh=$(identify -format "%h" "$fname")
+	     n=$((pageh/h + 1))  # number of vertical slices
+	     overlap=$(((h*n-pageh)/(n-1)))
+	     dh=$((h-overlap))
+	     state=("/" "–" "\\" "|")
+	     for c in `seq 0 $((COLUMNS-1))` ; do  # for every column
+	       for j in `seq 0 $((n-1))` ; do # for every partial image
+		 pagename=`printf "%06d_%d_%02d.$pmext" $i $c $j`
+		 djvuname=`printf "%06d_%d_%02d.djvu" $i $c $j`
+		 convert "$fname" -crop ${w}x${h}+$((c*w))+$((j*dh)) \
+		     -bordercolor white -border $MARGIN "$pagename"
+		 $compress -dpi $DPI "$pagename" "$djvuname"
+		 rm "$pagename"
+		 printf "\b%s" ${state[$(((c*n+j)%4))]}
+	       done
+	     done
+	     printf "\b "
+	   else # keep page as is
+             mogrify -bordercolor white -border $MARGIN "$fname"
+             $compress -dpi $DPI "$fname" `printf "%06d.djvu" $i`
+             printf "\b "
+           fi
         else
            numlen=`printf "$i"|wc -c`
            for j in `seq 0 $numlen` ; do printf "\b" ; done ;