Blame gdk-pixbuf/gdk-pixbuf-loader.c

Packit 979760
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
Packit 979760
/* GdkPixbuf library - Progressive loader object
Packit 979760
 *
Packit 979760
 * Copyright (C) 1999 The Free Software Foundation
Packit 979760
 *
Packit 979760
 * Authors: Mark Crichton <crichton@gimp.org>
Packit 979760
 *          Miguel de Icaza <miguel@gnu.org>
Packit 979760
 *          Federico Mena-Quintero <federico@gimp.org>
Packit 979760
 *          Jonathan Blandford <jrb@redhat.com>
Packit 979760
 *
Packit 979760
 * This library is free software; you can redistribute it and/or
Packit 979760
 * modify it under the terms of the GNU Lesser General Public
Packit 979760
 * License as published by the Free Software Foundation; either
Packit 979760
 * version 2 of the License, or (at your option) any later version.
Packit 979760
 *
Packit 979760
 * This library is distributed in the hope that it will be useful,
Packit 979760
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 979760
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 979760
 * Lesser General Public License for more details.
Packit 979760
 *
Packit 979760
 * You should have received a copy of the GNU Lesser General Public
Packit 979760
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit 979760
 */
Packit 979760
Packit 979760
#include "config.h"
Packit 979760
#include <string.h>
Packit 979760
Packit 979760
#include "gdk-pixbuf-private.h"
Packit 979760
#include "gdk-pixbuf-animation.h"
Packit 979760
#include "gdk-pixbuf-scaled-anim.h"
Packit 979760
#include "gdk-pixbuf-loader.h"
Packit 979760
#include "gdk-pixbuf-marshal.h"
Packit 979760
Packit 979760
/**
Packit 979760
 * SECTION:gdk-pixbuf-loader
Packit 979760
 * @Short_description: Application-driven progressive image loading.
Packit 979760
 * @Title: GdkPixbufLoader
Packit 979760
 * @See_also: gdk_pixbuf_new_from_file(), gdk_pixbuf_animation_new_from_file()
Packit 979760
 * 
Packit 979760
 * #GdkPixbufLoader provides a way for applications to drive the
Packit 979760
 * process of loading an image, by letting them send the image data
Packit 979760
 * directly to the loader instead of having the loader read the data
Packit 979760
 * from a file.  Applications can use this functionality instead of
Packit 979760
 * gdk_pixbuf_new_from_file() or gdk_pixbuf_animation_new_from_file() 
Packit 979760
 * when they need to parse image data in
Packit 979760
 * small chunks.  For example, it should be used when reading an
Packit 979760
 * image from a (potentially) slow network connection, or when
Packit 979760
 * loading an extremely large file.
Packit 979760
 * 
Packit 979760
 * 
Packit 979760
 * To use #GdkPixbufLoader to load an image, just create a new one, and
Packit 979760
 * call gdk_pixbuf_loader_write() to send the data to it.  When done,
Packit 979760
 * gdk_pixbuf_loader_close() should be called to end the stream and
Packit 979760
 * finalize everything. The loader will emit three important signals
Packit 979760
 * throughout the process. The first, #GdkPixbufLoader::size-prepared,
Packit 979760
 * will be emitted as soon as the image has enough information to
Packit 979760
 * determine the size of the image to be used. If you want to scale
Packit 979760
 * the image while loading it, you can call gdk_pixbuf_loader_set_size()
Packit 979760
 * in response to this signal.
Packit 979760
 * 
Packit 979760
 * 
Packit 979760
 * The second signal, #GdkPixbufLoader::area-prepared, will be emitted as
Packit 979760
 * soon as the pixbuf of the desired has been allocated. You can obtain it
Packit 979760
 * by calling gdk_pixbuf_loader_get_pixbuf(). If you want to use it, simply
Packit 979760
 * ref it. You can also call gdk_pixbuf_loader_get_pixbuf() later and get
Packit 979760
 * the same pixbuf.
Packit 979760
 * 
Packit 979760
 * The last signal, #GdkPixbufLoader::area-updated, gets emitted every time
Packit 979760
 * a region is updated. This way you can update a partially completed image.
Packit 979760
 * Note that you do not know anything about the completeness of an image
Packit 979760
 * from the updated area. For example, in an interlaced image, you need to
Packit 979760
 * make several passes before the image is done loading.
Packit 979760
 * 
Packit 979760
 * # Loading an animation 
Packit 979760
 *
Packit 979760
 * Loading an animation is almost as easy as loading an image. Once the first
Packit 979760
 * #GdkPixbufLoader::area-prepared signal has been emitted, you can call
Packit 979760
 * gdk_pixbuf_loader_get_animation() to get the #GdkPixbufAnimation struct
Packit 979760
 * and gdk_pixbuf_animation_get_iter() to get a #GdkPixbufAnimationIter for
Packit 979760
 * displaying it. 
Packit 979760
 */
