Krolyxon
Commit 6851d5c

Branches

Refs →

nsxivimage.c

113 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 Imlib_Image *im;
44
45 if (!img || !filename)
46 return;
47
48 if (imlib_context_get_image())
49 imlib_free_image();
50
51 if (!(im = imlib_load_image(filename)))
52 DIE("could not open image: %s", filename);
53
54 imlib_context_set_image(im);
55
56 img->w = imlib_image_get_width();
57 img->h = imlib_image_get_height();
58}
59
60void img_render(img_t *img, win_t *win) {
61 float zw, zh;
62 unsigned int sx, sy, sw, sh;
63 unsigned int dx, dy, dw, dh;
64
65 if (!img || !win || !imlib_context_get_image())
66 return;
67
68 /* set zoom level to fit image into window */
69 if (img->scalemode != SCALE_ZOOM) {
70 zw = (float) win->w / (float) img->w;
71 zh = (float) win->h / (float) img->h;
72 img->zoom = MIN(zw, zh);
73
74 if (img->zoom * 100.0 < ZOOM_MIN)
75 img->zoom = ZOOM_MIN / 100.0;
76 else if (img->zoom * 100.0 > ZOOM_MAX)
77 img->zoom = ZOOM_MAX / 100.0;
78
79 if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
80 img->zoom = 1.0;
81 }
82
83 /* center image in window */
84 img->x = (win->w - img->w * img->zoom) / 2;
85 img->y = (win->h - img->h * img->zoom) / 2;
86
87 if (img->x < 0) {
88 sx = -img->x / img->zoom;
89 sw = (img->x + win->w) / img->zoom;
90 dx = 0;
91 dw = win->w;
92 } else {
93 sx = 0;
94 sw = img->w;
95 dx = img->x;
96 dw = img->w * img->zoom;
97 }
98 if (img->y < 0) {
99 sy = -img->y / img->zoom;
100 sh = (img->y + win->h) / img->zoom;
101 dy = 0;
102 dh = win->h;
103 } else {
104 sy = 0;
105 sh = img->h;
106 dy = img->y;
107 dh = img->h * img->zoom;
108 }
109
110 win_clear(win);
111
112 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
113}