1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
5 * Copyright (C) 2011-2014, Rob Norris <rw_norris@hotmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * This uses EXIF information from images to create waypoints at those positions
26 * The initial implementation uses libexif, which keeps Viking a pure C program.
27 * Now libgexiv2 is available (in C as a wrapper around the more powerful libexiv2 [C++]) so this is the preferred build.
28 * The attentative reader will notice the use of gexiv2 is a lot simpler as well.
29 * For the time being the libexif code + build is still made available.
32 #include "geotag_exif.h"
44 #include <glib/gi18n.h>
45 #include <glib/gstdio.h>
47 #include <gexiv2/gexiv2.h>
50 #include <libexif/exif-data.h>
51 #include "libjpeg/jpeg-data.h"
56 static void metadata_free ( GExiv2Metadata *gemd )
58 #if GEXIV2_CHECK_VERSION (0,10,4)
59 g_object_unref ( gemd );
61 gexiv2_metadata_free ( gemd );
66 * Attempt to get a single comment from the various exif fields
68 static gchar* geotag_get_exif_comment ( GExiv2Metadata *gemd )
71 // Try various options to create a comment
73 if ( gexiv2_metadata_has_tag ( gemd, "Exif.Image.ImageDescription" ) )
74 return g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Image.ImageDescription" ) );
76 if ( gexiv2_metadata_has_tag ( gemd, "Exif.Image.XPComment" ) )
77 return g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Image.XPComment" ) );
79 if ( gexiv2_metadata_has_tag ( gemd, "Exif.Image.XPSubject" ) )
80 return g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Image.XPSubject" ) );
82 if ( gexiv2_metadata_has_tag ( gemd, "Exif.Image.DateTimeOriginal" ) )
83 return g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Image.DateTimeOriginal" ) );
85 // Otherwise nothing found
92 * Attempt to get a single comment from the various exif fields
94 static gchar* geotag_get_exif_comment ( ExifData *ed )
99 // Try various options to create a comment
101 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_0], EXIF_TAG_IMAGE_DESCRIPTION);
103 exif_entry_get_value ( ee, str, 128 );
104 return g_strdup ( str );
107 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_0], EXIF_TAG_XP_COMMENT);
109 exif_entry_get_value ( ee, str, 128 );
110 return g_strdup ( str );
113 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_0], EXIF_TAG_XP_SUBJECT);
115 exif_entry_get_value ( ee, str, 128 );
116 return g_strdup ( str );
119 // Consider using these for existing GPS info??
120 //#define EXIF_TAG_GPS_TIME_STAMP 0x0007
121 //#define EXIF_TAG_GPS_DATE_STAMP 0x001d
122 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_EXIF], EXIF_TAG_DATE_TIME_ORIGINAL);
124 exif_entry_get_value ( ee, str, 128 );
125 return g_strdup ( str );
128 // Otherwise nothing found
133 * Handles 3 part location Rationals
134 * Handles 1 part rational (must specify 0 for the offset)
136 static gdouble Rational2Double ( unsigned char *data, int offset, ExifByteOrder order )
138 // Explaination from GPS Correlate 'exif-gps.cpp' v 1.6.1
139 // What we are trying to do here is convert the three rationals:
143 // dd/v is easy: result = dd/v.
148 // ss/v is sorta easy.
150 // -- / 3600 = result
152 // Each part is added to the final number.
155 er = exif_get_rational (data, order);
156 ans = (gdouble)er.numerator / (gdouble)er.denominator;
160 er = exif_get_rational (data+(1*offset), order);
161 ans = ans + ( ( (gdouble)er.numerator / (gdouble)er.denominator ) / 60.0 );
162 er = exif_get_rational (data+(2*offset), order);
163 ans = ans + ( ( (gdouble)er.numerator / (gdouble)er.denominator ) / 3600.0 );
168 static struct LatLon get_latlon ( ExifData *ed )
170 struct LatLon ll = { 0.0, 0.0 };
171 const struct LatLon ll0 = { 0.0, 0.0 };
176 // Lat & Long is necessary to form a waypoint.
178 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_LATITUDE);
179 if ( ! ( ee && ee->components == 3 && ee->format == EXIF_FORMAT_RATIONAL ) )
182 ll.lat = Rational2Double ( ee->data,
183 exif_format_get_size(ee->format),
184 exif_data_get_byte_order(ed) );
186 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_LATITUDE_REF);
188 exif_entry_get_value ( ee, str, 128 );
193 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_LONGITUDE);
194 if ( ! ( ee && ee->components == 3 && ee->format == EXIF_FORMAT_RATIONAL ) )
197 ll.lon = Rational2Double ( ee->data,
198 exif_format_get_size(ee->format),
199 exif_data_get_byte_order(ed) );
201 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_LONGITUDE_REF);
203 exif_entry_get_value ( ee, str, 128 );
213 * a_geotag_get_position:
215 * @filename: The (JPG) file with EXIF information in it
217 * Returns: The position in LatLon format.
218 * It will be 0,0 if some kind of failure occurs.
220 struct LatLon a_geotag_get_position ( const gchar *filename )
222 struct LatLon ll = { 0.0, 0.0 };
224 #ifdef HAVE_LIBGEXIV2
225 GExiv2Metadata *gemd = gexiv2_metadata_new ();
226 if ( gexiv2_metadata_open_path ( gemd, filename, NULL ) ) {
230 if ( gexiv2_metadata_get_gps_info ( gemd, &lon, &lat, &alt ) ) {
235 metadata_free ( gemd );
238 // open image with libexif
239 ExifData *ed = exif_data_new_from_file ( filename );
241 // Detect EXIF load failure
245 ExifEntry *ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_VERSION_ID);
246 // Confirm this has a GPS Id - normally "2.0.0.0" or "2.2.0.0"
247 if ( ! ( ee && ee->components == 4 ) )
250 ll = get_latlon ( ed );
253 // Finished with EXIF
254 exif_data_free ( ed );
262 * a_geotag_create_waypoint_from_file:
263 * @filename: The image file to process
264 * @vcmode: The current location mode to use in the positioning of Waypoint
265 * @name: Returns a name for the Waypoint (can be NULL)
267 * Returns: An allocated Waypoint or NULL if Waypoint could not be generated (e.g. no EXIF info)
270 VikWaypoint* a_geotag_create_waypoint_from_file ( const gchar *filename, VikCoordMode vcmode, gchar **name )
272 // Default return values (for failures)
274 VikWaypoint *wp = NULL;
276 #ifdef HAVE_LIBGEXIV2
277 GExiv2Metadata *gemd = gexiv2_metadata_new ();
278 if ( gexiv2_metadata_open_path ( gemd, filename, NULL ) ) {
282 if ( gexiv2_metadata_get_gps_info ( gemd, &lon, &lat, &alt ) ) {
288 // Now create Waypoint with acquired information
290 wp = vik_waypoint_new();
292 // Set info from exif values
294 vik_coord_load_from_latlon ( &(wp->coord), vcmode, &ll );
298 if ( gexiv2_metadata_has_tag ( gemd, "Exif.Image.XPTitle" ) )
299 *name = g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Image.XPTitle" ) );
300 wp->comment = geotag_get_exif_comment ( gemd );
302 vik_waypoint_set_image ( wp, filename );
305 metadata_free ( gemd );
309 //ExifLog *log = NULL;
311 // open image with libexif
312 ExifData *ed = exif_data_new_from_file ( filename );
314 // Detect EXIF load failure
316 // return with no Waypoint
324 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_VERSION_ID);
325 // Confirm this has a GPS Id - normally "2.0.0.0" or "2.2.0.0"
326 if ( ! ( ee && ee->components == 4 ) )
328 // Could test for these versions explicitly but may have byte order issues...
329 //if ( ! ( ee->data[0] == 2 && ee->data[2] == 0 && ee->data[3] == 0 ) )
332 ll = get_latlon ( ed );
334 // Hopefully won't have valid images at 0,0!
335 if ( ll.lat == 0.0 && ll.lon == 0.0 )
339 // Not worried if none of the other fields exist, as can default the values to something
342 gdouble alt = VIK_DEFAULT_ALTITUDE;
343 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_ALTITUDE);
344 if ( ee && ee->components == 1 && ee->format == EXIF_FORMAT_RATIONAL ) {
345 alt = Rational2Double ( ee->data,
347 exif_data_get_byte_order(ed) );
349 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_ALTITUDE_REF);
350 if ( ee && ee->components == 1 && ee->format == EXIF_FORMAT_BYTE && ee->data[0] == 1 )
355 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_0], EXIF_TAG_XP_TITLE);
357 exif_entry_get_value ( ee, str, 128 );
358 *name = g_strdup ( str );
362 // Now create Waypoint with acquired information
364 wp = vik_waypoint_new();
366 // Set info from exif values
368 vik_coord_load_from_latlon ( &(wp->coord), vcmode, &ll );
372 wp->comment = geotag_get_exif_comment ( ed );
374 vik_waypoint_set_image ( wp, filename );
377 // Finished with EXIF
378 exif_data_free ( ed );
386 * a_geotag_waypoint_positioned:
387 * @filename: The image file to process
388 * @coord: The location for positioning the Waypoint
389 * @name: Returns a name for the Waypoint (can be NULL)
390 * @waypoint: An existing waypoint to update (can be NULL to generate a new waypoint)
392 * Returns: An allocated waypoint if the input waypoint is NULL,
393 * otherwise the passed in waypoint is updated
395 * Here EXIF processing is used to get non position related information (i.e. just the comment)
398 VikWaypoint* a_geotag_waypoint_positioned ( const gchar *filename, VikCoord coord, gdouble alt, gchar **name, VikWaypoint *wp )
402 // Need to create waypoint
403 wp = vik_waypoint_new();
409 #ifdef HAVE_LIBGEXIV2
410 GExiv2Metadata *gemd = gexiv2_metadata_new ();
411 if ( gexiv2_metadata_open_path ( gemd, filename, NULL ) ) {
412 wp->comment = geotag_get_exif_comment ( gemd );
413 if ( gexiv2_metadata_has_tag ( gemd, "Exif.Image.XPTitle" ) )
414 *name = g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Image.XPTitle" ) );
416 metadata_free ( gemd );
419 ExifData *ed = exif_data_new_from_file ( filename );
421 // Set info from exif values
423 wp->comment = geotag_get_exif_comment ( ed );
428 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_0], EXIF_TAG_XP_TITLE);
430 exif_entry_get_value ( ee, str, 128 );
431 *name = g_strdup ( str );
434 // Finished with EXIF
435 exif_data_free ( ed );
440 vik_waypoint_set_image ( wp, filename );
446 * a_geotag_get_exif_date_from_file:
447 * @filename: The image file to process
448 * @has_GPS_info: Returns whether the file has existing GPS information
450 * Returns: An allocated string with the date and time in EXIF_DATE_FORMAT, otherwise NULL if some kind of failure
452 * Here EXIF processing is used to get time information
455 gchar* a_geotag_get_exif_date_from_file ( const gchar *filename, gboolean *has_GPS_info )
457 gchar* datetime = NULL;
458 *has_GPS_info = FALSE;
460 #ifdef HAVE_LIBGEXIV2
461 GExiv2Metadata *gemd = gexiv2_metadata_new ();
462 if ( gexiv2_metadata_open_path ( gemd, filename, NULL ) ) {
464 *has_GPS_info = ( gexiv2_metadata_get_gps_longitude(gemd,&lon) && gexiv2_metadata_get_gps_latitude(gemd,&lat) );
466 // Prefer 'Photo' version over 'Image'
467 if ( gexiv2_metadata_has_tag ( gemd, "Exif.Photo.DateTimeOriginal" ) )
468 datetime = g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Photo.DateTimeOriginal" ) );
470 datetime = g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Image.DateTimeOriginal" ) );
472 metadata_free ( gemd );
475 ExifData *ed = exif_data_new_from_file ( filename );
477 // Detect EXIF load failure
484 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_EXIF], EXIF_TAG_DATE_TIME_ORIGINAL);
486 exif_entry_get_value ( ee, str, 128 );
487 datetime = g_strdup ( str );
492 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_VERSION_ID);
493 // Confirm this has a GPS Id - normally "2.0.0.0" or "2.2.0.0"
494 if ( ee && ee->components == 4 )
495 *has_GPS_info = TRUE;
497 // Check other basic GPS fields exist too
498 // I have encountered some images which have just the EXIF_TAG_GPS_VERSION_ID but nothing else
499 // So to confirm check more EXIF GPS TAGS:
500 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_LATITUDE);
502 *has_GPS_info = FALSE;
503 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_LONGITUDE);
505 *has_GPS_info = FALSE;
507 exif_data_free ( ed );
515 /**! If the entry doesn't exist, create it.
516 * Based on exif command line action_create_value function in exif 0.6.20
518 static ExifEntry* my_exif_create_value (ExifData *ed, ExifTag tag, ExifIfd ifd)
520 ExifEntry *e = exif_content_get_entry (ed->ifd[ifd], tag);
522 e = exif_entry_new ();
523 exif_content_add_entry (ed->ifd[ifd], e);
525 exif_entry_initialize (e, tag);
527 // exif_entry_initialize doesn't seem to do much, especially for the GPS tags
528 // so have to setup fields ourselves:
531 if ( tag == EXIF_TAG_GPS_VERSION_ID ) {
532 e->format = EXIF_FORMAT_BYTE;
534 e->size = sizeof (char) * e->components;
537 e->data = g_malloc (e->size);
539 if ( tag == EXIF_TAG_GPS_MAP_DATUM ||
540 tag == EXIF_TAG_GPS_LATITUDE_REF || tag == EXIF_TAG_GPS_LONGITUDE_REF ||
541 tag == EXIF_TAG_GPS_PROCESSING_METHOD ) {
542 e->format = EXIF_FORMAT_ASCII;
543 // NB Allocation is handled later on when the actual string used is known
545 if ( tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE ) {
546 e->format = EXIF_FORMAT_RATIONAL;
548 e->size = sizeof (ExifRational) * e->components;
551 e->data = g_malloc (e->size);
553 if ( tag == EXIF_TAG_GPS_ALTITUDE ) {
554 e->format = EXIF_FORMAT_RATIONAL;
556 e->size = sizeof (ExifRational) * e->components;
559 e->data = g_malloc (e->size);
561 if ( tag == EXIF_TAG_GPS_ALTITUDE_REF ) {
563 e->size = sizeof (char) * e->components;
566 e->data = g_malloc (e->size);
568 /* The entry has been added to the IFD, so we can unref it */
569 //exif_entry_unref(e);
570 // Crashes later on, when saving to jpeg if the above unref is enabled!!
571 // ?Some other malloc problem somewhere?
576 /** Heavily based on convert_arg_to_entry from exif command line tool.
577 * But without ExifLog, exitting, use of g_* io functions
578 * and can take a gdouble value instead of a string
580 static void convert_to_entry (const char *set_value, gdouble gdvalue, ExifEntry *e, ExifByteOrder o)
582 unsigned int i, numcomponents;
583 char *value_p = NULL;
586 * ASCII strings are handled separately,
587 * since they don't require any conversion.
589 if (e->format == EXIF_FORMAT_ASCII ||
590 e->tag == EXIF_TAG_USER_COMMENT) {
591 if (e->data) g_free (e->data);
592 e->components = strlen (set_value) + 1;
593 if (e->tag == EXIF_TAG_USER_COMMENT)
594 e->components += 8 - 1;
595 e->size = sizeof (char) * e->components;
596 e->data = g_malloc (e->size);
598 g_warning (_("Not enough memory."));
601 if (e->tag == EXIF_TAG_USER_COMMENT) {
602 /* assume ASCII charset */
603 /* TODO: get this from the current locale */
604 memcpy ((char *) e->data, "ASCII\0\0\0", 8);
605 memcpy ((char *) e->data + 8, set_value,
608 strcpy ((char *) e->data, set_value);
613 * Make sure we can handle this entry
615 if ((e->components == 0) && *set_value) {
616 g_warning (_("Setting a value for this tag is unsupported!"));
620 gboolean use_string = (set_value != NULL);
622 /* Copy the string so we can modify it */
623 buf = g_strdup (set_value);
626 value_p = strtok (buf, " ");
629 numcomponents = e->components;
630 for (i = 0; i < numcomponents; ++i) {
635 g_warning (_("Too few components specified (need %d, found %d)\n"), numcomponents, i);
638 if (!isdigit(*value_p) && (*value_p != '+') && (*value_p != '-')) {
639 g_warning (_("Numeric value expected\n"));
644 s = exif_format_get_size (e->format);
646 case EXIF_FORMAT_ASCII:
647 g_warning (_("This shouldn't happen!"));
650 case EXIF_FORMAT_SHORT:
651 exif_set_short (e->data + (s * i), o, atoi (value_p));
653 case EXIF_FORMAT_SSHORT:
654 exif_set_sshort (e->data + (s * i), o, atoi (value_p));
656 case EXIF_FORMAT_RATIONAL: {
660 if ( use_string && value_p )
661 val = fabs (atol (value_p));
663 val = fabs (gdvalue);
666 // One (or first) part rational
668 // Sneak peek into tag as location tags need rounding down to give just the degrees part
669 if ( e->tag == EXIF_TAG_GPS_LATITUDE || e->tag == EXIF_TAG_GPS_LONGITUDE ) {
670 er.numerator = (ExifLong) floor ( val );
671 er.denominator = 1.0;
674 // I don't see any point in doing anything too complicated here,
675 // such as trying to work out the 'best' denominator
676 // For the moment use KISS principle.
677 // Fix a precision of 1/100 metre as that's more than enough for GPS accuracy especially altitudes!
678 er.denominator = 100.0;
679 er.numerator = (ExifLong) (val * er.denominator);
683 // Now for Location 3 part rationals do Mins and Seconds format
685 // Rounded down minutes
687 er.denominator = 1.0;
688 er.numerator = (ExifLong) ( (int) floor ( ( val - floor (val) ) * 60.0 ) );
693 er.denominator = 100.0;
695 // Fractional minute.
696 double FracPart = ((val - floor(val)) * 60) - (double)(int) floor ( ( val - floor (val) ) * 60.0 );
697 er.numerator = (ExifLong) ( (int)floor(FracPart * 6000) ); // Convert to seconds.
699 exif_set_rational (e->data + (s * i), o, er );
702 case EXIF_FORMAT_LONG:
703 exif_set_long (e->data + (s * i), o, atol (value_p));
705 case EXIF_FORMAT_SLONG:
706 exif_set_slong (e->data + (s * i), o, atol (value_p));
708 case EXIF_FORMAT_BYTE:
709 case EXIF_FORMAT_SBYTE:
710 case EXIF_FORMAT_UNDEFINED: /* treat as byte array */
711 e->data[s * i] = atoi (value_p);
713 case EXIF_FORMAT_FLOAT:
714 case EXIF_FORMAT_DOUBLE:
715 case EXIF_FORMAT_SRATIONAL:
717 g_warning (_("Not yet implemented!"));
722 value_p = strtok (NULL, " ");
730 g_warning (_("Warning; Too many components specified!"));
735 * a_geotag_write_exif_gps:
736 * @filename: The image file to save information in
737 * @coord: The location
738 * @alt: The elevation
740 * Returns: A value indicating success: 0, or some other value for failure
743 gint a_geotag_write_exif_gps ( const gchar *filename, VikCoord coord, gdouble alt, gboolean no_change_mtime )
745 gint result = 0; // OK so far...
747 // Save mtime for later use
748 struct stat stat_save;
749 if ( no_change_mtime )
750 if ( stat ( filename, &stat_save ) != 0 )
751 g_warning ( "%s couldn't read: %s", __FUNCTION__, filename );
753 #ifdef HAVE_LIBGEXIV2
754 GExiv2Metadata *gemd = gexiv2_metadata_new ();
755 if ( gexiv2_metadata_open_path ( gemd, filename, NULL ) ) {
757 vik_coord_to_latlon ( &coord, &ll );
758 if ( ! gexiv2_metadata_set_gps_info ( gemd, ll.lon, ll.lat, alt ) ) {
759 result = 1; // Failed
762 GError *error = NULL;
763 if ( ! gexiv2_metadata_save_file ( gemd, filename, &error ) ) {
765 g_warning ( "Write EXIF failure:%s" , error->message );
766 g_error_free ( error );
770 metadata_free ( gemd );
774 Appears libexif doesn't actually support writing EXIF data directly to files
775 Thus embed command line exif writing method within Viking
776 (for example this is done by Enlightment - http://www.enlightenment.org/ )
777 This appears to be JPEG only, but is probably 99% of our use case
778 Alternatively consider using libexiv2 and C++...
781 // Actual EXIF settings here...
784 /* Parse the JPEG file. */
785 jdata = jpeg_data_new ();
786 jpeg_data_load_file (jdata, filename);
788 // Get current values
789 ExifData *ed = exif_data_new_from_file ( filename );
791 ed = exif_data_new ();
793 // Update ExifData with our new settings
796 // I don't understand it, but when saving the 'ed' nothing gets set after putting in the GPS ID tag - so it must come last
797 // (unless of course there is some bug in the setting of the ID, that prevents subsequent tags)
800 ee = my_exif_create_value (ed, EXIF_TAG_GPS_ALTITUDE, EXIF_IFD_GPS);
801 convert_to_entry ( NULL, alt, ee, exif_data_get_byte_order(ed) );
803 // byte 0 meaning "sea level" or 1 if the value is negative.
804 ee = my_exif_create_value (ed, EXIF_TAG_GPS_ALTITUDE_REF, EXIF_IFD_GPS);
805 convert_to_entry ( alt < 0.0 ? "1" : "0", 0.0, ee, exif_data_get_byte_order(ed) );
807 ee = my_exif_create_value (ed, EXIF_TAG_GPS_PROCESSING_METHOD, EXIF_IFD_GPS);
808 // see http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/GPS.html
809 convert_to_entry ( "MANUAL", 0.0, ee, exif_data_get_byte_order(ed) );
811 ee = my_exif_create_value (ed, EXIF_TAG_GPS_MAP_DATUM, EXIF_IFD_GPS);
812 convert_to_entry ( "WGS-84", 0.0, ee, exif_data_get_byte_order(ed) );
815 vik_coord_to_latlon ( &coord, &ll );
817 ee = my_exif_create_value (ed, EXIF_TAG_GPS_LATITUDE_REF, EXIF_IFD_GPS);
819 convert_to_entry ( ll.lat < 0.0 ? "S" : "N", 0.0, ee, exif_data_get_byte_order(ed) );
821 ee = my_exif_create_value (ed, EXIF_TAG_GPS_LATITUDE, EXIF_IFD_GPS);
822 convert_to_entry ( NULL, ll.lat, ee, exif_data_get_byte_order(ed) );
824 ee = my_exif_create_value (ed, EXIF_TAG_GPS_LONGITUDE_REF, EXIF_IFD_GPS);
826 convert_to_entry ( ll.lon < 0.0 ? "W" : "E", 0.0, ee, exif_data_get_byte_order(ed) );
828 ee = my_exif_create_value (ed, EXIF_TAG_GPS_LONGITUDE, EXIF_IFD_GPS);
829 convert_to_entry ( NULL, ll.lon, ee, exif_data_get_byte_order(ed) );
831 ee = my_exif_create_value (ed, EXIF_TAG_GPS_VERSION_ID, EXIF_IFD_GPS);
832 //convert_to_entry ( "2 0 0 0", 0.0, ee, exif_data_get_byte_order(ed) );
833 convert_to_entry ( "2 2 0 0", 0.0, ee, exif_data_get_byte_order(ed) );
835 jpeg_data_set_exif_data (jdata, ed);
838 /* Save the modified image. */
839 result = jpeg_data_save_file (jdata, filename);
841 // Convert result from 1 for success, 0 for failure into our scheme
844 jpeg_data_unref (jdata);
847 // Epic fail - file probably not a JPEG
851 exif_data_free ( ed );
855 if ( no_change_mtime ) {
856 // Restore mtime, using the saved value
857 struct stat stat_tmp;
859 (void)stat ( filename, &stat_tmp );
860 utb.actime = stat_tmp.st_atime;
861 utb.modtime = stat_save.st_mtime;
862 // Not security critical, thus potential Time of Check Time of Use race condition is not bad
864 if ( g_utime ( filename, &utb ) != 0 )
865 g_warning ( "%s couldn't set time on: %s", __FUNCTION__, filename );