GdkPixbufLoader

## GdkPixbufLoader GdkPixbufLoader — Application-driven progressive image loading. |

## Synopsis

    #include <gdk-pixbuf/gdk-pixbuf.h>

    struct      GdkPixbufLoader;
    GdkPixbufLoader* gdk_pixbuf_loader_new      (void);
    GdkPixbufLoader* gdk_pixbuf_loader_new_with_type
                                                (const char *image_type,
                                                 GError **error);
    GdkPixbufLoader* gdk_pixbuf_loader_new_with_mime_type
                                                (const char *mime_type,
                                                 GError **error);
    GdkPixbufFormat* gdk_pixbuf_loader_get_format
                                                (GdkPixbufLoader *loader);
    gboolean    gdk_pixbuf_loader_write         (GdkPixbufLoader *loader,
                                                 const guchar *buf,
                                                 gsize count,
                                                 GError **error);
    void        gdk_pixbuf_loader_set_size      (GdkPixbufLoader *loader,
                                                 int width,
                                                 int height);
    GdkPixbuf*  gdk_pixbuf_loader_get_pixbuf    (GdkPixbufLoader *loader);
    GdkPixbufAnimation* gdk_pixbuf_loader_get_animation
                                                (GdkPixbufLoader *loader);
    gboolean    gdk_pixbuf_loader_close         (GdkPixbufLoader *loader,
                                                 GError **error);

## Object Hierarchy

      GObject
       +----GdkPixbufLoader

## Signal Prototypes

    "area-prepared"
                void        user_function      (GdkPixbufLoader *loader,
                                                gpointer user_data);
    "area-updated"
                void        user_function      (GdkPixbufLoader *loader,
                                                gint x,
                                                gint y,
                                                gint width,
                                                gint height,
                                                gpointer user_data);
    "closed"    void        user_function      (GdkPixbufLoader *loader,
                                                gpointer user_data);
    "size-prepared"
                void        user_function      (GdkPixbufLoader *loader,
                                                gint width,
                                                gint height,
                                                gpointer user_data);

## Description

GdkPixbufLoader provides a way for applications to drive the process of loading an image, by letting them send the image data directly to the loader instead of having the loader read the data from a file. Applications can use this functionality instead of `gdk_pixbuf_new_from_file()` or `gdk_pixbuf_animation_new_from_file()` when they need to parse image data in small chunks. For example, it should be used when reading an image from a (potentially) slow network connection, or when loading an extremely large file.

To use GdkPixbufLoader to load an image, just create a new one, and call `gdk_pixbuf_loader_write()` to send the data to it. When done, `gdk_pixbuf_loader_close()` should be called to end the stream and finalize everything. The loader will emit three important signals throughout the process. The first, "size_prepared", will be called as soon as the image has enough information to determine the size of the image to be used. If you want to scale the image while loading it, you can call `gdk_pixbuf_loader_set_size()` in response to this signal.

The second signal, "area_prepared", will be called as soon as the pixbuf of the desired has been allocated. You can obtain it by calling `gdk_pixbuf_loader_get_pixbuf()`. If you want to use it, simply ref it. In addition, no actual information will be passed in yet, so the pixbuf can be safely filled with any temporary graphics (or an initial color) as needed. You can also call `gdk_pixbuf_loader_get_pixbuf()` later and get the same pixbuf.

The last signal, "area_updated" gets called every time a region is updated. This way you can update a partially completed image. Note that you do not know anything about the completeness of an image from the area updated. For example, in an interlaced image, you need to make several passes before the image is done loading.

### Loading an animation

Loading an animation is almost as easy as loading an image. Once the first "area_prepared" signal has been emitted, you can call `gdk_pixbuf_loader_get_animation()` to get the GdkPixbufAnimation struct and `gdk_pixbuf_animation_get_iter()` to get an GdkPixbufAnimationIter for displaying it.

## Details

### struct GdkPixbufLoader

    struct GdkPixbufLoader;

