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




commit 55138cd0b3a46dd8df409928ac226d3f0399c6da
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 | 41 ++++++++++++++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 11 deletions(-)
---
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index 7b055238fd..480cc434f9 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,42 @@ 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 = NULL;
+      GdkPixbuf *pixbuf = NULL;
+
+      stream = G_INPUT_STREAM (g_file_read (file, NULL, error));
+      if (stream == NULL)
+        goto done;
+
+      pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
+      if (pixbuf == NULL)
+        goto done;
+
+      texture = gdk_texture_new_for_pixbuf (pixbuf);
+
+done:
+      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]