]> Pileus Git - ~andy/gtk/blobdiff - gdk-pixbuf/gdk-pixbuf.c
ru.po: Updated Russian translation from Russian team <gnome-cyr@gnome.org>.
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf.c
index 60750f4a05a6f2a01fc64709f8b35bc7e1b8295f..9428fa650d7c1fa31e3f14714e07109f895d681d 100644 (file)
@@ -1,3 +1,4 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
 /* GdkPixbuf library - Basic memory management
  *
  * Copyright (C) 1999 The Free Software Foundation
 #include "gdk-pixbuf.h"
 #include "gdk-pixbuf-private.h"
 
-static void gdk_pixbuf_class_init  (GdkPixbufClass *klass);
-static void gdk_pixbuf_finalize    (GObject        *object);
+static void gdk_pixbuf_class_init   (GdkPixbufClass *klass);
+static void gdk_pixbuf_finalize     (GObject        *object);
+static void gdk_pixbuf_set_property (GObject        *object,
+                                    guint           prop_id,
+                                    const GValue   *value,
+                                    GParamSpec     *pspec);
+static void gdk_pixbuf_get_property (GObject        *object,
+                                    guint           prop_id,
+                                    GValue         *value,
+                                    GParamSpec     *pspec);
 
 \f
+enum 
+{
+  PROP_0,
+  PROP_COLORSPACE,
+  PROP_N_CHANNELS,
+  PROP_HAS_ALPHA,
+  PROP_BITS_PER_SAMPLE,
+  PROP_WIDTH,
+  PROP_HEIGHT,
+  PROP_ROWSTRIDE,
+  PROP_PIXELS
+};
 
 static gpointer parent_class;
 
@@ -70,6 +91,91 @@ gdk_pixbuf_class_init (GdkPixbufClass *klass)
         parent_class = g_type_class_peek_parent (klass);
         
         object_class->finalize = gdk_pixbuf_finalize;
+        object_class->set_property = gdk_pixbuf_set_property;
+        object_class->get_property = gdk_pixbuf_get_property;
+        
+        g_object_class_install_property (object_class,
+                                         PROP_N_CHANNELS,
+                                         g_param_spec_int ("n_channels",
+                                                           P_("Number of Channels"),
+                                                           P_("The number of samples per pixel"),
+                                                           0,
+                                                           G_MAXINT,
+                                                           3,
+                                                           G_PARAM_READWRITE |
+                                                           G_PARAM_CONSTRUCT_ONLY));
+
+        g_object_class_install_property (object_class,
+                                         PROP_COLORSPACE,
+                                         g_param_spec_enum ("colorspace",
+                                                            P_("Colorspace"),
+                                                            P_("The colorspace in which the samples are interpreted"),
+                                                            GDK_TYPE_COLORSPACE,
+                                                            GDK_COLORSPACE_RGB,
+                                                            
+                                                            G_PARAM_READWRITE |
+                                                            G_PARAM_CONSTRUCT_ONLY));
+
+        g_object_class_install_property (object_class,
+                                         PROP_HAS_ALPHA,
+                                         g_param_spec_boolean ("has_alpha",
+                                                               P_("Has Alpha"),
+                                                               P_("Whether the pixbuf has an alpha channel"),
+                                                               FALSE,
+                                                               G_PARAM_READWRITE |
+                                                               G_PARAM_CONSTRUCT_ONLY));
+
+        g_object_class_install_property (object_class,
+                                         PROP_BITS_PER_SAMPLE,
+                                         g_param_spec_int ("bits_per_sample",
+                                                           P_("Bits per Sample"),
+                                                           P_("The number of bits per sample"),
+                                                           1,
+                                                           16,
+                                                           8,
+                                                           G_PARAM_READWRITE |
+                                                           G_PARAM_CONSTRUCT_ONLY));
+
+        g_object_class_install_property (object_class,
+                                         PROP_WIDTH,
+                                         g_param_spec_int ("width",
+                                                           P_("Width"),
+                                                           P_("The number of columns of the pixbuf"),
+                                                           1,
+                                                           G_MAXINT,
+                                                           1,
+                                                           G_PARAM_READWRITE |
+                                                           G_PARAM_CONSTRUCT_ONLY));
+
+        g_object_class_install_property (object_class,
+                                         PROP_HEIGHT,
+                                         g_param_spec_int ("height",
+                                                           P_("Height"),
+                                                           P_("The number of rows of the pixbuf"),
+                                                           1,
+                                                           G_MAXINT,
+                                                           1,
+                                                           G_PARAM_READWRITE |
+                                                           G_PARAM_CONSTRUCT_ONLY));
+
+        g_object_class_install_property (object_class,
+                                         PROP_ROWSTRIDE,
+                                         g_param_spec_int ("rowstride",
+                                                           P_("Rowstride"),
+                                                           P_("The number of bytes between the start of a row and the start of the next row"),
+                                                           1,
+                                                           G_MAXINT,
+                                                           1,
+                                                           G_PARAM_READWRITE |
+                                                           G_PARAM_CONSTRUCT_ONLY));
+
+        g_object_class_install_property (object_class,
+                                         PROP_PIXELS,
+                                         g_param_spec_pointer ("pixels",
+                                                               P_("Pixels"),
+                                                               P_("A pointer to the pixel data of the pixbuf"),
+                                                               G_PARAM_READWRITE |
+                                                               G_PARAM_CONSTRUCT_ONLY));
 }
 
 static void
@@ -82,33 +188,36 @@ gdk_pixbuf_finalize (GObject *object)
         
         G_OBJECT_CLASS (parent_class)->finalize (object);
 }
+\f
 
 /**
  * gdk_pixbuf_ref:
  * @pixbuf: A pixbuf.
  *
- * Adds a reference to a pixbuf. Deprecated; use g_object_ref().
+ * Adds a reference to a pixbuf. 
  *
  * Return value: The same as the @pixbuf argument.
+ *
+ * Deprecated: Use g_object_ref().
  **/
 GdkPixbuf *
 gdk_pixbuf_ref (GdkPixbuf *pixbuf)
 {
-        return (GdkPixbuf *) g_object_ref (G_OBJECT(pixbuf));
+        return (GdkPixbuf *) g_object_ref (pixbuf);
 }
 
 /**
  * gdk_pixbuf_unref:
  * @pixbuf: A pixbuf.
  *
- * Removes a reference from a pixbuf. Deprecated; use
- * g_object_unref().
+ * Removes a reference from a pixbuf. 
  *
+ * Deprecated: Use g_object_unref().
  **/
 void
 gdk_pixbuf_unref (GdkPixbuf *pixbuf)
 {
-        g_object_unref (G_OBJECT (pixbuf));
+        g_object_unref (pixbuf);
 }
 
 \f
