/* * Copyright (c) 2001-2002 Michael David Adams. * All rights reserved. */ /* __START_OF_JASPER_LICENSE__ * * JasPer License Version 2.0 * * Copyright (c) 2001-2006 Michael David Adams * Copyright (c) 1999-2000 Image Power, Inc. * Copyright (c) 1999-2000 The University of British Columbia * * All rights reserved. * * Permission is hereby granted, free of charge, to any person (the * "User") obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, and/or sell copies of the Software, and to permit * persons to whom the Software is furnished to do so, subject to the * following conditions: * * 1. The above copyright notices and this permission notice (which * includes the disclaimer below) shall be included in all copies or * substantial portions of the Software. * * 2. The name of a copyright holder shall not be used to endorse or * promote products derived from the Software without specific prior * written permission. * * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. * * __END_OF_JASPER_LICENSE__ */ /******************************************************************************\* Includes.\******************************************************************************/ #define JAS_FOR_INTERNAL_USE_ONLY #include "jasper/jas_init.h"#include "jasper/jas_image.h"#include "jasper/jas_malloc.h"#include "jasper/jas_debug.h"#include "jasper/jas_string.h"#include "jasper/jas_thread.h" /******************************************************************************\* Types.\******************************************************************************/ /*!@brief Entry in image format table.*/typedef struct { /*! A unique name identifying the format. */ const char *name; /*! A short description of the format. */ const char *desc; /*! A whitespace delimited list of file extensions associated with the format. */ const char *exts; /*! The operations for the format (e.g., encode, decode, and validate). */ const jas_image_fmtops_t ops; /*! A boolean flag indicating if the format is enabled. */ int enabled; } jas_image_fmt_t; /*User-configurable settings for library.This is for internal library use only.*/typedef struct { /* A boolean flag indicating if the library has been configured by invoking the jas_conf_clear function. */ bool initialized; /* A boolean flag indicating if the library is potentially going to be used by more than one thread. */ bool multithread; /* The allocator to be used by the library. */ jas_allocator_t *allocator; /* A boolean flag indicating if the allocator should be accessed through a wrapper that allows memory usage to be tracked and limited. */ bool enable_allocator_wrapper; /* The maximum amount of memory to be used by the library if the allocator wrapper is used. */ size_t max_mem; bool max_mem_valid; /* The image format information to be used to populate the image format table in newly created contexts. */ const jas_image_fmt_t *image_formats; size_t num_image_formats; /* The maximum number of samples allowable in an image to be decoded to be used in newly created contexts. */ size_t dec_default_max_samples; /* The level of debugging checks/output enabled by the library for newly created contexts. A larger value corresponds to a greater level of debugging checks/output. */ int debug_level; /* The function used to output error/warning/informational messages for newly created contexts. */ int (*vlogmsgf)(jas_logtype_t type, const char *format, va_list ap); } jas_conf_t; typedef struct { /* A copy of the run-time library configuration settings. */ jas_conf_t conf; /* A flag indicating if the library is initialized.*/ bool initialized; /* The number of threads currently using the library. */ size_t num_active_threads; /* If JAS_THREADS is not defined, this pointer is used for both the global context and default context. Otherwise, this pointer is only used for the global context. */ jas_ctx_t *ctx; /* The storage for the global context. */ jas_ctx_t ctx_buf; #if defined(JAS_THREADS) /* A mutex to protect this data structure. */ jas_mutex_t lock; /* The TSS for the current context pointer. */ jas_tss_t cur_ctx_tss; /* The TSS for the default context pointer. */ jas_tss_t default_ctx_tss; #endif } jas_global_t; /******************************************************************************\* Function prototypes.\******************************************************************************/ //static jas_conf_t *jas_get_conf_ptr(void);void jas_ctx_init(jas_ctx_t *ctx);static int jas_init_codecs(jas_ctx_t *ctx);void jas_ctx_cleanup(jas_ctx_t *ctx);void jas_set_ctx(jas_ctx_t *ctx);void jas_set_default_ctx(jas_ctx_t *ctx);jas_ctx_t *jas_ctx_create(void);void jas_ctx_destroy(jas_ctx_t *ctx);jas_ctx_t *jas_get_ctx_internal(void);jas_ctx_t *jas_get_default_ctx(void); /******************************************************************************\* Codec Table.\******************************************************************************/ static const jas_image_fmt_t jas_image_fmts[] = { #if defined(JAS_INCLUDE_PNM_CODEC) { .name = "pnm", .desc = "Portable Graymap/Pixmap (PNM)", .exts = "pnm pbm pgm ppm", .ops = { .decode = pnm_decode, .encode = pnm_encode, .validate = pnm_validate }, .enabled = JAS_ENABLE_PNM_CODEC, },#endif #if defined(JAS_INCLUDE_BMP_CODEC) { .name = "bmp", .desc = "Microsoft Bitmap (BMP)", .exts = "bmp", .ops = { .decode = bmp_decode, .encode = bmp_encode, .validate = bmp_validate }, .enabled = JAS_ENABLE_BMP_CODEC, },#endif #if defined(JAS_INCLUDE_RAS_CODEC) { .name = "ras", .desc = "Sun Rasterfile (RAS)", .exts = "ras", .ops = { .decode = ras_decode, .encode = ras_encode, .validate = ras_validate }, .enabled = JAS_ENABLE_RAS_CODEC, },#endif #if defined(JAS_INCLUDE_PGX_CODEC) { .name = "pgx", .desc = "JPEG-2000 VM Format (PGX)", .exts = "pgx", .ops = { .decode = pgx_decode, .encode = pgx_encode, .validate = pgx_validate }, .enabled = JAS_ENABLE_PGX_CODEC, },#endif #if defined(JAS_INCLUDE_JPC_CODEC) || defined(JAS_INCLUDE_JP2_CODEC) { .name = "jpc", .desc = "JPEG-2000 Code Stream Syntax (ISO/IEC 15444-1)", .exts = "jpc", .ops = { .decode = jpc_decode, .encode = jpc_encode, .validate = jpc_validate }, .enabled = JAS_ENABLE_JPC_CODEC, },#endif #if defined(JAS_INCLUDE_JP2_CODEC) { .name = "jp2", .desc = "JPEG-2000 JP2 File Format Syntax (ISO/IEC 15444-1)", .exts = "jp2", .ops = { .decode = jp2_decode, .encode = jp2_encode, .validate = jp2_validate }, .enabled = JAS_ENABLE_JP2_CODEC, },#endif #if defined(JAS_INCLUDE_MIF_CODEC) && defined(JAS_ENABLE_MIF_CODEC) { .name = "mif", .desc = "My Image Format (MIF)", .exts = "mif", .ops = { .decode = mif_decode, .encode = mif_encode, .validate = mif_validate }, .enabled = JAS_ENABLE_MIF_CODEC, },#endif #if defined(JAS_INCLUDE_JPG_CODEC) { .name = "jpg", .desc = "JPEG (ISO/IEC 10918-1)", .exts = "jpg", .ops = { .decode = jpg_decode, .encode = jpg_encode, .validate = jpg_validate }, .enabled = JAS_ENABLE_JPG_CODEC, },#endif #if defined(JAS_INCLUDE_HEIC_CODEC) { .name = "heic", .desc = "HEIC (ISO/IEC 23008-12)", .exts = "heic heif", .ops = { .decode = jas_heic_decode, .encode = jas_heic_encode, .validate = jas_heic_validate }, .enabled = JAS_ENABLE_HEIC_CODEC, },#endif }; /******************************************************************************\* Configuration and Library Data.\******************************************************************************/ /* MUTABLE_SHARED_STATE_TAG: This is mutable shared state. *//*Various user-configurable settings.*/jas_conf_t jas_conf = { .initialized = 0,}; /* MUTABLE_SHARED_STATE_TAG: This is mutable shared state. */jas_global_t jas_global = { .initialized = 0,#if defined(JAS_THREADS) .lock = JAS_MUTEX_INITIALIZER,#endif}; #if defined(JAS_HAVE_THREAD_LOCAL)_Thread_local jas_ctx_t *jas_cur_ctx = 0;_Thread_local jas_ctx_t *jas_default_ctx = 0;#endif /******************************************************************************\* Library Configuration.\******************************************************************************/ JAS_EXPORTvoid jas_conf_clear(){ jas_conf.multithread = 0; jas_conf.allocator = 0; jas_conf.enable_allocator_wrapper = 1; jas_conf.max_mem = 0; jas_conf.max_mem_valid = 0; jas_conf.num_image_formats = sizeof(jas_image_fmts) / sizeof(jas_image_fmt_t); jas_conf.image_formats = jas_image_fmts;#if defined(JAS_LEGACY_MODE) jas_conf.dec_default_max_samples = JAS_DEC_DEFAULT_MAX_SAMPLES;#else jas_conf.dec_default_max_samples = 0;#endif jas_conf.debug_level = 0; jas_conf.vlogmsgf = jas_vlogmsgf_stderr; //jas_conf.vlogmsgf = jas_vlogmsgf_discard; jas_conf.initialized = 1;} JAS_EXPORTvoid jas_conf_set_multithread(int multithread){ jas_conf.multithread = multithread;} JAS_EXPORTvoid jas_conf_set_allocator(jas_allocator_t *allocator){ jas_conf.allocator = allocator;} #ifdef JAS_COMMENTJAS_EXPORTvoid jas_conf_set_allocator_wrapper(int enabled){ jas_conf.enable_allocator_wrapper = enabled;}#endif JAS_EXPORTvoid jas_conf_set_debug_level(int debug_level){ jas_conf.debug_level = debug_level;} JAS_EXPORTvoid jas_conf_set_max_mem_usage(size_t max_mem){ jas_conf.max_mem = max_mem; jas_conf.max_mem_valid = 1;} JAS_EXPORTvoid jas_conf_set_dec_default_max_samples(size_t n){ jas_conf.dec_default_max_samples = n;} JAS_EXPORTvoid jas_conf_set_vlogmsgf(int (*func)(jas_logtype_t type, const char *, va_list)){ jas_conf.vlogmsgf = func;} /******************************************************************************\* Library Initialization and Cleanup.\******************************************************************************/ /*Note: I think that it should actually be okay for jas_init_library andjas_cleanup_library to be called from different threads, since:the mutex ensures synchronization and also guarantees that only onethread can use the global context at once.*/JAS_EXPORTint jas_init_library(){ int ret = 0;#if defined(JAS_THREADS) bool has_lock = false;#endif #if defined(JAS_THREADS) jas_mutex_lock(&jas_global.lock); has_lock = true;#endif /* The following check requires that library configuration be performed on the same thread as library initialization. */ if (!jas_conf.initialized) { jas_eprintf("FATAL ERROR: " "jas_init_library called before JasPer library configured\n"); abort(); } /* The following check requires that library configuration be performed on the same thread as library initialization. */ assert(jas_conf.initialized); assert(!jas_global.initialized); jas_global.conf = jas_conf; assert(jas_global.conf.initialized);Lines 1–500 of 1033. Load more
/* * Copyright (c) 2001-2002 Michael David Adams. * All rights reserved. */ /* __START_OF_JASPER_LICENSE__ * * JasPer License Version 2.0 * * Copyright (c) 2001-2006 Michael David Adams * Copyright (c) 1999-2000 Image Power, Inc. * Copyright (c) 1999-2000 The University of British Columbia * * All rights reserved. * * Permission is hereby granted, free of charge, to any person (the * "User") obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, and/or sell copies of the Software, and to permit * persons to whom the Software is furnished to do so, subject to the * following conditions: * * 1. The above copyright notices and this permission notice (which * includes the disclaimer below) shall be included in all copies or * substantial portions of the Software. * * 2. The name of a copyright holder shall not be used to endorse or * promote products derived from the Software without specific prior * written permission. * * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. * * __END_OF_JASPER_LICENSE__ */ /******************************************************************************\ * Includes. \******************************************************************************/ #define JAS_FOR_INTERNAL_USE_ONLY #include "jasper/jas_init.h" #include "jasper/jas_image.h" #include "jasper/jas_malloc.h" #include "jasper/jas_debug.h" #include "jasper/jas_string.h" #include "jasper/jas_thread.h" /******************************************************************************\ * Types. \******************************************************************************/ /*! @brief Entry in image format table. */ typedef struct { /*! A unique name identifying the format. */ const char *name; /*! A short description of the format. */ const char *desc; /*! A whitespace delimited list of file extensions associated with the format. */ const char *exts; /*! The operations for the format (e.g., encode, decode, and validate). */ const jas_image_fmtops_t ops; /*! A boolean flag indicating if the format is enabled. */ int enabled; } jas_image_fmt_t; /* User-configurable settings for library. This is for internal library use only. */ typedef struct { /* A boolean flag indicating if the library has been configured by invoking the jas_conf_clear function. */ bool initialized; /* A boolean flag indicating if the library is potentially going to be used by more than one thread. */ bool multithread; /* The allocator to be used by the library. */ jas_allocator_t *allocator; /* A boolean flag indicating if the allocator should be accessed through a wrapper that allows memory usage to be tracked and limited. */ bool enable_allocator_wrapper; /* The maximum amount of memory to be used by the library if the allocator wrapper is used. */ size_t max_mem; bool max_mem_valid; /* The image format information to be used to populate the image format table in newly created contexts. */ const jas_image_fmt_t *image_formats; size_t num_image_formats; /* The maximum number of samples allowable in an image to be decoded to be used in newly created contexts. */ size_t dec_default_max_samples; /* The level of debugging checks/output enabled by the library for newly created contexts. A larger value corresponds to a greater level of debugging checks/output. */ int debug_level; /* The function used to output error/warning/informational messages for newly created contexts. */ int (*vlogmsgf)(jas_logtype_t type, const char *format, va_list ap); } jas_conf_t; typedef struct { /* A copy of the run-time library configuration settings. */ jas_conf_t conf; /* A flag indicating if the library is initialized.*/ bool initialized; /* The number of threads currently using the library. */ size_t num_active_threads; /* If JAS_THREADS is not defined, this pointer is used for both the global context and default context. Otherwise, this pointer is only used for the global context. */ jas_ctx_t *ctx; /* The storage for the global context. */ jas_ctx_t ctx_buf; #if defined(JAS_THREADS) /* A mutex to protect this data structure. */ jas_mutex_t lock; /* The TSS for the current context pointer. */ jas_tss_t cur_ctx_tss; /* The TSS for the default context pointer. */ jas_tss_t default_ctx_tss; #endif } jas_global_t; /******************************************************************************\ * Function prototypes. \******************************************************************************/ //static jas_conf_t *jas_get_conf_ptr(void); void jas_ctx_init(jas_ctx_t *ctx); static int jas_init_codecs(jas_ctx_t *ctx); void jas_ctx_cleanup(jas_ctx_t *ctx); void jas_set_ctx(jas_ctx_t *ctx); void jas_set_default_ctx(jas_ctx_t *ctx); jas_ctx_t *jas_ctx_create(void); void jas_ctx_destroy(jas_ctx_t *ctx); jas_ctx_t *jas_get_ctx_internal(void); jas_ctx_t *jas_get_default_ctx(void); /******************************************************************************\ * Codec Table. \******************************************************************************/ static const jas_image_fmt_t jas_image_fmts[] = { #if defined(JAS_INCLUDE_PNM_CODEC) { .name = "pnm", .desc = "Portable Graymap/Pixmap (PNM)", .exts = "pnm pbm pgm ppm", .ops = { .decode = pnm_decode, .encode = pnm_encode, .validate = pnm_validate }, .enabled = JAS_ENABLE_PNM_CODEC, }, #endif #if defined(JAS_INCLUDE_BMP_CODEC) { .name = "bmp", .desc = "Microsoft Bitmap (BMP)", .exts = "bmp", .ops = { .decode = bmp_decode, .encode = bmp_encode, .validate = bmp_validate }, .enabled = JAS_ENABLE_BMP_CODEC, }, #endif #if defined(JAS_INCLUDE_RAS_CODEC) { .name = "ras", .desc = "Sun Rasterfile (RAS)", .exts = "ras", .ops = { .decode = ras_decode, .encode = ras_encode, .validate = ras_validate }, .enabled = JAS_ENABLE_RAS_CODEC, }, #endif #if defined(JAS_INCLUDE_PGX_CODEC) { .name = "pgx", .desc = "JPEG-2000 VM Format (PGX)", .exts = "pgx", .ops = { .decode = pgx_decode, .encode = pgx_encode, .validate = pgx_validate }, .enabled = JAS_ENABLE_PGX_CODEC, }, #endif #if defined(JAS_INCLUDE_JPC_CODEC) || defined(JAS_INCLUDE_JP2_CODEC) { .name = "jpc", .desc = "JPEG-2000 Code Stream Syntax (ISO/IEC 15444-1)", .exts = "jpc", .ops = { .decode = jpc_decode, .encode = jpc_encode, .validate = jpc_validate }, .enabled = JAS_ENABLE_JPC_CODEC, }, #endif #if defined(JAS_INCLUDE_JP2_CODEC) { .name = "jp2", .desc = "JPEG-2000 JP2 File Format Syntax (ISO/IEC 15444-1)", .exts = "jp2", .ops = { .decode = jp2_decode, .encode = jp2_encode, .validate = jp2_validate }, .enabled = JAS_ENABLE_JP2_CODEC, }, #endif #if defined(JAS_INCLUDE_MIF_CODEC) && defined(JAS_ENABLE_MIF_CODEC) { .name = "mif", .desc = "My Image Format (MIF)", .exts = "mif", .ops = { .decode = mif_decode, .encode = mif_encode, .validate = mif_validate }, .enabled = JAS_ENABLE_MIF_CODEC, }, #endif #if defined(JAS_INCLUDE_JPG_CODEC) { .name = "jpg", .desc = "JPEG (ISO/IEC 10918-1)", .exts = "jpg", .ops = { .decode = jpg_decode, .encode = jpg_encode, .validate = jpg_validate }, .enabled = JAS_ENABLE_JPG_CODEC, }, #endif #if defined(JAS_INCLUDE_HEIC_CODEC) { .name = "heic", .desc = "HEIC (ISO/IEC 23008-12)", .exts = "heic heif", .ops = { .decode = jas_heic_decode, .encode = jas_heic_encode, .validate = jas_heic_validate }, .enabled = JAS_ENABLE_HEIC_CODEC, }, #endif }; /******************************************************************************\ * Configuration and Library Data. \******************************************************************************/ /* MUTABLE_SHARED_STATE_TAG: This is mutable shared state. */ /* Various user-configurable settings. */ jas_conf_t jas_conf = { .initialized = 0, }; /* MUTABLE_SHARED_STATE_TAG: This is mutable shared state. */ jas_global_t jas_global = { .initialized = 0, #if defined(JAS_THREADS) .lock = JAS_MUTEX_INITIALIZER, #endif }; #if defined(JAS_HAVE_THREAD_LOCAL) _Thread_local jas_ctx_t *jas_cur_ctx = 0; _Thread_local jas_ctx_t *jas_default_ctx = 0; #endif /******************************************************************************\ * Library Configuration. \******************************************************************************/ JAS_EXPORT void jas_conf_clear() { jas_conf.multithread = 0; jas_conf.allocator = 0; jas_conf.enable_allocator_wrapper = 1; jas_conf.max_mem = 0; jas_conf.max_mem_valid = 0; jas_conf.num_image_formats = sizeof(jas_image_fmts) / sizeof(jas_image_fmt_t); jas_conf.image_formats = jas_image_fmts; #if defined(JAS_LEGACY_MODE) jas_conf.dec_default_max_samples = JAS_DEC_DEFAULT_MAX_SAMPLES; #else jas_conf.dec_default_max_samples = 0; #endif jas_conf.debug_level = 0; jas_conf.vlogmsgf = jas_vlogmsgf_stderr; //jas_conf.vlogmsgf = jas_vlogmsgf_discard; jas_conf.initialized = 1; } JAS_EXPORT void jas_conf_set_multithread(int multithread) { jas_conf.multithread = multithread; } JAS_EXPORT void jas_conf_set_allocator(jas_allocator_t *allocator) { jas_conf.allocator = allocator; } #ifdef JAS_COMMENT JAS_EXPORT void jas_conf_set_allocator_wrapper(int enabled) { jas_conf.enable_allocator_wrapper = enabled; } #endif JAS_EXPORT void jas_conf_set_debug_level(int debug_level) { jas_conf.debug_level = debug_level; } JAS_EXPORT void jas_conf_set_max_mem_usage(size_t max_mem) { jas_conf.max_mem = max_mem; jas_conf.max_mem_valid = 1; } JAS_EXPORT void jas_conf_set_dec_default_max_samples(size_t n) { jas_conf.dec_default_max_samples = n; } JAS_EXPORT void jas_conf_set_vlogmsgf(int (*func)(jas_logtype_t type, const char *, va_list)) { jas_conf.vlogmsgf = func; } /******************************************************************************\ * Library Initialization and Cleanup. \******************************************************************************/ /* Note: I think that it should actually be okay for jas_init_library and jas_cleanup_library to be called from different threads, since: the mutex ensures synchronization and also guarantees that only one thread can use the global context at once. */ JAS_EXPORT int jas_init_library() { int ret = 0; #if defined(JAS_THREADS) bool has_lock = false; #endif #if defined(JAS_THREADS) jas_mutex_lock(&jas_global.lock); has_lock = true; #endif /* The following check requires that library configuration be performed on the same thread as library initialization. */ if (!jas_conf.initialized) { jas_eprintf("FATAL ERROR: " "jas_init_library called before JasPer library configured\n"); abort(); } /* The following check requires that library configuration be performed on the same thread as library initialization. */ assert(jas_conf.initialized); assert(!jas_global.initialized); jas_global.conf = jas_conf; assert(jas_global.conf.initialized);