[tracker-miners/wip/carlosg/parallel-installable: 40/41] libtracker-extract: Use g_ascii_dtostr for GPS coordinates in Exif



commit b1b59af2d821a5b0ea4aece498d4a7116735fe80
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Mar 23 16:07:20 2020 +0100

    libtracker-extract: Use g_ascii_dtostr for GPS coordinates in Exif
    
    g_strdup_printf() use of separators is locale-specific. Not what we
    want here. Expectations in tests also had to be updated, even though
    they correctly expect dot separators (a reason of breakage with certain
    locales), the numbers will be given in full precision instead of some
    random printf-friendly truncation.

 src/libtracker-extract/tracker-exif.c              | 41 ++++++++++++++++++++--
 .../images/jpeg-gps-location.expected.json         |  4 +--
 tests/libtracker-extract/tracker-exif-test.c       |  8 ++---
 3 files changed, 44 insertions(+), 9 deletions(-)
---
diff --git a/src/libtracker-extract/tracker-exif.c b/src/libtracker-extract/tracker-exif.c
index ade5c66ed..df4ef6362 100644
--- a/src/libtracker-extract/tracker-exif.c
+++ b/src/libtracker-extract/tracker-exif.c
@@ -311,6 +311,7 @@ get_gps_coordinate (ExifData *exif,
        if (entry && refentry) {
                ExifByteOrder order;
                ExifRational degrees, minutes, seconds;
+               gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
                gfloat f;
 
                if (entry->size == 24) {
@@ -340,7 +341,7 @@ get_gps_coordinate (ExifData *exif,
                                return NULL;
                        }
 
-                       return g_strdup_printf ("%f", f);
+                       return g_strdup (g_ascii_dtostr (buf, sizeof (buf), (gdouble) f));
                } else {
                        gchar buf[25] = { 0 };
 
@@ -380,6 +381,7 @@ get_gps_altitude (ExifData *exif,
        if (entry) {
                ExifByteOrder order;
                ExifRational c;
+               gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
                gfloat f;
 
                order = exif_data_get_byte_order (exif);
@@ -401,7 +403,7 @@ get_gps_altitude (ExifData *exif,
                                f = -1 * f;
                        }
                }
-               return g_strdup_printf ("%f", f);
+               return g_strdup (g_ascii_dtostr (buf, sizeof (buf), (gdouble) f));
        }
 
        return NULL;
@@ -423,6 +425,39 @@ get_int (ExifData *exif,
        return -1;
 }
 
+static gboolean
+get_double (ExifData *exif,
+            ExifTag   tag,
+            gdouble  *val)
+{
+       ExifEntry *entry = exif_data_get_entry (exif, tag);
+
+       if (entry) {
+               ExifByteOrder order;
+               ExifRational value;
+
+               order = exif_data_get_byte_order (exif);
+               value = exif_get_rational (entry->data, order);
+
+               *val = (gdouble) value.numerator / (gdouble) value.denominator;
+               return TRUE;
+       }
+
+       return FALSE;
+}
+
+static gchar *
+get_double_str (ExifData *exif,
+                ExifTag   tag)
+{
+       gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
+       gdouble value;
+
+       if (!get_double (exif, tag, &value))
+               return NULL;
+
+       return g_strdup (g_ascii_dtostr (buf, sizeof(buf), value));
+}
 
 static gchar *
 get_value (ExifData *exif,
@@ -544,7 +579,7 @@ parse_exif (const unsigned char *buffer,
        if(!data->gps_longitude)
                data->gps_longitude = get_gps_coordinate (exif, EXIF_TAG_GPS_LONGITUDE, 
EXIF_TAG_GPS_LONGITUDE_REF);
        if(!data->gps_direction)
-               data->gps_direction = get_value (exif, EXIF_TAG_GPS_IMG_DIRECTION);
+               data->gps_direction = get_double_str (exif, EXIF_TAG_GPS_IMG_DIRECTION);
 
        exif_data_free (exif);
 #endif /* HAVE_LIBEXIF */
diff --git a/tests/functional-tests/test-extraction-data/images/jpeg-gps-location.expected.json 
b/tests/functional-tests/test-extraction-data/images/jpeg-gps-location.expected.json
index a2feae5cb..33c37fc6e 100644
--- a/tests/functional-tests/test-extraction-data/images/jpeg-gps-location.expected.json
+++ b/tests/functional-tests/test-extraction-data/images/jpeg-gps-location.expected.json
@@ -12,8 +12,8 @@
         "nmm:focalLength": "5.0",
         "nie:comment": "This is a for tracker test",
         "slo:location": {
-            "slo:latitude": "-35.253368",
-            "slo:longitude": "149.112579",
+            "slo:latitude": "-35.253368377685547",
+            "slo:longitude": "149.11257934570312",
             "slo:postalAddress": {
                 "nco:locality": "Tig",
                 "nco:country": "Banglore"
diff --git a/tests/libtracker-extract/tracker-exif-test.c b/tests/libtracker-extract/tracker-exif-test.c
index 1245bedff..490c4671d 100644
--- a/tests/libtracker-extract/tracker-exif-test.c
+++ b/tests/libtracker-extract/tracker-exif-test.c
@@ -60,10 +60,10 @@ test_exif_parse (void)
         g_assert_cmpstr (exif->y_resolution, ==, "72");
         g_assert_cmpint (exif->resolution_unit, ==, 2);
 
-        g_assert_cmpstr (exif->gps_altitude, ==, "237.000000"); // -n -exif:gpsaltitude=237 
-        g_assert_cmpstr (exif->gps_latitude, ==, "-42.500000"); // -exif:gpslatitude="42 30 0.00" 
-exif:gpslatituderef=S
-        g_assert_cmpstr (exif->gps_longitude, ==, "-10.166675"); // -exif:gpslongitude="10 10 0.03" 
-exif:gpslongituderef=W
-        g_assert_cmpstr (exif->gps_direction, ==, "12.3"); // -n -Exif:GPSImgDirection=12.3
+        g_assert_cmpstr (exif->gps_altitude, ==, "237"); // -n -exif:gpsaltitude=237
+        g_assert_cmpstr (exif->gps_latitude, ==, "-42.5"); // -exif:gpslatitude="42 30 0.00" 
-exif:gpslatituderef=S
+        g_assert_cmpstr (exif->gps_longitude, ==, "-10.166674613952637"); // -exif:gpslongitude="10 10 0.03" 
-exif:gpslongituderef=W
+        g_assert_cmpstr (exif->gps_direction, ==, "12.300000000000001"); // -n -Exif:GPSImgDirection=12.3
         
         tracker_exif_free (exif);
         g_free (blob);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]