Krolyxon
Commit 629d373

Branches

Refs →

nsxivimage.c

205 lines · 4.3 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 imlib_init(win_t *win) {
32 if (!win)
33 return;
34
35 imlib_context_set_display(win->env.dpy);
36 imlib_context_set_visual(win->env.vis);
37 imlib_context_set_colormap(win->env.cmap);
38
39 zl_cnt = sizeof(zoom_levels) / sizeof(zoom_levels[0]);
40 zoom_min = zoom_levels[0] / 100.0;
41 zoom_max = zoom_levels[zl_cnt - 1] / 100.0;
42}
43
44void imlib_destroy() {
45 if (imlib_context_get_image())
46 imlib_free_image();
47}
48
49int img_load(img_t *img, const char *filename) {
50 Imlib_Image *im;
51
52 if (!img || !filename)
53 return -1;
54
55 if (imlib_context_get_image())
56 imlib_free_image();
57
58 if (!(im = imlib_load_image(filename))) {
59 WARN("could not open image: %s", filename);
60 return -1;
61 }
62
63 imlib_context_set_image(im);
64
65 img->re = 0;
66 img->w = imlib_image_get_width();
67 img->h = imlib_image_get_height();
68
69 return 0;
70}
71
72void img_check_pan(img_t *img, win_t *win) {
73 if (!img)
74 return;
75
76 if (img->w * img->zoom > win->w) {
77 if (img->x > 0 && img->x + img->w * img->zoom > win->w)
78 img->x = 0;
79 if (img->x < 0 && img->x + img->w * img->zoom < win->w)
80 img->x = win->w - img->w * img->zoom;
81 } else {
82 img->x = (win->w - img->w * img->zoom) / 2;
83 }
84 if (img->h * img->zoom > win->h) {
85 if (img->y > 0 && img->y + img->h * img->zoom > win->h)
86 img->y = 0;
87 if (img->y < 0 && img->y + img->h * img->zoom < win->h)
88 img->y = win->h - img->h * img->zoom;
89 } else {
90 img->y = (win->h - img->h * img->zoom) / 2;
91 }
92}
93
94void img_render(img_t *img, win_t *win) {
95 int sx, sy, sw, sh;
96 int dx, dy, dw, dh;
97 float zw, zh;
98
99 if (!img || !win || !imlib_context_get_image())
100 return;
101
102 if (!img->re) {
103 /* rendered for the first time */
104 img->re = 1;
105
106 /* set zoom level to fit image into window */
107 if (img->scalemode != SCALE_ZOOM) {
108 zw = (float) win->w / (float) img->w;
109 zh = (float) win->h / (float) img->h;
110 img->zoom = MIN(zw, zh);
111
112 if (img->zoom < zoom_min)
113 img->zoom = zoom_min;
114 else if (img->zoom > zoom_max)
115 img->zoom = zoom_max;
116
117 if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
118 img->zoom = 1.0;
119 }
120
121 /* center image in window */
122 img->x = (win->w - img->w * img->zoom) / 2;
123 img->y = (win->h - img->h * img->zoom) / 2;
124 } else {
125 /* typically after zooming and panning */
126 img_check_pan(img, win);
127 }
128
129 if (img->x < 0) {
130 sx = -img->x / img->zoom;
131 sw = win->w / img->zoom;
132 dx = 0;
133 dw = win->w;
134 } else {
135 sx = 0;
136 sw = img->w;
137 dx = img->x;
138 dw = img->w * img->zoom;
139 }
140 if (img->y < 0) {
141 sy = -img->y / img->zoom;
142 sh = win->h / img->zoom;
143 dy = 0;
144 dh = win->h;
145 } else {
146 sy = 0;
147 sh = img->h;
148 dy = img->y;
149 dh = img->h * img->zoom;
150 }
151
152 win_clear(win);
153
154 imlib_context_set_drawable(win->pm);
155 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
156
157 win_draw(win);
158}
159
160int img_zoom(img_t *img, float z) {
161 if (!img)
162 return 0;
163
164 if (z < zoom_min)
165 z = zoom_min;
166 else if (z > zoom_max)
167 z = zoom_max;
168
169 if (z != img->zoom) {
170 img->x -= (img->w * z - img->w * img->zoom) / 2;
171 img->y -= (img->h * z - img->h * img->zoom) / 2;
172 img->zoom = z;
173 return 1;
174 } else {
175 return 0;
176 }
177}
178
179int img_zoom_in(img_t *img) {
180 int i;
181
182 if (!img)
183 return 0;
184
185 for (i = 1; i < zl_cnt; ++i) {
186 if (zoom_levels[i] > img->zoom * 100.0)
187 return img_zoom(img, zoom_levels[i] / 100.0);
188 }
189
190 return 0;
191}
192
193int img_zoom_out(img_t *img) {
194 int i;
195
196 if (!img)
197 return 0;
198
199 for (i = zl_cnt - 2; i >= 0; --i) {
200 if (zoom_levels[i] < img->zoom * 100.0)
201 return img_zoom(img, zoom_levels[i] / 100.0);
202 }
203
204 return 0;
205}