Fix #381: Feature-Request: Make JPEG comment available for overlays
[geeqie.git] / src / exif.c
index f54c996..872b9cc 100644 (file)
@@ -1,13 +1,22 @@
 /*
- * Geeqie
- * (C) 2006 John Ellis
- *  Copyright (C) 2008 The Geeqie Team
+ * Copyright (C) 2003, 2006 John Ellis
+ * Copyright (C) 2008 - 2016 The Geeqie Team
  *
- *  Authors:
- *    Support for Exif file format, originally written by Eric Swalens.
- *    Modified by Quy Tonthat
+ * Authors: Eric Swalens, Quy Tonthat
  *
- *    Reimplemented with generic data storage by John Ellis (Nov 2003)
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  *  The tags were added with information from the FREE document:
  *     http://www.ba.wakwak.com/~tsuruzoh/Computer/Digicams/exif-e.html
  *        ComponentsConfiguration
  *        UserComment (convert this to UTF-8?)
  *     Add support for marker tag 0x0000
- *
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-*/
+ */
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
 #include <math.h>
 
 #include <glib.h>
+#include <glib/gprintf.h>
 
 #include "intl.h"
 
 #include "main.h"
 #include "exif-int.h"
+#include "jpeg_parser.h"
 
 #include "format_raw.h"
 #include "ui_fileops.h"
