[clutter/wip/actor-content: 26/33] image-loader: Allow using size when loading (sync)



commit 83c39e37dfbee55b8c10c7d1349b8050f504fbaa
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Mon Apr 18 11:52:05 2011 +0100

    image-loader: Allow using size when loading (sync)
    
    Loading an image with a specific size should be allowed; this avoids
    loading a huge image and then scaling it down for display purposes.

 clutter/clutter-image-loader.c                     |   22 ++++-
 clutter/clutter-image-loader.h                     |   92 ++++++++++++--------
 clutter/clutter-image.c                            |    2 +
 .../image-loaders/clutter-image-loader-pixbuf.c    |   20 +++-
 4 files changed, 92 insertions(+), 44 deletions(-)
---
diff --git a/clutter/clutter-image-loader.c b/clutter/clutter-image-loader.c
index 48ea287..9a667e3 100644
--- a/clutter/clutter-image-loader.c
+++ b/clutter/clutter-image-loader.c
@@ -175,6 +175,15 @@ _clutter_image_loader_get_texture_handle (ClutterImageLoader *loader)
  * clutter_image_loader_load_stream:
  * @loader: a #ClutterImageLoader
  * @stream: a #GInputStream
+ * @width: the width to use when loading the image, or -1 to use the
+ *   image's own size; if @flags is %CLUTTER_IMAGE_LOAD_PRESERVE_ASPECT
+ *   and width is -1 then the width of the image will be set to preserve
+ *   the aspect ratio with the given @height
+ * @height: the height to use when loading the image, or -1 to use the
+ *   image's own size; if @flags is %CLUTTER_IMAGE_LOAD_PRESERVE_ASPECT
+ *   and height is -1 then the height of the image will be set to
+ *   preserve the aspect ratio with the given @width
+ * @flags: flags to control the image loading
  * @cancellable: (allow-none): a #GCancellable or %NULL
  * @error: return location for a #GError or %NULL
  *
@@ -185,10 +194,13 @@ _clutter_image_loader_get_texture_handle (ClutterImageLoader *loader)
  * Since: 1.8
  */
 gboolean
