File: geeqie-rotate

package info (click to toggle)
geeqie 1%3A2.5-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,780 kB
  • sloc: cpp: 110,189; xml: 11,497; sh: 3,681; awk: 124; perl: 88; python: 80; makefile: 23
file content (256 lines) | stat: -rwxr-xr-x 5,064 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/bin/sh

## @file
## @brief This is a helper script that rotates image files according to the metadata
##
## Requirements: ImageMagick, exiftran, exiv2
##

GQ_METADATA_DIR="$HOME/.local/share/geeqie/metadata"

gq_exiftran()
{
	if ! [ -x "$(command -v exiftran)" ]
	then
		zenity --title="Geeqie rotate" --info --width=200 --text="Exiftran is not installed" --window-icon=/usr/local/share/pixmaps/geeqie.png 2> /dev/null
		exit 0
	fi

	exiftran "$@"
}

gq_exiv2()
{
	if ! [ -x "$(command -v exiv2)" ]
	then
		zenity --title="Geeqie rotate" --info --width=200 --text="Exiv2 is not installed" --window-icon=/usr/local/share/pixmaps/geeqie.png 2> /dev/null
		exit 0
	fi

	exiv2 "$@"
}

gq_mogrify()
{
	if ! [ -x "$(command -v mogrify)" ]
	then
		zenity --title="Geeqie rotate" --info --width=200 --text="ImageMagick is not installed" --window-icon=/usr/local/share/pixmaps/geeqie.png 2> /dev/null
		exit 0
	fi

	mogrify "$@"
}

rotate()
{
	ext=$(echo "${1##*.}" | tr "[:upper:]" "[:lower:]")
	[ "$ext" = "" ] && return 1 #no extension

	gq_metadata="$GQ_METADATA_DIR/$1.gq.xmp"
	if [ -f "$gq_metadata" ]
	then
		gq_orientation=$(gq_exiv2 -PXkv "$gq_metadata" | grep Xmp.tiff.Orientation | sed -e "s|Xmp.tiff.Orientation *||")
		# shellcheck disable=2181
		[ $? != 0 ] && exit 1
	else
		gq_orientation=
	fi

	case "$ext" in
		jpg | jpeg)
			if [ -n "$gq_orientation" ]
			then
				gq_exiv2 -M "set Exif.Image.Orientation $gq_orientation" "$1"
				# shellcheck disable=2181
				[ $? != 0 ] && exit 1
			fi
			if gq_exiftran -aip "$1"
			then
				# exiftran ignores xmp, set it manually
				gq_exiv2 -M "set Xmp.tiff.Orientation 1" "$1"
				# shellcheck disable=2181
				[ $? != 0 ] && exit 1
				#http://dev.exiv2.org/issues/639
				if [ -n "$gq_orientation" ]
				then
					gq_exiv2 -M "set Xmp.tiff.Orientation 1" \
						-M "set Exif.Image.Orientation 1" "$gq_metadata"
					# shellcheck disable=2181
					[ $? != 0 ] && exit 1
				fi
				return 0
			else
				exit 1
			fi
			;;

		tif | tiff | png)
			if [ -n "$gq_orientation" ]
			then
				gq_exiv2 -M "set Exif.Image.Orientation $gq_orientation" "$1"
				# shellcheck disable=2181
				[ $? != 0 ] && exit 1
			fi
			if gq_mogrify -auto-orient "$1"
			then
				# mogrify ignores xmp, set it manually
				gq_exiv2 -M "set Xmp.tiff.Orientation 1" "$1"
				# shellcheck disable=2181
				[ $? != 0 ] && exit 1
				#http://dev.exiv2.org/issues/639
				if [ -n "$gq_orientation" ]
				then
					gq_exiv2 -M "set Xmp.tiff.Orientation 1" \
						-M "set Exif.Image.Orientation 1" "$gq_metadata"
					# shellcheck disable=2181
					[ $? != 0 ] && exit 1
				fi
				return 0
			else
				exit 1
			fi
			;;
		*) #not supported
			return 4
			;;
	esac
}

rotate_image_file()
{
	ext=$(echo "${3##*.}" | tr "[:upper:]" "[:lower:]")
	[ "$ext" = "" ] && return 1 #no extension

	case "$ext" in
		jpg | jpeg)
			gq_exiftran -i "$1" "$3"
			# shellcheck disable=2181
			[ $? != 0 ] && return 6
			return 0
			;;

		tif | tiff | png)
			gq_mogrify "$2" "$3"
			# shellcheck disable=2181
			[ $? != 0 ] && return 7
			return 0
			;;

		*) #not supported
			return 4
			;;
	esac
}

get_sidecars=
if [ "$1" = "-g" ]
then
	get_sidecars=yes
	shift
fi

rotate_image_file=
rotation=
if [ "$1" = "-r" ]
then
	rotate_image_file=yes
	shift
	rotation="$1"
	shift
fi

preserve_mtime=
if [ "$1" = "-t" ]
then
	preserve_mtime=yes
	shift
fi

if [ -n "$rotation" ]
then
	if [ "$rotation" = "0" ]
	then
		exit 0
	fi
	if [ "$rotation" = "2" ]
	then
		mogrify_param="-flop"
		exiftran_param="-F"
	fi
	if [ "$rotation" = "3" ]
	then
		mogrify_param="-rotate 180"
		exiftran_param="-1"
	fi
	if [ "$rotation" = "4" ]
	then
		mogrify_param="-flip"
		exiftran_param="-f"
	fi
	if [ "$rotation" = "5" ]
	then
		mogrify_param="-transpose"
		exiftran_param="-t"
	fi
	if [ "$rotation" = "6" ]
	then
		mogrify_param="-rotate 90"
		exiftran_param="-9"
	fi
	if [ "$rotation" = "7" ]
	then
		mogrify_param="-transverse"
		exiftran_param="-T"
	fi
	if [ "$rotation" = "8" ]
	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 --remote --get-sidecars="$file" | while read -r sidecar
		do
			# the main file is included in the sidecar file list, no special handling is required
			[ ! -w "$sidecar" ] && exit 5
			rotate "$sidecar"
			ret=$?
		done
		# Bourne shell runs DO loops in a sub-shell
		ret=$?
		[ "$ret" != 0 ] && exit "$ret"
	else
		[ ! -w "$file" ] && exit 5
		if [ -n "$rotate_image_file" ]
		then
			if [ -n "$preserve_mtime" ]
			then
				mtime=$(mktemp "${TMPDIR:-/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
			[ "$ret" != 0 ] && exit "$ret"
		else
			rotate "$file"
			ret=$?
			[ "$ret" != 0 ] && exit "$ret"
		fi
	fi
done

exit 0