@@ -510,8 +506,6 @@ ExifItem *exif_item_new(ExifFormatType format, guint tag,
        item->tag = tag;
        item->marker = marker;
        item->elements = elements;
-       item->data = NULL;
-       item->data_len = 0;
 
        switch (format)
                {
@@ -608,7 +602,7 @@ gchar *exif_item_get_description(ExifItem *item)
        return g_strdup(_(item->marker->description));
 }
 
-const gchar *exif_item_get_format_name(ExifItem *item, gint brief)
+const gchar *exif_item_get_format_name(ExifItem *item, gboolean brief)
 {
        if (!item || !item->marker) return NULL;
        return (brief) ? ExifFormatList[item->format].short_name : ExifFormatList[item->format].description;
@@ -928,7 +922,7 @@ static gint exif_parse_IFD_entry(ExifData *exif, guchar *tiff, guint offset,
        if (data_length > 4)
                {
                data_offset = data_val;
-               if (size < data_offset + data_length)
+               if (size < data_offset || size < data_offset + data_length)
                        {
                        log_printf("warning: exif tag %s data will overrun end of file, ignored.\n", marker->key);
                        return -1;
@@ -1087,7 +1081,7 @@ static gint exif_jpeg_parse(ExifData *exif,
                return -2;
                }
 
-       if (exif_jpeg_segment_find(data, size, JPEG_MARKER_APP1,
+       if (jpeg_segment_find(data, size, JPEG_MARKER_APP1,
                                   "Exif\x00\x00", 6,
                                   &seg_offset, &seg_length))
                {
@@ -1111,6 +1105,18 @@ guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
 }
 
 
+gchar* exif_get_image_comment(FileData* fd)
+{
+       log_printf("%s", _("Can't get image comment: not compiled with Exiv2.\n"));
+       return g_strdup("");
+}
+
+void exif_set_image_comment(FileData* fd, const gchar* comment)
+{
+       log_printf("%s", _("Can't set image comment: not compiled with Exiv2.\n"));
+}
+
+
 /*
  *-------------------------------------------------------------------
  * misc
@@ -1185,6 +1191,11 @@ static gint unmap_file(gpointer mapping, gint size)
        return 0;
 }
 
+ExifData *exif_get_original(ExifData *processed)
+{
+       return processed;
+}
+
 void exif_free(ExifData *exif)
 {
        GList *work;
@@ -1204,7 +1215,7 @@ void exif_free(ExifData *exif)
        g_free(exif);
 }
 
-ExifData *exif_read(gchar *path, gchar *sidecar_path)
+ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
 {
        ExifData *exif;
        gpointer f;
@@ -1222,8 +1233,6 @@ ExifData *exif_read(gchar *path, gchar *sidecar_path)
        g_free(pathl);
 
        exif = g_new0(ExifData, 1);
-       exif->items = NULL;
-       exif->current = NULL;
        exif->path = g_strdup(path);
 
        res = exif_jpeg_parse(exif, (guchar *)f, size, ExifKnownMarkersList);
@@ -1278,11 +1287,6 @@ ExifData *exif_read(gchar *path, gchar *sidecar_path)
 
        if (exif) exif->items = g_list_reverse(exif->items);
 
-#if 0
-       exif_write_data_list(exif, stdout, TRUE);
-       exif_write_data_list(exif, stdout, FALSE);
-#endif
-
        return exif;
 }
 
@@ -1306,13 +1310,8 @@ ExifItem *exif_get_item(ExifData *exif, const gchar *key)
 
 #define EXIF_DATA_AS_TEXT_MAX_COUNT 16
 
-gchar *exif_item_get_string(ExifItem *item, gint idx)
-{
-       return exif_item_get_data_as_text(item);
-}
-
 
-gchar *exif_item_get_data_as_text(ExifItem *item)
+static gchar *exif_item_get_data_as_text_full(ExifItem *item, MetadataFormat format)
 {
        const ExifMarker *marker;
        gpointer data;
@@ -1337,7 +1336,7 @@ gchar *exif_item_get_data_as_text(ExifItem *item)
                case EXIF_FORMAT_BYTE_UNSIGNED:
                case EXIF_FORMAT_BYTE:
                case EXIF_FORMAT_UNDEFINED:
-                       if (ne == 1 && marker->list)
+                       if (ne == 1 && marker->list && format == METADATA_FORMATTED)
                                {
                                gchar *result;
                                guchar val;
@@ -1362,10 +1361,10 @@ gchar *exif_item_get_data_as_text(ExifItem *item)
                                }
                        break;
                case EXIF_FORMAT_STRING:
-                       string = g_string_append(string, (gchar *)(item->data));
+                       if (item->data) string = g_string_append(string, (gchar *)(item->data));
                        break;
                case EXIF_FORMAT_SHORT_UNSIGNED:
-                       if (ne == 1 && marker->list)
+                       if (ne == 1 && marker->list && format == METADATA_FORMATTED)
                                {
                                gchar *result;
 
@@ -1448,9 +1447,20 @@ gchar *exif_item_get_data_as_text(ExifItem *item)
        return text;
 }
 
+gchar *exif_item_get_string(ExifItem *item, gint idx)
+{
+       return exif_item_get_data_as_text_full(item, METADATA_PLAIN);
+}
+
+gchar *exif_item_get_data_as_text(ExifItem *item)
+{
+       return exif_item_get_data_as_text_full(item, METADATA_FORMATTED);
+}
+
 gint exif_item_get_integer(ExifItem *item, gint *value)
 {
        if (!item) return FALSE;
+       if (!item->elements) return FALSE;
 
        switch (item->format)
                {
@@ -1509,7 +1519,7 @@ gchar *exif_get_tag_description_by_key(const gchar *key)
        i = 0;
        while (ExifKnownGPSInfoMarkersList[i].tag > 0)
        {
-          if (strcmp(key, ExifKnownGPSInfoMarkersList[i].key) == 0) return _(ExifKnownGPSInfoMarkersList[i].description);
+          if (strcmp(key, ExifKnownGPSInfoMarkersList[i].key) == 0) return g_strdup(_(ExifKnownGPSInfoMarkersList[i].description));
           i++;
        }
 
@@ -1524,7 +1534,7 @@ static void exif_write_item(FILE *f, ExifItem *item)
        if (text)
                {
                gchar *tag = exif_item_get_tag_name(item);
-               fprintf(f, "%4x %9s %30s %s\n", item->tag, ExifFormatList[item->format].short_name,
+               g_fprintf(f, "%4x %9s %30s %s\n", item->tag, ExifFormatList[item->format].short_name,
                        tag, text);
                g_free(tag);
                }
@@ -1535,8 +1545,8 @@ void exif_write_data_list(ExifData *exif, FILE *f, gint human_readable_list)
 {
        if (!f || !exif) return;
 
-       fprintf(f, " tag   format                             key value\n");
-       fprintf(f, "----------------------------------------------------\n");
+       g_fprintf(f, " tag   format                             key value\n");
+       g_fprintf(f, "----------------------------------------------------\n");
 
        if (human_readable_list)
                {
@@ -1550,7 +1560,7 @@ void exif_write_data_list(ExifData *exif, FILE *f, gint human_readable_list)
                        text = exif_get_formatted_by_key(exif, ExifFormattedList[i].key, NULL);
                        if (text)
                                {
-                               fprintf(f, "     %9s %30s %s\n", "string", ExifFormattedList[i].key, text);
+                               g_fprintf(f, "     %9s %30s %s\n", "string", ExifFormattedList[i].key, text);
                                }
                        i++;
                        }
@@ -1570,30 +1580,54 @@ void exif_write_data_list(ExifData *exif, FILE *f, gint human_readable_list)
                        exif_write_item(f, item);
                        }
                }
-       fprintf(f, "----------------------------------------------------\n");
+       g_fprintf(f, "----------------------------------------------------\n");
 }
 
-gint exif_write(ExifData *exif)
+gboolean exif_write(ExifData *exif)
 {
-       log_printf("Not compiled with EXIF write support");
-       return 0;
+       log_printf("Not compiled with EXIF write support\n");
+       return FALSE;
 }
 
-ExifItem *exif_add_item(ExifData *exif, const gchar *key)
+gboolean exif_write_sidecar(ExifData *exif, gchar *path)
 {
-       return NULL;
+       log_printf("Not compiled with EXIF write support\n");
+       return FALSE;
 }
 
-gint exif_item_delete(ExifData *exif, ExifItem *item)
+
+gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
 {
        return 0;
 }
 
-gint exif_item_set_string(ExifItem *item, const gchar *str)
+GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
 {
-       return 0;
-}
+       gchar *str;
+       ExifItem *item;
+
+       if (!key) return NULL;
+
+       /* convert xmp key to exif key */
+       if (strcmp(key, "Xmp.tiff.Orientation") == 0) key = "Exif.Image.Orientation";
 
+       if (format == METADATA_FORMATTED)
+               {
+               gchar *text;
+               gint key_valid;
+               text = exif_get_formatted_by_key(exif, key, &key_valid);
+               if (key_valid) return g_list_append(NULL, text);
+               }
+
+       item = exif_get_item(exif, key);
+       if (!item) return NULL;
+
+       str = exif_item_get_data_as_text_full(item, format);
+
+       if (!str) return NULL;
+
+       return g_list_append(NULL, str);
+}
 
 typedef struct _UnmapData UnmapData;
 struct _UnmapData
@@ -1605,22 +1639,21 @@ struct _UnmapData
 
 static GList *exif_unmap_list = 0;
 
-guchar *exif_get_preview(ExifData *exif, guint *data_len)
+guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
 {
-       int success;
        guint offset;
        const gchar* path;
        struct stat st;
        guchar *map_data;
        size_t map_len;
        int fd;
-       
+
        if (!exif) return NULL;
        path = exif->path;
 
        fd = open(path, O_RDONLY);
-               
-               
+
+
        if (fd == -1)
                {
                return 0;
@@ -1644,14 +1677,14 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len)
                {
                UnmapData *ud;
 
-               DEBUG_1("%s: offset %lu", path, offset);
+               DEBUG_1("%s: offset %u", path, offset);
 
                *data_len = map_len - offset;
                ud = g_new(UnmapData, 1);
                ud->ptr = map_data + offset;
                ud->map_data = map_data;
                ud->map_len = map_len;
-               
+
                exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
                return ud->ptr;
                }
@@ -1664,7 +1697,7 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len)
 void exif_free_preview(guchar *buf)
 {
        GList *work = exif_unmap_list;
-       
+
        while (work)
                {
                UnmapData *ud = (UnmapData *)work->data;
@@ -1679,6 +1712,9 @@ void exif_free_preview(guchar *buf)
        g_assert_not_reached();
 }
 
+void exif_init(void)
+{
+}
 
 #endif
 /* not HAVE_EXIV2 */