blob: 620a299a386aecd0ff61dbe19149639f4548333b (
plain) (
blame)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
#! /usr/bin/env dash
# shellcheck source=/dev/null
SHELL_LIBRARY_VERSION="2.0.13" . %SHELL_LIBRARY_PATH
# needed for help() and version
# shellcheck disable=2034
AUTHORS="Soispha"
# shellcheck disable=2034
YEARS="2023"
# shellcheck disable=2034
VERSION="1.0.0"
# NAME is from the wrapper
# shellcheck disable=SC2269
NAME="$NAME"
help() {
cat <<EOF
Scan images and turn them into a pdf.
Usage:
$NAME [OPTIONS] --name --device
OPTIONS:
--out-dir | -o [FILE]
Path to place the generated pdf files (default: ./pdf).
--name | -n NAME
Name for the pdf files (e.g. <NAME>_1.pdf).
--num-pages | -p NUM
Number of pages to merge into one pdf (default: 1).
--device | -d DEVICE
Device used for scanning.
--method | -m METHOD
Method to use for scanning (default: ADF).
--help | -h
Display this help and exit.
--version | -v
Display version and copyright information and exit.
ARGUMENTS:
FILE := [[fd . --max-depth 3]]
A name of a file to store, default is: ./pdf
NAME | * := [[fd . --max-depth 3]]
The basename of the generated files
NUM | *([0-9]) := 0 | 1 | 2 | 3 | 4
Possible numbers of pages, can be more than 4
DEVICE := [[$(cat %DEVICE_FUNCTION)]]
Possible scanner names
METHOD := ADF | Flatbed
The scanning method to use, not all scanners support both of
these. The default is ADF
EOF
}
scan_adf() {
device="$1"
sides_per_page="$2"
method="ADF"
for i in $(seq "$sides_per_page"); do
do_until_success \
"scanimage --format=tiff --progress --source='$method' --device='$device' --batch=%d.tif --batch-increment='$sides_per_page' --batch-start='$i'" \
"warn 'Retrying scan, as we assume a network error!'"
if [ "$sides_per_page" -ne 1 ]; then
msg "Finished turn, please change side!"
readp "Press enter to continue" noop
fi
done
}
process_images_adf() {
tiff_temp_path="$1"
output_directory="$2"
name="$3"
counter=0
pdf_counter=0
image_cache="$(mktmp)"
while read -r scanned_image; do
dbg "$scanned_image (scanned_image) at $counter (counter)"
echo "$scanned_image" >>"$image_cache"
: $((counter += 1))
if [ "$counter" = "$number_of_pages" ]; then
dbg "$counter == $number_of_pages"
counter=0
convert_images "$image_cache" "${name}_$pdf_counter" "$output_directory"
: $((pdf_counter += 1))
printf "" >"$image_cache"
fi
done <"$(tmp_pipe fd . "$tiff_temp_path" "|" sort -V)"
}
scan_flatbed() {
device="$1"
number_of_pages"$2"
method="Flatbed"
for i in $(seq "$number_of_pages"); do
do_until_success \
"scanimage --format=tiff --progress --source='$method' --device='$device' --output-file=$i.tiff" \
"warn 'Retrying scan, as we assume a network error!'"
if [ "$number_of_pages" -ne 1 ]; then
msg "Finished turn, please change side!"
readp "Press enter to continue" noop
fi
done
}
process_images_flatbed() {
tiff_temp_path="$1"
output_directory="$2"
name="$3"
counter=0
image_cache="$(mktmp)"
while read -r scanned_image; do
echo "$scanned_image" >>"$image_cache"
: $((counter += 1))
if [ "$counter" = "$number_of_pages" ]; then
counter=0
convert_images "$image_cache" "$name" "$output_directory"
printf "" >"$image_cache"
fi
done <"$(tmp_pipe fd . "$tiff_temp_path" "|" sort -V)"
}
convert_images() {
image_cache="$1"
pdf_name="$2"
output_dir="$3"
set --
while read -r image; do
dbg "setting image: $image"
set -- "$@" "$image"
done <"$image_cache"
while [ -e "$output_dir/${pdf_name}.pdf" ]; do
pdf_name="${pdf_name}_$(tr -dc 'A-Za-z0-9' </dev/urandom | head -c 25)"
done
dbg "using pdf_name: $pdf_name"
convert "$@" -compress jpeg -quality 100 "$output_dir/${pdf_name}.pdf"
}
scan() {
number_of_pages="$1"
device="$2"
output_directory="$(readlink -f "$3")"
name="$4"
method="$5"
[ -z "$number_of_pages" ] && die "Parameter 'number_of_pages' is not set!"
[ -z "$device" ] && die "Parameter 'device' is not set!"
[ -z "$output_directory" ] && die "Parameter 'output_directory' is not set!"
[ -z "$name" ] && die "Parameter 'name' is not set!"
[ -z "$method" ] && die "Parameter 'method' is not set!"
tiff_temp_path="$(mktmp -d)"
cd "$tiff_temp_path" || die "Bug"
msg "Started scanning..."
if [ "$method" = "Flatbed" ]; then
scan_flatbed "$device" "$number_of_pages"
else
scan_adf "$device" "$number_of_pages"
fi
msg "Creating output directory..."
mkdir "$output_directory"
cd "$output_directory" || die "Bug"
msg "Converting images to pdfs..."
if [ "$method" = "Flatbed" ]; then
process_images_flatbed "$tiff_temp_path" "$output_directory" "$name"
else
process_images_adf "$tiff_temp_path" "$output_directory" "$name"
fi
}
for input in "$@"; do
case "$input" in
"--help" | "-h")
help
exit 0
;;
"--version" | "-v")
version
exit 0
;;
esac
done
number_of_pages="1"
unset device
output_directory="$(pwd)/pdf"
unset name
method="ADF"
while [ "$#" -ne 0 ]; do
case "$1" in
"--help" | "-h") ;;
"--version" | "-v") ;;
"--out-dir" | "-o")
shift 1
output_directory="$1"
;;
"--name" | "-n")
shift 1
name="$1"
;;
"--num-pages" | "-p")
shift 1
number_of_pages="$1"
;;
"--device" | "-d")
shift 1
device="$1"
;;
"--method" | "-m")
shift 1
method="$1"
;;
*)
die "Command line arg $1 does not exist. See --help for a list."
;;
esac
shift 1
done
scan "$number_of_pages" "$device" "$output_directory" "$name" "$method"
|