diff options
Diffstat (limited to 'tumbler')
-rw-r--r-- | tumbler/tumbler-util.c | 75 | ||||
-rw-r--r-- | tumbler/tumbler-util.h | 10 |
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__ */ |