Blame gdk-pixbuf/gdk-pixbuf.c

Packit a4058c
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
Packit a4058c
/* GdkPixbuf library - Basic memory management
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
 *
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
Packit a4058c
#include <math.h>
Packit a4058c
#include <stdlib.h>
Packit a4058c
#include <string.h>
Packit a4058c
Packit a4058c
#define GDK_PIXBUF_C_COMPILATION
Packit a4058c
#include "gdk-pixbuf-private.h"
Packit a4058c
#include "gdk-pixbuf-features.h"
Packit a4058c
#include "gdk-pixbuf-enum-types.h"
Packit a4058c
Packit a4058c
/* Include the marshallers */
Packit a4058c
#include <glib-object.h>
Packit a4058c
#include <gio/gio.h>
Packit a4058c
#include "gdk-pixbuf-marshal.h"
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * SECTION:creating
Packit a4058c
 * @Short_description: Creating a pixbuf from image data that is already in memory.
Packit a4058c
 * @Title: Image Data in Memory
Packit a4058c
 * @See_also: gdk_pixbuf_finalize().
Packit a4058c
 * 
Packit a4058c
 * The most basic way to create a pixbuf is to wrap an existing pixel
Packit a4058c
 * buffer with a #GdkPixbuf structure.  You can use the
Packit a4058c
 * gdk_pixbuf_new_from_data() function to do this You need to specify
Packit a4058c
 * the destroy notification function that will be called when the
Packit a4058c
 * data buffer needs to be freed; this will happen when a #GdkPixbuf
Packit a4058c
 * is finalized by the reference counting functions If you have a
Packit a4058c
 * chunk of static data compiled into your application, you can pass
Packit a4058c
 * in %NULL as the destroy notification function so that the data
Packit a4058c
 * will not be freed.
Packit a4058c
 * 
Packit a4058c
 * The gdk_pixbuf_new() function can be used as a convenience to
Packit a4058c
 * create a pixbuf with an empty buffer.  This is equivalent to
Packit a4058c
 * allocating a data buffer using malloc() and then wrapping it with
Packit a4058c
 * gdk_pixbuf_new_from_data(). The gdk_pixbuf_new() function will
Packit a4058c
 * compute an optimal rowstride so that rendering can be performed
Packit a4058c
 * with an efficient algorithm.
Packit a4058c
 * 
Packit a4058c
 * As a special case, you can use the gdk_pixbuf_new_from_xpm_data()
Packit a4058c
 * function to create a pixbuf from inline XPM image data.
Packit a4058c
 * 
Packit a4058c
 * You can also copy an existing pixbuf with the gdk_pixbuf_copy()
Packit a4058c
 * function.  This is not the same as just doing a g_object_ref()
Packit a4058c
 * on the old pixbuf; the copy function will actually duplicate the
Packit a4058c
 * pixel data in memory and create a new #GdkPixbuf structure for it.
Packit a4058c
 */
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * SECTION:refcounting
Packit a4058c
 * @Short_description: Functions for reference counting and memory management on pixbufs.
Packit a4058c
 * @Title: Reference Counting and Memory Mangement
Packit a4058c
 * @See_also: #GdkPixbuf, gdk_pixbuf_new_from_data().
Packit a4058c
 * 
Packit a4058c
 * #GdkPixbuf structures are reference counted.  This means that an
Packit a4058c
 * application can share a single pixbuf among many parts of the
Packit a4058c
 * code.  When a piece of the program needs to keep a pointer to a
Packit a4058c
 * pixbuf, it should add a reference to it by calling g_object_ref().
Packit a4058c
 * When it no longer needs the pixbuf, it should subtract a reference
Packit a4058c
 * by calling g_object_unref().  The pixbuf will be destroyed when
Packit a4058c
 * its reference count drops to zero.  Newly-created #GdkPixbuf
Packit a4058c
 * structures start with a reference count of one.
Packit a4058c
 * 
Packit a4058c
 * > As #GdkPixbuf is derived from #GObject now, gdk_pixbuf_ref() and
Packit a4058c
 * > gdk_pixbuf_unref() are deprecated in favour of g_object_ref()
Packit a4058c
 * > and g_object_unref() resp.
Packit a4058c
 * 
Packit a4058c
 * Finalizing a pixbuf means to free its pixel data and to free the
Packit a4058c
 * #GdkPixbuf structure itself.  Most of the library functions that
Packit a4058c
 * create #GdkPixbuf structures create the pixel data by themselves
Packit a4058c
 * and define the way it should be freed; you do not need to worry
Packit a4058c
 * about those.
Packit a4058c
 *
Packit a4058c
 * To provide preallocated pixel data, use
Packit a4058c
 * gdk_pixbuf_new_from_bytes().  The gdk_pixbuf_new_from_data() API is
Packit a4058c
 * an older variant that predates the existence of #GBytes.
Packit a4058c
 */
Packit a4058c
Packit a4058c
static void gdk_pixbuf_finalize     (GObject        *object);
Packit a4058c
static void gdk_pixbuf_set_property (GObject        *object,
Packit a4058c
				     guint           prop_id,
Packit a4058c
				     const GValue   *value,
Packit a4058c
				     GParamSpec     *pspec);
Packit a4058c
static void gdk_pixbuf_get_property (GObject        *object,
Packit a4058c
				     guint           prop_id,
Packit a4058c
				     GValue         *value,
Packit a4058c
				     GParamSpec     *pspec);
Packit a4058c
Packit a4058c

Packit a4058c
enum 
Packit a4058c
{
Packit a4058c
  PROP_0,
Packit a4058c
  PROP_COLORSPACE,
Packit a4058c
  PROP_N_CHANNELS,
Packit a4058c
  PROP_HAS_ALPHA,
Packit a4058c
  PROP_BITS_PER_SAMPLE,
Packit a4058c
  PROP_WIDTH,
Packit a4058c
  PROP_HEIGHT,
Packit a4058c
  PROP_ROWSTRIDE,
Packit a4058c
  PROP_PIXELS,
Packit a4058c
  PROP_PIXEL_BYTES
Packit a4058c
};
Packit a4058c
Packit a4058c
static void gdk_pixbuf_icon_iface_init (GIconIface *iface);
Packit a4058c
static void gdk_pixbuf_loadable_icon_iface_init (GLoadableIconIface *iface);
Packit a4058c
Packit a4058c
G_DEFINE_TYPE_WITH_CODE (GdkPixbuf, gdk_pixbuf, G_TYPE_OBJECT,
Packit a4058c
                         G_IMPLEMENT_INTERFACE (G_TYPE_ICON, gdk_pixbuf_icon_iface_init)
Packit a4058c
                         G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON, gdk_pixbuf_loadable_icon_iface_init))