Packit 979760
Packit 979760
Packit 979760
enum {
Packit 979760
        SIZE_PREPARED,
Packit 979760
        AREA_PREPARED,
Packit 979760
        AREA_UPDATED,
Packit 979760
        CLOSED,
Packit 979760
        LAST_SIGNAL
Packit 979760
};
Packit 979760
Packit 979760
Packit 979760
static void gdk_pixbuf_loader_finalize (GObject *loader);
Packit 979760
Packit 979760
static guint    pixbuf_loader_signals[LAST_SIGNAL] = { 0 };
Packit 979760
Packit 979760
/* Internal data */
Packit 979760
Packit 979760
typedef struct
Packit 979760
{
Packit 979760
        GdkPixbufAnimation *animation;
Packit 979760
        gboolean closed;
Packit 979760
        guchar header_buf[SNIFF_BUFFER_SIZE];
Packit 979760
        gint header_buf_offset;
Packit 979760
        GdkPixbufModule *image_module;
Packit 979760
        gpointer context;
Packit 979760
        gint width;
Packit 979760
        gint height;
Packit 979760
        gboolean size_fixed;
Packit 979760
        gboolean needs_scale;
Packit 979760
	gchar *filename;
Packit 979760
} GdkPixbufLoaderPrivate;
Packit 979760
Packit 979760
G_DEFINE_TYPE (GdkPixbufLoader, gdk_pixbuf_loader, G_TYPE_OBJECT)
Packit 979760
Packit 979760
Packit 979760
static void
Packit 979760
gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
Packit 979760
{
Packit 979760
        GObjectClass *object_class;
Packit 979760
  
Packit 979760
        object_class = (GObjectClass *) class;
Packit 979760
  
Packit 979760
        object_class->finalize = gdk_pixbuf_loader_finalize;
Packit 979760
Packit 979760
        /**
Packit 979760
         * GdkPixbufLoader::size-prepared:
Packit 979760
         * @loader: the object which received the signal.
Packit 979760
         * @width: the original width of the image
Packit 979760
         * @height: the original height of the image
Packit 979760
         *
Packit 979760
         * This signal is emitted when the pixbuf loader has been fed the
Packit 979760
         * initial amount of data that is required to figure out the size
Packit 979760
         * of the image that it will create.  Applications can call  
Packit 979760
         * gdk_pixbuf_loader_set_size() in response to this signal to set
Packit 979760
         * the desired size to which the image should be scaled.
Packit 979760
         */
Packit 979760
        pixbuf_loader_signals[SIZE_PREPARED] =
Packit 979760
                g_signal_new ("size-prepared",
Packit 979760
                              G_TYPE_FROM_CLASS (object_class),
Packit 979760
                              G_SIGNAL_RUN_LAST,
Packit 979760
                              G_STRUCT_OFFSET (GdkPixbufLoaderClass, size_prepared),
Packit 979760
                              NULL, NULL,
Packit 979760
                              _gdk_pixbuf_marshal_VOID__INT_INT,
Packit 979760
                              G_TYPE_NONE, 2, 
Packit 979760
                              G_TYPE_INT,
Packit 979760
                              G_TYPE_INT);
Packit 979760
  
Packit 979760
        /**
Packit 979760
         * GdkPixbufLoader::area-prepared:
Packit 979760
         * @loader: the object which received the signal.
Packit 979760
         *
Packit 979760
         * This signal is emitted when the pixbuf loader has allocated the 
Packit 979760
         * pixbuf in the desired size.  After this signal is emitted, 
Packit 979760
         * applications can call gdk_pixbuf_loader_get_pixbuf() to fetch 
Packit 979760
         * the partially-loaded pixbuf.
Packit 979760
         */
Packit 979760
        pixbuf_loader_signals[AREA_PREPARED] =
Packit 979760
                g_signal_new ("area-prepared",
Packit 979760
                              G_TYPE_FROM_CLASS (object_class),
Packit 979760
                              G_SIGNAL_RUN_LAST,
Packit 979760
                              G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_prepared),
Packit 979760
                              NULL, NULL,
Packit 979760
                              _gdk_pixbuf_marshal_VOID__VOID,
Packit 979760
                              G_TYPE_NONE, 0);
Packit 979760
Packit 979760
        /**
Packit 979760
         * GdkPixbufLoader::area-updated:
Packit 979760
         * @loader: the object which received the signal.
Packit 979760
         * @x: X offset of upper-left corner of the updated area.
Packit 979760
         * @y: Y offset of upper-left corner of the updated area.
Packit 979760
         * @width: Width of updated area.
Packit 979760
         * @height: Height of updated area.
Packit 979760
         *
Packit 979760
         * This signal is emitted when a significant area of the image being
Packit 979760
         * loaded has been updated.  Normally it means that a complete
Packit 979760
         * scanline has been read in, but it could be a different area as
Packit 979760
         * well.  Applications can use this signal to know when to repaint
Packit 979760
         * areas of an image that is being loaded.
Packit 979760
         */
Packit 979760
        pixbuf_loader_signals[AREA_UPDATED] =
Packit 979760
                g_signal_new ("area-updated",
Packit 979760
                              G_TYPE_FROM_CLASS (object_class),
Packit 979760
                              G_SIGNAL_RUN_LAST,
Packit 979760
                              G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_updated),
Packit 979760
                              NULL, NULL,
Packit 979760
                              _gdk_pixbuf_marshal_VOID__INT_INT_INT_INT,
Packit 979760
                              G_TYPE_NONE, 4,
Packit 979760
                              G_TYPE_INT,
Packit 979760
                              G_TYPE_INT,
Packit 979760
                              G_TYPE_INT,
Packit 979760
                              G_TYPE_INT);
Packit 979760
  
Packit 979760
        /**
Packit 979760
         * GdkPixbufLoader::closed:
Packit 979760
         * @loader: the object which received the signal.
Packit 979760
         *
Packit 979760
         * This signal is emitted when gdk_pixbuf_loader_close() is called.
Packit 979760
         * It can be used by different parts of an application to receive
Packit 979760
         * notification when an image loader is closed by the code that
Packit 979760
         * drives it.
Packit 979760
         */
