From 28e58ba0dbf30a4a28465f460c67e1c4337bd3a4 Mon Sep 17 00:00:00 2001 From: nirav Date: Mon, 3 Sep 2018 23:38:29 +0530 Subject: Fixed zoom --- image.c | 47 +++++++++++++++++++++++++---------------------- image.h | 4 ++-- main.c | 2 +- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/image.c b/image.c index 034dd62..5489259 100644 --- a/image.c +++ b/image.c @@ -1,7 +1,7 @@ #include "image.h" #include -GtkWidget* new_image() +GtkWidget *new_image() { image = gtk_image_new(); return image; @@ -16,8 +16,9 @@ int load_image(char *file_name, int win_width, int win_height) pixbuf = gdk_pixbuf_new_from_file(file_name, &error); if (error) return -1; - pixbuf_width = gdk_pixbuf_get_width(GDK_PIXBUF(pixbuf)); - pixbuf_height = gdk_pixbuf_get_height(GDK_PIXBUF(pixbuf)); + curr_pixbuf_width = pixbuf_width = gdk_pixbuf_get_width(GDK_PIXBUF(pixbuf)); + curr_pixbuf_height = pixbuf_height = + gdk_pixbuf_get_height(GDK_PIXBUF(pixbuf)); aspect_ratio = (double)pixbuf_width / (double)pixbuf_height; curr_zoom = 1.0; fit_image(win_width, win_height); @@ -30,8 +31,9 @@ void update_pixbuf() g_object_unref(curr_pixbuf); if (pixbuf == NULL) return; - curr_pixbuf = gdk_pixbuf_scale_simple(GDK_PIXBUF(pixbuf), pixbuf_width, - pixbuf_height, GDK_INTERP_BILINEAR); + curr_pixbuf = + gdk_pixbuf_scale_simple(GDK_PIXBUF(pixbuf), curr_pixbuf_width, + curr_pixbuf_height, GDK_INTERP_BILINEAR); gtk_image_set_from_pixbuf(GTK_IMAGE(image), GDK_PIXBUF(curr_pixbuf)); } @@ -40,26 +42,26 @@ void fit_image(int win_width, int win_height) if (pixbuf == NULL || win_width < 1 || win_height < 1) return; if (win_width < pixbuf_width && win_height > pixbuf_height) { - pixbuf_width = win_width; - pixbuf_height = (double)pixbuf_width / aspect_ratio; + curr_pixbuf_width = win_width; + curr_pixbuf_height = (double)curr_pixbuf_width / aspect_ratio; } else if (win_width > pixbuf_width && win_height < pixbuf_height) { - pixbuf_height = win_height; - pixbuf_width = (double)pixbuf_height * aspect_ratio; + curr_pixbuf_height = win_height; + curr_pixbuf_width = (double)curr_pixbuf_height * aspect_ratio; } else if (win_width < pixbuf_width && win_height < pixbuf_height) { if (((double)win_width / (double)win_height) > aspect_ratio) { - pixbuf_height = win_height; - pixbuf_width = ((double)pixbuf_height * aspect_ratio); + curr_pixbuf_height = win_height; + curr_pixbuf_width = ((double)curr_pixbuf_height * aspect_ratio); } else { - pixbuf_width = win_width; - pixbuf_height = (double)pixbuf_width / aspect_ratio; + curr_pixbuf_width = win_width; + curr_pixbuf_height = (double)curr_pixbuf_width / aspect_ratio; } } else { - pixbuf_width = pixbuf_width; - pixbuf_height = pixbuf_height; + curr_pixbuf_width = pixbuf_width; + curr_pixbuf_height = pixbuf_height; } - curr_zoom = (double)pixbuf_width / (double)pixbuf_width; - if (pixbuf_width < 1 || pixbuf_height < 1) + curr_zoom = (double)curr_pixbuf_width / (double)pixbuf_width; + if (curr_pixbuf_width < 1 || curr_pixbuf_height < 1) return; update_pixbuf(); @@ -72,18 +74,19 @@ void zoom(int type) if (type == 0) { if (curr_zoom == 1.0) return; - curr_zoom = (double)1.0; + curr_zoom = 1.0; } else if (type < 0) { if (curr_zoom < 0.2) return; - curr_zoom -= (double)ZOOM_FACTOR; + curr_zoom -= 0.1; } else if (type > 0) { if (curr_zoom > 2) return; - curr_zoom += (double)ZOOM_FACTOR; + curr_zoom += 0.1; } - pixbuf_width *= curr_zoom; - pixbuf_height += curr_zoom; + + curr_pixbuf_width = curr_zoom * (double)pixbuf_width; + curr_pixbuf_height = curr_zoom * (double)pixbuf_height; update_pixbuf(); } diff --git a/image.h b/image.h index d82fd74..3771790 100644 --- a/image.h +++ b/image.h @@ -13,11 +13,11 @@ enum scale_mode { GtkWidget *image; GdkPixbuf *pixbuf; GdkPixbuf *curr_pixbuf; -int pixbuf_width, pixbuf_height; +int pixbuf_width, pixbuf_height, curr_pixbuf_width, curr_pixbuf_height; double aspect_ratio; double curr_zoom; -GtkWidget* new_image(); +GtkWidget *new_image(); int load_image(char *file_name, int win_width, int win_height); void fit_image(int win_width, int win_height); void zoom(int type); diff --git a/main.c b/main.c index c1a9eaa..d3b25f6 100644 --- a/main.c +++ b/main.c @@ -102,7 +102,7 @@ gboolean scroll_callback(GtkWindow *window, GdkEvent *event, gpointer data) break; case GDK_SCROLL_DOWN: if (state & GDK_CONTROL_MASK) { - zoom(1); + zoom(-1); curr_scale_mod = zoomed; } else { prev(); -- cgit v1.2.3