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

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