Packit a4058c
Packit a4058c
static void 
Packit a4058c
gdk_pixbuf_init (GdkPixbuf *pixbuf)
Packit a4058c
{
Packit a4058c
  pixbuf->colorspace = GDK_COLORSPACE_RGB;
Packit a4058c
  pixbuf->n_channels = 3;
Packit a4058c
  pixbuf->bits_per_sample = 8;
Packit a4058c
  pixbuf->has_alpha = FALSE;
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
gdk_pixbuf_class_init (GdkPixbufClass *klass)
Packit a4058c
{
Packit a4058c
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
Packit a4058c
Packit a4058c
        _gdk_pixbuf_init_gettext ();
Packit a4058c
Packit a4058c
        object_class->finalize = gdk_pixbuf_finalize;
Packit a4058c
        object_class->set_property = gdk_pixbuf_set_property;
Packit a4058c
        object_class->get_property = gdk_pixbuf_get_property;
Packit a4058c
Packit a4058c
#define PIXBUF_PARAM_FLAGS G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY|\
Packit a4058c
                           G_PARAM_EXPLICIT_NOTIFY|\
Packit a4058c
                           G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
Packit a4058c
        /**
Packit a4058c
         * GdkPixbuf:n-channels:
Packit a4058c
         *
Packit a4058c
         * The number of samples per pixel. 
Packit a4058c
         * Currently, only 3 or 4 samples per pixel are supported.
Packit a4058c
         */
Packit a4058c
        g_object_class_install_property (object_class,
Packit a4058c
                                         PROP_N_CHANNELS,
Packit a4058c
                                         g_param_spec_int ("n-channels",
Packit a4058c
                                                           _("Number of Channels"),
Packit a4058c
                                                           _("The number of samples per pixel"),
Packit a4058c
                                                           0,
Packit a4058c
                                                           G_MAXINT,
Packit a4058c
                                                           3,
Packit a4058c
                                                           PIXBUF_PARAM_FLAGS));
Packit a4058c
Packit a4058c
        g_object_class_install_property (object_class,
Packit a4058c
                                         PROP_COLORSPACE,
Packit a4058c
                                         g_param_spec_enum ("colorspace",
Packit a4058c
                                                            _("Colorspace"),
Packit a4058c
                                                            _("The colorspace in which the samples are interpreted"),
Packit a4058c
                                                            GDK_TYPE_COLORSPACE,
Packit a4058c
                                                            GDK_COLORSPACE_RGB,
Packit a4058c
                                                            PIXBUF_PARAM_FLAGS));
Packit a4058c
Packit a4058c
        g_object_class_install_property (object_class,
Packit a4058c
                                         PROP_HAS_ALPHA,
Packit a4058c
                                         g_param_spec_boolean ("has-alpha",
Packit a4058c
                                                               _("Has Alpha"),
Packit a4058c
                                                               _("Whether the pixbuf has an alpha channel"),
Packit a4058c
                                                               FALSE,
Packit a4058c
                                                               PIXBUF_PARAM_FLAGS));
Packit a4058c
Packit a4058c
        /**
Packit a4058c
         * GdkPixbuf:bits-per-sample:
Packit a4058c
         *
Packit a4058c
         * The number of bits per sample. 
Packit a4058c
         * Currently only 8 bit per sample are supported.
Packit a4058c
         */
Packit a4058c
        g_object_class_install_property (object_class,
Packit a4058c
                                         PROP_BITS_PER_SAMPLE,
Packit a4058c
                                         g_param_spec_int ("bits-per-sample",
Packit a4058c
                                                           _("Bits per Sample"),
Packit a4058c
                                                           _("The number of bits per sample"),
Packit a4058c
                                                           1,
Packit a4058c
                                                           16,
Packit a4058c
                                                           8,
Packit a4058c
                                                           PIXBUF_PARAM_FLAGS));
Packit a4058c
Packit a4058c
        g_object_class_install_property (object_class,
Packit a4058c
                                         PROP_WIDTH,
Packit a4058c
                                         g_param_spec_int ("width",
Packit a4058c
                                                           _("Width"),
Packit a4058c
                                                           _("The number of columns of the pixbuf"),
Packit a4058c
                                                           1,
Packit a4058c
                                                           G_MAXINT,
Packit a4058c
                                                           1,
Packit a4058c
                                                           PIXBUF_PARAM_FLAGS));
Packit a4058c
Packit a4058c
        g_object_class_install_property (object_class,
Packit a4058c
                                         PROP_HEIGHT,
Packit a4058c
                                         g_param_spec_int ("height",
Packit a4058c
                                                           _("Height"),
Packit a4058c
                                                           _("The number of rows of the pixbuf"),
Packit a4058c
                                                           1,
Packit a4058c
                                                           G_MAXINT,
Packit a4058c
                                                           1,
Packit a4058c
                                                           PIXBUF_PARAM_FLAGS));
Packit a4058c
Packit a4058c
        /**
Packit a4058c
         * GdkPixbuf:rowstride:
Packit a4058c
         *
Packit a4058c
         * The number of bytes between the start of a row and 
Packit a4058c
         * the start of the next row. This number must (obviously)
Packit a4058c
         * be at least as large as the width of the pixbuf.
Packit a4058c
         */
Packit a4058c
        g_object_class_install_property (object_class,
Packit a4058c
                                         PROP_ROWSTRIDE,
Packit a4058c
                                         g_param_spec_int ("rowstride",
Packit a4058c
                                                           _("Rowstride"),
Packit a4058c
                                                           _("The number of bytes between the start of a row and the start of the next row"),
Packit a4058c
                                                           1,
Packit a4058c
                                                           G_MAXINT,
Packit a4058c
                                                           1,
Packit a4058c
                                                           PIXBUF_PARAM_FLAGS));
Packit a4058c
Packit a4058c
        g_object_class_install_property (object_class,
Packit a4058c
                                         PROP_PIXELS,
Packit a4058c
                                         g_param_spec_pointer ("pixels",
Packit a4058c
                                                               _("Pixels"),
Packit a4058c
                                                               _("A pointer to the pixel data of the pixbuf"),
Packit a4058c
                                                               PIXBUF_PARAM_FLAGS));
Packit a4058c
Packit a4058c
        /**
Packit a4058c
         * GdkPixbuf::pixel-bytes:
Packit a4058c
         *
Packit a4058c
         * If set, this pixbuf was created from read-only #GBytes.
Packit a4058c
         * Replaces GdkPixbuf::pixels.
Packit a4058c
         * 
Packit a4058c
         * Since: 2.32
Packit a4058c
         */
Packit a4058c
        g_object_class_install_property (object_class,
Packit a4058c
                                         PROP_PIXEL_BYTES,
Packit a4058c
                                         g_param_spec_boxed ("pixel-bytes",
Packit a4058c
                                                             _("Pixel Bytes"),
Packit a4058c
                                                             _("Readonly pixel data"),
Packit a4058c
                                                             G_TYPE_BYTES,
Packit a4058c
                                                             PIXBUF_PARAM_FLAGS));
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
gdk_pixbuf_finalize (GObject *object)
Packit a4058c
{
Packit a4058c
        GdkPixbuf *pixbuf = GDK_PIXBUF (object);
Packit a4058c
        
Packit a4058c
        if (pixbuf->pixels && pixbuf->destroy_fn)
Packit a4058c
                (* pixbuf->destroy_fn) (pixbuf->pixels, pixbuf->destroy_fn_data);
Packit a4058c
Packit a4058c
        g_clear_pointer (&pixbuf->bytes, g_bytes_unref);
Packit a4058c
        
Packit a4058c
        G_OBJECT_CLASS (gdk_pixbuf_parent_class)->finalize (object);
Packit a4058c
}
Packit a4058c
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_ref: (skip)
Packit a4058c
 * @pixbuf: A pixbuf.
Packit a4058c
 *
Packit a4058c
 * Adds a reference to a pixbuf.
Packit a4058c
 *
Packit a4058c
 * Return value: The same as the @pixbuf argument.
Packit a4058c
 *
Packit a4058c
 * Deprecated: 2.0: Use g_object_ref().
Packit a4058c
 **/
Packit a4058c
GdkPixbuf *
Packit a4058c
gdk_pixbuf_ref (GdkPixbuf *pixbuf)
Packit a4058c
{
Packit a4058c
        return (GdkPixbuf *) g_object_ref (pixbuf);
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_unref: (skip)
Packit a4058c
 * @pixbuf: A pixbuf.
Packit a4058c
 *
Packit a4058c
 * Removes a reference from a pixbuf.
Packit a4058c
 *
Packit a4058c
 * Deprecated: 2.0: Use g_object_unref().
Packit a4058c
 **/
Packit a4058c
void
Packit a4058c
gdk_pixbuf_unref (GdkPixbuf *pixbuf)
Packit a4058c
{
Packit a4058c
        g_object_unref (pixbuf);
Packit a4058c
}
Packit a4058c
Packit a4058c
static GBytes *
Packit a4058c
gdk_pixbuf_make_bytes (GdkPixbuf  *pixbuf,
Packit a4058c
                       GError    **error)
Packit a4058c
{
Packit a4058c
  gchar *buffer;
Packit a4058c
  gsize size;
Packit a4058c
Packit a4058c
  if (!gdk_pixbuf_save_to_buffer (pixbuf, &buffer, &size, "png", error, NULL))
Packit a4058c
    return NULL;
Packit a4058c
Packit a4058c
  return g_bytes_new_take (buffer, size);
Packit a4058c
}
Packit a4058c
Packit a4058c
static GVariant *
Packit a4058c
gdk_pixbuf_serialize (GIcon *icon)
Packit a4058c
{
Packit a4058c
  GError *error = NULL;
Packit a4058c
  GVariant *result;
Packit a4058c
  GBytes *bytes;
Packit a4058c
Packit a4058c
  bytes = gdk_pixbuf_make_bytes (GDK_PIXBUF (icon), &error);
Packit a4058c
  if (!bytes)
Packit a4058c
    {
Packit a4058c
      g_critical ("Unable to serialise GdkPixbuf to png (via g_icon_serialize()): %s", error->message);
Packit a4058c
      g_error_free (error);
Packit a4058c
      return NULL;
Packit a4058c
    }
Packit a4058c
  result = g_variant_new_from_bytes (G_VARIANT_TYPE_BYTESTRING, bytes, TRUE);
Packit a4058c
  g_bytes_unref (bytes);
Packit a4058c
Packit a4058c
  return g_variant_new ("(sv)", "bytes", result);
Packit a4058c
}
Packit a4058c
Packit a4058c
static GInputStream *
Packit a4058c
gdk_pixbuf_load (GLoadableIcon  *icon,
Packit a4058c
                 int             size,
Packit a4058c
                 char          **type,
Packit a4058c
                 GCancellable   *cancellable,
Packit a4058c
                 GError        **error)
Packit a4058c
{
Packit a4058c
  GInputStream *stream;
Packit a4058c
  GBytes *bytes;
Packit a4058c
Packit a4058c
  bytes = gdk_pixbuf_make_bytes (GDK_PIXBUF (icon), error);
Packit a4058c
  if (!bytes)
Packit a4058c
    return NULL;
Packit a4058c
Packit a4058c
  stream = g_memory_input_stream_new_from_bytes (bytes);
Packit a4058c
  g_bytes_unref (bytes);
Packit a4058c
Packit a4058c
  if (type)
Packit a4058c
    *type = g_strdup ("image/png");
Packit a4058c
Packit a4058c
  return stream;
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
gdk_pixbuf_load_async (GLoadableIcon       *icon,
Packit a4058c
                       int                  size,
Packit a4058c
                       GCancellable        *cancellable,
Packit a4058c
                       GAsyncReadyCallback  callback,
Packit a4058c
                       gpointer             user_data)
Packit a4058c
{
Packit a4058c
  GTask *task;
Packit a4058c
Packit a4058c
  task = g_task_new (icon, cancellable, callback, user_data);
Packit a4058c
  g_task_return_pointer (task, icon, NULL);
Packit a4058c
  g_object_unref (task);
Packit a4058c
}
Packit a4058c
Packit a4058c
static GInputStream *
Packit a4058c
gdk_pixbuf_load_finish (GLoadableIcon  *icon,
Packit a4058c
                        GAsyncResult   *res,
Packit a4058c
                        char          **type,
Packit a4058c
                        GError        **error)
Packit a4058c
{
Packit a4058c
  g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
Packit a4058c
Packit a4058c
  if (!g_task_propagate_pointer (G_TASK (res), error))
Packit a4058c
    return NULL;
Packit a4058c
Packit a4058c
  return gdk_pixbuf_load (icon, 0, type, NULL, error);
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
gdk_pixbuf_loadable_icon_iface_init (GLoadableIconIface *iface)
Packit a4058c
{
Packit a4058c
  iface->load = gdk_pixbuf_load;
Packit a4058c
Packit a4058c
  /* In theory encoding a png could be time-consuming but we're talking
Packit a4058c
   * about icons here, so assume it's probably going to be OK and handle
Packit a4058c
   * the async variant of the call in-thread instead of having the
Packit a4058c
   * default implementation dispatch it to a worker.
Packit a4058c
   */
Packit a4058c
  iface->load_async = gdk_pixbuf_load_async;
Packit a4058c
  iface->load_finish = gdk_pixbuf_load_finish;
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
gdk_pixbuf_icon_iface_init (GIconIface *iface)
Packit a4058c
{
Packit a4058c
        iface->hash = (guint (*) (GIcon *)) g_direct_hash;
Packit a4058c
        iface->equal = (gboolean (*) (GIcon *, GIcon *)) g_direct_equal;
Packit a4058c
        iface->serialize = gdk_pixbuf_serialize;
Packit a4058c
}
Packit a4058c
Packit a4058c
/* Used as the destroy notification function for gdk_pixbuf_new() */
Packit a4058c
static void
Packit a4058c
free_buffer (guchar *pixels, gpointer data)
Packit a4058c
{
Packit a4058c
	g_free (pixels);
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_calculate_rowstride:
Packit a4058c
 * @colorspace: Color space for image
Packit a4058c
 * @has_alpha: Whether the image should have transparency information
Packit a4058c
 * @bits_per_sample: Number of bits per color sample
Packit a4058c
 * @width: Width of image in pixels, must be > 0
Packit a4058c
 * @height: Height of image in pixels, must be > 0
Packit a4058c
 *
Packit a4058c
 * Calculates the rowstride that an image created with those values would
Packit a4058c
 * have. This is useful for front-ends and backends that want to sanity
Packit a4058c
 * check image values without needing to create them.
Packit a4058c
 *
Packit a4058c
 * Return value: the rowstride for the given values, or -1 in case of error.
Packit a4058c
 *
Packit a4058c
 * Since: 2.36.8
Packit a4058c
 */
Packit a4058c
gint
Packit a4058c
gdk_pixbuf_calculate_rowstride (GdkColorspace colorspace,
Packit a4058c
				gboolean      has_alpha,
Packit a4058c
				int           bits_per_sample,
Packit a4058c
				int           width,
Packit a4058c
				int           height)
Packit a4058c
{
Packit a4058c
	unsigned int channels;
Packit a4058c
Packit a4058c
	g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, -1);
Packit a4058c
	g_return_val_if_fail (bits_per_sample == 8, -1);
Packit a4058c
	g_return_val_if_fail (width > 0, -1);
Packit a4058c
	g_return_val_if_fail (height > 0, -1);
Packit a4058c
Packit a4058c
	channels = has_alpha ? 4 : 3;
Packit a4058c
Packit a4058c
	/* Overflow? */
Packit a4058c
	if (width > (G_MAXINT - 3) / channels)
Packit a4058c
		return -1;
Packit a4058c
Packit a4058c
	/* Always align rows to 32-bit boundaries */
Packit a4058c
	return (width * channels + 3) & ~3;
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_new:
Packit a4058c
 * @colorspace: Color space for image
Packit a4058c
 * @has_alpha: Whether the image should have transparency information
Packit a4058c
 * @bits_per_sample: Number of bits per color sample
Packit a4058c
 * @width: Width of image in pixels, must be > 0
Packit a4058c
 * @height: Height of image in pixels, must be > 0
Packit a4058c
 *
Packit a4058c
 * Creates a new #GdkPixbuf structure and allocates a buffer for it.  The 
Packit a4058c
 * buffer has an optimal rowstride.  Note that the buffer is not cleared;
Packit a4058c
 * you will have to fill it completely yourself.
Packit a4058c
 *
Packit a4058c
 * Return value: A newly-created #GdkPixbuf with a reference count of 1, or 
Packit a4058c
 * %NULL if not enough memory could be allocated for the image buffer.
Packit a4058c
 **/
Packit a4058c
GdkPixbuf *
Packit a4058c
gdk_pixbuf_new (GdkColorspace colorspace, 
Packit a4058c
                gboolean      has_alpha,
Packit a4058c
                int           bits_per_sample,
Packit a4058c
                int           width,
Packit a4058c
                int           height)
Packit a4058c
{
Packit a4058c
	guchar *buf;
Packit a4058c
	int rowstride;
Packit a4058c
Packit a4058c
	rowstride = gdk_pixbuf_calculate_rowstride (colorspace,
Packit a4058c
						    has_alpha,
Packit a4058c
						    bits_per_sample,
Packit a4058c
						    width,
Packit a4058c
						    height);
Packit a4058c
	if (rowstride <= 0)
Packit a4058c
		return NULL;
Packit a4058c
Packit a4058c
	buf = g_try_malloc_n (height, rowstride);
Packit a4058c
	if (!buf)
Packit a4058c
		return NULL;
Packit a4058c
Packit a4058c
	return gdk_pixbuf_new_from_data (buf, colorspace, has_alpha, bits_per_sample,
Packit a4058c
					 width, height, rowstride,
Packit a4058c
					 free_buffer, NULL);
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_copy:
Packit a4058c
 * @pixbuf: A pixbuf.
Packit a4058c
 * 
Packit a4058c
 * Creates a new #GdkPixbuf with a copy of the information in the specified
Packit a4058c
 * @pixbuf. Note that this does not copy the options set on the original #GdkPixbuf,
Packit a4058c
 * use gdk_pixbuf_copy_options() for this.
Packit a4058c
 * 
Packit a4058c
 * Return value: (transfer full): A newly-created pixbuf with a reference count of 1, or %NULL if
Packit a4058c
 * not enough memory could be allocated.
Packit a4058c
 **/
Packit a4058c
GdkPixbuf *
Packit a4058c
gdk_pixbuf_copy (const GdkPixbuf *pixbuf)
Packit a4058c
{
Packit a4058c
	guchar *buf;
Packit a4058c
	int size;
Packit a4058c
Packit a4058c
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
Packit a4058c
Packit a4058c
	/* Calculate a semi-exact size.  Here we copy with full rowstrides;
Packit a4058c
	 * maybe we should copy each row individually with the minimum
Packit a4058c
	 * rowstride?
Packit a4058c
	 */
Packit a4058c
Packit a4058c
	size = gdk_pixbuf_get_byte_length (pixbuf);
Packit a4058c
Packit a4058c
	buf = g_try_malloc (size);
Packit a4058c
	if (!buf)
Packit a4058c
		return NULL;
Packit a4058c
Packit a4058c
	memcpy (buf, gdk_pixbuf_read_pixels (pixbuf), size);
Packit a4058c
Packit a4058c
	return gdk_pixbuf_new_from_data (buf,
Packit a4058c
					 pixbuf->colorspace, pixbuf->has_alpha,
Packit a4058c
					 pixbuf->bits_per_sample,
Packit a4058c
					 pixbuf->width, pixbuf->height,
Packit a4058c
					 pixbuf->rowstride,
Packit a4058c
					 free_buffer,
Packit a4058c
					 NULL);
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_new_subpixbuf:
Packit a4058c
 * @src_pixbuf: a #GdkPixbuf
Packit a4058c
 * @src_x: X coord in @src_pixbuf
Packit a4058c
 * @src_y: Y coord in @src_pixbuf
Packit a4058c
 * @width: width of region in @src_pixbuf
Packit a4058c
 * @height: height of region in @src_pixbuf
Packit a4058c
 * 
Packit a4058c
 * Creates a new pixbuf which represents a sub-region of @src_pixbuf.
Packit a4058c
 * The new pixbuf shares its pixels with the original pixbuf, so
Packit a4058c
 * writing to one affects both.  The new pixbuf holds a reference to
Packit a4058c
 * @src_pixbuf, so @src_pixbuf will not be finalized until the new
Packit a4058c
 * pixbuf is finalized.
Packit a4058c
 *
Packit a4058c
 * Note that if @src_pixbuf is read-only, this function will force it
Packit a4058c
 * to be mutable.
Packit a4058c
 *
Packit a4058c
 * Return value: (transfer full): a new pixbuf 
Packit a4058c
 **/
Packit a4058c
GdkPixbuf*
Packit a4058c
gdk_pixbuf_new_subpixbuf (GdkPixbuf *src_pixbuf,
Packit a4058c
                          int        src_x,
Packit a4058c
                          int        src_y,
Packit a4058c
                          int        width,
Packit a4058c
                          int        height)
Packit a4058c
{
Packit a4058c
        guchar *pixels;
Packit a4058c
        GdkPixbuf *sub;
Packit a4058c
Packit a4058c
        g_return_val_if_fail (GDK_IS_PIXBUF (src_pixbuf), NULL);
Packit a4058c
        g_return_val_if_fail (src_x >= 0 && src_x + width <= src_pixbuf->width, NULL);
Packit a4058c
        g_return_val_if_fail (src_y >= 0 && src_y + height <= src_pixbuf->height, NULL);
Packit a4058c
        
Packit a4058c
        /* Note causes an implicit copy where src_pixbuf owns the data */
Packit a4058c
        pixels = (gdk_pixbuf_get_pixels (src_pixbuf)
Packit a4058c
                  + src_y * src_pixbuf->rowstride
Packit a4058c
                  + src_x * src_pixbuf->n_channels);
Packit a4058c
Packit a4058c
        sub = gdk_pixbuf_new_from_data (pixels,
Packit a4058c
                                        src_pixbuf->colorspace,
Packit a4058c
                                        src_pixbuf->has_alpha,
Packit a4058c
                                        src_pixbuf->bits_per_sample,
Packit a4058c
                                        width, height,
Packit a4058c
                                        src_pixbuf->rowstride,
Packit a4058c
                                        NULL, NULL);
Packit a4058c
Packit a4058c
        /* Keep a reference to src_pixbuf */
Packit a4058c
        g_object_ref (src_pixbuf);
Packit a4058c
  
Packit a4058c
        g_object_set_qdata_full (G_OBJECT (sub),
Packit a4058c
                                 g_quark_from_static_string ("gdk-pixbuf-subpixbuf-src"),
Packit a4058c
                                 src_pixbuf,
Packit a4058c
                                 (GDestroyNotify) g_object_unref);
Packit a4058c
Packit a4058c
        return sub;
Packit a4058c
}
Packit a4058c
Packit a4058c

Packit a4058c
Packit a4058c
/* Accessors */
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_get_colorspace:
Packit a4058c
 * @pixbuf: A pixbuf.
Packit a4058c
 *
Packit a4058c
 * Queries the color space of a pixbuf.
Packit a4058c
 *
Packit a4058c
 * Return value: Color space.
Packit a4058c
 **/
Packit a4058c
GdkColorspace
Packit a4058c
gdk_pixbuf_get_colorspace (const GdkPixbuf *pixbuf)
Packit a4058c
{
Packit a4058c
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), GDK_COLORSPACE_RGB);
Packit a4058c
Packit a4058c
	return pixbuf->colorspace;
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_get_n_channels:
Packit a4058c
 * @pixbuf: A pixbuf.
Packit a4058c
 *
Packit a4058c
 * Queries the number of channels of a pixbuf.
Packit a4058c
 *
Packit a4058c
 * Return value: Number of channels.
Packit a4058c
 **/
Packit a4058c
int
Packit a4058c
gdk_pixbuf_get_n_channels (const GdkPixbuf *pixbuf)
Packit a4058c
{
Packit a4058c
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
Packit a4058c
Packit a4058c
	return pixbuf->n_channels;
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_get_has_alpha:
Packit a4058c
 * @pixbuf: A pixbuf.
Packit a4058c
 *
Packit a4058c
 * Queries whether a pixbuf has an alpha channel (opacity information).
Packit a4058c
 *
Packit a4058c
 * Return value: %TRUE if it has an alpha channel, %FALSE otherwise.
Packit a4058c
 **/
Packit a4058c
gboolean
Packit a4058c
gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf)
Packit a4058c
{
Packit a4058c
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
Packit a4058c
Packit a4058c
	return pixbuf->has_alpha ? TRUE : FALSE;
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_get_bits_per_sample:
Packit a4058c
 * @pixbuf: A pixbuf.
Packit a4058c
 *
Packit a4058c
 * Queries the number of bits per color sample in a pixbuf.
Packit a4058c
 *
Packit a4058c
 * Return value: Number of bits per color sample.
Packit a4058c
 **/
Packit a4058c
int
Packit a4058c
gdk_pixbuf_get_bits_per_sample (const GdkPixbuf *pixbuf)
Packit a4058c
{
Packit a4058c
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
Packit a4058c
Packit a4058c
	return pixbuf->bits_per_sample;
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_get_pixels:
Packit a4058c
 * @pixbuf: A pixbuf.
Packit a4058c
 *
Packit a4058c
 * Queries a pointer to the pixel data of a pixbuf.
Packit a4058c
 *
Packit a4058c
 * Return value: (array): A pointer to the pixbuf's pixel data.
Packit a4058c
 * Please see the section on [image data](image-data) for information
Packit a4058c
 * about how the pixel data is stored in memory.
Packit a4058c
 *
Packit a4058c
 * This function will cause an implicit copy of the pixbuf data if the
Packit a4058c
 * pixbuf was created from read-only data.
Packit a4058c
 **/
Packit a4058c
guchar *
Packit a4058c
gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)
Packit a4058c
{
Packit a4058c
        return gdk_pixbuf_get_pixels_with_length (pixbuf, NULL);
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_get_pixels_with_length: (rename-to gdk_pixbuf_get_pixels)
Packit a4058c
 * @pixbuf: A pixbuf.
Packit a4058c
 * @length: (out): The length of the binary data.
Packit a4058c
 *
Packit a4058c
 * Queries a pointer to the pixel data of a pixbuf.
Packit a4058c
 *
Packit a4058c
 * Return value: (array length=length): A pointer to the pixbuf's
Packit a4058c
 * pixel data.  Please see the section on [image data](image-data)
Packit a4058c
 * for information about how the pixel data is stored in memory.
Packit a4058c
 *
Packit a4058c
 * This function will cause an implicit copy of the pixbuf data if the
Packit a4058c
 * pixbuf was created from read-only data.
Packit a4058c
 *
Packit a4058c
 * Since: 2.26
Packit a4058c
 */
Packit a4058c
guchar *
Packit a4058c
gdk_pixbuf_get_pixels_with_length (const GdkPixbuf *pixbuf,
Packit a4058c
                                   guint           *length)
Packit a4058c
{
Packit a4058c
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
Packit a4058c
Packit a4058c
        if (pixbuf->bytes) {
Packit a4058c
                GdkPixbuf *mut_pixbuf = (GdkPixbuf*)pixbuf;
Packit a4058c
                gsize len;
Packit a4058c
                mut_pixbuf->pixels = g_bytes_unref_to_data (pixbuf->bytes, &len;;
Packit a4058c
                mut_pixbuf->bytes = NULL;
Packit a4058c
        }
Packit a4058c
Packit a4058c
        if (length)
Packit a4058c
                *length = gdk_pixbuf_get_byte_length (pixbuf);
Packit a4058c
Packit a4058c
	return pixbuf->pixels;
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_read_pixels:
Packit a4058c
 * @pixbuf: A pixbuf
Packit a4058c
 *
Packit a4058c
 * Returns a read-only pointer to the raw pixel data; must not be
Packit a4058c
 * modified.  This function allows skipping the implicit copy that
Packit a4058c
 * must be made if gdk_pixbuf_get_pixels() is called on a read-only
Packit a4058c
 * pixbuf.
Packit a4058c
 *
Packit a4058c
 * Since: 2.32
Packit a4058c
 */
Packit a4058c
const guint8*
Packit a4058c
gdk_pixbuf_read_pixels (const GdkPixbuf  *pixbuf)
Packit a4058c
{
Packit a4058c
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
Packit a4058c
        
Packit a4058c
        if (pixbuf->bytes) {
Packit a4058c
                gsize len;
Packit a4058c
                /* Ignore len; callers know the size via other variables */
Packit a4058c
                return g_bytes_get_data (pixbuf->bytes, &len;;
Packit a4058c
        } else {
Packit a4058c
                return pixbuf->pixels;
Packit a4058c
        }
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_read_pixel_bytes:
Packit a4058c
 * @pixbuf: A pixbuf
Packit a4058c
 *
Packit a4058c
 * Returns: (transfer full): A new reference to a read-only copy of
Packit a4058c
 * the pixel data.  Note that for mutable pixbufs, this function will
Packit a4058c
 * incur a one-time copy of the pixel data for conversion into the
Packit a4058c
 * returned #GBytes.
Packit a4058c
 *
Packit a4058c
 * Since: 2.32
Packit a4058c
 */
Packit a4058c
GBytes *
Packit a4058c
gdk_pixbuf_read_pixel_bytes (const GdkPixbuf  *pixbuf)
Packit a4058c
{
Packit a4058c
        g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
Packit a4058c
Packit a4058c
        if (pixbuf->bytes) {
Packit a4058c
                return g_bytes_ref (pixbuf->bytes);
Packit a4058c
        } else {
Packit a4058c
                return g_bytes_new (pixbuf->pixels,
Packit a4058c
                                    gdk_pixbuf_get_byte_length (pixbuf));
Packit a4058c
        }
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_get_width:
Packit a4058c
 * @pixbuf: A pixbuf.
Packit a4058c
 *
Packit a4058c
 * Queries the width of a pixbuf.
Packit a4058c
 *
Packit a4058c
 * Return value: Width in pixels.
Packit a4058c
 **/
Packit a4058c
int
Packit a4058c
gdk_pixbuf_get_width (const GdkPixbuf *pixbuf)
Packit a4058c
{
Packit a4058c
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
Packit a4058c
Packit a4058c
	return pixbuf->width;
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_get_height:
Packit a4058c
 * @pixbuf: A pixbuf.
Packit a4058c
 *
Packit a4058c
 * Queries the height of a pixbuf.
Packit a4058c
 *
Packit a4058c
 * Return value: Height in pixels.
Packit a4058c
 **/
Packit a4058c
int
Packit a4058c
gdk_pixbuf_get_height (const GdkPixbuf *pixbuf)
Packit a4058c
{
Packit a4058c
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
Packit a4058c
Packit a4058c
	return pixbuf->height;
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_get_rowstride:
Packit a4058c
 * @pixbuf: A pixbuf.
Packit a4058c
 *
Packit a4058c
 * Queries the rowstride of a pixbuf, which is the number of bytes between
Packit a4058c
 * the start of a row and the start of the next row.
Packit a4058c
 *
Packit a4058c
 * Return value: Distance between row starts.
Packit a4058c
 **/
Packit a4058c
int
Packit a4058c
gdk_pixbuf_get_rowstride (const GdkPixbuf *pixbuf)
Packit a4058c
{
Packit a4058c
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
Packit a4058c
Packit a4058c
	return pixbuf->rowstride;
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_get_byte_length:
Packit a4058c
 * @pixbuf: A pixbuf
Packit a4058c
 *
Packit a4058c
 * Returns the length of the pixel data, in bytes.
Packit a4058c
 *
Packit a4058c
 * Return value: The length of the pixel data.
Packit a4058c
 *
Packit a4058c
 * Since: 2.26
Packit a4058c
 */
Packit a4058c
gsize
Packit a4058c
gdk_pixbuf_get_byte_length (const GdkPixbuf *pixbuf)
Packit a4058c
{
Packit a4058c
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
Packit a4058c
Packit a4058c
        return ((pixbuf->height - 1) * pixbuf->rowstride +
Packit a4058c
                pixbuf->width * ((pixbuf->n_channels * pixbuf->bits_per_sample + 7) / 8));
Packit a4058c
}
Packit a4058c
Packit a4058c

Packit a4058c
Packit a4058c
/* General initialization hooks */
Packit a4058c
const guint gdk_pixbuf_major_version = GDK_PIXBUF_MAJOR;
Packit a4058c
const guint gdk_pixbuf_minor_version = GDK_PIXBUF_MINOR;
Packit a4058c
const guint gdk_pixbuf_micro_version = GDK_PIXBUF_MICRO;
Packit a4058c
Packit a4058c
const char *gdk_pixbuf_version = GDK_PIXBUF_VERSION;
Packit a4058c
Packit a4058c
/* Error quark */
Packit a4058c
GQuark
Packit a4058c
gdk_pixbuf_error_quark (void)
Packit a4058c
{
Packit a4058c
  return g_quark_from_static_string ("gdk-pixbuf-error-quark");
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_fill:
Packit a4058c
 * @pixbuf: a #GdkPixbuf
Packit a4058c
 * @pixel: RGBA pixel to clear to
Packit a4058c
 *         (0xffffffff is opaque white, 0x00000000 transparent black)
Packit a4058c
 *
Packit a4058c
 * Clears a pixbuf to the given RGBA value, converting the RGBA value into
Packit a4058c
 * the pixbuf's pixel format. The alpha will be ignored if the pixbuf
Packit a4058c
 * doesn't have an alpha channel.
Packit a4058c
 * 
Packit a4058c
 **/
Packit a4058c
void
Packit a4058c
gdk_pixbuf_fill (GdkPixbuf *pixbuf,
Packit a4058c
                 guint32    pixel)
Packit a4058c
{
Packit a4058c
        guchar *pixels;
Packit a4058c
        guint r, g, b, a;
Packit a4058c
        guchar *p;
Packit a4058c
        guint w, h;
Packit a4058c
Packit a4058c
        g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
Packit a4058c
        g_return_if_fail (pixbuf->pixels || pixbuf->bytes);
Packit a4058c
Packit a4058c
        if (pixbuf->width == 0 || pixbuf->height == 0)
Packit a4058c
                return;
Packit a4058c
Packit a4058c
        /* Force an implicit copy */
Packit a4058c
        pixels = gdk_pixbuf_get_pixels (pixbuf);
Packit a4058c
Packit a4058c
        r = (pixel & 0xff000000) >> 24;
Packit a4058c
        g = (pixel & 0x00ff0000) >> 16;
Packit a4058c
        b = (pixel & 0x0000ff00) >> 8;
Packit a4058c
        a = (pixel & 0x000000ff);
Packit a4058c
Packit a4058c
        h = pixbuf->height;
Packit a4058c
        
Packit a4058c
        while (h--) {
Packit a4058c
                w = pixbuf->width;
Packit a4058c
                p = pixels;
Packit a4058c
Packit a4058c
                switch (pixbuf->n_channels) {
Packit a4058c
                case 3:
Packit a4058c
                        while (w--) {
Packit a4058c
                                p[0] = r;
Packit a4058c
                                p[1] = g;
Packit a4058c
                                p[2] = b;
Packit a4058c
                                p += 3;
Packit a4058c
                        }
Packit a4058c
                        break;
Packit a4058c
                case 4:
Packit a4058c
                        while (w--) {
Packit a4058c
                                p[0] = r;
Packit a4058c
                                p[1] = g;
Packit a4058c
                                p[2] = b;
Packit a4058c
                                p[3] = a;
Packit a4058c
                                p += 4;
Packit a4058c
                        }
Packit a4058c
                        break;
Packit a4058c
                default:
Packit a4058c
                        break;
Packit a4058c
                }
Packit a4058c
                
Packit a4058c
                pixels += pixbuf->rowstride;
Packit a4058c
        }
Packit a4058c
}
Packit a4058c
Packit a4058c

Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_get_option:
Packit a4058c
 * @pixbuf: a #GdkPixbuf
Packit a4058c
 * @key: a nul-terminated string.
Packit a4058c
 * 
Packit a4058c
 * Looks up @key in the list of options that may have been attached to the
Packit a4058c
 * @pixbuf when it was loaded, or that may have been attached by another
Packit a4058c
 * function using gdk_pixbuf_set_option().
Packit a4058c
 *
Packit a4058c
 * For instance, the ANI loader provides "Title" and "Artist" options. 
Packit a4058c
 * The ICO, XBM, and XPM loaders provide "x_hot" and "y_hot" hot-spot 
Packit a4058c
 * options for cursor definitions. The PNG loader provides the tEXt ancillary
Packit a4058c
 * chunk key/value pairs as options. Since 2.12, the TIFF and JPEG loaders
Packit a4058c
 * return an "orientation" option string that corresponds to the embedded 
Packit a4058c
 * TIFF/Exif orientation tag (if present). Since 2.32, the TIFF loader sets
Packit a4058c
 * the "multipage" option string to "yes" when a multi-page TIFF is loaded.
Packit a4058c
 * Since 2.32 the JPEG and PNG loaders set "x-dpi" and "y-dpi" if the file
Packit a4058c
 * contains image density information in dots per inch.
Packit a4058c
 * Since 2.36.6, the JPEG loader sets the "comment" option with the comment
Packit a4058c
 * EXIF tag.
Packit a4058c
 * 
Packit a4058c
 * Return value: the value associated with @key. This is a nul-terminated 
Packit a4058c
 * string that should not be freed or %NULL if @key was not found.
Packit a4058c
 **/
Packit a4058c
const gchar *
Packit a4058c
gdk_pixbuf_get_option (GdkPixbuf   *pixbuf,
Packit a4058c
                       const gchar *key)
Packit a4058c
{
Packit a4058c
        gchar **options;
Packit a4058c
        gint i;
Packit a4058c
Packit a4058c
        g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
Packit a4058c
        g_return_val_if_fail (key != NULL, NULL);
Packit a4058c
  
Packit a4058c
        options = g_object_get_qdata (G_OBJECT (pixbuf), 
Packit a4058c
                                      g_quark_from_static_string ("gdk_pixbuf_options"));
Packit a4058c
        if (options) {
Packit a4058c
                for (i = 0; options[2*i]; i++) {
Packit a4058c
                        if (strcmp (options[2*i], key) == 0)
Packit a4058c
                                return options[2*i+1];
Packit a4058c
                }
Packit a4058c
        }
Packit a4058c
        
Packit a4058c
        return NULL;
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_get_options:
Packit a4058c
 * @pixbuf: a #GdkPixbuf
Packit a4058c
 *
Packit a4058c
 * Returns a #GHashTable with a list of all the options that may have been
Packit a4058c
 * attached to the @pixbuf when it was loaded, or that may have been
Packit a4058c
 * attached by another function using gdk_pixbuf_set_option().
Packit a4058c
 *
Packit a4058c
 * See gdk_pixbuf_get_option() for more details.
Packit a4058c
 *
Packit a4058c
 * Return value: (transfer container) (element-type utf8 utf8): a #GHashTable of key/values
Packit a4058c
 *
Packit a4058c
 * Since: 2.32
Packit a4058c
 **/
Packit a4058c
GHashTable *
Packit a4058c
gdk_pixbuf_get_options (GdkPixbuf *pixbuf)
Packit a4058c
{
Packit a4058c
        GHashTable *ht;
Packit a4058c
        gchar **options;
Packit a4058c
Packit a4058c
        g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
Packit a4058c
Packit a4058c
        ht = g_hash_table_new (g_str_hash, g_str_equal);
Packit a4058c
Packit a4058c
        options = g_object_get_qdata (G_OBJECT (pixbuf),
Packit a4058c
                                      g_quark_from_static_string ("gdk_pixbuf_options"));
Packit a4058c
        if (options) {
Packit a4058c
                gint i;
Packit a4058c
Packit a4058c
                for (i = 0; options[2*i]; i++)
Packit a4058c
                        g_hash_table_insert (ht, options[2*i], options[2*i+1]);
Packit a4058c
        }
Packit a4058c
Packit a4058c
        return ht;
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_remove_option:
Packit a4058c
 * @pixbuf: a #GdkPixbuf
Packit a4058c
 * @key: a nul-terminated string representing the key to remove.
Packit a4058c
 *
Packit a4058c
 * Remove the key/value pair option attached to a #GdkPixbuf.
Packit a4058c
 *
Packit a4058c
 * Return value: %TRUE if an option was removed, %FALSE if not.
Packit a4058c
 *
Packit a4058c
 * Since: 2.36
Packit a4058c
 **/
Packit a4058c
gboolean
Packit a4058c
gdk_pixbuf_remove_option (GdkPixbuf   *pixbuf,
Packit a4058c
                          const gchar *key)
Packit a4058c
{
Packit a4058c
        GQuark  quark;
Packit a4058c
        gchar **options;
Packit a4058c
        guint n;
Packit a4058c
        GPtrArray *array;
Packit a4058c
        gboolean found;
Packit a4058c
Packit a4058c
        g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
Packit a4058c
        g_return_val_if_fail (key != NULL, FALSE);
Packit a4058c
Packit a4058c
        quark = g_quark_from_static_string ("gdk_pixbuf_options");
Packit a4058c
Packit a4058c
        options = g_object_get_qdata (G_OBJECT (pixbuf), quark);
Packit a4058c
        if (!options)
Packit a4058c
                return FALSE;
Packit a4058c
Packit a4058c
        g_object_steal_qdata (G_OBJECT (pixbuf), quark);
Packit a4058c
Packit a4058c
        /* There's at least a nul-terminator */
Packit a4058c
        array = g_ptr_array_new_full (1, g_free);
Packit a4058c
Packit a4058c
        found = FALSE;
Packit a4058c
        for (n = 0; options[2*n]; n++) {
Packit a4058c
                if (strcmp (options[2*n], key) != 0) {
Packit a4058c
                        g_ptr_array_add (array, g_strdup (options[2*n]));   /* key */
Packit a4058c
                        g_ptr_array_add (array, g_strdup (options[2*n+1])); /* value */
Packit a4058c
                } else {
Packit a4058c
                        found = TRUE;
Packit a4058c
                }
Packit a4058c
        }
Packit a4058c
Packit a4058c
        if (array->len == 0) {
Packit a4058c
                g_ptr_array_unref (array);
Packit a4058c
                g_strfreev (options);
Packit a4058c
                return found;
Packit a4058c
        }
Packit a4058c
Packit a4058c
        if (!found) {
Packit a4058c
                g_ptr_array_free (array, TRUE);
Packit a4058c
                g_object_set_qdata_full (G_OBJECT (pixbuf), quark,
Packit a4058c
                                         options, (GDestroyNotify) g_strfreev);
Packit a4058c
                return FALSE;
Packit a4058c
        }
Packit a4058c
Packit a4058c
        g_ptr_array_add (array, NULL);
Packit a4058c
        g_object_set_qdata_full (G_OBJECT (pixbuf), quark,
Packit a4058c
                                 g_ptr_array_free (array, FALSE), (GDestroyNotify) g_strfreev);
Packit a4058c
        g_strfreev (options);
Packit a4058c
Packit a4058c
        return TRUE;
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_set_option:
Packit a4058c
 * @pixbuf: a #GdkPixbuf
Packit a4058c
 * @key: a nul-terminated string.
Packit a4058c
 * @value: a nul-terminated string.
Packit a4058c
 * 
Packit a4058c
 * Attaches a key/value pair as an option to a #GdkPixbuf. If @key already
Packit a4058c
 * exists in the list of options attached to @pixbuf, the new value is 
Packit a4058c
 * ignored and %FALSE is returned.
Packit a4058c
 *
Packit a4058c
 * Return value: %TRUE on success.
Packit a4058c
 *
Packit a4058c
 * Since: 2.2
Packit a4058c
 **/
Packit a4058c
gboolean
Packit a4058c
gdk_pixbuf_set_option (GdkPixbuf   *pixbuf,
Packit a4058c
                       const gchar *key,
Packit a4058c
                       const gchar *value)
Packit a4058c
{
Packit a4058c
        GQuark  quark;
Packit a4058c
        gchar **options;
Packit a4058c
        gint n = 0;
Packit a4058c
 
Packit a4058c
        g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
Packit a4058c
        g_return_val_if_fail (key != NULL, FALSE);
Packit a4058c
        g_return_val_if_fail (value != NULL, FALSE);
Packit a4058c
Packit a4058c
        quark = g_quark_from_static_string ("gdk_pixbuf_options");
Packit a4058c
Packit a4058c
        options = g_object_get_qdata (G_OBJECT (pixbuf), quark);
Packit a4058c
Packit a4058c
        if (options) {
Packit a4058c
                for (n = 0; options[2*n]; n++) {
Packit a4058c
                        if (strcmp (options[2*n], key) == 0)
Packit a4058c
                                return FALSE;
Packit a4058c
                }
Packit a4058c
Packit a4058c
                g_object_steal_qdata (G_OBJECT (pixbuf), quark);
Packit a4058c
                options = g_renew (gchar *, options, 2*(n+1) + 1);
Packit a4058c
        } else {
Packit a4058c
                options = g_new (gchar *, 3);
Packit a4058c
        }
Packit a4058c
        
Packit a4058c
        options[2*n]   = g_strdup (key);
Packit a4058c
        options[2*n+1] = g_strdup (value);
Packit a4058c
        options[2*n+2] = NULL;
Packit a4058c
Packit a4058c
        g_object_set_qdata_full (G_OBJECT (pixbuf), quark,
Packit a4058c
                                 options, (GDestroyNotify) g_strfreev);
Packit a4058c
        
Packit a4058c
        return TRUE;
Packit a4058c
}
Packit a4058c
Packit a4058c
/**
Packit a4058c
 * gdk_pixbuf_copy_options:
Packit a4058c
 * @src_pixbuf: a #GdkPixbuf to copy options from
Packit a4058c
 * @dest_pixbuf: the #GdkPixbuf to copy options to
Packit a4058c
 *
Packit a4058c
 * Copy the key/value pair options attached to a #GdkPixbuf to another.
Packit a4058c
 * This is useful to keep original metadata after having manipulated
Packit a4058c
 * a file. However be careful to remove metadata which you've already
Packit a4058c
 * applied, such as the "orientation" option after rotating the image.
Packit a4058c
 *
Packit a4058c
 * Return value: %TRUE on success.
Packit a4058c
 *
Packit a4058c
 * Since: 2.36
Packit a4058c
 **/
Packit a4058c
gboolean
Packit a4058c
gdk_pixbuf_copy_options (GdkPixbuf *src_pixbuf,
Packit a4058c
                         GdkPixbuf *dest_pixbuf)
Packit a4058c
{
Packit a4058c
        GQuark  quark;
Packit a4058c
        gchar **options;
Packit a4058c
Packit a4058c
        g_return_val_if_fail (GDK_IS_PIXBUF (src_pixbuf), FALSE);
Packit a4058c
        g_return_val_if_fail (GDK_IS_PIXBUF (dest_pixbuf), FALSE);
Packit a4058c
Packit a4058c
        quark = g_quark_from_static_string ("gdk_pixbuf_options");
Packit a4058c
Packit a4058c
        options = g_object_dup_qdata (G_OBJECT (src_pixbuf),
Packit a4058c
                                      quark,
Packit a4058c
                                      (GDuplicateFunc) g_strdupv,
Packit a4058c
                                      NULL);
Packit a4058c
Packit a4058c
        if (options == NULL)
Packit a4058c
                return TRUE;
Packit a4058c
Packit a4058c
        g_object_set_qdata_full (G_OBJECT (dest_pixbuf), quark,
Packit a4058c
                                 options, (GDestroyNotify) g_strfreev);
Packit a4058c
Packit a4058c
        return TRUE;
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
gdk_pixbuf_set_property (GObject         *object,
Packit a4058c
			 guint            prop_id,
Packit a4058c
			 const GValue    *value,
Packit a4058c
			 GParamSpec      *pspec)
Packit a4058c
{
Packit a4058c
  GdkPixbuf *pixbuf = GDK_PIXBUF (object);
Packit a4058c
  gboolean notify = TRUE;
Packit a4058c
Packit a4058c
  switch (prop_id)
Packit a4058c
          {
Packit a4058c
          case PROP_COLORSPACE:
Packit a4058c
                  notify = pixbuf->colorspace != g_value_get_enum (value);
Packit a4058c
                  pixbuf->colorspace = g_value_get_enum (value);
Packit a4058c
                  break;
Packit a4058c
          case PROP_N_CHANNELS:
Packit a4058c
                  notify = pixbuf->n_channels != g_value_get_int (value);
Packit a4058c
                  pixbuf->n_channels = g_value_get_int (value);
Packit a4058c
                  break;
Packit a4058c
          case PROP_HAS_ALPHA:
Packit a4058c
                  notify = pixbuf->has_alpha != g_value_get_boolean (value);
Packit a4058c
                  pixbuf->has_alpha = g_value_get_boolean (value);
Packit a4058c
                  break;
Packit a4058c
          case PROP_BITS_PER_SAMPLE:
Packit a4058c
                  notify = pixbuf->bits_per_sample != g_value_get_int (value);
Packit a4058c
                  pixbuf->bits_per_sample = g_value_get_int (value);
Packit a4058c
                  break;
Packit a4058c
          case PROP_WIDTH:
Packit a4058c
                  notify = pixbuf->width != g_value_get_int (value);
Packit a4058c
                  pixbuf->width = g_value_get_int (value);
Packit a4058c
                  break;
Packit a4058c
          case PROP_HEIGHT:
Packit a4058c
                  notify = pixbuf->height != g_value_get_int (value);
Packit a4058c
                  pixbuf->height = g_value_get_int (value);
Packit a4058c
                  break;
Packit a4058c
          case PROP_ROWSTRIDE:
Packit a4058c
                  notify = pixbuf->rowstride != g_value_get_int (value);
Packit a4058c
                  pixbuf->rowstride = g_value_get_int (value);
Packit a4058c
                  break;
Packit a4058c
          case PROP_PIXELS:
Packit a4058c
                  notify = pixbuf->pixels != (guchar *) g_value_get_pointer (value);
Packit a4058c
                  pixbuf->pixels = (guchar *) g_value_get_pointer (value);
Packit a4058c
                  break;
Packit a4058c
          case PROP_PIXEL_BYTES:
Packit a4058c
                  notify = pixbuf->bytes != g_value_get_boxed (value);
Packit a4058c
                  pixbuf->bytes = g_value_dup_boxed (value);
Packit a4058c
                  break;
Packit a4058c
          default:
Packit a4058c
                  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit a4058c
                  break;
Packit a4058c
          }
Packit a4058c
Packit a4058c
        if (notify)
Packit a4058c
                g_object_notify_by_pspec (G_OBJECT (object), pspec);
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
gdk_pixbuf_get_property (GObject         *object,
Packit a4058c
			 guint            prop_id,
Packit a4058c
			 GValue          *value,
Packit a4058c
			 GParamSpec      *pspec)
Packit a4058c
{
Packit a4058c
  GdkPixbuf *pixbuf = GDK_PIXBUF (object);
Packit a4058c
  
Packit a4058c
  switch (prop_id)
Packit a4058c
          {
Packit a4058c
          case PROP_COLORSPACE:
Packit a4058c
                  g_value_set_enum (value, gdk_pixbuf_get_colorspace (pixbuf));
Packit a4058c
                  break;
Packit a4058c
          case PROP_N_CHANNELS:
Packit a4058c
                  g_value_set_int (value, gdk_pixbuf_get_n_channels (pixbuf));
Packit a4058c
                  break;
Packit a4058c
          case PROP_HAS_ALPHA:
Packit a4058c
                  g_value_set_boolean (value, gdk_pixbuf_get_has_alpha (pixbuf));
Packit a4058c
                  break;
Packit a4058c
          case PROP_BITS_PER_SAMPLE:
Packit a4058c
                  g_value_set_int (value, gdk_pixbuf_get_bits_per_sample (pixbuf));
Packit a4058c
                  break;
Packit a4058c
          case PROP_WIDTH:
Packit a4058c
                  g_value_set_int (value, gdk_pixbuf_get_width (pixbuf));
Packit a4058c
                  break;
Packit a4058c
          case PROP_HEIGHT:
Packit a4058c
                  g_value_set_int (value, gdk_pixbuf_get_height (pixbuf));
Packit a4058c
                  break;
Packit a4058c
          case PROP_ROWSTRIDE:
Packit a4058c
                  g_value_set_int (value, gdk_pixbuf_get_rowstride (pixbuf));
Packit a4058c
                  break;
Packit a4058c
          case PROP_PIXELS:
Packit a4058c
                  g_value_set_pointer (value, gdk_pixbuf_get_pixels (pixbuf));
Packit a4058c
                  break;
Packit a4058c
          case PROP_PIXEL_BYTES:
Packit a4058c
                  g_value_set_boxed (value, pixbuf->bytes);
Packit a4058c
                  break;
Packit a4058c
          default:
Packit a4058c
                  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit a4058c
                  break;
Packit a4058c
          }
Packit a4058c
}