Fix #264, 274, 285, 436: Add 'Losslessly rotate image' keyboard shortcuts
[geeqie.git] / plugins / rotate / geeqie-rotate
1 #!/bin/sh
2
3 # This is a helper script that rotate image files according to the metadata
4 # requirements: ImageMagick, exiftran, exiv2
5
6 GQ_METADATA_DIR="$HOME/.local/share/geeqie/metadata"
7
8 rotate()
9 {
10     ext=`echo "${1##*.}" |tr "[:upper:]" "[:lower:]"`
11     [ "x$ext" = "x" ] && return 1 #no extension
12
13     gq_metadata="$GQ_METADATA_DIR/$1.gq.xmp"
14     if [ -f "$gq_metadata" ]; then
15         gq_orientation=`exiv2 -PXkv "$gq_metadata"|grep Xmp.tiff.Orientation|sed -e "s|Xmp.tiff.Orientation *||"`
16     else
17         gq_orientation=
18     fi
19
20     case "$ext" in
21         jpg|jpeg) 
22                 [ -n "$gq_orientation" ] && exiv2 -M "set Exif.Image.Orientation $gq_orientation" "$1"
23                 if exiftran -aip "$1" ; then
24                     # exiftran ignores xmp, set it manually
25                     exiv2 -M "set Xmp.tiff.Orientation 1" "$1"
26                     #http://dev.exiv2.org/issues/show/639
27                     [ -n "$gq_orientation" ] && exiv2 -M "set Xmp.tiff.Orientation 1" \
28                                                       -M "set Exif.Image.Orientation 1" "$gq_metadata"
29                     return 0
30                 fi
31                 ;;
32         
33         tif|tiff|png)
34                 [ -n "$gq_orientation" ] && exiv2 -M "set Exif.Image.Orientation $gq_orientation" "$1"
35                 if mogrify -auto-orient "$1" ; then
36                     # mogrify ignores xmp, set it manually
37                     exiv2 -M "set Xmp.tiff.Orientation 1" "$1"
38                     #http://dev.exiv2.org/issues/show/639
39                     [ -n "$gq_orientation" ] && exiv2 -M "set Xmp.tiff.Orientation 1" \
40                                                       -M "set Exif.Image.Orientation 1" "$gq_metadata"
41                     return 0
42                 fi
43                 ;;
44         *)      #not supported
45                 return 0
46                 ;;
47     esac
48 }
49
50 rotate_image_file()
51 {
52         ext=`echo "${3##*.}" |tr "[:upper:]" "[:lower:]"`
53         [ "x$ext" = "x" ] && return 4 #no extension
54
55         case "$ext" in
56         jpg|jpeg)
57                 exiftran -i "$1" "$3"
58                 return 0
59                 ;;
60
61         tif|tiff|png)
62                 mogrify $2 "$3"
63                 return 0
64                 ;;
65
66         *)      #not supported
67                 return 4
68                 ;;
69         esac
70 }
71
72 get_sidecars=
73 if [ "x$1" = "x-g" ] ; then
74     get_sidecars=yes
75     shift
76 fi
77
78 rotate_image_file=
79 rotation=
80 if [ "x$1" = "x-r" ] ; then
81         rotate_image_file=yes
82         shift
83         rotation="$1"
84         shift
85 fi
86
87 preserve_mtime=
88 if [ "x$1" = "x-t" ] ; then
89         preserve_mtime=yes
90         shift
91 fi
92
93 if [ -n "$rotation" ] ; then
94         if [ "x$rotation" = "x0" ] ; then
95                 exit 0
96         fi
97         if [ "x$rotation" = "x2" ] ; then
98                 mogrify_param="-flop"
99                 exiftran_param="-F"
100         fi
101         if [ "x$rotation" = "x3" ] ; then
102                 mogrify_param="-rotate 180"
103                 exiftran_param="-1"
104         fi
105         if [ "x$rotation" = "x4" ] ; then
106                 mogrify_param="-flip"
107                 exiftran_param="-f"
108         fi
109         if [ "x$rotation" = "x5" ] ; then
110                 mogrify_param="-transpose"
111                 exiftran_param="-t"
112         fi
113         if [ "x$rotation" = "x6" ] ; then
114                 mogrify_param="-rotate 90"
115                 exiftran_param="-9"
116         fi
117         if [ "x$rotation" = "x7" ] ; then
118                 mogrify_param="-transverse"
119                 exiftran_param="-T"
120         fi
121         if [ "x$rotation" = "x8" ] ; then
122                 mogrify_param="-rotate -90"
123                 exiftran_param="-2"
124         fi
125 fi
126
127 # iterate over files on commandline
128 for file in "$@" ; do
129     if [ -n "$get_sidecars" ] ; then
130         # we got only one file for each group, typically the main one
131         # get the sidecars:
132         geeqie -r --get-sidecars:"$file" |while read sidecar ; do
133             # the main file is included in the sidecar file list, no special handling is required
134             rotate "$sidecar"
135         done
136     else
137                 if [ -n "$rotate_image_file" ] ; then
138                         if [ -n "$preserve_mtime" ] ; then
139                                 mtime=`mktemp /tmp/geeqie-rotate.XXXXXXXXXX` || exit 3
140                                 touch --reference="$file" "$mtime"
141                         fi
142
143                         rotate_image_file "$exiftran_param" "$mogrify_param" "$file"
144                         ret=$?
145
146                         if [ -n "$preserve_mtime" ] ; then
147                                 touch --reference="$mtime" "$file"
148                                 rm "$mtime"
149                         fi
150                         if [ $ret -eq 4 ] ; then
151                                 exit 4
152                         fi
153                 else
154                         rotate "$file"
155                 fi
156     fi
157 done
158