Silent 2 warnings using explicit type casting.
[geeqie.git] / src / exif-common.c
index 49b31f2..12956de 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Geeqie
  * (C) 2006 John Ellis
- * Copyright (C) 2008 - 2010 The Geeqie Team
+ * Copyright (C) 2008 - 2012 The Geeqie Team
  *
 */
 
 #ifdef HAVE_LCMS
 /*** color support enabled ***/
 
-#ifdef HAVE_LCMS_LCMS_H
-  #include <lcms/lcms.h>
+#ifdef HAVE_LCMS2
+#include <lcms2.h>
 #else
-  #include <lcms.h>
+#include <lcms.h>
 #endif
 #endif
 
@@ -43,6 +43,7 @@
 #include "format_raw.h"
 #include "ui_fileops.h"
 #include "cache.h"
+#include "jpeg_parser.h"
 
 
 static gdouble exif_rational_to_double(ExifRational *r, gint sign)
@@ -124,13 +125,13 @@ static gdouble get_crop_factor(ExifData *exif)
 static gboolean remove_suffix(gchar *str, const gchar *suffix, gint suffix_len)
 {
        gint str_len = strlen(str);
-       
+
        if (suffix_len < 0) suffix_len = strlen(suffix);
        if (str_len < suffix_len) return FALSE;
-       
+
        if (strcmp(str + str_len - suffix_len, suffix) != 0) return FALSE;
        str[str_len - suffix_len] = '\0';
-       
+
        return TRUE;
 }
 
@@ -160,7 +161,7 @@ static gchar *exif_build_formatted_Camera(ExifData *exif)
                gint i, j;
 
                g_strstrip(software);
-               
+
                /* remove superfluous spaces (pentax K100D) */
                for (i = 0, j = 0; software[i]; i++, j++)
                        {
@@ -412,6 +413,9 @@ static gchar *exif_build_formatted_Resolution(ExifData *exif)
 
 static gchar *exif_build_formatted_ColorProfile(ExifData *exif)
 {
+#ifdef HAVE_LCMS2
+       cmsUInt8Number profileID[17];
+#endif
        const gchar *name = "";
        const gchar *source = "";
        guchar *profile_data;
@@ -451,7 +455,13 @@ static gchar *exif_build_formatted_ColorProfile(ExifData *exif)
                        profile = cmsOpenProfileFromMem(profile_data, profile_len);
                        if (profile)
                                {
-                               name = cmsTakeProductName(profile);
+#ifdef HAVE_LCMS2
+                               profileID[16] = '\0';
+                               cmsGetHeaderProfileID(profile, profileID);
+                               name = (gchar *) profileID;
+#else
+                               name = (gchar *) cmsTakeProductName(profile);
+#endif
                                cmsCloseProfile(profile);
                                }
                        g_free(profile_data);
@@ -636,16 +646,23 @@ void exif_release_cb(FileData *fd)
        fd->exif = NULL;
 }
 
+void exif_init_cache(void)
+{
+       g_assert(!exif_cache);
+       exif_cache = file_cache_new(exif_release_cb, 4);
+}
+
 ExifData *exif_read_fd(FileData *fd)
 {
        gchar *sidecar_path;
 
-       if (!fd || !is_readable_file(fd->path)) return NULL;
-       
-       if (!exif_cache) exif_cache = file_cache_new(exif_release_cb, 4);
-       
+       if (!exif_cache) exif_init_cache();
+
+       if (!fd) return NULL;
+
        if (file_cache_get(exif_cache, fd)) return fd->exif;
-       
+       g_assert(fd->exif == NULL);
+
        /* CACHE_TYPE_XMP_METADATA file should exist only if the metadata are
         * not writable directly, thus it should contain the most up-to-date version */
        sidecar_path = NULL;
@@ -660,6 +677,7 @@ ExifData *exif_read_fd(FileData *fd)
        fd->exif = exif_read(fd->path, sidecar_path, fd->modified_xmp);
 
        g_free(sidecar_path);
+       file_cache_put(exif_cache, fd, 1);
        return fd->exif;
 }
 
@@ -668,70 +686,10 @@ void exif_free_fd(FileData *fd, ExifData *exif)
 {
        if (!fd) return;
        g_assert(fd->exif == exif);
-       
-       file_cache_put(exif_cache, fd, 1);
 }
 
 /* embedded icc in jpeg */
 
-
-#define JPEG_MARKER            0xFF
-#define JPEG_MARKER_SOI                0xD8
-#define JPEG_MARKER_EOI                0xD9
-#define JPEG_MARKER_APP1       0xE1
-#define JPEG_MARKER_APP2       0xE2
-
-/* jpeg container format:
-     all data markers start with 0XFF
-     2 byte long file start and end markers: 0xFFD8(SOI) and 0XFFD9(EOI)
-     4 byte long data segment markers in format: 0xFFTTSSSSNNN...
-       FF:   1 byte standard marker identifier
-       TT:   1 byte data type
-       SSSS: 2 bytes in Motorola byte alignment for length of the data.
-            This value includes these 2 bytes in the count, making actual
-            length of NN... == SSSS - 2.
-       NNN.: the data in this segment
- */
-
-gboolean exif_jpeg_segment_find(guchar *data, guint size,
-                           guchar app_marker, const gchar *magic, guint magic_len,
-                           guint *seg_offset, guint *seg_length)
-{
-       guchar marker = 0;
-       guint offset = 0;
-       guint length = 0;
-
-       while (marker != app_marker &&
-              marker != JPEG_MARKER_EOI)
-               {
-               offset += length;
-               length = 2;
-
-               if (offset + 2 >= size ||
-                   data[offset] != JPEG_MARKER) return FALSE;
-
-               marker = data[offset + 1];
-               if (marker != JPEG_MARKER_SOI &&
-                   marker != JPEG_MARKER_EOI)
-                       {
-                       if (offset + 4 >= size) return FALSE;
-                       length += ((guint)data[offset + 2] << 8) + data[offset + 3];
-                       }
-               }
-
-       if (marker == app_marker &&
-           offset + length < size &&
-           length >= 4 + magic_len &&
-           memcmp(data + offset + 4, magic, magic_len) == 0)
-               {
-               *seg_offset = offset + 4;
-               *seg_length = length - 4;
-               return TRUE;
-               }
-
-       return FALSE;
-}
-
 gboolean exif_jpeg_parse_color(ExifData *exif, guchar *data, guint size)
 {
        guint seg_offset = 0;
@@ -746,7 +704,7 @@ gboolean exif_jpeg_parse_color(ExifData *exif, guchar *data, guint size)
           TT = total number of ICC segments (TT in each ICC segment should match)
         */
 
-       while (exif_jpeg_segment_find(data + seg_offset + seg_length,
+       while (jpeg_segment_find(data + seg_offset + seg_length,
                                      size - seg_offset - seg_length,
                                      JPEG_MARKER_APP2,
                                      "ICC_PROFILE\x00", 12,