"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "scripts/opt-jpg.in" between
littleutils-1.2.3.tar.lz and littleutils-1.2.4.tar.lz

About: littleutils are a collection of small and simple utilities (rename files, search for duplicate files, ...).

opt-jpg.in  (littleutils-1.2.3.tar.lz):opt-jpg.in  (littleutils-1.2.4.tar.lz)
#! PROGDASH #! PROGBASH
# vim: set filetype=sh: # vim: set filetype=bash:
# opt-jpg: Recompresses .jpg files # opt-jpg: Recompresses .jpg files
# Copyright (C) 2004-2020 by Brian Lindholm. This file is part of the # Copyright (C) 2004-2021 by Brian Lindholm. This file is part of the
# littleutils utility set. # littleutils utility set.
# #
# The opt-jpg utility is free software; you can redistribute it and/or modify # The opt-jpg utility is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the Free # it under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later version. # Software Foundation; either version 3, or (at your option) any later version.
# #
# The opt-jpg utility is distributed in the hope that it will be useful, but # The opt-jpg utility is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details. # more details.
# #
# You should have received a copy of the GNU General Public License along with # You should have received a copy of the GNU General Public License along with
# the littleutils. If not, see <https://www.gnu.org/licenses/>. # the littleutils. If not, see <https://www.gnu.org/licenses/>.
# get a valid temporary directory and set up traps
TMPWILD=`tempname -w opt-jpg_$$` || exit 99
trap 'rm -f ${TMPWILD} ; exit 1' 1 2 3 13 15
trap 'rm -f ${TMPWILD} ; exit 0' 0
# get command-line options # get command-line options
ARITHMETIC=n ARITHMETIC='n'
GRAY=n GRAY='n'
MARKERS=none MARKERS='none'
TOUCH=n TOUCH='n'
VERBOSE=y VERBOSE='y'
while getopts aghm:qt opts while getopts aghm:qt opts
do do
case $opts in case $opts in
a) ARITHMETIC=y ;; a) ARITHMETIC='y' ;;
g) GRAY=y ;; g) GRAY='y' ;;
h) echo 'opt-jpg LU_VERSION' h) echo 'opt-jpg LU_VERSION'
echo 'usage: opt-jpg [-a(rithmetic)] [-g(ray)] [-h(elp)] [-m MARKERS]' echo 'usage: opt-jpg [-a(rithmetic)] [-g(ray)] [-h(elp)] [-m MARKERS]'
echo ' [-q(uiet)] [-t(ouch)] JPEG_filename ...' echo ' [-q(uiet)] [-t(ouch)] JPEG_filename ...'
exit 0 ;; exit 0 ;;
m) MARKERS=${OPTARG} ;; m) MARKERS=$OPTARG ;;
q) VERBOSE=n ;; q) VERBOSE='n' ;;
t) TOUCH=y ;; t) TOUCH='y' ;;
*) echo 'opt-jpg LU_VERSION' *) echo 'opt-jpg LU_VERSION'
echo 'usage: opt-jpg [-a(rithmetic)] [-g(ray)] [-h(elp)] [-m MARKERS]' echo 'usage: opt-jpg [-a(rithmetic)] [-g(ray)] [-h(elp)] [-m MARKERS]'
echo ' [-q(uiet)] [-t(ouch)] JPEG_filename ...' echo ' [-q(uiet)] [-t(ouch)] JPEG_filename ...'
exit 1 ;; exit 1 ;;
esac esac
done done
shift `expr ${OPTIND} - 1` shift $((${OPTIND}-1))
# set up traps
trap 'rm -f $TMPJPG1 $TMPJPG2 $TMPJPG3 $TMPJPG4 ; exit 1' 1 2 3 13 15
# run through files # run through files
declare -i S0=0 S2=0 S3=0 SX=0
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
# make sure we can read and modify file # make sure we can read and modify file
if [ ! -f "$1" -o ! -r "$1" -o ! -w "$1" ]; then if [ ! -f "$1" -o ! -r "$1" -o ! -w "$1" ]; then
echo "opt-jpg warning: $1 is not a writeable non-directory file" echo "opt-jpg warning: $1 is not a writeable non-directory file"
shift; continue shift; continue
fi fi
# are we forcing to grayscale? # are we forcing to grayscale?
if [ "$GRAY" = 'y' ]; then if [ "$GRAY" = 'y' ]; then
imagsize "$1" | grep -v -q 'depth=8' imagsize "$1" | grep -v -q 'depth=8'
OUT0="$?" if [ $? -eq 1 ]; then
if [ "$OUT0" -eq 1 ]; then FORCE='n'
FORCE=n
else else
FORCE=y FORCE='y'
fi fi
else else
FORCE=N FORCE='n'
fi fi
# trial 1: optimally recompress # trial 1: optimally recompress
TMPJPGX=`tempname -s .jpg opt-jpg_$$_1` || exit 99 TMPJPG1=$(tempname -s .jpg opt-jpg_$$_1) || exit 99
if [ "$FORCE" = 'y' ]; then if [ "$FORCE" = 'y' ]; then
jpegtran -grayscale -optimize -copy ${MARKERS} -outfile ${TMPJPGX} "$1" jpegtran -grayscale -optimize -copy $MARKERS -outfile $TMPJPG1 "$1"
else else
jpegtran -optimize -copy ${MARKERS} -outfile ${TMPJPGX} "$1" jpegtran -optimize -copy $MARKERS -outfile $TMPJPG1 "$1"
fi fi
OUT1="$?" if [ $? -ne 0 -a $? -ne 2 ]; then
if [ "$OUT1" -ne 0 -a "$OUT1" -ne 2 ]; then echo "opt-jpg error: $1 is a bad jpg file: jpegtran rc = $?"
echo "opt-jpg error: $1 is a bad jpg file: jpegtran rc = ${OUT1}" rm -f $TMPJPG1
rm -f ${TMPJPGX}
shift; continue shift; continue
fi fi
SX=`filesize "$TMPJPGX"` SX=$(filesize $TMPJPG1)
# trial 2: optimally recompress with progressive scan # trial 2: optimally recompress with progressive scan
TMPJPG2=`tempname -s .jpg opt-jpg_$$_2` || exit 99 TMPJPG2=$(tempname -s .jpg opt-jpg_$$_2) || exit 99
if [ "$FORCE" = 'y' ]; then if [ "$FORCE" = 'y' ]; then
jpegtran -grayscale -progressive -optimize -copy ${MARKERS} -outfile ${TMPJP G2} "$1" jpegtran -grayscale -progressive -optimize -copy $MARKERS -outfile $TMPJPG2 "$1"
else else
jpegtran -progressive -optimize -copy ${MARKERS} -outfile ${TMPJPG2} "$1" jpegtran -progressive -optimize -copy $MARKERS -outfile $TMPJPG2 "$1"
fi fi
OUT2="$?" if [ $? -ne 0 -a $? -ne 2 ]; then
if [ "$OUT2" -ne 0 -a "$OUT2" -ne 2 ]; then echo "opt-jpg error: $1 is a bad jpg file: jpegtran rc = $?"
echo "opt-jpg error: $1 is a bad jpg file: jpegtran rc = ${OUT2}" rm -f $TMPJPG1 $TMPJPG2
rm -f ${TMPJPGX} ${TMPJPG2}
shift; continue shift; continue
fi fi
# overwrite previous trial if smaller # overwrite previous trial if smaller
S2=`filesize "$TMPJPG2"` S2=$(filesize $TMPJPG2)
if [ "$S2" -lt "$SX" ]; then if [ $S2 -lt $SX ]; then
mv ${TMPJPG2} ${TMPJPGX} mv $TMPJPG2 $TMPJPG1
SX=${S2} SX=$S2
else else
rm -f ${TMPJPG2} rm -f $TMPJPG2
fi fi
# trial 3: optimally recompress with arithmetic encoding # trial 3: optimally recompress with arithmetic encoding
if [ "$ARITHMETIC" = 'y' ]; then if [ "$ARITHMETIC" = 'y' ]; then
TMPJPG3=`tempname -s .jpg opt-jpg_$$_3` || exit 99 TMPJPG3=$(tempname -s .jpg opt-jpg_$$_3) || exit 99
if [ "$FORCE" = 'y' ]; then if [ "$FORCE" = 'y' ]; then
jpegtran -grayscale -arithmetic -copy ${MARKERS} -outfile ${TMPJPG3} "$1" jpegtran -grayscale -arithmetic -copy $MARKERS -outfile $TMPJPG3 "$1"
else else
jpegtran -arithmetic -copy ${MARKERS} -outfile ${TMPJPG3} "$1" jpegtran -arithmetic -copy $MARKERS -outfile $TMPJPG3 "$1"
fi fi
OUT3="$?" if [ $? -ne 0 -a $? -ne 2 ]; then
if [ "$OUT3" -ne 0 -a "$OUT3" -ne 2 ]; then echo "opt-jpg error: $1 is a bad jpg file: jpegtran rc = $?"
echo "opt-jpg error: $1 is a bad jpg file: jpegtran rc = ${OUT3}" rm -f $TMPJPG1 $TMPJPG2 $TMPJPG3
rm -f ${TMPJPGX} ${TMPJPG2} ${TMPJPG3}
shift; continue shift; continue
fi fi
# overwrite previous trial if smaller # overwrite previous trial if smaller
S3=`filesize "$TMPJPG3"` S3=$(filesize $TMPJPG3)
if [ "$S3" -lt "$SX" ]; then if [ $S3 -lt $SX ]; then
mv ${TMPJPG3} ${TMPJPGX} mv $TMPJPG3 $TMPJPG1
SX=${S3} SX=$S3
else else
rm -f ${TMPJPG3} rm -f $TMPJPG3
fi fi
fi fi
# trial 4: optimally recompress with progressive scan and arithmetic encoding # trial 4: optimally recompress with progressive scan and arithmetic encoding
if [ "$ARITHMETIC" = 'y' ]; then if [ "$ARITHMETIC" = 'y' ]; then
TMPJPG4=`tempname -s .jpg opt-jpg_$$_4` || exit 99 TMPJPG4=$(tempname -s .jpg opt-jpg_$$_4) || exit 99
if [ "$FORCE" = 'y' ]; then if [ "$FORCE" = 'y' ]; then
jpegtran -grayscale -progressive -arithmetic -copy ${MARKERS} -outfile ${T MPJPG4} "$1" jpegtran -grayscale -progressive -arithmetic -copy $MARKERS -outfile $TMPJ PG4 "$1"
else else
jpegtran -progressive -arithmetic -copy ${MARKERS} -outfile ${TMPJPG4} "$1 " jpegtran -progressive -arithmetic -copy $MARKERS -outfile $TMPJPG4 "$1"
fi fi
OUT4="$?" if [ $? -ne 0 -a $? -ne 2 ]; then
if [ "$OUT4" -ne 0 -a "$OUT4" -ne 2 ]; then echo "opt-jpg error: $1 is a bad jpg file: jpegtran rc = $?"
echo "opt-jpg error: $1 is a bad jpg file: jpegtran rc = ${OUT4}" rm -f $TMPJPG1 $TMPJPG2 $TMPJPG3 $TMPJPG4
rm -f ${TMPJPGX} ${TMPJPG2} ${TMPJPG3} ${TMPJPG4}
shift; continue shift; continue
fi fi
# overwrite previous trial if smaller # overwrite previous trial if smaller
S4=`filesize "$TMPJPG4"` S4=$(filesize $TMPJPG4)
if [ "$S4" -lt "$SX" ]; then if [ $S4 -lt $SX ]; then
mv ${TMPJPG4} ${TMPJPGX} mv $TMPJPG4 $TMPJPG1
SX=${S4} SX=$S4
else else
rm -f ${TMPJPG4} rm -f $TMPJPG4
fi fi
fi fi
# ensure that smallest trial is smaller than original file # ensure that smallest trial is smaller than original file
if [ "$TOUCH" = 'y' ]; then if [ "$TOUCH" = 'y' ]; then
touch -r "$1" ${TMPJPGX} touch -r "$1" $TMPJPG1
fi fi
S0=`filesize "$1"` S0=$(filesize "$1")
if [ "$FORCE" = 'y' -o "$SX" -lt "$S0" ]; then if [ "$FORCE" = 'y' -o $SX -lt $S0 ]; then
cp --preserve=timestamps ${TMPJPGX} "$1" cp --preserve=timestamps $TMPJPG1 "$1"
if [ "$VERBOSE" = 'y' ]; then if [ "$VERBOSE" = 'y' ]; then
echo "$1: ${S0} vs. ${SX}" echo "$1: $S0 vs. $SX"
fi fi
else else
if [ "$VERBOSE" = 'y' ]; then if [ "$VERBOSE" = 'y' ]; then
echo "$1: unchanged" echo "$1: unchanged"
fi fi
fi fi
# clean up afterwards # clean up afterwards
rm -f ${TMPJPGX} rm -f $TMPJPG1
shift shift
done done
rm -f ${TMPWILD}
 End of changes. 39 change blocks. 
74 lines changed or deleted 68 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)