"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.4/scripts/opt-png.in" (28 Mar 2021, 4870 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 "opt-png.in": 1.2.3_vs_1.2.4.

    1 #! PROGBASH
    2 # vim: set filetype=bash:
    3 
    4 # opt-png: Recompresses .png files
    5 
    6 # Copyright (C) 2004-2021 by Brian Lindholm.  This file is part of the
    7 # littleutils utility set.
    8 #
    9 # The opt-png utility is free software; you can redistribute it and/or modify
   10 # it under the terms of the GNU General Public License as published by the Free
   11 # Software Foundation; either version 3, or (at your option) any later version.
   12 #
   13 # The opt-png utility is distributed in the hope that it will be useful, but
   14 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   15 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   16 # more details.
   17 #
   18 # You should have received a copy of the GNU General Public License along with
   19 # the littleutils.  If not, see <https://www.gnu.org/licenses/>.
   20 
   21 # get command-line options
   22 declare -i DPI=0
   23 declare -i DPM=0
   24 GRAY='n'
   25 TOUCH='n'
   26 VERBOSE='y'
   27 while getopts ghqr:t opts
   28 do
   29   case $opts in
   30     g) GRAY='y' ;;
   31     h) echo 'opt-png LU_VERSION'
   32        echo 'usage: opt-png [-g(ray)] [-h(elp)] [-q(uiet)] [-t(ouch)] PNG_filename ...'
   33        exit 0 ;;
   34     q) VERBOSE='n' ;;
   35     r) DPI=$OPTARG ;;
   36     t) TOUCH='y' ;;
   37     *) echo 'opt-png LU_VERSION'
   38        echo 'usage: opt-png [-g(ray)] [-h(elp)] [-q(uiet)] [-t(ouch)] PNG_filename ...'
   39        exit 1 ;;
   40   esac
   41 done
   42 shift $((${OPTIND}-1))
   43 if [ $DPI -gt 0 ]; then
   44   DPM=$((${DPI}*10000/254))
   45 fi
   46 
   47 # set up traps
   48 trap 'rm -f $TMPPNG0 $TMPPNG1 $TMPPNG2A $TMPPNG2 ; exit 1' 1 2 3 13 15
   49 
   50 # run through files
   51 declare -i RC=0 S0=0 S1=0 S2=0
   52 while [ $# -gt 0 ]; do
   53 
   54   # make sure we can read and modify file
   55   if [ ! -f "$1" -o ! -r "$1" -o ! -w "$1" ]; then
   56     echo "opt-png warning: $1 is not a writeable non-directory file"
   57     shift; continue
   58   fi
   59 
   60   # are we forcing to grayscale?
   61   if [ "$GRAY" = 'y' ]; then
   62     imagsize "$1" | grep -v -q 'png-gray'
   63     if [ $? -eq 1 ]; then
   64       FORCE=n
   65     else
   66       FORCE=y
   67     fi
   68   else
   69     FORCE=n
   70   fi
   71 
   72   # strip out extraneous information
   73   TMPPNG0=$(tempname -s .png opt-png_$$_0) || exit 99
   74   if [ $DPM -gt 0 ]; then
   75     if [ "$FORCE" = 'y' ]; then
   76       pngstrip -r $DPM -g "$1" $TMPPNG0
   77     else
   78       pngstrip -r $DPM "$1" $TMPPNG0
   79     fi
   80   else
   81     if [ "$FORCE" = 'y' ]; then
   82       pngstrip -g "$1" $TMPPNG0
   83     else
   84       pngstrip "$1" $TMPPNG0
   85     fi
   86   fi
   87   if [ $? -ne 0 ]; then
   88     echo "opt-png error: $1 is a bad png file: pngstrip rc = $?"
   89     rm -f $TMPPNG0
   90     shift; continue
   91   fi
   92 
   93   # optimally recompress
   94   TMPPNG1=$(tempname -s .png opt-png_$$_1) || exit 99
   95   if [ $DPI -gt 0 ]; then
   96     pngcrush -brute -l 9 -res $DPI -s $TMPPNG0 $TMPPNG1
   97   else
   98     pngcrush -brute -l 9 -s $TMPPNG0 $TMPPNG1
   99   fi
  100   if [ $? -ne 0 ]; then
  101     echo "opt-png error: $1 is a bad png file: pngcrush rc = $?"
  102     rm -f $TMPPNG0 $TMPPNG1
  103     shift; continue
  104   fi
  105 
  106   # if not a grayscape PNG file, attempt to color-reduce
  107   imagsize $TMPPNG0 | grep -v -q png-gray
  108   if [ $? -eq 1 ]; then
  109     RC=1
  110   else
  111     TMPPNG2A=$(tempname -s .png opt-png_$$_2a) || exit 99
  112     pngrecolor -q $TMPPNG0 $TMPPNG2A
  113     RC=$?
  114   fi
  115 
  116   # if successful on color-reduction
  117   if [ $RC -eq 0 ]; then
  118 
  119     # optimally recompress color-reduced image
  120     TMPPNG2=$(tempname -s .png opt-png_$$_2) || exit 99
  121     if [ $DPI -gt 0 ]; then
  122       pngcrush -brute -l 9 -res "$DPI" -s $TMPPNG2A $TMPPNG2
  123     else
  124       pngcrush -brute -l 9 -s $TMPPNG2A $TMPPNG2
  125     fi
  126     if [ $? -ne 0 ]; then
  127       echo "opt-png error: bizzare result on $1: pngcrush rc = $?"
  128       rm -f $TMPPNG0 $TMPPNG1 $TMPPNG2A $TMPPNG2
  129       shift; continue
  130     fi
  131 
  132     # ensure that new file is smaller
  133     if [ "$TOUCH" = 'y' ]; then
  134       touch -r "$1" $TMPPNG1 $TMPPNG2
  135     fi
  136     S0=$(filesize "$1")
  137     S1=$(filesize $TMPPNG1)
  138     S2=$(filesize $TMPPNG2)
  139     if [ $S2 -lt $S1 ]; then
  140       if [ "$FORCE" = 'y' -o $S2 -lt $S0 ]; then
  141         cp --preserve=timestamps $TMPPNG2 "$1"
  142         if [ "$VERBOSE" = 'y' ]; then
  143           echo "$1: $S0 vs. $S2"
  144         fi
  145       else
  146         if [ "$VERBOSE" = 'y' ]; then
  147           echo "$1: unchanged"
  148         fi
  149       fi
  150     else
  151       if [ "$FORCE" = 'y' -o $S1 -lt $S0 ]; then
  152         cp --preserve=timestamps $TMPPNG1 "$1"
  153         if [ "$VERBOSE" = 'y' ]; then
  154           echo "$1: $S0 vs. $S1"
  155         fi
  156       else
  157         if [ "$VERBOSE" = 'y' ]; then
  158           echo "$1: unchanged"
  159         fi
  160       fi
  161     fi
  162 
  163   # if color-reduction not successful or skipped
  164   else
  165 
  166     if [ "$TOUCH" = 'y' ]; then
  167       touch -r "$1" $TMPPNG1
  168     fi
  169     S0=$(filesize "$1")
  170     S1=$(filesize $TMPPNG1)
  171     if [ "$FORCE" = 'y' -o $S1 -lt $S0 ]; then
  172       cp --preserve=timestamps $TMPPNG1 "$1"
  173       if [ "$VERBOSE" = 'y' ]; then
  174         echo "$1: $S0 vs. $S1"
  175       fi
  176     else
  177       if [ "$VERBOSE" = 'y' ]; then
  178         echo "$1: unchanged"
  179       fi
  180     fi
  181 
  182   fi
  183 
  184   # clean up afterwards
  185   rm -f $TMPPNG0 $TMPPNG1 $TMPPNG2A $TMPPNG2
  186   shift
  187 
  188 done