fixed charset of exiv2 strings in non-utf8 locales
[geeqie.git] / src / exif-common.c
index 3c13575..77becdd 100644 (file)
 #include "main.h"
 #include "exif.h"
 
-#include "debug.h"
-#include "filelist.h"
+#include "filedata.h"
+#include "filefilter.h"
+#include "filecache.h"
 #include "format_raw.h"
 #include "ui_fileops.h"
 
 
-static double exif_rational_to_double(ExifRational *r, gint sign)
+static gdouble exif_rational_to_double(ExifRational *r, gint sign)
 {
        if (!r || r->den == 0.0) return 0.0;
 
-       if (sign) return (double)((int)r->num) / (double)((int)r->den);
-       return (double)r->num / r->den;
+       if (sign) return (gdouble)((gint)r->num) / (gdouble)((gint)r->den);
+       return (gdouble)r->num / r->den;
 }
 
-static double exif_get_rational_as_double(ExifData *exif, const gchar *key)
+static gdouble exif_get_rational_as_double(ExifData *exif, const gchar *key)
 {
        ExifRational *r;
        gint sign;
@@ -85,15 +86,14 @@ static gchar *remove_common_prefix(gchar *s, gchar *t)
        return s;
 }
 
-static double get_crop_factor(ExifData *exif)
+static gdouble get_crop_factor(ExifData *exif)
 {
-       double res_unit_tbl[] = {0.0, 25.4, 25.4, 10.0, 1.0, 0.001 };
-
-       double xres = exif_get_rational_as_double(exif, "Exif.Photo.FocalPlaneXResolution");
-       double yres = exif_get_rational_as_double(exif, "Exif.Photo.FocalPlaneYResolution");
-       int res_unit;
-       int w, h;
-       double xsize, ysize, size, ratio;
+       gdouble res_unit_tbl[] = {0.0, 25.4, 25.4, 10.0, 1.0, 0.001 };
+       gdouble xres = exif_get_rational_as_double(exif, "Exif.Photo.FocalPlaneXResolution");
+       gdouble yres = exif_get_rational_as_double(exif, "Exif.Photo.FocalPlaneYResolution");
+       gint res_unit;
+       gint w, h;
+       gdouble xsize, ysize, size, ratio;
 
        if (xres == 0.0 || yres == 0.0) return 0.0;
 
@@ -125,13 +125,13 @@ static gint remove_suffix(gchar *str, const gchar *suffix, gint suffix_len)
        if (suffix_len < 0) suffix_len = strlen(suffix);
        if (str_len < suffix_len) return FALSE;
        
-       if (strcmp(str + str_len - suffix_len, suffix) != 0) return FALSE; 
+       if (strcmp(str + str_len - suffix_len, suffix) != 0) return FALSE;
        str[str_len - suffix_len] = '\0';
        
        return TRUE;
 }
 
-static gchar *exif_build_fCamera(ExifData *exif)
+static gchar *exif_build_formatted_Camera(ExifData *exif)
 {
        gchar *text;
        gchar *make = exif_get_data_as_text(exif, "Exif.Image.Make");
@@ -154,19 +154,18 @@ static gchar *exif_build_fCamera(ExifData *exif)
 
        if (software)
                {
-               gint i;
+               gint i, j;
 
                g_strstrip(software);
                
                /* remove superfluous spaces (pentax K100D) */
-               for (i = 0; software[i]; i++)
+               for (i = 0, j = 0; software[i]; i++, j++)
+                       {
                        if (software[i] == ' ' && software[i + 1] == ' ')
-                               {
-                               gint j;
-
-                               for (j = 1; software[i + j] == ' '; j++);
-                               memmove(software + i + 1, software + i + j, strlen(software + i + j) + 1);
-                               }
+                               i++;
+                       if (i != j) software[j] = software[i];
+                       }
+               software[j] = '\0';
                }
 
        model2 = remove_common_prefix(make, model);
@@ -184,7 +183,7 @@ static gchar *exif_build_fCamera(ExifData *exif)
        return text;
 }
 
-static gchar *exif_build_fDateTime(ExifData *exif)
+static gchar *exif_build_formatted_DateTime(ExifData *exif)
 {
        gchar *text = exif_get_data_as_text(exif, "Exif.Photo.DateTimeOriginal");
        gchar *subsec = NULL;
@@ -205,24 +204,24 @@ static gchar *exif_build_fDateTime(ExifData *exif)
        return text;
 }
 