@@ -128,32 +237,46 @@ free_buffer (guchar *pixels, gpointer data)
  * @width: Width of image in pixels.
  * @height: Height of image in pixels.
  *
- * Creates a new #GdkPixbuf structure and allocates a buffer for it.  The buffer
- * has an optimal rowstride.  Note that the buffer is not cleared; you will have
- * to fill it completely yourself.
+ * Creates a new #GdkPixbuf structure and allocates a buffer for it.  The 
+ * buffer has an optimal rowstride.  Note that the buffer is not cleared;
+ * you will have to fill it completely yourself.
  *
- * Return value: A newly-created #GdkPixbuf with a reference count of 1, or NULL
- * if not enough memory could be allocated for the image buffer.
+ * Return value: A newly-created #GdkPixbuf with a reference count of 1, or 
+ * %NULL if not enough memory could be allocated for the image buffer.
  **/
 GdkPixbuf *
-gdk_pixbuf_new (GdkColorspace colorspace, gboolean has_alpha, int bits_per_sample,
-               int width, int height)
+gdk_pixbuf_new (GdkColorspace colorspace, 
+                gboolean      has_alpha,
+                int           bits_per_sample,
+                int           width,
+                int           height)
 {
        guchar *buf;
        int channels;
        int rowstride;
+        gsize bytes;
 
        g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, NULL);
        g_return_val_if_fail (bits_per_sample == 8, NULL);
        g_return_val_if_fail (width > 0, NULL);
        g_return_val_if_fail (height > 0, NULL);
 
