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 #define GDK_PIXBUF_C_COMPILATION
31 #include "gdk-pixbuf.h"
32 #include "gdk-pixbuf-private.h"
33 /* Include the marshallers */
34 #include <glib-object.h>
35 #include "gdk-pixbuf-marshal.c"
36 #include "gdk-pixbuf-alias.h"
38 static void gdk_pixbuf_finalize (GObject *object);
39 static void gdk_pixbuf_set_property (GObject *object,
43 static void gdk_pixbuf_get_property (GObject *object,
62 G_DEFINE_TYPE (GdkPixbuf, gdk_pixbuf, G_TYPE_OBJECT)
65 gdk_pixbuf_init (GdkPixbuf *pixbuf)
70 gdk_pixbuf_class_init (GdkPixbufClass *klass)
72 GObjectClass *object_class = G_OBJECT_CLASS (klass);
74 object_class->finalize = gdk_pixbuf_finalize;
75 object_class->set_property = gdk_pixbuf_set_property;
76 object_class->get_property = gdk_pixbuf_get_property;
78 #define PIXBUF_PARAM_FLAGS G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY|\
79 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
81 * GdkPixbuf:n-channels:
83 * The number of samples per pixel.
84 * Currently, only 3 or 4 samples per pixel are supported.
86 g_object_class_install_property (object_class,
88 g_param_spec_int ("n-channels",
89 P_("Number of Channels"),
90 P_("The number of samples per pixel"),
96 g_object_class_install_property (object_class,
98 g_param_spec_enum ("colorspace",
100 P_("The colorspace in which the samples are interpreted"),
103 PIXBUF_PARAM_FLAGS));
105 g_object_class_install_property (object_class,
107 g_param_spec_boolean ("has-alpha",
109 P_("Whether the pixbuf has an alpha channel"),
111 PIXBUF_PARAM_FLAGS));
114 * GdkPixbuf:bits-per-sample:
116 * The number of bits per sample.
117 * Currently only 8 bit per sample are supported.
119 g_object_class_install_property (object_class,
120 PROP_BITS_PER_SAMPLE,
121 g_param_spec_int ("bits-per-sample",
122 P_("Bits per Sample"),
123 P_("The number of bits per sample"),
127 PIXBUF_PARAM_FLAGS));
129 g_object_class_install_property (object_class,
131 g_param_spec_int ("width",
133 P_("The number of columns of the pixbuf"),
137 PIXBUF_PARAM_FLAGS));
139 g_object_class_install_property (object_class,
141 g_param_spec_int ("height",
143 P_("The number of rows of the pixbuf"),
147 PIXBUF_PARAM_FLAGS));
150 * GdkPixbuf:rowstride:
152 * The number of bytes between the start of a row and
153 * the start of the next row. This number must (obviously)
154 * be at least as large as the width of the pixbuf.
156 g_object_class_install_property (object_class,
158 g_param_spec_int ("rowstride",
160 P_("The number of bytes between the start of a row and the start of the next row"),
164 PIXBUF_PARAM_FLAGS));
166 g_object_class_install_property (object_class,
168 g_param_spec_pointer ("pixels",
170 P_("A pointer to the pixel data of the pixbuf"),
171 PIXBUF_PARAM_FLAGS));
175 gdk_pixbuf_finalize (GObject *object)
177 GdkPixbuf *pixbuf = GDK_PIXBUF (object);
179 if (pixbuf->destroy_fn)
180 (* pixbuf->destroy_fn) (pixbuf->pixels, pixbuf->destroy_fn_data);
182 G_OBJECT_CLASS (gdk_pixbuf_parent_class)->finalize (object);
190 * Adds a reference to a pixbuf.
192 * Return value: The same as the @pixbuf argument.
194 * Deprecated: Use g_object_ref().
197 gdk_pixbuf_ref (GdkPixbuf *pixbuf)
199 return (GdkPixbuf *) g_object_ref (pixbuf);
206 * Removes a reference from a pixbuf.
208 * Deprecated: Use g_object_unref().
211 gdk_pixbuf_unref (GdkPixbuf *pixbuf)
213 g_object_unref (pixbuf);
218 /* Used as the destroy notification function for gdk_pixbuf_new() */
220 free_buffer (guchar *pixels, gpointer data)
227 * @colorspace: Color space for image
228 * @has_alpha: Whether the image should have transparency information
229 * @bits_per_sample: Number of bits per color sample
230 * @width: Width of image in pixels, must be > 0
231 * @height: Height of image in pixels, must be > 0
233 * Creates a new #GdkPixbuf structure and allocates a buffer for it. The
234 * buffer has an optimal rowstride. Note that the buffer is not cleared;
235 * you will have to fill it completely yourself.
237 * Return value: A newly-created #GdkPixbuf with a reference count of 1, or
238 * %NULL if not enough memory could be allocated for the image buffer.
241 gdk_pixbuf_new (GdkColorspace colorspace,
252 g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, NULL);
253 g_return_val_if_fail (bits_per_sample == 8, NULL);
254 g_return_val_if_fail (width > 0, NULL);
255 g_return_val_if_fail (height > 0, NULL);
257 channels = has_alpha ? 4 : 3;
258 rowstride = width * channels;
259 if (rowstride / channels != width || rowstride + 3 < 0) /* overflow */
262 /* Always align rows to 32-bit boundaries */
263 rowstride = (rowstride + 3) & ~3;
265 bytes = height * rowstride;
266 if (bytes / rowstride != height) /* overflow */
269 buf = g_try_malloc (bytes);
273 return gdk_pixbuf_new_from_data (buf, colorspace, has_alpha, bits_per_sample,
274 width, height, rowstride,
282 * Creates a new #GdkPixbuf with a copy of the information in the specified
285 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
286 * not enough memory could be allocated.
289 gdk_pixbuf_copy (const GdkPixbuf *pixbuf)
294 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
296 /* Calculate a semi-exact size. Here we copy with full rowstrides;
297 * maybe we should copy each row individually with the minimum
301 size = ((pixbuf->height - 1) * pixbuf->rowstride +
302 pixbuf->width * ((pixbuf->n_channels * pixbuf->bits_per_sample + 7) / 8));
304 buf = g_try_malloc (size * sizeof (guchar));
308 memcpy (buf, pixbuf->pixels, size);
310 return gdk_pixbuf_new_from_data (buf,
311 pixbuf->colorspace, pixbuf->has_alpha,
312 pixbuf->bits_per_sample,
313 pixbuf->width, pixbuf->height,
320 * gdk_pixbuf_new_subpixbuf:
321 * @src_pixbuf: a #GdkPixbuf
322 * @src_x: X coord in @src_pixbuf
323 * @src_y: Y coord in @src_pixbuf
324 * @width: width of region in @src_pixbuf
325 * @height: height of region in @src_pixbuf
327 * Creates a new pixbuf which represents a sub-region of
328 * @src_pixbuf. The new pixbuf shares its pixels with the
329 * original pixbuf, so writing to one affects both.
330 * The new pixbuf holds a reference to @src_pixbuf, so
331 * @src_pixbuf will not be finalized until the new pixbuf
334 * Return value: a new pixbuf
337 gdk_pixbuf_new_subpixbuf (GdkPixbuf *src_pixbuf,
346 g_return_val_if_fail (GDK_IS_PIXBUF (src_pixbuf), NULL);
347 g_return_val_if_fail (src_x >= 0 && src_x + width <= src_pixbuf->width, NULL);
348 g_return_val_if_fail (src_y >= 0 && src_y + height <= src_pixbuf->height, NULL);
350 pixels = (gdk_pixbuf_get_pixels (src_pixbuf)
351 + src_y * src_pixbuf->rowstride
352 + src_x * src_pixbuf->n_channels);
354 sub = gdk_pixbuf_new_from_data (pixels,
355 src_pixbuf->colorspace,
356 src_pixbuf->has_alpha,
357 src_pixbuf->bits_per_sample,
359 src_pixbuf->rowstride,
362 /* Keep a reference to src_pixbuf */
363 g_object_ref (src_pixbuf);
365 g_object_set_qdata_full (G_OBJECT (sub),
366 g_quark_from_static_string ("gdk-pixbuf-subpixbuf-src"),
368 (GDestroyNotify) g_object_unref);
378 * gdk_pixbuf_get_colorspace:
381 * Queries the color space of a pixbuf.
383 * Return value: Color space.
386 gdk_pixbuf_get_colorspace (const GdkPixbuf *pixbuf)
388 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), GDK_COLORSPACE_RGB);
390 return pixbuf->colorspace;
394 * gdk_pixbuf_get_n_channels:
397 * Queries the number of channels of a pixbuf.
399 * Return value: Number of channels.
402 gdk_pixbuf_get_n_channels (const GdkPixbuf *pixbuf)
404 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
406 return pixbuf->n_channels;
410 * gdk_pixbuf_get_has_alpha:
413 * Queries whether a pixbuf has an alpha channel (opacity information).
415 * Return value: %TRUE if it has an alpha channel, %FALSE otherwise.
418 gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf)
420 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
422 return pixbuf->has_alpha ? TRUE : FALSE;
426 * gdk_pixbuf_get_bits_per_sample:
429 * Queries the number of bits per color sample in a pixbuf.
431 * Return value: Number of bits per color sample.
434 gdk_pixbuf_get_bits_per_sample (const GdkPixbuf *pixbuf)
436 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
438 return pixbuf->bits_per_sample;
442 * gdk_pixbuf_get_pixels:
445 * Queries a pointer to the pixel data of a pixbuf.
447 * Return value: A pointer to the pixbuf's pixel data. Please see <xref linkend="image-data"/>
448 * for information about how the pixel data is stored in
452 gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)
454 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
456 return pixbuf->pixels;
460 * gdk_pixbuf_get_width:
463 * Queries the width of a pixbuf.
465 * Return value: Width in pixels.
468 gdk_pixbuf_get_width (const GdkPixbuf *pixbuf)
470 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
472 return pixbuf->width;
476 * gdk_pixbuf_get_height:
479 * Queries the height of a pixbuf.
481 * Return value: Height in pixels.
484 gdk_pixbuf_get_height (const GdkPixbuf *pixbuf)
486 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
488 return pixbuf->height;
492 * gdk_pixbuf_get_rowstride:
495 * Queries the rowstride of a pixbuf, which is the number of bytes between the start of a row
496 * and the start of the next row.
498 * Return value: Distance between row starts.
501 gdk_pixbuf_get_rowstride (const GdkPixbuf *pixbuf)
503 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
505 return pixbuf->rowstride;
510 /* General initialization hooks */
511 const guint gdk_pixbuf_major_version = GDK_PIXBUF_MAJOR;
512 const guint gdk_pixbuf_minor_version = GDK_PIXBUF_MINOR;
513 const guint gdk_pixbuf_micro_version = GDK_PIXBUF_MICRO;
515 const char *gdk_pixbuf_version = GDK_PIXBUF_VERSION;
519 gdk_pixbuf_error_quark (void)
521 return g_quark_from_static_string ("gdk-pixbuf-error-quark");
526 * @pixbuf: a #GdkPixbuf
527 * @pixel: RGBA pixel to clear to
528 * (0xffffffff is opaque white, 0x00000000 transparent black)
530 * Clears a pixbuf to the given RGBA value, converting the RGBA value into
531 * the pixbuf's pixel format. The alpha will be ignored if the pixbuf
532 * doesn't have an alpha channel.
536 gdk_pixbuf_fill (GdkPixbuf *pixbuf,
544 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
546 if (pixbuf->width == 0 || pixbuf->height == 0)
549 pixels = pixbuf->pixels;
551 r = (pixel & 0xff000000) >> 24;
552 g = (pixel & 0x00ff0000) >> 16;
553 b = (pixel & 0x0000ff00) >> 8;
554 a = (pixel & 0x000000ff);
562 switch (pixbuf->n_channels) {
584 pixels += pixbuf->rowstride;
591 * gdk_pixbuf_get_option:
592 * @pixbuf: a #GdkPixbuf
593 * @key: a nul-terminated string.
595 * Looks up @key in the list of options that may have been attached to the
596 * @pixbuf when it was loaded, or that may have been attached by another
597 * function using gdk_pixbuf_set_option().
599 * For instance, the ANI loader provides "Title" and "Artist" options.
600 * The ICO, XBM, and XPM loaders provide "x_hot" and "y_hot" hot-spot
601 * options for cursor definitions. The PNG loader provides the tEXt ancillary
602 * chunk key/value pairs as options. Since 2.12, the TIFF and JPEG loaders
603 * return an "orientation" option string that corresponds to the embedded
604 * TIFF/Exif orientation tag (if present).
606 * Return value: the value associated with @key. This is a nul-terminated
607 * string that should not be freed or %NULL if @key was not found.
609 G_CONST_RETURN gchar *
610 gdk_pixbuf_get_option (GdkPixbuf *pixbuf,
616 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
617 g_return_val_if_fail (key != NULL, NULL);
619 options = g_object_get_qdata (G_OBJECT (pixbuf),
620 g_quark_from_static_string ("gdk_pixbuf_options"));
622 for (i = 0; options[2*i]; i++) {
623 if (strcmp (options[2*i], key) == 0)
624 return options[2*i+1];
632 * gdk_pixbuf_set_option:
633 * @pixbuf: a #GdkPixbuf
634 * @key: a nul-terminated string.
635 * @value: a nul-terminated string.
637 * Attaches a key/value pair as an option to a #GdkPixbuf. If %key already
638 * exists in the list of options attached to @pixbuf, the new value is
639 * ignored and %FALSE is returned.
641 * Return value: %TRUE on success.
646 gdk_pixbuf_set_option (GdkPixbuf *pixbuf,
654 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
655 g_return_val_if_fail (key != NULL, FALSE);
656 g_return_val_if_fail (value != NULL, FALSE);
658 quark = g_quark_from_static_string ("gdk_pixbuf_options");
660 options = g_object_get_qdata (G_OBJECT (pixbuf), quark);
663 for (n = 0; options[2*n]; n++) {
664 if (strcmp (options[2*n], key) == 0)
668 g_object_steal_qdata (G_OBJECT (pixbuf), quark);
669 options = g_renew (gchar *, options, 2*(n+1) + 1);
671 options = g_new (gchar *, 3);
674 options[2*n] = g_strdup (key);
675 options[2*n+1] = g_strdup (value);
676 options[2*n+2] = NULL;
678 g_object_set_qdata_full (G_OBJECT (pixbuf), quark,
679 options, (GDestroyNotify) g_strfreev);
685 gdk_pixbuf_set_property (GObject *object,
690 GdkPixbuf *pixbuf = GDK_PIXBUF (object);
694 case PROP_COLORSPACE:
695 pixbuf->colorspace = g_value_get_enum (value);
697 case PROP_N_CHANNELS:
698 pixbuf->n_channels = g_value_get_int (value);
701 pixbuf->has_alpha = g_value_get_boolean (value);
703 case PROP_BITS_PER_SAMPLE:
704 pixbuf->bits_per_sample = g_value_get_int (value);
707 pixbuf->width = g_value_get_int (value);
710 pixbuf->height = g_value_get_int (value);
713 pixbuf->rowstride = g_value_get_int (value);
716 pixbuf->pixels = (guchar *) g_value_get_pointer (value);
719 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
725 gdk_pixbuf_get_property (GObject *object,
730 GdkPixbuf *pixbuf = GDK_PIXBUF (object);
734 case PROP_COLORSPACE:
735 g_value_set_enum (value, gdk_pixbuf_get_colorspace (pixbuf));
737 case PROP_N_CHANNELS:
738 g_value_set_int (value, gdk_pixbuf_get_n_channels (pixbuf));
741 g_value_set_boolean (value, gdk_pixbuf_get_has_alpha (pixbuf));
743 case PROP_BITS_PER_SAMPLE:
744 g_value_set_int (value, gdk_pixbuf_get_bits_per_sample (pixbuf));
747 g_value_set_int (value, gdk_pixbuf_get_width (pixbuf));
750 g_value_set_int (value, gdk_pixbuf_get_height (pixbuf));
753 g_value_set_int (value, gdk_pixbuf_get_rowstride (pixbuf));
756 g_value_set_pointer (value, gdk_pixbuf_get_pixels (pixbuf));
759 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
764 #define __GDK_PIXBUF_C__
765 #include "gdk-pixbuf-aliasdef.c"