[gnome-shell/wip/rstrode/fix-smooshed-fedora] texture-cache: preserve aspect ratio of loaded pixbufs



commit f581ade9a38db1caa19a22bf2d911c13706c775d
Author: Ray Strode <rstrode redhat com>
Date:   Tue Apr 30 14:58:26 2019 -0400

    texture-cache: preserve aspect ratio of loaded pixbufs
    
    st_texture_cache_load_file_async takes optional width and height hints
    to specify how much space on-screen the loaded image will take up. One
    or both of the width and height can be negative.
    
    gdk-pixbuf uses this information to scale down the image to an
    appropriate size, when necessary, to save memory and prevent
    aliasing.
    
    gdk-pixbuf never uses this information to scale up the image, though;
    that would just waste memory. Instead, the width and height hints are
    used to set the geometry of the clutter content, so the texture may
    get upscaled on-screen.
    
    If both the passed in width and height are negative, it means to assume
    the image will fit at native size, and gdk-pixbuf shouldn't downscale
    the image.
    
    If just one dimension is negative, it means the image should be
    downscaled by gdk-pixbuf proportionally to fit the given (positive)
    dimension.
    
    If neither dimension is negative, it means the image should be
    downscaled by gdk-pixbuf proportionally to fit whichever dimension keeps
    the whole image within the bounds of its clutter content.
    
    In any case, if the passed in width and height are bigger than the
    dimensions of the loaded image, then those passed in values won't be used
    by gdk-pixbuf, since it has no downscaling to do.
    
    The problem is, the code that converts a pixbuf to a clutter content
    fails to handle the case where the passed in dimensions aren't used by
    gdk-pixbuf.  It assumes at least one of the pixbuf dimensions matches the
    dimensions passed to st_texture_cache_load_file_async.  That won't be the
    case if the pixbuf code had no downscaling to do.
    
    This commit fixes that problem by deriving the unknown clutter content
    dimension from the known clutter content dimension and the aspect ratio
    of the image.

 src/st/st-texture-cache.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)
---
diff --git a/src/st/st-texture-cache.c b/src/st/st-texture-cache.c
index cbe3afaba..957f353ff 100644
--- a/src/st/st-texture-cache.c
+++ b/src/st/st-texture-cache.c
@@ -495,16 +495,34 @@ pixbuf_to_st_content_image (GdkPixbuf *pixbuf,
 {
   ClutterContent *image;
   g_autoptr(GError) error = NULL;
+  int pixbuf_width, pixbuf_height;
 
-  if (width < 0)
-    width = ceilf (gdk_pixbuf_get_width (pixbuf) / resource_scale);
-  else
-    width *= paint_scale;
+  pixbuf_width = gdk_pixbuf_get_width (pixbuf);
+  pixbuf_height = gdk_pixbuf_get_height (pixbuf);
+
+  if (width >= 0 || height >= 0)
+    {
+      if (height < 0)
+        {
+          float aspect_scale = (float) pixbuf_height / pixbuf_width;
+
+          height = ceilf (width * aspect_scale);
+        }
+      else if (width < 0)
+        {
+          float aspect_scale = (float) pixbuf_width / pixbuf_height;
+
+          width = ceilf (height * aspect_scale);
+        }
 
-  if (height < 0)
-    height = ceilf (gdk_pixbuf_get_height (pixbuf) / resource_scale);
+      width *= paint_scale;
+      height *= paint_scale;
+    }
   else
-    height *= paint_scale;
+    {
+      width = ceilf (pixbuf_width / resource_scale);
+      height = ceilf (pixbuf_height / resource_scale);
+    }
 
   image = st_image_content_new_with_preferred_size (width, height);
   clutter_image_set_data (CLUTTER_IMAGE (image),


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