summaryrefslogtreecommitdiffstats
path: root/image.c
diff options
context:
space:
mode:
authorBert <ber.t@gmx.com>2011-01-18 20:11:28 +0100
committerBert <ber.t@gmx.com>2011-01-18 20:11:28 +0100
commit79c7e6178e9b577158408157ec23a477556ecf16 (patch)
treeabbefa4cd1b884ca19d396359b4e40dad0057589 /image.c
parentbb4edc756c7ceddb0f13194929cd1d363c85d653 (diff)
downloadnsxiv-79c7e6178e9b577158408157ec23a477556ecf16.tar.zst
Render image on window
Diffstat (limited to 'image.c')
-rw-r--r--image.c61
1 files changed, 58 insertions, 3 deletions
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);
+}