## Module Interface

Module Interface — Extending gdk-pixbuf

## Synopsis

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

    gboolean    gdk_pixbuf_set_option           (GdkPixbuf *pixbuf,
                                                 const gchar *key,
                                                 const gchar *value);
    GSList*     gdk_pixbuf_get_formats          (void);
    gchar*      gdk_pixbuf_format_get_name      (GdkPixbufFormat *format);
    gchar*      gdk_pixbuf_format_get_description
                                                (GdkPixbufFormat *format);
    gchar**     gdk_pixbuf_format_get_mime_types
                                                (GdkPixbufFormat *format);
    gchar**     gdk_pixbuf_format_get_extensions
                                                (GdkPixbufFormat *format);
    gboolean    gdk_pixbuf_format_is_writable   (GdkPixbufFormat *format);
    struct      GdkPixbufFormat;
    enum        GdkPixbufFormatFlags;
    struct      GdkPixbufModulePattern;
    void        (*GdkPixbufModuleFillVtableFunc)
                                                (GdkPixbufModule *module);
    void        (*GdkPixbufModuleFillInfoFunc)  (GdkPixbufFormat *info);
    void        (*GdkPixbufModuleSizeFunc)      (gint *width,
                                                 gint *height,
                                                 gpointer user_data);
    void        (*GdkPixbufModulePreparedFunc)  (GdkPixbuf *pixbuf,
                                                 GdkPixbufAnimation *anim,
                                                 gpointer user_data);
    void        (*GdkPixbufModuleUpdatedFunc)   (GdkPixbuf *pixbuf,
                                                 int x,
                                                 int y,
                                                 int width,
                                                 int height,
                                                 gpointer user_data);
    struct      GdkPixbufModule;

    struct      GdkPixbufAnimationClass;
    struct      GdkPixbufAnimationIterClass;

## Description

If gdk-pixbuf has been compiled with GModule support, it can be extended by modules which can load (and perhaps also save) new image and animation formats. Each loadable module must export a GdkPixbufModuleFillInfoFunc function named `fill_info` and a GdkPixbufModuleFillVtableFunc function named `fill_vtable`.

In order to make format-checking work before actually loading the modules (which may require dlopening image libraries), modules export their signatures (and other information) via the `fill_info` function. An external utility, gdk-pixbuf-query-loaders, uses this to create a text file containing a list of all available loaders and their signatures. This file is then read at runtime by gdk-pixbuf to obtain the list of available loaders and their signatures.

Modules may only implement a subset of the functionality available via GdkPixbufModule. If a particular functionality is not implemented, the `fill_vtable` function will simply not set the corresponding function pointers of the GdkPixbufModule structure. If a module supports incremental loading (i.e. provides begin_load, stop_load and load_increment), it doesn't have to implement load, since gdk-pixbuf can supply a generic load implementation wrapping the incremental loading.

Installing a module is a two-step process:

- copy the module file(s) to the loader directory (normally ``libdir`/gtk-2.0/`version`/loaders`, unless overridden by the environment variable `GDK_PIXBUF_MODULEDIR`)

- call gdk-pixbuf-query-loaders to update the module file (normally ``sysconfdir`/gtk-2.0/gdk-pixbuf.loaders`, unless overridden by the environment variable `GDK_PIXBUF_MODULE_FILE`)

The gdk-pixbuf interfaces needed for implementing modules are contained in `gdk-pixbuf-io.h` (and `gdk-pixbuf-animation.h` if the module supports animations). They are not covered by the same stability guarantees as the regular gdk-pixbuf API. To underline this fact, they are protected by `ifdef GDK_PIXBUF_ENABLE_BACKEND`.

## Details

### gdk_pixbuf_set_option ()

    gboolean    gdk_pixbuf_set_option           (GdkPixbuf *pixbuf,
                                                 const gchar *key,
                                                 const gchar *value);

Attaches a key/value pair as an option to a GdkPixbuf. If `key` already exists in the list of options attached to `pixbuf`, the new value is ignored and `FALSE` is returned.

