]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf.c
Pixbuf saving, patch from David Welton.
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf.c
1 /* GdkPixbuf library - Basic memory management
2  *
3  * Copyright (C) 1999 The Free Software Foundation
4  *
5  * Authors: Mark Crichton <crichton@gimp.org>
6  *          Miguel de Icaza <miguel@gnu.org>
7  *          Federico Mena-Quintero <federico@gimp.org>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser 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.
13  *
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  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser 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.
23  */
24
25 #include <config.h>
26 #include <math.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "gdk-pixbuf.h"
30 #include "gdk-pixbuf-private.h"
31
32 static void gdk_pixbuf_class_init  (GdkPixbufClass *klass);
33 static void gdk_pixbuf_finalize    (GObject        *object);
34
35 \f
36
37 static gpointer parent_class;
38
39 GType
40 gdk_pixbuf_get_type (void)
41 {
42         static GType object_type = 0;
43
44         if (!object_type) {
45                 static const GTypeInfo object_info = {
46                         sizeof (GdkPixbufClass),
47                         (GBaseInitFunc) NULL,
48                         (GBaseFinalizeFunc) NULL,
49                         (GClassInitFunc) gdk_pixbuf_class_init,
50                         NULL,           /* class_finalize */
51                         NULL,           /* class_data */
52                         sizeof (GdkPixbuf),
53                         0,              /* n_preallocs */
54                         (GInstanceInitFunc) NULL,
55                 };
56       
57                 object_type = g_type_register_static (G_TYPE_OBJECT,
58                                                       "GdkPixbuf",
59                                                       &object_info);
60         }
61   
62         return object_type;
63 }
64
65 static void
66 gdk_pixbuf_class_init (GdkPixbufClass *klass)
67 {
68         GObjectClass *object_class = G_OBJECT_CLASS (klass);
69         
70         parent_class = g_type_class_peek_parent (klass);
71         
72         object_class->finalize = gdk_pixbuf_finalize;
73 }
74
75 static void
76 gdk_pixbuf_finalize (GObject *object)
77 {
78         GdkPixbuf *pixbuf = GDK_PIXBUF (object);
79         
80         if (pixbuf->destroy_fn)
81                 (* pixbuf->destroy_fn) (pixbuf->pixels, pixbuf->destroy_fn_data);
82         
83         G_OBJECT_CLASS (parent_class)->finalize (object);
84 }
85
86 /**
87  * gdk_pixbuf_ref:
88  * @pixbuf: A pixbuf.
89  *
90  * Adds a reference to a pixbuf. Deprecated; use g_object_ref().
91  *
92  * Return value: The same as the @pixbuf argument.
93  **/
94 GdkPixbuf *
95 gdk_pixbuf_ref (GdkPixbuf *pixbuf)
96 {
97         return (GdkPixbuf *) g_object_ref (G_OBJECT(pixbuf));
98 }
99
100 /**
101  * gdk_pixbuf_unref:
102  * @pixbuf: A pixbuf.
103  *
104  * Removes a reference from a pixbuf. Deprecated; use
105  * g_object_unref().
106  *
107  **/
108 void
109 gdk_pixbuf_unref (GdkPixbuf *pixbuf)
110 {
111         g_object_unref (G_OBJECT (pixbuf));
112 }
113
114 \f
115
116 /* Used as the destroy notification function for gdk_pixbuf_new() */
117 static void
118 free_buffer (guchar *pixels, gpointer data)
119 {
120         free (pixels);
121 }
122
123 /**
124  * gdk_pixbuf_new:
125  * @colorspace: Color space for image.
126  * @has_alpha: Whether the image should have transparency information.
127  * @bits_per_sample: Number of bits per color sample.
128  * @width: Width of image in pixels.
129  * @height: Height of image in pixels.
130  *
131  * Creates a new #GdkPixbuf structure and allocates a buffer for it.  The buffer
132  * has an optimal rowstride.  Note that the buffer is not cleared; you will have
133  * to fill it completely yourself.
134  *
135  * Return value: A newly-created #GdkPixbuf with a reference count of 1, or NULL
136  * if not enough memory could be allocated for the image buffer.
137  **/
138 GdkPixbuf *
139 gdk_pixbuf_new (GdkColorspace colorspace, gboolean has_alpha, int bits_per_sample,
140                 int width, int height)
141 {
142         guchar *buf;
143         int channels;
144         int rowstride;
145
146         g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, NULL);
147         g_return_val_if_fail (bits_per_sample == 8, NULL);
148         g_return_val_if_fail (width > 0, NULL);
149         g_return_val_if_fail (height > 0, NULL);
150
151         /* Always align rows to 32-bit boundaries */
152
153         channels = has_alpha ? 4 : 3;
154         rowstride = 4 * ((channels * width + 3) / 4);
155
156         buf = malloc (height * rowstride);
157         if (!buf)
158                 return NULL;
159
160         return gdk_pixbuf_new_from_data (buf, colorspace, has_alpha, bits_per_sample,
161                                          width, height, rowstride,
162                                          free_buffer, NULL);
163 }
164
165 /**
166  * gdk_pixbuf_copy:
167  * @pixbuf: A pixbuf.
168  * 
169  * Creates a new #GdkPixbuf with a copy of the information in the specified
170  * @pixbuf.
171  * 
172  * Return value: A newly-created pixbuf with a reference count of 1, or NULL if
173  * not enough memory could be allocated.
174  **/
175 GdkPixbuf *
176 gdk_pixbuf_copy (const GdkPixbuf *pixbuf)
177 {
178         guchar *buf;
179         int size;
180
181         g_return_val_if_fail (pixbuf != NULL, NULL);
182
183         /* Calculate a semi-exact size.  Here we copy with full rowstrides;
184          * maybe we should copy each row individually with the minimum
185          * rowstride?
186          */
187
188         size = ((pixbuf->height - 1) * pixbuf->rowstride +
189                 pixbuf->width * ((pixbuf->n_channels * pixbuf->bits_per_sample + 7) / 8));
190
191         buf = malloc (size * sizeof (guchar));
192         if (!buf)
193                 return NULL;
194
195         memcpy (buf, pixbuf->pixels, size);
196
197         return gdk_pixbuf_new_from_data (buf,
198                                          pixbuf->colorspace, pixbuf->has_alpha,
199                                          pixbuf->bits_per_sample,
200                                          pixbuf->width, pixbuf->height,
201                                          pixbuf->rowstride,
202                                          free_buffer,
203                                          NULL);
204 }
205
206 \f
207
208 /* Accessors */
209
210 /**
211  * gdk_pixbuf_get_colorspace:
212  * @pixbuf: A pixbuf.
213  *
214  * Queries the color space of a pixbuf.
215  *
216  * Return value: Color space.
217  **/
218 GdkColorspace
219 gdk_pixbuf_get_colorspace (const GdkPixbuf *pixbuf)
220 {
221         g_return_val_if_fail (pixbuf != NULL, GDK_COLORSPACE_RGB);
222
223         return pixbuf->colorspace;
224 }
225
226 /**
227  * gdk_pixbuf_get_n_channels:
228  * @pixbuf: A pixbuf.
229  *
230  * Queries the number of channels of a pixbuf.
231  *
232  * Return value: Number of channels.
233  **/
234 int
235 gdk_pixbuf_get_n_channels (const GdkPixbuf *pixbuf)
236 {
237         g_return_val_if_fail (pixbuf != NULL, -1);
238
239         return pixbuf->n_channels;
240 }
241
242 /**
243  * gdk_pixbuf_get_has_alpha:
244  * @pixbuf: A pixbuf.
245  *
246  * Queries whether a pixbuf has an alpha channel (opacity information).
247  *
248  * Return value: TRUE if it has an alpha channel, FALSE otherwise.
249  **/
250 gboolean
251 gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf)
252 {
253         g_return_val_if_fail (pixbuf != NULL, -1);
254
255         return pixbuf->has_alpha ? TRUE : FALSE;
256 }
257
258 /**
259  * gdk_pixbuf_get_bits_per_sample:
260  * @pixbuf: A pixbuf.
261  *
262  * Queries the number of bits per color sample in a pixbuf.
263  *
264  * Return value: Number of bits per color sample.
265  **/
266 int
267 gdk_pixbuf_get_bits_per_sample (const GdkPixbuf *pixbuf)
268 {
269         g_return_val_if_fail (pixbuf != NULL, -1);
270
271         return pixbuf->bits_per_sample;
272 }
273
274 /**
275  * gdk_pixbuf_get_pixels:
276  * @pixbuf: A pixbuf.
277  *
278  * Queries a pointer to the pixel data of a pixbuf.
279  *
280  * Return value: A pointer to the pixbuf's pixel data.
281  **/
282 guchar *
283 gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)
284 {
285         g_return_val_if_fail (pixbuf != NULL, NULL);
286
287         return pixbuf->pixels;
288 }
289
290 /**
291  * gdk_pixbuf_get_width:
292  * @pixbuf: A pixbuf.
293  *
294  * Queries the width of a pixbuf.
295  *
296  * Return value: Width in pixels.
297  **/
298 int
299 gdk_pixbuf_get_width (const GdkPixbuf *pixbuf)
300 {
301         g_return_val_if_fail (pixbuf != NULL, -1);
302
303         return pixbuf->width;
304 }
305
306 /**
307  * gdk_pixbuf_get_height:
308  * @pixbuf: A pixbuf.
309  *
310  * Queries the height of a pixbuf.
311  *
312  * Return value: Height in pixels.
313  **/
314 int
315 gdk_pixbuf_get_height (const GdkPixbuf *pixbuf)
316 {
317         g_return_val_if_fail (pixbuf != NULL, -1);
318
319         return pixbuf->height;
320 }
321
322 /**
323  * gdk_pixbuf_get_rowstride:
324  * @pixbuf: A pixbuf.
325  *
326  * Queries the rowstride of a pixbuf, which is the number of bytes between rows.
327  *
328  * Return value: Number of bytes between rows.
329  **/
330 int
331 gdk_pixbuf_get_rowstride (const GdkPixbuf *pixbuf)
332 {
333         g_return_val_if_fail (pixbuf != NULL, -1);
334
335         return pixbuf->rowstride;
336 }
337
338 \f
339
340 /* General initialization hooks */
341 const guint gdk_pixbuf_major_version = GDK_PIXBUF_MAJOR;
342 const guint gdk_pixbuf_minor_version = GDK_PIXBUF_MINOR;
343 const guint gdk_pixbuf_micro_version = GDK_PIXBUF_MICRO;
344
345 const char *gdk_pixbuf_version = GDK_PIXBUF_VERSION;
346
347 void
348 gdk_pixbuf_preinit (gpointer app, gpointer modinfo)
349 {
350         g_type_init ();
351 }
352
353 void
354 gdk_pixbuf_postinit (gpointer app, gpointer modinfo)
355 {
356 }
357
358 void
359 gdk_pixbuf_init (void)
360 {
361         gdk_pixbuf_preinit (NULL, NULL);
362         gdk_pixbuf_postinit (NULL, NULL);
363 }
364
365 /* Error quark */
366 GQuark
367 gdk_pixbuf_error_quark (void)
368 {
369   static GQuark q = 0;
370   if (q == 0)
371     q = g_quark_from_static_string ("gdk-pixbuf-error-quark");
372
373   return q;
374 }