[gtk/image-loading: 19/31] Load pngs without gdk-pixbuf




commit fdcb23e03a2949e664667105723058f1f5d5563a
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 | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
---
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index 7b055238fd..3c937e24fb 100644
--- a/gdk/gdktexture.c
+++ b/gdk/gdktexture.c
@@ -46,6 +46,7 @@
 #include "gdksnapshot.h"
 
 #include <graphene.h>
+#include "gdkpng.h"
 
 /* HACK: So we don't need to include any (not-yet-created) GSK or GTK headers */
 void
@@ -385,7 +386,9 @@ gdk_texture_new_from_file (GFile   *file,
 {
   GdkTexture *texture;
   GdkPixbuf *pixbuf;
-  GInputStream *stream;
+  GInputStream *stream, *buffered;
+  const char *data;
+  gsize size;
 
   g_return_val_if_fail (G_IS_FILE (file), NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
@@ -394,6 +397,20 @@ gdk_texture_new_from_file (GFile   *file,
   if (stream == NULL)
     return NULL;
 
+  buffered = g_buffered_input_stream_new (stream);
+  g_object_unref (stream);
+  stream = buffered;
+
+  g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream), 8, NULL, NULL);
+  data = g_buffered_input_stream_peek_buffer (G_BUFFERED_INPUT_STREAM (stream), &size);
+
+  if (memcmp (data, "\x89PNG", 4) == 0)
+    {
+      texture = gdk_load_png (stream, error);
+      g_object_unref (stream);
+      return texture;
+    }
+
   pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
   g_object_unref (stream);
   if (pixbuf == NULL)


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