e99375392ef77e3f8a63963b13c25de4e8b16188
[swftools.git] / lib / art / 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 "config.h"
21 #include "art_pixbuf.h"
22
23 #include "art_misc.h"
24 #include <string.h>
25
26 /**
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.
34  *
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.
38  *
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.
44  *
45  * Return value: The newly created #ArtPixBuf.
46  **/
47 ArtPixBuf *
48 art_pixbuf_new_rgb_dnotify (art_u8 *pixels, int width, int height, int rowstride,
49                             void *dfunc_data, ArtDestroyNotify dfunc)
50 {
51   ArtPixBuf *pixbuf;
52
53   pixbuf = art_new (ArtPixBuf, 1);
54
55   pixbuf->format = ART_PIX_RGB;
56   pixbuf->n_channels = 3;
57   pixbuf->has_alpha = 0;
58   pixbuf->bits_per_sample = 8;
59
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;
66
67   return pixbuf;
68 }
69
70 /**
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.
78  *
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.
82  *
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.
88  *
89  * Return value: The newly created #ArtPixBuf.
90  **/
91 ArtPixBuf *
92 art_pixbuf_new_rgba_dnotify (art_u8 *pixels, int width, int height, int rowstride,
93                              void *dfunc_data, ArtDestroyNotify dfunc)
94 {
95   ArtPixBuf *pixbuf;
96
97   pixbuf = art_new (ArtPixBuf, 1);
98
99   pixbuf->format = ART_PIX_RGB;
100   pixbuf->n_channels = 4;
101   pixbuf->has_alpha = 1;
102   pixbuf->bits_per_sample = 8;
103
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;
110
111   return pixbuf;
112 }
113
114 /**
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.
120  *
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.
124  *
125  * No action is taken when the #ArtPixBuf is destroyed. Thus, this
126  * function is useful when the pixel data is constant or statically
127  * allocated.
128  *
129  * Return value: The newly created #ArtPixBuf.
130  **/
131 ArtPixBuf *
132 art_pixbuf_new_const_rgb (const art_u8 *pixels, int width, int height, int rowstride)
133 {
134   return art_pixbuf_new_rgb_dnotify ((art_u8 *) pixels, width, height, rowstride, NULL, NULL);
135 }
136
137 /**
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.
143  *
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.
147  *
148  * No action is taken when the #ArtPixBuf is destroyed. Thus, this
149  * function is suitable when the pixel data is constant or statically
150  * allocated.
151  *
152  * Return value: The newly created #ArtPixBuf.
153  **/
154 ArtPixBuf *
155 art_pixbuf_new_const_rgba (const art_u8 *pixels, int width, int height, int rowstride)
156 {
157   return art_pixbuf_new_rgba_dnotify ((art_u8 *) pixels, width, height, rowstride, NULL, NULL);
158 }
159
160 static void
161 art_pixel_destroy (void *func_data, void *data)
162 {
163   art_free (data);
164 }
165
166 /**
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.
172  *
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.
176  *
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().
180  *
181  * Return value: The newly created #ArtPixBuf.
182  **/
183 ArtPixBuf *
184 art_pixbuf_new_rgb (art_u8 *pixels, int width, int height, int rowstride)
185 {
186   return art_pixbuf_new_rgb_dnotify (pixels, width, height, rowstride, NULL, art_pixel_destroy);
187 }
188
189 /**
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.
195  *
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.
199  *
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().
203  *
204  * Return value: The newly created #ArtPixBuf.
205  **/
206 ArtPixBuf *
207 art_pixbuf_new_rgba (art_u8 *pixels, int width, int height, int rowstride)
208 {
209   return art_pixbuf_new_rgba_dnotify (pixels, width, height, rowstride, NULL, art_pixel_destroy);
210 }
211
212 /**
213  * art_pixbuf_free: Destroy an #ArtPixBuf.
214  * @pixbuf: The #ArtPixBuf to be destroyed.
215  *
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.
219  **/
220 void
221 art_pixbuf_free (ArtPixBuf *pixbuf)
222 {
223   ArtDestroyNotify destroy = pixbuf->destroy;
224   void *destroy_data = pixbuf->destroy_data;
225   art_u8 *pixels = pixbuf->pixels;
226
227   pixbuf->pixels = NULL;
228   pixbuf->destroy = NULL;
229   pixbuf->destroy_data = NULL;
230
231   if (destroy)
232     destroy (destroy_data, pixels);
233
234   art_free (pixbuf);
235 }
236
237 /**
238  * art_pixbuf_free_shallow:
239  * @pixbuf: The #ArtPixBuf to be destroyed.
240  *
241  * Destroys the #ArtPixBuf without calling the destroy notification function.
242  *
243  * This function is deprecated. Use the _dnotify variants for
244  * allocation instead.
245  **/
246 void
247 art_pixbuf_free_shallow (ArtPixBuf *pixbuf)
248 {
249   art_free (pixbuf);
250 }
251
252 /**
253  * art_pixbuf_duplicate: Duplicate a pixbuf.
254  * @pixbuf: The #ArtPixBuf to duplicate.
255  *
256  * Duplicates a pixbuf, including duplicating the buffer.
257  *
258  * Return value: The duplicated pixbuf.
259  **/
260 ArtPixBuf *
261 art_pixbuf_duplicate (const ArtPixBuf *pixbuf)
262 {
263   ArtPixBuf *result;
264   int size;
265
266   result = art_new (ArtPixBuf, 1);
267
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;
272
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);
277
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;
283
284   return result;
285 }