From b9166551ac620382a82b79124ff969b1d9e5b340 Mon Sep 17 00:00:00 2001 From: Kim Woelders Date: Mon, 25 Nov 2019 18:50:55 +0100 Subject: [PATCH] Add infrastructure for new loader entry - load2() --- src/lib/image.c | 52 ++++++++++++++++++++++++++--- src/lib/image.h | 1 + src/lib/loaders.c | 6 ++-- src/lib/loaders.h | 1 + src/modules/loaders/loader_common.h | 1 + 5 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/lib/image.c b/src/lib/image.c index 75086ef..24cd1cc 100644 --- a/src/lib/image.c +++ b/src/lib/image.c @@ -547,10 +547,32 @@ __imlib_LoadImageWrapper(const ImlibLoader * l, ImlibImage * im, int load_data) { int rc; - if (im->lc) - rc = l->load(im, im->lc->progress, im->lc->granularity, 1); + if (l->load2) + { + FILE *fp = NULL; + + if (!im->fp) + { + fp = im->fp = fopen(im->real_file, "rb"); + if (!im->fp) + return 0; + } + rc = l->load2(im, load_data); + + if (fp) + fclose(fp); + } + else if (l->load) + { + if (im->lc) + rc = l->load(im, im->lc->progress, im->lc->granularity, 1); + else + rc = l->load(im, NULL, 0, load_data); + } else - rc = l->load(im, NULL, 0, load_data); + { + return 0; + } if (rc == 0) { @@ -597,6 +619,7 @@ __imlib_LoadEmbedded(ImlibLoader * l, ImlibImage * im, const char *file, { int rc; char *file_save; + FILE *fp_save; if (!l || !im) return 0; @@ -604,9 +627,12 @@ __imlib_LoadEmbedded(ImlibLoader * l, ImlibImage * im, const char *file, /* remember the original filename */ file_save = im->real_file; im->real_file = strdup(file); + fp_save = im->fp; + im->fp = NULL; rc = __imlib_LoadImageWrapper(l, im, load_data); + im->fp = fp_save; free(im->real_file); im->real_file = file_save; @@ -676,12 +702,24 @@ __imlib_LoadImage(const char *file, ImlibProgressFunction progress, im->key = __imlib_FileKey(file); } + im->fp = fopen(im->real_file, "rb"); + if (!im->fp) + { + if (er) + *er = __imlib_ErrorFromErrno(errno, 0); + __imlib_ConsumeImage(im); + return NULL; + } + im->moddate = __imlib_FileModDate(im->real_file); im->data_memory_func = imlib_context_get_image_data_memory_function(); if (progress) - __imlib_LoadCtxInit(im, &ilc, progress, progress_granularity); + { + __imlib_LoadCtxInit(im, &ilc, progress, progress_granularity); + immediate_load = 1; + } /* ok - just check all our loaders are up to date */ __imlib_RescanLoaders(); @@ -710,6 +748,7 @@ __imlib_LoadImage(const char *file, ImlibProgressFunction progress, /* if its not the best loader that already failed - try load */ if (l == best_loader) continue; + rewind(im->fp); loader_ret = __imlib_LoadImageWrapper(l, im, immediate_load); if (loader_ret > 0) break; @@ -732,6 +771,9 @@ __imlib_LoadImage(const char *file, ImlibProgressFunction progress, im->lc = NULL; + fclose(im->fp); + im->fp = NULL; + /* all loaders have been tried and they all failed. free the skeleton */ /* image struct we had and return NULL */ if (loader_ret <= 0) @@ -759,7 +801,7 @@ __imlib_LoadImage(const char *file, ImlibProgressFunction progress, int __imlib_LoadImageData(ImlibImage * im) { - if ((!(im->data)) && (im->loader) && (im->loader->load)) + if (!im->data && im->loader) if (__imlib_LoadImageWrapper(im->loader, im, 1) == 0) return 1; /* Load failed */ return im->data == NULL; diff --git a/src/lib/image.h b/src/lib/image.h index 045c40b..11e2af1 100644 --- a/src/lib/image.h +++ b/src/lib/image.h @@ -70,6 +70,7 @@ struct _imlibimage { char *key; ImlibImageDataMemoryFunction data_memory_func; ImlibLdCtx *lc; + FILE *fp; }; #ifdef BUILD_X11 diff --git a/src/lib/loaders.c b/src/lib/loaders.c index 56518d5..4b0aa77 100644 --- a/src/lib/loaders.c +++ b/src/lib/loaders.c @@ -37,11 +37,12 @@ __imlib_ProduceLoader(char *file) return NULL; } l->load = dlsym(l->handle, "load"); + l->load2 = dlsym(l->handle, "load2"); l->save = dlsym(l->handle, "save"); l_formats = dlsym(l->handle, "formats"); /* each loader must provide formats() and at least load() or save() */ - if (!l_formats || (!l->load && !l->save)) + if (!l_formats || !(l->load2 || l->load || l->save)) { dlclose(l->handle); free(l); @@ -188,7 +189,8 @@ __imlib_FindBestLoaderForFormat(const char *format, int for_save) if (strcasecmp(l->formats[i], format) == 0) { /* does it provide the function we need? */ - if ((for_save && l->save) || (!for_save && l->load)) + if ((for_save && l->save) || + (!for_save && (l->load || l->load2))) goto done; } } diff --git a/src/lib/loaders.h b/src/lib/loaders.h index 6d0dead..1f31799 100644 --- a/src/lib/loaders.h +++ b/src/lib/loaders.h @@ -15,6 +15,7 @@ struct _imlibloader { ImlibProgressFunction progress, char progress_granularity); ImlibLoader *next; + int (*load2)(ImlibImage * im, int load_data); }; #endif /* __LOADERS */ diff --git a/src/modules/loaders/loader_common.h b/src/modules/loaders/loader_common.h index 5adce56..d1716c9 100644 --- a/src/modules/loaders/loader_common.h +++ b/src/modules/loaders/loader_common.h @@ -7,6 +7,7 @@ __EXPORT__ char load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity, char load_data); +__EXPORT__ int load2(ImlibImage * im, int load_data); __EXPORT__ char save(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity); __EXPORT__ void formats(ImlibLoader * l);