Krolyxon
Commit b900105

Branches

Refs →

nsxivimage.c

206 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->w = imlib_image_get_width();
66 img->h = imlib_image_get_height();
67
68 return 0;
69}
70
71void img_display(img_t *img, win_t *win) {
72 float zw, zh;
73
74 if (!img || !win || !imlib_context_get_image())
75 return;
76
77 /* set zoom level to fit image into window */
78 if (img->scalemode != SCALE_ZOOM) {
79 zw = (float) win->w / (float) img->w;
80 zh = (float) win->h / (float) img->h;
81 img->zoom = MIN(zw, zh);
82
83 if (img->zoom < zoom_min)
84 img->zoom = zoom_min;
85 else if (img->zoom > zoom_max)
86 img->zoom = zoom_max;
87
88 if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
89 img->zoom = 1.0;
90 }
91
92 /* center image in window */
93 img->x = (win->w - img->w * img->zoom) / 2;
94 img->y = (win->h - img->h * img->zoom) / 2;
95
96 img_render(img, win);
97}
98
99void img_check_pan(img_t *img, win_t *win) {
100 if (!img)
101 return;
102
103 if (img->w * img->zoom > win->w) {
104 if (img->x > 0 && img->x + img->w * img->zoom > win->w)
105 img->x = 0;
106 if (img->x < 0 && img->x + img->w * img->zoom < win->w)
107 img->x = win->w - img->w * img->zoom;
108 } else {
109 img->x = (win->w - img->w * img->zoom) / 2;
110 }
111 if (img->h * img->zoom > win->h) {
112 if (img->y > 0 && img->y + img->h * img->zoom > win->h)
113 img->y = 0;
114 if (img->y < 0 && img->y + img->h * img->zoom < win->h)
115 img->y = win->h - img->h * img->zoom;
116 } else {
117 img->y = (win->h - img->h * img->zoom) / 2;
118 }
119}
120
121void img_render(img_t *img, win_t *win) {
122 int sx, sy, sw, sh;
123 int dx, dy, dw, dh;
124
125 if (!img || !win || !imlib_context_get_image())
126 return;
127
128 img_check_pan(img, win);
129
130 if (img->x < 0) {
131 sx = -img->x / img->zoom;
132 sw = win->w / img->zoom;
133 dx = 0;
134 dw = win->w;
135 } else {
136 sx = 0;
137 sw = img->w;
138 dx = img->x;
139 dw = img->w * img->zoom;
140 }
141 if (img->y < 0) {
142 sy = -img->y / img->zoom;
143 sh = win->h / img->zoom;
144 dy = 0;
145 dh = win->h;
146 } else {
147 sy = 0;
148 sh = img->h;
149 dy = img->y;
150 dh = img->h * img->zoom;
151 }
152
153 win_clear(win);
154
155 imlib_context_set_drawable(win->pm);
156 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
157
158 win_draw(win);
159}
160
161int img_zoom(img_t *img, float z) {
162 if (!img)
163 return 0;
164
165 if (z < zoom_min)
166 z = zoom_min;
167 else if (z > zoom_max)
168 z = zoom_max;
169
170 if (z != img->zoom) {
171 img->x -= (img->w * z - img->w * img->zoom) / 2;
172 img->y -= (img->h * z - img->h * img->zoom) / 2;
173 img->zoom = z;
174 return 1;
175 } else {
176 return 0;
177 }
178}
179
180int img_zoom_in(img_t *img) {
181 int i;
182
183 if (!img)
184 return 0;
185
186 for (i = 1; i < zl_cnt; ++i) {
187 if (zoom_levels[i] > img->zoom * 100.0)
188 return img_zoom(img, zoom_levels[i] / 100.0);
189 }
190
191 return 0;
192}
193
194int img_zoom_out(img_t *img) {
195 int i;
196
197 if (!img)
198 return 0;
199
200 for (i = zl_cnt - 2; i >= 0; --i) {
201 if (zoom_levels[i] < img->zoom * 100.0)
202 return img_zoom(img, zoom_levels[i] / 100.0);
203 }
204
205 return 0;
206}