-static gchar *exif_build_fShutterSpeed(ExifData *exif)
+static gchar *exif_build_formatted_ShutterSpeed(ExifData *exif)
 {
        ExifRational *r;
 
        r = exif_get_rational(exif, "Exif.Photo.ExposureTime", NULL);
        if (r && r->num && r->den)
                {
-               double n = (double)r->den / (double)r->num;
+               gdouble n = (gdouble)r->den / (gdouble)r->num;
                return g_strdup_printf("%s%.0fs", n > 1.0 ? "1/" : "",
                                                  n > 1.0 ? n : 1.0 / n);
                }
        r = exif_get_rational(exif, "Exif.Photo.ShutterSpeedValue", NULL);
        if (r && r->num  && r->den)
                {
-               double n = pow(2.0, exif_rational_to_double(r, TRUE));
+               gdouble n = pow(2.0, exif_rational_to_double(r, TRUE));
 
                /* Correct exposure time to avoid values like 1/91s (seen on Minolta DImage 7) */
-               if (n > 1.0 && (int)n - ((int)(n/10))*10 == 1) n--;
+               if (n > 1.0 && (gint)n - ((gint)(n/10))*10 == 1) n--;
 
                return g_strdup_printf("%s%.0fs", n > 1.0 ? "1/" : "",
                                                  n > 1.0 ? floor(n) : 1.0 / n);
@@ -230,9 +229,9 @@ static gchar *exif_build_fShutterSpeed(ExifData *exif)
        return NULL;
 }
 
-static gchar *exif_build_fAperture(ExifData *exif)
+static gchar *exif_build_formatted_Aperture(ExifData *exif)
 {
-       double n;
+       gdouble n;
 
        n = exif_get_rational_as_double(exif, "Exif.Photo.FNumber");
        if (n == 0.0) n = exif_get_rational_as_double(exif, "Exif.Photo.ApertureValue");
@@ -241,11 +240,11 @@ static gchar *exif_build_fAperture(ExifData *exif)
        return g_strdup_printf("f/%.1f", n);
 }
 
-static gchar *exif_build_fExposureBias(ExifData *exif)
+static gchar *exif_build_formatted_ExposureBias(ExifData *exif)
 {
        ExifRational *r;
        gint sign;
-       double n;
+       gdouble n;
 
        r = exif_get_rational(exif, "Exif.Photo.ExposureBiasValue", &sign);
        if (!r) return NULL;
@@ -254,19 +253,19 @@ static gchar *exif_build_fExposureBias(ExifData *exif)
        return g_strdup_printf("%+.1f", n);
 }
 
-static gchar *exif_build_fFocalLength(ExifData *exif)
+static gchar *exif_build_formatted_FocalLength(ExifData *exif)
 {
-       double n;
+       gdouble n;
 
        n = exif_get_rational_as_double(exif, "Exif.Photo.FocalLength");
        if (n == 0.0) return NULL;
        return g_strdup_printf("%.0f mm", n);
 }
 
-static gchar *exif_build_fFocalLength35mmFilm(ExifData *exif)
+static gchar *exif_build_formatted_FocalLength35mmFilm(ExifData *exif)
 {
        gint n;
-       double f, c;
+       gdouble f, c;
 
        if (exif_get_integer(exif, "Exif.Photo.FocalLengthIn35mmFilm", &n) && n != 0)
                {
@@ -274,17 +273,15 @@ static gchar *exif_build_fFocalLength35mmFilm(ExifData *exif)
                }
 
        f = exif_get_rational_as_double(exif, "Exif.Photo.FocalLength");
-       c = get_crop_factor(exif);
+       if (f == 0.0) return NULL;
 
-       if (f != 0.0 && c != 0.0)
-               {
-               return g_strdup_printf("%.0f mm", f * c);
-               }
+       c = get_crop_factor(exif);
+       if (c == 0.0) return NULL;
 
-       return NULL;
+       return g_strdup_printf("%.0f mm", f * c);
 }
 
-static gchar *exif_build_fISOSpeedRating(ExifData *exif)
+static gchar *exif_build_formatted_ISOSpeedRating(ExifData *exif)
 {
        gchar *text;
 
@@ -294,24 +291,24 @@ static gchar *exif_build_fISOSpeedRating(ExifData *exif)
        return text;
 }
 
-static gchar *exif_build_fSubjectDistance(ExifData *exif)
+static gchar *exif_build_formatted_SubjectDistance(ExifData *exif)
 {
        ExifRational *r;
        gint sign;
-       double n;
+       gdouble n;
 
        r = exif_get_rational(exif, "Exif.Photo.SubjectDistance", &sign);
        if (!r) return NULL;
 
-       if ((long)r->num == 0xffffffff) return g_strdup(_("infinity"));
-       if ((long)r->num == 0) return g_strdup(_("unknown"));
+       if ((glong)r->num == (glong)0xffffffff) return g_strdup(_("infinity"));
+       if ((glong)r->num == 0) return g_strdup(_("unknown"));
 
        n = exif_rational_to_double(r, sign);
        if (n == 0.0) return _("unknown");
        return g_strdup_printf("%.3f m", n);
 }
 
-static gchar *exif_build_fFlash(ExifData *exif)
+static gchar *exif_build_formatted_Flash(ExifData *exif)
 {
        /* grr, flash is a bitmask... */
        GString *string;
@@ -361,7 +358,7 @@ static gchar *exif_build_fFlash(ExifData *exif)
        return text;
 }
 
-static gchar *exif_build_fResolution(ExifData *exif)
+static gchar *exif_build_formatted_Resolution(ExifData *exif)
 {
        ExifRational *rx, *ry;
        gchar *units;
@@ -372,19 +369,19 @@ static gchar *exif_build_fResolution(ExifData *exif)
        if (!rx || !ry) return NULL;
 
        units = exif_get_data_as_text(exif, "Exif.Image.ResolutionUnit");
-       text = g_strdup_printf("%0.f x %0.f (%s/%s)", rx->den ? (double)rx->num / rx->den : 1.0,
-                                                     ry->den ? (double)ry->num / ry->den : 1.0,
+       text = g_strdup_printf("%0.f x %0.f (%s/%s)", rx->den ? (gdouble)rx->num / rx->den : 1.0,
+                                                     ry->den ? (gdouble)ry->num / ry->den : 1.0,
                                                      _("dot"), (units) ? units : _("unknown"));
 
        g_free(units);
        return text;
 }
 
-static gchar *exif_build_fColorProfile(ExifData *exif)
+static gchar *exif_build_formatted_ColorProfile(ExifData *exif)
 {
        const gchar *name = "";
        const gchar *source = "";
-       unsigned char *profile_data;
+       guchar *profile_data;
        guint profile_len;
 
        profile_data = exif_get_color_profile(exif, &profile_len);
@@ -432,37 +429,111 @@ static gchar *exif_build_fColorProfile(ExifData *exif)
        return g_strdup_printf("%s (%s)", name, source);
 }
 
+static gchar *exif_build_formatted_GPSPosition(ExifData *exif)
+{
+       GString *string;
+       gchar *text, *ref;
+       ExifRational *value;
+       ExifItem *item;
+       guint i;
+       gdouble p, p3;
+       gulong p1, p2;
+
+       string = g_string_new("");
+
+       item = exif_get_item(exif, "Exif.GPSInfo.GPSLatitude");
+       ref = exif_get_data_as_text(exif, "Exif.GPSInfo.GPSLatitudeRef");
+       if (item && ref)
+               {
+               p = 0;
+               for (i = 0; i < exif_item_get_elements(item); i++)
+                       {
+                       value = exif_item_get_rational(item, NULL, i);
+                       if (value && value->num && value->den)
+                               p += (gdouble)value->num / (gdouble)value->den / pow(60.0, (gdouble)i);
+                       }
+               p1 = (gint)p;
+               p2 = (gint)((p - p1)*60);
+               p3 = ((p - p1)*60 - p2)*60;
+
+               g_string_append_printf(string, "%0d° %0d' %0.2f\" %.1s", p1, p2, p3, ref);
+               } // if (item && ref)
+
+       item = exif_get_item(exif, "Exif.GPSInfo.GPSLongitude");
+       ref = exif_get_data_as_text(exif, "Exif.GPSInfo.GPSLongitudeRef");
+       if (item && ref)
+               {
+               p = 0;
+               for (i = 0; i < exif_item_get_elements(item); i++)
+                       {
+                       value = exif_item_get_rational(item, NULL, i);
+                       if (value && value->num && value->den)
+                       p += (gdouble)value->num / (gdouble)value->den / pow(60.0, (gdouble)i);
+                       }
+               p1 = (gint)p;
+               p2 = (gint)((p - p1)*60);
+               p3 = ((p - p1)*60 - p2)*60;
+
+               g_string_append_printf(string, ", %0d° %0d' %0.2f\" %.1s", p1, p2, p3, ref);
+               } // if (item && ref)
+
+       text = string->str;
+       g_string_free(string, FALSE);
+
+       return text;
+} // static gchar *exif_build_forma...
+
+static gchar *exif_build_formatted_GPSAltitude(ExifData *exif)
+{
+       ExifRational *r;
+       ExifItem *item;
+       gdouble alt;
+       gint ref;
+
+       item = exif_get_item(exif, "Exif.GPSInfo.GPSAltitudeRef");
+       r = exif_get_rational(exif, "Exif.GPSInfo.GPSAltitude", NULL);
+
+       if (!r || !item) return NULL;
+
+       alt = exif_rational_to_double(r, 0);
+       exif_item_get_integer(item, &ref);
+
+       return g_strdup_printf("%0.f m %s", alt, (ref==0)?_("Above Sea Level"):_("Below Sea Level"));
+}
+
 
 /* List of custom formatted pseudo-exif tags */
-#define EXIF_FORMATTED_TAG(name, label) { #name, label, exif_build##_##name }
+#define EXIF_FORMATTED_TAG(name, label) { "formatted."#name, label, exif_build_formatted##_##name }
 
 ExifFormattedText ExifFormattedList[] = {
-       EXIF_FORMATTED_TAG(fCamera,             N_("Camera")),
-       EXIF_FORMATTED_TAG(fDateTime,           N_("Date")),
-       EXIF_FORMATTED_TAG(fShutterSpeed,       N_("Shutter speed")),
-       EXIF_FORMATTED_TAG(fAperture,           N_("Aperture")),
-       EXIF_FORMATTED_TAG(fExposureBias,       N_("Exposure bias")),
-       EXIF_FORMATTED_TAG(fISOSpeedRating,     N_("ISO sensitivity")),
-       EXIF_FORMATTED_TAG(fFocalLength,        N_("Focal length")),
-       EXIF_FORMATTED_TAG(fFocalLength35mmFilm,N_("Focal length 35mm")),
-       EXIF_FORMATTED_TAG(fSubjectDistance,    N_("Subject distance")),
-       EXIF_FORMATTED_TAG(fFlash,              N_("Flash")),
-       EXIF_FORMATTED_TAG(fResolution,         N_("Resolution")),
-       EXIF_FORMATTED_TAG(fColorProfile,       N_("Color profile")),
+       EXIF_FORMATTED_TAG(Camera,              N_("Camera")),
+       EXIF_FORMATTED_TAG(DateTime,            N_("Date")),
+       EXIF_FORMATTED_TAG(ShutterSpeed,        N_("Shutter speed")),
+       EXIF_FORMATTED_TAG(Aperture,            N_("Aperture")),
+       EXIF_FORMATTED_TAG(ExposureBias,        N_("Exposure bias")),
+       EXIF_FORMATTED_TAG(ISOSpeedRating,      N_("ISO sensitivity")),
+       EXIF_FORMATTED_TAG(FocalLength,         N_("Focal length")),
+       EXIF_FORMATTED_TAG(FocalLength35mmFilm, N_("Focal length 35mm")),
+       EXIF_FORMATTED_TAG(SubjectDistance,     N_("Subject distance")),
+       EXIF_FORMATTED_TAG(Flash,               N_("Flash")),
+       EXIF_FORMATTED_TAG(Resolution,          N_("Resolution")),
+       EXIF_FORMATTED_TAG(ColorProfile,        N_("Color profile")),
+       EXIF_FORMATTED_TAG(GPSPosition,         N_("GPS position")),
+       EXIF_FORMATTED_TAG(GPSAltitude,         N_("GPS altitude")),
        { NULL, NULL, NULL }
 };
 
 gchar *exif_get_formatted_by_key(ExifData *exif, const gchar *key, gint *key_valid)
 {
-       /* must begin with f, else not formatted */
-       if (key[0] == 'f')
+       if (strncmp(key, "formatted.", 10) == 0)
                {
                gint i;
 
                if (key_valid) *key_valid = TRUE;
 
+               key += 10;
                for (i = 0; ExifFormattedList[i].key; i++)
-                       if (strcmp(key, ExifFormattedList[i].key) == 0)
+                       if (strcmp(key, ExifFormattedList[i].key + 10) == 0)
                                return ExifFormattedList[i].build_func(exif);
                }
 
@@ -470,15 +541,19 @@ gchar *exif_get_formatted_by_key(ExifData *exif, const gchar *key, gint *key_val
        return NULL;
 }
 
-const gchar *exif_get_description_by_key(const gchar *key)
+gchar *exif_get_description_by_key(const gchar *key)
 {
-       gint i;
-
        if (!key) return NULL;
 
-       for (i = 0; ExifFormattedList[i].key; i++)
-               if (strcmp(key, ExifFormattedList[i].key) == 0)
-                       return _(ExifFormattedList[i].description);
+       if (strncmp(key, "formatted.", 10) == 0)
+               {
+               gint i;
+
+               key += 10;
+               for (i = 0; ExifFormattedList[i].key; i++)
+                       if (strcmp(key, ExifFormattedList[i].key + 10) == 0)
+                               return g_strdup(_(ExifFormattedList[i].description));
+               }
 
        return exif_get_tag_description_by_key(key);
 }
@@ -496,7 +571,7 @@ ExifRational *exif_get_rational(ExifData *exif, const gchar *key, gint *sign)
        ExifItem *item;
 
        item = exif_get_item(exif, key);
-       return exif_item_get_rational(item, sign);
+       return exif_item_get_rational(item, sign, 0);
 }
 
 gchar *exif_get_data_as_text(ExifData *exif, const gchar *key)
@@ -516,11 +591,24 @@ gchar *exif_get_data_as_text(ExifData *exif, const gchar *key)
        return NULL;
 }
 
+
+static FileCacheData *exif_cache;
+
+void exif_release_cb(FileData *fd)
+{
+       exif_free(fd->exif);
+       fd->exif = NULL;
+}
+
 ExifData *exif_read_fd(FileData *fd)
 {
        gchar *sidecar_path = NULL;
 
        if (!fd) return NULL;
+       
+       if (!exif_cache) exif_cache = file_cache_new(exif_release_cb, 4);
+       
+       if (file_cache_get(exif_cache, fd)) return fd->exif;
 
        if (filter_file_class(fd->extension, FORMAT_CLASS_RAWIMAGE))
                {
@@ -539,12 +627,17 @@ ExifData *exif_read_fd(FileData *fd)
                        }
                }
 
-
-       // FIXME: some caching would be nice
-       return exif_read(fd->path, sidecar_path);
+       fd->exif = exif_read(fd->path, sidecar_path);
+       return fd->exif;
 }
 