Packit 979760
        pixbuf_loader_signals[CLOSED] =
Packit 979760
                g_signal_new ("closed",
Packit 979760
                              G_TYPE_FROM_CLASS (object_class),
Packit 979760
                              G_SIGNAL_RUN_LAST,
Packit 979760
                              G_STRUCT_OFFSET (GdkPixbufLoaderClass, closed),
Packit 979760
                              NULL, NULL,
Packit 979760
                              _gdk_pixbuf_marshal_VOID__VOID,
Packit 979760
                              G_TYPE_NONE, 0);
Packit 979760
}
Packit 979760
Packit 979760
static void
Packit 979760
gdk_pixbuf_loader_init (GdkPixbufLoader *loader)
Packit 979760
{
Packit 979760
        GdkPixbufLoaderPrivate *priv;
Packit 979760
  
Packit 979760
        priv = g_new0 (GdkPixbufLoaderPrivate, 1);
Packit 979760
        priv->width = -1;
Packit 979760
        priv->height = -1;
Packit 979760
Packit 979760
        loader->priv = priv;
Packit 979760
}
Packit 979760
Packit 979760
static void
Packit 979760
gdk_pixbuf_loader_finalize (GObject *object)
Packit 979760
{
Packit 979760
        GdkPixbufLoader *loader;
Packit 979760
        GdkPixbufLoaderPrivate *priv = NULL;
Packit 979760
  
Packit 979760
        loader = GDK_PIXBUF_LOADER (object);
Packit 979760
        priv = loader->priv;
Packit 979760
Packit 979760
        if (!priv->closed) {
Packit 979760
                g_warning ("GdkPixbufLoader finalized without calling gdk_pixbuf_loader_close() - this is not allowed. You must explicitly end the data stream to the loader before dropping the last reference.");
Packit 979760
        }
Packit 979760
        if (priv->animation)
Packit 979760
                g_object_unref (priv->animation);
Packit 979760
  
Packit 979760
	g_free (priv->filename);
Packit 979760
Packit 979760
        g_free (priv);
Packit 979760
  
Packit 979760
        G_OBJECT_CLASS (gdk_pixbuf_loader_parent_class)->finalize (object);
Packit 979760
}
Packit 979760
Packit 979760
/**
Packit 979760
 * gdk_pixbuf_loader_set_size:
Packit 979760
 * @loader: A pixbuf loader.
Packit 979760
 * @width: The desired width of the image being loaded.
Packit 979760
 * @height: The desired height of the image being loaded.
Packit 979760
 *
Packit 979760
 * Causes the image to be scaled while it is loaded. The desired
Packit 979760
 * image size can be determined relative to the original size of
Packit 979760
 * the image by calling gdk_pixbuf_loader_set_size() from a
Packit 979760
 * signal handler for the ::size-prepared signal.
Packit 979760
 *
Packit 979760
 * Attempts to set the desired image size  are ignored after the 
Packit 979760
 * emission of the ::size-prepared signal.
Packit 979760
 *
Packit 979760
 * Since: 2.2
Packit 979760
 */
Packit 979760
void 
Packit 979760
gdk_pixbuf_loader_set_size (GdkPixbufLoader *loader,
Packit 979760
			    gint             width,
Packit 979760
			    gint             height)
Packit 979760
{
Packit 979760
        GdkPixbufLoaderPrivate *priv;
Packit 979760
Packit 979760
        g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
Packit 979760
        g_return_if_fail (width >= 0 && height >= 0);
Packit 979760
Packit 979760
        priv = GDK_PIXBUF_LOADER (loader)->priv;
Packit 979760
Packit 979760
        if (!priv->size_fixed) 
Packit 979760
                {
Packit 979760
                        priv->width = width;
Packit 979760
                        priv->height = height;
Packit 979760
                }
Packit 979760
}
Packit 979760
Packit 979760
static void
Packit 979760
gdk_pixbuf_loader_size_func (gint *width, gint *height, gpointer loader)
Packit 979760
{
Packit 979760
        GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
Packit 979760
Packit 979760
        /* allow calling gdk_pixbuf_loader_set_size() before the signal */
Packit 979760
        if (priv->width == -1 && priv->height == -1) 
Packit 979760
                {
Packit 979760
                        priv->width = *width;
Packit 979760
                        priv->height = *height;
Packit 979760
                }
Packit 979760
Packit 979760
        g_signal_emit (loader, pixbuf_loader_signals[SIZE_PREPARED], 0, *width, *height);
Packit 979760
        priv->size_fixed = TRUE;
Packit 979760
Packit 979760
        *width = priv->width;
Packit 979760
        *height = priv->height;
Packit 979760
}
Packit 979760
Packit 979760
static void
Packit 979760
gdk_pixbuf_loader_prepare (GdkPixbuf          *pixbuf,
Packit 979760
                           GdkPixbufAnimation *anim,
Packit 979760
			   gpointer            loader)
Packit 979760
{
Packit 979760
        GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
Packit 979760
        gint width, height;
Packit 979760
        g_return_if_fail (pixbuf != NULL);
Packit 979760
Packit 979760
        width = anim ? gdk_pixbuf_animation_get_width (anim) :
Packit 979760
                gdk_pixbuf_get_width (pixbuf);
Packit 979760
        height = anim ? gdk_pixbuf_animation_get_height (anim) :
Packit 979760
                gdk_pixbuf_get_height (pixbuf);
Packit 979760
Packit 979760
        if (!priv->size_fixed) 
Packit 979760
                {
Packit 979760
			gint w = width;
Packit 979760
			gint h = height;
Packit 979760
                        /* Defend against lazy loaders which don't call size_func */
Packit 979760
                        gdk_pixbuf_loader_size_func (&w, &h, loader);
Packit 979760
                }
Packit 979760
Packit 979760
        priv->needs_scale = FALSE;
Packit 979760
        if (priv->width > 0 && priv->height > 0 &&
Packit 979760
            (priv->width != width || priv->height != height))
Packit 979760
                priv->needs_scale = TRUE;
Packit 979760
Packit 979760
        if (anim)
Packit 979760
                g_object_ref (anim);
Packit 979760
        else
Packit 979760
                anim = gdk_pixbuf_non_anim_new (pixbuf);
Packit 979760
  
Packit 979760
	if (priv->needs_scale && width != 0 && height != 0) {
Packit 979760
		priv->animation  = GDK_PIXBUF_ANIMATION (_gdk_pixbuf_scaled_anim_new (anim,
Packit 979760
                                         (double) priv->width / width,
Packit 979760
                                         (double) priv->height / height,
Packit 979760
					  1.0));
Packit 979760
			g_object_unref (anim);
Packit 979760
	}
Packit 979760
	else
Packit 979760
        	priv->animation = anim;
Packit 979760
  
Packit 979760
        if (!priv->needs_scale)
Packit 979760
                g_signal_emit (loader, pixbuf_loader_signals[AREA_PREPARED], 0);
Packit 979760
}
Packit 979760
Packit 979760
static void
Packit 979760
gdk_pixbuf_loader_update (GdkPixbuf *pixbuf,
Packit 979760
			  gint       x,
Packit 979760
			  gint       y,
Packit 979760
			  gint       width,
Packit 979760
			  gint       height,
Packit 979760
			  gpointer   loader)
Packit 979760
{
Packit 979760
        GdkPixbufLoaderPrivate *priv = GDK_PIXBUF_LOADER (loader)->priv;
Packit 979760
  
Packit 979760
        if (!priv->needs_scale)
Packit 979760
                g_signal_emit (loader,
Packit 979760
                               pixbuf_loader_signals[AREA_UPDATED],
Packit 979760
                               0,
Packit 979760
                               x, y,
Packit 979760
                               /* sanity check in here.  Defend against an errant loader */
Packit 979760
                               MIN (width, gdk_pixbuf_animation_get_width (priv->animation)),
Packit 979760
                               MIN (height, gdk_pixbuf_animation_get_height (priv->animation)));
Packit 979760
}
Packit 979760
Packit 979760
/* Defense against broken loaders; DO NOT take this as a GError example! */
Packit 979760
static void
Packit 979760
gdk_pixbuf_loader_ensure_error (GdkPixbufLoader *loader,
Packit 979760
                                GError         **error)
Packit 979760
{ 
Packit 979760
        GdkPixbufLoaderPrivate *priv = loader->priv;
Packit 979760
Packit 979760
        if (error == NULL || *error != NULL)
Packit 979760
                return;
Packit 979760
Packit 979760
        g_warning ("Bug! loader '%s' didn't set an error on failure",
Packit 979760
                   priv->image_module->module_name);
Packit 979760
        g_set_error (error,
Packit 979760
                     GDK_PIXBUF_ERROR,
Packit 979760
                     GDK_PIXBUF_ERROR_FAILED,
Packit 979760
                     _("Internal error: Image loader module “%s” failed to"
Packit 979760
                       " complete an operation, but didn’t give a reason for"
Packit 979760
                       " the failure"),
Packit 979760
                     priv->image_module->module_name);
Packit 979760
}
Packit 979760
Packit 979760
static gint
Packit 979760
gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader,
Packit 979760
                               const char      *image_type,
Packit 979760
                               GError         **error)
Packit 979760
{
Packit 979760
        GdkPixbufLoaderPrivate *priv = loader->priv;
Packit 979760
Packit 979760
        if (image_type)
Packit 979760
                {
Packit 979760
                        priv->image_module = _gdk_pixbuf_get_named_module (image_type,
Packit 979760
                                                                           error);
Packit 979760
                }
Packit 979760
        else
Packit 979760
                {
Packit 979760
                        priv->image_module = _gdk_pixbuf_get_module (priv->header_buf,
Packit 979760
                                                                     priv->header_buf_offset,
Packit 979760
                                                                     priv->filename,
Packit 979760
                                                                     error);
Packit 979760
                }
Packit 979760
  
Packit 979760
        if (priv->image_module == NULL)
Packit 979760
                return 0;
Packit 979760
  
Packit 979760
        if (!_gdk_pixbuf_load_module (priv->image_module, error))
Packit 979760
                return 0;
Packit 979760
  
Packit 979760
        if (priv->image_module->module == NULL)
Packit 979760
                return 0;
Packit 979760
  
Packit 979760
        if ((priv->image_module->begin_load == NULL) ||
Packit 979760
            (priv->image_module->stop_load == NULL) ||
Packit 979760
            (priv->image_module->load_increment == NULL))
Packit 979760
                {
Packit 979760
                        g_set_error (error,
Packit 979760
                                     GDK_PIXBUF_ERROR,
Packit 979760
                                     GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
Packit 979760
                                     _("Incremental loading of image type “%s” is not supported"),
Packit 979760
                                     priv->image_module->module_name);
Packit 979760
Packit 979760
                        return 0;
Packit 979760
                }
Packit 979760
Packit 979760
        priv->context = priv->image_module->begin_load (gdk_pixbuf_loader_size_func,
Packit 979760
                                                        gdk_pixbuf_loader_prepare,
Packit 979760
                                                        gdk_pixbuf_loader_update,
Packit 979760
                                                        loader,
Packit 979760
                                                        error);
Packit 979760
  
Packit 979760
        if (priv->context == NULL)
Packit 979760
                {
Packit 979760
                        gdk_pixbuf_loader_ensure_error (loader, error);
Packit 979760
                        return 0;
Packit 979760
                }
Packit 979760
  
Packit 979760
        if (priv->header_buf_offset
Packit 979760
            && priv->image_module->load_increment (priv->context, priv->header_buf, priv->header_buf_offset, error))
Packit 979760
                return priv->header_buf_offset;
Packit 979760
  
Packit 979760
        return 0;
Packit 979760
}
Packit 979760
Packit 979760
static int
Packit 979760
gdk_pixbuf_loader_eat_header_write (GdkPixbufLoader *loader,
Packit 979760
				    const guchar    *buf,
Packit 979760
				    gsize            count,
Packit 979760
                                    GError         **error)
Packit 979760
{
Packit 979760
        gint n_bytes;
Packit 979760
        GdkPixbufLoaderPrivate *priv = loader->priv;
Packit 979760
  
Packit 979760
        n_bytes = MIN(SNIFF_BUFFER_SIZE - priv->header_buf_offset, count);
Packit 979760
        memcpy (priv->header_buf + priv->header_buf_offset, buf, n_bytes);
Packit 979760
  
Packit 979760
        priv->header_buf_offset += n_bytes;
Packit 979760
  
Packit 979760
        if (priv->header_buf_offset >= SNIFF_BUFFER_SIZE)
Packit 979760
                {
Packit 979760
                        if (gdk_pixbuf_loader_load_module (loader, NULL, error) == 0)
Packit 979760
                                return 0;
Packit 979760
                }
Packit 979760
  
Packit 979760
        return n_bytes;
Packit 979760
}
Packit 979760
Packit 979760
/**
Packit 979760
 * gdk_pixbuf_loader_write:
Packit 979760
 * @loader: A pixbuf loader.
Packit 979760
 * @buf: (array length=count): Pointer to image data.
Packit 979760
 * @count: Length of the @buf buffer in bytes.
Packit 979760
 * @error: return location for errors
Packit 979760
 *
Packit 979760
 * This will cause a pixbuf loader to parse the next @count bytes of
Packit 979760
 * an image.  It will return %TRUE if the data was loaded successfully,
Packit 979760
 * and %FALSE if an error occurred.  In the latter case, the loader
Packit 979760
 * will be closed, and will not accept further writes. If %FALSE is
Packit 979760
 * returned, @error will be set to an error from the #GDK_PIXBUF_ERROR
Packit 979760
 * or #G_FILE_ERROR domains.
Packit 979760
 *
Packit 979760
 * Return value: %TRUE if the write was successful, or %FALSE if the loader
Packit 979760
 * cannot parse the buffer.
Packit 979760
 **/
Packit 979760
gboolean
Packit 979760
gdk_pixbuf_loader_write (GdkPixbufLoader *loader,
Packit 979760
			 const guchar    *buf,
Packit 979760
			 gsize            count,
Packit 979760
                         GError         **error)
Packit 979760
{
Packit 979760
        GdkPixbufLoaderPrivate *priv;
Packit 979760
  
Packit 979760
        g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
Packit 979760
  
Packit 979760
        g_return_val_if_fail (buf != NULL, FALSE);
Packit 979760
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
Packit 979760
  
Packit 979760
        priv = loader->priv;
Packit 979760
Packit 979760
        /* we expect it's not to be closed */
Packit 979760
        g_return_val_if_fail (priv->closed == FALSE, FALSE);
Packit 979760
  
Packit 979760
        if (count > 0 && priv->image_module == NULL)
Packit 979760
                {
Packit 979760
                        gint eaten;
Packit 979760
      
Packit 979760
                        eaten = gdk_pixbuf_loader_eat_header_write (loader, buf, count, error);
Packit 979760
                        if (eaten <= 0)
Packit 979760
                               goto fail; 
Packit 979760
      
Packit 979760
                        count -= eaten;
Packit 979760
                        buf += eaten;
Packit 979760
                }
Packit 979760
  
Packit 979760
        /* By this point, we expect the image_module to have been loaded. */
Packit 979760
        g_assert (count == 0 || priv->image_module != NULL);
Packit 979760
Packit 979760
        if (count > 0 && priv->image_module->load_increment != NULL)
Packit 979760
                {
Packit 979760
                        if (!priv->image_module->load_increment (priv->context, buf, count,
Packit 979760
                                                                 error))
Packit 979760
				goto fail;
Packit 979760
                }
Packit 979760
      
Packit 979760
        return TRUE;
Packit 979760
Packit 979760
 fail:
Packit 979760
        gdk_pixbuf_loader_ensure_error (loader, error);
Packit 979760
        gdk_pixbuf_loader_close (loader, NULL);
Packit 979760
Packit 979760
        return FALSE;
Packit 979760
}
Packit 979760
Packit 979760
/**
Packit 979760
 * gdk_pixbuf_loader_write_bytes:
Packit 979760
 * @loader: A pixbuf loader.
Packit 979760
 * @buffer: The image data as a #GBytes
Packit 979760
 * @error: return location for errors
Packit 979760
 *
Packit 979760
 * This will cause a pixbuf loader to parse a buffer inside a #GBytes
Packit 979760
 * for an image.  It will return %TRUE if the data was loaded successfully,
Packit 979760
 * and %FALSE if an error occurred.  In the latter case, the loader
Packit 979760
 * will be closed, and will not accept further writes. If %FALSE is
Packit 979760
 * returned, @error will be set to an error from the #GDK_PIXBUF_ERROR
Packit 979760
 * or #G_FILE_ERROR domains.
Packit 979760
 *
Packit 979760
 * See also: gdk_pixbuf_loader_write()
Packit 979760
 *
Packit 979760
 * Return value: %TRUE if the write was successful, or %FALSE if the loader
Packit 979760
 * cannot parse the buffer.
Packit 979760
 *
Packit 979760
 * Since: 2.30
Packit 979760
 */
