"Fossies" - the Fresh Open Source Software Archive

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

    1 #! PROGBASH
    2 # vim: set filetype=bash:
    3 
    4 # recomp-jpg: Recompresses JPEG files to a specified quality level, only
    5 # keeping the new version if it is actually *smaller*.
    6 
    7 # Copyright (C) 2004-2021 by Brian Lindholm.  This file is part of the
    8 # littleutils utility set.
    9 #
   10 # The recomp-jpg utility is free software; you can redistribute it and/or
   11 # modify it under the terms of the GNU General Public License as published by
   12 # the Free Software Foundation; either version 3, or (at your option) any later
   13 # version.
   14 #
   15 # The recomp-jpg 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 ARITHMETIC='n'
   25 MARKERS='none'
   26 declare -i TARGQUAL=85
   27 VERBOSE='y'
   28 while getopts ahqt: opts
   29 do
   30   case $opts in
   31     a) ARITHMETIC='y' ;;
   32     h) echo 'recomp-jpg LU_VERSION'
   33        echo 'usage: recomp-jpg [-a(rithmetic)] [-h(elp)] [-q(uiet)]'
   34        echo '         [-t target_quality] JPEG_filename ...'
   35        exit 0 ;;
   36     q) VERBOSE='n' ;;
   37     t) TARGQUAL=$OPTARG ;;
   38     *) echo 'recomp-jpg LU_VERSION'
   39        echo 'usage: recomp-jpg [-a(rithmetic)] [-h(elp)] [-q(uiet)]'
   40        echo '         [-t target_quality] JPEG_filename ...'
   41        exit 1 ;;
   42   esac
   43 done
   44 shift $((${OPTIND}-1))
   45 
   46 # set up traps
   47 trap 'rm -f $TMPJPG ; exit 1' 1 2 3 13 15
   48 
   49 # run through files
   50 declare -i S0=0 S1=0
   51 while [ $# -gt 0 ]; do
   52 
   53   # make sure we can read and modify file
   54   if [ ! -f "$1" -o ! -r "$1" -o ! -w "$1" ]; then
   55     echo "recomp-jpg error: $1 is not a writeable non-directory file"
   56     shift; continue
   57   fi
   58 
   59   # determine current sampling scheme
   60   SAMPLING=$(imagsize "$1" | perl -n -e '/samp=(\S+)\s/ ; print "$1"')
   61   if [ "X$SAMPLING" = 'Xnone' ]; then
   62     echo "recomp-jpg error: $1 does not appear to be a JPEG file"
   63     shift; continue
   64   fi
   65 
   66   # recompress the file to a new JPEG file
   67   TMPJPG=$(tempname -s .jpg recomp-jpg_$$) || exit 99
   68   djpeg -dct float -bmp "$1" | cjpeg -dct float -quality $TARGQUAL -sample $SAMPLING > $TMPJPG
   69   if [ $? -ne 0 -a $? -ne 2 ]; then
   70     echo "recomp-jpg error: $1 is a bad jpg file: [cd]jpeg rc = $?"
   71     rm -f $TMPJPG
   72     shift; continue
   73   fi
   74 
   75   # optimize and check that the new file is smaller by at least 5%
   76   if [ "$ARITHMETIC" = 'y' ]; then
   77     opt-jpg -a -q $TMPJPG
   78   else
   79     opt-jpg -q $TMPJPG
   80   fi
   81   S0=$(filesize "$1")
   82   S1=$(filesize $TMPJPG)
   83   if [ $((${S1}*20/19)) -lt $S0 ]; then
   84     cp $TMPJPG "$1"
   85     if [ "$VERBOSE" = 'y' ]; then
   86       echo "$1: $S0 vs. $S1"
   87     fi
   88   else
   89     if [ "$VERBOSE" = 'y' ]; then
   90       echo "$1: unchanged"
   91     fi
   92   fi
   93 
   94   # clean up afterwards
   95   rm -f $TMPJPG
   96   shift
   97 
   98 done