summaryrefslogtreecommitdiff
path: root/tumbler
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 /tumbler
parent62d0f3e2faaf4cdab3dd9f45eaa03cb9ad4219c6 (diff)
downloadtumbler-01548638d5a59191076551a9ce1be7f6b1616262.tar.gz
Refactoring: Avoid code duplication
Diffstat (limited to 'tumbler')
-rw-r--r--tumbler/tumbler-util.c75
-rw-r--r--tumbler/tumbler-util.h10
2 files changed, 85 insertions, 0 deletions
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__ */