From 629d37376d1c9b175a525e8c30f8d468996a3bd6 Mon Sep 17 00:00:00 2001 From: Bert Date: Fri, 21 Jan 2011 12:13:52 +0100 Subject: Merged img_display() into img_render() --- image.c | 57 ++++++++++++++++++++++++++++----------------------------- image.h | 2 +- main.c | 6 +++--- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/image.c b/image.c index 688e83e..b6c10bf 100644 --- a/image.c +++ b/image.c @@ -62,40 +62,13 @@ int img_load(img_t *img, const char *filename) { imlib_context_set_image(im); + img->re = 0; img->w = imlib_image_get_width(); img->h = imlib_image_get_height(); return 0; } -void img_display(img_t *img, win_t *win) { - float zw, zh; - - if (!img || !win || !imlib_context_get_image()) - return; - - /* set zoom level to fit image into window */ - if (img->scalemode != SCALE_ZOOM) { - zw = (float) win->w / (float) img->w; - zh = (float) win->h / (float) img->h; - img->zoom = MIN(zw, zh); - - if (img->zoom < zoom_min) - img->zoom = zoom_min; - else if (img->zoom > zoom_max) - img->zoom = zoom_max; - - if (img->scalemode == SCALE_DOWN && img->zoom > 1.0) - img->zoom = 1.0; - } - - /* center image in window */ - img->x = (win->w - img->w * img->zoom) / 2; - img->y = (win->h - img->h * img->zoom) / 2; - - img_render(img, win); -} - void img_check_pan(img_t *img, win_t *win) { if (!img) return; @@ -121,11 +94,37 @@ void img_check_pan(img_t *img, win_t *win) { void img_render(img_t *img, win_t *win) { int sx, sy, sw, sh; int dx, dy, dw, dh; + float zw, zh; if (!img || !win || !imlib_context_get_image()) return; - img_check_pan(img, win); + if (!img->re) { + /* rendered for the first time */ + img->re = 1; + + /* set zoom level to fit image into window */ + if (img->scalemode != SCALE_ZOOM) { + zw = (float) win->w / (float) img->w; + zh = (float) win->h / (float) img->h; + img->zoom = MIN(zw, zh); + + if (img->zoom < zoom_min) + img->zoom = zoom_min; + else if (img->zoom > zoom_max) + img->zoom = zoom_max; + + if (img->scalemode == SCALE_DOWN && img->zoom > 1.0) + img->zoom = 1.0; + } + + /* center image in window */ + img->x = (win->w - img->w * img->zoom) / 2; + img->y = (win->h - img->h * img->zoom) / 2; + } else { + /* typically after zooming and panning */ + img_check_pan(img, win); + } if (img->x < 0) { sx = -img->x / img->zoom; diff --git a/image.h b/image.h index 659f3b9..091e834 100644 --- a/image.h +++ b/image.h @@ -30,6 +30,7 @@ typedef enum scalemode_e { typedef struct img_s { float zoom; scalemode_t scalemode; + unsigned char re; int x; int y; int w; @@ -40,7 +41,6 @@ void imlib_init(win_t*); void imlib_destroy(); int img_load(img_t*, const char*); -void img_display(img_t*, win_t*); void img_render(img_t*, win_t*); int img_zoom_in(img_t*); diff --git a/main.c b/main.c index 5d9f7a1..e9a2970 100644 --- a/main.c +++ b/main.c @@ -93,7 +93,7 @@ int main(int argc, char **argv) { imlib_init(&win); img_load(&img, filenames[fileidx]); - img_display(&img, &win); + img_render(&img, &win); update_title(); run(); @@ -146,14 +146,14 @@ void on_keypress(XEvent *ev) { case 'n': if (fileidx + 1 < filecnt) { img_load(&img, filenames[++fileidx]); - img_display(&img, &win); + img_render(&img, &win); update_title(); } break; case 'p': if (fileidx > 0) { img_load(&img, filenames[--fileidx]); - img_display(&img, &win); + img_render(&img, &win); update_title(); } break; -- cgit v1.2.3