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