"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.4/extra/pdfidiff.in" (28 Mar 2021, 6900 Bytes) of package /linux/privat/littleutils-1.2.4.tar.lz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "pdfidiff.in": 1.2.3_vs_1.2.4.

    1 #! PROGBASH
    2 # vim: set filetype=bash:
    3 
    4 # pdfidiff: Create a new PDF file showing the differences between two specified
    5 # PDF files.  Relies on ghostscript, img2pdf, imagdiff, pdftocairo, and
    6 # (optionally) GNU parallel.
    7 
    8 # Copyright (C) 2018-2021 by Brian Lindholm.  This file is part of the
    9 # littleutils utility set.
   10 #
   11 # The pdfidiff utility is free software; you can redistribute it and/or modify
   12 # it under the terms of the GNU General Public License as published by the Free
   13 # Software Foundation; either version 3, or (at your option) any later version.
   14 #
   15 # The opt-pdf utility is distributed in the hope that it will be useful, but
   16 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   17 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   18 # more details.
   19 #
   20 # You should have received a copy of the GNU General Public License along with
   21 # the littleutils.  If not, see <https://www.gnu.org/licenses/>.
   22 
   23 # get command-line options
   24 DEVICE='pnggray'
   25 declare -i DPI=300
   26 ENGINE='poppler'
   27 declare -i FUZZ=1
   28 declare -i MODE=2
   29 OPTIMIZE='n'
   30 USE_PARALLEL='y'
   31 QUIET='n'
   32 while getopts f:ghm:oqr:sv opts
   33 do
   34   case $opts in
   35     f) FUZZ=$OPTARG ;;
   36     g) ENGINE='ghostscript' ;;
   37     h) echo 'pdfidiff LU_VERSION'
   38        echo 'usage: pdfidiff [-h(elp)] [-g(hostscript)] [-f fuzz_dist] [-m mode]'
   39        echo '         [-q(uiet)] [-r resolution_DPI] [-o(ptimize)] [-s(ingle_threaded)]'
   40        echo '         old.pdf new.pdf diff.pdf'
   41        echo '       mode 1 = gray, 2 = light gray, 3 = dark gray, 4 = color,'
   42        echo '            5 = stretched color'
   43        exit 0 ;;
   44     m) MODE=$OPTARG ;;
   45     o) OPTIMIZE='y' ;;
   46     q) QUIET='y' ;;
   47     r) DPI=$OPTARG ;;
   48     s) USE_PARALLEL='n' ;;
   49     v) QUIET='n' ;;
   50     *) echo 'pdfidiff LU_VERSION'
   51        echo 'usage: pdfidiff [-h(elp)] [-g(hostscript)] [-f fuzz_dist] [-m mode]'
   52        echo '         [-q(uiet)] [-r resolution_DPI] [-o(ptimize)] [-s(ingle_threaded)]'
   53        echo '         old.pdf new.pdf diff.pdf'
   54        echo '       mode 1 = gray, 2 = light gray, 3 = dark gray, 4 = color,'
   55        echo '            5 = stretched color'
   56        exit 1 ;;
   57   esac
   58 done
   59 shift $((${OPTIND}-1))
   60 
   61 # double-check parameters
   62 if [ $MODE -lt 1 -o $MODE -gt 5 ]; then
   63   MODE=2
   64 fi
   65 command -v parallel > /dev/null
   66 if [ $? -ne 0 ]; then
   67   USE_PARALLEL='n'
   68 fi
   69 
   70 # compute ancilliary parameters
   71 if [ $DPI -gt 0 ]; then
   72   if [ $DPI -gt 2400 ]; then
   73     DPI=2400
   74   fi
   75   DPM=$((DPI*10000/254))
   76 fi
   77 if [ "$MODE" = '1' ]; then
   78   DEVICE='pnggray'
   79   CAIROOPT='-gray'
   80 elif [ "$MODE" = '2' ]; then
   81   DEVICE='pnggray'
   82   CAIROOPT='-gray'
   83 elif [ "$MODE" = '3' ]; then
   84   DEVICE='pnggray'
   85   CAIROOPT='-gray'
   86 elif [ "$MODE" = '4' ]; then
   87   DEVICE='png16m'
   88   CAIROOPT=''
   89 elif [ "$MODE" = '5' ]; then
   90   DEVICE='png16m'
   91   CAIROOPT=''
   92 fi
   93 
   94 # make sure that input files and targets are specified
   95 if [ ! -r "$1" ]; then
   96   echo "pdfidiff error: old input file ${1} is missing"
   97   exit 1
   98 fi
   99 if [ ! -r "$2" ]; then
  100   echo "pdfidiff error: new input file ${2} is missing"
  101   exit 1
  102 fi
  103 if [ "X$3" = 'X' ]; then
  104   echo "pdfidiff error: output file ${3} is unspecified"
  105   exit 1
  106 fi
  107 OLDFILE=$(realpath "$1")
  108 NEWFILE=$(realpath "$2")
  109 OUTPUTFILE=$(realpath "$3")
  110 
  111 # set up traps, create and change to temporary folder
  112 trap 'rm -rf $TMPFOLDER ; exit 1' 1 2 3 13 15
  113 TMPFOLDER=$(tempname -D pdfidiff_$$) || exit 99
  114 pushd $TMPFOLDER > /dev/null
  115 
  116 # explode original files into individual pages
  117 if [ "$QUIET" = 'n' ]; then
  118   echo 'pdfidiff message:  -BEGIN-'
  119   echo "pdfidiff message: splitting (old) $1 and (new) $2 into pages..."
  120 fi
  121 if [ "$ENGINE" = 'ghostscript' ]; then
  122   if [ "$USE_PARALLEL" = 'n' ]; then
  123     gs -q -dBATCH -dSAFER -dNOPAUSE -sDEVICE=$DEVICE -r${DPI} -sBandListStorage=memory -sOutputFile=old-%05d.png -f "$OLDFILE"
  124     gs -q -dBATCH -dSAFER -dNOPAUSE -sDEVICE=$DEVICE -r${DPI} -sBandListStorage=memory -sOutputFile=new-%05d.png -f "$NEWFILE"
  125   else
  126     parallel -s 10000 "gs -q -dBATCH -dSAFER -dNOPAUSE -sDEVICE=${DEVICE} -r${DPI} -sBandListStorage=memory -sOutputFile={1}-%05d.png -f {2}" \
  127       ::: 'old' 'new' :::+ "$OLDFILE" "$NEWFILE"
  128   fi
  129 else
  130   if [ "$USE_PARALLEL" = 'n' ]; then
  131     pdftocairo -png $CAIROOPT -r $DPI "$OLDFILE" old
  132     pdftocairo -png $CAIROOPT -r $DPI "$NEWFILE" new
  133   else
  134     parallel -s 10000 "pdftocairo -png $CAIROOPT -r $DPI {1} {2}" ::: "$OLDFILE" "$NEWFILE" :::+ 'old' 'new'
  135   fi
  136 fi
  137 
  138 # count image files
  139 for INDEX in old-*.png ; do
  140   touch "diff${INDEX#old}"
  141   if [ ! -r "new${INDEX#old}" ]; then
  142     echo "pdfidiff warning: $INDEX from $OLDFILE has no counterpart"
  143   fi
  144 done
  145 for INDEX in new-*.png ; do
  146   if [ ! -r "diff${INDEX#new}" ]; then
  147     touch "diff${INDEX#new}"
  148   fi
  149   if [ ! -r "old${INDEX#new}" ]; then
  150     echo "pdfidiff warning: $INDEX from $NEWFILE has no counterpart"
  151   fi
  152 done
  153 
  154 # create delta image files
  155 if [ "$QUIET" = 'n' ]; then
  156   echo 'pdfidiff message: calculating difference between old and new pages...'
  157 fi
  158 if [ "$USE_PARALLEL" = 'n' ]; then
  159   for INDEX in diff-*.png ; do
  160     imagdiff -f $FUZZ -m $MODE "old${INDEX#diff}" "new${INDEX#diff}" "$INDEX"
  161   done
  162 else
  163   parallel -s 10000 --plus "imagdiff -f $FUZZ -m $MODE old{#diff} new{#diff} {}" ::: diff-*.png
  164 fi
  165 
  166 # take some steps to reduce file size
  167 if [ "$OPTIMIZE" = 'y' ]; then
  168   # full-blown PNG image optimization if requested
  169   if [ "$QUIET" = 'n' ]; then
  170     echo 'pdfidiff message: attempting filesize optimization on delta pages...'
  171   fi
  172   if [ "$USE_PARALLEL" = 'n' ]; then
  173     opt-png -q -r $DPI diff-*.png
  174   else
  175     parallel -s 10000 opt-png -q -r $DPI ::: diff-*.png
  176   fi
  177 else
  178   # otherwise, attempt colorspace reduction and alpha channel stripping
  179   if [ "$QUIET" = 'n' ]; then
  180     echo 'pdfidiff message: attempting colorspace reduction on delta pages...'
  181   fi
  182   if [ "$USE_PARALLEL" = 'n' ]; then
  183     for INDEX in diff-*.png ; do
  184       pngrecolor -q -n "$INDEX" "rc${INDEX#diff}"
  185       if [ -s "rc${INDEX#diff}" ]; then
  186         mv "rc${INDEX#diff}" "$INDEX"
  187       fi
  188     done
  189   else
  190     parallel -s 10000 --plus 'pngrecolor -q -n {} rc{#diff} ; if [ -s rc{#diff} ]; then mv rc{#diff} {} ; fi' ::: diff-*.png
  191   fi
  192   if [ "$QUIET" = 'n' ]; then
  193     echo 'pdfidiff message: stripping alpha channel and setting DPI on delta pages...'
  194   fi
  195   if [ "$USE_PARALLEL" = 'n' ]; then
  196     for INDEX in diff-*.png ; do
  197       pngstrip -a -r $DPM "$INDEX" "st${INDEX#diff}"
  198       if [ -s "st${INDEX#diff}" ]; then
  199         mv "st${INDEX#diff}" "$INDEX"
  200       fi
  201     done
  202   else
  203     parallel -s 10000 --plus "pngstrip -a -r $DPM {} st{#diff} ; if [ -s st{#diff} ]; then mv st{#diff} {} ; fi" ::: diff-*.png
  204   fi
  205 fi
  206 
  207 # convert delta image files to PDF
  208 if [ "$QUIET" = 'n' ]; then
  209   echo "pdfidiff message: combining delta pages files into (output) $3..."
  210 fi
  211 img2pdf -D --engine=internal -o "$OUTPUTFILE" diff-*.png
  212 
  213 # clean up afterwards
  214 if [ "$QUIET" = 'n' ]; then
  215   echo 'pdfidiff message:  -END-'
  216 fi
  217 popd > /dev/null
  218 rm -rf $TMPFOLDER