image: Add a data setter using GBytes

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.
This commit is contained in:
Emmanuele Bassi 2012-06-04 10:34:22 +01:00
parent dcae4909f3
commit 65c8b11604
4 changed files with 74 additions and 1 deletions

View File

@ -183,7 +183,7 @@ clutter_image_new (void)
* @row_stride: the length of each row inside @data * @row_stride: the length of each row inside @data
* @error: return location for a #GError, or %NULL * @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. * If the image data was successfully loaded, the @image will be invalidated.
* *
@ -235,6 +235,69 @@ clutter_image_set_data (ClutterImage *image,
return TRUE; return TRUE;
} }
/**
* 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: * clutter_image_set_area:
* @image: a #ClutterImage * @image: a #ClutterImage

View File

@ -122,6 +122,14 @@ gboolean clutter_image_set_area (ClutterImage
const cairo_rectangle_int_t *rect, const cairo_rectangle_int_t *rect,
guint row_stride, guint row_stride,
GError **error); 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) #if defined(COGL_ENABLE_EXPERIMENTAL_API) && defined(CLUTTER_ENABLE_EXPERIMENTAL_API)
CLUTTER_AVAILABLE_IN_1_10 CLUTTER_AVAILABLE_IN_1_10

View File

@ -756,6 +756,7 @@ clutter_image_get_texture
clutter_image_get_type clutter_image_get_type
clutter_image_new clutter_image_new
clutter_image_set_area clutter_image_set_area
clutter_image_set_bytes
clutter_image_set_data clutter_image_set_data
clutter_init clutter_init
clutter_init_error_get_type clutter_init_error_get_type

View File

@ -3114,6 +3114,7 @@ CLUTTER_IMAGE_ERROR
ClutterImageError ClutterImageError
clutter_image_new clutter_image_new
clutter_image_set_data clutter_image_set_data
clutter_image_set_bytes
clutter_image_set_area clutter_image_set_area
clutter_image_get_texture clutter_image_get_texture
<SUBSECTION Standard> <SUBSECTION Standard>