15 #include <sys/types.h>
27 #include "format_raw.h"
28 #include "ui_fileops.h"
31 /* human readable key list */
33 ExifFormattedText ExifFormattedList[] = {
34 { "fCamera", N_("Camera") },
35 { "fDateTime", N_("Date") },
36 { "fShutterSpeed", N_("Shutter speed") },
37 { "fAperture", N_("Aperture") },
38 { "fExposureBias", N_("Exposure bias") },
39 { "fISOSpeedRating", N_("ISO sensitivity") },
40 { "fFocalLength", N_("Focal length") },
41 { "fFocalLength35mmFilm",N_("Focal length 35mm") },
42 { "fSubjectDistance", N_("Subject distance") },
43 { "fFlash", N_("Flash") },
44 { "fResolution", N_("Resolution") },
48 static ExifTextList ExifFlashList[] = {
51 { 5, N_("yes, not detected by strobe") },
52 { 7, N_("yes, detected by strobe") },
56 double exif_rational_to_double(ExifRational *r, gint sign)
58 if (!r || r->den == 0.0) return 0.0;
60 if (sign) return (double)((int)r->num) / (double)((int)r->den);
61 return (double)r->num / r->den;
64 double exif_get_rational_as_double(ExifData *exif, const gchar *key)
69 r = exif_get_rational(exif, key, &sign);
70 return exif_rational_to_double(r, sign);
73 static GString *append_comma_text(GString *string, const gchar *text)
75 string = g_string_append(string, ", ");
76 string = g_string_append(string, text);
81 static gchar *remove_common_prefix(gchar *s, gchar *t)
85 if (!s || !t) return t;
87 for (i = 0; s[i] == t[i]; i++)
91 if (s[i]==' ' || s[i]==0)
100 static double get_crop_factor(ExifData *exif)
102 double res_unit_tbl[] = {0.0, 25.4, 25.4, 10.0, 1.0, 0.001 };
104 double xres = exif_get_rational_as_double(exif, "Exif.Photo.FocalPlaneXResolution");
105 double yres = exif_get_rational_as_double(exif, "Exif.Photo.FocalPlaneYResolution");
108 double xsize, ysize, size, ratio;
110 if (xres == 0.0 || yres == 0.0) return 0.0;
112 if (!exif_get_integer(exif, "Exif.Photo.FocalPlaneResolutionUnit", &res_unit)) return 0.0;
113 if (res_unit < 1 || res_unit > 5) return 0.0;
115 if (!exif_get_integer(exif, "Exif.Photo.PixelXDimension", &w)) return 0.0;
116 if (!exif_get_integer(exif, "Exif.Photo.PixelYDimension", &h)) return 0.0;
118 xsize = w * res_unit_tbl[res_unit] / xres;
119 ysize = h * res_unit_tbl[res_unit] / yres;
121 ratio = xsize / ysize;
123 if (ratio < 0.5 || ratio > 2.0) return 0.0; /* reasonable ratio */
125 size = sqrt(xsize * xsize + ysize * ysize);
127 if (size < 1.0 || size > 100.0) return 0.0; /* reasonable sensor size in mm */
129 return sqrt(36*36+24*24) / size;
134 gchar *exif_get_formatted_by_key(ExifData *exif, const gchar *key, gint *key_valid)
136 /* must begin with f, else not formatted */
139 if (key_valid) *key_valid = FALSE;
143 if (key_valid) *key_valid = TRUE;
145 if (strcmp(key, "fCamera") == 0)
148 gchar *make = exif_get_data_as_text(exif, "Exif.Image.Make");
149 gchar *model = exif_get_data_as_text(exif, "Exif.Image.Model");
150 gchar *software = exif_get_data_as_text(exif, "Exif.Image.Software");
160 #define REMOVE_SUFFIX(str,suff) \
162 if (g_str_has_suffix(str,suff)) \
163 str[strlen(str)-(sizeof(suff)-1)] = 0; \
165 REMOVE_SUFFIX(make," Corporation"); /* Pentax */
166 REMOVE_SUFFIX(make," OPTICAL CO.,LTD"); /* OLYMPUS */
171 g_strstrip(software);
172 /* remove superfluous spaces (pentax K100D) */
173 for (i=0; software && software[i]; i++)
174 if (software[i] == ' ' && software[i+1] == ' ')
178 for (j=1; software[i+j]; j++)
179 if (software[i+j] != ' ')
181 memmove(software+i+1, software+i+j, strlen(software+i+j)+1);
184 model2 = remove_common_prefix(make, model);
185 software2 = remove_common_prefix(model2, software);
187 text = g_strdup_printf("%s%s%s%s%s%s", (make) ? make : "", ((make) && (model)) ? " " : "",
188 (model2) ? model2 : "",
189 (software2) ? " (" : "",
190 (software2) ? software2 : "",
191 (software2) ? ")" : "");
198 if (strcmp(key, "fDateTime") == 0)
200 gchar *text = exif_get_data_as_text(exif, "Exif.Photo.DateTimeOriginal");
201 gchar *subsec = NULL;
202 if (text) subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTimeOriginal");
205 text = exif_get_data_as_text(exif, "Exif.Image.DateTime");
206 if (text) subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTime");
211 text = g_strconcat(tmp, ".", subsec, NULL);
217 if (strcmp(key, "fShutterSpeed") == 0)
221 r = exif_get_rational(exif, "Exif.Photo.ExposureTime", NULL);
222 if (r && r->num && r->den)
224 double n = (double)r->den / (double)r->num;
225 return g_strdup_printf("%s%.0fs", n > 1.0 ? "1/" : "",
226 n > 1.0 ? n : 1.0 / n);
228 r = exif_get_rational(exif, "Exif.Photo.ShutterSpeedValue", NULL);
229 if (r && r->num && r->den)
231 double n = pow(2.0, exif_rational_to_double(r, TRUE));
233 /* Correct exposure time to avoid values like 1/91s (seen on Minolta DImage 7) */
234 if (n > 1.0 && (int)n - ((int)(n/10))*10 == 1) n--;
236 return g_strdup_printf("%s%.0fs", n > 1.0 ? "1/" : "",
237 n > 1.0 ? floor(n) : 1.0 / n);
241 if (strcmp(key, "fAperture") == 0)
245 n = exif_get_rational_as_double(exif, "Exif.Photo.FNumber");
246 if (n == 0.0) n = exif_get_rational_as_double(exif, "Exif.Photo.ApertureValue");
247 if (n == 0.0) return NULL;
249 return g_strdup_printf("f/%.1f", n);
251 if (strcmp(key, "fExposureBias") == 0)
257 r = exif_get_rational(exif, "Exif.Photo.ExposureBiasValue", &sign);
260 n = exif_rational_to_double(r, sign);
261 return g_strdup_printf("%+.1f", n);
263 if (strcmp(key, "fFocalLength") == 0)
267 n = exif_get_rational_as_double(exif, "Exif.Photo.FocalLength");
268 if (n == 0.0) return NULL;
269 return g_strdup_printf("%.0f mm", n);
271 if (strcmp(key, "fFocalLength35mmFilm") == 0)
276 if (exif_get_integer(exif, "Exif.Photo.FocalLengthIn35mmFilm", &n) && n != 0)
278 return g_strdup_printf("%d mm", n);
281 f = exif_get_rational_as_double(exif, "Exif.Photo.FocalLength");
282 c = get_crop_factor(exif);
284 if (f != 0.0 && c != 0.0)
286 return g_strdup_printf("%.0f mm", f * c);
291 if (strcmp(key, "fISOSpeedRating") == 0)
295 text = exif_get_data_as_text(exif, "Exif.Photo.ISOSpeedRatings");
296 /* kodak may set this instead */
297 if (!text) text = exif_get_data_as_text(exif, "Exif.Photo.ExposureIndex");
300 if (strcmp(key, "fSubjectDistance") == 0)
306 r = exif_get_rational(exif, "Exif.Photo.SubjectDistance", &sign);
309 if ((long)r->num == 0xffffffff) return g_strdup(_("infinity"));
310 if ((long)r->num == 0) return g_strdup(_("unknown"));
312 n = exif_rational_to_double(r, sign);
313 if (n == 0.0) return _("unknown");
314 return g_strdup_printf("%.3f m", n);
316 if (strcmp(key, "fFlash") == 0)
318 /* grr, flash is a bitmask... */
324 if (!exif_get_integer(exif, "Exif.Photo.Flash", &n)) return NULL;
326 /* Exif 2.1 only defines first 3 bits */
327 if (n <= 0x07) return exif_get_data_as_text(exif, "Exif.Photo.Flash");
329 /* must be Exif 2.2 */
330 string = g_string_new("");
332 /* flash fired (bit 0) */
333 string = g_string_append(string, (n & 0x01) ? _("yes") : _("no"));
335 /* flash mode (bits 3, 4) */
337 if (v) string = append_comma_text(string, _("mode:"));
341 string = g_string_append(string, _("on"));
344 string = g_string_append(string, _("off"));
347 string = g_string_append(string, _("auto"));
351 /* return light (bits 1, 2) */
353 if (v == 2) string = append_comma_text(string, _("not detected by strobe"));
354 if (v == 3) string = append_comma_text(string, _("detected by strobe"));
356 /* we ignore flash function (bit 5) */
358 /* red-eye (bit 6) */
359 if ((n >> 5) & 0x01) string = append_comma_text(string, _("red-eye reduction"));
362 g_string_free(string, FALSE);
365 if (strcmp(key, "fResolution") == 0)
367 ExifRational *rx, *ry;
371 rx = exif_get_rational(exif, "Exif.Image.XResolution", NULL);
372 ry = exif_get_rational(exif, "Exif.Image.YResolution", NULL);
373 if (!rx || !ry) return NULL;
375 units = exif_get_data_as_text(exif, "Exif.Image.ResolutionUnit");
376 text = g_strdup_printf("%0.f x %0.f (%s/%s)", rx->den ? (double)rx->num / rx->den : 1.0,
377 ry->den ? (double)ry->num / ry->den : 1.0,
378 _("dot"), (units) ? units : _("unknown"));
384 if (key_valid) *key_valid = FALSE;
388 const gchar *exif_get_description_by_key(const gchar *key)
392 if (!key) return NULL;
395 while (ExifFormattedList[i].key != NULL)
397 if (strcmp(key, ExifFormattedList[i].key) == 0) return _(ExifFormattedList[i].description);
401 return exif_get_tag_description_by_key(key);
404 gint exif_get_integer(ExifData *exif, const gchar *key, gint *value)
408 item = exif_get_item(exif, key);
409 return exif_item_get_integer(item, value);
412 ExifRational *exif_get_rational(ExifData *exif, const gchar *key, gint *sign)
416 item = exif_get_item(exif, key);
417 return exif_item_get_rational(item, sign);
420 gchar *exif_get_data_as_text(ExifData *exif, const gchar *key)
426 if (!key) return NULL;
428 text = exif_get_formatted_by_key(exif, key, &key_valid);
429 if (key_valid) return text;
431 item = exif_get_item(exif, key);
432 if (item) return exif_item_get_data_as_text(item);
437 ExifData *exif_read_fd(FileData *fd, gint parse_color_profile)
440 gchar *sidecar_path = NULL;
442 if (!fd) return NULL;
444 work = fd->parent ? fd->parent->sidecar_files : fd->sidecar_files;
446 if (filter_file_class(fd->extension, FORMAT_CLASS_RAWIMAGE))
450 FileData *sfd = work->data;
452 if (strcasecmp(sfd->extension, ".xmp") == 0)
454 sidecar_path = sfd->path;
461 // FIXME: some caching would be nice
462 return exif_read(fd->path, sidecar_path, parse_color_profile);