[gnome-shell/message-tray: 2/5] Add shell_texture_cache_load_from_data, for loading non-file-based images



commit f883e32f264869025dc2e31d57e905bae8c77b14
Author: Dan Winship <danw gnome org>
Date:   Tue Oct 27 11:56:36 2009 -0400

    Add shell_texture_cache_load_from_data, for loading non-file-based images
    
    Based on a patch from Will Thompson
    
    https://bugzilla.gnome.org/show_bug.cgi?id=599193

 src/shell-texture-cache.c |   96 +++++++++++++++++++++++++++++++++-----------
 src/shell-texture-cache.h |    7 +++
 2 files changed, 79 insertions(+), 24 deletions(-)
---
diff --git a/src/shell-texture-cache.c b/src/shell-texture-cache.c
index 55bdc2c..2bea038 100644
--- a/src/shell-texture-cache.c
+++ b/src/shell-texture-cache.c
@@ -316,7 +316,8 @@ on_image_size_prepared (GdkPixbufLoader *pixbuf_loader,
 }
 
 static GdkPixbuf *
-impl_load_pixbuf_file (const char     *uri,
+impl_load_pixbuf_data (const guchar   *data,
+                       gsize           size,
                        int             available_width,
                        int             available_height,
                        GError        **error)
@@ -324,22 +325,10 @@ impl_load_pixbuf_file (const char     *uri,
   GdkPixbufLoader *pixbuf_loader = NULL;
   GdkPixbuf *rotated_pixbuf = NULL;
   GdkPixbuf *pixbuf;
-  GFile *file = NULL;
-  char *contents = NULL;
-  gsize size;
   gboolean success;
   Dimensions available_dimensions;
   int width_before_rotation, width_after_rotation;
 
-  file = g_file_new_for_uri (uri);
-
-  success = g_file_load_contents (file, NULL, &contents, &size, NULL, error);
-
-  if (!success)
-    {
-      goto out;
-    }
-
   pixbuf_loader = gdk_pixbuf_loader_new ();
 
   available_dimensions.width = available_width;
@@ -347,10 +336,7 @@ impl_load_pixbuf_file (const char     *uri,
   g_signal_connect (pixbuf_loader, "size-prepared",
                     G_CALLBACK (on_image_size_prepared), &available_dimensions);
 
-  success = gdk_pixbuf_loader_write (pixbuf_loader,
-                                     (const guchar *) contents,
-                                     size,
-                                     error);
+  success = gdk_pixbuf_loader_write (pixbuf_loader, data, size, error);
   if (!success)
     goto out;
   success = gdk_pixbuf_loader_close (pixbuf_loader, error);
@@ -382,10 +368,7 @@ impl_load_pixbuf_file (const char     *uri,
       g_signal_connect (pixbuf_loader, "size-prepared",
                         G_CALLBACK (on_image_size_prepared), &available_dimensions);
 
-      success = gdk_pixbuf_loader_write (pixbuf_loader,
-                                         (const guchar *) contents,
-                                         size,
-                                         error);
+      success = gdk_pixbuf_loader_write (pixbuf_loader, data, size, error);
       if (!success)
         goto out;
 
@@ -399,15 +382,37 @@ impl_load_pixbuf_file (const char     *uri,
     }
 
 out:
-  g_free (contents);
-  if (file)
-    g_object_unref (file);
   if (pixbuf_loader)
     g_object_unref (pixbuf_loader);
   return rotated_pixbuf;
 }
 
 static GdkPixbuf *
+impl_load_pixbuf_file (const char     *uri,
+                       int             available_width,
+                       int             available_height,
+                       GError        **error)
+{
+  GdkPixbuf *pixbuf = NULL;
+  GFile *file;
+  char *contents = NULL;
+  gsize size;
+
+  file = g_file_new_for_uri (uri);
+  if (g_file_load_contents (file, NULL, &contents, &size, NULL, error))
+    {
+      pixbuf = impl_load_pixbuf_data ((const guchar *) contents, size,
+                                      available_width, available_height,
+                                      error);
+    }
+
+  g_object_unref (file);
+  g_free (contents);
+
+  return pixbuf;
+}
+
+static GdkPixbuf *
 impl_load_thumbnail (ShellTextureCache *cache,
                      const char        *uri,
                      const char        *mime_type,
@@ -1136,6 +1141,49 @@ shell_texture_cache_load_uri_sync (ShellTextureCache *cache,
 }
 
 /**
+ * shell_texture_cache_load_from_data:
+ * @cache: The texture cache instance
+ * @data: Raw image data
+ * @len: length of @data
+ * @available_width: available width for the image, can be -1 if not limited
+ * @available_height: available height for the image, can be -1 if not limited
+ * @error: Return location for error
+ *
+ * Synchronously creates an image from @data. The image is scaled down
+ * to fit the available width and height dimensions, but the image is
+ * never scaled up beyond its actual size. The pixbuf is rotated
+ * according to the associated orientation setting.
+ *
+ * Return value: (transfer none): A new #ClutterActor with the image data loaded if it was
+ *               generated succesfully, %NULL otherwise
+ */
+ClutterActor *
+shell_texture_cache_load_from_data (ShellTextureCache *cache,
+                                    const guchar      *data,
+                                    gsize              len,
+                                    int                available_width,
+                                    int                available_height,
+                                    GError           **error)
+{
+  ClutterTexture *texture;
+  CoglHandle texdata;
+  GdkPixbuf *pixbuf;
+
+  pixbuf = impl_load_pixbuf_data (data, len, available_width, available_height, error);
+  if (pixbuf == NULL)
+    return NULL;
+
+  texture = create_default_texture (cache);
+  texdata = pixbuf_to_cogl_handle (pixbuf);
+  set_texture_cogl_texture (texture, texdata);
+
+  g_object_unref (pixbuf);
+  cogl_handle_unref (texdata);
+
+  return CLUTTER_ACTOR (texture);
+}
+
+/**
  * shell_texture_cache_load_thumbnail:
  * @cache:
  * @size: Size in pixels to use for thumbnail
diff --git a/src/shell-texture-cache.h b/src/shell-texture-cache.h
index 21dc7d7..d88967a 100644
--- a/src/shell-texture-cache.h
+++ b/src/shell-texture-cache.h
@@ -80,6 +80,13 @@ ClutterActor *shell_texture_cache_load_uri_sync (ShellTextureCache *cache,
                                                  int                available_height,
                                                  GError           **error);
 
+ClutterActor *shell_texture_cache_load_from_data (ShellTextureCache *cache,
+                                                  const guchar      *data,
+                                                  gsize              len,
+                                                  int                available_width,
+                                                  int                available_height,
+                                                  GError           **error);
+
 gboolean shell_texture_cache_pixbuf_equal (ShellTextureCache *cache, GdkPixbuf *a, GdkPixbuf *b);
 
 #endif /* __SHELL_TEXTURE_CACHE_H__ */



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