More fixes.

dev
Franco Masotti 2018-12-06 18:59:33 +01:00
parent 4b00b518b6
commit 70ae952fbc
2 changed files with 93 additions and 74 deletions

23
fbopt
View File

@ -43,8 +43,9 @@ Options:
-r, --resolution=RESOLUTION page resolution in DPI
-s, --source=SOURCE scan from the ADF, Flatbed or whatever
supported method
-t, --two-sided toggle preserve the order in double sided paper:
scan a batch of papers one side, then the other
-t, --two-sided toggle preserve the order in
double sided paper: scan a batch of
papers one side, then the other
--two-sided-reverse same as '--odd-even' but with the need of
reversing every single paper.
-u, --unpaper[=OPTIONS] enable unpaper. You may pass
@ -161,10 +162,10 @@ main()
# ./program -flag ''
# This can also be done inside the option parser loop but to avoid nestings
# I prefer it done here.
{ [ -z "${imagemagick}" \
{ [ -z "${imagemagick}" ] \
|| [ -z "${mode}" ] \
|| [ -z "${resolution}" ] \
|| [ -z "${source}"; } \
|| [ -z "${source}" ]; } \
&& getopt_error && return 1
[ "${print_flags}" = 'true' ] \
&& show_flags \
@ -181,14 +182,20 @@ main()
# From now on you should call a function or an external program
# using the values of the flag variables.
[ "${list_modes}" = 'true' ] && get_supported_modes && return ${?}
[ "${list_resolutions}" = 'true' ] && get_supported_resolutions && return ${?}
[ "${list_sources}" = 'true' ] && get_supported_sources && return ${?}
[ "${list_modes}" = 'true' ] \
&& get_supported_modes "$(get_supported_parameters)" \
&& return ${?}
[ "${list_resolutions}" = 'true' ] \
&& get_supported_resolutions "$(get_supported_parameters)" \
&& return ${?}
[ "${list_sources}" = 'true' ] \
&& get_supported_sources "$(get_supported_parameters)" \
&& return ${?}
local output_file="${1}"
[ -z "${output_file}" ] && getopt_error && return 1
preliminary_controls \
preliminary_checks \
"${imagemagick}" \
"${mode}" \
"${resolution}" \

View File

@ -22,23 +22,28 @@
# along with spectrscan. If not, see <http://www.gnu.org/licenses/>.
#
tmp_dir="$(pwd)/.spectrscan-$RANDOM"
src_dir="$(pwd)"
set -x
#set -e
TMP_DIR="$(pwd)/.spectrscan-$RANDOM"
SRC_DIR="$(pwd)"
output_file=""
########
########
init()
check_software()
{
mkdir "$tmp_dir"
pushd "$tmp_dir"
which \
scanimage \
unapaper \
awk \
convert \
pdftk \
parallel \
file \
pamfix \
netpbm
} 1>/dev/null 2>/dev/null
scan()
{
local file_counter="$1"
local file_counter="${1}"
local batch_start=1
local batch_increment=1
@ -60,8 +65,8 @@ scan()
scanimage \
--source "$source" \
--batch=spectrscan_out%08d.pnm \
--batch-start $batch_start \
--batch-increment $batch_increment \
--batch-start ${batch_start} \
--batch-increment ${batch_increment} \
--resolution "$resolution" \
--mode "$mode" \
--progress \
@ -71,8 +76,8 @@ scan()
exists_output_file()
{
# Check if output file exists and is a pdf file.
if [ -f "$src_dir"/"$output_file" ]; then
if [ "$(file --mime-type "$src_dir"/"$output_file" \
if [ -f "$SRC_DIR"/"$output_file" ]; then
if [ "$(file --mime-type "$SRC_DIR"/"$output_file" \
| awk '{ print $2 }')" = "application/pdf" ]; then
printf "true"
else
@ -87,9 +92,9 @@ exists_output_file()
convert_single()
{
# First convert to pdf with the image enhancements, then apply unpaper.
# This giuves better results because it avoids missing information in
# the finakl result.
local filE="$1"
# This gives better results because it avoids missing information
# removed by unpaper in the final result.
local filE="${1}"
pamfix -truncate "$filE" > "$filE".tmp.pnm.bis
@ -139,15 +144,15 @@ pdf_cat()
# O(1)
if [ "$(exists_output_file)" = "true" ]; then
cp "$src_dir"/"$output_file" "$src_dir"/."$output_file"
cp "$SRC_DIR"/"$output_file" "$SRC_DIR"/."$output_file"
pdftk "$src_dir"/."$output_file" "$output_file".tmp \
cat output "$src_dir"/"$output_file"
pdftk "$SRC_DIR"/."$output_file" "$output_file".tmp \
cat output "$SRC_DIR"/"$output_file"
rm "$src_dir"/."$output_file"
rm "$SRC_DIR"/."$output_file"
elif [ "$(exists_output_file)" = "false" ]; then
mv "$output_file".tmp "$src_dir"/"$output_file"
mv "$output_file".tmp "$SRC_DIR"/"$output_file"
else
printf "[ERROR]\n" 1>&2-
exit 1
@ -156,36 +161,6 @@ pdf_cat()
printf "Done.\n" 1>&2-
}
cleanup()
{
popd
rm -rf "$tmp_dir"
} 1>/dev/null 2>/dev/null
chain()
{
init
if [ "$odd_even" = "true" ]; then
scan "odd" \
&& printf "Turn the paper(s) and hit return when ready\n" \
&& read \
&& scan "even"
else
scan
fi
# Feeder out of documents.
if [ $? -eq 7 ]; then
printf "No paper inserted\n" 1>&2
return 1
fi
pnm_to_pdf
pdf_cat
cleanup
}
get_supported_parameters()
{
scanimage -A | grep -e '--source' -e '--mode' -e '--resolution'
@ -214,38 +189,75 @@ get_supported_sources()
check_supported_parameters()
{
local parameter="$1"
local value="$2"
local parameter="${1}"
local value="${2}"
local supported_parameters="${3}"
local counter=0
local parameter_full_name=""
local p=""
local parameter_full_name=''
local p=''
#TODO.
parameter_full_name="supported_${parameter}"
eval "$parameter_full_name='$(get_supported_${parameter})'"
eval "$parameter_full_name='$(get_supported_"${parameter}" "${supported_parameters}")'"
for p in ${!parameter_full_name}; do
if [ "$value" = "$p" ]; then
counter=$(($counter+1))
fi
done
if [ $counter -eq 0 ]; then
printf "Supported $parameter: ${!parameter_full_name}\n" 1>&2-
printf "ERROR: Supported $parameter: ${!parameter_full_name}\n" 1>&2-
return 1
fi
}
preliminary_controls()
chain()
{
printf "Probing scanner and its options...\n" 1>&2-
mkdir "$TMP_DIR"
pushd "$TMP_DIR"
params="$(get_supported_parameters)"
if [ "$odd_even" = "true" ]; then
scan "odd" \
&& printf "%s\n" 'turn the paper(s) and hit return when ready' \
&& read \
&& scan "even"
else
scan
fi
# Feeder out of documents.
if [ $? -eq 7 ]; then
printf "No paper inserted\n" 1>&2
return 1
fi
pnm_to_pdf
pdf_cat
cleanup
popd
rm -rf "$TMP_DIR"
}
preliminary_checks()
{
# Leak all variables to avoid passing them every time.
imagemagick="${1}"
mode="${2}"
resolution="${3}"
source="${4}"
unpaper="${5}"
output_file="${6}"
# Get the parameters and check that the scanner is connected.
local params="$(get_supported_parameters)"
[ ${?} -ne 0 ] && return ${?}
{ check_supported_parameters "modes" "$mode" \
&& check_supported_parameters "resolutions" "$resolution" \
&& check_supported_parameters "sources" "$source"; } || return 1
{ check_supported_parameters "modes" "${mode}" "${params}" \
&& check_supported_parameters "resolutions" "${resolution}" "${params}" \
&& check_supported_parameters "sources" "${source}" "${params}"; } \
|| return 1
chain
}
check_software || { printf "%s\n" 'check software'; exit ${?}; }
. ./fbopt