The BIG graph update
[rrdtool.git] / libraries / libart_lgpl-2.3.7 / art_pixbuf.c
1 /* Libart_LGPL - library of basic graphic primitives
2  * Copyright (C) 1998 Raph Levien
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "art_misc.h"
21 #include "art_pixbuf.h"
22 #include <string.h>
23
24 /**
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.
32  *
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.
36  *
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.
42  *
43  * Return value: The newly created #ArtPixBuf.
44  **/
45 ArtPixBuf *
46 art_pixbuf_new_rgb_dnotify (art_u8 *pixels, int width, int height, int rowstride,
47                             void *dfunc_data, ArtDestroyNotify dfunc)
48 {
49   ArtPixBuf *pixbuf;
50
51   pixbuf = art_new (ArtPixBuf, 1);
52
53   pixbuf->format = ART_PIX_RGB;
54   pixbuf->n_channels = 3;
55   pixbuf->has_alpha = 0;
56   pixbuf->bits_per_sample = 8;
57
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;
64
65   return pixbuf;
66 }
67
68 /**
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.
76  *
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.
80  *
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.
86  *
87  * Return value: The newly created #ArtPixBuf.
88  **/
89 ArtPixBuf *
90 art_pixbuf_new_rgba_dnotify (art_u8 *pixels, int width, int height, int rowstride,
91                              void *dfunc_data, ArtDestroyNotify dfunc)
92 {
93   ArtPixBuf *pixbuf;
94
95   pixbuf = art_new (ArtPixBuf, 1);
96
97   pixbuf->format = ART_PIX_RGB;
98   pixbuf->n_channels = 4;
99   pixbuf->has_alpha = 1;
100   pixbuf->bits_per_sample = 8;
101
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;
108
109   return pixbuf;
110 }
111
112 /**
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.
118  *
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.
122  *
123  * No action is taken when the #ArtPixBuf is destroyed. Thus, this
124  * function is useful when the pixel data is constant or statically
125  * allocated.
126  *
127  * Return value: The newly created #ArtPixBuf.
128  **/
129 ArtPixBuf *
130 art_pixbuf_new_const_rgb (const art_u8 *pixels, int width, int height, int rowstride)
131 {
132   return art_pixbuf_new_rgb_dnotify ((art_u8 *) pixels, width, height, rowstride, NULL, NULL);
133 }
134
135 /**
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.
141  *
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.
145  *
146  * No action is taken when the #ArtPixBuf is destroyed. Thus, this
147  * function is suitable when the pixel data is constant or statically
148  * allocated.
149  *
150  * Return value: The newly created #ArtPixBuf.
151  **/
152 ArtPixBuf *
153 art_pixbuf_new_const_rgba (const art_u8 *pixels, int width, int height, int rowstride)
154 {
155   return art_pixbuf_new_rgba_dnotify ((art_u8 *) pixels, width, height, rowstride, NULL, NULL);
156 }
157
158 static void
159 art_pixel_destroy (void *func_data, void *data)
160 {
161   art_free (data);
162 }
163
164 /**
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.
170  *
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.
174  *
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().
178  *
179  * Return value: The newly created #ArtPixBuf.
180  **/
181 ArtPixBuf *
182 art_pixbuf_new_rgb (art_u8 *pixels, int width, int height, int rowstride)
183 {
184   return art_pixbuf_new_rgb_dnotify (pixels, width, height, rowstride, NULL, art_pixel_destroy);
185 }
186
187 /**
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.
193  *
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.
197  *
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().
201  *
202  * Return value: The newly created #ArtPixBuf.
203  **/
204 ArtPixBuf *
205 art_pixbuf_new_rgba (art_u8 *pixels, int width, int height, int rowstride)
206 {
207   return art_pixbuf_new_rgba_dnotify (pixels, width, height, rowstride, NULL, art_pixel_destroy);
208 }
209
210 /**
211  * art_pixbuf_free: Destroy an #ArtPixBuf.
212  * @pixbuf: The #ArtPixBuf to be destroyed.
213  *
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.
217  **/
218 void
219 art_pixbuf_free (ArtPixBuf *pixbuf)
220 {
221   ArtDestroyNotify destroy = pixbuf->destroy;
222   void *destroy_data = pixbuf->destroy_data;
223   art_u8 *pixels = pixbuf->pixels;
224
225   pixbuf->pixels = NULL;
226   pixbuf->destroy = NULL;
227   pixbuf->destroy_data = NULL;
228
229   if (destroy)
230     destroy (destroy_data, pixels);
231
232   art_free (pixbuf);
233 }
234
235 /**
236  * art_pixbuf_free_shallow:
237  * @pixbuf: The #ArtPixBuf to be destroyed.
238  *
239  * Destroys the #ArtPixBuf without calling the destroy notification function.
240  *
241  * This function is deprecated. Use the _dnotify variants for
242  * allocation instead.
243  **/
244 void
245 art_pixbuf_free_shallow (ArtPixBuf *pixbuf)
246 {
247   art_free (pixbuf);
248 }
249
250 /**
251  * art_pixbuf_duplicate: Duplicate a pixbuf.
252  * @pixbuf: The #ArtPixBuf to duplicate.
253  *
254  * Duplicates a pixbuf, including duplicating the buffer.
255  *
256  * Return value: The duplicated pixbuf.
257  **/
258 ArtPixBuf *
259 art_pixbuf_duplicate (const ArtPixBuf *pixbuf)
260 {
261   ArtPixBuf *result;
262   int size;
263
264   result = art_new (ArtPixBuf, 1);
265
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;
270
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);
275
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;
281
282   return result;
283 }