[gimp] plug-ins: Read comment and Exif data from EXR files



commit e81cef7c03acbd6a5ea8f41e6066eb49c0ecdda6
Author: Tobias Ellinghaus <me houz org>
Date:   Fri Apr 22 14:41:34 2016 +0200

    plug-ins: Read comment and Exif data from EXR files

 plug-ins/file-exr/exr-attribute-blob.h |   94 ++++++++++++++++++++++++++++++++
 plug-ins/file-exr/file-exr.c           |   82 +++++++++++++++++++++++++---
 plug-ins/file-exr/openexr-wrapper.cc   |   46 ++++++++++++++++
 plug-ins/file-exr/openexr-wrapper.h    |    7 +++
 4 files changed, 220 insertions(+), 9 deletions(-)
---
diff --git a/plug-ins/file-exr/exr-attribute-blob.h b/plug-ins/file-exr/exr-attribute-blob.h
new file mode 100644
index 0000000..c6a90a3
--- /dev/null
+++ b/plug-ins/file-exr/exr-attribute-blob.h
@@ -0,0 +1,94 @@
+/*
+    This file is part of GIMP,
+    copyright (c) 2012 johannes hanika
+
+    GIMP 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 3 of the License, or
+    (at your option) any later version.
+
+    GIMP 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 GIMP.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#pragma once
+
+#include <ciso646>
+#include <inttypes.h>
+
+#if defined(_LIBCPP_VERSION)
+#include <memory>
+#else
+#include <tr1/memory>
+#endif
+
+#include <OpenEXR/ImfFrameBuffer.h>
+#include <OpenEXR/ImfTestFile.h>
+#include <OpenEXR/ImfInputFile.h>
+#include <OpenEXR/ImfTiledInputFile.h>
+#include <OpenEXR/ImfChannelList.h>
+#include <OpenEXR/ImfStandardAttributes.h>
+
+#ifdef OPENEXR_IMF_INTERNAL_NAMESPACE
+#define IMF_NS OPENEXR_IMF_INTERNAL_NAMESPACE
+#else
+#define IMF_NS Imf
+#endif
+
+// this stores our exif data as a blob.
+
+template <typename T> struct array_deleter
+{
+  void operator()(T const *p)
+  {
+    delete[] p;
+  }
+};
+
+namespace IMF_NS
+{
+class Blob
+{
+public:
+  Blob() : size(0), data((uint8_t *)NULL)
+  {
+  }
+
+  Blob(uint32_t _size, uint8_t *_data) : size(_size)
+  {
+    uint8_t *tmp_ptr = new uint8_t[_size];
+    memcpy(tmp_ptr, _data, _size);
+    data.reset(tmp_ptr, array_deleter<uint8_t>());
+  }
+
+  uint32_t size;
+#if defined(_LIBCPP_VERSION)
+  std::shared_ptr<uint8_t> data;
+#else
+  std::tr1::shared_ptr<uint8_t> data;
+#endif
+};
+
+
+typedef IMF_NS::TypedAttribute<IMF_NS::Blob> BlobAttribute;
+template <> const char *BlobAttribute::staticTypeName()
+{
+  return "blob";
+}
+template <> void BlobAttribute::writeValueTo(OStream &os, int version) const
+{
+  Xdr::write<StreamIO>(os, _value.size);
+  Xdr::write<StreamIO>(os, (char *)(_value.data.get()), _value.size);
+}
+
+template <> void BlobAttribute::readValueFrom(IStream &is, int size, int version)
+{
+  Xdr::read<StreamIO>(is, _value.size);
+  _value.data.reset(new uint8_t[_value.size], array_deleter<uint8_t>());
+  Xdr::read<StreamIO>(is, (char *)(_value.data.get()), _value.size);
+}
+}
diff --git a/plug-ins/file-exr/file-exr.c b/plug-ins/file-exr/file-exr.c
index 3629534..71af9c6 100644
--- a/plug-ins/file-exr/file-exr.c
+++ b/plug-ins/file-exr/file-exr.c
@@ -32,17 +32,18 @@
 /*
  * Declare some local functions.
  */
-static void     query      (void);
-static void     run        (const gchar      *name,
-                            gint              nparams,
-                            const GimpParam  *param,
-                            gint             *nreturn_vals,
-                            GimpParam       **return_vals);
+static void     query                 (void);
+static void     run                   (const gchar      *name,
+                                       gint              nparams,
+                                       const GimpParam  *param,
+                                       gint             *nreturn_vals,
+                                       GimpParam       **return_vals);
 
-static gint32   load_image (const gchar      *filename,
-                            gboolean          interactive,
-                            GError          **error);
+static gint32   load_image            (const gchar      *filename,
+                                       gboolean          interactive,
+                                       GError          **error);
 
