"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.5/extra/pdfquant.in" (29 Oct 2021, 3933 Bytes) of package /linux/privat/littleutils-1.2.5.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.

    1 #! PROGBASH
    2 # vim: set filetype=bash:
    3 
    4 # pdfquant: Reduces the filesize of a PDFs composed of scanned images by
    5 # extracting images, quantizing them, and creating a new PDF.  Relies on
    6 # img2pdf, pdfimages, pdfseparate, and pngquant.
    7 
    8 # Copyright (C) 2021 by Brian Lindholm.  This file is part of the littleutils
    9 # utility set.
   10 #
   11 # The pdfquant 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 pdfquant 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 declare -i COLORS=8
   25 DITHERING='--ordered'
   26 OPTIMIZE='n'
   27 USE_PARALLEL='y'
   28 QUIET='n'
   29 SUFFIX='-or8'
   30 while getopts fhn:oqs opts
   31 do
   32   case $opts in
   33     f) DITHERING='--floyd=1'
   34        SUFFIX='-fs8' ;;
   35     h) echo 'pdfquant LU_VERSION'
   36        echo 'usage: pdfquant [-f(loyd-steinberg)] [-h(elp)] [-n colors] [-o(ptimize)]'
   37        echo '         [-q(uiet)] [-s(ingle_threaded)] pdf_file...'
   38        exit 0 ;;
   39     n) COLORS=$OPTARG ;;
   40     o) OPTIMIZE='y' ;;
   41     q) QUIET='y' ;;
   42     s) USE_PARALLEL='n' ;;
   43     *) echo 'pdfquant LU_VERSION'
   44        echo 'usage: pdfquant [-f(loyd-steinberg)] [-h(elp)] [-n colors] [-o(ptimize)]'
   45        echo '         [-q(uiet)] [-s(ingle_threaded)] pdf_file...'
   46        exit 1 ;;
   47   esac
   48 done
   49 shift $((${OPTIND}-1))
   50 
   51 # double-check parameters
   52 if [ $COLORS -lt 2 ]; then
   53   COLORS=2
   54 elif [ $COLORS -gt 256 ]; then
   55   COLORS=256
   56 fi
   57 command -v parallel > /dev/null
   58 if [ $? -ne 0 ]; then
   59   USE_PARALLEL='n'
   60 fi
   61 
   62 # set up traps and create temporary folder
   63 trap 'rm -rf $TMPFOLDER ; exit 1' 1 2 3 13 15
   64 TMPFOLDER=$(tempname -D pdfquant_$$) || exit 99
   65 
   66 # run through files
   67 while [ $# -gt 0 ]; do
   68 
   69   # make sure that input file is specified
   70   if [ ! -f "$1" -o ! -r "$1" ]; then
   71     echo "pdfquant error: input file ${1} is not a readable file"
   72     shift; continue
   73   fi
   74   INPUTFILE=$(realpath "$1")
   75   pushd $TMPFOLDER > /dev/null
   76 
   77   # explode original files into individual pages
   78   if [ "$QUIET" = 'n' ]; then
   79     echo 'pdfquant message:  -BEGIN-'
   80     echo "pdfquant message: splitting $1 into pages..."
   81   fi
   82   cd $TMPFOLDER
   83   pdfseparate "$INPUTFILE" page-%06d.pdf
   84 
   85   # convert pages into PNG images
   86   if [ "$QUIET" = 'n' ]; then
   87     echo 'pdfquant message: converting pages to PNG...'
   88   fi
   89   if [ "$USE_PARALLEL" = 'n' ]; then
   90     for PAGE in page*.pdf ; do
   91       pdfimages -png $PAGE ${PAGE%.pdf}
   92     done
   93   else
   94     parallel -s 10000 --plus 'pdfimages -png {} {%.pdf}' ::: page*.pdf
   95   fi
   96 
   97   # created quantized versions of PNG images
   98   if [ "$QUIET" = 'n' ]; then
   99     echo 'pdfquant message: quantizing PNG pages...'
  100   fi
  101   if [ "$USE_PARALLEL" = 'n' ]; then
  102     for PNG in page*.png ; do
  103       pngquant -s 1 $DITHERING $COLORS $PNG
  104     done
  105   else
  106     parallel -s 10000 pngquant -s 1 $DITHERING $COLORS ::: page*.png
  107   fi
  108 
  109   # optimize if requested
  110   if [ "$OPTIMIZE" = 'y' ]; then
  111     if [ "$QUIET" = 'n' ]; then
  112       echo 'pdfquant message: optimizing PNG pages...'
  113     fi
  114     if [ "$USE_PARALLEL" = 'n' ]; then
  115       opt-png -q page*${SUFFIX}.png
  116     else
  117       parallel -s 10000 opt-png -q ::: page*${SUFFIX}.png
  118     fi
  119   fi
  120 
  121   # reassemble PNG images into new PDF
  122   if [ "$QUIET" = 'n' ]; then
  123     echo 'pdfquant message: restoring PNG pages to PDF...'
  124   fi
  125   img2pdf -D --engine=internal -o "${INPUTFILE%.pdf}-${COLORS}.pdf" page*${SUFFIX}.png
  126 
  127   # clean up afterwards
  128   if [ "$QUIET" = 'n' ]; then
  129     echo 'pdfidiff message:  -END-'
  130     echo ''
  131   fi
  132   rm -f page*
  133   popd > /dev/null
  134   shift
  135 
  136 done
  137 rmdir ${TMPFOLDER}