Mercurial > geeqie
annotate src/exif-common.c @ 363:5b8617c3821c
collection_load(): accept whitespaces at start of lines.
author | zas_ |
---|---|
date | Mon, 14 Apr 2008 23:08:27 +0000 |
parents | bda275cf9821 |
children | eff049b3d308 |
rev | line source |
---|---|
182 | 1 /* |
2 * GQView | |
3 * (C) 2006 John Ellis | |
4 * | |
5 */ | |
6 | |
7 #ifdef HAVE_CONFIG_H | |
8 # include "config.h" | |
9 #endif | |
10 | |
11 #include <stdio.h> | |
12 #include <string.h> | |
13 #include <fcntl.h> | |
14 #include <unistd.h> | |
15 #include <sys/types.h> | |
16 #include <sys/stat.h> | |
17 #include <sys/mman.h> | |
18 #include <math.h> | |
19 | |
20 #include <glib.h> | |
21 | |
22 #include "intl.h" | |
23 | |
281 | 24 #include "main.h" |
182 | 25 #include "exif.h" |
26 | |
239
cccba3e30a44
Remove two unused variables declarations, and add a missing #include.
zas_
parents:
222
diff
changeset
|
27 #include "filelist.h" |
182 | 28 #include "format_raw.h" |
29 #include "ui_fileops.h" | |
30 | |
31 | |
32 /* human readable key list */ | |
33 | |
34 ExifFormattedText ExifFormattedList[] = { | |
35 { "fCamera", N_("Camera") }, | |
36 { "fDateTime", N_("Date") }, | |
37 { "fShutterSpeed", N_("Shutter speed") }, | |
38 { "fAperture", N_("Aperture") }, | |
39 { "fExposureBias", N_("Exposure bias") }, | |
40 { "fISOSpeedRating", N_("ISO sensitivity") }, | |
41 { "fFocalLength", N_("Focal length") }, | |
222
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
42 { "fFocalLength35mmFilm",N_("Focal length 35mm") }, |
182 | 43 { "fSubjectDistance", N_("Subject distance") }, |
44 { "fFlash", N_("Flash") }, | |
45 { "fResolution", N_("Resolution") }, | |
46 { NULL, NULL } | |
47 }; | |
48 | |
49 double exif_rational_to_double(ExifRational *r, gint sign) | |
50 { | |
51 if (!r || r->den == 0.0) return 0.0; | |
52 | |
53 if (sign) return (double)((int)r->num) / (double)((int)r->den); | |
54 return (double)r->num / r->den; | |
55 } | |
56 | |
57 double exif_get_rational_as_double(ExifData *exif, const gchar *key) | |
58 { | |
59 ExifRational *r; | |
60 gint sign; | |
61 | |
62 r = exif_get_rational(exif, key, &sign); | |
63 return exif_rational_to_double(r, sign); | |
64 } | |
65 | |
66 static GString *append_comma_text(GString *string, const gchar *text) | |
67 { | |
68 string = g_string_append(string, ", "); | |
69 string = g_string_append(string, text); | |
70 | |
71 return string; | |
72 } | |
73 | |
222
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
74 static gchar *remove_common_prefix(gchar *s, gchar *t) |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
75 { |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
76 gint i; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
77 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
78 if (!s || !t) return t; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
79 |
297
76cdc3f1fe34
Fix broken remove_common_prefix(), fCamera didn't display model as it should.
zas_
parents:
281
diff
changeset
|
80 for (i = 0; s[i] && t[i] && s[i] == t[i]; i++) |
222
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
81 ; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
82 if (!i) |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
83 return t; |
300 | 84 if (s[i-1] == ' ' || !s[i]) |
222
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
85 { |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
86 while (t[i] == ' ') |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
87 i++; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
88 return t + i; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
89 } |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
90 return s; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
91 } |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
92 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
93 static double get_crop_factor(ExifData *exif) |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
94 { |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
95 double res_unit_tbl[] = {0.0, 25.4, 25.4, 10.0, 1.0, 0.001 }; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
96 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
97 double xres = exif_get_rational_as_double(exif, "Exif.Photo.FocalPlaneXResolution"); |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
98 double yres = exif_get_rational_as_double(exif, "Exif.Photo.FocalPlaneYResolution"); |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
99 int res_unit; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
100 int w, h; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
101 double xsize, ysize, size, ratio; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
102 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
103 if (xres == 0.0 || yres == 0.0) return 0.0; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
104 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
105 if (!exif_get_integer(exif, "Exif.Photo.FocalPlaneResolutionUnit", &res_unit)) return 0.0; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
106 if (res_unit < 1 || res_unit > 5) return 0.0; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
107 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
108 if (!exif_get_integer(exif, "Exif.Photo.PixelXDimension", &w)) return 0.0; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
109 if (!exif_get_integer(exif, "Exif.Photo.PixelYDimension", &h)) return 0.0; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
110 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
111 xsize = w * res_unit_tbl[res_unit] / xres; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
112 ysize = h * res_unit_tbl[res_unit] / yres; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
113 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
114 ratio = xsize / ysize; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
115 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
116 if (ratio < 0.5 || ratio > 2.0) return 0.0; /* reasonable ratio */ |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
117 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
118 size = sqrt(xsize * xsize + ysize * ysize); |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
119 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
120 if (size < 1.0 || size > 100.0) return 0.0; /* reasonable sensor size in mm */ |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
121 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
122 return sqrt(36*36+24*24) / size; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
123 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
124 } |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
125 |
182 | 126 |
127 gchar *exif_get_formatted_by_key(ExifData *exif, const gchar *key, gint *key_valid) | |
128 { | |
129 /* must begin with f, else not formatted */ | |
130 if (key[0] != 'f') | |
131 { | |
132 if (key_valid) *key_valid = FALSE; | |
133 return NULL; | |
134 } | |
135 | |
136 if (key_valid) *key_valid = TRUE; | |
137 | |
138 if (strcmp(key, "fCamera") == 0) | |
139 { | |
140 gchar *text; | |
141 gchar *make = exif_get_data_as_text(exif, "Exif.Image.Make"); | |
142 gchar *model = exif_get_data_as_text(exif, "Exif.Image.Model"); | |
143 gchar *software = exif_get_data_as_text(exif, "Exif.Image.Software"); | |
222
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
144 gchar *model2; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
145 gchar *software2; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
146 gint i; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
147 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
148 if (make) |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
149 { |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
150 g_strstrip(make); |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
151 #define REMOVE_SUFFIX(str,suff) \ |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
152 do { \ |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
153 if (g_str_has_suffix(str,suff)) \ |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
154 str[strlen(str)-(sizeof(suff)-1)] = 0; \ |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
155 } while(0) |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
156 REMOVE_SUFFIX(make," Corporation"); /* Pentax */ |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
157 REMOVE_SUFFIX(make," OPTICAL CO.,LTD"); /* OLYMPUS */ |
300 | 158 REMOVE_SUFFIX(make," CORPORATION"); /* Nikon */ |
222
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
159 } |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
160 if (model) |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
161 g_strstrip(model); |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
162 if (software) |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
163 g_strstrip(software); |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
164 /* remove superfluous spaces (pentax K100D) */ |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
165 for (i=0; software && software[i]; i++) |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
166 if (software[i] == ' ' && software[i+1] == ' ') |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
167 { |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
168 gint j; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
169 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
170 for (j=1; software[i+j]; j++) |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
171 if (software[i+j] != ' ') |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
172 break; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
173 memmove(software+i+1, software+i+j, strlen(software+i+j)+1); |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
174 } |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
175 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
176 model2 = remove_common_prefix(make, model); |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
177 software2 = remove_common_prefix(model2, software); |
182 | 178 |
300 | 179 text = g_strdup_printf("%s%s%s%s%s%s", (make) ? make : "", (make && model2) ? " " : "", |
222
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
180 (model2) ? model2 : "", |
300 | 181 (software2 && (make || model2)) ? " (" : "", |
222
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
182 (software2) ? software2 : "", |
300 | 183 (software2 && (make || model2)) ? ")" : ""); |
182 | 184 |
185 g_free(make); | |
186 g_free(model); | |
187 g_free(software); | |
188 return text; | |
189 } | |
190 if (strcmp(key, "fDateTime") == 0) | |
191 { | |
192 gchar *text = exif_get_data_as_text(exif, "Exif.Photo.DateTimeOriginal"); | |
193 gchar *subsec = NULL; | |
194 if (text) subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTimeOriginal"); | |
195 if (!text) | |
196 { | |
197 text = exif_get_data_as_text(exif, "Exif.Image.DateTime"); | |
198 if (text) subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTime"); | |
199 } | |
200 if (subsec) | |
201 { | |
202 gchar *tmp = text; | |
203 text = g_strconcat(tmp, ".", subsec, NULL); | |
204 g_free(tmp); | |
205 g_free(subsec); | |
206 } | |
207 return text; | |
208 } | |
209 if (strcmp(key, "fShutterSpeed") == 0) | |
210 { | |
211 ExifRational *r; | |
212 | |
213 r = exif_get_rational(exif, "Exif.Photo.ExposureTime", NULL); | |
214 if (r && r->num && r->den) | |
215 { | |
216 double n = (double)r->den / (double)r->num; | |
217 return g_strdup_printf("%s%.0fs", n > 1.0 ? "1/" : "", | |
218 n > 1.0 ? n : 1.0 / n); | |
219 } | |
220 r = exif_get_rational(exif, "Exif.Photo.ShutterSpeedValue", NULL); | |
221 if (r && r->num && r->den) | |
222 { | |
223 double n = pow(2.0, exif_rational_to_double(r, TRUE)); | |
224 | |
225 /* Correct exposure time to avoid values like 1/91s (seen on Minolta DImage 7) */ | |
226 if (n > 1.0 && (int)n - ((int)(n/10))*10 == 1) n--; | |
227 | |
228 return g_strdup_printf("%s%.0fs", n > 1.0 ? "1/" : "", | |
229 n > 1.0 ? floor(n) : 1.0 / n); | |
230 } | |
231 return NULL; | |
232 } | |
233 if (strcmp(key, "fAperture") == 0) | |
234 { | |
235 double n; | |
236 | |
237 n = exif_get_rational_as_double(exif, "Exif.Photo.FNumber"); | |
238 if (n == 0.0) n = exif_get_rational_as_double(exif, "Exif.Photo.ApertureValue"); | |
239 if (n == 0.0) return NULL; | |
240 | |
241 return g_strdup_printf("f/%.1f", n); | |
242 } | |
243 if (strcmp(key, "fExposureBias") == 0) | |
244 { | |
245 ExifRational *r; | |
246 gint sign; | |
247 double n; | |
248 | |
249 r = exif_get_rational(exif, "Exif.Photo.ExposureBiasValue", &sign); | |
250 if (!r) return NULL; | |
251 | |
252 n = exif_rational_to_double(r, sign); | |
253 return g_strdup_printf("%+.1f", n); | |
254 } | |
255 if (strcmp(key, "fFocalLength") == 0) | |
256 { | |
257 double n; | |
258 | |
259 n = exif_get_rational_as_double(exif, "Exif.Photo.FocalLength"); | |
260 if (n == 0.0) return NULL; | |
222
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
261 return g_strdup_printf("%.0f mm", n); |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
262 } |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
263 if (strcmp(key, "fFocalLength35mmFilm") == 0) |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
264 { |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
265 gint n; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
266 double f, c; |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
267 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
268 if (exif_get_integer(exif, "Exif.Photo.FocalLengthIn35mmFilm", &n) && n != 0) |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
269 { |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
270 return g_strdup_printf("%d mm", n); |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
271 } |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
272 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
273 f = exif_get_rational_as_double(exif, "Exif.Photo.FocalLength"); |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
274 c = get_crop_factor(exif); |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
275 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
276 if (f != 0.0 && c != 0.0) |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
277 { |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
278 return g_strdup_printf("%.0f mm", f * c); |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
279 } |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
280 |
77f1bcc6c161
various exif improvements based on patch by Uwe Ohse
nadvornik
parents:
212
diff
changeset
|
281 return NULL; |
182 | 282 } |
283 if (strcmp(key, "fISOSpeedRating") == 0) | |
284 { | |
285 gchar *text; | |
286 | |
287 text = exif_get_data_as_text(exif, "Exif.Photo.ISOSpeedRatings"); | |
288 /* kodak may set this instead */ | |
289 if (!text) text = exif_get_data_as_text(exif, "Exif.Photo.ExposureIndex"); | |
290 return text; | |
291 } | |
292 if (strcmp(key, "fSubjectDistance") == 0) | |
293 { | |
294 ExifRational *r; | |
295 gint sign; | |
296 double n; | |
297 | |
298 r = exif_get_rational(exif, "Exif.Photo.SubjectDistance", &sign); | |
299 if (!r) return NULL; | |
300 | |
301 if ((long)r->num == 0xffffffff) return g_strdup(_("infinity")); | |
302 if ((long)r->num == 0) return g_strdup(_("unknown")); | |
303 | |
304 n = exif_rational_to_double(r, sign); | |
305 if (n == 0.0) return _("unknown"); | |
306 return g_strdup_printf("%.3f m", n); | |
307 } | |
308 if (strcmp(key, "fFlash") == 0) | |
309 { | |
310 /* grr, flash is a bitmask... */ | |
311 GString *string; | |
312 gchar *text; | |
313 gint n; | |
314 gint v; | |
315 | |
316 if (!exif_get_integer(exif, "Exif.Photo.Flash", &n)) return NULL; | |
317 | |
318 /* Exif 2.1 only defines first 3 bits */ | |
184 | 319 if (n <= 0x07) return exif_get_data_as_text(exif, "Exif.Photo.Flash"); |
182 | 320 |
321 /* must be Exif 2.2 */ | |
322 string = g_string_new(""); | |
323 | |
324 /* flash fired (bit 0) */ | |
325 string = g_string_append(string, (n & 0x01) ? _("yes") : _("no")); | |
326 | |
327 /* flash mode (bits 3, 4) */ | |
328 v = (n >> 3) & 0x03; | |
329 if (v) string = append_comma_text(string, _("mode:")); | |
330 switch (v) | |
331 { | |
332 case 1: | |
333 string = g_string_append(string, _("on")); | |
334 break; | |
335 case 2: | |
336 string = g_string_append(string, _("off")); | |
337 break; | |
338 case 3: | |
339 string = g_string_append(string, _("auto")); | |
340 break; | |
341 } | |
342 | |
343 /* return light (bits 1, 2) */ | |
344 v = (n >> 1) & 0x03; | |
345 if (v == 2) string = append_comma_text(string, _("not detected by strobe")); | |
346 if (v == 3) string = append_comma_text(string, _("detected by strobe")); | |
347 | |
348 /* we ignore flash function (bit 5) */ | |
349 | |
350 /* red-eye (bit 6) */ | |
351 if ((n >> 5) & 0x01) string = append_comma_text(string, _("red-eye reduction")); | |
352 | |
353 text = string->str; | |
354 g_string_free(string, FALSE); | |
355 return text; | |
356 } | |
357 if (strcmp(key, "fResolution") == 0) | |
358 { | |
359 ExifRational *rx, *ry; | |
360 gchar *units; | |
361 gchar *text; | |
362 | |
363 rx = exif_get_rational(exif, "Exif.Image.XResolution", NULL); | |
364 ry = exif_get_rational(exif, "Exif.Image.YResolution", NULL); | |
365 if (!rx || !ry) return NULL; | |
366 | |
367 units = exif_get_data_as_text(exif, "Exif.Image.ResolutionUnit"); | |
368 text = g_strdup_printf("%0.f x %0.f (%s/%s)", rx->den ? (double)rx->num / rx->den : 1.0, | |
369 ry->den ? (double)ry->num / ry->den : 1.0, | |
370 _("dot"), (units) ? units : _("unknown")); | |
371 | |
372 g_free(units); | |
373 return text; | |
374 } | |
375 | |
376 if (key_valid) *key_valid = FALSE; | |
377 return NULL; | |
378 } | |
379 | |
380 const gchar *exif_get_description_by_key(const gchar *key) | |
381 { | |
382 gint i; | |
383 | |
384 if (!key) return NULL; | |
385 | |
386 i = 0; | |
387 while (ExifFormattedList[i].key != NULL) | |
388 { | |
389 if (strcmp(key, ExifFormattedList[i].key) == 0) return _(ExifFormattedList[i].description); | |
390 i++; | |
391 } | |
392 | |
393 return exif_get_tag_description_by_key(key); | |
394 } | |
184 | 395 |
396 gint exif_get_integer(ExifData *exif, const gchar *key, gint *value) | |
397 { | |
398 ExifItem *item; | |
399 | |
400 item = exif_get_item(exif, key); | |
401 return exif_item_get_integer(item, value); | |
402 } | |
403 | |
404 ExifRational *exif_get_rational(ExifData *exif, const gchar *key, gint *sign) | |
405 { | |
406 ExifItem *item; | |
407 | |
408 item = exif_get_item(exif, key); | |
409 return exif_item_get_rational(item, sign); | |
410 } | |
411 | |
412 gchar *exif_get_data_as_text(ExifData *exif, const gchar *key) | |
413 { | |
414 ExifItem *item; | |
415 gchar *text; | |
416 gint key_valid; | |
417 | |
418 if (!key) return NULL; | |
419 | |
420 text = exif_get_formatted_by_key(exif, key, &key_valid); | |
421 if (key_valid) return text; | |
422 | |
423 item = exif_get_item(exif, key); | |
424 if (item) return exif_item_get_data_as_text(item); | |
425 | |
426 return NULL; | |
427 } | |
188
0584cb78aa14
write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents:
184
diff
changeset
|
428 |
0584cb78aa14
write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents:
184
diff
changeset
|
429 ExifData *exif_read_fd(FileData *fd, gint parse_color_profile) |
0584cb78aa14
write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents:
184
diff
changeset
|
430 { |
204 | 431 GList *work; |
188
0584cb78aa14
write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents:
184
diff
changeset
|
432 gchar *sidecar_path = NULL; |
190
c2923efebfdc
whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents:
188
diff
changeset
|
433 |
204 | 434 if (!fd) return NULL; |
435 | |
436 work = fd->parent ? fd->parent->sidecar_files : fd->sidecar_files; | |
437 | |
212
c7021159079d
differentiate among normal image, raw image and metadata
nadvornik
parents:
204
diff
changeset
|
438 if (filter_file_class(fd->extension, FORMAT_CLASS_RAWIMAGE)) |
188
0584cb78aa14
write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents:
184
diff
changeset
|
439 { |
190
c2923efebfdc
whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents:
188
diff
changeset
|
440 while(work) |
188
0584cb78aa14
write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents:
184
diff
changeset
|
441 { |
190
c2923efebfdc
whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents:
188
diff
changeset
|
442 FileData *sfd = work->data; |
c2923efebfdc
whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents:
188
diff
changeset
|
443 work = work->next; |
c2923efebfdc
whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents:
188
diff
changeset
|
444 if (strcasecmp(sfd->extension, ".xmp") == 0) |
c2923efebfdc
whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents:
188
diff
changeset
|
445 { |
c2923efebfdc
whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents:
188
diff
changeset
|
446 sidecar_path = sfd->path; |
c2923efebfdc
whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents:
188
diff
changeset
|
447 break; |
c2923efebfdc
whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents:
188
diff
changeset
|
448 } |
188
0584cb78aa14
write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents:
184
diff
changeset
|
449 } |
0584cb78aa14
write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents:
184
diff
changeset
|
450 } |
0584cb78aa14
write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents:
184
diff
changeset
|
451 |
0584cb78aa14
write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents:
184
diff
changeset
|
452 |
0584cb78aa14
write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents:
184
diff
changeset
|
453 // FIXME: some caching would be nice |
0584cb78aa14
write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents:
184
diff
changeset
|
454 return exif_read(fd->path, sidecar_path, parse_color_profile); |
0584cb78aa14
write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents:
184
diff
changeset
|
455 } |