Remove commented out code.
[geeqie.git] / src / exif.c
index 7ba5918..dff35ed 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Geeqie
  * (C) 2006 John Ellis
- *  Copyright (C) 2008 The Geeqie Team
+ *  Copyright (C) 2008 - 2012 The Geeqie Team
  *
  *  Authors:
  *    Support for Exif file format, originally written by Eric Swalens.
 #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 +512,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 +608,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 +928,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 +1087,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))
                {
@@ -1227,8 +1227,6 @@ ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
        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);
@@ -1283,11 +1281,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;
 }
 
@@ -1311,13 +1304,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;
@@ -1342,7 +1330,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;
@@ -1367,10 +1355,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;
 
@@ -1453,9 +1441,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)
                {
@@ -1514,7 +1513,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++;
        }
 
@@ -1529,7 +1528,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);
                }
@@ -1540,8 +1539,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)
                {
@@ -1555,7 +1554,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++;
                        }
@@ -1575,18 +1574,18 @@ 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");
 }
 
 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;
 }
 
@@ -1596,6 +1595,34 @@ gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
        return 0;
 }
 
+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;
+               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
 {
@@ -1679,6 +1706,9 @@ void exif_free_preview(guchar *buf)
        g_assert_not_reached();
 }
 
+void exif_init(void)
+{
+}
 
 #endif
 /* not HAVE_EXIV2 */