introduce img_free()

this removes some repetitive code.
This commit is contained in:
NRK 2023-07-02 20:15:53 +06:00
parent 69d4957a92
commit 53a43cb388
3 changed files with 24 additions and 40 deletions

40
image.c
View file

@ -148,12 +148,10 @@ void exif_auto_orientate(const fileinfo_t *file)
static void img_multiframe_context_set(img_t *img) static void img_multiframe_context_set(img_t *img)
{ {
if (img->multi.cnt > 1) { if (img->multi.cnt > 1) {
imlib_context_set_image(img->im); img_free(img->im, false);
imlib_free_image();
img->im = img->multi.frames[0].im; img->im = img->multi.frames[0].im;
} else if (img->multi.cnt == 1) { } else if (img->multi.cnt == 1) {
imlib_context_set_image(img->multi.frames[0].im); img_free(img->multi.frames[0].im, false);
imlib_free_image();
img->multi.cnt = 0; 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 || if (canvas == NULL || frame == NULL || finfo.frame_count != (int)fcnt ||
finfo.canvas_w != img->w || finfo.canvas_h != img->h) finfo.canvas_w != img->w || finfo.canvas_h != img->h)
{ {
if (frame != NULL) { img_free(frame, false);
imlib_context_set_image(frame); img_free(canvas, false);
imlib_free_image();
}
if (canvas != NULL) {
imlib_context_set_image(canvas);
imlib_free_image();
}
error(0, 0, "%s: failed to load frame %d", file->name, n); error(0, 0, "%s: failed to load frame %d", file->name, n);
break; 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->frames[m->cnt].delay = finfo.frame_delay ? finfo.frame_delay : DEF_ANIM_DELAY;
m->length += m->frames[m->cnt].delay; m->length += m->frames[m->cnt].delay;
m->cnt++; m->cnt++;
imlib_context_set_image(frame); img_free(frame, false);
imlib_free_image();
} }
imlib_context_set_image(blank); img_free(blank, false);
imlib_free_image();
img_multiframe_context_set(img); img_multiframe_context_set(img);
imlib_context_set_color_modifier(img->cmod); /* restore cmod */ imlib_context_set_color_modifier(img->cmod); /* restore cmod */
return m->cnt > 0; return m->cnt > 0;
@ -632,21 +622,25 @@ bool img_load(img_t *img, const fileinfo_t *file)
return true; 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) CLEANUP void img_close(img_t *img, bool decache)
{ {
unsigned int i; unsigned int i;
void (*free_img)(void) = decache ? imlib_free_image_and_decache : imlib_free_image;
if (img->multi.cnt > 0) { if (img->multi.cnt > 0) {
for (i = 0; i < img->multi.cnt; i++) { for (i = 0; i < img->multi.cnt; i++)
imlib_context_set_image(img->multi.frames[i].im); img_free(img->multi.frames[i].im, decache);
free_img();
}
img->multi.cnt = 0; img->multi.cnt = 0;
img->im = NULL; img->im = NULL;
} else if (img->im != NULL) { } else if (img->im != NULL) {
imlib_context_set_image(img->im); img_free(img->im, decache);
free_img();
img->im = NULL; img->im = NULL;
} }
} }

View file

@ -217,6 +217,7 @@ struct img {
void img_init(img_t*, win_t*); void img_init(img_t*, win_t*);
bool img_load(img_t*, const fileinfo_t*); bool img_load(img_t*, const fileinfo_t*);
CLEANUP void img_free(Imlib_Image, bool);
CLEANUP void img_close(img_t*, bool); CLEANUP void img_close(img_t*, bool);
void img_render(img_t*); void img_render(img_t*);
bool img_fit_win(img_t*, scalemode_t); bool img_fit_win(img_t*, scalemode_t);

View file

@ -179,12 +179,8 @@ CLEANUP void tns_free(tns_t *tns)
int i; int i;
if (tns->thumbs != NULL) { if (tns->thumbs != NULL) {
for (i = 0; i < *tns->cnt; i++) { for (i = 0; i < *tns->cnt; i++)
if (tns->thumbs[i].im != NULL) { img_free(tns->thumbs[i].im, false);
imlib_context_set_image(tns->thumbs[i].im);
imlib_free_image();
}
}
free(tns->thumbs); free(tns->thumbs);
tns->thumbs = NULL; tns->thumbs = NULL;
} }
@ -233,12 +229,8 @@ bool tns_load(tns_t *tns, int n, bool force, bool cache_only)
return false; return false;
t = &tns->thumbs[n]; t = &tns->thumbs[n];
img_free(t->im, false);
if (t->im != NULL) {
imlib_context_set_image(t->im);
imlib_free_image();
t->im = NULL; t->im = NULL;
}
if (!force) { if (!force) {
if ((im = tns_cache_load(file->path, &force)) != NULL) { if ((im = tns_cache_load(file->path, &force)) != NULL) {
@ -363,12 +355,9 @@ void tns_unload(tns_t *tns, int n)
assert(n >= 0 && n < *tns->cnt); assert(n >= 0 && n < *tns->cnt);
t = &tns->thumbs[n]; t = &tns->thumbs[n];
if (t->im != NULL) { img_free(t->im, false);
imlib_context_set_image(t->im);
imlib_free_image();
t->im = NULL; t->im = NULL;
} }
}
static void tns_check_view(tns_t *tns, bool scrolled) static void tns_check_view(tns_t *tns, bool scrolled)
{ {