Skip to content

Instantly share code, notes, and snippets.

@ony
Created October 10, 2012 07:53
Show Gist options
  • Save ony/3863877 to your computer and use it in GitHub Desktop.
Save ony/3863877 to your computer and use it in GitHub Desktop.
Assemble DjVu book from set of different images (including multi-page tiffs)
#!/bin/bash
# djvubook
#
# Author:
# Nikolay Orlyuk <virkony@gmail.com>
#
# Copyright (c) 2012 (c) Nikolay Orlyuk
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
msg() {
echo " >> $*" >&2
}
die() {
msg "$*"
exit 1
}
tmpdir=`mktemp -d`
trap "rm -rf '$tmpdir'" EXIT
trap 'die Interrupted' SIGINT
color=
case "$1" in
-color) color=1; shift ;;
esac
[ -z "$1" ] && die "Missing output djvu file as first argument"
book="$1"
shift
[ -z "$1" ] && die "Missing input files as arguments after output djvu file"
let n=0
next() {
let n+=1
dst=$(printf "%s/page-%05d.$1" "$tmpdir" "$n")
}
abspath() {
case "$1" in
/*) echo "$1" ;;
*) echo "$PWD/$1" ;;
esac
}
is_color() {
[ -n "$color" ]
}
if gm -version > /dev/null; then
do_convert() {
gm convert "$@"
}
else
do_convert() {
convert "$@"
}
fi
is_color && ifmt=ppm || ifmt=pbm
for page in "$@"; do
msg "Prepearing $page"
case "$page" in
*.ppm)
if is_color; then
next "$ifmt"
ln -s $(abspath "$page") "$dst" || die "Failed to make symlink for $page at $tmpdir"
else
msg "Converting $page"
next "$ifmt"
do_convert "$page" "$dst" || die "Failed to convert $page"
fi
;;
*.pgm|*.pbm|*.djvu)
next "${page##*.}"
ln -s $(abspath "$page") "$dst" || die "Failed to make symlink for $page at $tmpdir"
;;
*.tif|*.tiff)
tiffsplit "$page" "$tmpdir/page-" || die "Failed to split $page to $tmpdir/page-*.tif"
for subpage in "$tmpdir"/page-*.tif; do
name="${page}::${subpage##*/}"
msg "Converting $name"
next "$ifmt"
do_convert "$subpage" "$dst" || die "Failed to convert $name"
done
rm "$tmpdir"/page-*.tif || die "Failed to cleanup splitted pages"
;;
*.jpg|*.jpeg)
if is_color; then
next jpg
ln -s $(abspath "$page") "$dst" || die "Failed to make symlink for $page at $tmpdir"
else
msg "Converting $page"
next "$ifmt"
do_convert "$page" "$dst" || die "Failed to convert $name"
fi
;;
esac
done
if is_color; then
msg "Building DjVuDocuments"
for page in "$tmpdir"/page-*.p[pgb]m "$tmpdir"/page-*.jpg; do
case "$page" in
*-\*.*) continue ;;
esac
djvu="${page%.*}.djvu"
msg "Encode $page"
cpaldjvu -colors 8 -bgwhite "$page" "$djvu" || die "Failed to encode $page"
done
else
msg "Building DjVuBitonal"
for page in "$tmpdir"/page-*.p[pgb]m; do
djvu="${page%.p[pgb]m}.djvu"
msg "Encode $page"
cjb2 "$page" "$djvu" || die "Failed to encode $page"
done
fi
rm -f "$tmpdir"/page-*.p[pgb]m "$tmpdir"/page-*.jpg || die "Failed to cleanup intermediate images for pages"
djvm -c "$book" "$tmpdir"/page-*.djvu || die "Failed to create boundled $book"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment