]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf.c
feedback when check and radio buttons are depressed.
[~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 (G_OBJECT(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 (G_OBJECT (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 buffer
134  * has an optimal rowstride.  Note that the buffer is not cleared; you will have
135  * 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, gboolean has_alpha, int bits_per_sample,
142                 int width, int height)
143 {
144         guchar *buf;
145         int channels;
146         int rowstride;
147
148         g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, NULL);
149         g_return_val_if_fail (bits_per_sample == 8, NULL);
150         g_return_val_if_fail (width > 0, NULL);
151         g_return_val_if_fail (height > 0, NULL);
152
153         /* Always align rows to 32-bit boundaries */
154
155         channels = has_alpha ? 4 : 3;
156         rowstride = 4 * ((channels * width + 3) / 4);
157
158         buf = g_try_malloc (height * rowstride);
159         if (!buf)
160                 return NULL;
161
162         return gdk_pixbuf_new_from_data (buf, colorspace, has_alpha, bits_per_sample,
163                                          width, height, rowstride,
164                                          free_buffer, NULL);
165 }
166
167 /**
168  * gdk_pixbuf_copy:
169  * @pixbuf: A pixbuf.
170  * 
171  * Creates a new #GdkPixbuf with a copy of the information in the specified
172  * @pixbuf.
173  * 
174  * Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
175  * not enough memory could be allocated.
176  **/
177 GdkPixbuf *
178 gdk_pixbuf_copy (const GdkPixbuf *pixbuf)
179 {
180         guchar *buf;
181         int size;
182
183         g_return_val_if_fail (pixbuf != NULL, NULL);
184
185         /* Calculate a semi-exact size.  Here we copy with full rowstrides;
186          * maybe we should copy each row individually with the minimum
187          * rowstride?
188          */
189
190         size = ((pixbuf->height - 1) * pixbuf->rowstride +
191                 pixbuf->width * ((pixbuf->n_channels * pixbuf->bits_per_sample + 7) / 8));
192
193         buf = g_try_malloc (size * sizeof (guchar));
194         if (!buf)
195                 return NULL;
196
197         memcpy (buf, pixbuf->pixels, size);
198
199         return gdk_pixbuf_new_from_data (buf,
200                                          pixbuf->colorspace, pixbuf->has_alpha,
201                                          pixbuf->bits_per_sample,
202                                          pixbuf->width, pixbuf->height,
203                                          pixbuf->rowstride,
204                                          free_buffer,
205                                          NULL);
206 }
207
208 /**
209  * gdk_pixbuf_new_subpixbuf:
210  * @src_pixbuf: a #GdkPixbuf
211  * @src_x: X coord in @src_pixbuf
212  * @src_y: Y coord in @src_pixbuf
213  * @width: width of region in @src_pixbuf
214  * @height: height of region in @src_pixbuf
215  * 
216  * Creates a new pixbuf which represents a sub-region of
217  * @src_pixbuf. The new pixbuf shares its pixels with the
218  * original pixbuf, so writing to one affects both.
219  * The new pixbuf holds a reference to @src_pixbuf, so
220  * @src_pixbuf will not be finalized until the new pixbuf
221  * is finalized.
222  * 
223  * Return value: a new pixbuf 
224  **/
225 GdkPixbuf*
226 gdk_pixbuf_new_subpixbuf (GdkPixbuf *src_pixbuf,
227                           int        src_x,
228                           int        src_y,
229                           int        width,
230                           int        height)
231 {
232         guchar *pixels;
233         GdkPixbuf *sub;
234
235         g_return_val_if_fail (GDK_IS_PIXBUF (src_pixbuf), NULL);
236         g_return_val_if_fail (src_x >= 0 && src_x + width <= src_pixbuf->width, NULL);
237         g_return_val_if_fail (src_y >= 0 && src_y + height <= src_pixbuf->height, NULL);
238
239         pixels = (gdk_pixbuf_get_pixels (src_pixbuf)
240                   + src_y * src_pixbuf->rowstride
241                   + src_x * src_pixbuf->n_channels);
242
243         sub = gdk_pixbuf_new_from_data (pixels,
244                                         src_pixbuf->colorspace,
245                                         src_pixbuf->has_alpha,
246                                         src_pixbuf->bits_per_sample,
247                                         width, height,
248                                         src_pixbuf->rowstride,
249                                         NULL, NULL);
250
251         /* Keep a reference to src_pixbuf */
252         g_object_ref (src_pixbuf);
253   
254         g_object_set_qdata_full (G_OBJECT (sub),
255                                  g_quark_from_static_string ("gdk-pixbuf-subpixbuf-src"),
256                                  src_pixbuf,
257                                  (GDestroyNotify) g_object_unref);
258
259         return sub;
260 }
261
262 \f
263
264 /* Accessors */
265
266 /**
267  * gdk_pixbuf_get_colorspace:
268  * @pixbuf: A pixbuf.
269  *
270  * Queries the color space of a pixbuf.
271  *
272  * Return value: Color space.
273  **/
274 GdkColorspace
275 gdk_pixbuf_get_colorspace (const GdkPixbuf *pixbuf)
276 {
277         g_return_val_if_fail (pixbuf != NULL, GDK_COLORSPACE_RGB);
278
279         return pixbuf->colorspace;
280 }
281
282 /**
283  * gdk_pixbuf_get_n_channels:
284  * @pixbuf: A pixbuf.
285  *
286  * Queries the number of channels of a pixbuf.
287  *
288  * Return value: Number of channels.
289  **/
290 int
291 gdk_pixbuf_get_n_channels (const GdkPixbuf *pixbuf)
292 {
293         g_return_val_if_fail (pixbuf != NULL, -1);
294
295         return pixbuf->n_channels;
296 }
297
298 /**
299  * gdk_pixbuf_get_has_alpha:
300  * @pixbuf: A pixbuf.
301  *
302  * Queries whether a pixbuf has an alpha channel (opacity information).
303  *
304  * Return value: %TRUE if it has an alpha channel, %FALSE otherwise.
305  **/
306 gboolean
307 gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf)
308 {
309         g_return_val_if_fail (pixbuf != NULL, -1);
310
311         return pixbuf->has_alpha ? TRUE : FALSE;
312 }
313
314 /**
315  * gdk_pixbuf_get_bits_per_sample:
316  * @pixbuf: A pixbuf.
317  *
318  * Queries the number of bits per color sample in a pixbuf.
319  *
320  * Return value: Number of bits per color sample.
321  **/
322 int
323 gdk_pixbuf_get_bits_per_sample (const GdkPixbuf *pixbuf)
324 {
325         g_return_val_if_fail (pixbuf != NULL, -1);
326
327         return pixbuf->bits_per_sample;
328 }
329
330 /**
331  * gdk_pixbuf_get_pixels:
332  * @pixbuf: A pixbuf.
333  *
334  * Queries a pointer to the pixel data of a pixbuf.
335  *
336  * Return value: A pointer to the pixbuf's pixel data.
337  **/
338 guchar *
339 gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)
340 {
341         g_return_val_if_fail (pixbuf != NULL, NULL);
342
343         return pixbuf->pixels;
344 }
345
346 /**
347  * gdk_pixbuf_get_width:
348  * @pixbuf: A pixbuf.
349  *
350  * Queries the width of a pixbuf.
351  *
352  * Return value: Width in pixels.
353  **/
354 int
355 gdk_pixbuf_get_width (const GdkPixbuf *pixbuf)
356 {
357         g_return_val_if_fail (pixbuf != NULL, -1);
358
359         return pixbuf->width;
360 }
361
362 /**
363  * gdk_pixbuf_get_height:
364  * @pixbuf: A pixbuf.
365  *
366  * Queries the height of a pixbuf.
367  *
368  * Return value: Height in pixels.
369  **/
370 int
371 gdk_pixbuf_get_height (const GdkPixbuf *pixbuf)
372 {
373         g_return_val_if_fail (pixbuf != NULL, -1);
374
375         return pixbuf->height;
376 }
377
378 /**
379  * gdk_pixbuf_get_rowstride:
380  * @pixbuf: A pixbuf.
381  *
382  * Queries the rowstride of a pixbuf, which is the number of bytes between rows.
383  *
384  * Return value: Number of bytes between rows.
385  **/
386 int
387 gdk_pixbuf_get_rowstride (const GdkPixbuf *pixbuf)
388 {
389         g_return_val_if_fail (pixbuf != NULL, -1);
390
391         return pixbuf->rowstride;
392 }
393
394 \f
395
396 /* General initialization hooks */
397 const guint gdk_pixbuf_major_version = GDK_PIXBUF_MAJOR;
398 const guint gdk_pixbuf_minor_version = GDK_PIXBUF_MINOR;
399 const guint gdk_pixbuf_micro_version = GDK_PIXBUF_MICRO;
400
401 const char *gdk_pixbuf_version = GDK_PIXBUF_VERSION;
402
403 /* Error quark */
404 GQuark
405 gdk_pixbuf_error_quark (void)
406 {
407   static GQuark q = 0;
408   if (q == 0)
409     q = g_quark_from_static_string ("gdk-pixbuf-error-quark");
410
411   return q;
412 }
413
414 /**
415  * gdk_pixbuf_fill:
416  * @pixbuf: a #GdkPixbuf
417  * @pixel: RGBA pixel to clear to (0xffffffff is opaque white, 0x00000000 transparent black)
418  *
419  * Clears a pixbuf to the given RGBA value, converting the RGBA value into
420  * the pixbuf's pixel format. The alpha will be ignored if the pixbuf
421  * doesn't have an alpha channel.
422  * 
423  **/
424 void
425 gdk_pixbuf_fill (GdkPixbuf *pixbuf,
426                  guint32    pixel)
427 {
428         guchar *pixels;
429         gboolean all_the_same = FALSE;
430         guint r, g, b, a;
431         guchar *p;
432         gint n;
433
434         g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
435
436         if (pixbuf->width == 0 || pixbuf->height == 0)
437           return;
438
439         pixels = pixbuf->pixels;
440
441         r = (pixel & 0xff000000) >> 24;
442         g = (pixel & 0x00ff0000) >> 16;
443         b = (pixel & 0x0000ff00) >> 8;
444         a = (pixel & 0x000000ff);
445
446         if (r == g && g == b) {
447                 if (!pixbuf->has_alpha)
448                         all_the_same = TRUE;
449                 else
450                         all_the_same = (r == a);
451         }
452         
453         if (all_the_same) {
454                 if (pixbuf->has_alpha)
455                         memset (pixels, r, pixbuf->width * 4);
456                 else
457                         memset (pixels, r, pixbuf->width * 3);
458
459         } else {
460                 guchar  c[4];
461
462                 c[0] = r; c[1] = g; c[2] = b; c[3] = a;
463                 
464                 p = pixels;
465                 n = pixbuf->width;
466                 if (pixbuf->has_alpha) {
467                         do {
468                                 memcpy (p, c, 4);
469                                 p += 4;
470                         } while (--n);
471                 } else {
472                         do {
473                                 memcpy (p, c, 3);
474                                 p += 3;
475                         } while (--n);
476                 }
477
478         }
479         p = pixels;
480         n = pixbuf->height - 1;
481         while (n--) {
482                 p += pixbuf->rowstride;
483                 memcpy (p, pixels, pixbuf->width * pixbuf->n_channels);
484         }
485 }
486
487 \f
488
489 /**
490  * gdk_pixbuf_get_option:
491  * @pixbuf: a #GdkPixbuf
492  * @key: a nul-terminated string.
493  * 
494  * Looks up @key in the list of options that may have been attached to the
495  * @pixbuf when it was loaded. 
496  * 
497  * Return value: the value associated with @key. This is a nul-terminated 
498  * string that should not be freed or %NULL if @key was not found.
499  **/
500 G_CONST_RETURN gchar *
501 gdk_pixbuf_get_option (GdkPixbuf   *pixbuf,
502                        const gchar *key)
503 {
504         gchar **options;
505         gint i;
506
507         g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
508         g_return_val_if_fail (key != NULL, NULL);
509   
510         options = g_object_get_qdata (G_OBJECT (pixbuf), 
511                                       g_quark_from_static_string ("gdk_pixbuf_options"));
512         if (options) {
513                 for (i = 0; options[2*i]; i++) {
514                         if (strcmp (options[2*i], key) == 0)
515                                 return options[2*i+1];
516                 }
517         }
518         
519         return NULL;
520 }
521
522 /**
523  * gdk_pixbuf_set_option:
524  * @pixbuf: a #GdkPixbuf
525  * @key: a nul-terminated string.
526  * @value: a nul-terminated string.
527  * 
528  * Attaches a key/value pair as an option to a #GdkPixbuf. If %key already
529  * exists in the list of options attached to @pixbuf, the new value is 
530  * ignored and %FALSE is returned.
531  *
532  * Return value: %TRUE on success.
533  **/
534 gboolean
535 gdk_pixbuf_set_option (GdkPixbuf   *pixbuf,
536                        const gchar *key,
537                        const gchar *value)
538 {
539         GQuark  quark;
540         gchar **options;
541         gint n = 0;
542  
543         g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
544         g_return_val_if_fail (key != NULL, FALSE);
545         g_return_val_if_fail (value != NULL, FALSE);
546
547         quark = g_quark_from_static_string ("gdk_pixbuf_options");
548
549         options = g_object_get_qdata (G_OBJECT (pixbuf), quark);
550
551         if (options) {
552                 for (n = 0; options[2*n]; n++) {
553                         if (strcmp (options[2*n], key) == 0)
554                                 return FALSE;
555                 }
556
557                 g_object_steal_qdata (G_OBJECT (pixbuf), quark);
558                 options = g_renew (gchar *, options, 2*(n+1) + 1);
559         } else {
560                 options = g_new (gchar *, 3);
561         }
562         
563         options[2*n]   = g_strdup (key);
564         options[2*n+1] = g_strdup (value);
565         options[2*n+2] = NULL;
566
567         g_object_set_qdata_full (G_OBJECT (pixbuf), quark,
568                                  options, (GDestroyNotify) g_strfreev);
569         
570         return TRUE;
571 }
572
573 \f
574
575 /* Include the marshallers */
576 #include <glib-object.h>
577 #include "gdk-pixbuf-marshal.c"