diff options
author | Benjamin Otte <otte@redhat.com> | 2013-01-30 03:00:54 +0100 |
---|---|---|
committer | Benjamin Otte <otte@redhat.com> | 2013-01-30 03:00:54 +0100 |
commit | ff01866f4a5aef10c1a4a86fc2fe66e49a634bd1 (patch) | |
tree | 52cf8ed72eb77126505451fbd6a63012671d1ff2 /gdk-pixbuf/gdk-pixbuf-animation.c | |
parent | 735fc79582a27efb53ea34ae78cc1e4afdbab547 (diff) | |
download | gdk-pixbuf-ff01866f4a5aef10c1a4a86fc2fe66e49a634bd1.tar.gz |
API: Add gdk_pixbuf_animation_new_from_stream()
Diffstat (limited to 'gdk-pixbuf/gdk-pixbuf-animation.c')
-rw-r--r-- | gdk-pixbuf/gdk-pixbuf-animation.c | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/gdk-pixbuf/gdk-pixbuf-animation.c b/gdk-pixbuf/gdk-pixbuf-animation.c index e47a9f18d..60ef4a131 100644 --- a/gdk-pixbuf/gdk-pixbuf-animation.c +++ b/gdk-pixbuf/gdk-pixbuf-animation.c @@ -26,6 +26,7 @@ #include <errno.h> #include "gdk-pixbuf-private.h" #include "gdk-pixbuf-animation.h" +#include "gdk-pixbuf-loader.h" #include <glib/gstdio.h> @@ -311,6 +312,90 @@ gdk_pixbuf_animation_new_from_file (const char *filename, #endif /** + * gdk_pixbuf_animation_new_from_stream: + * @stream: a #GInputStream to load the pixbuf from + * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore + * @error: Return location for an error + * + * Creates a new animation by loading it from an input stream. + * + * The file format is detected automatically. If %NULL is returned, then + * @error will be set. The @cancellable can be used to abort the operation + * from another thread. If the operation was cancelled, the error + * %G_IO_ERROR_CANCELLED will be returned. Other possible errors are in + * the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains. + * + * The stream is not closed. + * + * Return value: A newly-created pixbuf, or %NULL if any of several error + * conditions occurred: the file could not be opened, the image format is + * not supported, there was not enough memory to allocate the image buffer, + * the stream contained invalid data, or the operation was cancelled. + * + * Since: 2.28 + **/ +GdkPixbufAnimation * +gdk_pixbuf_animation_new_from_stream (GInputStream *stream, + GCancellable *cancellable, + GError **error) +{ + GdkPixbufAnimation *animation; + GdkPixbufLoader *loader; + gssize n_read; + guchar buffer[LOAD_BUFFER_SIZE]; + gboolean res; + + g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL); + g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL); + g_return_val_if_fail (error == NULL || *error == NULL, NULL); + + loader = gdk_pixbuf_loader_new (); + + res = TRUE; + while (1) { + n_read = g_input_stream_read (stream, + buffer, + sizeof (buffer), + cancellable, + error); + if (n_read < 0) { + res = FALSE; + error = NULL; /* Ignore further errors */ + break; + } + + if (n_read == 0) + break; + + if (!gdk_pixbuf_loader_write (loader, + buffer, + n_read, + error)) { + res = FALSE; + error = NULL; + break; + } + } + + if (!gdk_pixbuf_loader_close (loader, error)) { + res = FALSE; + error = NULL; + } + + if (res) { + animation = gdk_pixbuf_loader_get_animation (loader); + if (animation) + g_object_ref (animation); + } else { + animation = NULL; + } + + g_object_unref (loader); + + return animation; +} + +/** * gdk_pixbuf_animation_ref: (skip) * @animation: An animation. * |