Merge branch 'misc-texture-things' into 'master'

Add gdk_texture_new_from_bytes

See merge request GNOME/gtk!3958
This commit is contained in:
Matthias Clasen 2021-09-16 03:14:12 +00:00
commit fe46da1bf4
3 changed files with 46 additions and 14 deletions

View File

@ -28,8 +28,8 @@
* `GdkPixbuf`, or a Cairo surface, or other pixel data. * `GdkPixbuf`, or a Cairo surface, or other pixel data.
* *
* The ownership of the pixel data is transferred to the `GdkTexture` * The ownership of the pixel data is transferred to the `GdkTexture`
* instance; you can only make a copy of it, via * instance; you can only make a copy of it, via [method@Gdk.Texture.download]
* [method@Gdk.Texture.download]. * or [method@Gdk.Texture.download_float].
* *
* `GdkTexture` is an immutable object: That means you cannot change * `GdkTexture` is an immutable object: That means you cannot change
* anything about it other than increasing the reference count via * anything about it other than increasing the reference count via
@ -383,19 +383,55 @@ GdkTexture *
gdk_texture_new_from_file (GFile *file, gdk_texture_new_from_file (GFile *file,
GError **error) GError **error)
{ {
GBytes *bytes;
GdkTexture *texture; GdkTexture *texture;
GdkPixbuf *pixbuf;
GInputStream *stream;
g_return_val_if_fail (G_IS_FILE (file), NULL); g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL);
stream = G_INPUT_STREAM (g_file_read (file, NULL, error)); bytes = g_file_load_bytes (file, NULL, NULL, error);
if (stream == NULL) if (bytes == NULL)
return 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); pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
g_object_unref (stream); g_object_unref (stream);
if (pixbuf == NULL) if (pixbuf == NULL)
return NULL; return NULL;

View File

@ -49,6 +49,9 @@ GdkTexture * gdk_texture_new_from_resource (const char
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
GdkTexture * gdk_texture_new_from_file (GFile *file, GdkTexture * gdk_texture_new_from_file (GFile *file,
GError **error); GError **error);
GDK_AVAILABLE_IN_4_6
GdkTexture * gdk_texture_new_from_bytes (GBytes *bytes,
GError **error);
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
int gdk_texture_get_width (GdkTexture *texture) G_GNUC_PURE; int gdk_texture_get_width (GdkTexture *texture) G_GNUC_PURE;

View File

@ -505,14 +505,7 @@ gtk_make_symbolic_pixbuf_from_file (GFile *file,
GdkTexture * GdkTexture *
gtk_load_symbolic_texture_from_resource (const char *path) gtk_load_symbolic_texture_from_resource (const char *path)
{ {
GdkPixbuf *pixbuf; return gdk_texture_new_from_resource (path);
GdkTexture *texture;
pixbuf = _gdk_pixbuf_new_from_resource (path, "png", NULL);
texture = gdk_texture_new_for_pixbuf (pixbuf);
g_object_unref (pixbuf);
return texture;
} }
GdkTexture * GdkTexture *