-       /* Always align rows to 32-bit boundaries */
+        if (width <= 0 || height <= 0)
+                return NULL;
 
        channels = has_alpha ? 4 : 3;
-       rowstride = 4 * ((channels * width + 3) / 4);
+        rowstride = width * channels;
+        if (rowstride / channels != width || rowstride + 3 < 0) /* overflow */
+                return NULL;
+        
+       /* Always align rows to 32-bit boundaries */
+       rowstride = (rowstride + 3) & ~3;
 
-       buf = g_try_malloc (height * rowstride);
+        bytes = height * rowstride;
+        if (bytes / rowstride !=  height) /* overflow */
+                return NULL;
+            
+       buf = g_try_malloc (bytes);
        if (!buf)
                return NULL;
 
@@ -169,7 +292,7 @@ gdk_pixbuf_new (GdkColorspace colorspace, gboolean has_alpha, int bits_per_sampl
  * Creates a new #GdkPixbuf with a copy of the information in the specified
  * @pixbuf.
  * 
- * Return value: A newly-created pixbuf with a reference count of 1, or NULL if
+ * Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
  * not enough memory could be allocated.
  **/
 GdkPixbuf *
@@ -247,7 +370,7 @@ gdk_pixbuf_new_subpixbuf (GdkPixbuf *src_pixbuf,
                                         NULL, NULL);
 
         /* Keep a reference to src_pixbuf */
-        g_object_ref (G_OBJECT (src_pixbuf));
+        g_object_ref (src_pixbuf);
   
         g_object_set_qdata_full (G_OBJECT (sub),
                                  g_quark_from_static_string ("gdk-pixbuf-subpixbuf-src"),
@@ -299,12 +422,12 @@ gdk_pixbuf_get_n_channels (const GdkPixbuf *pixbuf)
  *
  * Queries whether a pixbuf has an alpha channel (opacity information).
  *
- * Return value: TRUE if it has an alpha channel, FALSE otherwise.
+ * Return value: %TRUE if it has an alpha channel, %FALSE otherwise.
  **/
 gboolean
 gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf)
 {
-       g_return_val_if_fail (pixbuf != NULL, -1);
+       g_return_val_if_fail (pixbuf != NULL, FALSE);
 
        return pixbuf->has_alpha ? TRUE : FALSE;
 }
@@ -331,7 +454,9 @@ gdk_pixbuf_get_bits_per_sample (const GdkPixbuf *pixbuf)
  *
  * Queries a pointer to the pixel data of a pixbuf.
  *
- * Return value: A pointer to the pixbuf's pixel data.
+ * Return value: A pointer to the pixbuf's pixel data.  Please see <xref linkend="image-data"/>
+ * for information about how the pixel data is stored in
+ * memory.
  **/
 guchar *
 gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)
@@ -377,9 +502,10 @@ gdk_pixbuf_get_height (const GdkPixbuf *pixbuf)
  * gdk_pixbuf_get_rowstride:
  * @pixbuf: A pixbuf.
  *
- * Queries the rowstride of a pixbuf, which is the number of bytes between rows.
+ * Queries the rowstride of a pixbuf, which is the number of bytes between the start of a row
+ * and the start of the next row.
  *
- * Return value: Number of bytes between rows.
+ * Return value: Distance between row starts.
  **/
 int
 gdk_pixbuf_get_rowstride (const GdkPixbuf *pixbuf)
