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
This commit is contained in:
parent
c985c3cf78
commit
f883e32f26
@ -316,7 +316,8 @@ on_image_size_prepared (GdkPixbufLoader *pixbuf_loader,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static GdkPixbuf *
|
static GdkPixbuf *
|
||||||
impl_load_pixbuf_file (const char *uri,
|
impl_load_pixbuf_data (const guchar *data,
|
||||||
|
gsize size,
|
||||||
int available_width,
|
int available_width,
|
||||||
int available_height,
|
int available_height,
|
||||||
GError **error)
|
GError **error)
|
||||||
@ -324,22 +325,10 @@ impl_load_pixbuf_file (const char *uri,
|
|||||||
GdkPixbufLoader *pixbuf_loader = NULL;
|
GdkPixbufLoader *pixbuf_loader = NULL;
|
||||||
GdkPixbuf *rotated_pixbuf = NULL;
|
GdkPixbuf *rotated_pixbuf = NULL;
|
||||||
GdkPixbuf *pixbuf;
|
GdkPixbuf *pixbuf;
|
||||||
GFile *file = NULL;
|
|
||||||
char *contents = NULL;
|
|
||||||
gsize size;
|
|
||||||
gboolean success;
|
gboolean success;
|
||||||
Dimensions available_dimensions;
|
Dimensions available_dimensions;
|
||||||
int width_before_rotation, width_after_rotation;
|
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 ();
|
pixbuf_loader = gdk_pixbuf_loader_new ();
|
||||||
|
|
||||||
available_dimensions.width = available_width;
|
available_dimensions.width = available_width;
|
||||||
@ -347,10 +336,7 @@ impl_load_pixbuf_file (const char *uri,
|
|||||||
g_signal_connect (pixbuf_loader, "size-prepared",
|
g_signal_connect (pixbuf_loader, "size-prepared",
|
||||||
G_CALLBACK (on_image_size_prepared), &available_dimensions);
|
G_CALLBACK (on_image_size_prepared), &available_dimensions);
|
||||||
|
|
||||||
success = gdk_pixbuf_loader_write (pixbuf_loader,
|
success = gdk_pixbuf_loader_write (pixbuf_loader, data, size, error);
|
||||||
(const guchar *) contents,
|
|
||||||
size,
|
|
||||||
error);
|
|
||||||
if (!success)
|
if (!success)
|
||||||
goto out;
|
goto out;
|
||||||
success = gdk_pixbuf_loader_close (pixbuf_loader, error);
|
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_signal_connect (pixbuf_loader, "size-prepared",
|
||||||
G_CALLBACK (on_image_size_prepared), &available_dimensions);
|
G_CALLBACK (on_image_size_prepared), &available_dimensions);
|
||||||
|
|
||||||
success = gdk_pixbuf_loader_write (pixbuf_loader,
|
success = gdk_pixbuf_loader_write (pixbuf_loader, data, size, error);
|
||||||
(const guchar *) contents,
|
|
||||||
size,
|
|
||||||
error);
|
|
||||||
if (!success)
|
if (!success)
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
@ -399,14 +382,36 @@ impl_load_pixbuf_file (const char *uri,
|
|||||||
}
|
}
|
||||||
|
|
||||||
out:
|
out:
|
||||||
g_free (contents);
|
|
||||||
if (file)
|
|
||||||
g_object_unref (file);
|
|
||||||
if (pixbuf_loader)
|
if (pixbuf_loader)
|
||||||
g_object_unref (pixbuf_loader);
|
g_object_unref (pixbuf_loader);
|
||||||
return rotated_pixbuf;
|
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 *
|
static GdkPixbuf *
|
||||||
impl_load_thumbnail (ShellTextureCache *cache,
|
impl_load_thumbnail (ShellTextureCache *cache,
|
||||||
const char *uri,
|
const char *uri,
|
||||||
@ -1135,6 +1140,49 @@ shell_texture_cache_load_uri_sync (ShellTextureCache *cache,
|
|||||||
return CLUTTER_ACTOR (texture);
|
return CLUTTER_ACTOR (texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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:
|
* shell_texture_cache_load_thumbnail:
|
||||||
* @cache:
|
* @cache:
|
||||||
|
@ -80,6 +80,13 @@ ClutterActor *shell_texture_cache_load_uri_sync (ShellTextureCache *cache,
|
|||||||
int available_height,
|
int available_height,
|
||||||
GError **error);
|
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);
|
gboolean shell_texture_cache_pixbuf_equal (ShellTextureCache *cache, GdkPixbuf *a, GdkPixbuf *b);
|
||||||
|
|
||||||
#endif /* __SHELL_TEXTURE_CACHE_H__ */
|
#endif /* __SHELL_TEXTURE_CACHE_H__ */
|
||||||
|
Loading…
Reference in New Issue
Block a user