[gtk/matthiasc/color-profile-rebased: 17/47] png: Handle color spaces




commit 4195a26ba469a7fcf63958c2dfc637d23142593b
Author: Benjamin Otte <otte redhat com>
Date:   Sun May 8 10:12:59 2022 -0400

    png: Handle color spaces

 gdk/loaders/gdkpng.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 124 insertions(+), 3 deletions(-)
---
diff --git a/gdk/loaders/gdkpng.c b/gdk/loaders/gdkpng.c
index 01ca9fa0d6..aef47e9dd2 100644
--- a/gdk/loaders/gdkpng.c
+++ b/gdk/loaders/gdkpng.c
@@ -19,6 +19,8 @@
 
 #include "gdkpngprivate.h"
 
+#include "gdkcolorspaceprivate.h"
+#include "gdklcmscolorspaceprivate.h"
 #include "gdkintl.h"
 #include "gdkmemoryformatprivate.h"
 #include "gdkmemorytextureprivate.h"
@@ -26,6 +28,8 @@
 #include "gdktexture.h"
 #include "gdktextureprivate.h"
 #include "gsk/gl/fp16private.h"
+
+#include <lcms2.h>
 #include <png.h>
 #include <stdio.h>
 
@@ -127,8 +131,104 @@ png_simple_warning_callback (png_structp     png,
 {
 }
 
-/* }}} */
-/* {{{ Public API */
+static GdkColorSpace *
+gdk_png_get_color_space (png_struct *png,
+                         png_info   *info)
+{
+  GdkColorSpace *color_space;
+  guchar *icc_data;
+  png_uint_32 icc_len;
+  char *name;
+  double gamma;
+  cmsCIExyY whitepoint;
+  cmsCIExyYTRIPLE primaries;
+  cmsToneCurve *curve;
+  cmsHPROFILE lcms_profile;
+  int intent;
+
+  if (png_get_iCCP (png, info, &name, NULL, &icc_data, &icc_len))
+    {
+      GBytes *bytes = g_bytes_new (icc_data, icc_len);
+
+      color_space = gdk_color_space_new_from_icc_profile (bytes, NULL);
+      g_bytes_unref (bytes);
+      if (color_space)
+        return color_space;
+    }
+
+  if (png_get_sRGB (png, info, &intent))
+    return g_object_ref (gdk_color_space_get_srgb ());
+
+  /* If neither of those is valid, the result is sRGB */
+  if (!png_get_valid (png, info, PNG_INFO_gAMA) &&
+      !png_get_valid (png, info, PNG_INFO_cHRM))
+    return g_object_ref (gdk_color_space_get_srgb ());
+
+  if (!png_get_gAMA (png, info, &gamma))
+    gamma = 2.4;
+
+  if (!png_get_cHRM (png, info,
+                     &whitepoint.x, &whitepoint.y,
+                     &primaries.Red.x, &primaries.Red.y,
+                     &primaries.Green.x, &primaries.Green.y,
+                     &primaries.Blue.x, &primaries.Blue.y))
+    {
+      if (gamma == 2.4)
+        return g_object_ref (gdk_color_space_get_srgb ());
+
+      whitepoint = (cmsCIExyY) { 0.3127, 0.3290, 1.0 };
+      primaries = (cmsCIExyYTRIPLE) {
+                    { 0.6400, 0.3300, 1.0 },
+                    { 0.3000, 0.6000, 1.0 },
+                    { 0.1500, 0.0600, 1.0 }
+                  };
+    }
+  else
+    {
+      primaries.Red.Y = 1.0;
+      primaries.Green.Y = 1.0;
+      primaries.Blue.Y = 1.0;
+    }
+
+  curve = cmsBuildGamma (NULL, 1.0 / gamma);
+  lcms_profile = cmsCreateRGBProfile (&whitepoint,
+                                      &primaries,
+                                      (cmsToneCurve*[3]) { curve, curve, curve });
+  color_space = gdk_lcms_color_space_new_from_lcms_profile (lcms_profile);
+  /* FIXME: errors? */
+  if (color_space == NULL)
+    color_space = g_object_ref (gdk_color_space_get_srgb ());
+  cmsFreeToneCurve (curve);
+
+  return color_space;
+}
+
+static void
+gdk_png_set_color_space (png_struct    *png,
+                         png_info      *info,
+                         GdkColorSpace *color_space)
+{
+  /* FIXME: allow deconstructing RGB profiles into gAMA and cHRM
+   * instead of falling back to iCCP
+   */
+  if (color_space == gdk_color_space_get_srgb ())
+    {
+      png_set_sRGB_gAMA_and_cHRM (png, info, /* FIXME */ PNG_sRGB_INTENT_PERCEPTUAL);
+    }
+  else
+    {
+      GBytes *bytes = gdk_color_space_save_to_icc_profile (color_space, NULL);
+      png_set_iCCP (png, info,
+                    "ICC profile",
+                    0,
+                    g_bytes_get_data (bytes, NULL),
+                    g_bytes_get_size (bytes));
+      g_bytes_unref (bytes);
+    }
+}
+
+ /* }}} */
+ /* {{{ Public API */
 
 GdkTexture *
 gdk_load_png (GBytes  *bytes,
@@ -144,6 +244,7 @@ gdk_load_png (GBytes  *bytes,
   guchar *buffer = NULL;
   guchar **row_pointers = NULL;
   GBytes *out_bytes;
+  GdkColorSpace *color_space;
   GdkTexture *texture;
   int bpp;
   G_GNUC_UNUSED gint64 before = GDK_PROFILER_CURRENT_TIME;
@@ -247,6 +348,8 @@ gdk_load_png (GBytes  *bytes,
       return NULL;
     }
 
+  color_space = gdk_png_get_color_space (png, info);
+
   bpp = gdk_memory_format_bytes_per_pixel (format);
   stride = width * bpp;
   if (stride % 8)
@@ -257,6 +360,7 @@ gdk_load_png (GBytes  *bytes,
 
   if (!buffer || !row_pointers)
     {
+      g_object_unref (color_space);
       g_free (buffer);
       g_free (row_pointers);
       png_destroy_read_struct (&png, &info, NULL);
@@ -273,8 +377,11 @@ gdk_load_png (GBytes  *bytes,
   png_read_end (png, info);
 
   out_bytes = g_bytes_new_take (buffer, height * stride);
-  texture = gdk_memory_texture_new (width, height, format, out_bytes, stride);
+  texture = gdk_memory_texture_new_with_color_space (width, height,
+                                                     format, color_space,
+                                                     out_bytes, stride);
   g_bytes_unref (out_bytes);
+  g_object_unref (color_space);
 
   g_free (row_pointers);
   png_destroy_read_struct (&png, &info, NULL);
@@ -301,13 +408,17 @@ gdk_save_png (GdkTexture *texture)
   int y;
   GdkMemoryTexture *memtex;
   GdkMemoryFormat format;
+  GdkColorSpace *color_space;
   int png_format;
   int depth;
 
   width = gdk_texture_get_width (texture);
   height = gdk_texture_get_height (texture);
+  color_space = gdk_texture_get_color_space (texture);
   format = gdk_texture_get_format (texture);
 
+  memtex = gdk_memory_texture_from_texture (texture, format, color_space);
+
   switch (format)
     {
     case GDK_MEMORY_B8G8R8A8_PREMULTIPLIED:
@@ -317,14 +428,22 @@ gdk_save_png (GdkTexture *texture)
     case GDK_MEMORY_A8R8G8B8:
     case GDK_MEMORY_R8G8B8A8:
     case GDK_MEMORY_A8B8G8R8:
+#if G_BYTE_ORDER == G_LITTLE_ENDIAN
       format = GDK_MEMORY_R8G8B8A8;
+#elif G_BYTE_ORDER == G_BIG_ENDIAN
+      format = GDK_MEMORY_A8B8G8R8;
+#endif
       png_format = PNG_COLOR_TYPE_RGB_ALPHA;
       depth = 8;
       break;
 
     case GDK_MEMORY_R8G8B8:
     case GDK_MEMORY_B8G8R8:
+#if G_BYTE_ORDER == G_LITTLE_ENDIAN
       format = GDK_MEMORY_R8G8B8;
+#elif G_BYTE_ORDER == G_BIG_ENDIAN
+      format = GDK_MEMORY_B8G8R8;
+#endif
       png_format = PNG_COLOR_TYPE_RGB;
       depth = 8;
       break;
@@ -387,6 +506,8 @@ gdk_save_png (GdkTexture *texture)
                 PNG_COMPRESSION_TYPE_DEFAULT,
                 PNG_FILTER_TYPE_DEFAULT);
 
+  gdk_png_set_color_space (png, info, color_space);
+
   png_write_info (png, info);
 
 #if G_BYTE_ORDER == G_LITTLE_ENDIAN


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