[gtk/wip/otte/colorspace: 35/50] gdk: Add GdkLcmsColorSpace




commit d95353c1bc5c7c717bfc77930a0dbec0dbe66a18
Author: Benjamin Otte <otte redhat com>
Date:   Mon Sep 20 09:17:28 2021 +0200

    gdk: Add GdkLcmsColorSpace
    
    This is a colorspace implementation using LCMS to implement support
    for random colorspaces from ICC profiles.

 gdk/gdkcolorprofileprivate.h   |  15 +++++
 gdk/gdklcmscolorspace.c        | 148 +++++++++++++++++++++++++++++++++++++++++
 gdk/gdklcmscolorspaceprivate.h |  47 +++++++++++++
 gdk/meson.build                |   1 +
 4 files changed, 211 insertions(+)
---
diff --git a/gdk/gdkcolorprofileprivate.h b/gdk/gdkcolorprofileprivate.h
new file mode 100644
index 0000000000..39fd3dd81e
--- /dev/null
+++ b/gdk/gdkcolorprofileprivate.h
@@ -0,0 +1,15 @@
+#ifndef __GDK_COLOR_PROFILE_PRIVATE_H__
+#define __GDK_COLOR_PROFILE_PRIVATE_H__
+
+#include "gdkcolorprofile.h"
+
+#include <lcms2.h>
+
+G_BEGIN_DECLS
+
+
+cmsHPROFILE *                gdk_color_profile_get_lcms_profile           (GdkColorProfile      *self);
+
+G_END_DECLS
+
+#endif /* __GDK_COLOR_PROFILE_PRIVATE_H__ */
diff --git a/gdk/gdklcmscolorspace.c b/gdk/gdklcmscolorspace.c
new file mode 100644
index 0000000000..e91eb9eeed
--- /dev/null
+++ b/gdk/gdklcmscolorspace.c
@@ -0,0 +1,148 @@
+/* gdklcmscolorspace.c
+ *
+ * Copyright 2021 (c) Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "gdklcmscolorspaceprivate.h"
+
+#include "gdkintl.h"
+
+struct _GdkLcmsColorSpace
+{
+  GdkColorSpace parent_instance;
+
+  cmsHPROFILE lcms_profile;
+};
+
+struct _GdkLcmsColorSpaceClass
+{
+  GdkColorSpaceClass parent_class;
+};
+
+G_DEFINE_TYPE (GdkLcmsColorSpace, gdk_lcms_color_space, GDK_TYPE_COLOR_SPACE)
+
+static gboolean
+gdk_lcms_color_space_supports_format (GdkColorSpace   *space,
+                                      GdkMemoryFormat  format)
+{
+  GdkLcmsColorSpace *self = GDK_LCMS_COLOR_SPACE (space);
+
+  return cmsGetColorSpace (self->lcms_profile) == cmsSigRgbData;
+}
+
+static GBytes *
+gdk_lcms_color_space_save_to_icc_profile (GdkColorSpace  *space,
+                                          GError        **error)
+{
+  GdkLcmsColorSpace *self = GDK_LCMS_COLOR_SPACE (space);
+  cmsUInt32Number size;
+  guchar *data;
+
+  size = 0;
+  if (!cmsSaveProfileToMem (self->lcms_profile, NULL, &size))
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Could not prepare ICC profile"));
+      return NULL;
+    }
+
+  data = g_malloc (size);
+  if (!cmsSaveProfileToMem (self->lcms_profile, data, &size))
+    {
+      g_free (data);
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Failed to save ICC profile"));
+      return NULL;
+    }
+
+  return g_bytes_new_take (data, size);
+}
+
+static void
+gdk_lcms_color_space_dispose (GObject *object)
+{
+  GdkLcmsColorSpace *self = GDK_LCMS_COLOR_SPACE (object);
+
+  g_clear_pointer (&self->lcms_profile, cmsCloseProfile);
+
+  G_OBJECT_CLASS (gdk_lcms_color_space_parent_class)->dispose (object);
+}
+
+static void
+gdk_lcms_color_space_class_init (GdkLcmsColorSpaceClass *klass)
+{
+  GdkColorSpaceClass *color_space_class = GDK_COLOR_SPACE_CLASS (klass);
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  color_space_class->supports_format = gdk_lcms_color_space_supports_format;
+  color_space_class->save_to_icc_profile = gdk_lcms_color_space_save_to_icc_profile;
+
+  gobject_class->dispose = gdk_lcms_color_space_dispose;
+}
+
+static void
+gdk_lcms_color_space_init (GdkLcmsColorSpace *self)
+{
+}
+
+GdkColorSpace *
+gdk_lcms_color_space_new_from_lcms_profile (cmsHPROFILE lcms_profile)
+{
+  GdkLcmsColorSpace *result;
+
+  result = g_object_new (GDK_TYPE_LCMS_COLOR_SPACE, NULL);
+  result->lcms_profile = lcms_profile;
+
+  return GDK_COLOR_SPACE (result);
+}
+
+/**
+ * gdk_color_space_new_from_icc_profile:
+ * @icc_profile: The ICC profiles given as a `GBytes`
+ * @error: Return location for an error
+ *
+ * Creates a new color profile for the given ICC profile data.
+ *
+ * if the profile is not valid, %NULL is returned and an error
+ * is raised.
+ *
+ * Returns: a new `GdkLcmsColorSpace` or %NULL on error
+ *
+ * Since: 4.6
+ */
+GdkColorSpace *
+gdk_color_space_new_from_icc_profile (GBytes  *icc_profile,
+                                      GError **error)
+{
+  cmsHPROFILE lcms_profile;
+  const guchar *data;
+  gsize size;
+
+  g_return_val_if_fail (icc_profile != NULL, NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  data = g_bytes_get_data (icc_profile, &size);
+
+  lcms_profile = cmsOpenProfileFromMem (data, size);
+  if (lcms_profile == NULL)
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Failed to load ICC profile"));
+      return NULL;
+    }
+
+  return gdk_lcms_color_space_new_from_lcms_profile (lcms_profile);
+}
+
diff --git a/gdk/gdklcmscolorspaceprivate.h b/gdk/gdklcmscolorspaceprivate.h
new file mode 100644
index 0000000000..92f48c5e72
--- /dev/null
+++ b/gdk/gdklcmscolorspaceprivate.h
@@ -0,0 +1,47 @@
+/* gdklcmscolorspace.h
+ *
+ * Copyright 2021 (c) Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GDK_LCMS_COLOR_SPACE_PRIVATE_H__
+#define __GDK_LCMS_COLOR_SPACE_PRIVATE_H__
+
+#include "gdkcolorspaceprivate.h"
+
+#include <lcms2.h>
+
+G_BEGIN_DECLS
+
+#define GDK_TYPE_LCMS_COLOR_SPACE (gdk_lcms_color_space_get_type ())
+
+#define GDK_LCMS_COLOR_SPACE(obj)               (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
GDK_TYPE_LCMS_COLOR_SPACE, GdkLcmsColorSpace))
+#define GDK_IS_LCMS_COLOR_SPACE(obj)            (G_TYPE_CHECK_INSTANCE_TYPE ((obj), 
GDK_TYPE_LCMS_COLOR_SPACE))
+#define GDK_LCMS_COLOR_SPACE_CLASS(klass)       (G_TYPE_CHECK_CLASS_CAST ((klass), 
GDK_TYPE_LCMS_COLOR_SPACE, GdkLcmsColorSpaceClass))
+#define GDK_IS_LCMS_COLOR_SPACE_CLASS(klass)    (G_TYPE_CHECK_CLASS_TYPE ((klass), 
GDK_TYPE_LCMS_COLOR_SPACE))
+#define GDK_LCMS_COLOR_SPACE_GET_CLASS(obj)     (G_TYPE_INSTANCE_GET_CLASS ((obj), 
GDK_TYPE_LCMS_COLOR_SPACE, GdkLcmsColorSpaceClass))
+
+typedef struct _GdkLcmsColorSpace             GdkLcmsColorSpace;
+typedef struct _GdkLcmsColorSpaceClass        GdkLcmsColorSpaceClass;
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(GdkLcmsColorSpace, g_object_unref)
+
+GType                   gdk_lcms_color_space_get_type                   (void) G_GNUC_CONST;
+
+GdkColorSpace *         gdk_lcms_color_space_new_from_lcms_profile      (cmsHPROFILE             
lcms_profile);
+
+G_END_DECLS
+
+#endif /* __GDK_LCMS_COLOR_SPACE_PRIVATE_H__ */
diff --git a/gdk/meson.build b/gdk/meson.build
index 98f481ff57..d6384baee5 100644
--- a/gdk/meson.build
+++ b/gdk/meson.build
@@ -31,6 +31,7 @@ gdk_public_sources = files([
   'gdkhsla.c',
   'gdkkeys.c',
   'gdkkeyuni.c',
+  'gdklcmscolorspace.c',
   'gdkmemoryformat.c',
   'gdkmemorytexture.c',
   'gdkmonitor.c',


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