Packit 979760
gboolean
Packit 979760
gdk_pixbuf_loader_write_bytes (GdkPixbufLoader *loader,
Packit 979760
                               GBytes          *buffer,
Packit 979760
                               GError         **error)
Packit 979760
{
Packit 979760
        g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
Packit 979760
Packit 979760
        g_return_val_if_fail (buffer != NULL, FALSE);
Packit 979760
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
Packit 979760
Packit 979760
        return gdk_pixbuf_loader_write (loader,
Packit 979760
                                        g_bytes_get_data (buffer, NULL),
Packit 979760
                                        g_bytes_get_size (buffer),
Packit 979760
                                        error);
Packit 979760
}
Packit 979760
Packit 979760
/**
Packit 979760
 * gdk_pixbuf_loader_new:
Packit 979760
 *
Packit 979760
 * Creates a new pixbuf loader object.
Packit 979760
 *
Packit 979760
 * Return value: A newly-created pixbuf loader.
Packit 979760
 **/
Packit 979760
GdkPixbufLoader *
Packit 979760
gdk_pixbuf_loader_new (void)
Packit 979760
{
Packit 979760
        return g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
Packit 979760
}
Packit 979760
Packit 979760
/**
Packit 979760
 * gdk_pixbuf_loader_new_with_type:
Packit 979760
 * @image_type: name of the image format to be loaded with the image
Packit 979760
 * @error: (allow-none): return location for an allocated #GError, or %NULL to ignore errors
Packit 979760
 *
Packit 979760
 * Creates a new pixbuf loader object that always attempts to parse
Packit 979760
 * image data as if it were an image of type @image_type, instead of
Packit 979760
 * identifying the type automatically. Useful if you want an error if
Packit 979760
 * the image isn't the expected type, for loading image formats
Packit 979760
 * that can't be reliably identified by looking at the data, or if
Packit 979760
 * the user manually forces a specific type.
Packit 979760
 *
Packit 979760
 * The list of supported image formats depends on what image loaders
Packit 979760
 * are installed, but typically "png", "jpeg", "gif", "tiff" and 
Packit 979760
 * "xpm" are among the supported formats. To obtain the full list of
Packit 979760
 * supported image formats, call gdk_pixbuf_format_get_name() on each 
Packit 979760
 * of the #GdkPixbufFormat structs returned by gdk_pixbuf_get_formats().
Packit 979760
 *
Packit 979760
 * Return value: A newly-created pixbuf loader.
Packit 979760
 **/
Packit 979760
GdkPixbufLoader *
Packit 979760
gdk_pixbuf_loader_new_with_type (const char *image_type,
Packit 979760
                                 GError    **error)
Packit 979760
{
Packit 979760
        GdkPixbufLoader *retval;
Packit 979760
        GError *tmp;
Packit 979760
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Packit 979760
  
Packit 979760
        retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
Packit 979760
Packit 979760
        tmp = NULL;
Packit 979760
        gdk_pixbuf_loader_load_module (retval, image_type, &tmp);
Packit 979760
        if (tmp != NULL)
Packit 979760
                {
Packit 979760
                        g_propagate_error (error, tmp);
Packit 979760
                        gdk_pixbuf_loader_close (retval, NULL);
Packit 979760
                        g_object_unref (retval);
Packit 979760
                        return NULL;
Packit 979760
                }
Packit 979760
Packit 979760
        return retval;
Packit 979760
}
Packit 979760
Packit 979760
/**
Packit 979760
 * gdk_pixbuf_loader_new_with_mime_type:
Packit 979760
 * @mime_type: the mime type to be loaded 
Packit 979760
 * @error: (allow-none): return location for an allocated #GError, or %NULL to ignore errors
Packit 979760
 *
Packit 979760
 * Creates a new pixbuf loader object that always attempts to parse
Packit 979760
 * image data as if it were an image of mime type @mime_type, instead of
Packit 979760
 * identifying the type automatically. Useful if you want an error if
Packit 979760
 * the image isn't the expected mime type, for loading image formats
Packit 979760
 * that can't be reliably identified by looking at the data, or if
Packit 979760
 * the user manually forces a specific mime type.
Packit 979760
 *
Packit 979760
 * The list of supported mime types depends on what image loaders
Packit 979760
 * are installed, but typically "image/png", "image/jpeg", "image/gif", 
Packit 979760
 * "image/tiff" and "image/x-xpixmap" are among the supported mime types. 
Packit 979760
 * To obtain the full list of supported mime types, call 
Packit 979760
 * gdk_pixbuf_format_get_mime_types() on each of the #GdkPixbufFormat 
Packit 979760
 * structs returned by gdk_pixbuf_get_formats().
Packit 979760
 *
Packit 979760
 * Return value: A newly-created pixbuf loader.
Packit 979760
 * Since: 2.4
Packit 979760
 **/
