Krolyxon
Commit a7e30bb

Branches

Refs →

nsxivimage.c

111 lines · 2.7 KB · C

1/* sxiv: image.c
2 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#include <stdlib.h>
20#include <stdio.h>
21
22#include <Imlib2.h>
23
24#include "sxiv.h"
25#include "image.h"
26
27void imlib_init(win_t *win) {
28 if (!win)
29 return;
30
31 imlib_context_set_display(win->env.dpy);
32 imlib_context_set_visual(win->env.vis);
33 imlib_context_set_colormap(win->env.cmap);
34 imlib_context_set_drawable(win->xwin);
35}
36
37void imlib_destroy() {
38 if (imlib_context_get_image())
39 imlib_free_image();
40}
41
42void img_load(img_t *img, const char *filename) {
43 if (!img || !filename)
44 return;
45
46 if (imlib_context_get_image())
47 imlib_free_image();
48
49 if (!(img->im = imlib_load_image(filename)))
50 DIE("could not open image: %s", filename);
51
52 imlib_context_set_image(img->im);
53
54 img->w = imlib_image_get_width();
55 img->h = imlib_image_get_height();
56}
57
58void img_render(img_t *img, win_t *win) {
59 float zw, zh;
60 unsigned int sx, sy, sw, sh;
61 unsigned int dx, dy, dw, dh;
62
63 if (!img || !win || !imlib_context_get_image())
64 return;
65
66 /* set zoom level to fit image into window */
67 if (img->scalemode != SCALE_ZOOM) {
68 zw = (float) win->w / (float) img->w;
69 zh = (float) win->h / (float) img->h;
70 img->zoom = MIN(zw, zh);
71
72 if (img->zoom * 100.0 < ZOOM_MIN)
73 img->zoom = ZOOM_MIN / 100.0;
74 else if (img->zoom * 100.0 > ZOOM_MAX)
75 img->zoom = ZOOM_MAX / 100.0;
76
77 if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
78 img->zoom = 1.0;
79 }
80
81 /* center image in window */
82 img->x = (win->w - img->w * img->zoom) / 2;
83 img->y = (win->h - img->h * img->zoom) / 2;
84
85 if (img->x < 0) {
86 sx = -img->x / img->zoom;
87 sw = (img->x + win->w) / img->zoom;
88 dx = 0;
89 dw = win->w;
90 } else {
91 sx = 0;
92 sw = img->w;
93 dx = img->x;
94 dw = img->w * img->zoom;
95 }
96 if (img->y < 0) {
97 sy = -img->y / img->zoom;
98 sh = (img->y + win->h) / img->zoom;
99 dy = 0;
100 dh = win->h;
101 } else {
102 sy = 0;
103 sh = img->h;
104 dy = img->y;
105 dh = img->h * img->zoom;
106 }
107
108 win_clear(win);
109
110 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
111}