The GdkPixbufLoader struct contains only private fields.

### gdk_pixbuf_loader_new ()

    GdkPixbufLoader* gdk_pixbuf_loader_new      (void);

Creates a new pixbuf loader object.

Returns : | A newly-created pixbuf loader.

### gdk_pixbuf_loader_new_with_type ()

    GdkPixbufLoader* gdk_pixbuf_loader_new_with_type
                                                (const char *image_type,
                                                 GError **error);

Creates a new pixbuf loader object that always attempts to parse image data as if it were an image of type `image_type`, instead of identifying the type automatically. Useful if you want an error if the image isn't the expected type, for loading image formats that can't be reliably identified by looking at the data, or if the user manually forces a specific type.

`image_type` : | name of the image format to be loaded with the image
`error` : | return location for an allocated GError, or `NULL` to ignore errors
Returns : | A newly-created pixbuf loader.

### gdk_pixbuf_loader_new_with_mime_type ()

    GdkPixbufLoader* gdk_pixbuf_loader_new_with_mime_type
                                                (const char *mime_type,
                                                 GError **error);

Creates a new pixbuf loader object that always attempts to parse image data as if it were an image of mime type `mime_type`, instead of identifying the type automatically. Useful if you want an error if the image isn't the expected mime type, for loading image formats that can't be reliably identified by looking at the data, or if the user manually forces a specific mime type.

`mime_type` : | the mime type to be loaded
`error` : | return location for an allocated GError, or `NULL` to ignore errors
Returns : | A newly-created pixbuf loader.

Since 2.4

### gdk_pixbuf_loader_get_format ()

    GdkPixbufFormat* gdk_pixbuf_loader_get_format
                                                (GdkPixbufLoader *loader);

Obtains the available information about the format of the currently loading image file.

`loader` : | A pixbuf loader.
Returns : | A GdkPixbufFormat or `NULL`. The return value is owned by GdkPixbuf and should not be freed.

Since 2.2

### gdk_pixbuf_loader_write ()

    gboolean    gdk_pixbuf_loader_write         (GdkPixbufLoader *loader,
                                                 const guchar *buf,
                                                 gsize count,
                                                 GError **error);

This will cause a pixbuf loader to parse the next `count` bytes of an image. It will return `TRUE` if the data was loaded successfully, and `FALSE` if an error occurred. In the latter case, the loader will be closed, and will not accept further writes. If `FALSE` is returned, `error` will be set to an error from the GDK_PIXBUF_ERROR or G_FILE_ERROR domains.

`loader` : | A pixbuf loader.
`buf` : | Pointer to image data.
`count` : | Length of the `buf` buffer in bytes.
`error` : | return location for errors
Returns : | `TRUE` if the write was successful, or `FALSE` if the loader cannot parse the buffer.

### gdk_pixbuf_loader_set_size ()

    void        gdk_pixbuf_loader_set_size      (GdkPixbufLoader *loader,
                                                 int width,
                                                 int height);

Causes the image to be scaled while it is loaded. The desired image size can be determined relative to the original size of the image by calling `gdk_pixbuf_loader_set_size()` from a signal handler for the ::size_prepared signal.

Attempts to set the desired image size are ignored after the emission of the ::size_prepared signal.

`loader` : | A pixbuf loader.
`width` : | The desired width of the image being loaded.
`height` : | The desired height of the image being loaded.

Since 2.2

### gdk_pixbuf_loader_get_pixbuf ()

    GdkPixbuf*  gdk_pixbuf_loader_get_pixbuf    (GdkPixbufLoader *loader);

Queries the GdkPixbuf that a pixbuf loader is currently creating. In general it only makes sense to call this function after the "area_prepared" signal has been emitted by the loader; this means that enough data has been read to know the size of the image that will be allocated. If the loader has not received enough data via `gdk_pixbuf_loader_write()`, then this function returns `NULL`. The returned pixbuf will be the same in all future calls to the loader, so simply calling `g_object_ref()` should be sufficient to continue using it. Additionally, if the loader is an animation, it will return the "static image" of the animation (see `gdk_pixbuf_animation_get_static_image()`).

