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);
185 /* Used as the destroy notification function for gdk_pixbuf_new() */
187 free_buffer (guchar *pixels, gpointer data)
194 * @colorspace: Color space for image
195 * @has_alpha: Whether the image should have transparency information
196 * @bits_per_sample: Number of bits per color sample
197 * @width: Width of image in pixels, must be > 0
198 * @height: Height of image in pixels, must be > 0
200 * Creates a new #GdkPixbuf structure and allocates a buffer for it. The
201 * buffer has an optimal rowstride. Note that the buffer is not cleared;
202 * you will have to fill it completely yourself.
204 * Return value: A newly-created #GdkPixbuf with a reference count of 1, or
205 * %NULL if not enough memory could be allocated for the image buffer.
208 gdk_pixbuf_new (GdkColorspace colorspace,
219 g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, NULL);
220 g_return_val_if_fail (bits_per_sample == 8, NULL);
221 g_return_val_if_fail (width > 0, NULL);
222 g_return_val_if_fail (height > 0, NULL);
224 channels = has_alpha ? 4 : 3;
225 rowstride = width * channels;
226 if (rowstride / channels != width || rowstride + 3 < 0) /* overflow */
229 /* Always align rows to 32-bit boundaries */
230 rowstride = (rowstride + 3) & ~3;
232 bytes = height * rowstride;
233 if (bytes / rowstride != height) /* overflow */
236 buf = g_try_malloc (bytes);
240 return gdk_pixbuf_new_from_data (buf, colorspace, has_alpha, bits_per_sample,
241 width, height, rowstride,
249 * Creates a new #GdkPixbuf with a copy of the information in the specified
252 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
253 * not enough memory could be allocated.
256 gdk_pixbuf_copy (const GdkPixbuf *pixbuf)
261 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
263 /* Calculate a semi-exact size. Here we copy with full rowstrides;
264 * maybe we should copy each row individually with the minimum
268 size = ((pixbuf->height - 1) * pixbuf->rowstride +
269 pixbuf->width * ((pixbuf->n_channels * pixbuf->bits_per_sample + 7) / 8));
271 buf = g_try_malloc (size * sizeof (guchar));
275 memcpy (buf, pixbuf->pixels, size);
277 return gdk_pixbuf_new_from_data (buf,
278 pixbuf->colorspace, pixbuf->has_alpha,
279 pixbuf->bits_per_sample,
280 pixbuf->width, pixbuf->height,
287 * gdk_pixbuf_new_subpixbuf:
288 * @src_pixbuf: a #GdkPixbuf
289 * @src_x: X coord in @src_pixbuf
290 * @src_y: Y coord in @src_pixbuf
291 * @width: width of region in @src_pixbuf
292 * @height: height of region in @src_pixbuf
294 * Creates a new pixbuf which represents a sub-region of
295 * @src_pixbuf. The new pixbuf shares its pixels with the
296 * original pixbuf, so writing to one affects both.
297 * The new pixbuf holds a reference to @src_pixbuf, so
298 * @src_pixbuf will not be finalized until the new pixbuf
301 * Return value: a new pixbuf
304 gdk_pixbuf_new_subpixbuf (GdkPixbuf *src_pixbuf,
313 g_return_val_if_fail (GDK_IS_PIXBUF (src_pixbuf), NULL);
314 g_return_val_if_fail (src_x >= 0 && src_x + width <= src_pixbuf->width, NULL);
315 g_return_val_if_fail (src_y >= 0 && src_y + height <= src_pixbuf->height, NULL);
317 pixels = (gdk_pixbuf_get_pixels (src_pixbuf)
318 + src_y * src_pixbuf->rowstride
319 + src_x * src_pixbuf->n_channels);
321 sub = gdk_pixbuf_new_from_data (pixels,
322 src_pixbuf->colorspace,
323 src_pixbuf->has_alpha,
324 src_pixbuf->bits_per_sample,
326 src_pixbuf->rowstride,
329 /* Keep a reference to src_pixbuf */
330 g_object_ref (src_pixbuf);
332 g_object_set_qdata_full (G_OBJECT (sub),
333 g_quark_from_static_string ("gdk-pixbuf-subpixbuf-src"),
335 (GDestroyNotify) g_object_unref);
345 * gdk_pixbuf_get_colorspace:
348 * Queries the color space of a pixbuf.
350 * Return value: Color space.
353 gdk_pixbuf_get_colorspace (const GdkPixbuf *pixbuf)
355 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), GDK_COLORSPACE_RGB);
357 return pixbuf->colorspace;
361 * gdk_pixbuf_get_n_channels:
364 * Queries the number of channels of a pixbuf.
366 * Return value: Number of channels.
369 gdk_pixbuf_get_n_channels (const GdkPixbuf *pixbuf)
371 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
373 return pixbuf->n_channels;
377 * gdk_pixbuf_get_has_alpha:
380 * Queries whether a pixbuf has an alpha channel (opacity information).
382 * Return value: %TRUE if it has an alpha channel, %FALSE otherwise.
385 gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf)
387 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
389 return pixbuf->has_alpha ? TRUE : FALSE;
393 * gdk_pixbuf_get_bits_per_sample:
396 * Queries the number of bits per color sample in a pixbuf.
398 * Return value: Number of bits per color sample.
401 gdk_pixbuf_get_bits_per_sample (const GdkPixbuf *pixbuf)
403 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
405 return pixbuf->bits_per_sample;
409 * gdk_pixbuf_get_pixels:
412 * Queries a pointer to the pixel data of a pixbuf.
414 * Return value: A pointer to the pixbuf's pixel data. Please see <xref linkend="image-data"/>
415 * for information about how the pixel data is stored in
419 gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)
421 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
423 return pixbuf->pixels;
427 * gdk_pixbuf_get_width:
430 * Queries the width of a pixbuf.
432 * Return value: Width in pixels.
435 gdk_pixbuf_get_width (const GdkPixbuf *pixbuf)
437 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
439 return pixbuf->width;
443 * gdk_pixbuf_get_height:
446 * Queries the height of a pixbuf.
448 * Return value: Height in pixels.
451 gdk_pixbuf_get_height (const GdkPixbuf *pixbuf)
453 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
455 return pixbuf->height;
459 * gdk_pixbuf_get_rowstride:
462 * Queries the rowstride of a pixbuf, which is the number of bytes between the start of a row
463 * and the start of the next row.
465 * Return value: Distance between row starts.
468 gdk_pixbuf_get_rowstride (const GdkPixbuf *pixbuf)
470 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);
472 return pixbuf->rowstride;
477 /* General initialization hooks */
478 const guint gdk_pixbuf_major_version = GDK_PIXBUF_MAJOR;
479 const guint gdk_pixbuf_minor_version = GDK_PIXBUF_MINOR;
480 const guint gdk_pixbuf_micro_version = GDK_PIXBUF_MICRO;
482 const char *gdk_pixbuf_version = GDK_PIXBUF_VERSION;
486 gdk_pixbuf_error_quark (void)
488 return g_quark_from_static_string ("gdk-pixbuf-error-quark");
493 * @pixbuf: a #GdkPixbuf
494 * @pixel: RGBA pixel to clear to
495 * (0xffffffff is opaque white, 0x00000000 transparent black)
497 * Clears a pixbuf to the given RGBA value, converting the RGBA value into
498 * the pixbuf's pixel format. The alpha will be ignored if the pixbuf
499 * doesn't have an alpha channel.
503 gdk_pixbuf_fill (GdkPixbuf *pixbuf,
511 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
513 if (pixbuf->width == 0 || pixbuf->height == 0)
516 pixels = pixbuf->pixels;
518 r = (pixel & 0xff000000) >> 24;
519 g = (pixel & 0x00ff0000) >> 16;
520 b = (pixel & 0x0000ff00) >> 8;
521 a = (pixel & 0x000000ff);
529 switch (pixbuf->n_channels) {
551 pixels += pixbuf->rowstride;
558 * gdk_pixbuf_get_option:
559 * @pixbuf: a #GdkPixbuf
560 * @key: a nul-terminated string.
562 * Looks up @key in the list of options that may have been attached to the
563 * @pixbuf when it was loaded, or that may have been attached by another
564 * function using gdk_pixbuf_set_option().
566 * For instance, the ANI loader provides "Title" and "Artist" options.
567 * The ICO, XBM, and XPM loaders provide "x_hot" and "y_hot" hot-spot
568 * options for cursor definitions. The PNG loader provides the tEXt ancillary
569 * chunk key/value pairs as options. Since 2.12, the TIFF and JPEG loaders
570 * return an "orientation" option string that corresponds to the embedded
571 * TIFF/Exif orientation tag (if present).
573 * Return value: the value associated with @key. This is a nul-terminated
574 * string that should not be freed or %NULL if @key was not found.
576 G_CONST_RETURN gchar *
577 gdk_pixbuf_get_option (GdkPixbuf *pixbuf,
583 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
584 g_return_val_if_fail (key != NULL, NULL);
586 options = g_object_get_qdata (G_OBJECT (pixbuf),
587 g_quark_from_static_string ("gdk_pixbuf_options"));
589 for (i = 0; options[2*i]; i++) {
590 if (strcmp (options[2*i], key) == 0)
591 return options[2*i+1];
599 * gdk_pixbuf_set_option:
600 * @pixbuf: a #GdkPixbuf
601 * @key: a nul-terminated string.
602 * @value: a nul-terminated string.
604 * Attaches a key/value pair as an option to a #GdkPixbuf. If %key already
605 * exists in the list of options attached to @pixbuf, the new value is
606 * ignored and %FALSE is returned.
608 * Return value: %TRUE on success.
613 gdk_pixbuf_set_option (GdkPixbuf *pixbuf,
621 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
622 g_return_val_if_fail (key != NULL, FALSE);
623 g_return_val_if_fail (value != NULL, FALSE);
625 quark = g_quark_from_static_string ("gdk_pixbuf_options");
627 options = g_object_get_qdata (G_OBJECT (pixbuf), quark);
630 for (n = 0; options[2*n]; n++) {
631 if (strcmp (options[2*n], key) == 0)
635 g_object_steal_qdata (G_OBJECT (pixbuf), quark);
636 options = g_renew (gchar *, options, 2*(n+1) + 1);
638 options = g_new (gchar *, 3);
641 options[2*n] = g_strdup (key);
642 options[2*n+1] = g_strdup (value);
643 options[2*n+2] = NULL;
645 g_object_set_qdata_full (G_OBJECT (pixbuf), quark,
646 options, (GDestroyNotify) g_strfreev);
652 gdk_pixbuf_set_property (GObject *object,
657 GdkPixbuf *pixbuf = GDK_PIXBUF (object);
661 case PROP_COLORSPACE:
662 pixbuf->colorspace = g_value_get_enum (value);
664 case PROP_N_CHANNELS:
665 pixbuf->n_channels = g_value_get_int (value);
668 pixbuf->has_alpha = g_value_get_boolean (value);
670 case PROP_BITS_PER_SAMPLE:
671 pixbuf->bits_per_sample = g_value_get_int (value);
674 pixbuf->width = g_value_get_int (value);
677 pixbuf->height = g_value_get_int (value);
680 pixbuf->rowstride = g_value_get_int (value);
683 pixbuf->pixels = (guchar *) g_value_get_pointer (value);
686 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
692 gdk_pixbuf_get_property (GObject *object,
697 GdkPixbuf *pixbuf = GDK_PIXBUF (object);
701 case PROP_COLORSPACE:
702 g_value_set_enum (value, gdk_pixbuf_get_colorspace (pixbuf));
704 case PROP_N_CHANNELS:
705 g_value_set_int (value, gdk_pixbuf_get_n_channels (pixbuf));
708 g_value_set_boolean (value, gdk_pixbuf_get_has_alpha (pixbuf));
710 case PROP_BITS_PER_SAMPLE:
711 g_value_set_int (value, gdk_pixbuf_get_bits_per_sample (pixbuf));
714 g_value_set_int (value, gdk_pixbuf_get_width (pixbuf));
717 g_value_set_int (value, gdk_pixbuf_get_height (pixbuf));
720 g_value_set_int (value, gdk_pixbuf_get_rowstride (pixbuf));
723 g_value_set_pointer (value, gdk_pixbuf_get_pixels (pixbuf));
726 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
731 #define __GDK_PIXBUF_C__
732 #include "gdk-pixbuf-aliasdef.c"