Krolyxon
Commit 802c344

Branches

Refs →

nsxivimage.c

325 lines · 6.4 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
27int zl_cnt;
28float zoom_min;
29float zoom_max;
30
31void img_init(img_t *img, win_t *win) {
32 zl_cnt = sizeof(zoom_levels) / sizeof(zoom_levels[0]);
33 zoom_min = zoom_levels[0] / 100.0;
34 zoom_max = zoom_levels[zl_cnt - 1] / 100.0;
35
36 if (img) {
37 img->zoom = options->zoom;
38 img->zoom = MAX(img->zoom, zoom_min);
39 img->zoom = MIN(img->zoom, zoom_max);
40 img->aa = options->aa;
41 }
42
43 if (win) {
44 imlib_context_set_display(win->env.dpy);
45 imlib_context_set_visual(win->env.vis);
46 imlib_context_set_colormap(win->env.cmap);
47 }
48}
49
50void img_free(img_t* img) {
51 if (imlib_context_get_image())
52 imlib_free_image();
53}
54
55int img_check(const char *filename) {
56 Imlib_Image *im;
57
58 if (!filename)
59 return 0;
60
61 if (!(im = imlib_load_image(filename))) {
62 WARN("could not open image: %s", filename);
63 return 0;
64 }
65
66 imlib_context_set_image(im);
67 imlib_image_set_changes_on_disk();
68 imlib_free_image();
69
70 return 1;
71}
72
73int img_load(img_t *img, const char *filename) {
74 Imlib_Image *im;
75
76 if (!img || !filename)
77 return 0;
78
79 if (imlib_context_get_image())
80 imlib_free_image();
81
82 if (!(im = imlib_load_image(filename))) {
83 WARN("could not open image: %s", filename);
84 return 0;
85 }
86
87 imlib_context_set_image(im);
88 imlib_context_set_anti_alias(img->aa);
89 imlib_image_set_changes_on_disk();
90
91 img->re = 0;
92 img->checkpan = 0;
93 img->zoomed = 0;
94
95 img->w = imlib_image_get_width();
96 img->h = imlib_image_get_height();
97
98 return 1;
99}
100
101void img_check_pan(img_t *img, win_t *win) {
102 if (!img)
103 return;
104
105 if (img->w * img->zoom > win->w) {
106 if (img->x > 0 && img->x + img->w * img->zoom > win->w)
107 img->x = 0;
108 if (img->x < 0 && img->x + img->w * img->zoom < win->w)
109 img->x = win->w - img->w * img->zoom;
110 } else {
111 img->x = (win->w - img->w * img->zoom) / 2;
112 }
113 if (img->h * img->zoom > win->h) {
114 if (img->y > 0 && img->y + img->h * img->zoom > win->h)
115 img->y = 0;
116 if (img->y < 0 && img->y + img->h * img->zoom < win->h)
117 img->y = win->h - img->h * img->zoom;
118 } else {
119 img->y = (win->h - img->h * img->zoom) / 2;
120 }
121}
122
123void img_render(img_t *img, win_t *win) {
124 int sx, sy, sw, sh;
125 int dx, dy, dw, dh;
126
127 if (!img || !win || !imlib_context_get_image())
128 return;
129
130 if (!img->zoomed && options->scalemode != SCALE_ZOOM) {
131 img_fit(img, win);
132 if (options->scalemode == SCALE_DOWN && img->zoom > 1.0)
133 img->zoom = 1.0;
134 }
135
136 if (!img->re) {
137 /* rendered for the first time */
138 img->re = 1;
139 img_center(img, win);
140 }
141
142 if (img->checkpan) {
143 img_check_pan(img, win);
144 img->checkpan = 0;
145 }
146
147 /* calculate source and destination offsets */
148 if (img->x < 0) {
149 sx = -img->x / img->zoom;
150 sw = win->w / img->zoom;
151 dx = 0;
152 dw = win->w;
153 } else {
154 sx = 0;
155 sw = img->w;
156 dx = img->x;
157 dw = img->w * img->zoom;
158 }
159 if (img->y < 0) {
160 sy = -img->y / img->zoom;
161 sh = win->h / img->zoom;
162 dy = 0;
163 dh = win->h;
164 } else {
165 sy = 0;
166 sh = img->h;
167 dy = img->y;
168 dh = img->h * img->zoom;
169 }
170
171 win_clear(win);
172
173 imlib_context_set_drawable(win->pm);
174 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
175
176 win_draw(win);
177}
178
179int img_fit(img_t *img, win_t *win) {
180 float oz, zw, zh;
181
182 if (!img || !win)
183 return 0;
184
185 oz = img->zoom;
186 zw = (float) win->w / (float) img->w;
187 zh = (float) win->h / (float) img->h;
188
189 img->zoom = MIN(zw, zh);
190 img->zoom = MAX(img->zoom, zoom_min);
191 img->zoom = MIN(img->zoom, zoom_max);
192
193 return oz != img->zoom;
194}
195
196void img_center(img_t *img, win_t *win) {
197 if (!img || !win)
198 return;
199
200 img->x = (win->w - img->w * img->zoom) / 2;
201 img->y = (win->h - img->h * img->zoom) / 2;
202}
203
204int img_zoom(img_t *img, float z) {
205 if (!img)
206 return 0;
207
208 z = MAX(z, zoom_min);
209 z = MIN(z, zoom_max);
210
211 if (z != img->zoom) {
212 img->x -= (img->w * z - img->w * img->zoom) / 2;
213 img->y -= (img->h * z - img->h * img->zoom) / 2;
214 img->zoom = z;
215 img->checkpan = 1;
216 img->zoomed = 1;
217 return 1;
218 } else {
219 return 0;
220 }
221}
222
223int img_zoom_in(img_t *img) {
224 int i;
225
226 if (!img)
227 return 0;
228
229 for (i = 1; i < zl_cnt; ++i) {
230 if (zoom_levels[i] > img->zoom * 100.0)
231 return img_zoom(img, zoom_levels[i] / 100.0);
232 }
233
234 return 0;
235}
236
237int img_zoom_out(img_t *img) {
238 int i;
239
240 if (!img)
241 return 0;
242
243 for (i = zl_cnt - 2; i >= 0; --i) {
244 if (zoom_levels[i] < img->zoom * 100.0)
245 return img_zoom(img, zoom_levels[i] / 100.0);
246 }
247
248 return 0;
249}
250
251int img_move(img_t *img, win_t *win, int dx, int dy) {
252 int ox, oy;
253
254 if (!img || !win)
255 return 0;
256
257 ox = img->x;
258 oy = img->y;
259
260 img->x += dx;
261 img->y += dy;
262
263 img_check_pan(img, win);
264
265 return ox != img->x || oy != img->y;
266}
267
268int img_pan(img_t *img, win_t *win, pandir_t dir) {
269 if (!img || !win)
270 return 0;
271
272 switch (dir) {
273 case PAN_LEFT:
274 return img_move(img, win, win->w / 5, 0);
275 case PAN_RIGHT:
276 return img_move(img, win, win->w / 5 * -1, 0);
277 case PAN_UP:
278 return img_move(img, win, 0, win->h / 5);
279 case PAN_DOWN:
280 return img_move(img, win, 0, win->h / 5 * -1);
281 }
282
283 return 0;
284}
285
286int img_rotate(img_t *img, win_t *win, int d) {
287 int ox, oy, tmp;
288
289 if (!img || !win)
290 return 0;
291
292 ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom;
293 oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom;
294
295 imlib_image_orientate(d);
296
297 img->x = oy + (win->w - win->h) / 2;
298 img->y = ox + (win->h - win->w) / 2;
299
300 tmp = img->w;
301 img->w = img->h;
302 img->h = tmp;
303
304 img->checkpan = 1;
305
306 return 1;
307}
308
309int img_rotate_left(img_t *img, win_t *win) {
310 return img_rotate(img, win, 3);
311}
312
313int img_rotate_right(img_t *img, win_t *win) {
314 return img_rotate(img, win, 1);
315}
316
317int img_toggle_antialias(img_t *img) {
318 if (!img)
319 return 0;
320
321 img->aa ^= 1;
322 imlib_context_set_anti_alias(img->aa);
323
324 return 1;
325}