Fix #381: Feature-Request: Make JPEG comment available for overlays
[geeqie.git] / src / exif.c
index 2788b81..872b9cc 100644 (file)
@@ -1,13 +1,22 @@
 /*
- * Geeqie
- * (C) 2006 John Ellis
- *  Copyright (C) 2008 - 2009 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"
@@ -73,6 +67,7 @@
 
 #include "main.h"
 #include "exif-int.h"
+#include "jpeg_parser.h"
 
 #include "format_raw.h"
 #include "ui_fileops.h"
@@ -927,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;
@@ -1086,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))
                {
@@ -1110,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
@@ -1280,11 +1287,6 @@ ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
 
        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;
 }
 
@@ -1458,6 +1460,7 @@ gchar *exif_item_get_data_as_text(ExifItem *item)
 gint exif_item_get_integer(ExifItem *item, gint *value)
 {
        if (!item) return FALSE;
+       if (!item->elements) return FALSE;
 
        switch (item->format)
                {
@@ -1516,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++;
        }
 
@@ -1582,13 +1585,13 @@ void exif_write_data_list(ExifData *exif, FILE *f, gint human_readable_list)
 
 gboolean exif_write(ExifData *exif)
 {
-       log_printf("Not compiled with EXIF write support");
+       log_printf("Not compiled with EXIF write support\n");
        return FALSE;
 }
 
 gboolean exif_write_sidecar(ExifData *exif, gchar *path)
 {
-       log_printf("Not compiled with EXIF write support");
+       log_printf("Not compiled with EXIF write support\n");
        return FALSE;
 }
 
@@ -1602,9 +1605,12 @@ GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format
 {
        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;
@@ -1615,11 +1621,11 @@ GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format
 
        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);
 }
 
@@ -1641,13 +1647,13 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
        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;
@@ -1678,7 +1684,7 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
                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;
                }
@@ -1691,7 +1697,7 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
 void exif_free_preview(guchar *buf)
 {
        GList *work = exif_unmap_list;
-       
+
        while (work)
                {
                UnmapData *ud = (UnmapData *)work->data;