Krolyxon
Commit dafe7ea

Branches

Refs →

nsxivdafe7ea

Refactored imlib context handling

Author
Bert
Date
2011-02-25 12:08 +0100
Parent
62fb69b
Commit
dafe7eac742301952c088d68a455586b0225a7bd

+55 −38

5 files changed

  1. M icon.h +2 −0
  2. M image.c +39 −35
  3. M image.h +5 −1
  4. M main.c +5 −1
  5. M thumbs.c +4 −1
M icon.h View
@@ -1,6 +1,8 @@
11 #ifndef ICON_H
22 #define ICON_H
33
4+#include <Imlib2.h>
5+
46 static DATA32 icon_broken[] = {
57 0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000,
68 0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000,
M image.c View
@@ -17,7 +17,6 @@
1717 */
1818
1919 #include <unistd.h>
20−#include <Imlib2.h>
2120
2221 #include "config.h"
2322 #include "icon.h"
@@ -39,6 +38,7 @@ void img_init(img_t *img, win_t *win) {
3938 im_broken = imlib_create_image_using_data(32, 32, icon_broken);
4039
4140 if (img) {
41+ img->im = NULL;
4242 img->zoom = options->zoom;
4343 img->zoom = MAX(img->zoom, zoom_min);
4444 img->zoom = MIN(img->zoom, zoom_max);
@@ -53,48 +53,38 @@ void img_init(img_t *img, win_t *win) {
5353 }
5454
5555 void img_free(img_t* img) {
56− if (img && img->valid && imlib_context_get_image())
57− imlib_free_image();
5856 imlib_context_set_image(im_broken);
5957 imlib_free_image();
6058 }
6159
62−int _imlib_load_image(const char *filename) {
60+int img_check(const char *filename) {
6361 Imlib_Image *im;
6462
6563 if (!filename)
6664 return 0;
6765
68− if (access(filename, F_OK) || !(im = imlib_load_image(filename))) {
66+ if (!access(filename, F_OK) && (im = imlib_load_image(filename))) {
67+ imlib_context_set_image(im);
68+ imlib_image_set_changes_on_disk();
69+ imlib_free_image();
70+ return 1;
71+ } else {
6972 warn("could not open file: %s", filename);
7073 return 0;
7174 }
72−
73− imlib_context_set_image(im);
74− imlib_image_set_changes_on_disk();
75−
76− return 1;
77−}
78−
79−int img_check(const char *filename) {
80− int ret;
81−
82− if ((ret = _imlib_load_image(filename)))
83− imlib_free_image();
84− return ret;
8575 }
8676
8777 int img_load(img_t *img, const char *filename) {
8878 if (!img || !filename)
8979 return 0;
9080
91− if (img->valid && imlib_context_get_image())
92− imlib_free_image();
93−
94− if ((img->valid = _imlib_load_image(filename))) {
81+ if (!access(filename, F_OK) && (img->im = imlib_load_image(filename))) {
82+ imlib_context_set_image(img->im);
83+ imlib_image_set_changes_on_disk();
9584 imlib_context_set_anti_alias(img->aa);
9685 img->scalemode = options->scalemode;
9786 } else {
87+ warn("could not open file: %s", filename);
9888 imlib_context_set_image(im_broken);
9989 imlib_context_set_anti_alias(0);
10090 img->scalemode = SCALE_DOWN;
@@ -109,6 +99,14 @@ int img_load(img_t *img, const char *filename) {
10999 return 1;
110100 }
111101
102+void img_close(img_t *img) {
103+ if (img && img->im) {
104+ imlib_context_set_image(img->im);
105+ imlib_free_image();
106+ img->im = NULL;
107+ }
108+}
109+
112110 void img_check_pan(img_t *img, win_t *win) {
113111 if (!img || !win)
114112 return;
@@ -152,7 +150,7 @@ void img_render(img_t *img, win_t *win) {
152150 int sx, sy, sw, sh;
153151 int dx, dy, dw, dh;
154152
155− if (!img || !win || !imlib_context_get_image())
153+ if (!img || !win)
156154 return;
157155
158156 if (img->scalemode != SCALE_ZOOM) {
@@ -198,6 +196,11 @@ void img_render(img_t *img, win_t *win) {
198196
199197 win_clear(win);
200198
199+ if (img->im)
200+ imlib_context_set_image(img->im);
201+ else
202+ imlib_context_set_image(im_broken);
203+
201204 imlib_context_set_drawable(win->pm);
202205 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
203206
@@ -205,7 +208,7 @@ void img_render(img_t *img, win_t *win) {
205208 }
206209
207210 int img_fit_win(img_t *img, win_t *win) {
208− if (!img || !img->valid || !win)
211+ if (!img || !img->im || !win)
209212 return 0;
210213
211214 img->scalemode = SCALE_FIT;
@@ -228,7 +231,7 @@ int img_center(img_t *img, win_t *win) {
228231 }
229232
230233 int img_zoom(img_t *img, float z) {
231− if (!img || !img->valid)
234+ if (!img || !img->im)
232235 return 0;
233236
234237 z = MAX(z, zoom_min);
@@ -250,7 +253,7 @@ int img_zoom(img_t *img, float z) {
250253 int img_zoom_in(img_t *img) {
251254 int i;
252255
253− if (!img || !img->valid)
256+ if (!img || !img->im)
254257 return 0;
255258
256259 for (i = 1; i < zl_cnt; ++i) {
@@ -263,7 +266,7 @@ int img_zoom_in(img_t *img) {
263266 int img_zoom_out(img_t *img) {
264267 int i;
265268
266− if (!img || !img->valid)
269+ if (!img || !img->im)
267270 return 0;
268271
269272 for (i = zl_cnt - 2; i >= 0; --i) {
@@ -276,7 +279,7 @@ int img_zoom_out(img_t *img) {
276279 int img_move(img_t *img, win_t *win, int dx, int dy) {
277280 int ox, oy;
278281
279− if (!img || !img->valid || !win)
282+ if (!img || !img->im || !win)
280283 return 0;
281284
282285 ox = img->x;
@@ -291,7 +294,7 @@ int img_move(img_t *img, win_t *win, int dx, int dy) {
291294 }
292295
293296 int img_pan(img_t *img, win_t *win, pandir_t dir) {
294− if (!img || !img->valid || !win)
297+ if (!img || !img->im || !win)
295298 return 0;
296299
297300 switch (dir) {
@@ -311,12 +314,13 @@ int img_pan(img_t *img, win_t *win, pandir_t dir) {
311314 void img_rotate(img_t *img, win_t *win, int d) {
312315 int ox, oy, tmp;
313316
314− if (!img || !img->valid || !win)
317+ if (!img || !img->im || !win)
315318 return;
316319
317320 ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom;
318321 oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom;
319322
323+ imlib_context_set_image(img->im);
320324 imlib_image_orientate(d);
321325
322326 img->x = oy + (win->w - win->h) / 2;
@@ -338,9 +342,9 @@ void img_rotate_right(img_t *img, win_t *win) {
338342 }
339343
340344 void img_toggle_antialias(img_t *img) {
341− if (!img || !img->valid)
342− return;
343−
344− img->aa ^= 1;
345− imlib_context_set_anti_alias(img->aa);
345+ if (img && img->im) {
346+ img->aa ^= 1;
347+ imlib_context_set_image(img->im);
348+ imlib_context_set_anti_alias(img->aa);
349+ }
346350 }
M image.h View
@@ -19,6 +19,8 @@
1919 #ifndef IMAGE_H
2020 #define IMAGE_H
2121
22+#include <Imlib2.h>
23+
2224 #include "window.h"
2325
2426 typedef enum scalemode_e {
@@ -35,10 +37,11 @@ typedef enum pandir_e {
3537 } pandir_t;
3638
3739 typedef struct img_s {
40+ Imlib_Image *im;
41+
3842 float zoom;
3943 scalemode_t scalemode;
4044
41− unsigned char valid;
4245 unsigned char re;
4346 unsigned char checkpan;
4447 unsigned char aa;
@@ -54,6 +57,7 @@ void img_free(img_t*);
5457
5558 int img_check(const char*);
5659 int img_load(img_t*, const char*);
60+void img_close(img_t*);
5761
5862 void img_render(img_t*, win_t*);
5963
M main.c View
@@ -62,6 +62,7 @@ void cleanup() {
6262 static int in = 0;
6363
6464 if (!in++) {
65+ img_close(&img);
6566 img_free(&img);
6667 tns_free(&tns, &win);
6768 win_close(&win);
@@ -71,6 +72,8 @@ void cleanup() {
7172 int load_image() {
7273 struct stat fstats;
7374
75+ img_close(&img);
76+
7477 if (!stat(filenames[fileidx], &fstats))
7578 filesize = fstats.st_size;
7679 else
@@ -158,7 +161,7 @@ void update_title() {
158161 tns.cnt ? tns.sel + 1 : 0, tns.cnt,
159162 tns.cnt ? filenames[tns.sel] : "");
160163 } else {
161− if (img.valid) {
164+ if (img.im) {
162165 size = filesize;
163166 size_readable(&size, &unit);
164167 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> (%.2f%s) %s",
@@ -388,6 +391,7 @@ void on_keypress(XKeyEvent *kev) {
388391 case XK_Return:
389392 if (!tns.thumbs)
390393 tns_init(&tns, filecnt);
394+ img_close(&img);
391395 mode = MODE_THUMBS;
392396 win_set_cursor(&win, CURSOR_ARROW);
393397 timo_cursor = 0;
M thumbs.c View
@@ -79,10 +79,13 @@ void tns_load(tns_t *tns, win_t *win, const char *filename) {
7979
8080 t->pm = win_create_pixmap(win, t->w, t->h);
8181 imlib_context_set_drawable(t->pm);
82+ imlib_context_set_anti_alias(1);
8283 imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
8384 0, 0, t->w, t->h);
84− imlib_free_image();
8585 tns->dirty = 1;
86+
87+ if (im)
88+ imlib_free_image();
8689 }
8790
8891 void tns_check_view(tns_t *tns, Bool scrolled) {