`pixbuf` : | a GdkPixbuf
`key` : | a nul-terminated string.
`value` : | a nul-terminated string.
Returns : | `TRUE` on success.

Since 2.2

### gdk_pixbuf_get_formats ()

    GSList*     gdk_pixbuf_get_formats          (void);

Obtains the available information about the image formats supported by GdkPixbuf.

Returns : | A list of GdkPixbufFormats describing the supported image formats. The list should be freed when it is no longer needed, but the structures themselves are owned by GdkPixbuf and should not be freed.

Since 2.2

### gdk_pixbuf_format_get_name ()

    gchar*      gdk_pixbuf_format_get_name      (GdkPixbufFormat *format);

Returns the name of the format.

`format` : | a GdkPixbufFormat
Returns : | the name of the format.

Since 2.2

### gdk_pixbuf_format_get_description ()

    gchar*      gdk_pixbuf_format_get_description
                                                (GdkPixbufFormat *format);

Returns a description of the format.

`format` : | a GdkPixbufFormat
Returns : | a description of the format.

Since 2.2

### gdk_pixbuf_format_get_mime_types ()

    gchar**     gdk_pixbuf_format_get_mime_types
                                                (GdkPixbufFormat *format);

Returns the mime types supported by the format.

`format` : | a GdkPixbufFormat
Returns : | a `NULL`-terminated array of mime types.

Since 2.2

### gdk_pixbuf_format_get_extensions ()

    gchar**     gdk_pixbuf_format_get_extensions
                                                (GdkPixbufFormat *format);

Returns the filename extensions typically used for files in the given format.

`format` : | a GdkPixbufFormat
Returns : | a `NULL`-terminated array of filename extensions.

Since 2.2

### gdk_pixbuf_format_is_writable ()

    gboolean    gdk_pixbuf_format_is_writable   (GdkPixbufFormat *format);

Returns whether pixbufs can be saved in the given format.

`format` : | a GdkPixbufFormat
Returns : | whether pixbufs can be saved in the given format.

Since 2.2

### struct GdkPixbufFormat

    struct GdkPixbufFormat {

      gchar *name;
      GdkPixbufModulePattern *signature;
      gchar *domain;
      gchar *description;
      gchar **mime_types;
      gchar **extensions;
      guint32 flags;
    };

A GdkPixbufFormat contains information about the image format accepted by a module. Only modules should access the fields directly, applications should use the `gdk_pixbuf_format_*` functions.

gchar *`name` | the name of the image format.
GdkPixbufModulePattern *`signature` | the signature of the module.
gchar *`domain` | the message domain for the `description`.
gchar *`description` | a description of the image format.
gchar **`mime_types` | a `NULL`-terminated array of MIME types for the image format.
gchar **`extensions` | a `NULL`-terminated array of typical filename extensions for the image format.
guint32 `flags` | a combination of GdkPixbufFormatFlags.

Since 2.2

### enum GdkPixbufFormatFlags

    typedef enum /*< skip >*/
    {
      GDK_PIXBUF_FORMAT_WRITABLE = 1 << 0
    } GdkPixbufFormatFlags;

Flags which allow a module to specify further details about the supported operations.

`GDK_PIXBUF_FORMAT_WRITABLE` | the module can write out images in the format.

Since 2.2

### struct GdkPixbufModulePattern

    struct GdkPixbufModulePattern {

    	unsigned char *prefix;
    	unsigned char *mask;
    	int relevance;
    };

The signature of a module is a set of prefixes. Prefixes are encoded as pairs of ordinary strings, where the second string, if not `NULL`, may contain ' ', '!', 'x', 'z', and 'n' to indicate bytes that must be matched, not matched, "don't-care"-bytes, zeros and non-zeros. Each prefix has an associated integer that describes the relevance of the prefix, with 0 meaning a mismatch and 100 a "perfect match".

The signature of a module is stored as an array of GdkPixbufModulePatterns.

