"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "extra/pdfidiff.in" between
littleutils-1.2.3.tar.lz and littleutils-1.2.4.tar.lz

About: littleutils are a collection of small and simple utilities (rename files, search for duplicate files, ...).

pdfidiff.in  (littleutils-1.2.3.tar.lz):pdfidiff.in  (littleutils-1.2.4.tar.lz)
#! PROGBASH #! PROGBASH
# vim: set filetype=bash: # vim: set filetype=bash:
# pdfidiff: Create a new PDF file showing the differences between two specified # pdfidiff: Create a new PDF file showing the differences between two specified
# PDF files. Relies on ghostscript, img2pdf, imagdiff, and (optionally) GNU # PDF files. Relies on ghostscript, img2pdf, imagdiff, pdftocairo, and
# parallel. # (optionally) GNU parallel.
# Copyright (C) 2018-2020 by Brian Lindholm. This file is part of the # Copyright (C) 2018-2021 by Brian Lindholm. This file is part of the
# littleutils utility set. # littleutils utility set.
# #
# The pdfidiff utility is free software; you can redistribute it and/or modify # The pdfidiff utility 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 # it under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later version. # Software Foundation; either version 3, or (at your option) any later version.
# #
# The opt-pdf utility is distributed in the hope that it will be useful, but # The opt-pdf utility is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details. # more details.
# #
# You should have received a copy of the GNU General Public License along with # You should have received a copy of the GNU General Public License along with
# the littleutils. If not, see <https://www.gnu.org/licenses/>. # the littleutils. If not, see <https://www.gnu.org/licenses/>.
# get a valid temporary directory and set up traps
TMPFOLDER=`tempname -D pdfidiff_$$` || exit 99
trap 'rm -rf ${TMPFOLDER} ; exit 1' 1 2 3 13 15
trap 'rm -rf ${TMPFOLDER} ; exit 0' 0
# get command-line options # get command-line options
DEVICE=pnggray DEVICE='pnggray'
DPI=300 declare -i DPI=300
ENGINE=poppler ENGINE='poppler'
FUZZ=1 declare -i FUZZ=1
MODE=2 declare -i MODE=2
OPTIMIZE=n OPTIMIZE='n'
USE_PARALLEL=y USE_PARALLEL='y'
QUIET=n QUIET='n'
while getopts f:ghm:oqr:sv opts while getopts f:ghm:oqr:sv opts
do do
case $opts in case $opts in
f) FUZZ=${OPTARG} ;; f) FUZZ=$OPTARG ;;
g) ENGINE=ghostscript ;; g) ENGINE='ghostscript' ;;
h) echo 'pdfidiff LU_VERSION' h) echo 'pdfidiff LU_VERSION'
echo 'usage: pdfidiff [-h(elp)] [-g(hostscript)] [-f fuzz_dist] [-m mode] ' echo 'usage: pdfidiff [-h(elp)] [-g(hostscript)] [-f fuzz_dist] [-m mode] '
echo ' [-q(uiet)] [-r resolution_DPI] [-o(ptimize)] [-s(ingle_thr eaded)]' echo ' [-q(uiet)] [-r resolution_DPI] [-o(ptimize)] [-s(ingle_thr eaded)]'
echo ' old.pdf new.pdf diff.pdf' echo ' old.pdf new.pdf diff.pdf'
echo ' mode 1 = gray, 2 = light gray, 3 = dark gray, 4 = color,' echo ' mode 1 = gray, 2 = light gray, 3 = dark gray, 4 = color,'
echo ' 5 = stretched color' echo ' 5 = stretched color'
exit 0 ;; exit 0 ;;
m) MODE=${OPTARG} ;; m) MODE=$OPTARG ;;
o) OPTIMIZE=y ;; o) OPTIMIZE='y' ;;
q) QUIET=y ;; q) QUIET='y' ;;
r) DPI=${OPTARG} ;; r) DPI=$OPTARG ;;
s) USE_PARALLEL=n ;; s) USE_PARALLEL='n' ;;
v) QUIET=n ;; v) QUIET='n' ;;
*) echo 'pdfidiff LU_VERSION' *) echo 'pdfidiff LU_VERSION'
echo 'usage: pdfidiff [-h(elp)] [-g(hostscript)] [-f fuzz_dist] [-m mode] ' echo 'usage: pdfidiff [-h(elp)] [-g(hostscript)] [-f fuzz_dist] [-m mode] '
echo ' [-q(uiet)] [-r resolution_DPI] [-o(ptimize)] [-s(ingle_thr eaded)]' echo ' [-q(uiet)] [-r resolution_DPI] [-o(ptimize)] [-s(ingle_thr eaded)]'
echo ' old.pdf new.pdf diff.pdf' echo ' old.pdf new.pdf diff.pdf'
echo ' mode 1 = gray, 2 = light gray, 3 = dark gray, 4 = color,' echo ' mode 1 = gray, 2 = light gray, 3 = dark gray, 4 = color,'
echo ' 5 = stretched color' echo ' 5 = stretched color'
exit 1 ;; exit 1 ;;
esac esac
done done
shift `expr ${OPTIND} - 1` shift $((${OPTIND}-1))
# double-check parameters # double-check parameters
if [ "$MODE" -lt 1 -o "$MODE" -gt 5 ]; then if [ $MODE -lt 1 -o $MODE -gt 5 ]; then
MODE=2 MODE=2
fi fi
command -v parallel > /dev/null command -v parallel > /dev/null
if [ "$?" != '0' ]; then if [ $? -ne 0 ]; then
USE_PARALLEL=n USE_PARALLEL='n'
fi fi
# compute ancilliary parameters # compute ancilliary parameters
if [ "$DPI" -gt 0 ]; then if [ $DPI -gt 0 ]; then
if [ "$DPI" -gt 2400 ]; then if [ $DPI -gt 2400 ]; then
DPI='2400' DPI=2400
fi fi
DPM=$((DPI*10000/254)) DPM=$((DPI*10000/254))
fi fi
if [ "$MODE" = '1' ]; then if [ "$MODE" = '1' ]; then
DEVICE=pnggray DEVICE='pnggray'
CAIROOPT='-gray' CAIROOPT='-gray'
elif [ "$MODE" = '2' ]; then elif [ "$MODE" = '2' ]; then
DEVICE=pnggray DEVICE='pnggray'
CAIROOPT='-gray' CAIROOPT='-gray'
elif [ "$MODE" = '3' ]; then elif [ "$MODE" = '3' ]; then
DEVICE=pnggray DEVICE='pnggray'
CAIROOPT='-gray' CAIROOPT='-gray'
elif [ "$MODE" = '4' ]; then elif [ "$MODE" = '4' ]; then
DEVICE=png16m DEVICE='png16m'
CAIROOPT='' CAIROOPT=''
elif [ "$MODE" = '5' ]; then elif [ "$MODE" = '5' ]; then
DEVICE=png16m DEVICE='png16m'
CAIROOPT='' CAIROOPT=''
fi fi
# make sure that input files and targets are specified # make sure that input files and targets are specified
if [ ! -r "$1" ]; then if [ ! -r "$1" ]; then
echo "pdfidiff error: old input file ${1} is missing" echo "pdfidiff error: old input file ${1} is missing"
exit 1 exit 1
fi fi
if [ ! -r "$2" ]; then if [ ! -r "$2" ]; then
echo "pdfidiff error: new input file ${2} is missing" echo "pdfidiff error: new input file ${2} is missing"
exit 1 exit 1
fi fi
if [ "X$3" = 'X' ]; then if [ "X$3" = 'X' ]; then
echo "pdfidiff error: output file ${3} is unspecified" echo "pdfidiff error: output file ${3} is unspecified"
exit 1 exit 1
fi fi
OLDFILE=`realpath "$1"` OLDFILE=$(realpath "$1")
NEWFILE=`realpath "$2"` NEWFILE=$(realpath "$2")
OUTPUTFILE=`realpath "$3"` OUTPUTFILE=$(realpath "$3")
# change to temporary working directory # set up traps, create and change to temporary folder
pushd "$TMPFOLDER" > /dev/null trap 'rm -rf $TMPFOLDER ; exit 1' 1 2 3 13 15
TMPFOLDER=$(tempname -D pdfidiff_$$) || exit 99
pushd $TMPFOLDER > /dev/null
# explode original files into individual pages # explode original files into individual pages
if [ "$QUIET" = 'n' ]; then if [ "$QUIET" = 'n' ]; then
echo 'pdfidiff message: -BEGIN-' echo 'pdfidiff message: -BEGIN-'
echo "pdfidiff message: splitting (old) $1 and (new) $2 into pages..." echo "pdfidiff message: splitting (old) $1 and (new) $2 into pages..."
fi fi
if [ "$ENGINE" = 'ghostscript' ]; then if [ "$ENGINE" = 'ghostscript' ]; then
if [ "$USE_PARALLEL" = 'n' ]; then if [ "$USE_PARALLEL" = 'n' ]; then
gs -q -dBATCH -dSAFER -dNOPAUSE -sDEVICE=${DEVICE} -r${DPI} -sBandListStorag gs -q -dBATCH -dSAFER -dNOPAUSE -sDEVICE=$DEVICE -r${DPI} -sBandListStorage=
e=memory -sOutputFile=old-%05d.png -f "$OLDFILE" memory -sOutputFile=old-%05d.png -f "$OLDFILE"
gs -q -dBATCH -dSAFER -dNOPAUSE -sDEVICE=${DEVICE} -r${DPI} -sBandListStorag gs -q -dBATCH -dSAFER -dNOPAUSE -sDEVICE=$DEVICE -r${DPI} -sBandListStorage=
e=memory -sOutputFile=new-%05d.png -f "$NEWFILE" memory -sOutputFile=new-%05d.png -f "$NEWFILE"
else else
parallel -s 10000 "gs -q -dBATCH -dSAFER -dNOPAUSE -sDEVICE=${DEVICE} -r${DP I} -sBandListStorage=memory -sOutputFile={1}-%05d.png -f {2}" \ parallel -s 10000 "gs -q -dBATCH -dSAFER -dNOPAUSE -sDEVICE=${DEVICE} -r${DP I} -sBandListStorage=memory -sOutputFile={1}-%05d.png -f {2}" \
::: 'old' 'new' :::+ "$OLDFILE" "$NEWFILE" ::: 'old' 'new' :::+ "$OLDFILE" "$NEWFILE"
fi fi
else else
if [ "$USE_PARALLEL" = 'n' ]; then if [ "$USE_PARALLEL" = 'n' ]; then
pdftocairo -png ${CAIROOPT} -r ${DPI} "$OLDFILE" old pdftocairo -png $CAIROOPT -r $DPI "$OLDFILE" old
pdftocairo -png ${CAIROOPT} -r ${DPI} "$NEWFILE" new pdftocairo -png $CAIROOPT -r $DPI "$NEWFILE" new
else else
parallel -s 10000 "pdftocairo -png ${CAIROOPT} -r ${DPI} {1} {2}" ::: "$OLDF ILE" "$NEWFILE" :::+ 'old' 'new' parallel -s 10000 "pdftocairo -png $CAIROOPT -r $DPI {1} {2}" ::: "$OLDFILE" "$NEWFILE" :::+ 'old' 'new'
fi fi
fi fi
# count image files # count image files
for INDEX in old-*.png ; do for INDEX in old-*.png ; do
touch diff${INDEX#old} touch "diff${INDEX#old}"
if [ ! -r new${INDEX#old} ]; then if [ ! -r "new${INDEX#old}" ]; then
echo "pdfidiff warning: $INDEX from $OLDFILE has no counterpart" echo "pdfidiff warning: $INDEX from $OLDFILE has no counterpart"
fi fi
done done
for INDEX in new-*.png ; do for INDEX in new-*.png ; do
if [ ! -r diff${INDEX#new} ]; then if [ ! -r "diff${INDEX#new}" ]; then
touch diff${INDEX#new} touch "diff${INDEX#new}"
fi fi
if [ ! -r old${INDEX#new} ]; then if [ ! -r "old${INDEX#new}" ]; then
echo "pdfidiff warning: $INDEX from $NEWFILE has no counterpart" echo "pdfidiff warning: $INDEX from $NEWFILE has no counterpart"
fi fi
done done
# create delta image files # create delta image files
if [ "$QUIET" = 'n' ]; then if [ "$QUIET" = 'n' ]; then
echo 'pdfidiff message: calculating difference between old and new pages...' echo 'pdfidiff message: calculating difference between old and new pages...'
fi fi
if [ "$USE_PARALLEL" = 'n' ]; then if [ "$USE_PARALLEL" = 'n' ]; then
for INDEX in diff-*.png ; do for INDEX in diff-*.png ; do
imagdiff -f ${FUZZ} -m ${MODE} old${INDEX#diff} new${INDEX#diff} ${INDEX} imagdiff -f $FUZZ -m $MODE "old${INDEX#diff}" "new${INDEX#diff}" "$INDEX"
done done
else else
parallel -s 10000 --plus "imagdiff -f ${FUZZ} -m ${MODE} old{#diff} new{#diff} {}" ::: diff-*.png parallel -s 10000 --plus "imagdiff -f $FUZZ -m $MODE old{#diff} new{#diff} {}" ::: diff-*.png
fi fi
# take some steps to reduce file size # take some steps to reduce file size
if [ "$OPTIMIZE" = 'y' ]; then if [ "$OPTIMIZE" = 'y' ]; then
# full-blown PNG image optimization if requested # full-blown PNG image optimization if requested
if [ "$QUIET" = 'n' ]; then if [ "$QUIET" = 'n' ]; then
echo 'pdfidiff message: attempting filesize optimization on delta pages...' echo 'pdfidiff message: attempting filesize optimization on delta pages...'
fi fi
if [ "$USE_PARALLEL" = 'n' ]; then if [ "$USE_PARALLEL" = 'n' ]; then
opt-png -q -r ${DPI} diff-*.png opt-png -q -r $DPI diff-*.png
else else
parallel -s 10000 opt-png -q -r ${DPI} ::: diff-*.png parallel -s 10000 opt-png -q -r $DPI ::: diff-*.png
fi fi
else else
# otherwise, attempt colorspace reduction and alpha channel stripping # otherwise, attempt colorspace reduction and alpha channel stripping
if [ "$QUIET" = 'n' ]; then if [ "$QUIET" = 'n' ]; then
echo 'pdfidiff message: attempting colorspace reduction on delta pages...' echo 'pdfidiff message: attempting colorspace reduction on delta pages...'
fi fi
if [ "$USE_PARALLEL" = 'n' ]; then if [ "$USE_PARALLEL" = 'n' ]; then
for INDEX in diff-*.png ; do for INDEX in diff-*.png ; do
pngrecolor -q -n ${INDEX} rc${INDEX#diff} pngrecolor -q -n "$INDEX" "rc${INDEX#diff}"
if [ -s rc${INDEX#diff} ]; then if [ -s "rc${INDEX#diff}" ]; then
mv rc${INDEX#diff} ${INDEX} mv "rc${INDEX#diff}" "$INDEX"
fi fi
done done
else else
parallel -s 10000 --plus 'pngrecolor -q -n {} rc{#diff} ; if [ -s rc{#diff} ]; then mv rc{#diff} {} ; fi' ::: diff-*.png parallel -s 10000 --plus 'pngrecolor -q -n {} rc{#diff} ; if [ -s rc{#diff} ]; then mv rc{#diff} {} ; fi' ::: diff-*.png
fi fi
if [ "$QUIET" = 'n' ]; then if [ "$QUIET" = 'n' ]; then
echo 'pdfidiff message: stripping alpha channel and setting DPI on delta pag es...' echo 'pdfidiff message: stripping alpha channel and setting DPI on delta pag es...'
fi fi
if [ "$USE_PARALLEL" = 'n' ]; then if [ "$USE_PARALLEL" = 'n' ]; then
for INDEX in diff-*.png ; do for INDEX in diff-*.png ; do
pngstrip -a -r ${DPM} ${INDEX} st${INDEX#diff} pngstrip -a -r $DPM "$INDEX" "st${INDEX#diff}"
if [ -s st${INDEX#diff} ]; then if [ -s "st${INDEX#diff}" ]; then
mv st${INDEX#diff} ${INDEX} mv "st${INDEX#diff}" "$INDEX"
fi fi
done done
else else
parallel -s 10000 --plus "pngstrip -a -r ${DPM} {} st{#diff} ; if [ -s st{#d iff} ]; then mv st{#diff} {} ; fi" ::: diff-*.png parallel -s 10000 --plus "pngstrip -a -r $DPM {} st{#diff} ; if [ -s st{#dif f} ]; then mv st{#diff} {} ; fi" ::: diff-*.png
fi fi
fi fi
# convert delta image files to PDF # convert delta image files to PDF
if [ "$QUIET" = 'n' ]; then if [ "$QUIET" = 'n' ]; then
echo "pdfidiff message: combining delta pages files into (output) $3..." echo "pdfidiff message: combining delta pages files into (output) $3..."
fi fi
img2pdf -D --engine=internal -o "$OUTPUTFILE" diff-*.png img2pdf -D --engine=internal -o "$OUTPUTFILE" diff-*.png
# clean up afterwards # clean up afterwards
if [ "$QUIET" = 'n' ]; then if [ "$QUIET" = 'n' ]; then
echo 'pdfidiff message: -END-' echo 'pdfidiff message: -END-'
fi fi
popd > /dev/null popd > /dev/null
rm -rf ${TMPFOLDER} rm -rf $TMPFOLDER
 End of changes. 31 change blocks. 
65 lines changed or deleted 64 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)