Refactored imlib context handling
This commit is contained in:
		
							parent
							
								
									62fb69b328
								
							
						
					
					
						commit
						dafe7eac74
					
				
							
								
								
									
										2
									
								
								icon.h
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								icon.h
									
									
									
									
									
								
							| @ -1,6 +1,8 @@ | |||||||
| #ifndef ICON_H | #ifndef ICON_H | ||||||
| #define ICON_H | #define ICON_H | ||||||
| 
 | 
 | ||||||
|  | #include <Imlib2.h> | ||||||
|  | 
 | ||||||
| static DATA32 icon_broken[] = { | static DATA32 icon_broken[] = { | ||||||
| 	0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, | 	0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, | ||||||
| 	0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, | 	0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, 0xffcc0000, | ||||||
|  | |||||||
							
								
								
									
										74
									
								
								image.c
									
									
									
									
									
								
							
							
						
						
									
										74
									
								
								image.c
									
									
									
									
									
								
							| @ -17,7 +17,6 @@ | |||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| #include <unistd.h> | #include <unistd.h> | ||||||
| #include <Imlib2.h> |  | ||||||
| 
 | 
 | ||||||
| #include "config.h" | #include "config.h" | ||||||
| #include "icon.h" | #include "icon.h" | ||||||
| @ -39,6 +38,7 @@ void img_init(img_t *img, win_t *win) { | |||||||
| 	im_broken = imlib_create_image_using_data(32, 32, icon_broken); | 	im_broken = imlib_create_image_using_data(32, 32, icon_broken); | ||||||
| 
 | 
 | ||||||
| 	if (img) { | 	if (img) { | ||||||
|  | 		img->im = NULL; | ||||||
| 		img->zoom = options->zoom; | 		img->zoom = options->zoom; | ||||||
| 		img->zoom = MAX(img->zoom, zoom_min); | 		img->zoom = MAX(img->zoom, zoom_min); | ||||||
| 		img->zoom = MIN(img->zoom, zoom_max); | 		img->zoom = MIN(img->zoom, zoom_max); | ||||||
| @ -53,48 +53,38 @@ void img_init(img_t *img, win_t *win) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void img_free(img_t* img) { | void img_free(img_t* img) { | ||||||
| 	if (img && img->valid && imlib_context_get_image()) |  | ||||||
| 		imlib_free_image(); |  | ||||||
| 	imlib_context_set_image(im_broken); | 	imlib_context_set_image(im_broken); | ||||||
| 	imlib_free_image(); | 	imlib_free_image(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int _imlib_load_image(const char *filename) { | int img_check(const char *filename) { | ||||||
| 	Imlib_Image *im; | 	Imlib_Image *im; | ||||||
| 
 | 
 | ||||||
| 	if (!filename) | 	if (!filename) | ||||||
| 		return 0; | 		return 0; | ||||||
| 
 | 
 | ||||||
| 	if (access(filename, F_OK) || !(im = imlib_load_image(filename))) { | 	if (!access(filename, F_OK) && (im = imlib_load_image(filename))) { | ||||||
|  | 		imlib_context_set_image(im); | ||||||
|  | 		imlib_image_set_changes_on_disk(); | ||||||
|  | 		imlib_free_image(); | ||||||
|  | 		return 1; | ||||||
|  | 	} else { | ||||||
| 		warn("could not open file: %s", filename); | 		warn("could not open file: %s", filename); | ||||||
| 		return 0; | 		return 0; | ||||||
| 	} | 	} | ||||||
| 
 |  | ||||||
| 	imlib_context_set_image(im); |  | ||||||
| 	imlib_image_set_changes_on_disk(); |  | ||||||
| 
 |  | ||||||
| 	return 1; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| int img_check(const char *filename) { |  | ||||||
| 	int ret; |  | ||||||
| 
 |  | ||||||
| 	if ((ret = _imlib_load_image(filename))) |  | ||||||
| 		imlib_free_image(); |  | ||||||
| 	return ret; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int img_load(img_t *img, const char *filename) { | int img_load(img_t *img, const char *filename) { | ||||||
| 	if (!img || !filename) | 	if (!img || !filename) | ||||||
| 		return 0; | 		return 0; | ||||||
| 
 | 
 | ||||||
| 	if (img->valid && imlib_context_get_image()) | 	if (!access(filename, F_OK) && (img->im = imlib_load_image(filename))) { | ||||||
| 		imlib_free_image(); | 		imlib_context_set_image(img->im); | ||||||
| 
 | 		imlib_image_set_changes_on_disk(); | ||||||
| 	if ((img->valid = _imlib_load_image(filename))) { |  | ||||||
| 		imlib_context_set_anti_alias(img->aa); | 		imlib_context_set_anti_alias(img->aa); | ||||||
| 		img->scalemode = options->scalemode; | 		img->scalemode = options->scalemode; | ||||||
| 	} else { | 	} else { | ||||||
|  | 		warn("could not open file: %s", filename); | ||||||
| 		imlib_context_set_image(im_broken); | 		imlib_context_set_image(im_broken); | ||||||
| 		imlib_context_set_anti_alias(0); | 		imlib_context_set_anti_alias(0); | ||||||
| 		img->scalemode = SCALE_DOWN; | 		img->scalemode = SCALE_DOWN; | ||||||
| @ -109,6 +99,14 @@ int img_load(img_t *img, const char *filename) { | |||||||
| 	return 1; | 	return 1; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | void img_close(img_t *img) { | ||||||
|  | 	if (img && img->im) { | ||||||
|  | 		imlib_context_set_image(img->im); | ||||||
|  | 		imlib_free_image(); | ||||||
|  | 		img->im = NULL; | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
| void img_check_pan(img_t *img, win_t *win) { | void img_check_pan(img_t *img, win_t *win) { | ||||||
| 	if (!img || !win) | 	if (!img || !win) | ||||||
| 		return; | 		return; | ||||||
| @ -152,7 +150,7 @@ void img_render(img_t *img, win_t *win) { | |||||||
| 	int sx, sy, sw, sh; | 	int sx, sy, sw, sh; | ||||||
| 	int dx, dy, dw, dh; | 	int dx, dy, dw, dh; | ||||||
| 
 | 
 | ||||||
| 	if (!img || !win || !imlib_context_get_image()) | 	if (!img || !win) | ||||||
| 		return; | 		return; | ||||||
| 
 | 
 | ||||||
| 	if (img->scalemode != SCALE_ZOOM) { | 	if (img->scalemode != SCALE_ZOOM) { | ||||||
| @ -198,6 +196,11 @@ void img_render(img_t *img, win_t *win) { | |||||||
| 
 | 
 | ||||||
| 	win_clear(win); | 	win_clear(win); | ||||||
| 
 | 
 | ||||||
|  | 	if (img->im) | ||||||
|  | 		imlib_context_set_image(img->im); | ||||||
|  | 	else | ||||||
|  | 		imlib_context_set_image(im_broken); | ||||||
|  | 	 | ||||||
| 	imlib_context_set_drawable(win->pm); | 	imlib_context_set_drawable(win->pm); | ||||||
| 	imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh); | 	imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh); | ||||||
| 
 | 
 | ||||||
| @ -205,7 +208,7 @@ void img_render(img_t *img, win_t *win) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int img_fit_win(img_t *img, win_t *win) { | int img_fit_win(img_t *img, win_t *win) { | ||||||
| 	if (!img || !img->valid || !win) | 	if (!img || !img->im || !win) | ||||||
| 		return 0; | 		return 0; | ||||||
| 
 | 
 | ||||||
| 	img->scalemode = SCALE_FIT; | 	img->scalemode = SCALE_FIT; | ||||||
| @ -228,7 +231,7 @@ int img_center(img_t *img, win_t *win) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int img_zoom(img_t *img, float z) { | int img_zoom(img_t *img, float z) { | ||||||
| 	if (!img || !img->valid) | 	if (!img || !img->im) | ||||||
| 		return 0; | 		return 0; | ||||||
| 
 | 
 | ||||||
| 	z = MAX(z, zoom_min); | 	z = MAX(z, zoom_min); | ||||||
| @ -250,7 +253,7 @@ int img_zoom(img_t *img, float z) { | |||||||
| int img_zoom_in(img_t *img) { | int img_zoom_in(img_t *img) { | ||||||
| 	int i; | 	int i; | ||||||
| 
 | 
 | ||||||
| 	if (!img || !img->valid) | 	if (!img || !img->im) | ||||||
| 		return 0; | 		return 0; | ||||||
| 
 | 
 | ||||||
| 	for (i = 1; i < zl_cnt; ++i) { | 	for (i = 1; i < zl_cnt; ++i) { | ||||||
| @ -263,7 +266,7 @@ int img_zoom_in(img_t *img) { | |||||||
| int img_zoom_out(img_t *img) { | int img_zoom_out(img_t *img) { | ||||||
| 	int i; | 	int i; | ||||||
| 
 | 
 | ||||||
| 	if (!img || !img->valid) | 	if (!img || !img->im) | ||||||
| 		return 0; | 		return 0; | ||||||
| 
 | 
 | ||||||
| 	for (i = zl_cnt - 2; i >= 0; --i) { | 	for (i = zl_cnt - 2; i >= 0; --i) { | ||||||
| @ -276,7 +279,7 @@ int img_zoom_out(img_t *img) { | |||||||
| int img_move(img_t *img, win_t *win, int dx, int dy) { | int img_move(img_t *img, win_t *win, int dx, int dy) { | ||||||
| 	int ox, oy; | 	int ox, oy; | ||||||
| 
 | 
 | ||||||
| 	if (!img || !img->valid || !win) | 	if (!img || !img->im || !win) | ||||||
| 		return 0; | 		return 0; | ||||||
| 
 | 
 | ||||||
| 	ox = img->x; | 	ox = img->x; | ||||||
| @ -291,7 +294,7 @@ int img_move(img_t *img, win_t *win, int dx, int dy) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int img_pan(img_t *img, win_t *win, pandir_t dir) { | int img_pan(img_t *img, win_t *win, pandir_t dir) { | ||||||
| 	if (!img || !img->valid || !win) | 	if (!img || !img->im || !win) | ||||||
| 		return 0; | 		return 0; | ||||||
| 
 | 
 | ||||||
| 	switch (dir) { | 	switch (dir) { | ||||||
| @ -311,12 +314,13 @@ int img_pan(img_t *img, win_t *win, pandir_t dir) { | |||||||
| void img_rotate(img_t *img, win_t *win, int d) { | void img_rotate(img_t *img, win_t *win, int d) { | ||||||
| 	int ox, oy, tmp; | 	int ox, oy, tmp; | ||||||
| 
 | 
 | ||||||
| 	if (!img || !img->valid || !win) | 	if (!img || !img->im || !win) | ||||||
| 		return; | 		return; | ||||||
| 
 | 
 | ||||||
| 	ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom; | 	ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom; | ||||||
| 	oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom; | 	oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom; | ||||||
| 
 | 
 | ||||||
|  | 	imlib_context_set_image(img->im); | ||||||
| 	imlib_image_orientate(d); | 	imlib_image_orientate(d); | ||||||
| 
 | 
 | ||||||
| 	img->x = oy + (win->w - win->h) / 2; | 	img->x = oy + (win->w - win->h) / 2; | ||||||
| @ -338,9 +342,9 @@ void img_rotate_right(img_t *img, win_t *win) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void img_toggle_antialias(img_t *img) { | void img_toggle_antialias(img_t *img) { | ||||||
| 	if (!img || !img->valid) | 	if (img && img->im) { | ||||||
| 		return; | 		img->aa ^= 1; | ||||||
| 
 | 		imlib_context_set_image(img->im); | ||||||
| 	img->aa ^= 1; | 		imlib_context_set_anti_alias(img->aa); | ||||||
| 	imlib_context_set_anti_alias(img->aa); | 	} | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										6
									
								
								image.h
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								image.h
									
									
									
									
									
								
							| @ -19,6 +19,8 @@ | |||||||
| #ifndef IMAGE_H | #ifndef IMAGE_H | ||||||
| #define IMAGE_H | #define IMAGE_H | ||||||
| 
 | 
 | ||||||
|  | #include <Imlib2.h> | ||||||
|  | 
 | ||||||
| #include "window.h" | #include "window.h" | ||||||
| 
 | 
 | ||||||
| typedef enum scalemode_e { | typedef enum scalemode_e { | ||||||
| @ -35,10 +37,11 @@ typedef enum pandir_e { | |||||||
| } pandir_t; | } pandir_t; | ||||||
| 
 | 
 | ||||||
| typedef struct img_s { | typedef struct img_s { | ||||||
|  | 	Imlib_Image *im; | ||||||
|  | 
 | ||||||
| 	float zoom; | 	float zoom; | ||||||
| 	scalemode_t scalemode; | 	scalemode_t scalemode; | ||||||
| 
 | 
 | ||||||
| 	unsigned char valid; |  | ||||||
| 	unsigned char re; | 	unsigned char re; | ||||||
| 	unsigned char checkpan; | 	unsigned char checkpan; | ||||||
| 	unsigned char aa; | 	unsigned char aa; | ||||||
| @ -54,6 +57,7 @@ void img_free(img_t*); | |||||||
| 
 | 
 | ||||||
| int img_check(const char*); | int img_check(const char*); | ||||||
| int img_load(img_t*, const char*); | int img_load(img_t*, const char*); | ||||||
|  | void img_close(img_t*); | ||||||
| 
 | 
 | ||||||
| void img_render(img_t*, win_t*); | void img_render(img_t*, win_t*); | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										6
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								main.c
									
									
									
									
									
								
							| @ -62,6 +62,7 @@ void cleanup() { | |||||||
| 	static int in = 0; | 	static int in = 0; | ||||||
| 
 | 
 | ||||||
| 	if (!in++) { | 	if (!in++) { | ||||||
|  | 		img_close(&img); | ||||||
| 		img_free(&img); | 		img_free(&img); | ||||||
| 		tns_free(&tns, &win); | 		tns_free(&tns, &win); | ||||||
| 		win_close(&win); | 		win_close(&win); | ||||||
| @ -71,6 +72,8 @@ void cleanup() { | |||||||
| int load_image() { | int load_image() { | ||||||
| 	struct stat fstats; | 	struct stat fstats; | ||||||
| 
 | 
 | ||||||
|  | 	img_close(&img); | ||||||
|  | 
 | ||||||
| 	if (!stat(filenames[fileidx], &fstats)) | 	if (!stat(filenames[fileidx], &fstats)) | ||||||
| 		filesize = fstats.st_size; | 		filesize = fstats.st_size; | ||||||
| 	else | 	else | ||||||
| @ -158,7 +161,7 @@ void update_title() { | |||||||
| 		             tns.cnt ? tns.sel + 1 : 0, tns.cnt, | 		             tns.cnt ? tns.sel + 1 : 0, tns.cnt, | ||||||
| 		             tns.cnt ? filenames[tns.sel] : ""); | 		             tns.cnt ? filenames[tns.sel] : ""); | ||||||
| 	} else { | 	} else { | ||||||
| 		if (img.valid) { | 		if (img.im) { | ||||||
| 			size = filesize; | 			size = filesize; | ||||||
| 			size_readable(&size, &unit); | 			size_readable(&size, &unit); | ||||||
| 			n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> (%.2f%s) %s", | 			n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> (%.2f%s) %s", | ||||||
| @ -388,6 +391,7 @@ void on_keypress(XKeyEvent *kev) { | |||||||
| 			case XK_Return: | 			case XK_Return: | ||||||
| 				if (!tns.thumbs) | 				if (!tns.thumbs) | ||||||
| 					tns_init(&tns, filecnt); | 					tns_init(&tns, filecnt); | ||||||
|  | 				img_close(&img); | ||||||
| 				mode = MODE_THUMBS; | 				mode = MODE_THUMBS; | ||||||
| 				win_set_cursor(&win, CURSOR_ARROW); | 				win_set_cursor(&win, CURSOR_ARROW); | ||||||
| 				timo_cursor = 0; | 				timo_cursor = 0; | ||||||
|  | |||||||
							
								
								
									
										5
									
								
								thumbs.c
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								thumbs.c
									
									
									
									
									
								
							| @ -79,10 +79,13 @@ void tns_load(tns_t *tns, win_t *win, const char *filename) { | |||||||
| 
 | 
 | ||||||
| 	t->pm = win_create_pixmap(win, t->w, t->h); | 	t->pm = win_create_pixmap(win, t->w, t->h); | ||||||
| 	imlib_context_set_drawable(t->pm); | 	imlib_context_set_drawable(t->pm); | ||||||
|  | 	imlib_context_set_anti_alias(1); | ||||||
| 	imlib_render_image_part_on_drawable_at_size(0, 0, w, h, | 	imlib_render_image_part_on_drawable_at_size(0, 0, w, h, | ||||||
| 	                                            0, 0, t->w, t->h); | 	                                            0, 0, t->w, t->h); | ||||||
| 	imlib_free_image(); |  | ||||||
| 	tns->dirty = 1; | 	tns->dirty = 1; | ||||||
|  | 
 | ||||||
|  | 	if (im) | ||||||
|  | 		imlib_free_image(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void tns_check_view(tns_t *tns, Bool scrolled) { | void tns_check_view(tns_t *tns, Bool scrolled) { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Bert
						Bert