unsigned char *`prefix` | the prefix for this pattern
unsigned char *`mask` | mask containing bytes which modify how the prefix is matched against test data
int `relevance` | relevance of this pattern

Since 2.2

### GdkPixbufModuleFillVtableFunc ()

    void        (*GdkPixbufModuleFillVtableFunc)
                                                (GdkPixbufModule *module);

Defines the type of the function used to set the vtable of a GdkPixbufModule when it is loaded.

`module` : | a GdkPixbufModule.

Since 2.2

### GdkPixbufModuleFillInfoFunc ()

    void        (*GdkPixbufModuleFillInfoFunc)  (GdkPixbufFormat *info);

Defines the type of the function used to fill a GdkPixbufFormat structure with information about a module.

`info` : | a GdkPixbufFormat.

Since 2.2

### GdkPixbufModuleSizeFunc ()

    void        (*GdkPixbufModuleSizeFunc)      (gint *width,
                                                 gint *height,
                                                 gpointer user_data);

Defines the type of the function that gets called once the size of the loaded image is known.

The function is expected to set `width` and `height` to the desired size to which the image should be scaled. If a module has no efficient way to achieve the desired scaling during the loading of the image, it may either ignore the size request, or only approximate it -- gdk-pixbuf will then perform the required scaling on the completely loaded image.

`width` : | pointer to a location containing the current image width
`height` : | pointer to a location containing the current image height
`user_data` : | the loader.

Since 2.2

### GdkPixbufModulePreparedFunc ()

    void        (*GdkPixbufModulePreparedFunc)  (GdkPixbuf *pixbuf,
                                                 GdkPixbufAnimation *anim,
                                                 gpointer user_data);

Defines the type of the function that gets called once the initial setup of `pixbuf` is done.

GdkPixbufLoader uses a function of this type to emit the "area_prepared" signal.

`pixbuf` : | the GdkPixbuf that is currently being loaded.
`anim` : | if an animation is being loaded, the GdkPixbufAnimation, else `NULL`.
`user_data` : | the loader.

Since 2.2

### GdkPixbufModuleUpdatedFunc ()

    void        (*GdkPixbufModuleUpdatedFunc)   (GdkPixbuf *pixbuf,
                                                 int x,
                                                 int y,
                                                 int width,
                                                 int height,
                                                 gpointer user_data);

Defines the type of the function that gets called every time a region of `pixbuf` is updated.

GdkPixbufLoader uses a function of this type to emit the "area_updated" signal.

`pixbuf` : | the GdkPixbuf that is currently being loaded.
`x` : | the X origin of the updated area.
`y` : | the Y origin of the updated area.
`width` : | the width of the updated area.
`height` : | the height of the updated area.
`user_data` : | the loader.

Since 2.2

### struct GdkPixbufModule

    struct GdkPixbufModule {

    	char *module_name;
    	char *module_path;
    	GModule *module;
    	GdkPixbufFormat *info;

            GdkPixbuf *(* load) (FILE    *f,
                                 GError **error);
            GdkPixbuf *(* load_xpm_data) (const char **data);

            /* Incremental loading */

            gpointer (* begin_load)     (GdkPixbufModuleSizeFunc size_func,
                                         GdkPixbufModulePreparedFunc prepare_func,
                                         GdkPixbufModuleUpdatedFunc update_func,
                                         gpointer user_data,
                                         GError **error);
            gboolean (* stop_load)      (gpointer context,
                                         GError **error);
            gboolean (* load_increment) (gpointer      context,
                                         const guchar *buf,
                                         guint         size,
                                         GError      **error);

    	/* Animation loading */
    	GdkPixbufAnimation *(* load_animation) (FILE    *f,
                                                    GError **error);

            gboolean (* save) (FILE      *f,
                               GdkPixbuf *pixbuf,
                               gchar    **param_keys,
                               gchar    **param_values,
                               GError   **error);

    };

A GdkPixbufModule contains the necessary functions to load and save images in a certain file format.

