From 1230bce1337cb940851088a902e752ceeb8b10e2 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 13 Sep 2021 13:30:12 -0400 Subject: [PATCH 1/3] Add gdk_texture_new_from_bytes Add this new api, and make gdk_texture_new_from_file a wrapper around it. --- gdk/gdktexture.c | 44 ++++++++++++++++++++++++++++++++++++++++---- gdk/gdktexture.h | 3 +++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c index 7b055238fd..ee7570e7e7 100644 --- a/gdk/gdktexture.c +++ b/gdk/gdktexture.c @@ -383,19 +383,55 @@ GdkTexture * gdk_texture_new_from_file (GFile *file, GError **error) { + GBytes *bytes; GdkTexture *texture; - GdkPixbuf *pixbuf; - GInputStream *stream; 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 == NULL) return NULL; + texture = gdk_texture_new_from_bytes (bytes, error); + + g_bytes_unref (bytes); + + return texture; +} + + +/** + * gdk_texture_new_from_bytes: + * @bytes: a `GBytes` containing the data to load + * @error: Return location for an error + * + * Creates a new texture by loading an image from memory, + * + * The file format is detected automatically. The supported formats + * are PNG and JPEG, though more formats might be available. + * + * If %NULL is returned, then @error will be set. + * + * Return value: A newly-created `GdkTexture` + * + * Since: 4.6 + */ +GdkTexture * +gdk_texture_new_from_bytes (GBytes *bytes, + GError **error) +{ + GInputStream *stream; + GdkPixbuf *pixbuf; + GdkTexture *texture; + + g_return_val_if_fail (bytes != NULL, NULL); + g_return_val_if_fail (error == NULL || *error == NULL, NULL); + + stream = g_memory_input_stream_new_from_bytes (bytes); pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error); g_object_unref (stream); + if (pixbuf == NULL) return NULL; diff --git a/gdk/gdktexture.h b/gdk/gdktexture.h index 9e3b7a93fa..7cccdb5928 100644 --- a/gdk/gdktexture.h +++ b/gdk/gdktexture.h @@ -49,6 +49,9 @@ GdkTexture * gdk_texture_new_from_resource (const char GDK_AVAILABLE_IN_ALL GdkTexture * gdk_texture_new_from_file (GFile *file, GError **error); +GDK_AVAILABLE_IN_4_6 +GdkTexture * gdk_texture_new_from_bytes (GBytes *bytes, + GError **error); GDK_AVAILABLE_IN_ALL int gdk_texture_get_width (GdkTexture *texture) G_GNUC_PURE; From 9f8d6ff29c677132ee6f41e584843a07779264c7 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Tue, 14 Sep 2021 08:38:25 -0400 Subject: [PATCH 2/3] texture: Mention download_float in the docs --- gdk/gdktexture.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c index ee7570e7e7..edcf7a9d9c 100644 --- a/gdk/gdktexture.c +++ b/gdk/gdktexture.c @@ -28,8 +28,8 @@ * `GdkPixbuf`, or a Cairo surface, or other pixel data. * * The ownership of the pixel data is transferred to the `GdkTexture` - * instance; you can only make a copy of it, via - * [method@Gdk.Texture.download]. + * instance; you can only make a copy of it, via [method@Gdk.Texture.download] + * or [method@Gdk.Texture.download_float]. * * `GdkTexture` is an immutable object: That means you cannot change * anything about it other than increasing the reference count via From 7fd889909277b9b8e7fc6c8e9f592f7515ed4463 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Tue, 14 Sep 2021 18:17:44 +0200 Subject: [PATCH 3/3] pixbufutils: Simplify function --- gtk/gdkpixbufutils.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/gtk/gdkpixbufutils.c b/gtk/gdkpixbufutils.c index d1b6693918..2b48d76943 100644 --- a/gtk/gdkpixbufutils.c +++ b/gtk/gdkpixbufutils.c @@ -505,14 +505,7 @@ gtk_make_symbolic_pixbuf_from_file (GFile *file, GdkTexture * gtk_load_symbolic_texture_from_resource (const char *path) { - GdkPixbuf *pixbuf; - GdkTexture *texture; - - pixbuf = _gdk_pixbuf_new_from_resource (path, "png", NULL); - texture = gdk_texture_new_for_pixbuf (pixbuf); - g_object_unref (pixbuf); - - return texture; + return gdk_texture_new_from_resource (path); } GdkTexture *