@@ -412,7 +538,8 @@ gdk_pixbuf_error_quark (void)
 /**
  * gdk_pixbuf_fill:
  * @pixbuf: a #GdkPixbuf
- * @pixel: RGBA pixel to clear to (0xffffffff is opaque white, 0x00000000 transparent black)
+ * @pixel: RGBA pixel to clear to
+ *         (0xffffffff is opaque white, 0x00000000 transparent black)
  *
  * Clears a pixbuf to the given RGBA value, converting the RGBA value into
  * the pixbuf's pixel format. The alpha will be ignored if the pixbuf
@@ -424,13 +551,14 @@ gdk_pixbuf_fill (GdkPixbuf *pixbuf,
                  guint32    pixel)
 {
         guchar *pixels;
-        gboolean all_the_same = FALSE;
         guint r, g, b, a;
-        
+        guchar *p;
+        guint w, h;
+
         g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
 
         if (pixbuf->width == 0 || pixbuf->height == 0)
-          return;
+                return;
 
         pixels = pixbuf->pixels;
 
@@ -439,46 +567,207 @@ gdk_pixbuf_fill (GdkPixbuf *pixbuf,
         b = (pixel & 0x0000ff00) >> 8;
         a = (pixel & 0x000000ff);
 
-        if (r == g && g == b) {
-                if (!pixbuf->has_alpha)
-                        all_the_same = TRUE;
-                else
-                        all_the_same = (r == a);
-        }
+        h = pixbuf->height;
         
-        if (all_the_same) {
-                memset (pixels, r,
-                        pixbuf->rowstride * pixbuf->height);
-        } else {
-                guchar *p;
-                guchar  c[4];
-                gint    n;
-
-                c[0] = r; c[1] = g; c[2] = b; c[3] = a;
-                
+        while (h--) {
+                w = pixbuf->width;
                 p = pixels;
-                n = pixbuf->width;
-                if (pixbuf->has_alpha) {
-                        do {
-                                memcpy (p, c, 4);
-                                p += 4;
-                        } while (--n);
-                } else {
-                        do {
-                                memcpy (p, c, 3);
+
+                switch (pixbuf->n_channels) {
+                case 3:
+                        while (w--) {
+                                p[0] = r;
+                                p[1] = g;
+                                p[2] = b;
                                 p += 3;
-                        } while (--n);
+                        }
+                        break;
+                case 4:
+                        while (w--) {
+                                p[0] = r;
+                                p[1] = g;
+                                p[2] = b;
+                                p[3] = a;
+                                p += 4;
+                        }
+                        break;
+                default:
+                        break;
                 }
+                
+                pixels += pixbuf->rowstride;
+        }
+}
 
-                p = pixels;
-                n = pixbuf->height - 1;
-                while (n--) {
-                        p += pixbuf->rowstride;
-                        memcpy (p, pixels, pixbuf->width * pixbuf->n_channels);
+\f
+
+/**
+ * gdk_pixbuf_get_option:
+ * @pixbuf: a #GdkPixbuf
+ * @key: a nul-terminated string.
+ * 
+ * Looks up @key in the list of options that may have been attached to the
+ * @pixbuf when it was loaded. 
+ * 
+ * Return value: the value associated with @key. This is a nul-terminated 
+ * string that should not be freed or %NULL if @key was not found.
+ **/
+G_CONST_RETURN gchar *
+gdk_pixbuf_get_option (GdkPixbuf   *pixbuf,
+                       const gchar *key)
+{
+        gchar **options;
+        gint i;
+
+        g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
+        g_return_val_if_fail (key != NULL, NULL);
+  
+        options = g_object_get_qdata (G_OBJECT (pixbuf), 
+                                      g_quark_from_static_string ("gdk_pixbuf_options"));
+        if (options) {
+                for (i = 0; options[2*i]; i++) {
+                        if (strcmp (options[2*i], key) == 0)
+                                return options[2*i+1];
                 }
         }
+        
+        return NULL;
 }
 
