4 * An OpenGL based 'interactive image' library.
6 * Copyright (C) 2012 Intel Corporation.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 * Emmanuele Bassi <ebassi@linux.intel.com>
26 * SECTION:clutter-image
27 * @Title: ClutterImage
28 * @Short_Description: Image data content
30 * #ClutterImage is a #ClutterContent implementation that displays
33 * <informalexample><programlisting>
34 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../examples/image-content.c">
35 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
37 * </programlisting></informalexample>
39 * #ClutterImage is available since Clutter 1.10.
46 #define CLUTTER_ENABLE_EXPERIMENTAL_API
48 #include "clutter-image.h"
50 #include "clutter-color.h"
51 #include "clutter-content-private.h"
52 #include "clutter-debug.h"
53 #include "clutter-paint-node.h"
54 #include "clutter-paint-nodes.h"
55 #include "clutter-private.h"
57 struct _ClutterImagePrivate
62 static void clutter_content_iface_init (ClutterContentIface *iface);
64 G_DEFINE_TYPE_WITH_CODE (ClutterImage, clutter_image, G_TYPE_OBJECT,
65 G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_CONTENT,
66 clutter_content_iface_init))
69 clutter_image_error_quark (void)
71 return g_quark_from_static_string ("clutter-image-error-quark");
75 clutter_image_finalize (GObject *gobject)
77 ClutterImagePrivate *priv = CLUTTER_IMAGE (gobject)->priv;
79 if (priv->texture != NULL)
81 cogl_object_unref (priv->texture);
85 G_OBJECT_CLASS (clutter_image_parent_class)->finalize (gobject);
89 clutter_image_class_init (ClutterImageClass *klass)
91 g_type_class_add_private (klass, sizeof (ClutterImagePrivate));
93 G_OBJECT_CLASS (klass)->finalize = clutter_image_finalize;
97 clutter_image_init (ClutterImage *self)
99 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, CLUTTER_TYPE_IMAGE,
100 ClutterImagePrivate);
104 clutter_image_paint_content (ClutterContent *content,
106 ClutterPaintNode *root)
108 ClutterImagePrivate *priv = CLUTTER_IMAGE (content)->priv;
109 ClutterScalingFilter min_f, mag_f;
110 ClutterPaintNode *node;
113 guint8 paint_opacity;
115 if (priv->texture == NULL)
118 clutter_actor_get_content_box (actor, &box);
119 paint_opacity = clutter_actor_get_paint_opacity (actor);
120 clutter_actor_get_content_scaling_filters (actor, &min_f, &mag_f);
122 color.red = paint_opacity;
123 color.green = paint_opacity;
124 color.blue = paint_opacity;
125 color.alpha = paint_opacity;
127 node = clutter_texture_node_new (priv->texture, &color, min_f, mag_f);
128 clutter_paint_node_set_name (node, "Image");
129 clutter_paint_node_add_rectangle (node, &box);
130 clutter_paint_node_add_child (root, node);
131 clutter_paint_node_unref (node);
135 clutter_image_get_preferred_size (ClutterContent *content,
139 ClutterImagePrivate *priv = CLUTTER_IMAGE (content)->priv;
141 if (priv->texture == NULL)
145 *width = cogl_texture_get_width (priv->texture);
148 *height = cogl_texture_get_height (priv->texture);
154 clutter_content_iface_init (ClutterContentIface *iface)
156 iface->get_preferred_size = clutter_image_get_preferred_size;
157 iface->paint_content = clutter_image_paint_content;
163 * Creates a new #ClutterImage instance.
165 * Return value: (transfer full): the newly created #ClutterImage instance.
166 * Use g_object_unref() when done.
171 clutter_image_new (void)
173 return g_object_new (CLUTTER_TYPE_IMAGE, NULL);
177 * clutter_image_set_data:
178 * @image: a #ClutterImage
179 * @data: (array): the image data, as an array of bytes
180 * @pixel_format: the Cogl pixel format of the image data
181 * @width: the width of the image data
182 * @height: the height of the image data
183 * @row_stride: the length of each row inside @data
184 * @error: return location for a #GError, or %NULL
186 * Sets the image data to be displayed by @image.
188 * If the image data was successfully loaded, the @image will be invalidated.
190 * In case of error, the @error value will be set, and this function will
193 * The image data is copied in texture memory.
195 * Return value: %TRUE if the image data was successfully loaded,
196 * and %FALSE otherwise.
201 clutter_image_set_data (ClutterImage *image,
203 CoglPixelFormat pixel_format,
209 ClutterImagePrivate *priv;
211 g_return_val_if_fail (CLUTTER_IS_IMAGE (image), FALSE);
212 g_return_val_if_fail (data != NULL, FALSE);
216 if (priv->texture != NULL)
217 cogl_object_unref (priv->texture);
219 priv->texture = cogl_texture_new_from_data (width, height,
222 COGL_PIXEL_FORMAT_ANY,
225 if (priv->texture == NULL)
227 g_set_error_literal (error, CLUTTER_IMAGE_ERROR,
228 CLUTTER_IMAGE_ERROR_INVALID_DATA,
229 _("Unable to load image data"));
233 clutter_content_invalidate (CLUTTER_CONTENT (image));
239 * clutter_image_set_bytes:
240 * @image: a #ClutterImage
241 * @data: the image data, as a #GBytes
242 * @pixel_format: the Cogl pixel format of the image data
243 * @width: the width of the image data
244 * @height: the height of the image data
245 * @row_stride: the length of each row inside @data
246 * @error: return location for a #GError, or %NULL
248 * Sets the image data stored inside a #GBytes to be displayed by @image.
250 * If the image data was successfully loaded, the @image will be invalidated.
252 * In case of error, the @error value will be set, and this function will
255 * The image data contained inside the #GBytes is copied in texture memory,
256 * and no additional reference is acquired on the @data.
258 * Return value: %TRUE if the image data was successfully loaded,
259 * and %FALSE otherwise.
264 clutter_image_set_bytes (ClutterImage *image,
266 CoglPixelFormat pixel_format,
272 ClutterImagePrivate *priv;
274 g_return_val_if_fail (CLUTTER_IS_IMAGE (image), FALSE);
275 g_return_val_if_fail (data != NULL, FALSE);
279 if (priv->texture != NULL)
280 cogl_object_unref (priv->texture);
282 priv->texture = cogl_texture_new_from_data (width, height,
285 COGL_PIXEL_FORMAT_ANY,
287 g_bytes_get_data (data, NULL));
288 if (priv->texture == NULL)
290 g_set_error_literal (error, CLUTTER_IMAGE_ERROR,
291 CLUTTER_IMAGE_ERROR_INVALID_DATA,
292 _("Unable to load image data"));
296 clutter_content_invalidate (CLUTTER_CONTENT (image));
302 * clutter_image_set_area:
303 * @image: a #ClutterImage
304 * @data: (array): the image data, as an array of bytes
305 * @pixel_format: the Cogl pixel format of the image data
306 * @rect: a rectangle indicating the area that should be set
307 * @row_stride: the length of each row inside @data
308 * @error: return location for a #GError, or %NULL
310 * Sets the image data to be display by @image, using @rect to indicate
311 * the position and size of the image data to be set.
313 * If the @image does not have any image data set when this function is
314 * called, a new texture will be created with the size of the width and
315 * height of the rectangle, i.e. calling this function on a newly created
316 * #ClutterImage will be the equivalent of calling clutter_image_set_data().
318 * If the image data was successfully loaded, the @image will be invalidated.
320 * In case of error, the @error value will be set, and this function will
323 * The image data is copied in texture memory.
325 * Return value: %TRUE if the image data was successfully loaded,
326 * and %FALSE otherwise.
331 clutter_image_set_area (ClutterImage *image,
333 CoglPixelFormat pixel_format,
334 const cairo_rectangle_int_t *area,
338 ClutterImagePrivate *priv;
340 g_return_val_if_fail (CLUTTER_IS_IMAGE (image), FALSE);
341 g_return_val_if_fail (data != NULL, FALSE);
342 g_return_val_if_fail (area != NULL, FALSE);
346 if (priv->texture == NULL)
348 priv->texture = cogl_texture_new_from_data (area->width,
352 COGL_PIXEL_FORMAT_ANY,
360 res = cogl_texture_set_region (priv->texture,
363 area->width, area->height,
364 area->width, area->height,
371 cogl_object_unref (priv->texture);
372 priv->texture = NULL;
376 if (priv->texture == NULL)
378 g_set_error_literal (error, CLUTTER_IMAGE_ERROR,
379 CLUTTER_IMAGE_ERROR_INVALID_DATA,
380 _("Unable to load image data"));
384 clutter_content_invalidate (CLUTTER_CONTENT (image));
390 * clutter_image_get_texture:
391 * @image: a #ClutterImage
393 * Retrieves a pointer to the Cogl texture used by @image.
395 * If you change the contents of the returned Cogl texture you will need
396 * to manually invalidate the @image with clutter_content_invalidate()
397 * in order to update the actors using @image as their content.
399 * Return value: (transfer none): a pointer to the Cogl texture, or %NULL
402 * Stability: unstable
405 clutter_image_get_texture (ClutterImage *image)
407 g_return_val_if_fail (CLUTTER_IS_IMAGE (image), NULL);
409 return image->priv->texture;