]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf.c
New function to create a pixbuf pointing to a subregion of another pixbuf.
[~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, 0);
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 /**
207  * gdk_pixbuf_new_subpixbuf:
208  * @src_pixbuf: a #GdkPixbuf
209  * @src_x: X coord in @src_pixbuf
210  * @src_y: Y coord in @src_pixbuf
211  * @width: width of region in @src_pixbuf
212  * @height: height of region in @src_pixbuf
213  * 
214  * Creates a new pixbuf which represents a sub-region of
215  * @src_pixbuf. The new pixbuf shares its pixels with the
216  * original pixbuf, so writing to one affects both.
217  * The new pixbuf holds a reference to @src_pixbuf, so
218  * @src_pixbuf will not be finalized until the new pixbuf
219  * is finalized.
220  * 
221  * Return value: a new pixbuf 
222  **/
223 GdkPixbuf*
224 gdk_pixbuf_new_subpixbuf (GdkPixbuf *src_pixbuf,
225                           int        src_x,
226                           int        src_y,
227                           int        width,
228                           int        height)
229 {
230         guchar *pixels;
231         GdkPixbuf *sub;
232
233         g_return_val_if_fail (GDK_IS_PIXBUF (src_pixbuf), NULL);
234         g_return_val_if_fail (src_x >= 0 && src_x + width <= src_pixbuf->width, NULL);
235         g_return_val_if_fail (src_y >= 0 && src_y + height <= src_pixbuf->height, NULL);
236
237         pixels = (gdk_pixbuf_get_pixels (src_pixbuf)
238                   + src_y * src_pixbuf->rowstride
239                   + src_x * src_pixbuf->n_channels);
240
241         sub = gdk_pixbuf_new_from_data (pixels,
242                                         src_pixbuf->colorspace,
243                                         src_pixbuf->has_alpha,
244                                         src_pixbuf->bits_per_sample,
245                                         width, height,
246                                         src_pixbuf->rowstride,
247                                         NULL, NULL);
248
249         /* Keep a reference to src_pixbuf */
250         g_object_ref (G_OBJECT (src_pixbuf));
251   
252         g_object_set_qdata_full (G_OBJECT (sub),
253                                  g_quark_from_static_string ("gdk-pixbuf-subpixbuf-src"),
254                                  src_pixbuf,
255                                  (GDestroyNotify) g_object_unref);
256
257         return sub;
258 }
259
260 \f
261
262 /* Accessors */
263
264 /**
265  * gdk_pixbuf_get_colorspace:
266  * @pixbuf: A pixbuf.
267  *
268  * Queries the color space of a pixbuf.
269  *
270  * Return value: Color space.
271  **/
272 GdkColorspace
273 gdk_pixbuf_get_colorspace (const GdkPixbuf *pixbuf)
274 {
275         g_return_val_if_fail (pixbuf != NULL, GDK_COLORSPACE_RGB);
276
277         return pixbuf->colorspace;
278 }
279
280 /**
281  * gdk_pixbuf_get_n_channels:
282  * @pixbuf: A pixbuf.
283  *
284  * Queries the number of channels of a pixbuf.
285  *
286  * Return value: Number of channels.
287  **/
288 int
289 gdk_pixbuf_get_n_channels (const GdkPixbuf *pixbuf)
290 {
291         g_return_val_if_fail (pixbuf != NULL, -1);
292
293         return pixbuf->n_channels;
294 }
295
296 /**
297  * gdk_pixbuf_get_has_alpha:
298  * @pixbuf: A pixbuf.
299  *
300  * Queries whether a pixbuf has an alpha channel (opacity information).
301  *
302  * Return value: TRUE if it has an alpha channel, FALSE otherwise.
303  **/
304 gboolean
305 gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf)
306 {
307         g_return_val_if_fail (pixbuf != NULL, -1);
308
309         return pixbuf->has_alpha ? TRUE : FALSE;
310 }
311
312 /**
313  * gdk_pixbuf_get_bits_per_sample:
314  * @pixbuf: A pixbuf.
315  *
316  * Queries the number of bits per color sample in a pixbuf.
317  *
318  * Return value: Number of bits per color sample.
319  **/
320 int
321 gdk_pixbuf_get_bits_per_sample (const GdkPixbuf *pixbuf)
322 {
323         g_return_val_if_fail (pixbuf != NULL, -1);
324
325         return pixbuf->bits_per_sample;
326 }
327
328 /**
329  * gdk_pixbuf_get_pixels:
330  * @pixbuf: A pixbuf.
331  *
332  * Queries a pointer to the pixel data of a pixbuf.
333  *
334  * Return value: A pointer to the pixbuf's pixel data.
335  **/
336 guchar *
337 gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)
338 {
339         g_return_val_if_fail (pixbuf != NULL, NULL);
340
341         return pixbuf->pixels;
342 }
343
344 /**
345  * gdk_pixbuf_get_width:
346  * @pixbuf: A pixbuf.
347  *
348  * Queries the width of a pixbuf.
349  *
350  * Return value: Width in pixels.
351  **/
352 int
353 gdk_pixbuf_get_width (const GdkPixbuf *pixbuf)
354 {
355         g_return_val_if_fail (pixbuf != NULL, -1);
356
357         return pixbuf->width;
358 }
359
360 /**
361  * gdk_pixbuf_get_height:
362  * @pixbuf: A pixbuf.
363  *
364  * Queries the height of a pixbuf.
365  *
366  * Return value: Height in pixels.
367  **/
368 int
369 gdk_pixbuf_get_height (const GdkPixbuf *pixbuf)
370 {
371         g_return_val_if_fail (pixbuf != NULL, -1);
372
373         return pixbuf->height;
374 }
375
376 /**
377  * gdk_pixbuf_get_rowstride:
378  * @pixbuf: A pixbuf.
379  *
380  * Queries the rowstride of a pixbuf, which is the number of bytes between rows.
381  *
382  * Return value: Number of bytes between rows.
383  **/
384 int
385 gdk_pixbuf_get_rowstride (const GdkPixbuf *pixbuf)
386 {
387         g_return_val_if_fail (pixbuf != NULL, -1);
388
389         return pixbuf->rowstride;
390 }
391
392 \f
393
394 /* General initialization hooks */
395 const guint gdk_pixbuf_major_version = GDK_PIXBUF_MAJOR;
396 const guint gdk_pixbuf_minor_version = GDK_PIXBUF_MINOR;
397 const guint gdk_pixbuf_micro_version = GDK_PIXBUF_MICRO;
398
399 const char *gdk_pixbuf_version = GDK_PIXBUF_VERSION;
400
401 void
402 gdk_pixbuf_preinit (gpointer app, gpointer modinfo)
403 {
404         g_type_init ();
405 }
406
407 void
408 gdk_pixbuf_postinit (gpointer app, gpointer modinfo)
409 {
410 }
411
412 void
413 gdk_pixbuf_init (void)
414 {
415         gdk_pixbuf_preinit (NULL, NULL);
416         gdk_pixbuf_postinit (NULL, NULL);
417 }
418
419 /* Error quark */
420 GQuark
421 gdk_pixbuf_error_quark (void)
422 {
423   static GQuark q = 0;
424   if (q == 0)
425     q = g_quark_from_static_string ("gdk-pixbuf-error-quark");
426
427   return q;
428 }