[gtk/image-loading: 2/14] Load pngs without gdk-pixbuf




commit 03c76ac07e4afa9f735949d71aeb3a666b1db541
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Sep 11 16:23:53 2021 -0400

    Load pngs without gdk-pixbuf
    
    Use our own loader for pngs, which will allow
    us to get e.g. 16-bit data in the future.

 gdk/gdktexture.c | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)
---
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index 7b055238fd..c98eb7b4e3 100644
--- a/gdk/gdktexture.c
+++ b/gdk/gdktexture.c
@@ -46,6 +46,7 @@
 #include "gdksnapshot.h"
 
 #include <graphene.h>
+#include "loaders/gdkpngprivate.h"
 
 /* HACK: So we don't need to include any (not-yet-created) GSK or GTK headers */
 void
@@ -383,24 +384,39 @@ GdkTexture *
 gdk_texture_new_from_file (GFile   *file,
                            GError **error)
 {
-  GdkTexture *texture;
-  GdkPixbuf *pixbuf;
-  GInputStream *stream;
+  GdkTexture *texture = NULL;
+  GBytes *bytes;
+  const char *data;
 
   g_return_val_if_fail (G_IS_FILE (file), NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
-  stream = G_INPUT_STREAM (g_file_read (file, NULL, error));
-  if (stream == NULL)
+  bytes = g_file_load_bytes (file, NULL, NULL, error);
+  if (!bytes)
     return NULL;
 
-  pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
-  g_object_unref (stream);
-  if (pixbuf == NULL)
-    return NULL;
+  data = g_bytes_get_data (bytes, NULL);
 
-  texture = gdk_texture_new_for_pixbuf (pixbuf);
-  g_object_unref (pixbuf);
+  if (memcmp (data, PNG_SIGNATURE, strlen (PNG_SIGNATURE)) == 0)
+    texture = gdk_load_png (bytes, error);
+  else
+    {
+      GInputStream *stream;
+      GdkPixbuf *pixbuf;
+
+      stream = g_memory_input_stream_new_from_bytes (bytes);
+      pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
+
+      if (pixbuf)
+        {
+          texture = gdk_texture_new_for_pixbuf (pixbuf);
+          g_object_unref (pixbuf);
+        }
+
+      g_clear_object (&stream);
+    }
+
+  g_bytes_unref (bytes);
 
   return texture;
 }


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