image: Add a data setter using GBytes
authorEmmanuele Bassi <ebassi@linux.intel.com>
Mon, 4 Jun 2012 09:34:22 +0000 (10:34 +0100)
committerEmmanuele Bassi <ebassi@linux.intel.com>
Mon, 4 Jun 2012 09:34:22 +0000 (10:34 +0100)
The plain C bytes array, while convenient from a C perspective, is not
well handled by language bindings: the length of the array is not
specified, and it's only just implied by the image data size, rowstride,
and pixel format.

GBytes is a read-only bytes buffer that has an implicit length; we can
use it as the storage medium so that language bindings can actually
function correctly.

clutter/clutter-image.c
clutter/clutter-image.h
clutter/clutter.symbols
doc/reference/clutter/clutter-sections.txt

index 95d88b2..fb5738e 100644 (file)
@@ -183,7 +183,7 @@ clutter_image_new (void)
  * @row_stride: the length of each row inside @data
  * @error: return location for a #GError, or %NULL
  *
- * Sets the image data to be display by @image.
+ * Sets the image data to be displayed by @image.
  *
  * If the image data was successfully loaded, the @image will be invalidated.
  *
@@ -236,6 +236,69 @@ clutter_image_set_data (ClutterImage     *image,
 }
 
 /**
+ * clutter_image_set_bytes:
+ * @image: a #ClutterImage
+ * @data: the image data, as a #GBytes
+ * @pixel_format: the Cogl pixel format of the image data
+ * @width: the width of the image data
+ * @height: the height of the image data
+ * @row_stride: the length of each row inside @data
+ * @error: return location for a #GError, or %NULL
+ *
+ * Sets the image data stored inside a #GBytes to be displayed by @image.
+ *
+ * If the image data was successfully loaded, the @image will be invalidated.
+ *
+ * In case of error, the @error value will be set, and this function will
+ * return %FALSE.
+ *
+ * The image data contained inside the #GBytes is copied in texture memory,
+ * and no additional reference is acquired on the @data.
+ *
+ * Return value: %TRUE if the image data was successfully loaded,
+ *   and %FALSE otherwise.
+ *
+ * Since: 1.12
+ */
+gboolean
+clutter_image_set_bytes (ClutterImage     *image,
+                         GBytes           *data,
+                         CoglPixelFormat   pixel_format,
+                         guint             width,
+                         guint             height,
+                         guint             row_stride,
+                         GError          **error)
+{
+  ClutterImagePrivate *priv;
+
+  g_return_val_if_fail (CLUTTER_IS_IMAGE (image), FALSE);
+  g_return_val_if_fail (data != NULL, FALSE);
+
+  priv = image->priv;
+
+  if (priv->texture != NULL)
+    cogl_object_unref (priv->texture);
+
+  priv->texture = cogl_texture_new_from_data (width, height,
+                                              COGL_TEXTURE_NONE,
+                                              pixel_format,
+                                              COGL_PIXEL_FORMAT_ANY,
+                                              row_stride,
+                                              g_bytes_get_data (data, NULL));
+  if (priv->texture == NULL)
+    {
+      g_set_error_literal (error, CLUTTER_IMAGE_ERROR,
+                           CLUTTER_IMAGE_ERROR_INVALID_DATA,
+                           _("Unable to load image data"));
+      return FALSE;
+    }
+
+  clutter_content_invalidate (CLUTTER_CONTENT (image));
+
+  return TRUE;
+}
+
+/**
  * clutter_image_set_area:
  * @image: a #ClutterImage
  * @data: (array): the image data, as an array of bytes
index 178a2d3..af8df1e 100644 (file)
@@ -122,6 +122,14 @@ gboolean                clutter_image_set_area          (ClutterImage
                                                          const cairo_rectangle_int_t  *rect,
                                                          guint                         row_stride,
                                                          GError                      **error);
+CLUTTER_AVAILABLE_IN_1_12
+gboolean                clutter_image_set_bytes         (ClutterImage                 *image,
+                                                         GBytes                       *data,
+                                                         CoglPixelFormat               pixel_format,
+                                                         guint                         width,
+                                                         guint                         height,
+                                                         guint                         row_stride,
+                                                         GError                      **error);
 
 #if defined(COGL_ENABLE_EXPERIMENTAL_API) && defined(CLUTTER_ENABLE_EXPERIMENTAL_API)
 CLUTTER_AVAILABLE_IN_1_10
index 0c71a6e..c7c2578 100644 (file)
@@ -756,6 +756,7 @@ clutter_image_get_texture
 clutter_image_get_type
 clutter_image_new
 clutter_image_set_area
+clutter_image_set_bytes
 clutter_image_set_data
 clutter_init
 clutter_init_error_get_type
index 1a3abae..d10fa56 100644 (file)
@@ -3114,6 +3114,7 @@ CLUTTER_IMAGE_ERROR
 ClutterImageError
 clutter_image_new
 clutter_image_set_data
+clutter_image_set_bytes
 clutter_image_set_area
 clutter_image_get_texture
 <SUBSECTION Standard>