[gtk/matthiasc/color-profile-rebased: 15/46] tiff: Add color profile support




commit c8fce2c35db29b36bebdfc44b11c8cb7abf25321
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun May 8 10:25:39 2022 -0400

    tiff: Add color profile support
    
    Apply an embedded icc profile when loading tiff
    images, and save associated icc profile information
    when saving tiff images.

 gdk/loaders/gdktiff.c | 65 ++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 57 insertions(+), 8 deletions(-)
---
diff --git a/gdk/loaders/gdktiff.c b/gdk/loaders/gdktiff.c
index a605e3922a..55012c41e6 100644
--- a/gdk/loaders/gdktiff.c
+++ b/gdk/loaders/gdktiff.c
@@ -220,6 +220,44 @@ tiff_open_write (GBytes **result)
                          NULL, NULL);
 }
 
+/* }}} */
+/* {{{ Color profile handling */
+
+static GdkColorProfile *
+gdk_tiff_get_color_profile (TIFF *tiff)
+{
+  const char *icc_data;
+  guint icc_len;
+
+  if (TIFFGetField (tiff, TIFFTAG_ICCPROFILE, &icc_len, &icc_data))
+    {
+      GBytes *icc_bytes;
+      GdkColorProfile *profile;
+
+      icc_bytes = g_bytes_new (icc_data, icc_len);
+      profile = gdk_color_profile_new_from_icc_bytes (icc_bytes, NULL);
+      g_bytes_unref (icc_bytes);
+
+      if (profile)
+        return profile;
+    }
+
+  return g_object_ref (gdk_color_profile_get_srgb ());
+}
+
+static void
+gdk_tiff_set_color_profile (TIFF            *tiff,
+                            GdkColorProfile *profile)
+{
+  GBytes *bytes = gdk_color_profile_get_icc_profile (profile);
+
+  TIFFSetField (tiff, TIFFTAG_ICCPROFILE,
+                g_bytes_get_size (bytes),
+                g_bytes_get_data (bytes, NULL));
+
+  g_bytes_unref (bytes);
+}
+
 /* }}} */
 /* {{{ Public API */
 
@@ -266,6 +304,7 @@ gdk_save_tiff (GdkTexture *texture)
   GBytes *result = NULL;
   GdkMemoryTexture *memtex;
   GdkMemoryFormat format;
+  GdkColorProfile *color_profile;
   const FormatData *fdata = NULL;
 
   tif = tiff_open_write (&result);
@@ -273,6 +312,7 @@ gdk_save_tiff (GdkTexture *texture)
   width = gdk_texture_get_width (texture);
   height = gdk_texture_get_height (texture);
   format = gdk_texture_get_format (texture);
+  color_profile = gdk_texture_get_color_profile (texture);
   fdata = &format_data[format];
 
   if (fdata == NULL)
@@ -292,7 +332,9 @@ gdk_save_tiff (GdkTexture *texture)
   TIFFSetField (tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
   TIFFSetField (tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
 
-  memtex = gdk_memory_texture_from_texture (texture, fdata->format, gdk_color_profile_get_srgb ());
+  gdk_tiff_set_color_profile (tif, color_profile);
+
+  memtex = gdk_memory_texture_from_texture (texture, fdata->format, color_profile);
   data = gdk_memory_texture_get_data (memtex);
   stride = gdk_memory_texture_get_stride (memtex);
 
@@ -326,6 +368,7 @@ load_fallback (TIFF    *tif,
   int width, height;
   guchar *data;
   GBytes *bytes;
+  GdkColorProfile *profile;
   GdkTexture *texture;
 
   TIFFGetField (tif, TIFFTAG_IMAGEWIDTH, &width);
@@ -342,12 +385,15 @@ load_fallback (TIFF    *tif,
       return NULL;
     }
 
+  profile = gdk_tiff_get_color_profile (tif);
+
   bytes = g_bytes_new_take (data, width * height * 4);
 
-  texture = gdk_memory_texture_new (width, height,
-                                    GDK_MEMORY_R8G8B8A8_PREMULTIPLIED,
-                                    bytes,
-                                    width * 4);
+  texture = gdk_memory_texture_new_with_color_profile (width, height,
+                                                       GDK_MEMORY_R8G8B8A8_PREMULTIPLIED,
+                                                       profile,
+                                                       bytes,
+                                                       width * 4);
 
   g_bytes_unref (bytes);
 
@@ -372,6 +418,7 @@ gdk_load_tiff (GBytes  *input_bytes,
   gsize stride;
   int bpp;
   GBytes *bytes;
+  GdkColorProfile *profile;
   GdkTexture *texture;
   G_GNUC_UNUSED gint64 before = GDK_PROFILER_CURRENT_TIME;
 
@@ -471,12 +518,14 @@ gdk_load_tiff (GBytes  *input_bytes,
       line += stride;
     }
 
+  profile = gdk_tiff_get_color_profile (tif);
+
   bpp = gdk_memory_format_bytes_per_pixel (format);
   bytes = g_bytes_new_take (data, width * height * bpp);
 
-  texture = gdk_memory_texture_new (width, height,
-                                    format,
-                                    bytes, width * bpp);
+  texture = gdk_memory_texture_new_with_color_profile (width, height,
+                                                       format, profile,
+                                                       bytes, width * bpp);
   g_bytes_unref (bytes);
 
   TIFFClose (tif);


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