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.
27 #include <libart_lgpl/art_misc.h>
28 #include <libart_lgpl/art_affine.h>
29 #include <libart_lgpl/art_pixbuf.h>
30 #include <libart_lgpl/art_rgb_pixbuf_affine.h>
31 #include <libart_lgpl/art_alphagamma.h>
32 #include "gdk-pixbuf.h"
36 /* Reference counting */
42 * Adds a reference to a pixbuf. It must be released afterwards using
45 * Return value: The same as the @pixbuf argument.
48 gdk_pixbuf_ref (GdkPixbuf *pixbuf)
50 g_return_val_if_fail (pixbuf != NULL, NULL);
51 g_return_val_if_fail (pixbuf->ref_count > 0, NULL);
61 * Removes a reference from a pixbuf. It will be destroyed when the reference
62 * count drops to zero.
65 gdk_pixbuf_unref (GdkPixbuf *pixbuf)
67 g_return_if_fail (pixbuf != NULL);
68 g_return_if_fail (pixbuf->ref_count > 0);
72 if (pixbuf->ref_count == 0) {
73 art_pixbuf_free (pixbuf->art_pixbuf);
74 pixbuf->art_pixbuf = NULL;
81 /* Wrap a libart pixbuf */
84 * gdk_pixbuf_new_from_art_pixbuf:
85 * @art_pixbuf: A libart pixbuf.
87 * Creates a #GdkPixbuf by wrapping a libart pixbuf.
89 * Return value: A newly-created #GdkPixbuf structure with a reference count of
93 gdk_pixbuf_new_from_art_pixbuf (ArtPixBuf *art_pixbuf)
97 g_return_val_if_fail (art_pixbuf != NULL, NULL);
99 pixbuf = g_new (GdkPixbuf, 1);
100 pixbuf->ref_count = 1;
101 pixbuf->art_pixbuf = art_pixbuf;
106 /* Destroy notification function for gdk_pixbuf_new() */
108 free_buffer (gpointer user_data, gpointer data)
115 /* Create an empty pixbuf */
119 * @format: Image format.
120 * @has_alpha: Whether the image should have transparency information.
121 * @bits_per_sample: Number of bits per color sample.
122 * @width: Width of image in pixels.
123 * @height: Height of image in pixels.
125 * Creates a new #GdkPixbuf structure and allocates a buffer for it. The buffer
126 * has an optimal rowstride. Note that the buffer is not cleared; you will have
127 * to fill it completely.
129 * Return value: A newly-created #GdkPixbuf with a reference count of 1, or NULL
130 * if not enough memory could be allocated for the image buffer.
133 gdk_pixbuf_new (ArtPixFormat format, gboolean has_alpha, int bits_per_sample,
134 int width, int height)
140 g_return_val_if_fail (format == ART_PIX_RGB, NULL);
141 g_return_val_if_fail (bits_per_sample == 8, NULL);
142 g_return_val_if_fail (width > 0, NULL);
143 g_return_val_if_fail (height > 0, NULL);
145 /* Always align rows to 32-bit boundaries */
147 channels = has_alpha ? 4 : 3;
148 rowstride = 4 * ((channels * width + 3) / 4);
150 buf = malloc (height * rowstride);
154 return gdk_pixbuf_new_from_data (buf, format, has_alpha, width, height, rowstride,
160 /* Convenience functions */
163 * gdk_pixbuf_get_format:
166 * Queries the image format (color model) of a pixbuf.
168 * Return value: Image format.
171 gdk_pixbuf_get_format (GdkPixbuf *pixbuf)
173 g_return_val_if_fail (pixbuf != NULL, ART_PIX_RGB);
174 g_assert (pixbuf->art_pixbuf != NULL);
176 return pixbuf->art_pixbuf->format;
180 * gdk_pixbuf_get_n_channels:
183 * Queries the number of channels of a pixbuf.
185 * Return value: Number of channels.
188 gdk_pixbuf_get_n_channels (GdkPixbuf *pixbuf)
190 g_return_val_if_fail (pixbuf != NULL, -1);
191 g_assert (pixbuf->art_pixbuf != NULL);
193 return pixbuf->art_pixbuf->n_channels;
197 * gdk_pixbuf_get_has_alpha:
200 * Queries whether a pixbuf has an alpha channel (opacity information).
202 * Return value: TRUE if it has an alpha channel, FALSE otherwise.
205 gdk_pixbuf_get_has_alpha (GdkPixbuf *pixbuf)
207 g_return_val_if_fail (pixbuf != NULL, -1);
208 g_assert (pixbuf->art_pixbuf != NULL);
210 return pixbuf->art_pixbuf->has_alpha;
214 * gdk_pixbuf_get_bits_per_sample:
217 * Queries the number of bits per color sample in a pixbuf.
219 * Return value: Number of bits per color sample.
222 gdk_pixbuf_get_bits_per_sample (GdkPixbuf *pixbuf)
224 g_return_val_if_fail (pixbuf != NULL, -1);
225 g_assert (pixbuf->art_pixbuf != NULL);
227 return pixbuf->art_pixbuf->bits_per_sample;
231 * gdk_pixbuf_get_pixels:
234 * Queries a pointer to the pixel data of a pixbuf.
236 * Return value: A pointer to the pixbuf's pixel data.
239 gdk_pixbuf_get_pixels (GdkPixbuf *pixbuf)
241 g_return_val_if_fail (pixbuf != NULL, NULL);
242 g_assert (pixbuf->art_pixbuf != NULL);
244 return pixbuf->art_pixbuf->pixels;
248 * gdk_pixbuf_get_width:
251 * Queries the width of a pixbuf.
253 * Return value: Width in pixels.
256 gdk_pixbuf_get_width (GdkPixbuf *pixbuf)
258 g_return_val_if_fail (pixbuf != NULL, -1);
259 g_assert (pixbuf->art_pixbuf != NULL);
261 return pixbuf->art_pixbuf->width;
265 * gdk_pixbuf_get_height:
268 * Queries the height of a pixbuf.
270 * Return value: Height in pixels.
273 gdk_pixbuf_get_height (GdkPixbuf *pixbuf)
275 g_return_val_if_fail (pixbuf != NULL, -1);
276 g_assert (pixbuf->art_pixbuf != NULL);
278 return pixbuf->art_pixbuf->height;
282 * gdk_pixbuf_get_rowstride:
285 * Queries the rowstride of a pixbuf, or the number of bytes between rows.
287 * Return value: Number of bytes between rows.
290 gdk_pixbuf_get_rowstride (GdkPixbuf *pixbuf)
292 g_return_val_if_fail (pixbuf != NULL, -1);
293 g_assert (pixbuf->art_pixbuf != NULL);
295 return pixbuf->art_pixbuf->rowstride;
298 /* General initialization hooks */
299 const guint gdk_pixbuf_major_version=GDK_PIXBUF_MAJOR,
300 gdk_pixbuf_minor_version=GDK_PIXBUF_MINOR,
301 gdk_pixbuf_micro_version=GDK_PIXBUF_MICRO;
303 const char *gdk_pixbuf_version = GDK_PIXBUF_VERSION;
306 gdk_pixbuf_preinit(gpointer app, gpointer modinfo)
311 gdk_pixbuf_postinit(gpointer app, gpointer modinfo)