1 /* Libart_LGPL - library of basic graphic primitives
2 * Copyright (C) 1998 Raph Levien
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "art_pixbuf.h"
25 * art_pixbuf_new_rgb_dnotify: Create a new RGB #ArtPixBuf with explicit destroy notification.
26 * @pixels: A buffer containing the actual pixel data.
27 * @width: The width of the pixbuf.
28 * @height: The height of the pixbuf.
29 * @rowstride: The rowstride of the pixbuf.
30 * @dfunc_data: The private data passed to @dfunc.
31 * @dfunc: The destroy notification function.
33 * Creates a generic data structure for holding a buffer of RGB
34 * pixels. It is possible to think of an #ArtPixBuf as a
35 * virtualization over specific pixel buffer formats.
37 * @dfunc is called with @dfunc_data and @pixels as arguments when the
38 * #ArtPixBuf is destroyed. Using a destroy notification function
39 * allows a wide range of memory management disciplines for the pixel
40 * memory. A NULL value for @dfunc is also allowed and means that no
41 * special action will be taken on destruction.
43 * Return value: The newly created #ArtPixBuf.
46 art_pixbuf_new_rgb_dnotify (art_u8 *pixels, int width, int height, int rowstride,
47 void *dfunc_data, ArtDestroyNotify dfunc)
51 pixbuf = art_new (ArtPixBuf, 1);
53 pixbuf->format = ART_PIX_RGB;
54 pixbuf->n_channels = 3;
55 pixbuf->has_alpha = 0;
56 pixbuf->bits_per_sample = 8;
58 pixbuf->pixels = (art_u8 *) pixels;
59 pixbuf->width = width;
60 pixbuf->height = height;
61 pixbuf->rowstride = rowstride;
62 pixbuf->destroy_data = dfunc_data;
63 pixbuf->destroy = dfunc;
69 * art_pixbuf_new_rgba_dnotify: Create a new RGBA #ArtPixBuf with explicit destroy notification.
70 * @pixels: A buffer containing the actual pixel data.
71 * @width: The width of the pixbuf.
72 * @height: The height of the pixbuf.
73 * @rowstride: The rowstride of the pixbuf.
74 * @dfunc_data: The private data passed to @dfunc.
75 * @dfunc: The destroy notification function.
77 * Creates a generic data structure for holding a buffer of RGBA
78 * pixels. It is possible to think of an #ArtPixBuf as a
79 * virtualization over specific pixel buffer formats.
81 * @dfunc is called with @dfunc_data and @pixels as arguments when the
82 * #ArtPixBuf is destroyed. Using a destroy notification function
83 * allows a wide range of memory management disciplines for the pixel
84 * memory. A NULL value for @dfunc is also allowed and means that no
85 * special action will be taken on destruction.
87 * Return value: The newly created #ArtPixBuf.
90 art_pixbuf_new_rgba_dnotify (art_u8 *pixels, int width, int height, int rowstride,
91 void *dfunc_data, ArtDestroyNotify dfunc)
95 pixbuf = art_new (ArtPixBuf, 1);
97 pixbuf->format = ART_PIX_RGB;
98 pixbuf->n_channels = 4;
99 pixbuf->has_alpha = 1;
100 pixbuf->bits_per_sample = 8;
102 pixbuf->pixels = (art_u8 *) pixels;
103 pixbuf->width = width;
104 pixbuf->height = height;
105 pixbuf->rowstride = rowstride;
106 pixbuf->destroy_data = dfunc_data;
107 pixbuf->destroy = dfunc;
113 * art_pixbuf_new_const_rgb: Create a new RGB #ArtPixBuf with constant pixel data.
114 * @pixels: A buffer containing the actual pixel data.
115 * @width: The width of the pixbuf.
116 * @height: The height of the pixbuf.
117 * @rowstride: The rowstride of the pixbuf.
119 * Creates a generic data structure for holding a buffer of RGB
120 * pixels. It is possible to think of an #ArtPixBuf as a
121 * virtualization over specific pixel buffer formats.
123 * No action is taken when the #ArtPixBuf is destroyed. Thus, this
124 * function is useful when the pixel data is constant or statically
127 * Return value: The newly created #ArtPixBuf.
130 art_pixbuf_new_const_rgb (const art_u8 *pixels, int width, int height, int rowstride)
132 return art_pixbuf_new_rgb_dnotify ((art_u8 *) pixels, width, height, rowstride, NULL, NULL);
136 * art_pixbuf_new_const_rgba: Create a new RGBA #ArtPixBuf with constant pixel data.
137 * @pixels: A buffer containing the actual pixel data.
138 * @width: The width of the pixbuf.
139 * @height: The height of the pixbuf.
140 * @rowstride: The rowstride of the pixbuf.
142 * Creates a generic data structure for holding a buffer of RGBA
143 * pixels. It is possible to think of an #ArtPixBuf as a
144 * virtualization over specific pixel buffer formats.
146 * No action is taken when the #ArtPixBuf is destroyed. Thus, this
147 * function is suitable when the pixel data is constant or statically
150 * Return value: The newly created #ArtPixBuf.
153 art_pixbuf_new_const_rgba (const art_u8 *pixels, int width, int height, int rowstride)
155 return art_pixbuf_new_rgba_dnotify ((art_u8 *) pixels, width, height, rowstride, NULL, NULL);
159 art_pixel_destroy (void *func_data, void *data)
165 * art_pixbuf_new_rgb: Create a new RGB #ArtPixBuf.
166 * @pixels: A buffer containing the actual pixel data.
167 * @width: The width of the pixbuf.
168 * @height: The height of the pixbuf.
169 * @rowstride: The rowstride of the pixbuf.
171 * Creates a generic data structure for holding a buffer of RGB
172 * pixels. It is possible to think of an #ArtPixBuf as a
173 * virtualization over specific pixel buffer formats.
175 * The @pixels buffer is freed with art_free() when the #ArtPixBuf is
176 * destroyed. Thus, this function is suitable when the pixel data is
177 * allocated with art_alloc().
179 * Return value: The newly created #ArtPixBuf.
182 art_pixbuf_new_rgb (art_u8 *pixels, int width, int height, int rowstride)
184 return art_pixbuf_new_rgb_dnotify (pixels, width, height, rowstride, NULL, art_pixel_destroy);
188 * art_pixbuf_new_rgba: Create a new RGBA #ArtPixBuf.
189 * @pixels: A buffer containing the actual pixel data.
190 * @width: The width of the pixbuf.
191 * @height: The height of the pixbuf.
192 * @rowstride: The rowstride of the pixbuf.
194 * Creates a generic data structure for holding a buffer of RGBA
195 * pixels. It is possible to think of an #ArtPixBuf as a
196 * virtualization over specific pixel buffer formats.
198 * The @pixels buffer is freed with art_free() when the #ArtPixBuf is
199 * destroyed. Thus, this function is suitable when the pixel data is
200 * allocated with art_alloc().
202 * Return value: The newly created #ArtPixBuf.
205 art_pixbuf_new_rgba (art_u8 *pixels, int width, int height, int rowstride)
207 return art_pixbuf_new_rgba_dnotify (pixels, width, height, rowstride, NULL, art_pixel_destroy);
211 * art_pixbuf_free: Destroy an #ArtPixBuf.
212 * @pixbuf: The #ArtPixBuf to be destroyed.
214 * Destroys the #ArtPixBuf, calling the destroy notification function
215 * (if non-NULL) so that the memory for the pixel buffer can be
216 * properly reclaimed.
219 art_pixbuf_free (ArtPixBuf *pixbuf)
221 ArtDestroyNotify destroy = pixbuf->destroy;
222 void *destroy_data = pixbuf->destroy_data;
223 art_u8 *pixels = pixbuf->pixels;
225 pixbuf->pixels = NULL;
226 pixbuf->destroy = NULL;
227 pixbuf->destroy_data = NULL;
230 destroy (destroy_data, pixels);
236 * art_pixbuf_free_shallow:
237 * @pixbuf: The #ArtPixBuf to be destroyed.
239 * Destroys the #ArtPixBuf without calling the destroy notification function.
241 * This function is deprecated. Use the _dnotify variants for
242 * allocation instead.
245 art_pixbuf_free_shallow (ArtPixBuf *pixbuf)
251 * art_pixbuf_duplicate: Duplicate a pixbuf.
252 * @pixbuf: The #ArtPixBuf to duplicate.
254 * Duplicates a pixbuf, including duplicating the buffer.
256 * Return value: The duplicated pixbuf.
259 art_pixbuf_duplicate (const ArtPixBuf *pixbuf)
264 result = art_new (ArtPixBuf, 1);
266 result->format = pixbuf->format;
267 result->n_channels = pixbuf->n_channels;
268 result->has_alpha = pixbuf->has_alpha;
269 result->bits_per_sample = pixbuf->bits_per_sample;
271 size = (pixbuf->height - 1) * pixbuf->rowstride +
272 pixbuf->width * ((pixbuf->n_channels * pixbuf->bits_per_sample + 7) >> 3);
273 result->pixels = art_alloc (size);
274 memcpy (result->pixels, pixbuf->pixels, size);
276 result->width = pixbuf->width;
277 result->height = pixbuf->height;
278 result->rowstride = pixbuf->rowstride;
279 result->destroy_data = NULL;
280 result->destroy = art_pixel_destroy;