Krolyxon
Commit 029e120

Branches

Refs →

nsxivimage.c

127 lines · 2.8 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}
35
36void imlib_destroy() {
37 if (imlib_context_get_image())
38 imlib_free_image();
39}
40
41int img_load(img_t *img, const char *filename) {
42 Imlib_Image *im;
43
44 if (!img || !filename)
45 return -1;
46
47 if (imlib_context_get_image())
48 imlib_free_image();
49
50 if (!(im = imlib_load_image(filename))) {
51 WARN("could not open image: %s", filename);
52 return -1;
53 }
54
55 imlib_context_set_image(im);
56
57 img->w = imlib_image_get_width();
58 img->h = imlib_image_get_height();
59
60 return 0;
61}
62
63void img_display(img_t *img, win_t *win) {
64 float zw, zh;
65
66 if (!img || !win || !imlib_context_get_image())
67 return;
68
69 /* set zoom level to fit image into window */
70 if (img->scalemode != SCALE_ZOOM) {
71 zw = (float) win->w / (float) img->w;
72 zh = (float) win->h / (float) img->h;
73 img->zoom = MIN(zw, zh);
74
75 if (img->zoom * 100.0 < ZOOM_MIN)
76 img->zoom = ZOOM_MIN / 100.0;
77 else if (img->zoom * 100.0 > ZOOM_MAX)
78 img->zoom = ZOOM_MAX / 100.0;
79
80 if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
81 img->zoom = 1.0;
82 }
83
84 /* center image in window */
85 img->x = (win->w - img->w * img->zoom) / 2;
86 img->y = (win->h - img->h * img->zoom) / 2;
87
88 img_render(img, win);
89}
90
91void img_render(img_t *img, win_t *win) {
92 int sx, sy, sw, sh;
93 int dx, dy, dw, dh;
94
95 if (!img || !win || !imlib_context_get_image())
96 return;
97
98 if (img->x < 0) {
99 sx = -img->x / img->zoom;
100 sw = win->w / img->zoom;
101 dx = 0;
102 dw = win->w;
103 } else {
104 sx = 0;
105 sw = img->w;
106 dx = img->x;
107 dw = img->w * img->zoom;
108 }
109 if (img->y < 0) {
110 sy = -img->y / img->zoom;
111 sh = win->h / img->zoom;
112 dy = 0;
113 dh = win->h;
114 } else {
115 sy = 0;
116 sh = img->h;
117 dy = img->y;
118 dh = img->h * img->zoom;
119 }
120
121 win_clear(win);
122
123 imlib_context_set_drawable(win->pm);
124 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
125
126 win_draw(win);
127}