"Fossies" - the Fresh Open Source Software Archive

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

    1 #! PROGBASH
    2 # vim: set filetype=bash:
    3 
    4 # rot-jpg: Losslessly rotate JPEG files
    5 
    6 # Copyright (C) 2019-2021 by Brian Lindholm.  This file is part of the
    7 # littleutils utility set.
    8 #
    9 # The rot-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 rot-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 DIRECTION='none'
   23 OPTIMIZE='y'
   24 TOUCH='n'
   25 VERBOSE='y'
   26 while getopts fhlqrtu opts
   27 do
   28   case $opts in
   29     f) OPTIMIZE='n' ;;
   30     h) echo 'rot-jpg LU_VERSION'
   31        echo 'usage: rot-jpg [-f(ast)] [-h(elp)] [-l(eft)] [-q(uiet)] [-r(ight)]'
   32        echo '               [-t(ouch)] [-u(pside-down)] JPEG_filename ...'
   33        exit 0 ;;
   34     l) DIRECTION='left' ;;
   35     q) VERBOSE='n' ;;
   36     r) DIRECTION='right' ;;
   37     t) TOUCH='y' ;;
   38     u) DIRECTION='upsidedown' ;;
   39     *) echo 'rot-jpg LU_VERSION'
   40        echo 'usage: rot-jpg [-l(eft)] [-h(elp)] [-o(ptimize)] [-q(uiet)]'
   41        echo '               [-r(ight)] [-t(ouch)] [-u(pside-down)]'
   42        echo '               JPEG_filename ...'
   43        exit 1 ;;
   44   esac
   45 done
   46 shift $((${OPTIND}-1))
   47 if [ "X$DIRECTION" = 'Xnone' ]; then
   48   echo 'rot-jpg error: direction not specified; use -l, -r, or -u option'
   49   exit 1
   50 fi
   51 
   52 # set up traps
   53 trap 'rm -f $TMPJPG ; exit 1' 1 2 3 13 15
   54 
   55 # run through files
   56 while [ $# -gt 0 ]; do
   57 
   58   # make sure we can read and modify file
   59   if [ ! -f "$1" -o ! -r "$1" -o ! -w "$1" ]; then
   60     echo "rot-jpg warning: $1 is not a writeable non-directory file"
   61     shift; continue
   62   fi
   63 
   64   # make sure it's a JPEG
   65   imagsize "$1" | grep -q 'type=jpg'
   66   if [ $? -ne 0 ]; then
   67     echo "rot-jpg warning: $1 is not a JPEG"
   68     shift; continue
   69   fi
   70 
   71   # run through jpegtram
   72   TMPJPG=$(tempname -s .gif rot-jpg_$$) || exit 99
   73   if [ "X$DIRECTION" = 'Xleft' ]; then
   74     jpegtran -trim -rotate 270 "$1" > $TMPJPG
   75   elif [ "X$DIRECTION" = 'Xright' ]; then
   76     jpegtran -trim -rotate 90 "$1" > $TMPJPG
   77   else
   78     jpegtran -trim -rotate 180 "$1" > $TMPJPG
   79   fi
   80 
   81   # optimize image to minimize filesize
   82   if [ "$OPTIMIZE" = 'y' ]; then
   83     opt-jpg -q $TMPJPG
   84   fi
   85 
   86   # copy back to original filename
   87   if [ "$TOUCH" = 'y' ]; then
   88     touch -r "$1" $TMPJPG
   89     cp --preserve=timestamps $TMPJPG "$1"
   90   else
   91     cp $TMPJPG "$1"
   92   fi
   93 
   94   # clean up afterwards
   95   rm -f $TMPJPG
   96   shift
   97 
   98 done