Bug 578178 – Show large previews for image files

Use the actual image from the file for expanded mode previews for image files. Use the pixbuf loader to set the appropriate image size as soon as the image is prepared, but before it is loaded, to avoid loading large images. Apply the pixbuf orientation setting so that the image is properly rotated. Preserve the original size of the image if its dimensions are smaller than the space available.

Make sure we provide the accurate available width for the details actor. This
width has to exclude the padding and border width. Also provide the available height for the details actor.
This commit is contained in:
Marina Zhurakhinskaya
2009-04-28 15:35:36 -04:00
parent 59b12687d4
commit 82cf98740a
5 changed files with 273 additions and 31 deletions

View File

@ -15,6 +15,7 @@
#include <unistd.h>
#include <dbus/dbus-glib.h>
#include <libgnomeui/gnome-thumbnail.h>
#include <math.h>
#define SHELL_DBUS_SERVICE "org.gnome.Shell"
@ -368,6 +369,191 @@ shell_get_thumbnail_for_recent_info(GtkRecentInfo *recent_info)
return pixbuf;
}
// A private structure for keeping width and height.
typedef struct {
int width;
int height;
} Dimensions;
/**
* on_image_size_prepared:
*
* @pixbuf_loader: #GdkPixbufLoader loading the image
* @width: the original width of the image
* @height: the original height of the image
* @data: pointer to the #Dimensions sructure containing available width and height for the image,
* available width or height can be -1 if the dimension is not limited
*
* Private function.
*
* Sets the size of the image being loaded to fit the available width and height dimensions,
* but never scales up the image beyond its actual size.
* Intended to be used as a callback for #GdkPixbufLoader "size-prepared" signal.
*/
static void
on_image_size_prepared (GdkPixbufLoader *pixbuf_loader,
gint width,
gint height,
gpointer data)
{
Dimensions *available_dimensions = data;
int available_width = available_dimensions->width;
int available_height = available_dimensions->height;
int scaled_width = -1;
int scaled_height = -1;
if (width == 0 || height == 0)
return;
if (available_width >= 0 && available_height >= 0)
{
// This should keep the aspect ratio of the image intact, because if
// available_width < (available_height * width) / height
// than
// (available_width * height) / width < available_height
// So we are guaranteed to either scale the image to have an available_width
// for width and height scaled accordingly OR have the available_height
// for height and width scaled accordingly, whichever scaling results
// in the image that can fit both available dimensions.
scaled_width = MIN(available_width, (available_height * width) / height);
scaled_height = MIN(available_height, (available_width * height) / width);
}
else if (available_width >= 0)
{
scaled_width = available_width;
scaled_height = (available_width * height) / width;
}
else if (available_height >= 0)
{
scaled_width = (available_height * width) / height;
scaled_height = available_height;
}
// Scale the image only if that will not increase its original dimensions.
if (scaled_width >= 0 && scaled_height >= 0 && scaled_width < width && scaled_height < height)
gdk_pixbuf_loader_set_size (pixbuf_loader, scaled_width, scaled_height);
}
/**
* shell_create_pixbuf_from_image_file:
*
* @uri: uri of the image file from which to create a pixbuf
* @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
*
* Return value: (transfer full): #GdkPixbuf with the image file loaded if it was
* generated succesfully, %NULL otherwise
* 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.
*/
GdkPixbuf *
shell_create_pixbuf_from_image_file(const char *uri,
int available_width,
int available_height)
{
GdkPixbufLoader *pixbuf_loader = NULL;
GdkPixbuf *pixbuf;
GdkPixbuf *rotated_pixbuf = NULL;
GFile *file = NULL;
char *contents = NULL;
gsize size;
GError *error = NULL;
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)
{
g_warning ("Could not load contents of the file with uri %s: %s", uri, error->message);
goto out;
}
pixbuf_loader = gdk_pixbuf_loader_new ();
available_dimensions.width = available_width;
available_dimensions.height = available_height;
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);
if (!success)
{
g_warning ("Could not write contents of the file with uri %s to the gdk pixbuf loader: %s", uri, error->message);
goto out;
}
success = gdk_pixbuf_loader_close (pixbuf_loader, &error);
if (!success)
{
g_warning ("Could not close the pixbuf loader after writing contents of the file with uri %s: %s", uri, error->message);
goto out;
}
pixbuf = gdk_pixbuf_loader_get_pixbuf (pixbuf_loader);
width_before_rotation = gdk_pixbuf_get_width (pixbuf);
rotated_pixbuf = gdk_pixbuf_apply_embedded_orientation (pixbuf);
width_after_rotation = gdk_pixbuf_get_width (rotated_pixbuf);
// There is currently no way to tell if the pixbuf will need to be rotated before it is loaded,
// so we only check that once it is loaded, and reload it again if it needs to be rotated in order
// to use the available width and height correctly.
// http://bugzilla.gnome.org/show_bug.cgi?id=579003
if (width_before_rotation != width_after_rotation)
{
g_object_unref (pixbuf_loader);
g_object_unref (rotated_pixbuf);
rotated_pixbuf = NULL;
pixbuf_loader = gdk_pixbuf_loader_new ();
// We know that the image will later be rotated, so we reverse the available dimensions.
available_dimensions.width = available_height;
available_dimensions.height = available_width;
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);
if (!success)
{
g_warning ("Could not write contents of the file with uri %s to the gdk pixbuf loader: %s", uri, error->message);
goto out;
}
success = gdk_pixbuf_loader_close (pixbuf_loader, &error);
if (!success)
{
g_warning ("Could not close the pixbuf loader after writing contents of the file with uri %s: %s", uri, error->message);
goto out;
}
pixbuf = gdk_pixbuf_loader_get_pixbuf (pixbuf_loader);
rotated_pixbuf = gdk_pixbuf_apply_embedded_orientation (pixbuf);
}
out:
g_clear_error (&error);
g_free (contents);
if (file)
g_object_unref (file);
if (pixbuf_loader)
g_object_unref (pixbuf_loader);
return rotated_pixbuf;
}
/**
* shell_get_categories_for_desktop_file:
*

View File

@ -36,6 +36,10 @@ gboolean shell_clutter_texture_set_from_pixbuf (ClutterTexture *texture,
GdkPixbuf *shell_get_thumbnail_for_recent_info(GtkRecentInfo *recent_info);
GdkPixbuf *shell_create_pixbuf_from_image_file(const char *uri,
int available_width,
int available_height);
GSList *shell_get_categories_for_desktop_file(const char *desktop_file_name);
guint16 shell_get_event_key_symbol(ClutterEvent *event);