aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNRK <nrk@disroot.org>2023-07-02 16:15:53 +0200
committerNRK <nrk@disroot.org>2023-09-21 00:54:41 +0200
commit53a43cb38838bd65512b3b8902cc8a3ecfaaca56 (patch)
treedb32241dfce726a02e863c98690812c83cd1473f
parent69d4957a92030bf89323d9174c45263727483de4 (diff)
downloadnsxiv-53a43cb38838bd65512b3b8902cc8a3ecfaaca56.tar.zst
introduce img_free()
this removes some repetitive code.
-rw-r--r--image.c40
-rw-r--r--nsxiv.h1
-rw-r--r--thumbs.c23
3 files changed, 24 insertions, 40 deletions
diff --git a/image.c b/image.c
index 4e7d645..a8b91a0 100644
--- a/image.c
+++ b/image.c
@@ -148,12 +148,10 @@ void exif_auto_orientate(const fileinfo_t *file)
static void img_multiframe_context_set(img_t *img)
{
if (img->multi.cnt > 1) {
- imlib_context_set_image(img->im);
- imlib_free_image();
+ img_free(img->im, false);
img->im = img->multi.frames[0].im;
} else if (img->multi.cnt == 1) {
- imlib_context_set_image(img->multi.frames[0].im);
- imlib_free_image();
+ img_free(img->multi.frames[0].im, false);
img->multi.cnt = 0;
}
@@ -502,14 +500,8 @@ static bool img_load_multiframe(img_t *img, const fileinfo_t *file)
if (canvas == NULL || frame == NULL || finfo.frame_count != (int)fcnt ||
finfo.canvas_w != img->w || finfo.canvas_h != img->h)
{
- if (frame != NULL) {
- imlib_context_set_image(frame);
- imlib_free_image();
- }
- if (canvas != NULL) {
- imlib_context_set_image(canvas);
- imlib_free_image();
- }
+ img_free(frame, false);
+ img_free(canvas, false);
error(0, 0, "%s: failed to load frame %d", file->name, n);
break;
}
@@ -546,11 +538,9 @@ static bool img_load_multiframe(img_t *img, const fileinfo_t *file)
m->frames[m->cnt].delay = finfo.frame_delay ? finfo.frame_delay : DEF_ANIM_DELAY;
m->length += m->frames[m->cnt].delay;
m->cnt++;
- imlib_context_set_image(frame);
- imlib_free_image();
+ img_free(frame, false);
}
- imlib_context_set_image(blank);
- imlib_free_image();
+ img_free(blank, false);
img_multiframe_context_set(img);
imlib_context_set_color_modifier(img->cmod); /* restore cmod */
return m->cnt > 0;
@@ -632,21 +622,25 @@ bool img_load(img_t *img, const fileinfo_t *file)
return true;
}
+CLEANUP void img_free(Imlib_Image im, bool decache)
+{
+ if (im != NULL) {
+ imlib_context_set_image(im);
+ decache ? imlib_free_image_and_decache() : imlib_free_image();
+ }
+}
+
CLEANUP void img_close(img_t *img, bool decache)
{
unsigned int i;
- void (*free_img)(void) = decache ? imlib_free_image_and_decache : imlib_free_image;
if (img->multi.cnt > 0) {
- for (i = 0; i < img->multi.cnt; i++) {
- imlib_context_set_image(img->multi.frames[i].im);
- free_img();
- }
+ for (i = 0; i < img->multi.cnt; i++)
+ img_free(img->multi.frames[i].im, decache);
img->multi.cnt = 0;
img->im = NULL;
} else if (img->im != NULL) {
- imlib_context_set_image(img->im);
- free_img();
+ img_free(img->im, decache);
img->im = NULL;
}
}
diff --git a/nsxiv.h b/nsxiv.h
index e68b215..949fc4b 100644
--- a/nsxiv.h
+++ b/nsxiv.h
@@ -217,6 +217,7 @@ struct img {
void img_init(img_t*, win_t*);
bool img_load(img_t*, const fileinfo_t*);
+CLEANUP void img_free(Imlib_Image, bool);
CLEANUP void img_close(img_t*, bool);
void img_render(img_t*);
bool img_fit_win(img_t*, scalemode_t);
diff --git a/thumbs.c b/thumbs.c
index d5a6c6b..d2f91a7 100644
--- a/thumbs.c
+++ b/thumbs.c
@@ -179,12 +179,8 @@ CLEANUP void tns_free(tns_t *tns)
int i;
if (tns->thumbs != NULL) {
- for (i = 0; i < *tns->cnt; i++) {
- if (tns->thumbs[i].im != NULL) {
- imlib_context_set_image(tns->thumbs[i].im);
- imlib_free_image();
- }
- }
+ for (i = 0; i < *tns->cnt; i++)
+ img_free(tns->thumbs[i].im, false);
free(tns->thumbs);
tns->thumbs = NULL;
}
@@ -233,12 +229,8 @@ bool tns_load(tns_t *tns, int n, bool force, bool cache_only)
return false;
t = &tns->thumbs[n];
-
- if (t->im != NULL) {
- imlib_context_set_image(t->im);
- imlib_free_image();
- t->im = NULL;
- }
+ img_free(t->im, false);
+ t->im = NULL;
if (!force) {
if ((im = tns_cache_load(file->path, &force)) != NULL) {
@@ -363,11 +355,8 @@ void tns_unload(tns_t *tns, int n)
assert(n >= 0 && n < *tns->cnt);
t = &tns->thumbs[n];
- if (t->im != NULL) {
- imlib_context_set_image(t->im);
- imlib_free_image();
- t->im = NULL;
- }
+ img_free(t->im, false);
+ t->im = NULL;
}
static void tns_check_view(tns_t *tns, bool scrolled)