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)
{
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;
}
}

View File

@ -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);

View File

@ -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();
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();
img_free(t->im, false);
t->im = NULL;
}
}
static void tns_check_view(tns_t *tns, bool scrolled)