1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - Basic memory management
4 * Copyright (C) 1999 The Free Software Foundation
6 * Authors: Mark Crichton <crichton@gimp.org>
7 * Miguel de Icaza <miguel@gnu.org>
8 * Federico Mena-Quintero <federico@gimp.org>
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.
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.
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.
30 #include "gdk-pixbuf.h"
31 #include "gdk-pixbuf-private.h"
33 static void gdk_pixbuf_class_init (GdkPixbufClass *klass);
34 static void gdk_pixbuf_finalize (GObject *object);
35 static void gdk_pixbuf_set_property (GObject *object,
39 static void gdk_pixbuf_get_property (GObject *object,
58 static gpointer parent_class;
61 gdk_pixbuf_get_type (void)
63 static GType object_type = 0;
66 static const GTypeInfo object_info = {
67 sizeof (GdkPixbufClass),
69 (GBaseFinalizeFunc) NULL,
70 (GClassInitFunc) gdk_pixbuf_class_init,
71 NULL, /* class_finalize */
72 NULL, /* class_data */
75 (GInstanceInitFunc) NULL,
78 object_type = g_type_register_static (G_TYPE_OBJECT,
87 gdk_pixbuf_class_init (GdkPixbufClass *klass)
89 GObjectClass *object_class = G_OBJECT_CLASS (klass);
91 parent_class = g_type_class_peek_parent (klass);
93 object_class->finalize = gdk_pixbuf_finalize;
94 object_class->set_property = gdk_pixbuf_set_property;
95 object_class->get_property = gdk_pixbuf_get_property;
97 g_object_class_install_property (object_class,
99 g_param_spec_int ("n_channels",
100 P_("Number of Channels"),
101 P_("The number of samples per pixel"),
106 G_PARAM_CONSTRUCT_ONLY));
108 g_object_class_install_property (object_class,
110 g_param_spec_enum ("colorspace",
112 P_("The colorspace in which the samples are interpreted"),
117 G_PARAM_CONSTRUCT_ONLY));
119 g_object_class_install_property (object_class,
121 g_param_spec_boolean ("has_alpha",
123 P_("Whether the pixbuf has an alpha channel"),
126 G_PARAM_CONSTRUCT_ONLY));
128 g_object_class_install_property (object_class,
129 PROP_BITS_PER_SAMPLE,
130 g_param_spec_int ("bits_per_sample",
131 P_("Bits per Sample"),
132 P_("The number of bits per sample"),
137 G_PARAM_CONSTRUCT_ONLY));
139 g_object_class_install_property (object_class,
141 g_param_spec_int ("width",
143 P_("The number of columns of the pixbuf"),
148 G_PARAM_CONSTRUCT_ONLY));
150 g_object_class_install_property (object_class,
152 g_param_spec_int ("height",
154 P_("The number of rows of the pixbuf"),
159 G_PARAM_CONSTRUCT_ONLY));
161 g_object_class_install_property (object_class,
163 g_param_spec_int ("rowstride",
165 P_("The number of bytes between the start of a row and the start of the next row"),
170 G_PARAM_CONSTRUCT_ONLY));
172 g_object_class_install_property (object_class,
174 g_param_spec_pointer ("pixels",
176 P_("A pointer to the pixel data of the pixbuf"),
178 G_PARAM_CONSTRUCT_ONLY));
182 gdk_pixbuf_finalize (GObject *object)
184 GdkPixbuf *pixbuf = GDK_PIXBUF (object);
186 if (pixbuf->destroy_fn)
187 (* pixbuf->destroy_fn) (pixbuf->pixels, pixbuf->destroy_fn_data);
189 G_OBJECT_CLASS (parent_class)->finalize (object);
197 * Adds a reference to a pixbuf.
199 * Return value: The same as the @pixbuf argument.
201 * Deprecated: Use g_object_ref().
204 gdk_pixbuf_ref (GdkPixbuf *pixbuf)
206 return (GdkPixbuf *) g_object_ref (pixbuf);
213 * Removes a reference from a pixbuf.
215 * Deprecated: Use g_object_unref().
218 gdk_pixbuf_unref (GdkPixbuf *pixbuf)
220 g_object_unref (pixbuf);
225 /* Used as the destroy notification function for gdk_pixbuf_new() */
227 free_buffer (guchar *pixels, gpointer data)
234 * @colorspace: Color space for image.
235 * @has_alpha: Whether the image should have transparency information.
236 * @bits_per_sample: Number of bits per color sample.
237 * @width: Width of image in pixels.
238 * @height: Height of image in pixels.
240 * Creates a new #GdkPixbuf structure and allocates a buffer for it. The
241 * buffer has an optimal rowstride. Note that the buffer is not cleared;
242 * you will have to fill it completely yourself.
244 * Return value: A newly-created #GdkPixbuf with a reference count of 1, or
245 * %NULL if not enough memory could be allocated for the image buffer.
248 gdk_pixbuf_new (GdkColorspace colorspace,
259 g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, NULL);
260 g_return_val_if_fail (bits_per_sample == 8, NULL);
261 g_return_val_if_fail (width > 0, NULL);
262 g_return_val_if_fail (height > 0, NULL);
264 if (width <= 0 || height <= 0)
267 channels = has_alpha ? 4 : 3;
268 rowstride = width * channels;
269 if (rowstride / channels != width || rowstride + 3 < 0) /* overflow */
272 /* Always align rows to 32-bit boundaries */
273 rowstride = (rowstride + 3) & ~3;
275 bytes = height * rowstride;
276 if (bytes / rowstride != height) /* overflow */
279 buf = g_try_malloc (bytes);
283 return gdk_pixbuf_new_from_data (buf, colorspace, has_alpha, bits_per_sample,
284 width, height, rowstride,
292 * Creates a new #GdkPixbuf with a copy of the information in the specified
295 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
296 * not enough memory could be allocated.
299 gdk_pixbuf_copy (const GdkPixbuf *pixbuf)
304 g_return_val_if_fail (pixbuf != NULL, NULL);
306 /* Calculate a semi-exact size. Here we copy with full rowstrides;
307 * maybe we should copy each row individually with the minimum
311 size = ((pixbuf->height - 1) * pixbuf->rowstride +
312 pixbuf->width * ((pixbuf->n_channels * pixbuf->bits_per_sample + 7) / 8));
314 buf = g_try_malloc (size * sizeof (guchar));
318 memcpy (buf, pixbuf->pixels, size);
320 return gdk_pixbuf_new_from_data (buf,
321 pixbuf->colorspace, pixbuf->has_alpha,
322 pixbuf->bits_per_sample,
323 pixbuf->width, pixbuf->height,
330 * gdk_pixbuf_new_subpixbuf:
331 * @src_pixbuf: a #GdkPixbuf
332 * @src_x: X coord in @src_pixbuf
333 * @src_y: Y coord in @src_pixbuf
334 * @width: width of region in @src_pixbuf
335 * @height: height of region in @src_pixbuf
337 * Creates a new pixbuf which represents a sub-region of
338 * @src_pixbuf. The new pixbuf shares its pixels with the
339 * original pixbuf, so writing to one affects both.
340 * The new pixbuf holds a reference to @src_pixbuf, so
341 * @src_pixbuf will not be finalized until the new pixbuf
344 * Return value: a new pixbuf
347 gdk_pixbuf_new_subpixbuf (GdkPixbuf *src_pixbuf,
356 g_return_val_if_fail (GDK_IS_PIXBUF (src_pixbuf), NULL);
357 g_return_val_if_fail (src_x >= 0 && src_x + width <= src_pixbuf->width, NULL);
358 g_return_val_if_fail (src_y >= 0 && src_y + height <= src_pixbuf->height, NULL);
360 pixels = (gdk_pixbuf_get_pixels (src_pixbuf)
361 + src_y * src_pixbuf->rowstride
362 + src_x * src_pixbuf->n_channels);
364 sub = gdk_pixbuf_new_from_data (pixels,
365 src_pixbuf->colorspace,
366 src_pixbuf->has_alpha,
367 src_pixbuf->bits_per_sample,
369 src_pixbuf->rowstride,
372 /* Keep a reference to src_pixbuf */
373 g_object_ref (src_pixbuf);
375 g_object_set_qdata_full (G_OBJECT (sub),
376 g_quark_from_static_string ("gdk-pixbuf-subpixbuf-src"),
378 (GDestroyNotify) g_object_unref);
388 * gdk_pixbuf_get_colorspace:
391 * Queries the color space of a pixbuf.
393 * Return value: Color space.
396 gdk_pixbuf_get_colorspace (const GdkPixbuf *pixbuf)
398 g_return_val_if_fail (pixbuf != NULL, GDK_COLORSPACE_RGB);
400 return pixbuf->colorspace;
404 * gdk_pixbuf_get_n_channels:
407 * Queries the number of channels of a pixbuf.
409 * Return value: Number of channels.
412 gdk_pixbuf_get_n_channels (const GdkPixbuf *pixbuf)
414 g_return_val_if_fail (pixbuf != NULL, -1);
416 return pixbuf->n_channels;
420 * gdk_pixbuf_get_has_alpha:
423 * Queries whether a pixbuf has an alpha channel (opacity information).
425 * Return value: %TRUE if it has an alpha channel, %FALSE otherwise.
428 gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf)
430 g_return_val_if_fail (pixbuf != NULL, FALSE);
432 return pixbuf->has_alpha ? TRUE : FALSE;
436 * gdk_pixbuf_get_bits_per_sample:
439 * Queries the number of bits per color sample in a pixbuf.
441 * Return value: Number of bits per color sample.
444 gdk_pixbuf_get_bits_per_sample (const GdkPixbuf *pixbuf)
446 g_return_val_if_fail (pixbuf != NULL, -1);
448 return pixbuf->bits_per_sample;
452 * gdk_pixbuf_get_pixels:
455 * Queries a pointer to the pixel data of a pixbuf.
457 * Return value: A pointer to the pixbuf's pixel data. Please see <xref linkend="image-data"/>
458 * for information about how the pixel data is stored in
462 gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)
464 g_return_val_if_fail (pixbuf != NULL, NULL);
466 return pixbuf->pixels;
470 * gdk_pixbuf_get_width:
473 * Queries the width of a pixbuf.
475 * Return value: Width in pixels.
478 gdk_pixbuf_get_width (const GdkPixbuf *pixbuf)
480 g_return_val_if_fail (pixbuf != NULL, -1);
482 return pixbuf->width;
486 * gdk_pixbuf_get_height:
489 * Queries the height of a pixbuf.
491 * Return value: Height in pixels.
494 gdk_pixbuf_get_height (const GdkPixbuf *pixbuf)
496 g_return_val_if_fail (pixbuf != NULL, -1);
498 return pixbuf->height;
502 * gdk_pixbuf_get_rowstride:
505 * Queries the rowstride of a pixbuf, which is the number of bytes between the start of a row
506 * and the start of the next row.
508 * Return value: Distance between row starts.
511 gdk_pixbuf_get_rowstride (const GdkPixbuf *pixbuf)
513 g_return_val_if_fail (pixbuf != NULL, -1);
515 return pixbuf->rowstride;
520 /* General initialization hooks */
521 const guint gdk_pixbuf_major_version = GDK_PIXBUF_MAJOR;
522 const guint gdk_pixbuf_minor_version = GDK_PIXBUF_MINOR;
523 const guint gdk_pixbuf_micro_version = GDK_PIXBUF_MICRO;
525 const char *gdk_pixbuf_version = GDK_PIXBUF_VERSION;
529 gdk_pixbuf_error_quark (void)
533 q = g_quark_from_static_string ("gdk-pixbuf-error-quark");
540 * @pixbuf: a #GdkPixbuf
541 * @pixel: RGBA pixel to clear to
542 * (0xffffffff is opaque white, 0x00000000 transparent black)
544 * Clears a pixbuf to the given RGBA value, converting the RGBA value into
545 * the pixbuf's pixel format. The alpha will be ignored if the pixbuf
546 * doesn't have an alpha channel.
550 gdk_pixbuf_fill (GdkPixbuf *pixbuf,
558 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
560 if (pixbuf->width == 0 || pixbuf->height == 0)
563 pixels = pixbuf->pixels;
565 r = (pixel & 0xff000000) >> 24;
566 g = (pixel & 0x00ff0000) >> 16;
567 b = (pixel & 0x0000ff00) >> 8;
568 a = (pixel & 0x000000ff);
576 switch (pixbuf->n_channels) {
598 pixels += pixbuf->rowstride;
605 * gdk_pixbuf_get_option:
606 * @pixbuf: a #GdkPixbuf
607 * @key: a nul-terminated string.
609 * Looks up @key in the list of options that may have been attached to the
610 * @pixbuf when it was loaded.
612 * Return value: the value associated with @key. This is a nul-terminated
613 * string that should not be freed or %NULL if @key was not found.
615 G_CONST_RETURN gchar *
616 gdk_pixbuf_get_option (GdkPixbuf *pixbuf,
622 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
623 g_return_val_if_fail (key != NULL, NULL);
625 options = g_object_get_qdata (G_OBJECT (pixbuf),
626 g_quark_from_static_string ("gdk_pixbuf_options"));
628 for (i = 0; options[2*i]; i++) {
629 if (strcmp (options[2*i], key) == 0)
630 return options[2*i+1];
638 * gdk_pixbuf_set_option:
639 * @pixbuf: a #GdkPixbuf
640 * @key: a nul-terminated string.
641 * @value: a nul-terminated string.
643 * Attaches a key/value pair as an option to a #GdkPixbuf. If %key already
644 * exists in the list of options attached to @pixbuf, the new value is
645 * ignored and %FALSE is returned.
647 * Return value: %TRUE on success.
652 gdk_pixbuf_set_option (GdkPixbuf *pixbuf,
660 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
661 g_return_val_if_fail (key != NULL, FALSE);
662 g_return_val_if_fail (value != NULL, FALSE);
664 quark = g_quark_from_static_string ("gdk_pixbuf_options");
666 options = g_object_get_qdata (G_OBJECT (pixbuf), quark);
669 for (n = 0; options[2*n]; n++) {
670 if (strcmp (options[2*n], key) == 0)
674 g_object_steal_qdata (G_OBJECT (pixbuf), quark);
675 options = g_renew (gchar *, options, 2*(n+1) + 1);
677 options = g_new (gchar *, 3);
680 options[2*n] = g_strdup (key);
681 options[2*n+1] = g_strdup (value);
682 options[2*n+2] = NULL;
684 g_object_set_qdata_full (G_OBJECT (pixbuf), quark,
685 options, (GDestroyNotify) g_strfreev);
691 gdk_pixbuf_set_property (GObject *object,
696 GdkPixbuf *pixbuf = GDK_PIXBUF (object);
700 case PROP_COLORSPACE:
701 pixbuf->colorspace = g_value_get_enum (value);
703 case PROP_N_CHANNELS:
704 pixbuf->n_channels = g_value_get_int (value);
707 pixbuf->has_alpha = g_value_get_boolean (value);
709 case PROP_BITS_PER_SAMPLE:
710 pixbuf->bits_per_sample = g_value_get_int (value);
713 pixbuf->width = g_value_get_int (value);
716 pixbuf->height = g_value_get_int (value);
719 pixbuf->rowstride = g_value_get_int (value);
722 pixbuf->pixels = (guchar *) g_value_get_pointer (value);
725 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
731 gdk_pixbuf_get_property (GObject *object,
736 GdkPixbuf *pixbuf = GDK_PIXBUF (object);
740 case PROP_COLORSPACE:
741 g_value_set_enum (value, gdk_pixbuf_get_colorspace (pixbuf));
743 case PROP_N_CHANNELS:
744 g_value_set_int (value, gdk_pixbuf_get_n_channels (pixbuf));
747 g_value_set_boolean (value, gdk_pixbuf_get_has_alpha (pixbuf));
749 case PROP_BITS_PER_SAMPLE:
750 g_value_set_int (value, gdk_pixbuf_get_bits_per_sample (pixbuf));
753 g_value_set_int (value, gdk_pixbuf_get_width (pixbuf));
756 g_value_set_int (value, gdk_pixbuf_get_height (pixbuf));
759 g_value_set_int (value, gdk_pixbuf_get_rowstride (pixbuf));
762 g_value_set_pointer (value, gdk_pixbuf_get_pixels (pixbuf));
765 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
772 /* Include the marshallers */
773 #include <glib-object.h>
774 #include "gdk-pixbuf-marshal.c"