[gtk/matthiasc/color-profile-rebased: 7/43] jpeg: Add color profile support




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

    jpeg: Add color profile support

 gdk/loaders/gdkjpeg.c | 39 +++++++++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)
---
diff --git a/gdk/loaders/gdkjpeg.c b/gdk/loaders/gdkjpeg.c
index 8f7e422f0c..5bbfc5cd76 100644
--- a/gdk/loaders/gdkjpeg.c
+++ b/gdk/loaders/gdkjpeg.c
@@ -21,9 +21,10 @@
 
 #include "gdkjpegprivate.h"
 
+#include "gdkcolorprofile.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;
+  GdkColorProfile *color_profile;
   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 profile */
+  jpeg_save_markers (&info, JPEG_APP0 + 2, 0xFFFF);
+
   jpeg_read_header (&info, TRUE);
   jpeg_start_decompress (&info);
 
@@ -222,14 +229,29 @@ 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_profile = gdk_color_profile_new_from_icc_bytes (icc_bytes, error);
+      g_bytes_unref (icc_bytes);
+    }
+  else
+    color_profile = g_object_ref (gdk_color_profile_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_profile)
+    {
+      texture = gdk_memory_texture_new_with_color_profile (width, height,
+                                                           format,
+                                                           color_profile,
+                                                           bytes, stride);
+    }
+  else
+    texture = NULL;
 
   g_bytes_unref (bytes);
 
@@ -252,9 +274,12 @@ gdk_save_jpeg (GdkTexture *texture)
   gsize texstride;
   guchar *row;
   int width, height;
+  GdkColorProfile *color_profile;
+  GBytes *bytes;
 
   width = gdk_texture_get_width (texture);
   height = gdk_texture_get_height (texture);
+  color_profile = gdk_texture_get_color_profile (texture);
 
   info.err = jpeg_std_error (&jerr.pub);
   jerr.pub.error_exit = fatal_error_handler;
@@ -289,6 +314,12 @@ gdk_save_jpeg (GdkTexture *texture)
 
   jpeg_start_compress (&info, TRUE);
 
+  bytes = gdk_color_profile_get_icc_profile (color_profile);
+  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]