"Fossies" - the Fresh Open Source Software Archive

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

    1 #! PROGBASH
    2 # vim: set filetype=bash:
    3 
    4 # opt-jpg: Recompresses .jpg files
    5 
    6 # Copyright (C) 2004-2021 by Brian Lindholm.  This file is part of the
    7 # littleutils utility set.
    8 #
    9 # The opt-jpg 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-jpg 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 ARITHMETIC='n'
   23 GRAY='n'
   24 MARKERS='none'
   25 TOUCH='n'
   26 VERBOSE='y'
   27 while getopts aghm:qt opts
   28 do
   29   case $opts in
   30     a) ARITHMETIC='y' ;;
   31     g) GRAY='y' ;;
   32     h) echo 'opt-jpg LU_VERSION'
   33        echo 'usage: opt-jpg [-a(rithmetic)] [-g(ray)] [-h(elp)] [-m MARKERS]'
   34        echo '         [-q(uiet)] [-t(ouch)] JPEG_filename ...'
   35        exit 0 ;;
   36     m) MARKERS=$OPTARG ;;
   37     q) VERBOSE='n' ;;
   38     t) TOUCH='y' ;;
   39     *) echo 'opt-jpg LU_VERSION'
   40        echo 'usage: opt-jpg [-a(rithmetic)] [-g(ray)] [-h(elp)] [-m MARKERS]'
   41        echo '         [-q(uiet)] [-t(ouch)] JPEG_filename ...'
   42        exit 1 ;;
   43   esac
   44 done
   45 shift $((${OPTIND}-1))
   46 
   47 # set up traps
   48 trap 'rm -f $TMPJPG1 $TMPJPG2 $TMPJPG3 $TMPJPG4 ; exit 1' 1 2 3 13 15
   49 
   50 # run through files
   51 declare -i S0=0 S2=0 S3=0 SX=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-jpg 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 'depth=8'
   63     if [ $? -eq 1 ]; then
   64       FORCE='n'
   65     else
   66       FORCE='y'
   67     fi
   68   else
   69     FORCE='n'
   70   fi
   71 
   72   # trial 1: optimally recompress
   73   TMPJPG1=$(tempname -s .jpg opt-jpg_$$_1) || exit 99
   74   if [ "$FORCE" = 'y' ]; then
   75     jpegtran -grayscale -optimize -copy $MARKERS -outfile $TMPJPG1 "$1"
   76   else
   77     jpegtran -optimize -copy $MARKERS -outfile $TMPJPG1 "$1"
   78   fi
   79   if [ $? -ne 0 -a $? -ne 2 ]; then
   80     echo "opt-jpg error: $1 is a bad jpg file: jpegtran rc = $?"
   81     rm -f $TMPJPG1
   82     shift; continue
   83   fi
   84   SX=$(filesize $TMPJPG1)
   85 
   86   # trial 2: optimally recompress with progressive scan
   87   TMPJPG2=$(tempname -s .jpg opt-jpg_$$_2) || exit 99
   88   if [ "$FORCE" = 'y' ]; then
   89     jpegtran -grayscale -progressive -optimize -copy $MARKERS -outfile $TMPJPG2 "$1"
   90   else
   91     jpegtran -progressive -optimize -copy $MARKERS -outfile $TMPJPG2 "$1"
   92   fi
   93   if [ $? -ne 0 -a $? -ne 2 ]; then
   94     echo "opt-jpg error: $1 is a bad jpg file: jpegtran rc = $?"
   95     rm -f $TMPJPG1 $TMPJPG2
   96     shift; continue
   97   fi
   98   # overwrite previous trial if smaller
   99   S2=$(filesize $TMPJPG2)
  100   if [ $S2 -lt $SX ]; then
  101     mv $TMPJPG2 $TMPJPG1
  102     SX=$S2
  103   else
  104     rm -f $TMPJPG2
  105   fi
  106 
  107   # trial 3: optimally recompress with arithmetic encoding
  108   if [ "$ARITHMETIC" = 'y' ]; then
  109     TMPJPG3=$(tempname -s .jpg opt-jpg_$$_3) || exit 99
  110     if [ "$FORCE" = 'y' ]; then
  111       jpegtran -grayscale -arithmetic -copy $MARKERS -outfile $TMPJPG3 "$1"
  112     else
  113       jpegtran -arithmetic -copy $MARKERS -outfile $TMPJPG3 "$1"
  114     fi
  115     if [ $? -ne 0 -a $? -ne 2 ]; then
  116       echo "opt-jpg error: $1 is a bad jpg file: jpegtran rc = $?"
  117       rm -f $TMPJPG1 $TMPJPG2 $TMPJPG3
  118       shift; continue
  119     fi
  120     # overwrite previous trial if smaller
  121     S3=$(filesize $TMPJPG3)
  122     if [ $S3 -lt $SX ]; then
  123       mv $TMPJPG3 $TMPJPG1
  124       SX=$S3
  125     else
  126       rm -f $TMPJPG3
  127     fi
  128   fi
  129 
  130   # trial 4: optimally recompress with progressive scan and arithmetic encoding
  131   if [ "$ARITHMETIC" = 'y' ]; then
  132     TMPJPG4=$(tempname -s .jpg opt-jpg_$$_4) || exit 99
  133     if [ "$FORCE" = 'y' ]; then
  134       jpegtran -grayscale -progressive -arithmetic -copy $MARKERS -outfile $TMPJPG4 "$1"
  135     else
  136       jpegtran -progressive -arithmetic -copy $MARKERS -outfile $TMPJPG4 "$1"
  137     fi
  138     if [ $? -ne 0 -a $? -ne 2 ]; then
  139       echo "opt-jpg error: $1 is a bad jpg file: jpegtran rc = $?"
  140       rm -f $TMPJPG1 $TMPJPG2 $TMPJPG3 $TMPJPG4
  141       shift; continue
  142     fi
  143     # overwrite previous trial if smaller
  144     S4=$(filesize $TMPJPG4)
  145     if [ $S4 -lt $SX ]; then
  146       mv $TMPJPG4 $TMPJPG1
  147       SX=$S4
  148     else
  149       rm -f $TMPJPG4
  150     fi
  151   fi
  152 
  153   # ensure that smallest trial is smaller than original file
  154   if [ "$TOUCH" = 'y' ]; then
  155     touch -r "$1" $TMPJPG1
  156   fi
  157   S0=$(filesize "$1")
  158   if [ "$FORCE" = 'y' -o $SX -lt $S0 ]; then
  159     cp --preserve=timestamps $TMPJPG1 "$1"
  160     if [ "$VERBOSE" = 'y' ]; then
  161       echo "$1: $S0 vs. $SX"
  162     fi
  163   else
  164     if [ "$VERBOSE" = 'y' ]; then
  165       echo "$1: unchanged"
  166     fi
  167   fi
  168 
  169   # clean up afterwards
  170   rm -f $TMPJPG1
  171   shift
  172 
  173 done