2 * Copyright (C) 2006 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/types.h>
36 /*** color support enabled ***/
53 #include "filefilter.h"
54 #include "filecache.h"
55 #include "format_raw.h"
57 #include "ui_fileops.h"
59 #include "jpeg_parser.h"
61 #include "zonedetect.h"
64 static gdouble exif_rational_to_double(ExifRational *r, gint sign)
66 if (!r || r->den == 0.0) return 0.0;
68 if (sign) return (gdouble)((gint)r->num) / (gdouble)((gint)r->den);
69 return (gdouble)r->num / r->den;
72 static gdouble exif_get_rational_as_double(ExifData *exif, const gchar *key)
77 r = exif_get_rational(exif, key, &sign);
78 return exif_rational_to_double(r, sign);
81 static GString *append_comma_text(GString *string, const gchar *text)
83 string = g_string_append(string, ", ");
84 string = g_string_append(string, text);
89 static gchar *remove_common_prefix(gchar *s, gchar *t)
93 if (!s || !t) return t;
95 for (i = 0; s[i] && t[i] && s[i] == t[i]; i++)
99 if (s[i-1] == ' ' || !s[i])
108 static gdouble get_crop_factor(ExifData *exif)
110 gdouble res_unit_tbl[] = {0.0, 25.4, 25.4, 10.0, 1.0, 0.001 };
111 gdouble xres = exif_get_rational_as_double(exif, "Exif.Photo.FocalPlaneXResolution");
112 gdouble yres = exif_get_rational_as_double(exif, "Exif.Photo.FocalPlaneYResolution");
115 gdouble xsize, ysize, size, ratio;
117 if (xres == 0.0 || yres == 0.0) return 0.0;
119 if (!exif_get_integer(exif, "Exif.Photo.FocalPlaneResolutionUnit", &res_unit)) return 0.0;
120 if (res_unit < 1 || res_unit > 5) return 0.0;
122 if (!exif_get_integer(exif, "Exif.Photo.PixelXDimension", &w)) return 0.0;
123 if (!exif_get_integer(exif, "Exif.Photo.PixelYDimension", &h)) return 0.0;
125 xsize = w * res_unit_tbl[res_unit] / xres;
126 ysize = h * res_unit_tbl[res_unit] / yres;
128 ratio = xsize / ysize;
130 if (ratio < 0.5 || ratio > 2.0) return 0.0; /* reasonable ratio */
132 size = sqrt(xsize * xsize + ysize * ysize);
134 if (size < 1.0 || size > 100.0) return 0.0; /* reasonable sensor size in mm */
136 return sqrt(36*36+24*24) / size;
140 static gboolean remove_suffix(gchar *str, const gchar *suffix, gint suffix_len)
142 gint str_len = strlen(str);
144 if (suffix_len < 0) suffix_len = strlen(suffix);
145 if (str_len < suffix_len) return FALSE;
147 if (strcmp(str + str_len - suffix_len, suffix) != 0) return FALSE;
148 str[str_len - suffix_len] = '\0';
153 static gchar *exif_build_formatted_Camera(ExifData *exif)
156 gchar *make = exif_get_data_as_text(exif, "Exif.Image.Make");
157 gchar *model = exif_get_data_as_text(exif, "Exif.Image.Model");
158 gchar *software = exif_get_data_as_text(exif, "Exif.Image.Software");
166 if (remove_suffix(make, " CORPORATION", 12)) { /* Nikon */ }
167 else if (remove_suffix(make, " Corporation", 12)) { /* Pentax */ }
168 else if (remove_suffix(make, " OPTICAL CO.,LTD", 16)) { /* OLYMPUS */ };
178 g_strstrip(software);
180 /* remove superfluous spaces (pentax K100D) */
181 for (i = 0, j = 0; software[i]; i++, j++)
183 if (software[i] == ' ' && software[i + 1] == ' ')
185 if (i != j) software[j] = software[i];
190 model2 = remove_common_prefix(make, model);
191 software2 = remove_common_prefix(model2, software);
193 text = g_strdup_printf("%s%s%s%s%s%s", (make) ? make : "", (make && model2) ? " " : "",
194 (model2) ? model2 : "",
195 (software2 && (make || model2)) ? " (" : "",
196 (software2) ? software2 : "",
197 (software2 && (make || model2)) ? ")" : "");
205 static gchar *exif_build_formatted_DateTime(ExifData *exif)
207 gchar *text = exif_get_data_as_text(exif, "Exif.Photo.DateTimeOriginal");
208 gchar *subsec = NULL;
213 GError *error = NULL;
217 subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTimeOriginal");
221 text = exif_get_data_as_text(exif, "Exif.Image.DateTime");
222 if (text) subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTime");
225 /* Convert the stuff into a tm struct */
226 memset(&tm, 0, sizeof(tm)); /* Uh, strptime could let garbage in tm! */
227 if (text && strptime(text, "%Y:%m:%d %H:%M:%S", &tm))
229 buflen = strftime(buf, sizeof(buf), "%x %X", &tm);
232 tmp = g_locale_to_utf8(buf, buflen, NULL, NULL, &error);
235 log_printf("Error converting locale strftime to UTF-8: %s\n", error->message);
241 text = g_strdup(tmp);
249 text = g_strconcat(tmp, ".", subsec, NULL);
256 static gchar *exif_build_formatted_DateTimeDigitized(ExifData *exif)
258 gchar *text = exif_get_data_as_text(exif, "Exif.Photo.DateTimeDigitized");
259 gchar *subsec = NULL;
264 GError *error = NULL;
268 subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTimeDigitized");
272 text = exif_get_data_as_text(exif, "Exif.Image.DateTime");
273 if (text) subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTime");
276 /* Convert the stuff into a tm struct */
277 memset(&tm, 0, sizeof(tm)); /* Uh, strptime could let garbage in tm! */
278 if (text && strptime(text, "%Y:%m:%d %H:%M:%S", &tm))
280 buflen = strftime(buf, sizeof(buf), "%x %X", &tm);
283 tmp = g_locale_to_utf8(buf, buflen, NULL, NULL, &error);
286 log_printf("Error converting locale strftime to UTF-8: %s\n", error->message);
292 text = g_strdup(tmp);
300 text = g_strconcat(tmp, ".", subsec, NULL);
307 static gchar *exif_build_formatted_ShutterSpeed(ExifData *exif)
311 r = exif_get_rational(exif, "Exif.Photo.ExposureTime", NULL);
312 if (r && r->num && r->den)
314 gdouble n = (gdouble)r->den / (gdouble)r->num;
315 return g_strdup_printf("%s%.0fs", n > 1.0 ? "1/" : "",
316 n > 1.0 ? n : 1.0 / n);
318 r = exif_get_rational(exif, "Exif.Photo.ShutterSpeedValue", NULL);
319 if (r && r->num && r->den)
321 gdouble n = pow(2.0, exif_rational_to_double(r, TRUE));
323 /* Correct exposure time to avoid values like 1/91s (seen on Minolta DImage 7) */
324 if (n > 1.0 && (gint)n - ((gint)(n/10))*10 == 1) n--;
326 return g_strdup_printf("%s%.0fs", n > 1.0 ? "1/" : "",
327 n > 1.0 ? floor(n) : 1.0 / n);
332 static gchar *exif_build_formatted_Aperture(ExifData *exif)
336 n = exif_get_rational_as_double(exif, "Exif.Photo.FNumber");
337 if (n == 0.0) n = exif_get_rational_as_double(exif, "Exif.Photo.ApertureValue");
338 if (n == 0.0) return NULL;
340 return g_strdup_printf("f/%.1f", n);
343 static gchar *exif_build_formatted_ExposureBias(ExifData *exif)
349 r = exif_get_rational(exif, "Exif.Photo.ExposureBiasValue", &sign);
352 n = exif_rational_to_double(r, sign);
353 return g_strdup_printf("%+.1f", n);
356 static gchar *exif_build_formatted_FocalLength(ExifData *exif)
360 n = exif_get_rational_as_double(exif, "Exif.Photo.FocalLength");
361 if (n == 0.0) return NULL;
362 return g_strdup_printf("%.0f mm", n);
365 static gchar *exif_build_formatted_FocalLength35mmFilm(ExifData *exif)
370 if (exif_get_integer(exif, "Exif.Photo.FocalLengthIn35mmFilm", &n) && n != 0)
372 return g_strdup_printf("%d mm", n);
375 f = exif_get_rational_as_double(exif, "Exif.Photo.FocalLength");
376 if (f == 0.0) return NULL;
378 c = get_crop_factor(exif);
379 if (c == 0.0) return NULL;
381 return g_strdup_printf("%.0f mm", f * c);
384 static gchar *exif_build_formatted_ISOSpeedRating(ExifData *exif)
388 text = exif_get_data_as_text(exif, "Exif.Photo.ISOSpeedRatings");
389 /* old canon may set this instead */
390 if (!text) text = exif_get_data_as_text(exif, "Exif.CanonSi.ISOSpeed");
391 /* kodak may set this instead */
392 if (!text) text = exif_get_data_as_text(exif, "Exif.Photo.ExposureIndex");
396 static gchar *exif_build_formatted_SubjectDistance(ExifData *exif)
402 r = exif_get_rational(exif, "Exif.Photo.SubjectDistance", &sign);
405 if ((glong)r->num == (glong)0xffffffff) return g_strdup(_("infinity"));
406 if ((glong)r->num == 0) return g_strdup(_("unknown"));
408 n = exif_rational_to_double(r, sign);
409 if (n == 0.0) return _("unknown");
410 return g_strdup_printf("%.3f m", n);
413 static gchar *exif_build_formatted_Flash(ExifData *exif)
415 /* grr, flash is a bitmask... */
421 if (!exif_get_integer(exif, "Exif.Photo.Flash", &n)) return NULL;
423 /* Exif 2.1 only defines first 3 bits */
424 if (n <= 0x07) return exif_get_data_as_text(exif, "Exif.Photo.Flash");
426 /* must be Exif 2.2 */
427 string = g_string_new("");
429 /* flash fired (bit 0) */
430 string = g_string_append(string, (n & 0x01) ? _("yes") : _("no"));
432 /* flash mode (bits 3, 4) */
434 if (v) string = append_comma_text(string, _("mode:"));
438 string = g_string_append(string, _("on"));
441 string = g_string_append(string, _("off"));
444 string = g_string_append(string, _("auto"));
448 /* return light (bits 1, 2) */
450 if (v == 2) string = append_comma_text(string, _("not detected by strobe"));
451 if (v == 3) string = append_comma_text(string, _("detected by strobe"));
453 /* we ignore flash function (bit 5) */
455 /* red-eye (bit 6) */
456 if ((n >> 5) & 0x01) string = append_comma_text(string, _("red-eye reduction"));
459 g_string_free(string, FALSE);
463 static gchar *exif_build_formatted_Resolution(ExifData *exif)
465 ExifRational *rx, *ry;
469 rx = exif_get_rational(exif, "Exif.Image.XResolution", NULL);
470 ry = exif_get_rational(exif, "Exif.Image.YResolution", NULL);
471 if (!rx || !ry) return NULL;
473 units = exif_get_data_as_text(exif, "Exif.Image.ResolutionUnit");
474 text = g_strdup_printf("%0.f x %0.f (%s/%s)", rx->den ? (gdouble)rx->num / rx->den : 1.0,
475 ry->den ? (gdouble)ry->num / ry->den : 1.0,
476 _("dot"), (units) ? units : _("unknown"));
482 static gchar *exif_build_formatted_ColorProfile(ExifData *exif)
485 cmsUInt8Number profileID[17];
487 const gchar *name = "";
488 const gchar *source = "";
489 guchar *profile_data;
492 profile_data = exif_get_color_profile(exif, &profile_len);
496 gchar *interop_index;
498 /* ColorSpace == 1 specifies sRGB per EXIF 2.2 */
499 if (!exif_get_integer(exif, "Exif.Photo.ColorSpace", &cs)) cs = 0;
500 interop_index = exif_get_data_as_text(exif, "Exif.Iop.InteroperabilityIndex");
505 source = "ColorSpace";
507 else if (cs == 2 || (interop_index && !strcmp(interop_index, "R03")))
509 name = _("AdobeRGB");
510 source = (cs == 2) ? "ColorSpace" : "Iop";
513 g_free(interop_index);
517 source = _("embedded");
523 profile = cmsOpenProfileFromMem(profile_data, profile_len);
527 profileID[16] = '\0';
528 cmsGetHeaderProfileID(profile, profileID);
529 name = (gchar *) profileID;
531 name = (gchar *) cmsTakeProductName(profile);
533 cmsCloseProfile(profile);
535 g_free(profile_data);
539 if (name[0] == 0 && source[0] == 0) return NULL;
540 return g_strdup_printf("%s (%s)", name, source);
543 static gchar *exif_build_formatted_GPSPosition(ExifData *exif)
553 string = g_string_new("");
555 item = exif_get_item(exif, "Exif.GPSInfo.GPSLatitude");
556 ref = exif_get_data_as_text(exif, "Exif.GPSInfo.GPSLatitudeRef");
560 for (i = 0; i < exif_item_get_elements(item); i++)
562 value = exif_item_get_rational(item, NULL, i);
563 if (value && value->num && value->den)
564 p += (gdouble)value->num / (gdouble)value->den / pow(60.0, (gdouble)i);
567 p2 = (gint)((p - p1)*60);
568 p3 = ((p - p1)*60 - p2)*60;
570 g_string_append_printf(string, "%0lu° %0lu' %0.2f\" %.1s", p1, p2, p3, ref);
571 } // if (item && ref)
573 item = exif_get_item(exif, "Exif.GPSInfo.GPSLongitude");
574 ref = exif_get_data_as_text(exif, "Exif.GPSInfo.GPSLongitudeRef");
578 for (i = 0; i < exif_item_get_elements(item); i++)
580 value = exif_item_get_rational(item, NULL, i);
581 if (value && value->num && value->den)
582 p += (gdouble)value->num / (gdouble)value->den / pow(60.0, (gdouble)i);
585 p2 = (gint)((p - p1)*60);
586 p3 = ((p - p1)*60 - p2)*60;
588 g_string_append_printf(string, ", %0lu° %0lu' %0.2f\" %.1s", p1, p2, p3, ref);
589 } // if (item && ref)
592 g_string_free(string, FALSE);
595 } // static gchar *exif_build_forma...
597 static gchar *exif_build_formatted_GPSAltitude(ExifData *exif)
604 item = exif_get_item(exif, "Exif.GPSInfo.GPSAltitudeRef");
605 r = exif_get_rational(exif, "Exif.GPSInfo.GPSAltitude", NULL);
607 if (!r || !item) return NULL;
609 alt = exif_rational_to_double(r, 0);
610 exif_item_get_integer(item, &ref);
612 return g_strdup_printf("%0.f m %s", alt, (ref==0)?_("Above Sea Level"):_("Below Sea Level"));
616 * @brief Extracts timezone data from a ZoneDetect search structure
617 * @param[in] results ZoneDetect search structure
618 * @param[out] timezone in the form "Europe/London"
619 * @param[out] countryname in the form "United Kingdom"
620 * @param[out] countryalpha2 in the form "GB"
622 * Refer to https://github.com/BertoldVdb/ZoneDetect
623 * for structure details
625 static void zd_tz(ZoneDetectResult *results, gchar **timezone, gchar **countryname, gchar **countryalpha2)
627 gchar *timezone_pre = NULL;
628 gchar *timezone_id = NULL;
629 unsigned int index = 0;
631 while(results[index].lookupResult != ZD_LOOKUP_END)
633 if(results[index].data)
635 for(unsigned int i=0; i<results[index].numFields; i++)
637 if (g_strstr_len(results[index].fieldNames[i], -1, "TimezoneIdPrefix"))
639 timezone_pre = g_strdup(results[index].data[i]);
641 if (g_strstr_len(results[index].fieldNames[i], -1, "TimezoneId"))
643 timezone_id = g_strdup(results[index].data[i]);
645 if (g_strstr_len(results[index].fieldNames[i], -1, "CountryName"))
647 *countryname = g_strdup(results[index].data[i]);
649 if (g_strstr_len(results[index].fieldNames[i], -1, "CountryAlpha2"))
651 *countryalpha2 = g_strdup(results[index].data[i]);
658 *timezone = g_strconcat(timezone_pre, timezone_id, NULL);
659 g_free(timezone_pre);
664 * @brief Gets timezone data from an exif structure
666 * @returns TRUE if timezone data found AND GPS date and time found
667 * @param[out] exif_date_time exif date/time in the form 2018:11:30:17:05:04
668 * @param[out] timezone in the form "Europe/London"
669 * @param[out] countryname in the form "United Kingdom"
670 * @param[out] countryalpha2 in the form "GB"
674 static gboolean exif_build_tz_data(ExifData *exif, gchar **exif_date_time, gchar **timezone, gchar **countryname, gchar **countryalpha2)
678 gchar *text_latitude;
679 gchar *text_longitude;
680 gchar *text_latitude_ref;
681 gchar *text_longitude_ref;
688 gchar *timezone_path;
690 ZoneDetectResult *results;
691 gboolean ret = FALSE;
695 text_latitude = exif_get_data_as_text(exif, "Exif.GPSInfo.GPSLatitude");
696 text_longitude = exif_get_data_as_text(exif, "Exif.GPSInfo.GPSLongitude");
697 text_latitude_ref = exif_get_data_as_text(exif, "Exif.GPSInfo.GPSLatitudeRef");
698 text_longitude_ref = exif_get_data_as_text(exif, "Exif.GPSInfo.GPSLongitudeRef");
699 text_date = exif_get_data_as_text(exif, "Exif.GPSInfo.GPSDateStamp");
700 text_time = exif_get_data_as_text(exif, "Exif.GPSInfo.GPSTimeStamp");
702 if (text_latitude && text_longitude && text_latitude_ref && text_longitude_ref)
704 lat_deg = strtok(text_latitude, "deg'");
705 lat_min = strtok(NULL, "deg'");
706 if (!lat_deg || !lat_min)
710 latitude = atof(lat_deg) + atof(lat_min) / 60;
711 if (!g_strcmp0(text_latitude_ref, "South"))
713 latitude = -latitude;
715 lon_deg = strtok(text_longitude, "deg'");
716 lon_min = strtok(NULL, "deg'");
717 if (!lon_deg || !lon_min)
721 longitude = atof(lon_deg) + atof(lon_min) / 60;
722 if (!g_strcmp0(text_longitude_ref, "West"))
724 longitude = -longitude;
727 path = path_from_utf8(TIMEZONE_DATABASE);
728 basename = g_path_get_basename(path);
729 timezone_path = g_build_filename(get_rc_dir(), basename, NULL);
730 if (g_file_test(timezone_path, G_FILE_TEST_EXISTS))
732 cd = ZDOpenDatabase(timezone_path);
735 results = ZDLookup(cd, latitude, longitude, NULL);
738 zd_tz(results, timezone, countryname, countryalpha2);
744 log_printf("Error: Init of timezone database %s failed\n", timezone_path);
749 g_free(timezone_path);
753 if (ret && text_date && text_time)
755 *exif_date_time = g_strconcat(text_date, ":", text_time, NULL);
765 * @brief Creates local time from GPS lat/long
767 * @returns Localised time and date
769 * GPS lat/long is translated to timezone using ZoneDetect.
770 * GPS UTC is converted to Unix time stamp (seconds since 1970).
771 * The TZ environment variable is set to the relevant timezone
772 * and the Unix timestamp converted to local time using locale.
773 * If the conversion fails, unformatted UTC is returned.
775 static gchar *exif_build_formatted_localtime(ExifData *exif)
780 GError *error = NULL;
781 gchar *time_zone_image;
782 gchar *time_zone_org;
786 gchar *exif_date_time = NULL;
787 gchar *timezone = NULL;
788 gchar *countryname = NULL;
789 gchar *countryalpha2 = NULL;
791 if (exif_build_tz_data(exif, &exif_date_time, &timezone, &countryname, &countryalpha2))
793 time_zone_image = g_strconcat("TZ=", timezone, NULL);
794 time_zone_org = g_strconcat("TZ=", getenv("TZ"), NULL);
797 memset(&tm_utc, 0, sizeof(tm_utc));
798 if (exif_date_time && strptime(exif_date_time, "%Y:%m:%d:%H:%M:%S", &tm_utc))
800 stamp = mktime(&tm_utc); // Convert the struct to a Unix timestamp
801 putenv(time_zone_image); // Switch to destination time zone
803 tm_local = localtime(&stamp);
805 /* Convert to localtime using locale */
806 buflen = strftime(buf, sizeof(buf), "%x %X", tm_local);
809 tmp = g_locale_to_utf8(buf, buflen, NULL, NULL, &error);
812 log_printf("Error converting locale strftime to UTF-8: %s\n", error->message);
817 g_free(exif_date_time);
818 exif_date_time = tmp;
822 putenv(time_zone_org);
824 g_free(time_zone_image);
825 g_free(time_zone_org);
830 g_free(countryalpha2);
832 return exif_date_time;
836 * @brief Gets timezone from GPS lat/long
838 * @returns Timezone string in the form "Europe/London"
842 static gchar *exif_build_formatted_timezone(ExifData *exif)
844 gchar *exif_date_time = NULL;
845 gchar *timezone = NULL;
846 gchar *countryname = NULL;
847 gchar *countryalpha2 = NULL;
849 exif_build_tz_data(exif, &exif_date_time, &timezone, &countryname, &countryalpha2);
851 g_free(exif_date_time);
853 g_free(countryalpha2);
859 * @brief Gets countryname from GPS lat/long
861 * @returns Countryname string
865 static gchar *exif_build_formatted_countryname(ExifData *exif)
867 gchar *exif_date_time = NULL;
868 gchar *timezone = NULL;
869 gchar *countryname = NULL;
870 gchar *countryalpha2 = NULL;
872 exif_build_tz_data(exif, &exif_date_time, &timezone, &countryname, &countryalpha2);
874 g_free(exif_date_time);
876 g_free(countryalpha2);
882 * @brief Gets two-letter country code from GPS lat/long
884 * @returns Countryalpha2 string
888 static gchar *exif_build_formatted_countrycode(ExifData *exif)
890 gchar *exif_date_time = NULL;
891 gchar *timezone = NULL;
892 gchar *countryname = NULL;
893 gchar *countryalpha2 = NULL;
895 exif_build_tz_data(exif, &exif_date_time, &timezone, &countryname, &countryalpha2);
897 g_free(exif_date_time);
901 return countryalpha2;
904 static gchar *exif_build_formatted_star_rating(ExifData *exif)
908 exif_get_integer(exif, "Xmp.xmp.Rating", &n);
910 return convert_rating_to_stars(n);
913 /* List of custom formatted pseudo-exif tags */
914 #define EXIF_FORMATTED_TAG(name, label) { EXIF_FORMATTED()#name, label, exif_build_formatted##_##name }
916 ExifFormattedText ExifFormattedList[] = {
917 EXIF_FORMATTED_TAG(Camera, N_("Camera")),
918 EXIF_FORMATTED_TAG(DateTime, N_("Date")),
919 EXIF_FORMATTED_TAG(DateTimeDigitized, N_("DateDigitized")),
920 EXIF_FORMATTED_TAG(ShutterSpeed, N_("Shutter speed")),
921 EXIF_FORMATTED_TAG(Aperture, N_("Aperture")),
922 EXIF_FORMATTED_TAG(ExposureBias, N_("Exposure bias")),
923 EXIF_FORMATTED_TAG(ISOSpeedRating, N_("ISO sensitivity")),
924 EXIF_FORMATTED_TAG(FocalLength, N_("Focal length")),
925 EXIF_FORMATTED_TAG(FocalLength35mmFilm, N_("Focal length 35mm")),
926 EXIF_FORMATTED_TAG(SubjectDistance, N_("Subject distance")),
927 EXIF_FORMATTED_TAG(Flash, N_("Flash")),
928 EXIF_FORMATTED_TAG(Resolution, N_("Resolution")),
929 EXIF_FORMATTED_TAG(ColorProfile, N_("Color profile")),
930 EXIF_FORMATTED_TAG(GPSPosition, N_("GPS position")),
931 EXIF_FORMATTED_TAG(GPSAltitude, N_("GPS altitude")),
932 EXIF_FORMATTED_TAG(localtime, N_("Local time")),
933 EXIF_FORMATTED_TAG(timezone, N_("Time zone")),
934 EXIF_FORMATTED_TAG(countryname, N_("Country name")),
935 EXIF_FORMATTED_TAG(countrycode, N_("Country code")),
936 EXIF_FORMATTED_TAG(star_rating, N_("Star rating")),
937 {"file.size", N_("File size"), NULL},
938 {"file.date", N_("File date"), NULL},
939 {"file.mode", N_("File mode"), NULL},
940 {"file.ctime", N_("File ctime"), NULL},
941 {"file.owner", N_("File owner"), NULL},
942 {"file.group", N_("File group"), NULL},
943 {"file.link", N_("File link"), NULL},
944 {"file.class", N_("File class"), NULL},
945 {"file.page_no", N_("Page no."), NULL},
946 {"lua.lensID", N_("Lens"), NULL},
950 gchar *exif_get_formatted_by_key(ExifData *exif, const gchar *key, gboolean *key_valid)
952 if (strncmp(key, EXIF_FORMATTED(), EXIF_FORMATTED_LEN) == 0)
956 if (key_valid) *key_valid = TRUE;
958 key += EXIF_FORMATTED_LEN;
959 for (i = 0; ExifFormattedList[i].key; i++)
960 if (ExifFormattedList[i].build_func && strcmp(key, ExifFormattedList[i].key + EXIF_FORMATTED_LEN) == 0)
961 return ExifFormattedList[i].build_func(exif);
964 if (key_valid) *key_valid = FALSE;
968 gchar *exif_get_description_by_key(const gchar *key)
970 if (!key) return NULL;
972 if (strncmp(key, EXIF_FORMATTED(), EXIF_FORMATTED_LEN) == 0 || strncmp(key, "file.", 5) == 0 || strncmp(key, "lua.", 4) == 0)
976 for (i = 0; ExifFormattedList[i].key; i++)
977 if (strcmp(key, ExifFormattedList[i].key) == 0)
978 return g_strdup(_(ExifFormattedList[i].description));
981 return exif_get_tag_description_by_key(key);
984 gint exif_get_integer(ExifData *exif, const gchar *key, gint *value)
988 item = exif_get_item(exif, key);
989 return exif_item_get_integer(item, value);
992 ExifRational *exif_get_rational(ExifData *exif, const gchar *key, gint *sign)
996 item = exif_get_item(exif, key);
997 return exif_item_get_rational(item, sign, 0);
1000 gchar *exif_get_data_as_text(ExifData *exif, const gchar *key)
1006 if (!key) return NULL;
1008 text = exif_get_formatted_by_key(exif, key, &key_valid);
1009 if (key_valid) return text;
1011 item = exif_get_item(exif, key);
1012 if (item) return exif_item_get_data_as_text(item);
1018 static FileCacheData *exif_cache;
1020 void exif_release_cb(FileData *fd)
1022 exif_free(fd->exif);
1026 void exif_init_cache(void)
1028 g_assert(!exif_cache);
1029 exif_cache = file_cache_new(exif_release_cb, 4);
1032 ExifData *exif_read_fd(FileData *fd)
1034 gchar *sidecar_path;
1036 if (!exif_cache) exif_init_cache();
1038 if (!fd) return NULL;
1040 if (file_cache_get(exif_cache, fd)) return fd->exif;
1041 g_assert(fd->exif == NULL);
1043 /* CACHE_TYPE_XMP_METADATA file should exist only if the metadata are
1044 * not writable directly, thus it should contain the most up-to-date version */
1045 sidecar_path = NULL;
1048 /* we are not able to handle XMP sidecars without exiv2 */
1049 sidecar_path = cache_find_location(CACHE_TYPE_XMP_METADATA, fd->path);
1051 if (!sidecar_path) sidecar_path = file_data_get_sidecar_path(fd, TRUE);
1054 fd->exif = exif_read(fd->path, sidecar_path, fd->modified_xmp);
1056 g_free(sidecar_path);
1057 file_cache_put(exif_cache, fd, 1);
1062 void exif_free_fd(FileData *fd, ExifData *exif)
1065 g_assert(fd->exif == exif);
1068 /* embedded icc in jpeg */
1070 gboolean exif_jpeg_parse_color(ExifData *exif, guchar *data, guint size)
1072 guint seg_offset = 0;
1073 guint seg_length = 0;
1074 guint chunk_offset[255];
1075 guint chunk_length[255];
1076 guint chunk_count = 0;
1078 /* For jpeg/jfif, ICC color profile data can be in more than one segment.
1079 the data is in APP2 data segments that start with "ICC_PROFILE\x00\xNN\xTT"
1080 NN = segment number for data
1081 TT = total number of ICC segments (TT in each ICC segment should match)
1084 while (jpeg_segment_find(data + seg_offset + seg_length,
1085 size - seg_offset - seg_length,
1087 "ICC_PROFILE\x00", 12,
1088 &seg_offset, &seg_length))
1093 if (seg_length < 14) return FALSE;
1095 chunk_num = data[seg_offset + 12];
1096 chunk_tot = data[seg_offset + 13];
1098 if (chunk_num == 0 || chunk_tot == 0) return FALSE;
1100 if (chunk_count == 0)
1104 chunk_count = (guint)chunk_tot;
1105 for (i = 0; i < chunk_count; i++) chunk_offset[i] = 0;
1106 for (i = 0; i < chunk_count; i++) chunk_length[i] = 0;
1109 if (chunk_tot != chunk_count ||
1110 chunk_num > chunk_count) return FALSE;
1113 chunk_offset[chunk_num] = seg_offset + 14;
1114 chunk_length[chunk_num] = seg_length - 14;
1117 if (chunk_count > 0)
1120 guint cp_length = 0;
1123 for (i = 0; i < chunk_count; i++) cp_length += chunk_length[i];
1124 cp_data = g_malloc(cp_length);
1126 for (i = 0; i < chunk_count; i++)
1128 if (chunk_offset[i] == 0)
1130 /* error, we never saw this chunk */
1134 memcpy(cp_data, data + chunk_offset[i], chunk_length[i]);
1136 DEBUG_1("Found embedded icc profile in jpeg");
1137 exif_add_jpeg_color_profile(exif, cp_data, cp_length);
1146 *-------------------------------------------------------------------
1148 * it is here because it shares tag neming infrastructure with exif
1149 * we should probably not invest too much effort into this because
1150 * new exiv2 will support the same functionality
1151 * http://dev.exiv2.org/issues/show/505
1152 *-------------------------------------------------------------------
1155 static gchar *mode_number(mode_t m)
1157 gint mb, mu, mg, mo;
1160 mb = mu = mg = mo = 0;
1162 if (m & S_ISUID) mb |= 4;
1163 if (m & S_ISGID) mb |= 2;
1164 if (m & S_ISVTX) mb |= 1;
1166 if (m & S_IRUSR) mu |= 4;
1167 if (m & S_IWUSR) mu |= 2;
1168 if (m & S_IXUSR) mu |= 1;
1170 if (m & S_IRGRP) mg |= 4;
1171 if (m & S_IWGRP) mg |= 2;
1172 if (m & S_IXGRP) mg |= 1;
1174 if (m & S_IROTH) mo |= 4;
1175 if (m & S_IWOTH) mo |= 2;
1176 if (m & S_IXOTH) mo |= 1;
1178 pbuf[0] = (m & S_IRUSR) ? 'r' : '-';
1179 pbuf[1] = (m & S_IWUSR) ? 'w' : '-';
1180 pbuf[2] = (m & S_IXUSR) ? 'x' : '-';
1181 pbuf[3] = (m & S_IRGRP) ? 'r' : '-';
1182 pbuf[4] = (m & S_IWGRP) ? 'w' : '-';
1183 pbuf[5] = (m & S_IXGRP) ? 'x' : '-';
1184 pbuf[6] = (m & S_IROTH) ? 'r' : '-';
1185 pbuf[7] = (m & S_IWOTH) ? 'w' : '-';
1186 pbuf[8] = (m & S_IXOTH) ? 'x' : '-';
1189 return g_strdup_printf("%s (%d%d%d%d)", pbuf, mb, mu, mg, mo);
1192 gchar *metadata_file_info(FileData *fd, const gchar *key, MetadataFormat format)
1196 if (strcmp(key, "file.size") == 0)
1198 return g_strdup_printf("%ld", (long)fd->size);
1200 if (strcmp(key, "file.date") == 0)
1202 return g_strdup(text_from_time(fd->date));
1204 if (strcmp(key, "file.mode") == 0)
1206 return mode_number(fd->mode);
1208 if (strcmp(key, "file.ctime") == 0)
1210 return g_strdup(text_from_time(fd->cdate));
1212 if (strcmp(key, "file.class") == 0)
1214 return g_strdup(format_class_list[fd->format_class]);
1216 if (strcmp(key, "file.owner") == 0)
1218 return g_strdup(fd->owner);
1220 if (strcmp(key, "file.group") == 0)
1222 return g_strdup(fd->group);
1224 if (strcmp(key, "file.link") == 0)
1226 return g_strdup(fd->sym_link);
1228 if (strcmp(key, "file.page_no") == 0)
1230 if (fd->page_total > 1)
1232 page_n_of_m = g_strdup_printf("[%d/%d]", fd->page_num + 1, fd->page_total);
1240 return g_strdup("");
1244 gchar *metadata_lua_info(FileData *fd, const gchar *key, MetadataFormat format)
1247 gchar *script_name_utf8;
1252 script_name_utf8 = g_strdup(key + 4);
1253 script_name = path_from_utf8(script_name_utf8);
1255 raw_data = lua_callvalue(fd, script_name, NULL);
1256 valid_data = g_utf8_make_valid(raw_data, -1);
1257 data = g_utf8_substring(valid_data, 0, 150);
1259 g_free(script_name);
1260 g_free(script_name_utf8);
1267 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */