From 79c7e6178e9b577158408157ec23a477556ecf16 Mon Sep 17 00:00:00 2001 From: Bert Date: Tue, 18 Jan 2011 20:11:28 +0100 Subject: Render image on window --- image.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 3 deletions(-) (limited to 'image.c') diff --git a/image.c b/image.c index a430b81..235b2d4 100644 --- a/image.c +++ b/image.c @@ -25,7 +25,7 @@ void imlib_init(win_t *win) { if (!win) return; - + imlib_context_set_display(win->env.dpy); imlib_context_set_visual(win->env.vis); imlib_context_set_colormap(win->env.cmap); @@ -41,9 +41,64 @@ void img_load(img_t *img, char *filename) { if (!(img->im = imlib_load_image(filename))) DIE("could not open image: %s", filename); - + imlib_context_set_image(img->im); - + img->w = imlib_image_get_width(); img->h = imlib_image_get_height(); } + +void img_render(img_t *img, win_t *win) { + float zw, zh; + unsigned int sx, sy, sw, sh; + unsigned int dx, dy, dw, dh; + + if (!img || !win) + 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 * 100.0 < ZOOM_MIN) + img->zoom = ZOOM_MIN / 100.0; + else if (img->zoom * 100.0 > ZOOM_MAX) + img->zoom = ZOOM_MAX / 100.0; + + 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; + + if (img->x < 0) { + sx = -img->x / img->zoom; + sw = (img->x + win->w) / img->zoom; + dx = 0; + dw = win->w; + } else { + sx = 0; + sw = img->w; + dx = img->x; + dw = img->w * img->zoom; + } + if (img->y < 0) { + sy = -img->y / img->zoom; + sh = (img->y + win->h) / img->zoom; + dy = 0; + dh = win->h; + } else { + sy = 0; + sh = img->h; + dy = img->y; + dh = img->h * img->zoom; + } + + win_clear(win); + + imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh); +} -- cgit v1.2.3