[gtk/image-loading: 7/18] Load pngs without gdk-pixbuf




commit 11ae8b71f671878c58d7f02f95845badd794aaae
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 7ac4f1bb53..02ac1610fa 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
@@ -361,7 +362,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);
@@ -370,6 +373,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]