+/**
+ * gdk_pixbuf_set_option:
+ * @pixbuf: a #GdkPixbuf
+ * @key: a nul-terminated string.
+ * @value: a nul-terminated string.
+ * 
+ * Attaches a key/value pair as an option to a #GdkPixbuf. If %key already
+ * exists in the list of options attached to @pixbuf, the new value is 
+ * ignored and %FALSE is returned.
+ *
+ * Return value: %TRUE on success.
+ *
+ * Since: 2.2
+ **/
+gboolean
+gdk_pixbuf_set_option (GdkPixbuf   *pixbuf,
+                       const gchar *key,
+                       const gchar *value)
+{
+        GQuark  quark;
+        gchar **options;
+        gint n = 0;
+        g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
+        g_return_val_if_fail (key != NULL, FALSE);
+        g_return_val_if_fail (value != NULL, FALSE);
+
+        quark = g_quark_from_static_string ("gdk_pixbuf_options");
+
+        options = g_object_get_qdata (G_OBJECT (pixbuf), quark);
+
+        if (options) {
+                for (n = 0; options[2*n]; n++) {
+                        if (strcmp (options[2*n], key) == 0)
+                                return FALSE;
+                }
+
+                g_object_steal_qdata (G_OBJECT (pixbuf), quark);
+                options = g_renew (gchar *, options, 2*(n+1) + 1);
+        } else {
+                options = g_new (gchar *, 3);
+        }
+        
+        options[2*n]   = g_strdup (key);
+        options[2*n+1] = g_strdup (value);
+        options[2*n+2] = NULL;
+
+        g_object_set_qdata_full (G_OBJECT (pixbuf), quark,
+                                 options, (GDestroyNotify) g_strfreev);
+        
+        return TRUE;
+}
+
+static void
+gdk_pixbuf_set_property (GObject         *object,
+                        guint            prop_id,
+                        const GValue    *value,
+                        GParamSpec      *pspec)
+{
+  GdkPixbuf *pixbuf = GDK_PIXBUF (object);
+
+  switch (prop_id)
+          {
+          case PROP_COLORSPACE:
+                  pixbuf->colorspace = g_value_get_enum (value);
+                  break;
+          case PROP_N_CHANNELS:
+                  pixbuf->n_channels = g_value_get_int (value);
+                  break;
+          case PROP_HAS_ALPHA:
+                  pixbuf->has_alpha = g_value_get_boolean (value);
+                  break;
+          case PROP_BITS_PER_SAMPLE:
+                  pixbuf->bits_per_sample = g_value_get_int (value);
+                  break;
+          case PROP_WIDTH:
+                  pixbuf->width = g_value_get_int (value);
+                  break;
+          case PROP_HEIGHT:
+                  pixbuf->height = g_value_get_int (value);
+                  break;
+          case PROP_ROWSTRIDE:
+                  pixbuf->rowstride = g_value_get_int (value);
+                  break;
+          case PROP_PIXELS:
+                  pixbuf->pixels = (guchar *) g_value_get_pointer (value);
+                  break;
+          default:
+                  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                  break;
+          }
+}
+
+static void
+gdk_pixbuf_get_property (GObject         *object,
+                        guint            prop_id,
+                        GValue          *value,
+                        GParamSpec      *pspec)
+{
+  GdkPixbuf *pixbuf = GDK_PIXBUF (object);
+  
+  switch (prop_id)
+          {
+          case PROP_COLORSPACE:
+                  g_value_set_enum (value, gdk_pixbuf_get_colorspace (pixbuf));
+                  break;
+          case PROP_N_CHANNELS:
+                  g_value_set_int (value, gdk_pixbuf_get_n_channels (pixbuf));
+                  break;
+          case PROP_HAS_ALPHA:
+                  g_value_set_boolean (value, gdk_pixbuf_get_has_alpha (pixbuf));
+                  break;
+          case PROP_BITS_PER_SAMPLE:
+                  g_value_set_int (value, gdk_pixbuf_get_bits_per_sample (pixbuf));
+                  break;
+          case PROP_WIDTH:
+                  g_value_set_int (value, gdk_pixbuf_get_width (pixbuf));
+                  break;
+          case PROP_HEIGHT:
+                  g_value_set_int (value, gdk_pixbuf_get_height (pixbuf));
+                  break;
+          case PROP_ROWSTRIDE:
+                  g_value_set_int (value, gdk_pixbuf_get_rowstride (pixbuf));
+                  break;
+          case PROP_PIXELS:
+                  g_value_set_pointer (value, gdk_pixbuf_get_pixels (pixbuf));
+                  break;
+          default:
+                  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                  break;
+          }
+}
+
+\f
 
 /* Include the marshallers */
 #include <glib-object.h>