summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGaël Bonithon <gael@xfce.org>2021-12-22 12:41:47 +0100
committerGaël Bonithon <gael@xfce.org>2021-12-22 13:06:47 +0100
commit01548638d5a59191076551a9ce1be7f6b1616262 (patch)
treec872e1b201115b574646fbabc0f841b925dd32e9
parent62d0f3e2faaf4cdab3dd9f45eaa03cb9ad4219c6 (diff)
downloadtumbler-01548638d5a59191076551a9ce1be7f6b1616262.tar.gz
Refactoring: Avoid code duplication
-rw-r--r--plugins/cover-thumbnailer/cover-thumbnailer.c46
-rw-r--r--plugins/desktop-thumbnailer/desktop-thumbnailer.c60
-rw-r--r--plugins/ffmpeg-thumbnailer/ffmpeg-thumbnailer.c39
-rw-r--r--plugins/font-thumbnailer/font-thumbnailer.c35
-rw-r--r--plugins/gepub-thumbnailer/gepub-thumbnailer.c49
-rw-r--r--plugins/gst-thumbnailer/gst-thumbnailer.c39
-rw-r--r--plugins/jpeg-thumbnailer/jpeg-thumbnailer.c40
-rw-r--r--plugins/odf-thumbnailer/odf-thumbnailer.c47
-rw-r--r--plugins/pixbuf-thumbnailer/pixbuf-thumbnailer.c47
-rw-r--r--plugins/poppler-thumbnailer/poppler-thumbnailer.c46
-rw-r--r--plugins/raw-thumbnailer/raw-thumbnailer.c39
-rw-r--r--tumbler/tumbler-util.c75
-rw-r--r--tumbler/tumbler-util.h10
13 files changed, 106 insertions, 466 deletions
diff --git a/plugins/cover-thumbnailer/cover-thumbnailer.c b/plugins/cover-thumbnailer/cover-thumbnailer.c
index a072f19..076ad7b 100644
--- a/plugins/cover-thumbnailer/cover-thumbnailer.c
+++ b/plugins/cover-thumbnailer/cover-thumbnailer.c
@@ -28,8 +28,6 @@
#include <sys/select.h>
#endif
-#include <math.h>
-
#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>
@@ -160,48 +158,6 @@ cover_thumbnailer_finalize (GObject *object)
-static void
-cover_thumbnailer_size_prepared (GdkPixbufLoader *loader,
- gint source_width,
- gint source_height,
- TumblerThumbnailFlavor *flavor)
-{
-
- gint dest_width;
- gint dest_height;
- gdouble hratio;
- gdouble wratio;
-
- g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
- g_return_if_fail (TUMBLER_IS_THUMBNAIL_FLAVOR (flavor));
-
- /* get the destination size */
- tumbler_thumbnail_flavor_get_size (flavor, &dest_width, &dest_height);
-
- if (source_width <= dest_width && source_height <= dest_height)
- {
- /* do not scale the image */
- dest_width = source_width;
- dest_height = source_height;
- }
- else
- {
- /* 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);
- }
-
- gdk_pixbuf_loader_set_size (loader, MAX (dest_width, 1), MAX (dest_height, 1));
-}
-
-
-
static size_t
cover_thumbnailer_load_pixbuf_write (gpointer data,
size_t size,
@@ -369,7 +325,7 @@ cover_thumbnailer_load_pixbuf (CoverThumbnailer *cover,
/* create a pixbuf loader */
loader = gdk_pixbuf_loader_new ();
g_signal_connect (loader, "size-prepared",
- G_CALLBACK (cover_thumbnailer_size_prepared), flavor);
+ G_CALLBACK (thumbler_util_size_prepared), flavor);
/* download the image into a pixbuf loader */
curl_handle = cover_thumbnailer_load_prepare (cover, url, cancellable);
diff --git a/plugins/desktop-thumbnailer/desktop-thumbnailer.c b/plugins/desktop-thumbnailer/desktop-thumbnailer.c
index 916d5bc..1662e5b 100644
--- a/plugins/desktop-thumbnailer/desktop-thumbnailer.c
+++ b/plugins/desktop-thumbnailer/desktop-thumbnailer.c
@@ -22,8 +22,6 @@
#include <config.h>
#endif
-#include <math.h>
-
#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>
@@ -196,49 +194,6 @@ te_string_append_quoted (GString *string,
-static GdkPixbuf *
-desktop_thumbnailer_get_pixbuf (GInputStream *stream,
- int dest_width,
- int dest_height,
- GCancellable *cancellable,
- GError **error)
-{
- GdkPixbuf *source;
- gdouble hratio;
- gdouble wratio;
- gint source_width;
- gint source_height;
-
- source = gdk_pixbuf_new_from_stream (stream, cancellable, error);
- if (source == NULL)
- return NULL;
-
- /* determine the source pixbuf dimensions */
- source_width = gdk_pixbuf_get_width (source);
- source_height = gdk_pixbuf_get_height (source);
-
- /* return the same pixbuf if no scaling is required */
- if (source_width <= dest_width && source_height <= dest_height)
- return 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 gboolean
desktop_thumbnailer_exec_parse (const gchar *exec,
const gchar *file_path,
@@ -315,7 +270,7 @@ desktop_thumbnailer_load_thumbnail (DesktopThumbnailer *thumbnailer,
gint cmd_argc;
gint size;
gchar *working_directory = NULL;
- GdkPixbuf *pixbuf = NULL;
+ GdkPixbuf *source, *pixbuf = NULL;
g_object_get (G_OBJECT (thumbnailer), "exec", &exec, NULL);
@@ -350,11 +305,14 @@ desktop_thumbnailer_load_thumbnail (DesktopThumbnailer *thumbnailer,
if (G_LIKELY (res))
{
- pixbuf = desktop_thumbnailer_get_pixbuf (g_io_stream_get_input_stream(G_IO_STREAM(stream)),
- width,
- height,
- cancellable,
- error);
+ source = gdk_pixbuf_new_from_stream (g_io_stream_get_input_stream (G_IO_STREAM (stream)),
+ cancellable, error);
+ if (source != NULL)
+ {
+ pixbuf = thumbler_util_scale_pixbuf (source, width, height);
+ g_object_unref (source);
+ }
+
g_unlink (tmpfilepath);
}
diff --git a/plugins/ffmpeg-thumbnailer/ffmpeg-thumbnailer.c b/plugins/ffmpeg-thumbnailer/ffmpeg-thumbnailer.c
index fb925c5..e8ef659 100644
--- a/plugins/ffmpeg-thumbnailer/ffmpeg-thumbnailer.c
+++ b/plugins/ffmpeg-thumbnailer/ffmpeg-thumbnailer.c
@@ -23,8 +23,6 @@
#include <config.h>
#endif
-#include <math.h>
-
#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>
@@ -126,41 +124,6 @@ ffmpeg_thumbnailer_finalize (GObject *object)
-static GdkPixbuf *
-generate_pixbuf (GdkPixbuf *source,
- gint dest_width,
- gint dest_height)
-{
- gdouble hratio;
- gdouble wratio;
- gint source_width;
- gint source_height;
-
- /* determine the source pixbuf dimensions */
- source_width = gdk_pixbuf_get_width (source);
- source_height = gdk_pixbuf_get_height (source);
-
- /* return the same pixbuf if no scaling is required */
- 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
ffmpeg_thumbnailer_create (TumblerAbstractThumbnailer *thumbnailer,
GCancellable *cancellable,
@@ -291,7 +254,7 @@ ffmpeg_thumbnailer_create (TumblerAbstractThumbnailer *thumbnailer,
}
/* generate a valid thumbnail */
- pixbuf = generate_pixbuf (v_pixbuf, dest_width, dest_height);
+ pixbuf = thumbler_util_scale_pixbuf (v_pixbuf, dest_width, dest_height);
g_assert (pixbuf != NULL);
diff --git a/plugins/font-thumbnailer/font-thumbnailer.c b/plugins/font-thumbnailer/font-thumbnailer.c
index 04b3bcc..75a139e 100644
--- a/plugins/font-thumbnailer/font-thumbnailer.c
+++ b/plugins/font-thumbnailer/font-thumbnailer.c
@@ -23,8 +23,6 @@
#include <config.h>
#endif
-#include <math.h>
-
#include <ft2build.h>
#include FT_FREETYPE_H
@@ -214,37 +212,6 @@ render_glyph (GdkPixbuf *pixbuf,
-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);
-
- /* 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 GdkPixbuf *
trim_and_scale_pixbuf (GdkPixbuf *pixbuf,
gint dest_width,
@@ -358,7 +325,7 @@ trim_and_scale_pixbuf (GdkPixbuf *pixbuf,
if (gdk_pixbuf_get_width (subpixbuf) > dest_width
|| gdk_pixbuf_get_height (subpixbuf) > dest_height)
{
- scaled = scale_pixbuf (subpixbuf, dest_width, dest_height);
+ scaled = thumbler_util_scale_pixbuf (subpixbuf, dest_width, dest_height);
g_object_unref (G_OBJECT (subpixbuf));
subpixbuf = scaled;
}
diff --git a/plugins/gepub-thumbnailer/gepub-thumbnailer.c b/plugins/gepub-thumbnailer/gepub-thumbnailer.c
index 00f56c6..9d3fd84 100644
--- a/plugins/gepub-thumbnailer/gepub-thumbnailer.c
+++ b/plugins/gepub-thumbnailer/gepub-thumbnailer.c
@@ -23,8 +23,6 @@
#include <config.h>
#endif
-#include <math.h>
-
#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gprintf.h>
@@ -94,50 +92,6 @@ gepub_thumbnailer_init (GepubThumbnailer *thumbnailer)
-static void
-gepub_thumbnailer_size_prepared (GdkPixbufLoader *loader,
- gint source_width,
- gint source_height,
- TumblerThumbnail *thumbnail)
-{
- TumblerThumbnailFlavor *flavor;
- gint dest_width;
- gint dest_height;
- gdouble hratio;
- gdouble wratio;
-
- g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
- g_return_if_fail (TUMBLER_IS_THUMBNAIL (thumbnail));
-
- flavor = tumbler_thumbnail_get_flavor (thumbnail);
- tumbler_thumbnail_flavor_get_size (flavor, &dest_width, &dest_height);
- g_object_unref (flavor);
-
- if (source_width <= dest_width && source_height <= dest_height)
- {
- /* do not scale the image */
- dest_width = source_width;
- dest_height = source_height;
- }
- else
- {
- /* 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);
- }
-
- gdk_pixbuf_loader_set_size (loader, MAX (dest_width, 1),
- MAX (dest_height, 1));
-}
-
-
-
static GdkPixbuf *
gepub_thumbnailer_create_from_mime (gchar *mime_type,
GBytes *content,
@@ -158,8 +112,7 @@ gepub_thumbnailer_create_from_mime (gchar *mime_type,
{
loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, &err);
g_signal_connect (loader, "size-prepared",
- G_CALLBACK (gepub_thumbnailer_size_prepared),
- thumbnail);
+ G_CALLBACK (thumbler_util_size_prepared), thumbnail);
if (gdk_pixbuf_loader_write_bytes (loader, content, &err))
{
if (gdk_pixbuf_loader_close (loader, &err))
diff --git a/plugins/gst-thumbnailer/gst-thumbnailer.c b/plugins/gst-thumbnailer/gst-thumbnailer.c
index 3ca6d6a..d10048a 100644
--- a/plugins/gst-thumbnailer/gst-thumbnailer.c
+++ b/plugins/gst-thumbnailer/gst-thumbnailer.c
@@ -26,7 +26,6 @@
#endif
#include <string.h>
-#include <math.h>
#include <glib.h>
#include <glib/gi18n.h>
@@ -516,42 +515,6 @@ gst_thumbnailer_play_init (TumblerFileInfo *info)
-static GdkPixbuf *
-gst_thumbnailer_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
gst_thumbnailer_create (TumblerAbstractThumbnailer *thumbnailer,
GCancellable *cancellable,
@@ -622,7 +585,7 @@ gst_thumbnailer_create (TumblerAbstractThumbnailer *thumbnailer,
if (G_LIKELY (pixbuf != NULL))
{
/* scale to correct size if required */
- scaled = gst_thumbnailer_scale_pixbuf (pixbuf, width, height);
+ scaled = thumbler_util_scale_pixbuf (pixbuf, width, height);
g_object_unref (pixbuf);
pixbuf = scaled;
diff --git a/plugins/jpeg-thumbnailer/jpeg-thumbnailer.c b/plugins/jpeg-thumbnailer/jpeg-thumbnailer.c
index e01ed28..9aa912f 100644
--- a/plugins/jpeg-thumbnailer/jpeg-thumbnailer.c
+++ b/plugins/jpeg-thumbnailer/jpeg-thumbnailer.c
@@ -58,8 +58,6 @@
#include <unistd.h>
#endif
-#include <math.h>
-
#include <jpeglib.h>
#include <glib.h>
@@ -772,42 +770,6 @@ 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,
@@ -949,7 +911,7 @@ jpeg_thumbnailer_create (TumblerAbstractThumbnailer *thumbnailer,
if (pixbuf != NULL)
{
- scaled = scale_pixbuf (pixbuf, width, height);
+ scaled = thumbler_util_scale_pixbuf (pixbuf, width, height);
g_object_unref (pixbuf);
pixbuf = scaled;
diff --git a/plugins/odf-thumbnailer/odf-thumbnailer.c b/plugins/odf-thumbnailer/odf-thumbnailer.c
index 682551a..0672de3 100644
--- a/plugins/odf-thumbnailer/odf-thumbnailer.c
+++ b/plugins/odf-thumbnailer/odf-thumbnailer.c
@@ -26,8 +26,6 @@
#include <config.h>
#endif
-#include <math.h>
-
#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>
@@ -112,49 +110,6 @@ odf_thumbnailer_init (OdfThumbnailer *thumbnailer)
-static void
-odf_thumbnailer_size_prepared (GdkPixbufLoader *loader,
- gint source_width,
- gint source_height,
- TumblerThumbnail *thumbnail)
-{
- TumblerThumbnailFlavor *flavor;
- gint dest_width;
- gint dest_height;
- gdouble hratio;
- gdouble wratio;
-
- g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
- g_return_if_fail (TUMBLER_IS_THUMBNAIL (thumbnail));
-
- flavor = tumbler_thumbnail_get_flavor (thumbnail);
- tumbler_thumbnail_flavor_get_size (flavor, &dest_width, &dest_height);
- g_object_unref (flavor);
-
- if (source_width <= dest_width && source_height <= dest_height)
- {
- /* do not scale the image */
- dest_width = source_width;
- dest_height = source_height;
- }
- else
- {
- /* 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);
- }
-
- gdk_pixbuf_loader_set_size (loader, MAX (dest_width, 1), MAX (dest_height, 1));
-}
-
-
-
static GdkPixbuf *
odf_thumbnailer_create_from_data (const guchar *data,
gsize bytes,
@@ -170,7 +125,7 @@ odf_thumbnailer_create_from_data (const guchar *data,
loader = gdk_pixbuf_loader_new ();
g_signal_connect (loader, "size-prepared",
- G_CALLBACK (odf_thumbnailer_size_prepared), thumbnail);
+ G_CALLBACK (thumbler_util_size_prepared), thumbnail);
if (gdk_pixbuf_loader_write (loader, data, bytes, &err))
{
if (gdk_pixbuf_loader_close (loader, &err))
diff --git a/plugins/pixbuf-thumbnailer/pixbuf-thumbnailer.c b/plugins/pixbuf-thumbnailer/pixbuf-thumbnailer.c
index 7c01a63..813b069 100644
--- a/plugins/pixbuf-thumbnailer/pixbuf-thumbnailer.c
+++ b/plugins/pixbuf-thumbnailer/pixbuf-thumbnailer.c
@@ -22,8 +22,6 @@
#include <config.h>
#endif
-#include <math.h>
-
#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>
@@ -107,49 +105,6 @@ pixbuf_thumbnailer_init (PixbufThumbnailer *thumbnailer)
-static void
-pixbuf_thumbnailer_size_prepared (GdkPixbufLoader *loader,
- gint source_width,
- gint source_height,
- TumblerThumbnail *thumbnail)
-{
- TumblerThumbnailFlavor *flavor;
- gint dest_width;
- gint dest_height;
- gdouble hratio;
- gdouble wratio;
-
- g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
- g_return_if_fail (TUMBLER_IS_THUMBNAIL (thumbnail));
-
- flavor = tumbler_thumbnail_get_flavor (thumbnail);
- tumbler_thumbnail_flavor_get_size (flavor, &dest_width, &dest_height);
- g_object_unref (flavor);
-
- if (source_width <= dest_width && source_height <= dest_height)
- {
- /* do not scale the image */
- dest_width = source_width;
- dest_height = source_height;
- }
- else
- {
- /* 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);
- }
-
- gdk_pixbuf_loader_set_size (loader, MAX (dest_width, 1), MAX (dest_height, 1));
-}
-
-
-
static GdkPixbuf *
pixbuf_thumbnailer_new_from_stream (GInputStream *stream,
TumblerThumbnail *thumbnail,
@@ -176,7 +131,7 @@ pixbuf_thumbnailer_new_from_stream (GInputStream *stream,
loader = gdk_pixbuf_loader_new ();
g_signal_connect (loader, "size-prepared",
- G_CALLBACK (pixbuf_thumbnailer_size_prepared), thumbnail);
+ G_CALLBACK (thumbler_util_size_prepared), thumbnail);
buffer = g_new (guchar, LOADER_BUFFER_SIZE);
for (;;)
diff --git a/plugins/poppler-thumbnailer/poppler-thumbnailer.c b/plugins/poppler-thumbnailer/poppler-thumbnailer.c
index 6fa1c1a..0dffe75 100644
--- a/plugins/poppler-thumbnailer/poppler-thumbnailer.c
+++ b/plugins/poppler-thumbnailer/poppler-thumbnailer.c
@@ -22,8 +22,6 @@
#include <config.h>
#endif
-#include <math.h>
-
#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>
@@ -204,46 +202,6 @@ poppler_thumbnailer_pixbuf_from_page (PopplerPage *page)
-static GdkPixbuf *
-generate_pixbuf (GdkPixbuf *source,
- TumblerThumbnailFlavor *flavor)
-{
- gdouble hratio;
- gdouble wratio;
- gint dest_width;
- gint dest_height;
- gint source_width;
- gint source_height;
-
- /* determine the source pixbuf dimensions */
- source_width = gdk_pixbuf_get_width (source);
- source_height = gdk_pixbuf_get_height (source);
-
- /* determine the desired size for this flavor */
- tumbler_thumbnail_flavor_get_size (flavor, &dest_width, &dest_height);
-
- /* return the same pixbuf if no scaling is required */
- 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);
-}
-
-
-
/* to be removed when Poppler version >= 0.82: GBytes takes care of that */
static void
poppler_thumbnailer_free (gpointer data)
@@ -276,6 +234,7 @@ poppler_thumbnailer_create (TumblerAbstractThumbnailer *thumbnailer,
#endif
gchar *contents = NULL;
gsize length;
+ gint width, height;
g_return_if_fail (IS_POPPLER_THUMBNAILER (thumbnailer));
g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
@@ -374,7 +333,8 @@ poppler_thumbnailer_create (TumblerAbstractThumbnailer *thumbnailer,
g_object_unref (document);
/* generate the final pixbuf (involves rescaling etc.) */
- pixbuf = generate_pixbuf (source_pixbuf, flavor);
+ tumbler_thumbnail_flavor_get_size (flavor, &width, &height);
+ pixbuf = thumbler_util_scale_pixbuf (source_pixbuf, width, height);
g_object_unref (flavor);
g_assert (pixbuf != NULL);
diff --git a/plugins/raw-thumbnailer/raw-thumbnailer.c b/plugins/raw-thumbnailer/raw-thumbnailer.c
index ecf8f16..808f454 100644
--- a/plugins/raw-thumbnailer/raw-thumbnailer.c
+++ b/plugins/raw-thumbnailer/raw-thumbnailer.c
@@ -30,7 +30,6 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <math.h>
#include <glib.h>
#include <glib/gi18n.h>
@@ -100,42 +99,6 @@ raw_thumbnailer_init (RawThumbnailer *thumbnailer)
-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
raw_thumbnailer_create (TumblerAbstractThumbnailer *thumbnailer,
GCancellable *cancellable,
@@ -196,7 +159,7 @@ raw_thumbnailer_create (TumblerAbstractThumbnailer *thumbnailer,
if (pixbuf != NULL)
{
- scaled = scale_pixbuf (pixbuf, width, height);
+ scaled = thumbler_util_scale_pixbuf (pixbuf, width, height);
g_object_unref (pixbuf);
pixbuf = scaled;
diff --git a/tumbler/tumbler-util.c b/tumbler/tumbler-util.c
index a414e26..f98eea8 100644
--- a/tumbler/tumbler-util.c
+++ b/tumbler/tumbler-util.c
@@ -26,6 +26,8 @@
#include <string.h>
#endif
+#include <math.h>
+
#include <glib.h>
#include <gio/gio.h>
@@ -165,3 +167,76 @@ gboolean tumbler_util_guess_is_sparse (TumblerFileInfo *info)
return ret_val;
}
+
+
+void
+thumbler_util_size_prepared (GdkPixbufLoader *loader,
+ gint source_width,
+ gint source_height,
+ TumblerThumbnailFlavor *flavor)
+{
+ gdouble hratio, wratio;
+ gint dest_width, dest_height;
+
+ g_return_if_fail (GDK_IS_PIXBUF_LOADER (loader));
+ g_return_if_fail (TUMBLER_IS_THUMBNAIL_FLAVOR (flavor));
+
+ /* get the destination size */
+ tumbler_thumbnail_flavor_get_size (flavor, &dest_width, &dest_height);
+
+ if (source_width <= dest_width && source_height <= dest_height)
+ {
+ /* do not scale the image */
+ dest_width = source_width;
+ dest_height = source_height;
+ }
+ else
+ {
+ /* 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);
+ }
+
+ gdk_pixbuf_loader_set_size (loader, MAX (dest_width, 1), MAX (dest_height, 1));
+}
+
+
+
+GdkPixbuf *
+thumbler_util_scale_pixbuf (GdkPixbuf *source,
+ gint dest_width,
+ gint dest_height)
+{
+ gdouble hratio, wratio;
+ gint source_width, source_height;
+
+ g_return_val_if_fail (GDK_IS_PIXBUF (source), NULL);
+
+ /* determine the source pixbuf dimensions */
+ source_width = gdk_pixbuf_get_width (source);
+ source_height = gdk_pixbuf_get_height (source);
+
+ /* return the same pixbuf if no scaling is required */
+ 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);
+}
diff --git a/tumbler/tumbler-util.h b/tumbler/tumbler-util.h
index 809332e..1299225 100644
--- a/tumbler/tumbler-util.h
+++ b/tumbler/tumbler-util.h
@@ -22,6 +22,7 @@
#define __TUMBLER_UTIL_H__
#include <glib.h>
+#include <gdk-pixbuf/gdk-pixbuf.h>
#include <tumbler/tumbler-file-info.h>
@@ -33,6 +34,15 @@ GKeyFile *tumbler_util_get_settings (void) G_GNUC_MALLOC;
gboolean tumbler_util_guess_is_sparse (TumblerFileInfo *info);
+void thumbler_util_size_prepared (GdkPixbufLoader *loader,
+ gint source_width,
+ gint source_height,
+ TumblerThumbnailFlavor *flavor);
+
+GdkPixbuf *thumbler_util_scale_pixbuf (GdkPixbuf *source,
+ gint dest_width,
+ gint dest_height);
+
G_END_DECLS
#endif /* !__TUMBLER_UTIL_H__ */