[Xfce4-commits] <tumbler:master> Avoid huge thumbnails from libjpeg (bug #8020).

Nick Schermer noreply at xfce.org
Thu Oct 27 22:34:01 CEST 2011


Updating branch refs/heads/master
         to d929d6109de877b84a5cf0cf0f4542c44ff00ef1 (commit)
       from 51aa53a5937c8e787ee821b04656324c6fd989b6 (commit)

commit d929d6109de877b84a5cf0cf0f4542c44ff00ef1
Author: Nick Schermer <nick at xfce.org>
Date:   Thu Oct 27 22:29:50 2011 +0200

    Avoid huge thumbnails from libjpeg (bug #8020).
    
    libjpeg only support scaling up to 1/16 of the origional. Quite
    often this is still too big for the thumbnail size, so resize the
    pixbuf before returning it to tumbler.

 plugins/jpeg-thumbnailer/jpeg-thumbnailer.c |   41 +++++++++++++++++++++++++++
 1 files changed, 41 insertions(+), 0 deletions(-)

diff --git a/plugins/jpeg-thumbnailer/jpeg-thumbnailer.c b/plugins/jpeg-thumbnailer/jpeg-thumbnailer.c
index f662c77..7a0a67e 100644
--- a/plugins/jpeg-thumbnailer/jpeg-thumbnailer.c
+++ b/plugins/jpeg-thumbnailer/jpeg-thumbnailer.c
@@ -669,6 +669,42 @@ tvtj_jpeg_load_thumbnail (const JOCTET *content,
 
 
 
+static GdkPixbuf*
+scale_pixbuf (GdkPixbuf *source,
+              gint       dest_width,
+              gint       dest_height)
+{
+  gdouble wratio;
+  gdouble hratio;
+  gint    source_width;
+  gint    source_height;
+
+  /* determine source pixbuf dimensions */
+  source_width  = gdk_pixbuf_get_width  (source);
+  source_height = gdk_pixbuf_get_height (source);
+
+  /* don't do anything if there is no need to resize */
+  if (source_width <= dest_width && source_height <= dest_height)
+    return g_object_ref (source);
+
+  /* determine which axis needs to be scaled down more */
+  wratio = (gdouble) source_width  / (gdouble) dest_width;
+  hratio = (gdouble) source_height / (gdouble) dest_height;
+
+  /* adjust the other axis */
+  if (hratio > wratio)
+    dest_width = rint (source_width / hratio);
+  else
+    dest_height = rint (source_height / wratio);
+
+  /* scale the pixbuf down to the desired size */
+  return gdk_pixbuf_scale_simple (source, MAX (dest_width, 1),
+                                  MAX (dest_height, 1),
+                                  GDK_INTERP_BILINEAR);
+}
+
+
+
 static void
 jpeg_thumbnailer_create (TumblerAbstractThumbnailer *thumbnailer,
                          GCancellable               *cancellable,
@@ -680,6 +716,7 @@ jpeg_thumbnailer_create (TumblerAbstractThumbnailer *thumbnailer,
   struct stat             statb;
   const gchar            *uri;
   GdkPixbuf              *pixbuf = NULL;
+  GdkPixbuf              *scaled;
   gboolean                streaming_needed = TRUE;
   JOCTET                 *content;
   GError                 *error = NULL;
@@ -792,6 +829,10 @@ jpeg_thumbnailer_create (TumblerAbstractThumbnailer *thumbnailer,
 
   if (pixbuf != NULL)
     {
+      scaled = scale_pixbuf (pixbuf, width, height);
+      g_object_unref (pixbuf);
+      pixbuf = scaled;
+
       data.data = gdk_pixbuf_get_pixels (pixbuf);
       data.has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
       data.bits_per_sample = gdk_pixbuf_get_bits_per_sample (pixbuf);


More information about the Xfce4-commits mailing list