-_clutter_image_loader_load_stream (ClutterImageLoader  *loader,
-                                   GInputStream        *stream,
-                                   GCancellable        *cancellable,
-                                   GError             **error)
+_clutter_image_loader_load_stream (ClutterImageLoader     *loader,
+                                   GInputStream           *stream,
+                                   gint                    width,
+                                   gint                    height,
+                                   ClutterImageLoadFlags   flags,
+                                   GCancellable           *cancellable,
+                                   GError                **error)
 {
   g_return_val_if_fail (CLUTTER_IS_IMAGE_LOADER (loader), FALSE);
   g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
@@ -197,6 +209,8 @@ _clutter_image_loader_load_stream (ClutterImageLoader  *loader,
 
 
   return CLUTTER_IMAGE_LOADER_GET_CLASS (loader)->load_stream (loader, stream,
+                                                               width, height,
+                                                               flags,
                                                                cancellable,
                                                                error);
 }
diff --git a/clutter/clutter-image-loader.h b/clutter/clutter-image-loader.h
index 016f29f..acfc4d6 100644
--- a/clutter/clutter-image-loader.h
+++ b/clutter/clutter-image-loader.h
@@ -56,6 +56,22 @@ typedef struct _ClutterImageLoader              ClutterImageLoader;
 typedef struct _ClutterImageLoaderClass         ClutterImageLoaderClass;
 
 /**
+ * ClutterImageLoadFlags:
+ * @CLUTTER_IMAGE_LOAD_NONE: No flags
+ * @CLUTTER_IMAGE_LOAD_PRESERVE_ASPECT: Preserve the aspect ratio of the
+ *   image when loading with a predefined size
+ *
+ * Flags to be passed to the image loading functions.
+ *
+ * Since: 1.8
+ */
+typedef enum {
+  CLUTTER_IMAGE_LOAD_NONE = 0,
+
+  CLUTTER_IMAGE_LOAD_PRESERVE_ASPECT = 1 << 0
+} ClutterImageLoadFlags;
+
+/**
  * ClutterImageLoader:
  *
  * The <structname>ClutterImageLoader</structname> structure contains
@@ -82,27 +98,30 @@ struct _ClutterImageLoaderClass
   /*< private >*/
   GObjectClass parent_class;
 
-  gboolean (* is_supported) (void);
+  gboolean   (* is_supported)       (void);
 
-  gboolean (* load_stream) (ClutterImageLoader *loader,
-                            GInputStream       *stream,
-                            GCancellable       *cancellable,
-                            GError            **error);
+  gboolean   (* load_stream)        (ClutterImageLoader     *loader,
+                                     GInputStream           *stream,
+                                     gint                    width,
+                                     gint                    height,
+                                     ClutterImageLoadFlags   flags,
+                                     GCancellable           *cancellable,
+                                     GError                **error);
 
-  void (* load_stream_async) (ClutterImageLoader *loader,
-                              GInputStream       *stream,
-                              GCancellable       *cancellable,
-                              GAsyncReadyCallback      callback,
-                              gpointer            user_data);
-  gboolean (* load_stream_finish) (ClutterImageLoader  *loader,
-                                   GAsyncResult        *result,
-                                   GError             **error);
+  void       (* load_stream_async)  (ClutterImageLoader     *loader,
+                                     GInputStream           *stream,
+                                     GCancellable           *cancellable,
+                                     GAsyncReadyCallback     callback,
+                                     gpointer                user_data);
+  gboolean   (* load_stream_finish) (ClutterImageLoader     *loader,
+                                     GAsyncResult           *result,
+                                     GError                **error);
 
-  void (* get_image_size) (ClutterImageLoader *loader,
-                           gint               *width,
-                           gint               *height);
+  void       (* get_image_size)     (ClutterImageLoader     *loader,
+                                     gint                   *width,
+                                     gint                   *height);
 
-  CoglHandle (* get_texture_handle) (ClutterImageLoader *loader);
+  CoglHandle (* get_texture_handle) (ClutterImageLoader     *loader);
 
   void (* _clutter_image_loader__1) (void);
   void (* _clutter_image_loader__2) (void);
@@ -127,24 +146,27 @@ GType clutter_image_loader_get_type (void) G_GNUC_CONST;
 
 ClutterImageLoader *    _clutter_image_loader_new                       (void);
 
-void                    _clutter_image_loader_get_image_size            (ClutterImageLoader   *loader,
-                                                                         gint                 *width,
-                                                                         gint                 *height);
-CoglHandle              _clutter_image_loader_get_texture_handle        (ClutterImageLoader   *loader);
-
-gboolean                _clutter_image_loader_load_stream               (ClutterImageLoader   *loader,
-                                                                         GInputStream         *stream,
-                                                                         GCancellable         *cancellable,
-                                                                         GError              **error);
-
-void                    _clutter_image_loader_load_stream_async         (ClutterImageLoader   *loader,
-                                                                         GInputStream         *stream,
-                                                                         GCancellable         *cancellable,
-                                                                         GAsyncReadyCallback   callback,
-                                                                         gpointer              user_data);
-gboolean                _clutter_image_loader_load_stream_finish        (ClutterImageLoader   *loader,
-                                                                         GAsyncResult         *result,
-                                                                         GError              **error);
+void                    _clutter_image_loader_get_image_size            (ClutterImageLoader     *loader,
+                                                                         gint                   *width,
+                                                                         gint                   *height);
+CoglHandle              _clutter_image_loader_get_texture_handle        (ClutterImageLoader     *loader);
+
+gboolean                _clutter_image_loader_load_stream               (ClutterImageLoader     *loader,
+                                                                         GInputStream           *stream,
+                                                                         gint                    width,
+                                                                         gint                    height,
+                                                                         ClutterImageLoadFlags   flags,
+                                                                         GCancellable           *cancellable,
+                                                                         GError                **error);
+
+void                    _clutter_image_loader_load_stream_async         (ClutterImageLoader     *loader,
+                                                                         GInputStream           *stream,
+                                                                         GCancellable           *cancellable,
+                                                                         GAsyncReadyCallback     callback,
+                                                                         gpointer                user_data);
+gboolean                _clutter_image_loader_load_stream_finish        (ClutterImageLoader     *loader,
+                                                                         GAsyncResult           *result,
+                                                                         GError                **error);
 
 G_END_DECLS
 
diff --git a/clutter/clutter-image.c b/clutter/clutter-image.c
index 02027f9..ad2fb68 100644
--- a/clutter/clutter-image.c
+++ b/clutter/clutter-image.c
@@ -486,6 +486,8 @@ clutter_image_load (ClutterImage  *image,
 
   res = _clutter_image_loader_load_stream (loader,
                                            G_INPUT_STREAM (stream),
+                                           -1, -1,
+                                           CLUTTER_IMAGE_LOAD_NONE,
                                            cancellable,
                                            error);
   if (!res)
diff --git a/clutter/image-loaders/clutter-image-loader-pixbuf.c b/clutter/image-loaders/clutter-image-loader-pixbuf.c
index 87a7dcf..cfa02bc 100644
--- a/clutter/image-loaders/clutter-image-loader-pixbuf.c
+++ b/clutter/image-loaders/clutter-image-loader-pixbuf.c
@@ -88,15 +88,25 @@ clutter_image_loader_pixbuf_is_supported (void)
 }
 
 static gboolean
-clutter_image_loader_pixbuf_load_stream (ClutterImageLoader *loader,
-                                         GInputStream       *stream,
-                                         GCancellable       *cancellable,
-                                         GError            **error)
+clutter_image_loader_pixbuf_load_stream (ClutterImageLoader     *loader,
+                                         GInputStream           *stream,
+                                         gint                    width,
+                                         gint                    height,
+                                         ClutterImageLoadFlags   flags,
+                                         GCancellable           *cancellable,
+                                         GError                **error)
 {
   ClutterImageLoaderPixbuf *self = CLUTTER_IMAGE_LOADER_PIXBUF (loader);
+  gboolean preserve_aspect_ratio;
   GdkPixbuf *pixbuf;
 
-  pixbuf = gdk_pixbuf_new_from_stream (stream, cancellable, error);
+  preserve_aspect_ratio = (flags & CLUTTER_IMAGE_LOAD_PRESERVE_ASPECT) != 0;
+
+  pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
+                                                width, height,
+                                                preserve_aspect_ratio,
+                                                cancellable,
+                                                error);
   if (pixbuf == NULL)
     return FALSE;
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]