Packit 979760
GdkPixbufLoader *
Packit 979760
gdk_pixbuf_loader_new_with_mime_type (const char *mime_type,
Packit 979760
                                      GError    **error)
Packit 979760
{
Packit 979760
        const char * image_type = NULL;
Packit 979760
        char ** mimes;
Packit 979760
Packit 979760
        GdkPixbufLoader *retval;
Packit 979760
        GError *tmp;
Packit 979760
  
Packit 979760
        GSList * formats;
Packit 979760
        GdkPixbufFormat *info;
Packit 979760
        int i, j, length;
Packit 979760
Packit 979760
        formats = gdk_pixbuf_get_formats ();
Packit 979760
        length = g_slist_length (formats);
Packit 979760
Packit 979760
        for (i = 0; i < length && image_type == NULL; i++) {
Packit 979760
                info = (GdkPixbufFormat *)g_slist_nth_data (formats, i);
Packit 979760
                mimes = info->mime_types;
Packit 979760
                
Packit 979760
                for (j = 0; mimes[j] != NULL; j++)
Packit 979760
                        if (g_ascii_strcasecmp (mimes[j], mime_type) == 0) {
Packit 979760
                                image_type = info->name;
Packit 979760
                                break;
Packit 979760
                        }
Packit 979760
        }
Packit 979760
Packit 979760
        g_slist_free (formats);
Packit 979760
Packit 979760
        retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
Packit 979760
Packit 979760
        tmp = NULL;
Packit 979760
        gdk_pixbuf_loader_load_module (retval, image_type, &tmp);
Packit 979760
        if (tmp != NULL)
Packit 979760
                {
Packit 979760
                        g_propagate_error (error, tmp);
Packit 979760
                        gdk_pixbuf_loader_close (retval, NULL);
Packit 979760
                        g_object_unref (retval);
Packit 979760
                        return NULL;
Packit 979760
                }
Packit 979760
Packit 979760
        return retval;
Packit 979760
}
Packit 979760
Packit 979760
GdkPixbufLoader *
Packit 979760
_gdk_pixbuf_loader_new_with_filename (const char *filename)
Packit 979760
{
Packit 979760
	GdkPixbufLoader *retval;
Packit 979760
        GdkPixbufLoaderPrivate *priv;
Packit 979760
Packit 979760
        retval = g_object_new (GDK_TYPE_PIXBUF_LOADER, NULL);
Packit 979760
	priv = retval->priv;
Packit 979760
	priv->filename = g_strdup (filename);
Packit 979760
Packit 979760
	return retval;
Packit 979760
}
Packit 979760
Packit 979760
/**
Packit 979760
 * gdk_pixbuf_loader_get_pixbuf:
Packit 979760
 * @loader: A pixbuf loader.
Packit 979760
 *
Packit 979760
 * Queries the #GdkPixbuf that a pixbuf loader is currently creating.
Packit 979760
 * In general it only makes sense to call this function after the
Packit 979760
 * "area-prepared" signal has been emitted by the loader; this means
Packit 979760
 * that enough data has been read to know the size of the image that
Packit 979760
 * will be allocated.  If the loader has not received enough data via
Packit 979760
 * gdk_pixbuf_loader_write(), then this function returns %NULL.  The
Packit 979760
 * returned pixbuf will be the same in all future calls to the loader,
Packit 979760
 * so simply calling g_object_ref() should be sufficient to continue
Packit 979760
 * using it.  Additionally, if the loader is an animation, it will
Packit 979760
 * return the "static image" of the animation
Packit 979760
 * (see gdk_pixbuf_animation_get_static_image()).
Packit 979760
 * 
Packit 979760
 * Return value: (transfer none): The #GdkPixbuf that the loader is creating, or %NULL if not
Packit 979760
 * enough data has been read to determine how to create the image buffer.
Packit 979760
 **/
Packit 979760
GdkPixbuf *
Packit 979760
gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
Packit 979760
{
Packit 979760
        GdkPixbufLoaderPrivate *priv;
Packit 979760
  
Packit 979760
        g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
Packit 979760
  
Packit 979760
        priv = loader->priv;
Packit 979760
Packit 979760
        if (priv->animation)
Packit 979760
                return gdk_pixbuf_animation_get_static_image (priv->animation);
Packit 979760
        else
Packit 979760
                return NULL;
Packit 979760
}
Packit 979760
Packit 979760
/**
Packit 979760
 * gdk_pixbuf_loader_get_animation:
Packit 979760
 * @loader: A pixbuf loader
Packit 979760
 *
Packit 979760
 * Queries the #GdkPixbufAnimation that a pixbuf loader is currently creating.
Packit 979760
 * In general it only makes sense to call this function after the "area-prepared"
Packit 979760
 * signal has been emitted by the loader. If the loader doesn't have enough
Packit 979760
 * bytes yet (hasn't emitted the "area-prepared" signal) this function will 
Packit 979760
 * return %NULL.
Packit 979760
 *
Packit 979760
 * Return value: (transfer none): The #GdkPixbufAnimation that the loader is loading, or %NULL if
Packit 979760
 * not enough data has been read to determine the information.
Packit 979760
**/
Packit 979760
GdkPixbufAnimation *
Packit 979760
gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
Packit 979760
{
Packit 979760
        GdkPixbufLoaderPrivate *priv;
Packit 979760
  
Packit 979760
        g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
Packit 979760
  
Packit 979760
        priv = loader->priv;
Packit 979760
  
Packit 979760
        return priv->animation;
Packit 979760
}
Packit 979760
Packit 979760
/**
Packit 979760
 * gdk_pixbuf_loader_close:
Packit 979760
 * @loader: A pixbuf loader.
Packit 979760
 * @error: (allow-none): return location for a #GError, or %NULL to ignore errors
Packit 979760
 *
Packit 979760
 * Informs a pixbuf loader that no further writes with
Packit 979760
 * gdk_pixbuf_loader_write() will occur, so that it can free its
Packit 979760
 * internal loading structures. Also, tries to parse any data that
Packit 979760
 * hasn't yet been parsed; if the remaining data is partial or
Packit 979760
 * corrupt, an error will be returned.  If %FALSE is returned, @error
Packit 979760
 * will be set to an error from the #GDK_PIXBUF_ERROR or #G_FILE_ERROR
Packit 979760
 * domains. If you're just cancelling a load rather than expecting it
Packit 979760
 * to be finished, passing %NULL for @error to ignore it is
Packit 979760
 * reasonable.
Packit 979760
 *
Packit 979760
 * Remember that this does not unref the loader, so if you plan not to
Packit 979760
 * use it anymore, please g_object_unref() it.
Packit 979760
 *
Packit 979760
 * Returns: %TRUE if all image data written so far was successfully
Packit 979760
            passed out via the update_area signal
Packit 979760
 **/
