[gtk/gamma-shenanigans: 27/29] Use our tiff loader for content (de)serialization




commit 59237666234aecd2eea30536c46427b859287883
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Sep 9 23:05:01 2021 -0400

    Use our tiff loader for content (de)serialization
    
    We still fall back to gdk-pixbuf for handling all
    the other image formats.

 gdk/gdkcontentdeserializer.c | 37 ++++++++++++++++++++++++
 gdk/gdkcontentserializer.c   | 69 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+)
---
diff --git a/gdk/gdkcontentdeserializer.c b/gdk/gdkcontentdeserializer.c
index 158ec23f6e..a3db3145f0 100644
--- a/gdk/gdkcontentdeserializer.c
+++ b/gdk/gdkcontentdeserializer.c
@@ -26,6 +26,7 @@
 #include "gdktexture.h"
 #include "gdkrgbaprivate.h"
 #include "gdkpng.h"
+#include "gdktiff.h"
 
 #include <gdk-pixbuf/gdk-pixbuf.h>
 
@@ -686,6 +687,36 @@ png_deserializer (GdkContentDeserializer *deserializer)
                       deserializer);
 }
 
+static void
+tiff_deserializer_finish (GObject      *source,
+                          GAsyncResult *res,
+                          gpointer      deserializer)
+{
+  GdkTexture *texture;
+  GValue *value;
+  GError *error = NULL;
+
+  texture = gdk_load_tiff_finish (res, &error);
+  if (texture == NULL)
+    {
+      gdk_content_deserializer_return_error (deserializer, error);
+      return;
+    }
+
+  value = gdk_content_deserializer_get_value (deserializer);
+  g_value_take_object (value, texture);
+  gdk_content_deserializer_return_success (deserializer);
+}
+
+static void
+tiff_deserializer (GdkContentDeserializer *deserializer)
+{
+  gdk_load_tiff_async (gdk_content_deserializer_get_input_stream (deserializer),
+                       gdk_content_deserializer_get_cancellable (deserializer),
+                       tiff_deserializer_finish,
+                       deserializer);
+}
+
 static void
 string_deserializer_finish (GObject      *source,
                             GAsyncResult *result,
@@ -900,6 +931,12 @@ init (void)
                                      NULL,
                                      NULL);
 
+  gdk_content_register_deserializer ("image/tiff",
+                                     GDK_TYPE_TEXTURE,
+                                     tiff_deserializer,
+                                     NULL,
+                                     NULL);
+
   formats = gdk_pixbuf_get_formats ();
 
   /* Make sure png comes first */
diff --git a/gdk/gdkcontentserializer.c b/gdk/gdkcontentserializer.c
index f2a0e8930c..d6679076a5 100644
--- a/gdk/gdkcontentserializer.c
+++ b/gdk/gdkcontentserializer.c
@@ -27,6 +27,7 @@
 #include "gdktextureprivate.h"
 #include "gdkrgba.h"
 #include "gdkpng.h"
+#include "gdktiff.h"
 #include "gdkmemorytextureprivate.h"
 
 #include <gdk-pixbuf/gdk-pixbuf.h>
@@ -724,6 +725,69 @@ png_serializer (GdkContentSerializer *serializer)
   g_object_unref (texture);
 }
 
+typedef struct {
+  GdkContentSerializer *serializer;
+  GBytes *bytes;
+} TiffSerializerData;
+
+static void
+tiff_serializer_finish (GObject      *source,
+                        GAsyncResult *res,
+                        gpointer      user_data)
+{
+  TiffSerializerData *data = user_data;
+  GError *error = NULL;
+
+  if (!gdk_save_tiff_finish (res, &error))
+    gdk_content_serializer_return_error (data->serializer, error);
+  else
+    gdk_content_serializer_return_success (data->serializer);
+
+  g_bytes_unref (data->bytes);
+  g_free (data);
+}
+
+static void
+tiff_serializer (GdkContentSerializer *serializer)
+{
+  const GValue *value;
+  GdkTexture *texture;
+  GdkMemoryFormat format;
+  GBytes *bytes = NULL;
+  TiffSerializerData *data;
+
+  value = gdk_content_serializer_get_value (serializer);
+
+  texture = g_value_get_object (value);
+
+  for (int i = 0; i < GDK_MEMORY_N_FORMATS; i++)
+    {
+      bytes = gdk_texture_download_format (texture, i);
+      if (bytes)
+        {
+          format = i;
+          break;
+        }
+    }
+
+  g_assert (bytes != NULL);
+
+  data = g_new0 (TiffSerializerData, 1);
+  data->serializer = serializer;
+  data->bytes = bytes;
+
+  gdk_save_tiff_async (gdk_content_serializer_get_output_stream (serializer),
+                       g_bytes_get_data (bytes, NULL),
+                       gdk_texture_get_width (texture),
+                       gdk_texture_get_height (texture),
+                       gdk_texture_get_width (texture) * gdk_memory_format_bytes_per_pixel (format),
+                       format,
+                       gdk_content_serializer_get_cancellable (serializer),
+                       tiff_serializer_finish,
+                       data);
+  g_object_unref (texture);
+}
+
 static void
 string_serializer_finish (GObject      *source,
                           GAsyncResult *result,
@@ -948,6 +1012,11 @@ init (void)
                                    png_serializer,
                                    NULL, NULL);
 
+  gdk_content_register_serializer (GDK_TYPE_TEXTURE,
+                                   "image/tiff",
+                                   tiff_serializer,
+                                   NULL, NULL);
+
   formats = gdk_pixbuf_get_formats ();
 
   /* Make sure png comes first */


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