From 51f42b6fdfd52818b9ce5bbb1eae2b472dd2f482 Mon Sep 17 00:00:00 2001 From: mio Date: Tue, 1 Oct 2024 16:40:37 +1000 Subject: [PATCH] Use JasPer 3 library initialization routines Update to use the jas_init_library() and jas_init_thread() functions as JasPer 3 deprecated jas_init and changed its behaviour to not register jas_cleanup() as an atexit callback. Signed-off-by: mio (cherry picked from commit 508ae3fc06f4c4407eab9fc38e951d168a2e0519) --- kernel/kls_jpeg2000/fmt_codec_jpeg2000.cpp | 66 +++++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/kernel/kls_jpeg2000/fmt_codec_jpeg2000.cpp b/kernel/kls_jpeg2000/fmt_codec_jpeg2000.cpp index 4b54009..b5a0eee 100644 --- a/kernel/kls_jpeg2000/fmt_codec_jpeg2000.cpp +++ b/kernel/kls_jpeg2000/fmt_codec_jpeg2000.cpp @@ -63,14 +63,66 @@ * */ +static bool jasperInitialized = false; + +static void initializeJasper() +{ +#if (JAS_VERSION_MAJOR >= 3) + jas_conf_clear(); + + // Limit JasPer memory usage to at most 512 MB + size_t memoryLimit = (512 * 1024) * 1024; + size_t jasperTotalMem = jas_get_total_mem_size(); + if (!jasperTotalMem) + { + jasperTotalMem = JAS_DEFAULT_MAX_MEM_USAGE; + } + memoryLimit = (memoryLimit < jasperTotalMem) ? memoryLimit : jasperTotalMem; + jas_conf_set_max_mem_usage(memoryLimit); + + if (!jas_init_library()) + { + if (!jas_init_thread()) + { + jasperInitialized = true; + } + else + { + jas_cleanup_library(); + } + } +#else + if (!jas_init()) + { + jasperInitialized = true; + } +#endif +} + +static void cleanupJasper() +{ +#if (JAS_VERSION_MAJOR >= 3) + if (jasperInitialized) + { + jas_cleanup_thread(); + jas_cleanup_library(); + } +#else + if (jasperInitialized) + { + jas_cleanup(); + } +#endif +} + fmt_codec::fmt_codec() : fmt_codec_base() { - jas_init(); + initializeJasper(); } fmt_codec::~fmt_codec() { - jas_cleanup(); + cleanupJasper(); } void fmt_codec::options(codec_options *o) @@ -101,6 +153,11 @@ s32 fmt_codec::read_init(const std::string &file) gs.data[1] = 0; gs.data[2] = 0; + if (!jasperInitialized) + { + return SQE_NOTOK; + } + in = jas_stream_fopen(file.c_str(), "rb"); if(!in) @@ -253,6 +310,11 @@ s32 fmt_codec::read_scanline(RGBA *scan) void fmt_codec::read_close() { + if (!jasperInitialized) + { + return; + } + for(s32 cmptno = 0; cmptno < 3; ++cmptno) { if (gs.data[cmptno])