Packit 979760
gboolean
Packit 979760
gdk_pixbuf_loader_close (GdkPixbufLoader *loader,
Packit 979760
                         GError         **error)
Packit 979760
{
Packit 979760
        GdkPixbufLoaderPrivate *priv;
Packit 979760
        gboolean retval = TRUE;
Packit 979760
  
Packit 979760
        g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), TRUE);
Packit 979760
        g_return_val_if_fail (error == NULL || *error == NULL, TRUE);
Packit 979760
  
Packit 979760
        priv = loader->priv;
Packit 979760
  
Packit 979760
        if (priv->closed)
Packit 979760
                return TRUE;
Packit 979760
  
Packit 979760
        /* We have less than SNIFF_BUFFER_SIZE bytes in the image.  
Packit 979760
         * Flush it, and keep going. 
Packit 979760
         */
Packit 979760
        if (priv->image_module == NULL)
Packit 979760
                {
Packit 979760
                        GError *tmp = NULL;
Packit 979760
                        gdk_pixbuf_loader_load_module (loader, NULL, &tmp);
Packit 979760
                        if (tmp != NULL)
Packit 979760
                                {
Packit 979760
                                        g_propagate_error (error, tmp);
Packit 979760
                                        retval = FALSE;
Packit 979760
                                }
Packit 979760
                }  
Packit 979760
Packit 979760
        if (priv->image_module && priv->image_module->stop_load && priv->context) 
Packit 979760
                {
Packit 979760
                        GError *tmp = NULL;
Packit 979760
                        if (!priv->image_module->stop_load (priv->context, &tmp) || tmp)
Packit 979760
                                {
Packit 979760
					/* don't call gdk_pixbuf_loader_ensure_error()
Packit 979760
 					 * here, since we might not get an error in the
Packit 979760
 					 * gdk_pixbuf_get_file_info() case
Packit 979760
 					 */
Packit 979760
					if (tmp) {
Packit 979760
						if (error && *error == NULL)
Packit 979760
							g_propagate_error (error, tmp);
Packit 979760
						else
Packit 979760
							g_error_free (tmp);
Packit 979760
					}
Packit 979760
                                        retval = FALSE;
Packit 979760
                                }
Packit 979760
                }
Packit 979760
  
Packit 979760
        priv->closed = TRUE;
Packit 979760
Packit 979760
        if (priv->needs_scale) 
Packit 979760
                {
Packit 979760
Packit 979760
                        g_signal_emit (loader, pixbuf_loader_signals[AREA_PREPARED], 0);
Packit 979760
                        g_signal_emit (loader, pixbuf_loader_signals[AREA_UPDATED], 0, 
Packit 979760
                                       0, 0, priv->width, priv->height);
Packit 979760
                }
Packit 979760
Packit 979760
        
Packit 979760
        g_signal_emit (loader, pixbuf_loader_signals[CLOSED], 0);
Packit 979760
Packit 979760
        return retval;
Packit 979760
}
Packit 979760
Packit 979760
/**
Packit 979760
 * gdk_pixbuf_loader_get_format:
Packit 979760
 * @loader: A pixbuf loader.
Packit 979760
 *
Packit 979760
 * Obtains the available information about the format of the 
Packit 979760
 * currently loading image file.
Packit 979760
 *
Packit 979760
 * Returns: (nullable) (transfer none): A #GdkPixbufFormat or
Packit 979760
 * %NULL. The return value is owned by GdkPixbuf and should not be
Packit 979760
 * freed.
Packit 979760
 * 
Packit 979760
 * Since: 2.2
Packit 979760
 */
Packit 979760
GdkPixbufFormat *
Packit 979760
gdk_pixbuf_loader_get_format (GdkPixbufLoader *loader)
Packit 979760
{
Packit 979760
        GdkPixbufLoaderPrivate *priv;
Packit 979760
  
Packit 979760
        g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
Packit 979760
  
Packit 979760
        priv = loader->priv;
Packit 979760
Packit 979760
        if (priv->image_module)
Packit 979760
                return _gdk_pixbuf_get_format (priv->image_module);
Packit 979760
        else
Packit 979760
                return NULL;
Packit 979760
}