-
+void exif_free_fd(FileData *fd, ExifData *exif)
+{
+       if (!fd) return;
+       g_assert(fd->exif == exif);
+       
+       file_cache_put(exif_cache, fd, 1);
+}
 
 /* embedded icc in jpeg */
 
@@ -567,7 +660,7 @@ ExifData *exif_read_fd(FileData *fd)
        NNN.: the data in this segment
  */
 
-gint exif_jpeg_segment_find(unsigned char *data, guint size,
+gint exif_jpeg_segment_find(guchar *data, guint size,
                            guchar app_marker, const gchar *magic, guint magic_len,
                            guint *seg_offset, guint *seg_length)
 {
@@ -606,7 +699,7 @@ gint exif_jpeg_segment_find(unsigned char *data, guint size,
        return FALSE;
 }
 
-gint exif_jpeg_parse_color(ExifData *exif, unsigned char *data, guint size)
+gint exif_jpeg_parse_color(ExifData *exif, guchar *data, guint size)
 {
        guint seg_offset = 0;
        guint seg_length = 0;
@@ -655,7 +748,7 @@ gint exif_jpeg_parse_color(ExifData *exif, unsigned char *data, guint size)
 
        if (chunk_count > 0)
                {
-               unsigned char *cp_data;
+               guchar *cp_data;
                guint cp_length = 0;
                guint i;