A GdkPixbufModule can be loaded dynamically from a GModule. Each loadable module must contain a GdkPixbufModuleFillVtableFunc function named `fill_vtable`, which will get called when the module is loaded and must set the function pointers of the GdkPixbufModule.

char *`module_name` | the name of the module, usually the same as the usual file extension for images of this type, eg. "xpm", "jpeg" or "png".
char *`module_path` | the path from which the module is loaded.
GModule *`module` | the loaded GModule.
GdkPixbufFormat *`info` | a GdkPixbufFormat holding information about the module.
GdkPixbuf* (*`load`) (FILE *f, GError **error) | loads an image from a file.
GdkPixbuf* (*`load_xpm_data`) (const char **data) | loads an image from data in memory.
gpointer (*`begin_load`) (GdkPixbufModuleSizeFunc size_func, GdkPixbufModulePreparedFunc prepare_func, GdkPixbufModuleUpdatedFunc update_func, gpointer user_data, GError **error) | begins an incremental load.
gboolean (*`stop_load`) (gpointer context, GError **error) | stops an incremental load.
gboolean (*`load_increment`) (gpointer context, const guchar *buf, guint size, GError **error) | continues an incremental load.
GdkPixbufAnimation* (*`load_animation`) (FILE *f, GError **error) | loads an animation from a file.
gboolean (*`save`) (FILE *f, GdkPixbuf *pixbuf, gchar **param_keys, gchar **param_values, GError **error) | saves a GdkPixbuf to a file.

### struct GdkPixbufAnimationClass

    struct GdkPixbufAnimationClass {

            GObjectClass parent_class;

            gboolean                (*is_static_image)  (GdkPixbufAnimation *anim);

            GdkPixbuf*              (*get_static_image) (GdkPixbufAnimation *anim);

            void                    (*get_size) (GdkPixbufAnimation *anim,
                                                 int                 *width,
                                                 int                 *height);

            GdkPixbufAnimationIter* (*get_iter) (GdkPixbufAnimation *anim,
                                                 const GTimeVal     *start_time);

    };

Modules supporting animations must derive a type from GdkPixbufAnimation, providing suitable implementations of the virtual functions.

GObjectClass `parent_class` |
gboolean (*`is_static_image`) (GdkPixbufAnimation *anim) | returns whether the given animation is just a static image.
GdkPixbuf* (*`get_static_image`) (GdkPixbufAnimation *anim) | returns a static image representing the given animation.
void (*`get_size`) (GdkPixbufAnimation *anim, int *width, int *height) | fills `width` and `height` with the frame size of the animation.
GdkPixbufAnimationIter* (*`get_iter`) (GdkPixbufAnimation *anim, const GTimeVal *start_time) | returns an iterator for the given animation.

### struct GdkPixbufAnimationIterClass

    struct GdkPixbufAnimationIterClass {

            GObjectClass parent_class;

            int        (*get_delay_time)   (GdkPixbufAnimationIter *iter);

            GdkPixbuf* (*get_pixbuf)       (GdkPixbufAnimationIter *iter);

            gboolean (*on_currently_loading_frame) (GdkPixbufAnimationIter *iter);

            gboolean (*advance)            (GdkPixbufAnimationIter *iter,
                                            const GTimeVal         *current_time);
    };

Modules supporting animations must derive a type from GdkPixbufAnimationIter, providing suitable implementations of the virtual functions.

GObjectClass `parent_class` |
int (*`get_delay_time`) (GdkPixbufAnimationIter *iter) | returns the time in milliseconds that the current frame should be shown.
GdkPixbuf* (*`get_pixbuf`) (GdkPixbufAnimationIter *iter) | returns the current frame.
gboolean (*`on_currently_loading_frame`) (GdkPixbufAnimationIter *iter) | returns whether the current frame of `iter` is being loaded.
gboolean (*`advance`) (GdkPixbufAnimationIter *iter, const GTimeVal *current_time) | advances the iterator to `current_time`, possibly changing the current frame.