Krolyxon
Commit c399c1a

Branches

Refs →

nsxivimage.c

124 lines · 3.0 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_display(img_t *img, win_t *win) {
61 float zw, zh;
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 win_clear(win);
86
87 img_render(img, win, 0, 0, win->w, win->h);
88}
89
90void img_render(img_t *img, win_t *win, int x, int y, int w, int h) {
91 int sx, sy, sw, sh;
92 int dx, dy, dw, dh;
93
94 if (!img || !win || !imlib_context_get_image())
95 return;
96
97 if (img->x < x) {
98 sx = (x - img->x) / img->zoom;
99 sw = MIN(w / img->zoom, img->w - sx);
100 dx = x;
101 dw = sw * img->zoom;
102 } else {
103 sx = 0;
104 sw = MIN((w - img->x + x) / img->zoom, img->w);
105 dx = img->x;
106 dw = sw * img->zoom;
107 }
108 if (img->y < y) {
109 sy = (y - img->y) / img->zoom;
110 sh = MIN(h / img->zoom, img->h - sy);
111 dy = y;
112 dh = sh * img->zoom;
113 } else {
114 sy = 0;
115 sh = MIN((h - img->y + y) / img->zoom, img->h);
116 dy = img->y;
117 dh = sh * img->zoom;
118 }
119
120 if (sw < 0 || sh < 0)
121 return;
122
123 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
124}