summaryrefslogtreecommitdiff
path: root/wallpapers.c
diff options
context:
space:
mode:
Diffstat (limited to 'wallpapers.c')
-rw-r--r--wallpapers.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/wallpapers.c b/wallpapers.c
index 56a72c6..fd893f3 100644
--- a/wallpapers.c
+++ b/wallpapers.c
@@ -118,3 +118,43 @@ void add_dir_recursive(const gchar *path, sqlite_uint64 parent, GtkStatusbar *st
g_free(msg);
gdk_threads_leave();
}
+
+GdkPixbuf *resize_pixbuf(GdkPixbuf *orig, gint win_width, gint win_height, gint *_width, gint *_height, gdouble *ratio) {
+ GdkPixbuf *pb;
+ gint img_width, img_height, width, height;
+ gdouble scalex, scaley, width_ratio, height_ratio;
+
+ img_width = gdk_pixbuf_get_width(orig);
+ img_height = gdk_pixbuf_get_height(orig);
+
+ /* do we need to resize? */
+ if(img_width > win_width || img_height > win_height) {
+ /* resize by width */
+ width_ratio = (gdouble)img_width / (gdouble)win_width;
+ height_ratio = (gdouble)img_height / (gdouble)win_height;
+ if(width_ratio > height_ratio) {
+ width = win_width;
+ height = (gint)(1.0 / width_ratio * img_height);
+ scalex = (gdouble)width / (gdouble)img_width;
+ scaley = (gdouble)height / (gdouble)img_height;
+ } else { /* resize by height */
+ height = win_height;
+ width = (gint)(1.0 / height_ratio * img_width);
+ scalex = (gdouble)width / (gdouble)img_width;
+ scaley = (gdouble)height / (gdouble)img_height;
+ }
+ if(width == 0 || height == 0) return NULL;
+ pb = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
+ gdk_pixbuf_scale(orig, pb, 0, 0, width, height, 0, 0, scalex, scaley, GDK_INTERP_BILINEAR);
+ } else {
+ width = img_width;
+ height = img_height;
+ pb = gdk_pixbuf_copy(orig);
+ }
+
+ *ratio = (gdouble)width / (gdouble)img_width;
+ *_width = width;
+ *_height = height;
+
+ return pb;
+}