summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--kernel/kls_jpeg2000/fmt_codec_jpeg2000.cpp66
1 files 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])