Krolyxon
Commit c7860b6

Branches

Refs →

nsxivimage.c

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