`loader` : | A pixbuf loader.
Returns : | The GdkPixbuf that the loader is creating, or `NULL` if not enough data has been read to determine how to create the image buffer.

### gdk_pixbuf_loader_get_animation ()

    GdkPixbufAnimation* gdk_pixbuf_loader_get_animation
                                                (GdkPixbufLoader *loader);

Queries the GdkPixbufAnimation that a pixbuf loader is currently creating. In general it only makes sense to call this function after the "area_prepared" signal has been emitted by the loader. If the loader doesn't have enough bytes yet (hasn't emitted the "area_prepared" signal) this function will return `NULL`.

`loader` : | A pixbuf loader
Returns : | The GdkPixbufAnimation that the loader is loading, or `NULL` if not enough data has been read to determine the information.

### gdk_pixbuf_loader_close ()

    gboolean    gdk_pixbuf_loader_close         (GdkPixbufLoader *loader,
                                                 GError **error);

Informs a pixbuf loader that no further writes with `gdk_pixbuf_loader_write()` will occur, so that it can free its internal loading structures. Also, tries to parse any data that hasn't yet been parsed; if the remaining data is partial or corrupt, an error will be returned. If `FALSE` is returned, `error` will be set to an error from the GDK_PIXBUF_ERROR or G_FILE_ERROR domains. If you're just cancelling a load rather than expecting it to be finished, passing `NULL` for `error` to ignore it is reasonable.

`loader` : | A pixbuf loader.
`error` : | return location for a GError, or `NULL` to ignore errors
Returns : | `TRUE` if all image data written so far was successfully passed out via the update_area signal

## Signals

### The "area-prepared" signal

    void        user_function                  (GdkPixbufLoader *loader,
                                                gpointer user_data);

This signal is emitted when the pixbuf loader has allocated the pixbuf in the desired size. After this signal is emitted, applications can call `gdk_pixbuf_loader_get_pixbuf()` to fetch the partially-loaded pixbuf.

`loader` : | the object which received the signal.
`user_data` : | user data set when the signal handler was connected.

### The "area-updated" signal

    void        user_function                  (GdkPixbufLoader *loader,
                                                gint x,
                                                gint y,
                                                gint width,
                                                gint height,
                                                gpointer user_data);

This signal is emitted when a significant area of the image being loaded has been updated. Normally it means that a complete scanline has been read in, but it could be a different area as well. Applications can use this signal to know when to repaint areas of an image that is being loaded.

`loader` : | the object which received the signal.
`x` : | X offset of upper-left corner of the updated area.
`y` : | Y offset of upper-left corner of the updated area.
`width` : | Width of updated area.
`height` : | Height of updated area.
`user_data` : | user data set when the signal handler was connected.

### The "closed" signal

    void        user_function                  (GdkPixbufLoader *loader,
                                                gpointer user_data);

This signal is emitted when `gdk_pixbuf_loader_close()` is called. It can be used by different parts of an application to receive notification when an image loader is closed by the code that drives it.

`loader` : | the object which received the signal.
`user_data` : | user data set when the signal handler was connected.

### The "size-prepared" signal

    void        user_function                  (GdkPixbufLoader *loader,
                                                gint width,
                                                gint height,
                                                gpointer user_data);

This signal is emitted when the pixbuf loader has been fed the initial amount of data that is required to figure out the size of the image that it will create. Applications can call `gdk_pixbuf_loader_set_size()` in response to this signal to set the desired size to which the image should be scaled.

`loader` : | the object which received the signal.
`width` : | the original width of the image
`height` : | the original height of the image
`user_data` : | user data set when the signal handler was connected.

## See Also

`gdk_pixbuf_new_from_file()`, `gdk_pixbuf_animation_new_from_file()`