1999-10-20 21:20:49 +00:00
|
|
|
|
/* GdkPixbuf library - Basic memory management
|
1999-01-04 23:53:12 +00:00
|
|
|
|
*
|
1999-10-18 19:29:45 +00:00
|
|
|
|
* Copyright (C) 1999 The Free Software Foundation
|
|
|
|
|
*
|
|
|
|
|
* Authors: Mark Crichton <crichton@gimp.org>
|
|
|
|
|
* Miguel de Icaza <miguel@gnu.org>
|
|
|
|
|
* Federico Mena-Quintero <federico@gimp.org>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
|
* Boston, MA 02111-1307, USA.
|
1999-01-04 23:53:12 +00:00
|
|
|
|
*/
|
1999-08-09 06:09:24 +00:00
|
|
|
|
|
1999-01-04 23:53:12 +00:00
|
|
|
|
#include <config.h>
|
1999-07-19 02:13:34 +00:00
|
|
|
|
#include <math.h>
|
2000-04-11 07:03:25 +00:00
|
|
|
|
#include <stdlib.h>
|
1999-12-02 20:44:43 +00:00
|
|
|
|
#include "gdk-pixbuf.h"
|
2000-04-11 07:03:25 +00:00
|
|
|
|
#include "gdk-pixbuf-private.h"
|
1999-01-04 23:53:12 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
1999-01-04 23:53:12 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
/* Reference counting */
|
1999-09-22 22:30:51 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* gdk_pixbuf_ref:
|
|
|
|
|
* @pixbuf: A pixbuf.
|
|
|
|
|
*
|
|
|
|
|
* Adds a reference to a pixbuf. It must be released afterwards using
|
|
|
|
|
* gdk_pixbuf_unref().
|
2000-01-21 22:54:44 +00:00
|
|
|
|
*
|
|
|
|
|
* Return value: The same as the @pixbuf argument.
|
1999-10-20 21:20:49 +00:00
|
|
|
|
**/
|
2000-01-21 22:54:44 +00:00
|
|
|
|
GdkPixbuf *
|
1999-10-18 19:29:45 +00:00
|
|
|
|
gdk_pixbuf_ref (GdkPixbuf *pixbuf)
|
1999-01-04 23:53:12 +00:00
|
|
|
|
{
|
2000-01-21 22:54:44 +00:00
|
|
|
|
g_return_val_if_fail (pixbuf != NULL, NULL);
|
|
|
|
|
g_return_val_if_fail (pixbuf->ref_count > 0, NULL);
|
1999-08-09 06:09:24 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
pixbuf->ref_count++;
|
2000-01-21 22:54:44 +00:00
|
|
|
|
return pixbuf;
|
1999-01-04 23:53:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* gdk_pixbuf_unref:
|
|
|
|
|
* @pixbuf: A pixbuf.
|
|
|
|
|
*
|
2000-04-11 07:03:25 +00:00
|
|
|
|
* Removes a reference from a pixbuf, which will be destroyed when the reference
|
1999-10-20 21:20:49 +00:00
|
|
|
|
* count drops to zero.
|
|
|
|
|
**/
|
1999-01-04 23:53:12 +00:00
|
|
|
|
void
|
1999-10-18 19:29:45 +00:00
|
|
|
|
gdk_pixbuf_unref (GdkPixbuf *pixbuf)
|
1999-01-04 23:53:12 +00:00
|
|
|
|
{
|
1999-10-18 19:29:45 +00:00
|
|
|
|
g_return_if_fail (pixbuf != NULL);
|
|
|
|
|
g_return_if_fail (pixbuf->ref_count > 0);
|
1999-08-09 06:09:24 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
pixbuf->ref_count--;
|
1999-09-17 05:59:53 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
if (pixbuf->ref_count == 0) {
|
2000-04-11 07:03:25 +00:00
|
|
|
|
if (pixbuf->destroy_fn)
|
|
|
|
|
(* pixbuf->destroy_fn) (pixbuf->pixels, pixbuf->destroy_fn_data);
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
g_free (pixbuf);
|
|
|
|
|
}
|
1999-07-16 20:35:21 +00:00
|
|
|
|
}
|
1999-01-04 23:53:12 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-07-17 20:03:34 +00:00
|
|
|
|
|
2000-04-11 07:03:25 +00:00
|
|
|
|
/* Used as the destroy notification function for gdk_pixbuf_new() */
|
1999-10-27 23:36:44 +00:00
|
|
|
|
static void
|
2000-04-11 07:03:25 +00:00
|
|
|
|
free_buffer (guchar *pixels, gpointer data)
|
1999-10-27 23:36:44 +00:00
|
|
|
|
{
|
2000-04-11 07:03:25 +00:00
|
|
|
|
free (pixels);
|
1999-10-27 23:36:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-10-27 17:28:44 +00:00
|
|
|
|
/**
|
|
|
|
|
* gdk_pixbuf_new:
|
2000-04-11 07:03:25 +00:00
|
|
|
|
* @colorspace: Color space for image.
|
1999-10-27 23:36:44 +00:00
|
|
|
|
* @has_alpha: Whether the image should have transparency information.
|
|
|
|
|
* @bits_per_sample: Number of bits per color sample.
|
|
|
|
|
* @width: Width of image in pixels.
|
|
|
|
|
* @height: Height of image in pixels.
|
1999-11-04 08:14:32 +00:00
|
|
|
|
*
|
1999-11-04 21:52:08 +00:00
|
|
|
|
* Creates a new #GdkPixbuf structure and allocates a buffer for it. The buffer
|
1999-10-27 23:36:44 +00:00
|
|
|
|
* has an optimal rowstride. Note that the buffer is not cleared; you will have
|
2000-04-11 07:03:25 +00:00
|
|
|
|
* to fill it completely yourself.
|
1999-11-04 08:14:32 +00:00
|
|
|
|
*
|
1999-11-04 21:52:08 +00:00
|
|
|
|
* Return value: A newly-created #GdkPixbuf with a reference count of 1, or NULL
|
|
|
|
|
* if not enough memory could be allocated for the image buffer.
|
1999-10-27 17:28:44 +00:00
|
|
|
|
**/
|
|
|
|
|
GdkPixbuf *
|
2000-04-11 07:03:25 +00:00
|
|
|
|
gdk_pixbuf_new (GdkColorspace colorspace, gboolean has_alpha, int bits_per_sample,
|
1999-10-27 23:36:44 +00:00
|
|
|
|
int width, int height)
|
1999-10-27 17:28:44 +00:00
|
|
|
|
{
|
1999-10-27 23:36:44 +00:00
|
|
|
|
guchar *buf;
|
|
|
|
|
int channels;
|
|
|
|
|
int rowstride;
|
1999-10-27 17:28:44 +00:00
|
|
|
|
|
2000-04-11 07:03:25 +00:00
|
|
|
|
g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, NULL);
|
1999-10-27 23:36:44 +00:00
|
|
|
|
g_return_val_if_fail (bits_per_sample == 8, NULL);
|
1999-10-27 17:28:44 +00:00
|
|
|
|
g_return_val_if_fail (width > 0, NULL);
|
1999-10-27 23:36:44 +00:00
|
|
|
|
g_return_val_if_fail (height > 0, NULL);
|
1999-10-27 17:28:44 +00:00
|
|
|
|
|
1999-10-27 23:36:44 +00:00
|
|
|
|
/* Always align rows to 32-bit boundaries */
|
1999-10-27 17:28:44 +00:00
|
|
|
|
|
1999-10-27 23:36:44 +00:00
|
|
|
|
channels = has_alpha ? 4 : 3;
|
|
|
|
|
rowstride = 4 * ((channels * width + 3) / 4);
|
1999-10-27 17:28:44 +00:00
|
|
|
|
|
1999-10-27 23:36:44 +00:00
|
|
|
|
buf = malloc (height * rowstride);
|
|
|
|
|
if (!buf)
|
|
|
|
|
return NULL;
|
1999-10-27 17:28:44 +00:00
|
|
|
|
|
2000-04-11 07:03:25 +00:00
|
|
|
|
return gdk_pixbuf_new_from_data (buf, colorspace, has_alpha, bits_per_sample,
|
|
|
|
|
width, height, rowstride,
|
1999-10-27 23:36:44 +00:00
|
|
|
|
free_buffer, NULL);
|
|
|
|
|
}
|
1999-10-28 19:00:02 +00:00
|
|
|
|
|
2000-04-11 07:03:25 +00:00
|
|
|
|
/**
|
|
|
|
|
* gdk_pixbuf_copy:
|
|
|
|
|
* @pixbuf: A pixbuf.
|
|
|
|
|
*
|
|
|
|
|
* Creates a new #GdkPixbuf with a copy of the information in the specified
|
|
|
|
|
* @pixbuf.
|
|
|
|
|
*
|
|
|
|
|
* Return value: A newly-created pixbuf with a reference count of 1, or NULL if
|
|
|
|
|
* not enough memory could be allocated.
|
|
|
|
|
**/
|
|
|
|
|
GdkPixbuf *
|
|
|
|
|
gdk_pixbuf_copy (const GdkPixbuf *pixbuf)
|
|
|
|
|
{
|
|
|
|
|
guchar *buf;
|
|
|
|
|
int size;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (pixbuf != NULL, NULL);
|
|
|
|
|
|
|
|
|
|
/* Calculate a semi-exact size. Here we copy with full rowstrides;
|
|
|
|
|
* maybe we should copy each row individually with the minimum
|
|
|
|
|
* rowstride?
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
size = ((pixbuf->height - 1) * pixbuf->rowstride +
|
|
|
|
|
pixbuf->width * ((pixbuf->n_channels * pixbuf->bits_per_sample + 7) / 8));
|
|
|
|
|
|
|
|
|
|
buf = malloc (size * sizeof (guchar));
|
|
|
|
|
if (!buf)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
memcpy (buf, pixbuf->pixels, size);
|
|
|
|
|
|
|
|
|
|
return gdk_pixbuf_new_from_data (buf,
|
|
|
|
|
pixbuf->colorspace, pixbuf->has_alpha,
|
|
|
|
|
pixbuf->bits_per_sample,
|
|
|
|
|
pixbuf->width, pixbuf->height,
|
|
|
|
|
pixbuf->rowstride,
|
|
|
|
|
free_buffer,
|
|
|
|
|
NULL);
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-04 08:14:32 +00:00
|
|
|
|
|
1999-10-28 19:00:02 +00:00
|
|
|
|
|
2000-04-11 07:03:25 +00:00
|
|
|
|
/* Accessors */
|
1999-11-04 08:14:32 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2000-04-11 07:03:25 +00:00
|
|
|
|
* gdk_pixbuf_get_colorspace:
|
1999-11-04 08:14:32 +00:00
|
|
|
|
* @pixbuf: A pixbuf.
|
|
|
|
|
*
|
2000-04-11 07:03:25 +00:00
|
|
|
|
* Queries the color space of a pixbuf.
|
1999-11-04 08:14:32 +00:00
|
|
|
|
*
|
2000-04-11 07:03:25 +00:00
|
|
|
|
* Return value: Color space.
|
1999-11-04 08:14:32 +00:00
|
|
|
|
**/
|
2000-04-11 07:03:25 +00:00
|
|
|
|
GdkColorspace
|
|
|
|
|
gdk_pixbuf_get_colorspace (const GdkPixbuf *pixbuf)
|
1999-10-28 19:00:02 +00:00
|
|
|
|
{
|
2000-04-11 07:03:25 +00:00
|
|
|
|
g_return_val_if_fail (pixbuf != NULL, GDK_COLORSPACE_RGB);
|
1999-10-28 19:00:02 +00:00
|
|
|
|
|
2000-04-11 07:03:25 +00:00
|
|
|
|
return pixbuf->colorspace;
|
1999-10-28 19:00:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-04 08:14:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* gdk_pixbuf_get_n_channels:
|
|
|
|
|
* @pixbuf: A pixbuf.
|
|
|
|
|
*
|
|
|
|
|
* Queries the number of channels of a pixbuf.
|
|
|
|
|
*
|
|
|
|
|
* Return value: Number of channels.
|
|
|
|
|
**/
|
1999-10-28 19:00:02 +00:00
|
|
|
|
int
|
2000-04-11 07:03:25 +00:00
|
|
|
|
gdk_pixbuf_get_n_channels (const GdkPixbuf *pixbuf)
|
1999-10-28 19:00:02 +00:00
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (pixbuf != NULL, -1);
|
|
|
|
|
|
2000-04-11 07:03:25 +00:00
|
|
|
|
return pixbuf->n_channels;
|
1999-10-28 19:00:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-04 08:14:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* gdk_pixbuf_get_has_alpha:
|
|
|
|
|
* @pixbuf: A pixbuf.
|
|
|
|
|
*
|
|
|
|
|
* Queries whether a pixbuf has an alpha channel (opacity information).
|
|
|
|
|
*
|
|
|
|
|
* Return value: TRUE if it has an alpha channel, FALSE otherwise.
|
|
|
|
|
**/
|
2000-03-29 19:54:29 +00:00
|
|
|
|
gboolean
|
2000-04-11 07:03:25 +00:00
|
|
|
|
gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf)
|
1999-10-28 19:00:02 +00:00
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (pixbuf != NULL, -1);
|
|
|
|
|
|
2000-04-11 07:03:25 +00:00
|
|
|
|
return pixbuf->has_alpha ? TRUE : FALSE;
|
1999-10-28 19:00:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-04 08:14:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* gdk_pixbuf_get_bits_per_sample:
|
|
|
|
|
* @pixbuf: A pixbuf.
|
|
|
|
|
*
|
|
|
|
|
* Queries the number of bits per color sample in a pixbuf.
|
|
|
|
|
*
|
|
|
|
|
* Return value: Number of bits per color sample.
|
|
|
|
|
**/
|
1999-10-28 19:00:02 +00:00
|
|
|
|
int
|
2000-04-11 07:03:25 +00:00
|
|
|
|
gdk_pixbuf_get_bits_per_sample (const GdkPixbuf *pixbuf)
|
1999-10-28 19:00:02 +00:00
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (pixbuf != NULL, -1);
|
|
|
|
|
|
2000-04-11 07:03:25 +00:00
|
|
|
|
return pixbuf->bits_per_sample;
|
1999-10-28 19:00:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-04 08:14:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* gdk_pixbuf_get_pixels:
|
|
|
|
|
* @pixbuf: A pixbuf.
|
|
|
|
|
*
|
|
|
|
|
* Queries a pointer to the pixel data of a pixbuf.
|
|
|
|
|
*
|
|
|
|
|
* Return value: A pointer to the pixbuf's pixel data.
|
|
|
|
|
**/
|
1999-10-28 19:00:02 +00:00
|
|
|
|
guchar *
|
2000-04-11 07:03:25 +00:00
|
|
|
|
gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)
|
1999-10-28 19:00:02 +00:00
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (pixbuf != NULL, NULL);
|
|
|
|
|
|
2000-04-11 07:03:25 +00:00
|
|
|
|
return pixbuf->pixels;
|
1999-10-28 19:00:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-04 08:14:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* gdk_pixbuf_get_width:
|
|
|
|
|
* @pixbuf: A pixbuf.
|
|
|
|
|
*
|
|
|
|
|
* Queries the width of a pixbuf.
|
|
|
|
|
*
|
|
|
|
|
* Return value: Width in pixels.
|
|
|
|
|
**/
|
1999-10-28 19:00:02 +00:00
|
|
|
|
int
|
2000-04-11 07:03:25 +00:00
|
|
|
|
gdk_pixbuf_get_width (const GdkPixbuf *pixbuf)
|
1999-10-28 19:00:02 +00:00
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (pixbuf != NULL, -1);
|
|
|
|
|
|
2000-04-11 07:03:25 +00:00
|
|
|
|
return pixbuf->width;
|
1999-10-28 19:00:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-04 08:14:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* gdk_pixbuf_get_height:
|
|
|
|
|
* @pixbuf: A pixbuf.
|
|
|
|
|
*
|
|
|
|
|
* Queries the height of a pixbuf.
|
|
|
|
|
*
|
|
|
|
|
* Return value: Height in pixels.
|
|
|
|
|
**/
|
1999-10-28 19:00:02 +00:00
|
|
|
|
int
|
2000-04-11 07:03:25 +00:00
|
|
|
|
gdk_pixbuf_get_height (const GdkPixbuf *pixbuf)
|
1999-10-28 19:00:02 +00:00
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (pixbuf != NULL, -1);
|
|
|
|
|
|
2000-04-11 07:03:25 +00:00
|
|
|
|
return pixbuf->height;
|
1999-10-28 19:00:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-11-04 08:14:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* gdk_pixbuf_get_rowstride:
|
|
|
|
|
* @pixbuf: A pixbuf.
|
|
|
|
|
*
|
2000-04-11 07:03:25 +00:00
|
|
|
|
* Queries the rowstride of a pixbuf, which is the number of bytes between rows.
|
1999-11-04 08:14:32 +00:00
|
|
|
|
*
|
|
|
|
|
* Return value: Number of bytes between rows.
|
|
|
|
|
**/
|
1999-10-28 19:00:02 +00:00
|
|
|
|
int
|
2000-04-11 07:03:25 +00:00
|
|
|
|
gdk_pixbuf_get_rowstride (const GdkPixbuf *pixbuf)
|
1999-10-28 19:00:02 +00:00
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (pixbuf != NULL, -1);
|
|
|
|
|
|
2000-04-11 07:03:25 +00:00
|
|
|
|
return pixbuf->rowstride;
|
1999-10-28 19:00:02 +00:00
|
|
|
|
}
|
2000-01-10 19:17:42 +00:00
|
|
|
|
|
2000-04-02 18:54:17 +00:00
|
|
|
|
|
2000-04-01 03:28:31 +00:00
|
|
|
|
|
2000-01-10 19:17:42 +00:00
|
|
|
|
/* General initialization hooks */
|
2000-04-11 07:03:25 +00:00
|
|
|
|
const guint gdk_pixbuf_major_version = GDK_PIXBUF_MAJOR;
|
|
|
|
|
const guint gdk_pixbuf_minor_version = GDK_PIXBUF_MINOR;
|
|
|
|
|
const guint gdk_pixbuf_micro_version = GDK_PIXBUF_MICRO;
|
2000-01-10 19:17:42 +00:00
|
|
|
|
|
|
|
|
|
const char *gdk_pixbuf_version = GDK_PIXBUF_VERSION;
|
|
|
|
|
|
|
|
|
|
void
|
2000-04-11 07:03:25 +00:00
|
|
|
|
gdk_pixbuf_preinit (gpointer app, gpointer modinfo)
|
2000-01-10 19:17:42 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2000-04-11 07:03:25 +00:00
|
|
|
|
gdk_pixbuf_postinit (gpointer app, gpointer modinfo)
|
2000-01-10 19:17:42 +00:00
|
|
|
|
{
|
|
|
|
|
}
|