-
Notifications
You must be signed in to change notification settings - Fork 0
/
img2djvu
executable file
·193 lines (180 loc) · 4.96 KB
/
img2djvu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
# vi:ts=4
set -ue # die on undefined variables and non-zero exit code
usage() {
cat <<USAGE
Usage: $0 [-options] -o <target.djvu> <image files> [-more options <more image files>]
Available options (in the order of application) are:
-o <file>
Set output file [REQUIRED]
-r <degrees>
rotate images by 90, 180 or 270 degrees
-s @<hpages>x<vpages>+<hpixels>+<vpixels>
split each image into <hpages>x<vpages> pages, leaving
overlapping part sized <hpixels>x<vpixels>
btw, this option is actually just imagemagick's -crop
-u <radius>x<sigma>[+<gain>+<threshold>]
apply unsharp mask filter
-g
make images grey-scale
-c <black>x<white>[%]
stretch contrast of the pages, blacking out at most <black>
pixels, whiting out at most <white> pixels; treat numbers as
percentiles if there is '%' sign at the end
-a <other options>
applies the specified IM options to the image
-q <colors>
quantize image, leaving only <colors> colors
this option implies cpaldjvu compression
-m <threshold>
make pages monochrome
this option implies cjb2 compression
-p
switch to DjVuPhoto encoding (not cjb2 or cpaldjvu)
-d <dpi>
set dpi for encoders, default 600
-e
skip non-existent files
-h
this help message
USAGE
exit $1
}
# encoding parameters and output file - from command line
declare rotate= split= unsharp= greyscale= cstretch= other= quantize= threshold= manual= encoder= target= dpi=600 skip=
# encoding mode and encoder command - from encoding parameters
declare -a encoder
declare format
# imagemagick processing parameters - from encoding parameters
declare -a process
# files to build target.djvu from
declare -a compressed=()
while [ $# -gt 0 ]; do
if getopts ":d:o:r:s:u:gc:q:m:ha:ep" OPT; then
case "$OPT" in
r)
rotate="$OPTARG"
[ $rotate ] && echo "Will rotate by $rotate degrees clockwise"
;;
s)
split="$OPTARG"
[ $split ] && echo "Will split by $split pieces"
;;
u)
unsharp="$OPTARG"
[ $unsharp ] && echo "Will apply unsharp mask ($unsharp)"
;;
g)
if [ $greyscale ]; then
greyscale=
else
greyscale=1
echo "Will convert to greyscale"
fi
;;
c)
cstretch="$OPTARG"
threshold=""
[ $cstretch ] && echo "Will stretch contrast ($cstretch)"
;;
a)
other="$OPTARG"
[ "$other" ] && echo "Will set additional options ($other)"
;;
q)
quantize="$OPTARG"
threshold=""
[ "$quantize" ] && echo "Will quantize to $quantize colors"
;;
m)
threshold="$OPTARG"
quantize=""
[ "$threshold" ] && echo "Will convert to monochrome with threshold $threshold"
;;
p)
threshold=""
quantize=""
echo "Will encode as DjVuPhoto"
;;
h)
usage 0
;;
o)
if [ "$target" ]; then
echo "Target already set to $target! What did you mean? Aborting"
exit 1
fi
target="$OPTARG"
echo "Will save to $target"
;;
d)
dpi="$OPTARG"
echo "Will set dpi to $dpi"
;;
e)
skip=1
echo "Will skip non-existent files"
;;
*)
echo "Invalid option: $OPT $OPTARG"
usage 1
;;
esac
shift $(($OPTIND-1)) # get $@ ready for next iteration
OPTIND=1 # get getopts ready, too
# now we need to update the encoding mode
if [ "$threshold" ]; then
encoder=("cjb2" "-verbose" "-dpi" "$dpi")
# cjb2 works with monochrome images
format=pbm
elif [ "$quantize" ]; then
encoder=("cpaldjvu" "-verbose" "-colors" "$quantize" "-dpi" "$dpi")
format=ppm
else
encoder=("c44" "-dpi" "$dpi")
format=ppm
fi
# and update the processing parameters
process=("-verbose")
[ "$rotate" ] && process+=("-rotate" "$rotate")
[ "$split" ] && process+=("-crop" "$split")
[ "$unsharp" ] && process+=("-unsharp" "$unsharp")
[ $greyscale ] && process+=("-colorspace" "Gray")
[ "$cstretch" ] && process+=("-contrast-stretch" "$cstretch")
[ "$other" ] && process+=($other) # yes, without quotes
[ "$quantize" ] && process+=("+dither" "-colors" "$quantize") # disable dithering to help cpaldjvu
[ "$threshold" ] && process+=("-threshold" "$threshold")
continue
fi
if ! [ "$target" ]; then
echo "About to start processing files, but no target given (yet?)!"
echo "Please set the target before the files to process (-o option)."
echo "Aborting."
exit 1
fi
if [ -e "$target" ]; then
echo "'$target' already exists! Will not overwrite it, aborting."
exit 1
fi
if [ ! -e "$1" -a -n "$skip" ]; then
echo "Skipping non-existent $1"
shift 1
continue
fi
convert "$1" "${process[@]}" "$1.%d.$format"
for part in "$1".*.$format; do
# $encoder = ${encoder[0]} = program name
"${encoder[@]}" "$part" "$part".$encoder
rm -v "$part"
compressed+=("$part".$encoder)
done
shift 1 # we processed the file, update $@ accordingly
done
if [ "${#compressed[@]}" -eq 0 ]; then
echo "Files to process are not specified! Nothing to do, aborting"
usage 1
fi
echo "Combining temp files into target $target"
djvm -c "$target" "${compressed[@]}"
echo "Removing temp files"
rm -v "${compressed[@]}"