"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "scripts/opt-png.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-png.in  (littleutils-1.2.3.tar.lz):opt-png.in  (littleutils-1.2.4.tar.lz)
#! PROGBASH #! PROGBASH
# vim: set filetype=bash: # vim: set filetype=bash:
# opt-png: Recompresses .png files # opt-png: Recompresses .png files
# Copyright (C) 2004-2019 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-png utility is free software; you can redistribute it and/or modify # The opt-png 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-png utility is distributed in the hope that it will be useful, but # The opt-png 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-png_$$` || 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
DPI=0 declare -i DPI=0
DPM=0 declare -i DPM=0
GRAY=n GRAY='n'
TOUCH=n TOUCH='n'
VERBOSE=y VERBOSE='y'
while getopts ghqr:t opts while getopts ghqr:t opts
do do
case $opts in case $opts in
g) GRAY=y ;; g) GRAY='y' ;;
h) echo 'opt-png LU_VERSION' h) echo 'opt-png LU_VERSION'
echo 'usage: opt-png [-g(ray)] [-h(elp)] [-q(uiet)] [-t(ouch)] PNG_filena me ...' echo 'usage: opt-png [-g(ray)] [-h(elp)] [-q(uiet)] [-t(ouch)] PNG_filena me ...'
exit 0 ;; exit 0 ;;
q) VERBOSE=n ;; q) VERBOSE='n' ;;
r) DPI=${OPTARG} ;; r) DPI=$OPTARG ;;
t) TOUCH=y ;; t) TOUCH='y' ;;
*) echo 'opt-png LU_VERSION' *) echo 'opt-png LU_VERSION'
echo 'usage: opt-png [-g(ray)] [-h(elp)] [-q(uiet)] [-t(ouch)] PNG_filena me ...' echo 'usage: opt-png [-g(ray)] [-h(elp)] [-q(uiet)] [-t(ouch)] PNG_filena me ...'
exit 1 ;; exit 1 ;;
esac esac
done done
shift `expr ${OPTIND} - 1` shift $((${OPTIND}-1))
if [ "$DPI" -gt 0 ]; then if [ $DPI -gt 0 ]; then
DPM=$((${DPI}*10000/254)) DPM=$((${DPI}*10000/254))
fi fi
# set up traps
trap 'rm -f $TMPPNG0 $TMPPNG1 $TMPPNG2A $TMPPNG2 ; exit 1' 1 2 3 13 15
# run through files # run through files
declare -i RC=0 S0=0 S1=0 S2=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-png warning: $1 is not a writeable non-directory file" echo "opt-png 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 'png-gray' imagsize "$1" | grep -v -q 'png-gray'
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
# strip out extraneous information # strip out extraneous information
TMPPNG0=`tempname -s .png opt-png_$$_0` || exit 99 TMPPNG0=$(tempname -s .png opt-png_$$_0) || exit 99
if [ "$DPM" -gt 0 ]; then if [ $DPM -gt 0 ]; then
if [ "$FORCE" = 'y' ]; then if [ "$FORCE" = 'y' ]; then
pngstrip -r "$DPM" -g "$1" ${TMPPNG0} pngstrip -r $DPM -g "$1" $TMPPNG0
else else
pngstrip -r "$DPM" "$1" ${TMPPNG0} pngstrip -r $DPM "$1" $TMPPNG0
fi fi
else else
if [ "$FORCE" = 'y' ]; then if [ "$FORCE" = 'y' ]; then
pngstrip -g "$1" ${TMPPNG0} pngstrip -g "$1" $TMPPNG0
else else
pngstrip "$1" ${TMPPNG0} pngstrip "$1" $TMPPNG0
fi fi
fi fi
OUT0="$?" if [ $? -ne 0 ]; then
if [ "$OUT0" -ne 0 ]; then echo "opt-png error: $1 is a bad png file: pngstrip rc = $?"
echo "opt-png error: $1 is a bad png file: pngstrip rc = ${OUT0}" rm -f $TMPPNG0
rm -f ${TMPPNG0}
shift; continue shift; continue
fi fi
# optimally recompress # optimally recompress
TMPPNG1=`tempname -s .png opt-png_$$_1` || exit 99 TMPPNG1=$(tempname -s .png opt-png_$$_1) || exit 99
if [ "$DPI" -gt 0 ]; then if [ $DPI -gt 0 ]; then
pngcrush -brute -l 9 -res "$DPI" -s ${TMPPNG0} ${TMPPNG1} pngcrush -brute -l 9 -res $DPI -s $TMPPNG0 $TMPPNG1
else else
pngcrush -brute -l 9 -s ${TMPPNG0} ${TMPPNG1} pngcrush -brute -l 9 -s $TMPPNG0 $TMPPNG1
fi fi
OUT1="$?" if [ $? -ne 0 ]; then
if [ "$OUT1" -ne 0 ]; then echo "opt-png error: $1 is a bad png file: pngcrush rc = $?"
echo "opt-png error: $1 is a bad png file: pngcrush rc = ${OUT1}" rm -f $TMPPNG0 $TMPPNG1
rm -f ${TMPPNG0} ${TMPPNG1}
shift; continue shift; continue
fi fi
# if not a grayscape PNG file, attempt to color-reduce # if not a grayscape PNG file, attempt to color-reduce
imagsize ${TMPPNG0} | grep -v -q png-gray imagsize $TMPPNG0 | grep -v -q png-gray
OUT1A="$?" if [ $? -eq 1 ]; then
if [ "$OUT1A" -eq 1 ]; then RC=1
OUT2=1
else else
TMPPNG2A=`tempname -s .png opt-png_$$_2a` || exit 99 TMPPNG2A=$(tempname -s .png opt-png_$$_2a) || exit 99
pngrecolor -q ${TMPPNG0} ${TMPPNG2A} pngrecolor -q $TMPPNG0 $TMPPNG2A
OUT2="$?" RC=$?
fi fi
# if successful on color-reduction # if successful on color-reduction
if [ "$OUT2" -eq 0 ]; then if [ $RC -eq 0 ]; then
# optimally recompress color-reduced image # optimally recompress color-reduced image
TMPPNG2=`tempname -s .png opt-png_$$_2` || exit 99 TMPPNG2=$(tempname -s .png opt-png_$$_2) || exit 99
if [ "$DPI" -gt 0 ]; then if [ $DPI -gt 0 ]; then
pngcrush -brute -l 9 -res "$DPI" -s ${TMPPNG2A} ${TMPPNG2} pngcrush -brute -l 9 -res "$DPI" -s $TMPPNG2A $TMPPNG2
else else
pngcrush -brute -l 9 -s ${TMPPNG2A} ${TMPPNG2} pngcrush -brute -l 9 -s $TMPPNG2A $TMPPNG2
fi fi
OUT3="$?" if [ $? -ne 0 ]; then
if [ "$OUT3" -ne 0 ]; then echo "opt-png error: bizzare result on $1: pngcrush rc = $?"
echo "opt-png error: bizzare result on $1: pngcrush rc = ${OUT3}" rm -f $TMPPNG0 $TMPPNG1 $TMPPNG2A $TMPPNG2
rm -f ${TMPPNG0} ${TMPPNG1} ${TMPPNG2A} ${TMPPNG2}
shift; continue shift; continue
fi fi
# ensure that new file is smaller # ensure that new file is smaller
if [ "$TOUCH" = 'y' ]; then if [ "$TOUCH" = 'y' ]; then
touch -r "$1" ${TMPPNG1} ${TMPPNG2} touch -r "$1" $TMPPNG1 $TMPPNG2
fi fi
S0=`filesize "$1"` S0=$(filesize "$1")
S1=`filesize "$TMPPNG1"` S1=$(filesize $TMPPNG1)
S2=`filesize "$TMPPNG2"` S2=$(filesize $TMPPNG2)
if [ "$S2" -lt "$S1" ]; then if [ $S2 -lt $S1 ]; then
if [ "$FORCE" = 'y' -o "$S2" -lt "$S0" ]; then if [ "$FORCE" = 'y' -o $S2 -lt $S0 ]; then
cp --preserve=timestamps ${TMPPNG2} "$1" cp --preserve=timestamps $TMPPNG2 "$1"
if [ "$VERBOSE" = 'y' ]; then if [ "$VERBOSE" = 'y' ]; then
echo "$1: ${S0} vs. ${S2}" echo "$1: $S0 vs. $S2"
fi fi
else else
if [ "$VERBOSE" = 'y' ]; then if [ "$VERBOSE" = 'y' ]; then
echo "$1: unchanged" echo "$1: unchanged"
fi fi
fi fi
else else
if [ "$FORCE" = 'y' -o "$S1" -lt "$S0" ]; then if [ "$FORCE" = 'y' -o $S1 -lt $S0 ]; then
cp --preserve=timestamps ${TMPPNG1} "$1" cp --preserve=timestamps $TMPPNG1 "$1"
if [ "$VERBOSE" = 'y' ]; then if [ "$VERBOSE" = 'y' ]; then
echo "$1: ${S0} vs. ${S1}" echo "$1: $S0 vs. $S1"
fi fi
else else
if [ "$VERBOSE" = 'y' ]; then if [ "$VERBOSE" = 'y' ]; then
echo "$1: unchanged" echo "$1: unchanged"
fi fi
fi fi
fi fi
# if color-reduction not successful or skipped # if color-reduction not successful or skipped
else else
if [ "$TOUCH" = 'y' ]; then if [ "$TOUCH" = 'y' ]; then
touch -r "$1" ${TMPPNG1} touch -r "$1" $TMPPNG1
fi fi
S0=`filesize "$1"` S0=$(filesize "$1")
S1=`filesize "$TMPPNG1"` S1=$(filesize $TMPPNG1)
if [ "$FORCE" = 'y' -o "$S1" -lt "$S0" ]; then if [ "$FORCE" = 'y' -o $S1 -lt $S0 ]; then
cp --preserve=timestamps ${TMPPNG1} "$1" cp --preserve=timestamps $TMPPNG1 "$1"
if [ "$VERBOSE" = 'y' ]; then if [ "$VERBOSE" = 'y' ]; then
echo "$1: ${S0} vs. ${S1}" echo "$1: $S0 vs. $S1"
fi fi
else else
if [ "$VERBOSE" = 'y' ]; then if [ "$VERBOSE" = 'y' ]; then
echo "$1: unchanged" echo "$1: unchanged"
fi fi
fi fi
fi fi
# clean up afterwards # clean up afterwards
rm -f ${TMPPNG0} ${TMPPNG1} ${TMPPNG2A} ${TMPPNG2} rm -f $TMPPNG0 $TMPPNG1 $TMPPNG2A $TMPPNG2
shift shift
done done
rm -f ${TMPWILD}
 End of changes. 33 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)