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"
27 * art_pixbuf_new_rgb_dnotify: Create a new RGB #ArtPixBuf with explicit destroy notification.
28 * @pixels: A buffer containing the actual pixel data.
29 * @width: The width of the pixbuf.
30 * @height: The height of the pixbuf.
31 * @rowstride: The rowstride of the pixbuf.
32 * @dfunc_data: The private data passed to @dfunc.
33 * @dfunc: The destroy notification function.
35 * Creates a generic data structure for holding a buffer of RGB
36 * pixels. It is possible to think of an #ArtPixBuf as a
37 * virtualization over specific pixel buffer formats.
39 * @dfunc is called with @dfunc_data and @pixels as arguments when the
40 * #ArtPixBuf is destroyed. Using a destroy notification function
41 * allows a wide range of memory management disciplines for the pixel
42 * memory. A NULL value for @dfunc is also allowed and means that no
43 * special action will be taken on destruction.
45 * Return value: The newly created #ArtPixBuf.
48 art_pixbuf_new_rgb_dnotify (art_u8 *pixels, int width, int height, int rowstride,
49 void *dfunc_data, ArtDestroyNotify dfunc)
53 pixbuf = art_new (ArtPixBuf, 1);
55 pixbuf->format = ART_PIX_RGB;
56 pixbuf->n_channels = 3;
57 pixbuf->has_alpha = 0;
58 pixbuf->bits_per_sample = 8;
60 pixbuf->pixels = (art_u8 *) pixels;
61 pixbuf->width = width;
62 pixbuf->height = height;
63 pixbuf->rowstride = rowstride;
64 pixbuf->destroy_data = dfunc_data;
65 pixbuf->destroy = dfunc;
71 * art_pixbuf_new_rgba_dnotify: Create a new RGBA #ArtPixBuf with explicit destroy notification.
72 * @pixels: A buffer containing the actual pixel data.
73 * @width: The width of the pixbuf.
74 * @height: The height of the pixbuf.
75 * @rowstride: The rowstride of the pixbuf.
76 * @dfunc_data: The private data passed to @dfunc.
77 * @dfunc: The destroy notification function.
79 * Creates a generic data structure for holding a buffer of RGBA
80 * pixels. It is possible to think of an #ArtPixBuf as a
81 * virtualization over specific pixel buffer formats.
83 * @dfunc is called with @dfunc_data and @pixels as arguments when the
84 * #ArtPixBuf is destroyed. Using a destroy notification function
85 * allows a wide range of memory management disciplines for the pixel
86 * memory. A NULL value for @dfunc is also allowed and means that no
87 * special action will be taken on destruction.
89 * Return value: The newly created #ArtPixBuf.
92 art_pixbuf_new_rgba_dnotify (art_u8 *pixels, int width, int height, int rowstride,
93 void *dfunc_data, ArtDestroyNotify dfunc)
97 pixbuf = art_new (ArtPixBuf, 1);
99 pixbuf->format = ART_PIX_RGB;
100 pixbuf->n_channels = 4;
101 pixbuf->has_alpha = 1;
102 pixbuf->bits_per_sample = 8;
104 pixbuf->pixels = (art_u8 *) pixels;
105 pixbuf->width = width;
106 pixbuf->height = height;
107 pixbuf->rowstride = rowstride;
108 pixbuf->destroy_data = dfunc_data;
109 pixbuf->destroy = dfunc;
115 * art_pixbuf_new_const_rgb: Create a new RGB #ArtPixBuf with constant pixel data.
116 * @pixels: A buffer containing the actual pixel data.
117 * @width: The width of the pixbuf.
118 * @height: The height of the pixbuf.
119 * @rowstride: The rowstride of the pixbuf.
121 * Creates a generic data structure for holding a buffer of RGB
122 * pixels. It is possible to think of an #ArtPixBuf as a
123 * virtualization over specific pixel buffer formats.
125 * No action is taken when the #ArtPixBuf is destroyed. Thus, this
126 * function is useful when the pixel data is constant or statically
129 * Return value: The newly created #ArtPixBuf.
132 art_pixbuf_new_const_rgb (const art_u8 *pixels, int width, int height, int rowstride)
134 return art_pixbuf_new_rgb_dnotify ((art_u8 *) pixels, width, height, rowstride, NULL, NULL);
138 * art_pixbuf_new_const_rgba: Create a new RGBA #ArtPixBuf with constant pixel data.
139 * @pixels: A buffer containing the actual pixel data.
140 * @width: The width of the pixbuf.
141 * @height: The height of the pixbuf.
142 * @rowstride: The rowstride of the pixbuf.
144 * Creates a generic data structure for holding a buffer of RGBA
145 * pixels. It is possible to think of an #ArtPixBuf as a
146 * virtualization over specific pixel buffer formats.
148 * No action is taken when the #ArtPixBuf is destroyed. Thus, this
149 * function is suitable when the pixel data is constant or statically
152 * Return value: The newly created #ArtPixBuf.
155 art_pixbuf_new_const_rgba (const art_u8 *pixels, int width, int height, int rowstride)
157 return art_pixbuf_new_rgba_dnotify ((art_u8 *) pixels, width, height, rowstride, NULL, NULL);
161 art_pixel_destroy (void *func_data, void *data)
167 * art_pixbuf_new_rgb: Create a new RGB #ArtPixBuf.
168 * @pixels: A buffer containing the actual pixel data.
169 * @width: The width of the pixbuf.
170 * @height: The height of the pixbuf.
171 * @rowstride: The rowstride of the pixbuf.
173 * Creates a generic data structure for holding a buffer of RGB
174 * pixels. It is possible to think of an #ArtPixBuf as a
175 * virtualization over specific pixel buffer formats.
177 * The @pixels buffer is freed with art_free() when the #ArtPixBuf is
178 * destroyed. Thus, this function is suitable when the pixel data is
179 * allocated with art_alloc().
181 * Return value: The newly created #ArtPixBuf.
184 art_pixbuf_new_rgb (art_u8 *pixels, int width, int height, int rowstride)
186 return art_pixbuf_new_rgb_dnotify (pixels, width, height, rowstride, NULL, art_pixel_destroy);
190 * art_pixbuf_new_rgba: Create a new RGBA #ArtPixBuf.
191 * @pixels: A buffer containing the actual pixel data.
192 * @width: The width of the pixbuf.
193 * @height: The height of the pixbuf.
194 * @rowstride: The rowstride of the pixbuf.
196 * Creates a generic data structure for holding a buffer of RGBA
197 * pixels. It is possible to think of an #ArtPixBuf as a
198 * virtualization over specific pixel buffer formats.
200 * The @pixels buffer is freed with art_free() when the #ArtPixBuf is
201 * destroyed. Thus, this function is suitable when the pixel data is
202 * allocated with art_alloc().
204 * Return value: The newly created #ArtPixBuf.
207 art_pixbuf_new_rgba (art_u8 *pixels, int width, int height, int rowstride)
209 return art_pixbuf_new_rgba_dnotify (pixels, width, height, rowstride, NULL, art_pixel_destroy);
213 * art_pixbuf_free: Destroy an #ArtPixBuf.
214 * @pixbuf: The #ArtPixBuf to be destroyed.
216 * Destroys the #ArtPixBuf, calling the destroy notification function
217 * (if non-NULL) so that the memory for the pixel buffer can be
218 * properly reclaimed.
221 art_pixbuf_free (ArtPixBuf *pixbuf)
223 ArtDestroyNotify destroy = pixbuf->destroy;
224 void *destroy_data = pixbuf->destroy_data;
225 art_u8 *pixels = pixbuf->pixels;
227 pixbuf->pixels = NULL;
228 pixbuf->destroy = NULL;
229 pixbuf->destroy_data = NULL;
232 destroy (destroy_data, pixels);
238 * art_pixbuf_free_shallow:
239 * @pixbuf: The #ArtPixBuf to be destroyed.
241 * Destroys the #ArtPixBuf without calling the destroy notification function.
243 * This function is deprecated. Use the _dnotify variants for
244 * allocation instead.
247 art_pixbuf_free_shallow (ArtPixBuf *pixbuf)
253 * art_pixbuf_duplicate: Duplicate a pixbuf.
254 * @pixbuf: The #ArtPixBuf to duplicate.
256 * Duplicates a pixbuf, including duplicating the buffer.
258 * Return value: The duplicated pixbuf.
261 art_pixbuf_duplicate (const ArtPixBuf *pixbuf)
266 result = art_new (ArtPixBuf, 1);
268 result->format = pixbuf->format;
269 result->n_channels = pixbuf->n_channels;
270 result->has_alpha = pixbuf->has_alpha;
271 result->bits_per_sample = pixbuf->bits_per_sample;
273 size = (pixbuf->height - 1) * pixbuf->rowstride +
274 pixbuf->width * ((pixbuf->n_channels * pixbuf->bits_per_sample + 7) >> 3);
275 result->pixels = art_alloc (size);
276 memcpy (result->pixels, pixbuf->pixels, size);
278 result->width = pixbuf->width;
279 result->height = pixbuf->height;
280 result->rowstride = pixbuf->rowstride;
281 result->destroy_data = NULL;
282 result->destroy = art_pixel_destroy;