[gimp] libgimpcolor: add gimp_lcms_profile_open_from_file() and _from_data()



commit f322be2fac7c561760d5fe48549d463958db71ef
Author: Michael Natterer <mitch gimp org>
Date:   Sun Mar 16 00:06:31 2014 +0100

    libgimpcolor: add gimp_lcms_profile_open_from_file() and _from_data()
    
    which work just like their lcms counterparts but return an optional
    md5 digest of the ICC data and a GError in case opening fails.

 libgimpcolor/gimpcolor.def |    2 +
 libgimpcolor/gimplcms.c    |   94 ++++++++++++++++++++++++++++++++++++++++++++
 libgimpcolor/gimplcms.h    |   25 ++++++++---
 po-libgimp/POTFILES.in     |    2 +
 4 files changed, 116 insertions(+), 7 deletions(-)
---
diff --git a/libgimpcolor/gimpcolor.def b/libgimpcolor/gimpcolor.def
index 4e8ded4..1e414f1 100644
--- a/libgimpcolor/gimpcolor.def
+++ b/libgimpcolor/gimpcolor.def
@@ -45,6 +45,8 @@ EXPORTS
        gimp_lcms_profile_get_summary
        gimp_lcms_profile_is_cmyk
        gimp_lcms_profile_is_rgb
+       gimp_lcms_profile_open_from_data
+       gimp_lcms_profile_open_from_file
        gimp_param_rgb_get_type
        gimp_param_spec_rgb
        gimp_param_spec_rgb_has_alpha
diff --git a/libgimpcolor/gimplcms.c b/libgimpcolor/gimplcms.c
index 2094925..aedb787 100644
--- a/libgimpcolor/gimplcms.c
+++ b/libgimpcolor/gimplcms.c
@@ -35,6 +35,8 @@
 
 #include "gimplcms.h"
 
+#include "libgimp/libgimp-intl.h"
+
 
 /**
  * SECTION: gimplcms
@@ -44,6 +46,98 @@
  * Definitions and Functions relating to LCMS.
  **/
 
+static GQuark
+gimp_lcms_error_quark (void)
+{
+  static GQuark quark = 0;
+
+  if (G_UNLIKELY (quark == 0))
+    quark = g_quark_from_static_string ("gimp-lcms-error-quark");
+
+  return quark;
+}
+
+static void
+gimp_lcms_calculate_checksum (const guint8 *data,
+                              gsize         length,
+                              guint8       *md5_digest)
+{
+  GChecksum *md5 = g_checksum_new (G_CHECKSUM_MD5);
+
+  g_checksum_update (md5,
+                     (const guchar *) data + sizeof (cmsICCHeader),
+                     length - sizeof (cmsICCHeader));
+
+  length = GIMP_LCMS_MD5_DIGEST_LENGTH;
+  g_checksum_get_digest (md5, md5_digest, &length);
+  g_checksum_free (md5);
+}
+
+GimpColorProfile
+gimp_lcms_profile_open_from_file (const gchar  *filename,
+                                  guint8       *md5_digest,
+                                  GError      **error)
+{
+  GimpColorProfile  profile;
+  GMappedFile      *file;
+  const guint8     *data;
+  gsize             length;
+
+  g_return_val_if_fail (filename != NULL, NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  file = g_mapped_file_new (filename, FALSE, error);
+
+  if (! file)
+    return NULL;
+
+  data   = (const guint8 *) g_mapped_file_get_contents (file);
+  length = g_mapped_file_get_length (file);
+
+  profile = cmsOpenProfileFromMem (data, length);
+
+  if (! profile)
+    {
+      g_set_error (error, gimp_lcms_error_quark (), 0,
+                   _("'%s' does not appear to be an ICC color profile"),
+                   gimp_filename_to_utf8 (filename));
+    }
+  else if (md5_digest)
+    {
+      gimp_lcms_calculate_checksum (data, length, md5_digest);
+    }
+
+  g_mapped_file_unref (file);
+
+  return profile;
+}
+
+GimpColorProfile
+gimp_lcms_profile_open_from_data (const guint8  *data,
+                                  gsize          length,
+                                  guint8        *md5_digest,
+                                  GError       **error)
+{
+  GimpColorProfile  profile;
+
+  g_return_val_if_fail (data != NULL, NULL);
+  g_return_val_if_fail (length > 0, NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  profile = cmsOpenProfileFromMem (data, length);
+
+  if (! profile)
+    {
+      g_set_error_literal (error, gimp_lcms_error_quark (), 0,
+                           _("Data does not appear to be an ICC color profile"));
+    }
+  else if (md5_digest)
+    {
+      gimp_lcms_calculate_checksum (data, length, md5_digest);
+    }
+
+  return profile;
+}
 
 static gchar *
 gimp_lcms_profile_get_info (GimpColorProfile profile,
diff --git a/libgimpcolor/gimplcms.h b/libgimpcolor/gimplcms.h
index 60c7a97..c414f65 100644
--- a/libgimpcolor/gimplcms.h
+++ b/libgimpcolor/gimplcms.h
@@ -35,15 +35,26 @@ G_BEGIN_DECLS
 typedef gpointer GimpColorProfile;
 
 
-gchar            * gimp_lcms_profile_get_description  (GimpColorProfile profile);
-gchar            * gimp_lcms_profile_get_manufacturer (GimpColorProfile profile);
-gchar            * gimp_lcms_profile_get_model        (GimpColorProfile profile);
-gchar            * gimp_lcms_profile_get_copyright    (GimpColorProfile profile);
+#define GIMP_LCMS_MD5_DIGEST_LENGTH 16
 
-gchar            * gimp_lcms_profile_get_summary      (GimpColorProfile profile);
 
-gboolean           gimp_lcms_profile_is_rgb           (GimpColorProfile profile);
-gboolean           gimp_lcms_profile_is_cmyk          (GimpColorProfile profile);
+GimpColorProfile   gimp_lcms_profile_open_from_file   (const gchar       *filename,
+                                                       guint8            *md5_digest,
+                                                       GError           **error);
+GimpColorProfile   gimp_lcms_profile_open_from_data   (const guint8      *data,
+                                                       gsize              length,
+                                                       guint8            *md5_digest,
+                                                       GError           **error);
+
+gchar            * gimp_lcms_profile_get_description  (GimpColorProfile   profile);
+gchar            * gimp_lcms_profile_get_manufacturer (GimpColorProfile   profile);
+gchar            * gimp_lcms_profile_get_model        (GimpColorProfile   profile);
+gchar            * gimp_lcms_profile_get_copyright    (GimpColorProfile   profile);
+
+gchar            * gimp_lcms_profile_get_summary      (GimpColorProfile   profile);
+
+gboolean           gimp_lcms_profile_is_rgb           (GimpColorProfile   profile);
+gboolean           gimp_lcms_profile_is_cmyk          (GimpColorProfile   profile);
 
 GimpColorProfile   gimp_lcms_create_srgb_profile      (void);
 
diff --git a/po-libgimp/POTFILES.in b/po-libgimp/POTFILES.in
index 1e4a42c..c7d8888 100644
--- a/po-libgimp/POTFILES.in
+++ b/po-libgimp/POTFILES.in
@@ -26,6 +26,8 @@ libgimpbase/gimpmemsize.c
 libgimpbase/gimpmetadata.c
 libgimpbase/gimputils.c
 
+libgimpcolor/gimplcms.c
+
 libgimpconfig/gimpcolorconfig.c
 libgimpconfig/gimpcolorconfig-enums.c
 libgimpconfig/gimpconfig-deserialize.c


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