+static void exr_load_sanitize_comment (gchar *comment);
 
 /*
  * Some global variables.
@@ -169,6 +170,10 @@ load_image (const gchar  *filename,
   gchar            *pixels = NULL;
   gint              begin;
   gint32            success = FALSE;
+  gchar            *comment;
+  guchar           *exif_data;
+  guint             exif_size;
+
 
   gimp_progress_init_printf (_("Opening '%s'"),
                              gimp_filename_to_utf8 (filename));
@@ -309,6 +314,49 @@ load_image (const gchar  *filename,
         }
     }
 
+  /* try to read the file comment */
+  comment = exr_loader_get_comment (loader);
+  if (comment)
+    {
+      GimpParasite *parasite;
+
+      exr_load_sanitize_comment (comment);
+      parasite = gimp_parasite_new ("gimp-comment",
+                                    GIMP_PARASITE_PERSISTENT,
+                                    strlen (comment) + 1,
+                                    comment);
+      gimp_image_attach_parasite (image, parasite);
+      gimp_parasite_free (parasite);
+
+      g_free (comment);
+    }
+
+  /* check if the image contains Exif data and read it */
+  exif_data = exr_loader_get_exif (loader, &exif_size);
+  if (exif_data)
+    {
+      GimpMetadata *metadata = gimp_image_get_metadata (image);
+
+      if (metadata)
+        g_object_ref (metadata);
+      else
+        metadata = gimp_metadata_new ();
+
+      if (gimp_metadata_set_from_exif (metadata,
+                                       exif_data,
+                                       exif_size,
+                                       NULL))
+        {
+          gimp_image_set_metadata (image, metadata);
+        }
+
+      g_object_unref (metadata);
+      g_free (exif_data);
+    }
+
+  // TODO: also read XMP data
+
+
   gimp_progress_update (1.0);
 
   success = TRUE;
@@ -331,3 +379,19 @@ load_image (const gchar  *filename,
 
   return -1;
 }
+
+/* copy & pasted from file-jpeg/jpeg-load.c */
+static void
+exr_load_sanitize_comment (gchar *comment)
+{
+  if (! g_utf8_validate (comment, -1, NULL))
+  {
+    gchar *c;
+
+    for (c = comment; *c; c++)
+    {
+      if (*c > 126 || (*c < 32 && *c != '\t' && *c != '\n' && *c != '\r'))
+        *c = '?';
+    }
+  }
+}
diff --git a/plug-ins/file-exr/openexr-wrapper.cc b/plug-ins/file-exr/openexr-wrapper.cc
index b7f46d0..8a0c9cb 100644
--- a/plug-ins/file-exr/openexr-wrapper.cc
+++ b/plug-ins/file-exr/openexr-wrapper.cc
@@ -18,6 +18,8 @@
 
 #include <string>
 
+#include "exr-attribute-blob.h"
+
 using namespace Imf;
 using namespace Imf::RgbaYca;
 using namespace Imath;
@@ -240,6 +242,36 @@ struct _EXRLoader
     return profile;
   }
 
+  gchar *getComment() const {
+    char *result = NULL;
+    const Imf::StringAttribute *comment = file_.header().findTypedAttribute<Imf::StringAttribute>("comment");
+    if (comment)
+      result = g_strdup (comment->value().c_str());
+    return result;
+  }
+
+  guchar *getExif(guint *size) const {
+    guchar jpeg_exif[] = "Exif\0\0";
+    guchar *exif_data = NULL;
+    *size = 0;
+
+    const Imf::BlobAttribute *exif = file_.header().findTypedAttribute<Imf::BlobAttribute>("exif");
+
+    if (exif)
+      {
+        exif_data = (guchar *)(exif->value().data.get());
+        *size = exif->value().size;
+        // darktable appends a jpg-compatible exif00 string, so get rid of that again:
+        if ( ! memcmp (jpeg_exif, exif_data, sizeof(jpeg_exif)))
+          {
+            *size -= 6;
+            exif_data += 6;
+          }
+      }
+
+    return (guchar *)g_memdup (exif_data, *size);
+  }
+
   size_t refcount_;
   InputFile file_;
   const Box2i data_window_;
@@ -259,6 +291,7 @@ exr_loader_new (const char *filename)
   // Don't let any exceptions propagate to the C layer.
   try
     {
+      Imf::BlobAttribute::registerAttributeType();
       file = new EXRLoader(filename);
     }
   catch (...)
@@ -346,6 +379,19 @@ exr_loader_get_icc_profile (EXRLoader *loader)
   return loader->getICCProfile ();
 }
 
+gchar *
+exr_loader_get_comment (EXRLoader *loader)
+{
+  return loader->getComment ();
+}
+
+guchar *
+exr_loader_get_exif (EXRLoader *loader,
+                     guint *size)
+{
+  return loader->getExif (size);
+}
+
 int
 exr_loader_read_pixel_row (EXRLoader *loader,
                            char *pixels,
diff --git a/plug-ins/file-exr/openexr-wrapper.h b/plug-ins/file-exr/openexr-wrapper.h
index a89d3a5..c1d348a 100644
--- a/plug-ins/file-exr/openexr-wrapper.h
+++ b/plug-ins/file-exr/openexr-wrapper.h
@@ -53,6 +53,13 @@ exr_loader_has_alpha (EXRLoader *loader);
 cmsHPROFILE
 exr_loader_get_icc_profile (EXRLoader *loader);
 
+gchar *
+exr_loader_get_comment (EXRLoader *loader);
+
+guchar *
+exr_loader_get_exif (EXRLoader *loader,
+                     guint *size);
+
 int
 exr_loader_read_pixel_row (EXRLoader *loader,
                            char *pixels,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]