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 Library 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 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library 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.
28 #include "gdk-pixbuf.h"
29 #include "gdk-pixbuf-private.h"
33 /* Reference counting */
39 * Adds a reference to a pixbuf. It must be released afterwards using
42 * Return value: The same as the @pixbuf argument.
45 gdk_pixbuf_ref (GdkPixbuf *pixbuf)
47 g_return_val_if_fail (pixbuf != NULL, NULL);
48 g_return_val_if_fail (pixbuf->ref_count > 0, NULL);
58 * Removes a reference from a pixbuf, which will be destroyed when the reference
59 * count drops to zero.
62 gdk_pixbuf_unref (GdkPixbuf *pixbuf)
64 g_return_if_fail (pixbuf != NULL);
65 g_return_if_fail (pixbuf->ref_count > 0);
69 if (pixbuf->ref_count == 0) {
70 if (pixbuf->destroy_fn)
71 (* pixbuf->destroy_fn) (pixbuf->pixels, pixbuf->destroy_fn_data);
79 /* Used as the destroy notification function for gdk_pixbuf_new() */
81 free_buffer (guchar *pixels, gpointer data)
88 * @colorspace: Color space for image.
89 * @has_alpha: Whether the image should have transparency information.
90 * @bits_per_sample: Number of bits per color sample.
91 * @width: Width of image in pixels.
92 * @height: Height of image in pixels.
94 * Creates a new #GdkPixbuf structure and allocates a buffer for it. The buffer
95 * has an optimal rowstride. Note that the buffer is not cleared; you will have
96 * to fill it completely yourself.
98 * Return value: A newly-created #GdkPixbuf with a reference count of 1, or NULL
99 * if not enough memory could be allocated for the image buffer.
102 gdk_pixbuf_new (GdkColorspace colorspace, gboolean has_alpha, int bits_per_sample,
103 int width, int height)
109 g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, NULL);
110 g_return_val_if_fail (bits_per_sample == 8, NULL);
111 g_return_val_if_fail (width > 0, NULL);
112 g_return_val_if_fail (height > 0, NULL);
114 /* Always align rows to 32-bit boundaries */
116 channels = has_alpha ? 4 : 3;
117 rowstride = 4 * ((channels * width + 3) / 4);
119 buf = malloc (height * rowstride);
123 return gdk_pixbuf_new_from_data (buf, colorspace, has_alpha, bits_per_sample,
124 width, height, rowstride,
132 * Creates a new #GdkPixbuf with a copy of the information in the specified
135 * Return value: A newly-created pixbuf with a reference count of 1, or NULL if
136 * not enough memory could be allocated.
139 gdk_pixbuf_copy (const GdkPixbuf *pixbuf)
144 g_return_val_if_fail (pixbuf != NULL, NULL);
146 /* Calculate a semi-exact size. Here we copy with full rowstrides;
147 * maybe we should copy each row individually with the minimum
151 size = ((pixbuf->height - 1) * pixbuf->rowstride +
152 pixbuf->width * ((pixbuf->n_channels * pixbuf->bits_per_sample + 7) / 8));
154 buf = malloc (size * sizeof (guchar));
158 memcpy (buf, pixbuf->pixels, size);
160 return gdk_pixbuf_new_from_data (buf,
161 pixbuf->colorspace, pixbuf->has_alpha,
162 pixbuf->bits_per_sample,
163 pixbuf->width, pixbuf->height,
174 * gdk_pixbuf_get_colorspace:
177 * Queries the color space of a pixbuf.
179 * Return value: Color space.
182 gdk_pixbuf_get_colorspace (const GdkPixbuf *pixbuf)
184 g_return_val_if_fail (pixbuf != NULL, GDK_COLORSPACE_RGB);
186 return pixbuf->colorspace;
190 * gdk_pixbuf_get_n_channels:
193 * Queries the number of channels of a pixbuf.
195 * Return value: Number of channels.
198 gdk_pixbuf_get_n_channels (const GdkPixbuf *pixbuf)
200 g_return_val_if_fail (pixbuf != NULL, -1);
202 return pixbuf->n_channels;
206 * gdk_pixbuf_get_has_alpha:
209 * Queries whether a pixbuf has an alpha channel (opacity information).
211 * Return value: TRUE if it has an alpha channel, FALSE otherwise.
214 gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf)
216 g_return_val_if_fail (pixbuf != NULL, -1);
218 return pixbuf->has_alpha ? TRUE : FALSE;
222 * gdk_pixbuf_get_bits_per_sample:
225 * Queries the number of bits per color sample in a pixbuf.
227 * Return value: Number of bits per color sample.
230 gdk_pixbuf_get_bits_per_sample (const GdkPixbuf *pixbuf)
232 g_return_val_if_fail (pixbuf != NULL, -1);
234 return pixbuf->bits_per_sample;
238 * gdk_pixbuf_get_pixels:
241 * Queries a pointer to the pixel data of a pixbuf.
243 * Return value: A pointer to the pixbuf's pixel data.
246 gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)
248 g_return_val_if_fail (pixbuf != NULL, NULL);
250 return pixbuf->pixels;
254 * gdk_pixbuf_get_width:
257 * Queries the width of a pixbuf.
259 * Return value: Width in pixels.
262 gdk_pixbuf_get_width (const GdkPixbuf *pixbuf)
264 g_return_val_if_fail (pixbuf != NULL, -1);
266 return pixbuf->width;
270 * gdk_pixbuf_get_height:
273 * Queries the height of a pixbuf.
275 * Return value: Height in pixels.
278 gdk_pixbuf_get_height (const GdkPixbuf *pixbuf)
280 g_return_val_if_fail (pixbuf != NULL, -1);
282 return pixbuf->height;
286 * gdk_pixbuf_get_rowstride:
289 * Queries the rowstride of a pixbuf, which is the number of bytes between rows.
291 * Return value: Number of bytes between rows.
294 gdk_pixbuf_get_rowstride (const GdkPixbuf *pixbuf)
296 g_return_val_if_fail (pixbuf != NULL, -1);
298 return pixbuf->rowstride;
303 /* General initialization hooks */
304 const guint gdk_pixbuf_major_version = GDK_PIXBUF_MAJOR;
305 const guint gdk_pixbuf_minor_version = GDK_PIXBUF_MINOR;
306 const guint gdk_pixbuf_micro_version = GDK_PIXBUF_MICRO;
308 const char *gdk_pixbuf_version = GDK_PIXBUF_VERSION;
311 gdk_pixbuf_preinit (gpointer app, gpointer modinfo)
316 gdk_pixbuf_postinit (gpointer app, gpointer modinfo)