#!/bin/bash # Copyright (C) 2008 Richard G. Heck, Jr. # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Updates etc should be available from: # http://frege.brown.edu/heck/linux/programs/bash.php # Change log # Version 0.1: First public release # TODO # Implement losslevel # convert input files to bitonal PROG=`basename $0`; function printUsage { cat </dev/null`; if [ -z "$CVT" ]; then echo "This script uses the convert utility from the ImageMagick package. It does not appear to be in your path."; exit 1; fi # Get filename we're looking for if [ -n "$1" ]; then FILENAME="$1"; else FILENAME="pages"; fi OLDFILES=$(ls -1 $FILENAME*.$EXT 2>/dev/null); if [ -z "$OLDFILES" ]; then echo "No files found matching $FILENAME*.$EXT"; exit 1; fi FIRSTFILE=""; LASTFILE=""; FILES=""; echo "Copying original files"; for fil in $OLDFILES; do echo -n "."; NEWFILE="tiff2djvu-$$-$fil"; $DEBUG cp $fil $NEWFILE; if [ -z "$FILES" ]; then FILES="$NEWFILE"; else FILES="$FILES $NEWFILE"; fi if [ -z "$FIRSTFILE" ]; then FIRSTFILE="$NEWFILE"; fi; LASTFILE="$NEWFILE"; done echo "done."; # Crop pages if requested if [ -n "$CROPDIMS" ]; then echo "Cropping to $CROPDIMS"; for fil in $FILES; do echo -n "."; CFIL="precrop-$fil"; if [ -z "$DEBUG" ]; then mv $fil $CFIL; fi $DEBUG convert $CFIL -crop $CROPDIMS +repage $fil; $DEBUG $KEEP rm $CFIL; done echo "done."; fi # Rotate pages if requested if [ -n "$ROTATE" ]; then echo "Rotating pages"; for fil in $FILES; do echo -n "."; RFIL="prerot-$fil"; if [ -z "$DEBUG" ]; then mv $fil $RFIL; fi $DEBUG convert -rotate $ROTATE $RFIL $fil; $DEBUG $KEEP rm $RFIL; done echo "done."; fi # Crop first and last pages if requested if [ -n "$CROPFIRST$CROPLAST" ]; then echo -n "Cropping"; if [ -n "$CROPFIRST" ]; then echo -n " first page"; crop $FIRSTFILE "W"; fi if [ -n "$CROPLAST" ]; then if [ -n "$CROPFIRST" ]; then echo -n " and"; fi if [ -z "$QUIET" ]; then echo " last page."; fi crop $LASTFILE "E"; else echo "."; fi fi echo "Converting"; for i in $FILES; do echo -n "."; if [ -n "$MKDJVU" ]; then DJVUFIL="djvu-$$-$i"; if identify $i | grep -q Bilevel; then ln -s $i $DJVUFIL; else $DEBUG convert $i -depth 1 $DJVUFIL; fi $DEBUG cjb2 $DJVUFIL ${i%$EXT}djvu; $DEBUG $KEEP rm $DJVUFIL; fi if [ -n "$PDF" ]; then PNGFIL="${i%$EXT}png"; PDFFIL="${i%$EXT}pdf"; $DEBUG convert $i -resize ${NEWDT}x$NEWHT +repage -type Grayscale -depth 4 +compress $PNGFIL; if [ -n "$BATCH" ]; then $DEBUG convert $PNGFIL $PDFFIL; $DEBUG $KEEP rm $PNGFIL; fi fi if [ -n "$PDFSPLIT" ]; then DATA=`identify $i | cut -d' ' -f 3`; WIDTH=${DATA%x*}; HEIGHT=${DATA#*x}; NEWDT=$(($WIDTH / 2)); NEWHT=$(($HEIGHT / 2)); if [ "$i" != "$FIRSTFILE" -o -z "$CROPFIRST" ]; then #$DEBUG convert $i -resize ${NEWDT}x$NEWHT +repage -type Grayscale +compress $i; #NEWDT=$(($NEWDT / 2)); $DEBUG convert $i -crop ${NEWDT}x$HEIGHT+0+0 +repage -type Grayscale -depth 4 +compress SPLIT-$$-$i.1.png; fi if [ "$i" != "$LASTFILE" -o -z "$CROPLAST" ]; then #$DEBUG convert $i -resize ${NEWDT}x$NEWHT +repage -type Grayscale +compress $i; #NEWDT=$(($NEWDT / 2)); $DEBUG convert $i -crop ${NEWDT}x$HEIGHT+${NEWDT}+0 +repage -type Grayscale -depth 4 +compress SPLIT-$$-$i.2.png; fi fi done echo "done."; if [ -z "$BATCH" ]; then if [ -n "$MKDJVU" ]; then echo "Creating DJVU."; $DEBUG djvm -c x$FILENAME.djvu tiff2djvu-$$-$FILENAME*djvu; rm tiff2djvu-$$-$FILENAME*djvu; mv x$FILENAME.djvu $FILENAME.djvu fi if [ -n "$PDF" ]; then echo "Creating PDF."; $DEBUG convert $FILENAME*png $FILENAME.pdf $DEBUG $KEEP rm $FILENAME*tiff; fi if [ -n "$PDFSPLIT" ]; then echo "Creating single-page PDF."; if [ -z "$PDF" ]; then PDFNAME=$FILENAME.pdf; else PDFNAME=$FILENAME-split.pdf; fi $DEBUG convert SPLIT-$$-*.png $PDFNAME; $DEBUG $KEEP rm SPLIT-$$-*.png; fi fi $DEBUG $KEEP rm tiff2djvu-$$-$FILENAME*.tiff;