]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf.c
1a0a1df6196338c28116a8aa9cd4213ded3d4a39
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - Basic memory management
3  *
4  * Copyright (C) 1999 The Free Software Foundation
5  *
6  * Authors: Mark Crichton <crichton@gimp.org>
7  *          Miguel de Icaza <miguel@gnu.org>
8  *          Federico Mena-Quintero <federico@gimp.org>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 #include <config.h>
27 #include <math.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "gdk-pixbuf.h"
31 #include "gdk-pixbuf-private.h"
32
33 static void gdk_pixbuf_class_init  (GdkPixbufClass *klass);
34 static void gdk_pixbuf_finalize    (GObject        *object);
35
36 \f
37
38 static gpointer parent_class;
39
40 GType
41 gdk_pixbuf_get_type (void)
42 {
43         static GType object_type = 0;
44
45         if (!object_type) {
46                 static const GTypeInfo object_info = {
47                         sizeof (GdkPixbufClass),
48                         (GBaseInitFunc) NULL,
49                         (GBaseFinalizeFunc) NULL,
50                         (GClassInitFunc) gdk_pixbuf_class_init,
51                         NULL,           /* class_finalize */
52                         NULL,           /* class_data */
53                         sizeof (GdkPixbuf),
54                         0,              /* n_preallocs */
55                         (GInstanceInitFunc) NULL,
56                 };
57                 
58                 object_type = g_type_register_static (G_TYPE_OBJECT,
59                                                       "GdkPixbuf",
60                                                       &object_info, 0);
61         }
62   
63         return object_type;
64 }
65
66 static void
67 gdk_pixbuf_class_init (GdkPixbufClass *klass)
68 {
69         GObjectClass *object_class = G_OBJECT_CLASS (klass);
70         
71         parent_class = g_type_class_peek_parent (klass);
72         
73         object_class->finalize = gdk_pixbuf_finalize;
74 }
75
76 static void
77 gdk_pixbuf_finalize (GObject *object)
78 {
79         GdkPixbuf *pixbuf = GDK_PIXBUF (object);
80         
81         if (pixbuf->destroy_fn)
82                 (* pixbuf->destroy_fn) (pixbuf->pixels, pixbuf->destroy_fn_data);
83         
84         G_OBJECT_CLASS (parent_class)->finalize (object);
85 }
86 \f
87
88 /**
89  * gdk_pixbuf_ref:
90  * @pixbuf: A pixbuf.
91  *
92  * Adds a reference to a pixbuf. Deprecated; use g_object_ref().
93  *
94  * Return value: The same as the @pixbuf argument.
95  **/
96 GdkPixbuf *
97 gdk_pixbuf_ref (GdkPixbuf *pixbuf)
98 {
99         return (GdkPixbuf *) g_object_ref (pixbuf);
100 }
101
102 /**
103  * gdk_pixbuf_unref:
104  * @pixbuf: A pixbuf.
105  *
106  * Removes a reference from a pixbuf. Deprecated; use
107  * g_object_unref().
108  *
109  **/
110 void
111 gdk_pixbuf_unref (GdkPixbuf *pixbuf)
112 {
113         g_object_unref (pixbuf);
114 }
115
116 \f
117
118 /* Used as the destroy notification function for gdk_pixbuf_new() */
119 static void
120 free_buffer (guchar *pixels, gpointer data)
121 {
122         g_free (pixels);
123 }
124
125 /**
126  * gdk_pixbuf_new:
127  * @colorspace: Color space for image.
128  * @has_alpha: Whether the image should have transparency information.
129  * @bits_per_sample: Number of bits per color sample.
130  * @width: Width of image in pixels.
131  * @height: Height of image in pixels.
132  *
133  * Creates a new #GdkPixbuf structure and allocates a buffer for it.  The 
134  * buffer has an optimal rowstride.  Note that the buffer is not cleared;
135  * you will have to fill it completely yourself.
136  *
137  * Return value: A newly-created #GdkPixbuf with a reference count of 1, or 
138  * %NULL if not enough memory could be allocated for the image buffer.
139  **/
140 GdkPixbuf *
141 gdk_pixbuf_new (GdkColorspace colorspace, 
142                 gboolean      has_alpha,
143                 int           bits_per_sample,
144                 int           width,
145                 int           height)
146 {
147         guchar *buf;
148         int channels;
149         int rowstride;
150         gsize bytes;
151
152         g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, NULL);
153         g_return_val_if_fail (bits_per_sample == 8, NULL);
154         g_return_val_if_fail (width > 0, NULL);
155         g_return_val_if_fail (height > 0, NULL);
156
157         if (width <= 0 || height <= 0)
158                 return NULL;
159
160         channels = has_alpha ? 4 : 3;
161         rowstride = width * channels;
162         if (rowstride / channels != width || rowstride + 3 < 0) /* overflow */
163                 return NULL;
164         
165         /* Always align rows to 32-bit boundaries */
166         rowstride = (rowstride + 3) & ~3;
167
168         bytes = height * rowstride;
169         if (bytes / rowstride !=  height) /* overflow */
170                 return NULL;
171             
172         buf = g_try_malloc (bytes);
173         if (!buf)
174                 return NULL;
175
176         return gdk_pixbuf_new_from_data (buf, colorspace, has_alpha, bits_per_sample,
177                                          width, height, rowstride,
178                                          free_buffer, NULL);
179 }
180
181 /**
182  * gdk_pixbuf_copy:
183  * @pixbuf: A pixbuf.
184  * 
185  * Creates a new #GdkPixbuf with a copy of the information in the specified
186  * @pixbuf.
187  * 
188  * Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
189  * not enough memory could be allocated.
190  **/
191 GdkPixbuf *
192 gdk_pixbuf_copy (const GdkPixbuf *pixbuf)
193 {
194         guchar *buf;
195         int size;
196
197         g_return_val_if_fail (pixbuf != NULL, NULL);
198
199         /* Calculate a semi-exact size.  Here we copy with full rowstrides;
200          * maybe we should copy each row individually with the minimum
201          * rowstride?
202          */
203
204         size = ((pixbuf->height - 1) * pixbuf->rowstride +
205                 pixbuf->width * ((pixbuf->n_channels * pixbuf->bits_per_sample + 7) / 8));
206
207         buf = g_try_malloc (size * sizeof (guchar));
208         if (!buf)
209                 return NULL;
210
211         memcpy (buf, pixbuf->pixels, size);
212
213         return gdk_pixbuf_new_from_data (buf,
214                                          pixbuf->colorspace, pixbuf->has_alpha,
215                                          pixbuf->bits_per_sample,
216                                          pixbuf->width, pixbuf->height,
217                                          pixbuf->rowstride,
218                                          free_buffer,
219                                          NULL);
220 }
221
222 /**
223  * gdk_pixbuf_new_subpixbuf:
224  * @src_pixbuf: a #GdkPixbuf
225  * @src_x: X coord in @src_pixbuf
226  * @src_y: Y coord in @src_pixbuf
227  * @width: width of region in @src_pixbuf
228  * @height: height of region in @src_pixbuf
229  * 
230  * Creates a new pixbuf which represents a sub-region of
231  * @src_pixbuf. The new pixbuf shares its pixels with the
232  * original pixbuf, so writing to one affects both.
233  * The new pixbuf holds a reference to @src_pixbuf, so
234  * @src_pixbuf will not be finalized until the new pixbuf
235  * is finalized.
236  * 
237  * Return value: a new pixbuf 
238  **/
239 GdkPixbuf*
240 gdk_pixbuf_new_subpixbuf (GdkPixbuf *src_pixbuf,
241                           int        src_x,
242                           int        src_y,
243                           int        width,
244                           int        height)
245 {
246         guchar *pixels;
247         GdkPixbuf *sub;
248
249         g_return_val_if_fail (GDK_IS_PIXBUF (src_pixbuf), NULL);
250         g_return_val_if_fail (src_x >= 0 && src_x + width <= src_pixbuf->width, NULL);
251         g_return_val_if_fail (src_y >= 0 && src_y + height <= src_pixbuf->height, NULL);
252
253         pixels = (gdk_pixbuf_get_pixels (src_pixbuf)
254                   + src_y * src_pixbuf->rowstride
255                   + src_x * src_pixbuf->n_channels);
256
257         sub = gdk_pixbuf_new_from_data (pixels,
258                                         src_pixbuf->colorspace,
259                                         src_pixbuf->has_alpha,
260                                         src_pixbuf->bits_per_sample,
261                                         width, height,
262                                         src_pixbuf->rowstride,
263                                         NULL, NULL);
264
265         /* Keep a reference to src_pixbuf */
266         g_object_ref (src_pixbuf);
267   
268         g_object_set_qdata_full (G_OBJECT (sub),
269                                  g_quark_from_static_string ("gdk-pixbuf-subpixbuf-src"),
270                                  src_pixbuf,
271                                  (GDestroyNotify) g_object_unref);
272
273         return sub;
274 }
275
276 \f
277
278 /* Accessors */
279
280 /**
281  * gdk_pixbuf_get_colorspace:
282  * @pixbuf: A pixbuf.
283  *
284  * Queries the color space of a pixbuf.
285  *
286  * Return value: Color space.
287  **/
288 GdkColorspace
289 gdk_pixbuf_get_colorspace (const GdkPixbuf *pixbuf)
290 {
291         g_return_val_if_fail (pixbuf != NULL, GDK_COLORSPACE_RGB);
292
293         return pixbuf->colorspace;
294 }
295
296 /**
297  * gdk_pixbuf_get_n_channels:
298  * @pixbuf: A pixbuf.
299  *
300  * Queries the number of channels of a pixbuf.
301  *
302  * Return value: Number of channels.
303  **/
304 int
305 gdk_pixbuf_get_n_channels (const GdkPixbuf *pixbuf)
306 {
307         g_return_val_if_fail (pixbuf != NULL, -1);
308
309         return pixbuf->n_channels;
310 }
311
312 /**
313  * gdk_pixbuf_get_has_alpha:
314  * @pixbuf: A pixbuf.
315  *
316  * Queries whether a pixbuf has an alpha channel (opacity information).
317  *
318  * Return value: %TRUE if it has an alpha channel, %FALSE otherwise.
319  **/
320 gboolean
321 gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf)
322 {
323         g_return_val_if_fail (pixbuf != NULL, -1);
324
325         return pixbuf->has_alpha ? TRUE : FALSE;
326 }
327
328 /**
329  * gdk_pixbuf_get_bits_per_sample:
330  * @pixbuf: A pixbuf.
331  *
332  * Queries the number of bits per color sample in a pixbuf.
333  *
334  * Return value: Number of bits per color sample.
335  **/
336 int
337 gdk_pixbuf_get_bits_per_sample (const GdkPixbuf *pixbuf)
338 {
339         g_return_val_if_fail (pixbuf != NULL, -1);
340
341         return pixbuf->bits_per_sample;
342 }
343
344 /**
345  * gdk_pixbuf_get_pixels:
346  * @pixbuf: A pixbuf.
347  *
348  * Queries a pointer to the pixel data of a pixbuf.
349  *
350  * Return value: A pointer to the pixbuf's pixel data.
351  **/
352 guchar *
353 gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)
354 {
355         g_return_val_if_fail (pixbuf != NULL, NULL);
356
357         return pixbuf->pixels;
358 }
359
360 /**
361  * gdk_pixbuf_get_width:
362  * @pixbuf: A pixbuf.
363  *
364  * Queries the width of a pixbuf.
365  *
366  * Return value: Width in pixels.
367  **/
368 int
369 gdk_pixbuf_get_width (const GdkPixbuf *pixbuf)
370 {
371         g_return_val_if_fail (pixbuf != NULL, -1);
372
373         return pixbuf->width;
374 }
375
376 /**
377  * gdk_pixbuf_get_height:
378  * @pixbuf: A pixbuf.
379  *
380  * Queries the height of a pixbuf.
381  *
382  * Return value: Height in pixels.
383  **/
384 int
385 gdk_pixbuf_get_height (const GdkPixbuf *pixbuf)
386 {
387         g_return_val_if_fail (pixbuf != NULL, -1);
388
389         return pixbuf->height;
390 }
391
392 /**
393  * gdk_pixbuf_get_rowstride:
394  * @pixbuf: A pixbuf.
395  *
396  * Queries the rowstride of a pixbuf, which is the number of bytes between rows.
397  *
398  * Return value: Number of bytes between rows.
399  **/
400 int
401 gdk_pixbuf_get_rowstride (const GdkPixbuf *pixbuf)
402 {
403         g_return_val_if_fail (pixbuf != NULL, -1);
404
405         return pixbuf->rowstride;
406 }
407
408 \f
409
410 /* General initialization hooks */
411 const guint gdk_pixbuf_major_version = GDK_PIXBUF_MAJOR;
412 const guint gdk_pixbuf_minor_version = GDK_PIXBUF_MINOR;
413 const guint gdk_pixbuf_micro_version = GDK_PIXBUF_MICRO;
414
415 const char *gdk_pixbuf_version = GDK_PIXBUF_VERSION;
416
417 /* Error quark */
418 GQuark
419 gdk_pixbuf_error_quark (void)
420 {
421   static GQuark q = 0;
422   if (q == 0)
423     q = g_quark_from_static_string ("gdk-pixbuf-error-quark");
424
425   return q;
426 }
427
428 /**
429  * gdk_pixbuf_fill:
430  * @pixbuf: a #GdkPixbuf
431  * @pixel: RGBA pixel to clear to
432  *         (0xffffffff is opaque white, 0x00000000 transparent black)
433  *
434  * Clears a pixbuf to the given RGBA value, converting the RGBA value into
435  * the pixbuf's pixel format. The alpha will be ignored if the pixbuf
436  * doesn't have an alpha channel.
437  * 
438  **/
439 void
440 gdk_pixbuf_fill (GdkPixbuf *pixbuf,
441                  guint32    pixel)
442 {
443         guchar *pixels;
444         guint r, g, b, a;
445         guchar *p;
446         guint w, h;
447
448         g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
449
450         if (pixbuf->width == 0 || pixbuf->height == 0)
451                 return;
452
453         pixels = pixbuf->pixels;
454
455         r = (pixel & 0xff000000) >> 24;
456         g = (pixel & 0x00ff0000) >> 16;
457         b = (pixel & 0x0000ff00) >> 8;
458         a = (pixel & 0x000000ff);
459
460         h = pixbuf->height;
461         
462         while (h--) {
463                 w = pixbuf->width;
464                 p = pixels;
465
466                 switch (pixbuf->n_channels) {
467                 case 3:
468                         while (w--) {
469                                 p[0] = r;
470                                 p[1] = g;
471                                 p[2] = b;
472                                 p += 3;
473                         }
474                         break;
475                 case 4:
476                         while (w--) {
477                                 p[0] = r;
478                                 p[1] = g;
479                                 p[2] = b;
480                                 p[3] = a;
481                                 p += 4;
482                         }
483                         break;
484                 default:
485                         break;
486                 }
487                 
488                 pixels += pixbuf->rowstride;
489         }
490 }
491
492 \f
493
494 /**
495  * gdk_pixbuf_get_option:
496  * @pixbuf: a #GdkPixbuf
497  * @key: a nul-terminated string.
498  * 
499  * Looks up @key in the list of options that may have been attached to the
500  * @pixbuf when it was loaded. 
501  * 
502  * Return value: the value associated with @key. This is a nul-terminated 
503  * string that should not be freed or %NULL if @key was not found.
504  **/
505 G_CONST_RETURN gchar *
506 gdk_pixbuf_get_option (GdkPixbuf   *pixbuf,
507                        const gchar *key)
508 {
509         gchar **options;
510         gint i;
511
512         g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
513         g_return_val_if_fail (key != NULL, NULL);
514   
515         options = g_object_get_qdata (G_OBJECT (pixbuf), 
516                                       g_quark_from_static_string ("gdk_pixbuf_options"));
517         if (options) {
518                 for (i = 0; options[2*i]; i++) {
519                         if (strcmp (options[2*i], key) == 0)
520                                 return options[2*i+1];
521                 }
522         }
523         
524         return NULL;
525 }
526
527 /**
528  * gdk_pixbuf_set_option:
529  * @pixbuf: a #GdkPixbuf
530  * @key: a nul-terminated string.
531  * @value: a nul-terminated string.
532  * 
533  * Attaches a key/value pair as an option to a #GdkPixbuf. If %key already
534  * exists in the list of options attached to @pixbuf, the new value is 
535  * ignored and %FALSE is returned.
536  *
537  * Return value: %TRUE on success.
538  **/
539 gboolean
540 gdk_pixbuf_set_option (GdkPixbuf   *pixbuf,
541                        const gchar *key,
542                        const gchar *value)
543 {
544         GQuark  quark;
545         gchar **options;
546         gint n = 0;
547  
548         g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
549         g_return_val_if_fail (key != NULL, FALSE);
550         g_return_val_if_fail (value != NULL, FALSE);
551
552         quark = g_quark_from_static_string ("gdk_pixbuf_options");
553
554         options = g_object_get_qdata (G_OBJECT (pixbuf), quark);
555
556         if (options) {
557                 for (n = 0; options[2*n]; n++) {
558                         if (strcmp (options[2*n], key) == 0)
559                                 return FALSE;
560                 }
561
562                 g_object_steal_qdata (G_OBJECT (pixbuf), quark);
563                 options = g_renew (gchar *, options, 2*(n+1) + 1);
564         } else {
565                 options = g_new (gchar *, 3);
566         }
567         
568         options[2*n]   = g_strdup (key);
569         options[2*n+1] = g_strdup (value);
570         options[2*n+2] = NULL;
571
572         g_object_set_qdata_full (G_OBJECT (pixbuf), quark,
573                                  options, (GDestroyNotify) g_strfreev);
574         
575         return TRUE;
576 }
577
578 \f
579
580 /* Include the marshallers */
581 #include <glib-object.h>
582 #include "gdk-pixbuf-marshal.c"