Fix #264, 274, 285, 436: Add 'Losslessly rotate image' keyboard shortcuts
[geeqie.git] / plugins / rotate / geeqie-rotate
index 7abe42b..ec82cc0 100755 (executable)
 #!/bin/sh
 
-# This is a helper script that rotate jpeg files using jpegtran
+# This is a helper script that rotate image files according to the metadata
+# requirements: ImageMagick, exiftran, exiv2
 
-rotation=$1
-shift
+GQ_METADATA_DIR="$HOME/.local/share/geeqie/metadata"
 
-for file in "$@" ; do
-       tmp="$file".$$
-       if jpegtran -rotate "$rotation" -copy all -outfile "$tmp" "$file"; then
-               mv -f "$tmp" "$file";
-       else
-               rm -f "$tmp";
+rotate()
+{
+    ext=`echo "${1##*.}" |tr "[:upper:]" "[:lower:]"`
+    [ "x$ext" = "x" ] && return 1 #no extension
+
+    gq_metadata="$GQ_METADATA_DIR/$1.gq.xmp"
+    if [ -f "$gq_metadata" ]; then
+       gq_orientation=`exiv2 -PXkv "$gq_metadata"|grep Xmp.tiff.Orientation|sed -e "s|Xmp.tiff.Orientation *||"`
+    else
+       gq_orientation=
+    fi
+
+    case "$ext" in
+       jpg|jpeg) 
+               [ -n "$gq_orientation" ] && exiv2 -M "set Exif.Image.Orientation $gq_orientation" "$1"
+               if exiftran -aip "$1" ; then
+                   # exiftran ignores xmp, set it manually
+                   exiv2 -M "set Xmp.tiff.Orientation 1" "$1"
+                   #http://dev.exiv2.org/issues/show/639
+                   [ -n "$gq_orientation" ] && exiv2 -M "set Xmp.tiff.Orientation 1" \
+                                                     -M "set Exif.Image.Orientation 1" "$gq_metadata"
+                   return 0
+               fi
+               ;;
+       
+       tif|tiff|png)
+               [ -n "$gq_orientation" ] && exiv2 -M "set Exif.Image.Orientation $gq_orientation" "$1"
+               if mogrify -auto-orient "$1" ; then
+                   # mogrify ignores xmp, set it manually
+                   exiv2 -M "set Xmp.tiff.Orientation 1" "$1"
+                   #http://dev.exiv2.org/issues/show/639
+                   [ -n "$gq_orientation" ] && exiv2 -M "set Xmp.tiff.Orientation 1" \
+                                                     -M "set Exif.Image.Orientation 1" "$gq_metadata"
+                   return 0
+               fi
+               ;;
+       *)      #not supported
+               return 0
+               ;;
+    esac
+}
+
+rotate_image_file()
+{
+       ext=`echo "${3##*.}" |tr "[:upper:]" "[:lower:]"`
+       [ "x$ext" = "x" ] && return 4 #no extension
+
+       case "$ext" in
+       jpg|jpeg)
+               exiftran -i "$1" "$3"
+               return 0
+               ;;
+
+       tif|tiff|png)
+               mogrify $2 "$3"
+               return 0
+               ;;
+
+       *)      #not supported
+               return 4
+               ;;
+       esac
+}
+
+get_sidecars=
+if [ "x$1" = "x-g" ] ; then
+    get_sidecars=yes
+    shift
+fi
+
+rotate_image_file=
+rotation=
+if [ "x$1" = "x-r" ] ; then
+       rotate_image_file=yes
+       shift
+       rotation="$1"
+       shift
+fi
+
+preserve_mtime=
+if [ "x$1" = "x-t" ] ; then
+       preserve_mtime=yes
+       shift
+fi
+
+if [ -n "$rotation" ] ; then
+       if [ "x$rotation" = "x0" ] ; then
+               exit 0
+       fi
+       if [ "x$rotation" = "x2" ] ; then
+               mogrify_param="-flop"
+               exiftran_param="-F"
        fi
+       if [ "x$rotation" = "x3" ] ; then
+               mogrify_param="-rotate 180"
+               exiftran_param="-1"
+       fi
+       if [ "x$rotation" = "x4" ] ; then
+               mogrify_param="-flip"
+               exiftran_param="-f"
+       fi
+       if [ "x$rotation" = "x5" ] ; then
+               mogrify_param="-transpose"
+               exiftran_param="-t"
+       fi
+       if [ "x$rotation" = "x6" ] ; then
+               mogrify_param="-rotate 90"
+               exiftran_param="-9"
+       fi
+       if [ "x$rotation" = "x7" ] ; then
+               mogrify_param="-transverse"
+               exiftran_param="-T"
+       fi
+       if [ "x$rotation" = "x8" ] ; then
+               mogrify_param="-rotate -90"
+               exiftran_param="-2"
+       fi
+fi
+
+# iterate over files on commandline
+for file in "$@" ; do
+    if [ -n "$get_sidecars" ] ; then
+        # we got only one file for each group, typically the main one
+        # get the sidecars:
+        geeqie -r --get-sidecars:"$file" |while read sidecar ; do
+            # the main file is included in the sidecar file list, no special handling is required
+            rotate "$sidecar"
+        done
+    else
+               if [ -n "$rotate_image_file" ] ; then
+                       if [ -n "$preserve_mtime" ] ; then
+                               mtime=`mktemp /tmp/geeqie-rotate.XXXXXXXXXX` || exit 3
+                               touch --reference="$file" "$mtime"
+                       fi
+
+                       rotate_image_file "$exiftran_param" "$mogrify_param" "$file"
+                       ret=$?
+
+                       if [ -n "$preserve_mtime" ] ; then
+                               touch --reference="$mtime" "$file"
+                               rm "$mtime"
+                       fi
+                       if [ $ret -eq 4 ] ; then
+                               exit 4
+                       fi
+               else
+                       rotate "$file"
+               fi
+    fi
 done
+