[gtk/matthiasc/color-profile-rebased: 27/66] jpeg: Add color space support




commit db3a0cf08bca2a1e0e3967a0ab5579384742da9f
Author: Benjamin Otte <otte redhat com>
Date:   Thu Sep 23 03:27:22 2021 +0200

    jpeg: Add color space support

 gdk/loaders/gdkjpeg.c | 44 +++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 39 insertions(+), 5 deletions(-)
---
diff --git a/gdk/loaders/gdkjpeg.c b/gdk/loaders/gdkjpeg.c
index 8f7e422f0c..9ed883479a 100644
--- a/gdk/loaders/gdkjpeg.c
+++ b/gdk/loaders/gdkjpeg.c
@@ -21,9 +21,10 @@
 
 #include "gdkjpegprivate.h"
 
+#include "gdkcolorspace.h"
 #include "gdkintl.h"
-#include "gdktexture.h"
 #include "gdkmemorytextureprivate.h"
+#include "gdktexture.h"
 
 #include "gdkprofilerprivate.h"
 
@@ -143,9 +144,12 @@ gdk_load_jpeg (GBytes  *input_bytes,
   guint width, height, stride;
   unsigned char *data;
   unsigned char *row[1];
+  JOCTET *icc_data;
+  unsigned int icc_len;
   GBytes *bytes;
   GdkTexture *texture;
   GdkMemoryFormat format;
+  GdkColorSpace *color_space;
   G_GNUC_UNUSED guint64 before = GDK_PROFILER_CURRENT_TIME;
 
   info.err = jpeg_std_error (&jerr.pub);
@@ -165,6 +169,9 @@ gdk_load_jpeg (GBytes  *input_bytes,
                 g_bytes_get_data (input_bytes, NULL),
                 g_bytes_get_size (input_bytes));
 
+  /* save color space */
+  jpeg_save_markers (&info, JPEG_APP0 + 2, 0xFFFF);
+
   jpeg_read_header (&info, TRUE);
   jpeg_start_decompress (&info);
 
@@ -222,19 +229,34 @@ gdk_load_jpeg (GBytes  *input_bytes,
       g_assert_not_reached ();
     }
 
+  if (jpeg_read_icc_profile (&info, &icc_data, &icc_len))
+    {
+      GBytes *icc_bytes = g_bytes_new_with_free_func (icc_data, icc_len, free, icc_data);
+      color_space = gdk_color_space_new_from_icc_profile (icc_bytes, error);
+      g_bytes_unref (icc_bytes);
+    }
+  else
+    color_space = g_object_ref (gdk_color_space_get_srgb ());
+
   jpeg_finish_decompress (&info);
   jpeg_destroy_decompress (&info);
 
   bytes = g_bytes_new_take (data, stride * height);
 
-  texture = gdk_memory_texture_new (width, height,
-                                    format,
-                                    bytes, stride);
+  if (color_space)
+    {
+      texture = gdk_memory_texture_new_with_color_space (width, height,
+                                                         format,
+                                                         color_space,
+                                                         bytes, stride);
+    }
+  else
+    texture = NULL;
 
   g_bytes_unref (bytes);
 
   gdk_profiler_end_mark (before, "jpeg load", NULL);
- 
+
   return texture;
 }
 
@@ -252,9 +274,12 @@ gdk_save_jpeg (GdkTexture *texture)
   gsize texstride;
   guchar *row;
   int width, height;
+  GdkColorSpace *color_space;
+  GBytes *bytes;
 
   width = gdk_texture_get_width (texture);
   height = gdk_texture_get_height (texture);
+  color_space = gdk_texture_get_color_space (texture);
 
   info.err = jpeg_std_error (&jerr.pub);
   jerr.pub.error_exit = fatal_error_handler;
@@ -289,6 +314,15 @@ gdk_save_jpeg (GdkTexture *texture)
 
   jpeg_start_compress (&info, TRUE);
 
+  bytes = gdk_color_space_save_to_icc_profile (color_space, NULL);
+  if (bytes)
+    {
+      jpeg_write_icc_profile (&info,
+                              g_bytes_get_data (bytes, NULL),
+                              g_bytes_get_size (bytes));
+      g_bytes_unref (bytes);
+    }
+
   while (info.next_scanline < info.image_height)
     {
       row = (guchar *) texdata + info.next_scanline * texstride;


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