1 /* GdkPixbuf library - Basic memory management
3 * Copyright (C) 1999 The Free Software Foundation
5 * Authors: Mark Crichton <crichton@gimp.org>
6 * Miguel de Icaza <miguel@gnu.org>
7 * Federico Mena-Quintero <federico@gimp.org>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
29 #include "gdk-pixbuf.h"
30 #include "gdk-pixbuf-private.h"
32 static void gdk_pixbuf_class_init (GdkPixbufClass *klass);
33 static void gdk_pixbuf_finalize (GObject *object);
37 static gpointer parent_class;
40 gdk_pixbuf_get_type (void)
42 static GType object_type = 0;
45 static const GTypeInfo object_info = {
46 sizeof (GdkPixbufClass),
48 (GBaseFinalizeFunc) NULL,
49 (GClassInitFunc) gdk_pixbuf_class_init,
50 NULL, /* class_finalize */
51 NULL, /* class_data */
54 (GInstanceInitFunc) NULL,
57 object_type = g_type_register_static (G_TYPE_OBJECT,
66 gdk_pixbuf_class_init (GdkPixbufClass *klass)
68 GObjectClass *object_class = G_OBJECT_CLASS (klass);
70 parent_class = g_type_class_peek_parent (klass);
72 object_class->finalize = gdk_pixbuf_finalize;
76 gdk_pixbuf_finalize (GObject *object)
78 GdkPixbuf *pixbuf = GDK_PIXBUF (object);
80 if (pixbuf->destroy_fn)
81 (* pixbuf->destroy_fn) (pixbuf->pixels, pixbuf->destroy_fn_data);
83 G_OBJECT_CLASS (parent_class)->finalize (object);
90 * Adds a reference to a pixbuf. Deprecated; use g_object_ref().
92 * Return value: The same as the @pixbuf argument.
95 gdk_pixbuf_ref (GdkPixbuf *pixbuf)
97 return (GdkPixbuf *) g_object_ref (G_OBJECT(pixbuf));
104 * Removes a reference from a pixbuf. Deprecated; use
109 gdk_pixbuf_unref (GdkPixbuf *pixbuf)
111 g_object_unref (G_OBJECT (pixbuf));
116 /* Used as the destroy notification function for gdk_pixbuf_new() */
118 free_buffer (guchar *pixels, gpointer data)
125 * @colorspace: Color space for image.
126 * @has_alpha: Whether the image should have transparency information.
127 * @bits_per_sample: Number of bits per color sample.
128 * @width: Width of image in pixels.
129 * @height: Height of image in pixels.
131 * Creates a new #GdkPixbuf structure and allocates a buffer for it. The buffer
132 * has an optimal rowstride. Note that the buffer is not cleared; you will have
133 * to fill it completely yourself.
135 * Return value: A newly-created #GdkPixbuf with a reference count of 1, or NULL
136 * if not enough memory could be allocated for the image buffer.
139 gdk_pixbuf_new (GdkColorspace colorspace, gboolean has_alpha, int bits_per_sample,
140 int width, int height)
146 g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, NULL);
147 g_return_val_if_fail (bits_per_sample == 8, NULL);
148 g_return_val_if_fail (width > 0, NULL);
149 g_return_val_if_fail (height > 0, NULL);
151 /* Always align rows to 32-bit boundaries */
153 channels = has_alpha ? 4 : 3;
154 rowstride = 4 * ((channels * width + 3) / 4);
156 buf = malloc (height * rowstride);
160 return gdk_pixbuf_new_from_data (buf, colorspace, has_alpha, bits_per_sample,
161 width, height, rowstride,
169 * Creates a new #GdkPixbuf with a copy of the information in the specified
172 * Return value: A newly-created pixbuf with a reference count of 1, or NULL if
173 * not enough memory could be allocated.
176 gdk_pixbuf_copy (const GdkPixbuf *pixbuf)
181 g_return_val_if_fail (pixbuf != NULL, NULL);
183 /* Calculate a semi-exact size. Here we copy with full rowstrides;
184 * maybe we should copy each row individually with the minimum
188 size = ((pixbuf->height - 1) * pixbuf->rowstride +
189 pixbuf->width * ((pixbuf->n_channels * pixbuf->bits_per_sample + 7) / 8));
191 buf = malloc (size * sizeof (guchar));
195 memcpy (buf, pixbuf->pixels, size);
197 return gdk_pixbuf_new_from_data (buf,
198 pixbuf->colorspace, pixbuf->has_alpha,
199 pixbuf->bits_per_sample,
200 pixbuf->width, pixbuf->height,
211 * gdk_pixbuf_get_colorspace:
214 * Queries the color space of a pixbuf.
216 * Return value: Color space.
219 gdk_pixbuf_get_colorspace (const GdkPixbuf *pixbuf)
221 g_return_val_if_fail (pixbuf != NULL, GDK_COLORSPACE_RGB);
223 return pixbuf->colorspace;
227 * gdk_pixbuf_get_n_channels:
230 * Queries the number of channels of a pixbuf.
232 * Return value: Number of channels.
235 gdk_pixbuf_get_n_channels (const GdkPixbuf *pixbuf)
237 g_return_val_if_fail (pixbuf != NULL, -1);
239 return pixbuf->n_channels;
243 * gdk_pixbuf_get_has_alpha:
246 * Queries whether a pixbuf has an alpha channel (opacity information).
248 * Return value: TRUE if it has an alpha channel, FALSE otherwise.
251 gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf)
253 g_return_val_if_fail (pixbuf != NULL, -1);
255 return pixbuf->has_alpha ? TRUE : FALSE;
259 * gdk_pixbuf_get_bits_per_sample:
262 * Queries the number of bits per color sample in a pixbuf.
264 * Return value: Number of bits per color sample.
267 gdk_pixbuf_get_bits_per_sample (const GdkPixbuf *pixbuf)
269 g_return_val_if_fail (pixbuf != NULL, -1);
271 return pixbuf->bits_per_sample;
275 * gdk_pixbuf_get_pixels:
278 * Queries a pointer to the pixel data of a pixbuf.
280 * Return value: A pointer to the pixbuf's pixel data.
283 gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)
285 g_return_val_if_fail (pixbuf != NULL, NULL);
287 return pixbuf->pixels;
291 * gdk_pixbuf_get_width:
294 * Queries the width of a pixbuf.
296 * Return value: Width in pixels.
299 gdk_pixbuf_get_width (const GdkPixbuf *pixbuf)
301 g_return_val_if_fail (pixbuf != NULL, -1);
303 return pixbuf->width;
307 * gdk_pixbuf_get_height:
310 * Queries the height of a pixbuf.
312 * Return value: Height in pixels.
315 gdk_pixbuf_get_height (const GdkPixbuf *pixbuf)
317 g_return_val_if_fail (pixbuf != NULL, -1);
319 return pixbuf->height;
323 * gdk_pixbuf_get_rowstride:
326 * Queries the rowstride of a pixbuf, which is the number of bytes between rows.
328 * Return value: Number of bytes between rows.
331 gdk_pixbuf_get_rowstride (const GdkPixbuf *pixbuf)
333 g_return_val_if_fail (pixbuf != NULL, -1);
335 return pixbuf->rowstride;
340 /* General initialization hooks */
341 const guint gdk_pixbuf_major_version = GDK_PIXBUF_MAJOR;
342 const guint gdk_pixbuf_minor_version = GDK_PIXBUF_MINOR;
343 const guint gdk_pixbuf_micro_version = GDK_PIXBUF_MICRO;
345 const char *gdk_pixbuf_version = GDK_PIXBUF_VERSION;
348 gdk_pixbuf_preinit (gpointer app, gpointer modinfo)
354 gdk_pixbuf_postinit (gpointer app, gpointer modinfo)
359 gdk_pixbuf_init (void)
361 gdk_pixbuf_preinit (NULL, NULL);
362 gdk_pixbuf_postinit (NULL, NULL);
367 gdk_pixbuf_error_quark (void)
371 q = g_quark_from_static_string ("gdk-pixbuf-error-quark");