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