From 9e47dfdfad9b0a646b30a1b58bd89f5463a00e80 Mon Sep 17 00:00:00 2001 From: Kimball Thurston Date: Sun, 21 May 2023 21:12:42 +1200 Subject: [PATCH] prepare for new version of libdeflate with thread-safe alloc function mechanism (#1425) Removes all other calls to internal_exr_alloc if there is a context, allowing full memory allocator overload without setting global functions. Also cleanup code formatting Signed-off-by: Kimball Thurston --- cmake/OpenEXRSetup.cmake | 10 +- src/lib/OpenEXR/ImfDwaCompressor.cpp | 6 + src/lib/OpenEXR/ImfIDManifest.cpp | 2 + src/lib/OpenEXR/ImfPxr24Compressor.cpp | 2 + src/lib/OpenEXR/ImfZip.cpp | 2 + src/lib/OpenEXRCore/chunk.c | 11 +- src/lib/OpenEXRCore/compression.c | 115 ++++--- src/lib/OpenEXRCore/context.c | 5 +- src/lib/OpenEXRCore/debug.c | 18 +- src/lib/OpenEXRCore/decoding.c | 5 +- src/lib/OpenEXRCore/encoding.c | 2 +- src/lib/OpenEXRCore/internal_b44.c | 2 +- src/lib/OpenEXRCore/internal_compress.h | 7 +- src/lib/OpenEXRCore/internal_cpuid.h | 26 +- .../OpenEXRCore/internal_dwa_channeldata.h | 16 +- src/lib/OpenEXRCore/internal_dwa_classifier.h | 110 +++---- src/lib/OpenEXRCore/internal_dwa_compressor.h | 288 +++++++++--------- src/lib/OpenEXRCore/internal_dwa_decoder.h | 34 +-- src/lib/OpenEXRCore/internal_dwa_encoder.h | 37 +-- src/lib/OpenEXRCore/internal_dwa_simd.h | 86 +++--- src/lib/OpenEXRCore/internal_huf.c | 147 +++++---- src/lib/OpenEXRCore/internal_memory.h | 3 +- src/lib/OpenEXRCore/internal_piz.c | 6 +- src/lib/OpenEXRCore/internal_pxr24.c | 10 +- src/lib/OpenEXRCore/internal_rle.c | 8 +- src/lib/OpenEXRCore/internal_structs.c | 12 +- src/lib/OpenEXRCore/internal_structs.h | 28 +- .../OpenEXRCore/internal_win32_file_impl.h | 5 +- src/lib/OpenEXRCore/internal_xdr.h | 4 +- src/lib/OpenEXRCore/internal_zip.c | 84 ++--- src/lib/OpenEXRCore/memory.c | 19 +- src/lib/OpenEXRCore/openexr_compression.h | 26 +- src/lib/OpenEXRCore/openexr_conf.h | 4 +- src/lib/OpenEXRCore/openexr_context.h | 7 +- src/lib/OpenEXRCore/openexr_version.h | 9 +- src/lib/OpenEXRCore/pack.c | 5 +- src/lib/OpenEXRCore/parse_header.c | 24 +- src/lib/OpenEXRCore/part_attr.c | 11 +- src/lib/OpenEXRCore/string.c | 2 +- src/lib/OpenEXRCore/unpack.c | 2 +- src/lib/OpenEXRCore/write_header.c | 4 +- src/test/OpenEXRCoreTest/buffer.cpp | 4 +- src/test/OpenEXRTest/testIDManifest.cpp | 3 +- 43 files changed, 633 insertions(+), 578 deletions(-) diff --git a/cmake/OpenEXRSetup.cmake b/cmake/OpenEXRSetup.cmake index b3c7bc4cd3..dc92e028be 100644 --- a/cmake/OpenEXRSetup.cmake +++ b/cmake/OpenEXRSetup.cmake @@ -177,8 +177,8 @@ if(NOT TARGET PkgConfig::deflate AND NOT deflate_FOUND) endif() include(FetchContent) FetchContent_Declare(Deflate - GIT_REPOSITORY ${OPENEXR_DEFLATE_REPO} - GIT_TAG ${OPENEXR_DEFLATE_TAG} + GIT_REPOSITORY "${OPENEXR_DEFLATE_REPO}" + GIT_TAG "${OPENEXR_DEFLATE_TAG}" GIT_SHALLOW ON ) @@ -206,7 +206,7 @@ if(NOT TARGET PkgConfig::deflate AND NOT deflate_FOUND) # lib/gzip_compress.c # lib/gzip_decompress.c file(READ ${deflate_SOURCE_DIR}/lib/lib_common.h DEFLATE_HIDE) - string(REPLACE default hidden DEFLATE_HIDE "${DEFLATE_HIDE}") + string(REPLACE "visibility(\"default\")" "visibility(\"hidden\")" DEFLATE_HIDE "${DEFLATE_HIDE}") string(REPLACE "__declspec(dllexport)" "/**/" DEFLATE_HIDE "${DEFLATE_HIDE}") file(WRITE ${deflate_SOURCE_DIR}/lib/lib_common.h "${DEFLATE_HIDE}") @@ -245,8 +245,8 @@ if(NOT TARGET Imath::Imath AND NOT Imath_FOUND) endif() include(FetchContent) FetchContent_Declare(Imath - GIT_REPOSITORY ${IMATH_REPO} - GIT_TAG ${IMATH_TAG} + GIT_REPOSITORY "${IMATH_REPO}" + GIT_TAG "${IMATH_TAG}" GIT_SHALLOW ON ) diff --git a/src/lib/OpenEXR/ImfDwaCompressor.cpp b/src/lib/OpenEXR/ImfDwaCompressor.cpp index ed79914402..d47add163d 100644 --- a/src/lib/OpenEXR/ImfDwaCompressor.cpp +++ b/src/lib/OpenEXR/ImfDwaCompressor.cpp @@ -2076,6 +2076,7 @@ DwaCompressor::compress ( { size_t outSize; if (EXR_ERR_SUCCESS != exr_compress_buffer( + nullptr, 9, // TODO: use default??? the old call to zlib had 9 hardcoded _planarUncBuffer[UNKNOWN], *unknownUncompressedSize, @@ -2116,6 +2117,7 @@ DwaCompressor::compress ( size_t sourceLen = *totalAcUncompressedCount * sizeof (unsigned short); size_t destLen; if (EXR_ERR_SUCCESS != exr_compress_buffer( + nullptr, 9, // TODO: use default??? the old call to zlib had 9 hardcoded _packedAcBuffer, sourceLen, @@ -2166,6 +2168,7 @@ DwaCompressor::compress ( size_t dstLen; if (EXR_ERR_SUCCESS != exr_compress_buffer( + nullptr, 9, // TODO: use default??? the old call to zlib had 9 hardcoded _rleBuffer, *rleUncompressedSize, @@ -2403,6 +2406,7 @@ DwaCompressor::uncompress ( } if (EXR_ERR_SUCCESS != exr_uncompress_buffer ( + nullptr, compressedUnknownBuf, unknownCompressedSize, _planarUncBuffer[UNKNOWN], @@ -2447,6 +2451,7 @@ DwaCompressor::uncompress ( size_t destLen; if (EXR_ERR_SUCCESS != exr_uncompress_buffer ( + nullptr, compressedAcBuf, acCompressedSize, _packedAcBuffer, @@ -2518,6 +2523,7 @@ DwaCompressor::uncompress ( size_t dstLen; if (EXR_ERR_SUCCESS != exr_uncompress_buffer ( + nullptr, compressedRleBuf, rleCompressedSize, _rleBuffer, diff --git a/src/lib/OpenEXR/ImfIDManifest.cpp b/src/lib/OpenEXR/ImfIDManifest.cpp index 866c0b996b..433e6a9434 100644 --- a/src/lib/OpenEXR/ImfIDManifest.cpp +++ b/src/lib/OpenEXR/ImfIDManifest.cpp @@ -540,6 +540,7 @@ IDManifest::IDManifest (const CompressedIDManifest& compressed) size_t outSize; size_t inSize = static_cast (compressed._compressedDataSize); if (EXR_ERR_SUCCESS != exr_uncompress_buffer ( + nullptr, compressed._data, inSize, uncomp.data(), @@ -1072,6 +1073,7 @@ CompressedIDManifest::CompressedIDManifest (const IDManifest& manifest) size_t compressedDataSize; _data = (unsigned char*) malloc (compressedBufferSize); if (EXR_ERR_SUCCESS != exr_compress_buffer ( + nullptr, -1, serial.data (), outputSize, diff --git a/src/lib/OpenEXR/ImfPxr24Compressor.cpp b/src/lib/OpenEXR/ImfPxr24Compressor.cpp index 90dfaad897..21172193a6 100644 --- a/src/lib/OpenEXR/ImfPxr24Compressor.cpp +++ b/src/lib/OpenEXR/ImfPxr24Compressor.cpp @@ -344,6 +344,7 @@ Pxr24Compressor::compress ( size_t outSize = exr_compress_max_buffer_size (inBufferSize); if (EXR_ERR_SUCCESS != exr_compress_buffer ( + nullptr, -1, _tmpBuffer, inBufferSize, @@ -371,6 +372,7 @@ Pxr24Compressor::uncompress ( size_t tmpSize = static_cast (_maxScanLineSize * _numScanLines); if (EXR_ERR_SUCCESS != exr_uncompress_buffer( + nullptr, inPtr, inSize, _tmpBuffer, diff --git a/src/lib/OpenEXR/ImfZip.cpp b/src/lib/OpenEXR/ImfZip.cpp index 44ced27466..4121c13b50 100644 --- a/src/lib/OpenEXR/ImfZip.cpp +++ b/src/lib/OpenEXR/ImfZip.cpp @@ -93,6 +93,7 @@ Zip::compress (const char* raw, int rawSize, char* compressed) // size_t outSize; if (EXR_ERR_SUCCESS != exr_compress_buffer ( + nullptr, _zipLevel, _tmpBuffer, rawSize, @@ -328,6 +329,7 @@ Zip::uncompress (const char* compressed, int compressedSize, char* raw) { size_t outSize = 0; if (EXR_ERR_SUCCESS != exr_uncompress_buffer ( + nullptr, compressed, (size_t)compressedSize, _tmpBuffer, diff --git a/src/lib/OpenEXRCore/chunk.c b/src/lib/OpenEXRCore/chunk.c index 06fb3b51c3..304a402ccc 100644 --- a/src/lib/OpenEXRCore/chunk.c +++ b/src/lib/OpenEXRCore/chunk.c @@ -292,7 +292,7 @@ extract_chunk_leader ( rv = ctxt->do_read ( ctxt, data, - (size_t)ntoread * sizeof (int32_t), + (size_t) ntoread * sizeof (int32_t), &nextoffset, NULL, EXR_MUST_READ_ALL); @@ -349,7 +349,7 @@ extract_chunk_leader ( return ctxt->print_error ( ctxt, EXR_ERR_BAD_CHUNK_LEADER, - "Invalid chunk size reconstructing chunk table: found out of range %"PRId64, + "Invalid chunk size reconstructing chunk table: found out of range %" PRId64, leaderdata->deep_data[1]); } leaderdata->packed_size = leaderdata->deep_packed_size; @@ -484,10 +484,7 @@ reconstruct_chunk_table ( chunk_start = curctable[0]; for (int ci = 1; ci < curpart->chunk_count; ++ci) { - if (curctable[ci] > chunk_start) - { - chunk_start = curctable[ci]; - } + if (curctable[ci] > chunk_start) { chunk_start = curctable[ci]; } } rv = extract_chunk_size ( @@ -1266,7 +1263,7 @@ exr_read_tile_chunk_info ( else if (fsize > 0) { uint64_t finpos = dataoff + (uint64_t) tdata[4]; - if (finpos > (uint64_t)fsize) + if (finpos > (uint64_t) fsize) { return pctxt->print_error ( pctxt, diff --git a/src/lib/OpenEXRCore/compression.c b/src/lib/OpenEXRCore/compression.c index db9ea1ef2a..1d2eaea56f 100644 --- a/src/lib/OpenEXRCore/compression.c +++ b/src/lib/OpenEXRCore/compression.c @@ -6,15 +6,23 @@ #include "openexr_compression.h" #include "openexr_base.h" #include "internal_memory.h" +#include "internal_structs.h" #include +#if ( \ + LIBDEFLATE_VERSION_MAJOR > 1 || \ + (LIBDEFLATE_VERSION_MAJOR == 1 && LIBDEFLATE_VERSION_MINOR > 18)) +# define EXR_USE_CONFIG_DEFLATE_STRUCT 1 +#endif + /* value Aras found to be better trade off of speed vs size */ #define EXR_DEFAULT_ZLIB_COMPRESS_LEVEL 4 /**************************************/ -size_t exr_compress_max_buffer_size (size_t in_bytes) +size_t +exr_compress_max_buffer_size (size_t in_bytes) { size_t r, extra; @@ -24,64 +32,71 @@ size_t exr_compress_max_buffer_size (size_t in_bytes) * but is unclear if it actually adds that or not * (see the comment on libdeflate_deflate_compress) */ - if (r > (SIZE_MAX - 9)) - return (size_t)(SIZE_MAX); + if (r > (SIZE_MAX - 9)) return (size_t) (SIZE_MAX); r += 9; /* * old library had uiAdd( uiAdd( in, ceil(in * 0.01) ), 100 ) */ - extra = (in_bytes * (size_t)130); - if (extra < in_bytes) - return (size_t)(SIZE_MAX); - extra /= (size_t)128; + extra = (in_bytes * (size_t) 130); + if (extra < in_bytes) return (size_t) (SIZE_MAX); + extra /= (size_t) 128; - if (extra > (SIZE_MAX - 100)) - return (size_t)(SIZE_MAX); + if (extra > (SIZE_MAX - 100)) return (size_t) (SIZE_MAX); - if (extra > r) - r = extra; + if (extra > r) r = extra; return r; } /**************************************/ -exr_result_t exr_compress_buffer ( - int level, - const void *in, - size_t in_bytes, - void *out, - size_t out_bytes_avail, - size_t *actual_out ) +exr_result_t +exr_compress_buffer ( + exr_const_context_t ctxt, + int level, + const void* in, + size_t in_bytes, + void* out, + size_t out_bytes_avail, + size_t* actual_out) { - struct libdeflate_compressor *comp; + struct libdeflate_compressor* comp; + const struct _internal_exr_context* pctxt = EXR_CCTXT (ctxt); +#ifdef EXR_USE_CONFIG_DEFLATE_STRUCT + struct libdeflate_options opt = { + .sizeof_options = sizeof (struct libdeflate_options), + .malloc_func = pctxt ? pctxt->alloc_fn : internal_exr_alloc, + .free_func = pctxt ? pctxt->free_fn : internal_exr_free}; + +#else + libdeflate_set_memory_allocator ( + pctxt ? pctxt->alloc_fn : internal_exr_alloc, + pctxt ? pctxt->free_fn : internal_exr_free); +#endif if (level < 0) { exr_get_default_zip_compression_level (&level); /* truly unset anywhere */ - if (level < 0) - level = EXR_DEFAULT_ZLIB_COMPRESS_LEVEL; + if (level < 0) level = EXR_DEFAULT_ZLIB_COMPRESS_LEVEL; } - libdeflate_set_memory_allocator (internal_exr_alloc, internal_exr_free); +#ifdef EXR_USE_CONFIG_DEFLATE_STRUCT + comp = libdeflate_alloc_compressor_ex (level, &opt); +#else comp = libdeflate_alloc_compressor (level); +#endif if (comp) { size_t outsz; - outsz = libdeflate_zlib_compress ( - comp, - in, - in_bytes, - out, - out_bytes_avail); + outsz = + libdeflate_zlib_compress (comp, in, in_bytes, out, out_bytes_avail); libdeflate_free_compressor (comp); if (outsz != 0) { - if (actual_out) - *actual_out = outsz; + if (actual_out) *actual_out = outsz; return EXR_ERR_SUCCESS; } return EXR_ERR_OUT_OF_MEMORY; @@ -91,19 +106,33 @@ exr_result_t exr_compress_buffer ( /**************************************/ -exr_result_t exr_uncompress_buffer ( - const void *in, - size_t in_bytes, - void *out, - size_t out_bytes_avail, - size_t *actual_out ) +exr_result_t +exr_uncompress_buffer ( + exr_const_context_t ctxt, + const void* in, + size_t in_bytes, + void* out, + size_t out_bytes_avail, + size_t* actual_out) { - struct libdeflate_decompressor *decomp; - enum libdeflate_result res; - size_t actual_in_bytes; - - libdeflate_set_memory_allocator (internal_exr_alloc, internal_exr_free); + struct libdeflate_decompressor* decomp; + enum libdeflate_result res; + size_t actual_in_bytes; + const struct _internal_exr_context* pctxt = EXR_CCTXT (ctxt); +#ifdef EXR_USE_CONFIG_DEFLATE_STRUCT + struct libdeflate_options opt = { + .sizeof_options = sizeof (struct libdeflate_options), + .malloc_func = pctxt ? pctxt->alloc_fn : internal_exr_alloc, + .free_func = pctxt ? pctxt->free_fn : internal_exr_free}; + + decomp = libdeflate_alloc_decompressor_ex (&opt); +#else + libdeflate_set_memory_allocator ( + pctxt ? pctxt->alloc_fn : internal_exr_alloc, + pctxt ? pctxt->free_fn : internal_exr_free); decomp = libdeflate_alloc_decompressor (); +#endif + if (decomp) { res = libdeflate_zlib_decompress_ex ( @@ -119,12 +148,10 @@ exr_result_t exr_uncompress_buffer ( if (res == LIBDEFLATE_SUCCESS) { - if (in_bytes == actual_in_bytes) - return EXR_ERR_SUCCESS; + if (in_bytes == actual_in_bytes) return EXR_ERR_SUCCESS; /* it's an error to not consume the full buffer, right? */ } return EXR_ERR_CORRUPT_CHUNK; } return EXR_ERR_OUT_OF_MEMORY; } - diff --git a/src/lib/OpenEXRCore/context.c b/src/lib/OpenEXRCore/context.c index a514c4a2f8..466125e5d9 100644 --- a/src/lib/OpenEXRCore/context.c +++ b/src/lib/OpenEXRCore/context.c @@ -113,10 +113,7 @@ process_query_size ( ctxt->file_size = (inits->size_fn) ((exr_const_context_t) ctxt, ctxt->user_data); } - else - { - ctxt->file_size = -1; - } + else { ctxt->file_size = -1; } return EXR_ERR_SUCCESS; } diff --git a/src/lib/OpenEXRCore/debug.c b/src/lib/OpenEXRCore/debug.c index ff54b225d3..7c5fd1c890 100644 --- a/src/lib/OpenEXRCore/debug.c +++ b/src/lib/OpenEXRCore/debug.c @@ -17,8 +17,7 @@ static void print_attr (const exr_attribute_t* a, int verbose) { - if (!a) - return; + if (!a) return; printf ("%s: ", a->name); if (verbose) printf ("%s ", a->type_name); @@ -253,11 +252,11 @@ print_attr (const exr_attribute_t* a, int verbose) case EXR_ATTR_V3D: printf ("[ %g, %g, %g ]", a->v3d->x, a->v3d->y, a->v3d->z); break; - case EXR_ATTR_OPAQUE: - { - uintptr_t faddr_unpack = (uintptr_t)a->opaque->unpack_func_ptr; - uintptr_t faddr_pack = (uintptr_t)a->opaque->pack_func_ptr; - uintptr_t faddr_destroy = (uintptr_t)a->opaque->destroy_unpacked_func_ptr; + case EXR_ATTR_OPAQUE: { + uintptr_t faddr_unpack = (uintptr_t) a->opaque->unpack_func_ptr; + uintptr_t faddr_pack = (uintptr_t) a->opaque->pack_func_ptr; + uintptr_t faddr_destroy = + (uintptr_t) a->opaque->destroy_unpacked_func_ptr; printf ( "(size %d unp size %d hdlrs %p %p %p)", a->opaque->size, @@ -292,10 +291,7 @@ exr_print_context_info (exr_const_context_t ctxt, int verbose) pctxt->is_multipart ? " multipart" : ""); printf (" parts: %d\n", pctxt->num_parts); } - else - { - printf ("File '%s':\n", pctxt->filename.str); - } + else { printf ("File '%s':\n", pctxt->filename.str); } for (int partidx = 0; partidx < pctxt->num_parts; ++partidx) { diff --git a/src/lib/OpenEXRCore/decoding.c b/src/lib/OpenEXRCore/decoding.c index 0f6f897346..322cbd8965 100644 --- a/src/lib/OpenEXRCore/decoding.c +++ b/src/lib/OpenEXRCore/decoding.c @@ -114,10 +114,7 @@ read_uncompressed_direct (exr_decode_pipeline_t* decode) ((uint64_t) (y / decc->y_samples) * (uint64_t) decc->user_line_stride); } - else - { - cdata += (uint64_t) y * (uint64_t) decc->user_line_stride; - } + else { cdata += (uint64_t) y * (uint64_t) decc->user_line_stride; } /* actual read into the output pointer */ rv = pctxt->do_read ( diff --git a/src/lib/OpenEXRCore/encoding.c b/src/lib/OpenEXRCore/encoding.c index 4cbdb06650..b074cceab2 100644 --- a/src/lib/OpenEXRCore/encoding.c +++ b/src/lib/OpenEXRCore/encoding.c @@ -26,7 +26,7 @@ default_compress_chunk (exr_encode_pipeline_t* encode) EXR_TRANSCODE_BUFFER_COMPRESSED, &(encode->compressed_buffer), &(encode->compressed_alloc_size), - exr_compress_max_buffer_size( encode->packed_bytes )); + exr_compress_max_buffer_size (encode->packed_bytes)); if (rv != EXR_ERR_SUCCESS) return pctxt->print_error ( pctxt, diff --git a/src/lib/OpenEXRCore/internal_b44.c b/src/lib/OpenEXRCore/internal_b44.c index b29b118058..30d0f6696d 100644 --- a/src/lib/OpenEXRCore/internal_b44.c +++ b/src/lib/OpenEXRCore/internal_b44.c @@ -389,7 +389,7 @@ compress_b44_impl (exr_encode_pipeline_t* encode, int flat_field) // by 4, then pad the data by repeating the // rightmost column and the bottom row. // - uint16_t* row0, * row1, * row2, * row3; + uint16_t *row0, *row1, *row2, *row3; row0 = (uint16_t*) scratch; row0 += y * nx; diff --git a/src/lib/OpenEXRCore/internal_compress.h b/src/lib/OpenEXRCore/internal_compress.h index 665a0d24cd..360015c120 100644 --- a/src/lib/OpenEXRCore/internal_compress.h +++ b/src/lib/OpenEXRCore/internal_compress.h @@ -12,11 +12,10 @@ uint64_t internal_rle_compress ( void* out, uint64_t outbytes, const void* src, uint64_t srcbytes); void internal_zip_deconstruct_bytes ( - uint8_t* scratch, - const uint8_t* source, - uint64_t count); + uint8_t* scratch, const uint8_t* source, uint64_t count); -void internal_zip_reconstruct_bytes (uint8_t* out, uint8_t* scratch_source, uint64_t count); +void internal_zip_reconstruct_bytes ( + uint8_t* out, uint8_t* scratch_source, uint64_t count); exr_result_t internal_exr_apply_rle (exr_encode_pipeline_t* encode); diff --git a/src/lib/OpenEXRCore/internal_cpuid.h b/src/lib/OpenEXRCore/internal_cpuid.h index 9eb89d011d..86c33f90a5 100644 --- a/src/lib/OpenEXRCore/internal_cpuid.h +++ b/src/lib/OpenEXRCore/internal_cpuid.h @@ -13,7 +13,8 @@ # endif #endif -static inline void check_for_x86_simd (int *f16c, int *avx, int *sse2) +static inline void +check_for_x86_simd (int* f16c, int* avx, int* sse2) { // Only use compiler flags on e2k (MCST Elbrus 2000) architecture #ifdef __e2k__ @@ -32,7 +33,7 @@ static inline void check_for_x86_simd (int *f16c, int *avx, int *sse2) # if defined(__AVX__) && defined(__F16C__) *f16c = 1; - *avx = 1; + *avx = 1; *sse2 = 1; # else # ifdef _WIN32 @@ -57,7 +58,7 @@ static inline void check_for_x86_simd (int *f16c, int *avx, int *sse2) *avx = (regs[2] & (1 << 28)) ? 1 : 0; *f16c = (regs[2] & (1 << 29)) ? 1 : 0; /* sse2 is in EDX bit 26 */ - *sse2 = (regs[3] & (1 << 26)) ? 1 : 0; + *sse2 = (regs[3] & (1 << 26)) ? 1 : 0; if (!osxsave) { @@ -67,12 +68,12 @@ static inline void check_for_x86_simd (int *f16c, int *avx, int *sse2) else { /* check extended control register */ -# if defined(OPENEXR_IMF_HAVE_GCC_INLINE_ASM_AVX) && \ - (defined(_M_X64) || defined(__x86_64__)) - __asm__ __volatile__("xgetbv" - : /* Output */ "=a"(regs[0]), "=d"(regs[3]) - : /* Input */ "c"(0) - : /* Clobber */); +# if defined(OPENEXR_IMF_HAVE_GCC_INLINE_ASM_AVX) && \ + (defined(_M_X64) || defined(__x86_64__)) + __asm__ __volatile__ ("xgetbv" + : /* Output */ "=a"(regs[0]), "=d"(regs[3]) + : /* Input */ "c"(0) + : /* Clobber */); /* eax bit 1 - SSE managed, bit 2 - AVX managed */ if ((regs[0] & 6) != 6) { @@ -80,8 +81,8 @@ static inline void check_for_x86_simd (int *f16c, int *avx, int *sse2) *f16c = 0; } # else - *avx = 0; - *f16c = 0; + *avx = 0; + *f16c = 0; # endif } # endif @@ -93,7 +94,8 @@ static inline void check_for_x86_simd (int *f16c, int *avx, int *sse2) #endif } -static inline int has_native_half (void) +static inline int +has_native_half (void) { #if defined(__x86_64__) || defined(_M_X64) int sse2, avx, f16c; diff --git a/src/lib/OpenEXRCore/internal_dwa_channeldata.h b/src/lib/OpenEXRCore/internal_dwa_channeldata.h index eaf28da9f7..e5207e4a8c 100644 --- a/src/lib/OpenEXRCore/internal_dwa_channeldata.h +++ b/src/lib/OpenEXRCore/internal_dwa_channeldata.h @@ -37,24 +37,28 @@ DctCoderChannelData_construct (DctCoderChannelData* d, exr_pixel_type_t t) } static void -DctCoderChannelData_destroy (DctCoderChannelData* d) +DctCoderChannelData_destroy (void (*free_fn) (void*), DctCoderChannelData* d) { - if (d->_rows) internal_exr_free (d->_rows); + if (d->_rows) free_fn (d->_rows); } static exr_result_t -DctCoderChannelData_push_row (DctCoderChannelData* d, uint8_t* r) +DctCoderChannelData_push_row ( + void* (*alloc_fn) (size_t), + void (*free_fn) (void*), + DctCoderChannelData* d, + uint8_t* r) { if (d->_size == d->_row_alloc_count) { size_t nsize = d->_size == 0 ? 16 : ((d->_size * 3) / 2); - uint8_t** n = internal_exr_alloc (nsize * sizeof (uint8_t*)); + uint8_t** n = alloc_fn (nsize * sizeof (uint8_t*)); if (n) { if (d->_rows) { memcpy (n, d->_rows, sizeof (uint8_t*) * d->_size); - internal_exr_free (d->_rows); + free_fn (d->_rows); } d->_rows = n; d->_row_alloc_count = nsize; @@ -90,7 +94,7 @@ typedef struct _ChannelData int processed; CompressorScheme compression; exr_pixel_type_t planarUncType; - uint8_t _pad[20]; + uint8_t _pad[20]; } ChannelData; /**************************************/ diff --git a/src/lib/OpenEXRCore/internal_dwa_classifier.h b/src/lib/OpenEXRCore/internal_dwa_classifier.h index c4eb2af9f2..e1114baf66 100644 --- a/src/lib/OpenEXRCore/internal_dwa_classifier.h +++ b/src/lib/OpenEXRCore/internal_dwa_classifier.h @@ -22,58 +22,67 @@ typedef struct _Classifier #define DWA_CLASSIFIER_FALSE 0 #define DWA_CLASSIFIER_TRUE 1 +// clang-format off + static Classifier sDefaultChannelRules[] = { - {"R", LOSSY_DCT, EXR_PIXEL_HALF, 0, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, - {"R", LOSSY_DCT, EXR_PIXEL_FLOAT, 0, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, - {"G", LOSSY_DCT, EXR_PIXEL_HALF, 1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, - {"G", LOSSY_DCT, EXR_PIXEL_FLOAT, 1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, - {"B", LOSSY_DCT, EXR_PIXEL_HALF, 2, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, - {"B", LOSSY_DCT, EXR_PIXEL_FLOAT, 2, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, - {"Y", LOSSY_DCT, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, - {"Y", LOSSY_DCT, EXR_PIXEL_FLOAT, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, - {"BY", LOSSY_DCT, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, + {"R", LOSSY_DCT, EXR_PIXEL_HALF, 0, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, + {"R", LOSSY_DCT, EXR_PIXEL_FLOAT, 0, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, + {"G", LOSSY_DCT, EXR_PIXEL_HALF, 1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, + {"G", LOSSY_DCT, EXR_PIXEL_FLOAT, 1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, + {"B", LOSSY_DCT, EXR_PIXEL_HALF, 2, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, + {"B", LOSSY_DCT, EXR_PIXEL_FLOAT, 2, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, + {"Y", LOSSY_DCT, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, + {"Y", LOSSY_DCT, EXR_PIXEL_FLOAT, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, + {"BY", LOSSY_DCT, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, {"BY", LOSSY_DCT, EXR_PIXEL_FLOAT, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, - {"RY", LOSSY_DCT, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, + {"RY", LOSSY_DCT, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, {"RY", LOSSY_DCT, EXR_PIXEL_FLOAT, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, - {"A", RLE, EXR_PIXEL_UINT, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, - {"A", RLE, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, - {"A", RLE, EXR_PIXEL_FLOAT, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}}; + {"A", RLE, EXR_PIXEL_UINT, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, + {"A", RLE, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}, + {"A", RLE, EXR_PIXEL_FLOAT, -1, DWA_CLASSIFIER_FALSE, DWA_CLASSIFIER_TRUE}}; static Classifier sLegacyChannelRules[] = { - {"r", LOSSY_DCT, EXR_PIXEL_HALF, 0, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"r", LOSSY_DCT, EXR_PIXEL_FLOAT, 0, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"red", LOSSY_DCT, EXR_PIXEL_HALF, 0, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"red", LOSSY_DCT, EXR_PIXEL_FLOAT, 0, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"g", LOSSY_DCT, EXR_PIXEL_HALF, 1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"g", LOSSY_DCT, EXR_PIXEL_FLOAT, 1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"grn", LOSSY_DCT, EXR_PIXEL_HALF, 1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"grn", LOSSY_DCT, EXR_PIXEL_FLOAT, 1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"green", LOSSY_DCT, EXR_PIXEL_HALF, 1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"green", LOSSY_DCT, EXR_PIXEL_FLOAT, 1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"b", LOSSY_DCT, EXR_PIXEL_HALF, 2, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"b", LOSSY_DCT, EXR_PIXEL_FLOAT, 2, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"blu", LOSSY_DCT, EXR_PIXEL_HALF, 2, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"blu", LOSSY_DCT, EXR_PIXEL_FLOAT, 2, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"blue", LOSSY_DCT, EXR_PIXEL_HALF, 2, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"blue", LOSSY_DCT, EXR_PIXEL_FLOAT, 2, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"y", LOSSY_DCT, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"y", LOSSY_DCT, EXR_PIXEL_FLOAT, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"by", LOSSY_DCT, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"by", LOSSY_DCT, EXR_PIXEL_FLOAT, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"ry", LOSSY_DCT, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"ry", LOSSY_DCT, EXR_PIXEL_FLOAT, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"a", RLE, EXR_PIXEL_UINT, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"a", RLE, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, - {"a", RLE, EXR_PIXEL_FLOAT, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}}; + {"r", LOSSY_DCT, EXR_PIXEL_HALF, 0, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"r", LOSSY_DCT, EXR_PIXEL_FLOAT, 0, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"red", LOSSY_DCT, EXR_PIXEL_HALF, 0, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"red", LOSSY_DCT, EXR_PIXEL_FLOAT, 0, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"g", LOSSY_DCT, EXR_PIXEL_HALF, 1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"g", LOSSY_DCT, EXR_PIXEL_FLOAT, 1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"grn", LOSSY_DCT, EXR_PIXEL_HALF, 1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"grn", LOSSY_DCT, EXR_PIXEL_FLOAT, 1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"green", LOSSY_DCT, EXR_PIXEL_HALF, 1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"green", LOSSY_DCT, EXR_PIXEL_FLOAT, 1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"b", LOSSY_DCT, EXR_PIXEL_HALF, 2, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"b", LOSSY_DCT, EXR_PIXEL_FLOAT, 2, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"blu", LOSSY_DCT, EXR_PIXEL_HALF, 2, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"blu", LOSSY_DCT, EXR_PIXEL_FLOAT, 2, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"blue", LOSSY_DCT, EXR_PIXEL_HALF, 2, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"blue", LOSSY_DCT, EXR_PIXEL_FLOAT, 2, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"y", LOSSY_DCT, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"y", LOSSY_DCT, EXR_PIXEL_FLOAT, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"by", LOSSY_DCT, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"by", LOSSY_DCT, EXR_PIXEL_FLOAT, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"ry", LOSSY_DCT, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"ry", LOSSY_DCT, EXR_PIXEL_FLOAT, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"a", RLE, EXR_PIXEL_UINT, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"a", RLE, EXR_PIXEL_HALF, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}, + {"a", RLE, EXR_PIXEL_FLOAT, -1, DWA_CLASSIFIER_TRUE, DWA_CLASSIFIER_TRUE}}; + +// clang-format on static inline void -Classifier_destroy (Classifier* p) +Classifier_destroy (void (*free_fn) (void*), Classifier* p) { - if (p->_suffix && !p->_stringStatic) internal_exr_free (EXR_CONST_CAST (void *,p->_suffix)); + if (p->_suffix && !p->_stringStatic) + free_fn (EXR_CONST_CAST (void*, p->_suffix)); } static exr_result_t -Classifier_read (Classifier* out, const uint8_t** ptr, size_t* size) +Classifier_read ( + void* (*alloc_fn) (size_t), + Classifier* out, + const uint8_t** ptr, + size_t* size) { const uint8_t* curin = *ptr; size_t len = 0; @@ -91,12 +100,12 @@ Classifier_read (Classifier* out, const uint8_t** ptr, size_t* size) { if (len > (*size - 3)) return EXR_ERR_CORRUPT_CHUNK; if (curin[len] == '\0') break; - suffix[len] = (char)curin[len]; + suffix[len] = (char) curin[len]; } len += 1; if (len == 128 + 1) return EXR_ERR_CORRUPT_CHUNK; - mem = internal_exr_alloc (len); + mem = alloc_fn (len); if (!mem) return EXR_ERR_OUT_OF_MEMORY; memcpy (mem, suffix, len); @@ -104,8 +113,7 @@ Classifier_read (Classifier* out, const uint8_t** ptr, size_t* size) out->_stringStatic = DWA_CLASSIFIER_FALSE; } - if (*size < len + 2 * sizeof (uint8_t)) - return EXR_ERR_CORRUPT_CHUNK; + if (*size < len + 2 * sizeof (uint8_t)) return EXR_ERR_CORRUPT_CHUNK; curin += len; @@ -118,17 +126,15 @@ Classifier_read (Classifier* out, const uint8_t** ptr, size_t* size) *size -= len + 2 * sizeof (uint8_t); out->_cscIdx = (int) (value >> 4) - 1; - if (out->_cscIdx < -1 || out->_cscIdx >= 3) - return EXR_ERR_CORRUPT_CHUNK; + if (out->_cscIdx < -1 || out->_cscIdx >= 3) return EXR_ERR_CORRUPT_CHUNK; out->_scheme = (CompressorScheme) ((value >> 2) & 3); - if (out->_scheme >= NUM_COMPRESSOR_SCHEMES) - return EXR_ERR_CORRUPT_CHUNK; + if (out->_scheme >= NUM_COMPRESSOR_SCHEMES) return EXR_ERR_CORRUPT_CHUNK; - out->_caseInsensitive = (value & 1 ? DWA_CLASSIFIER_TRUE : DWA_CLASSIFIER_FALSE); + out->_caseInsensitive = + (value & 1 ? DWA_CLASSIFIER_TRUE : DWA_CLASSIFIER_FALSE); - if (type >= EXR_PIXEL_LAST_TYPE) - return EXR_ERR_CORRUPT_CHUNK; + if (type >= EXR_PIXEL_LAST_TYPE) return EXR_ERR_CORRUPT_CHUNK; out->_type = (exr_pixel_type_t) type; return EXR_ERR_SUCCESS; diff --git a/src/lib/OpenEXRCore/internal_dwa_compressor.h b/src/lib/OpenEXRCore/internal_dwa_compressor.h index 892b15d45f..688bd51aad 100644 --- a/src/lib/OpenEXRCore/internal_dwa_compressor.h +++ b/src/lib/OpenEXRCore/internal_dwa_compressor.h @@ -37,6 +37,9 @@ typedef struct _DwaCompressor uint8_t* _planarUncBuffer[NUM_COMPRESSOR_SCHEMES]; uint64_t _planarUncBufferSize[NUM_COMPRESSOR_SCHEMES]; + exr_memory_allocation_func_t alloc_fn; + exr_memory_free_func_t free_fn; + int _zipLevel; float _dwaCompressionLevel; } DwaCompressor; @@ -107,15 +110,22 @@ DwaCompressor_construct ( if (encode) { - me->_channelData = - internal_exr_alloc_aligned ( - &(me->_channel_mem), - sizeof (ChannelData) * (size_t)encode->channel_count, - _SSE_ALIGNMENT); + const struct _internal_exr_context* pctxt = EXR_CCTXT (encode->context); + + me->alloc_fn = pctxt ? pctxt->alloc_fn : internal_exr_alloc; + me->free_fn = pctxt ? pctxt->free_fn : internal_exr_free; + + me->_channelData = internal_exr_alloc_aligned ( + me->alloc_fn, + &(me->_channel_mem), + sizeof (ChannelData) * (size_t) encode->channel_count, + _SSE_ALIGNMENT); if (!me->_channelData) return EXR_ERR_OUT_OF_MEMORY; memset ( - me->_channelData, 0, sizeof (ChannelData) * (size_t)encode->channel_count); + me->_channelData, + 0, + sizeof (ChannelData) * (size_t) encode->channel_count); me->_numChannels = encode->channel_count; for (int c = 0; c < encode->channel_count; ++c) @@ -144,15 +154,22 @@ DwaCompressor_construct ( } else { - me->_channelData = - internal_exr_alloc_aligned ( - &(me->_channel_mem), - sizeof (ChannelData) * (size_t)decode->channel_count, - _SSE_ALIGNMENT); + const struct _internal_exr_context* pctxt = EXR_CCTXT (decode->context); + + me->alloc_fn = pctxt ? pctxt->alloc_fn : internal_exr_alloc; + me->free_fn = pctxt ? pctxt->free_fn : internal_exr_free; + + me->_channelData = internal_exr_alloc_aligned ( + me->alloc_fn, + &(me->_channel_mem), + sizeof (ChannelData) * (size_t) decode->channel_count, + _SSE_ALIGNMENT); if (!me->_channelData) return EXR_ERR_OUT_OF_MEMORY; memset ( - me->_channelData, 0, sizeof (ChannelData) * (size_t)decode->channel_count); + me->_channelData, + 0, + sizeof (ChannelData) * (size_t) decode->channel_count); me->_numChannels = decode->channel_count; for (int c = 0; c < decode->channel_count; ++c) @@ -176,31 +193,31 @@ DwaCompressor_construct ( static void DwaCompressor_destroy (DwaCompressor* me) { - if (me->_packedAcBuffer) internal_exr_free (me->_packedAcBuffer); - if (me->_packedDcBuffer) internal_exr_free (me->_packedDcBuffer); - if (me->_rleBuffer) internal_exr_free (me->_rleBuffer); + if (me->_packedAcBuffer) me->free_fn (me->_packedAcBuffer); + if (me->_packedDcBuffer) me->free_fn (me->_packedDcBuffer); + if (me->_rleBuffer) me->free_fn (me->_rleBuffer); if (me->_channel_mem) { for (int c = 0; c < me->_numChannels; ++c) - DctCoderChannelData_destroy (&(me->_channelData[c]._dctData)); + DctCoderChannelData_destroy ( + me->free_fn, &(me->_channelData[c]._dctData)); - internal_exr_free (me->_channel_mem); + me->free_fn (me->_channel_mem); } - if (me->_cscChannelSets) internal_exr_free (me->_cscChannelSets); + if (me->_cscChannelSets) me->free_fn (me->_cscChannelSets); if (me->_channelRules != sLegacyChannelRules && me->_channelRules != sDefaultChannelRules) { for (size_t i = 0; i < me->_channelRuleCount; ++i) - Classifier_destroy (&(me->_channelRules[i])); - internal_exr_free (me->_channelRules); + Classifier_destroy (me->free_fn, &(me->_channelRules[i])); + me->free_fn (me->_channelRules); } for (int i = 0; i < NUM_COMPRESSOR_SCHEMES; ++i) { - if (me->_planarUncBuffer[i]) - internal_exr_free (me->_planarUncBuffer[i]); + if (me->_planarUncBuffer[i]) me->free_fn (me->_planarUncBuffer[i]); } } @@ -212,18 +229,18 @@ DwaCompressor_compress (DwaCompressor* me) exr_result_t rv; uint8_t* outPtr; uint64_t* sizes; - size_t outBufferSize = 0; - uint64_t dataBytes, nWritten = 0; + size_t outBufferSize = 0; + uint64_t dataBytes, nWritten = 0; uint64_t nAvail; uint64_t fileVersion = 2; - uint64_t* version; - uint64_t* unknownUncompressedSize; - uint64_t* unknownCompressedSize; - uint64_t* acCompressedSize; - uint64_t* dcCompressedSize; - uint64_t* rleCompressedSize; - uint64_t* rleUncompressedSize; - uint64_t* rleRawSize; + uint64_t* version; + uint64_t* unknownUncompressedSize; + uint64_t* unknownCompressedSize; + uint64_t* acCompressedSize; + uint64_t* dcCompressedSize; + uint64_t* rleCompressedSize; + uint64_t* rleUncompressedSize; + uint64_t* rleRawSize; uint64_t* totalAcUncompressedCount; uint64_t* totalDcUncompressedCount; @@ -231,8 +248,8 @@ DwaCompressor_compress (DwaCompressor* me) uint64_t* acCompression; uint8_t* packedAcEnd; uint8_t* packedDcEnd; - uint8_t* outDataPtr; - uint8_t* inDataPtr; + uint8_t* outDataPtr; + uint8_t* inDataPtr; // Starting with 2, we write the channel // classification rules into the file @@ -305,7 +322,7 @@ DwaCompressor_compress (DwaCompressor* me) if (rv != EXR_ERR_SUCCESS || nWritten >= me->_encode->compressed_alloc_size) return EXR_ERR_OUT_OF_MEMORY; - outDataPtr = outPtr; + outDataPtr = outPtr; // // We might not be dealing with any color data, in which @@ -346,7 +363,8 @@ DwaCompressor_compress (DwaCompressor* me) if ((y % chan->y_samples) != 0) continue; - rv = DctCoderChannelData_push_row (&(cd->_dctData), inDataPtr); + rv = DctCoderChannelData_push_row ( + me->alloc_fn, me->free_fn, &(cd->_dctData), inDataPtr); if (rv != EXR_ERR_SUCCESS) return rv; inDataPtr += chan->width * chan->bytes_per_element; @@ -374,7 +392,8 @@ DwaCompressor_compress (DwaCompressor* me) me->_channelData[cset->idx[0]].chan->width, me->_channelData[cset->idx[0]].chan->height); - if (rv == EXR_ERR_SUCCESS) rv = LossyDctEncoder_execute (&enc); + if (rv == EXR_ERR_SUCCESS) + rv = LossyDctEncoder_execute (me->alloc_fn, me->free_fn, &enc); *totalAcUncompressedCount = *totalAcUncompressedCount + enc._numAcComp; *totalDcUncompressedCount = *totalDcUncompressedCount + enc._numDcComp; @@ -421,7 +440,8 @@ DwaCompressor_compress (DwaCompressor* me) pchan->height); if (rv == EXR_ERR_SUCCESS) - rv = LossyDctEncoder_execute (&enc); + rv = LossyDctEncoder_execute ( + me->alloc_fn, me->free_fn, &enc); *totalAcUncompressedCount = *totalAcUncompressedCount + enc._numAcComp; @@ -454,7 +474,8 @@ DwaCompressor_compress (DwaCompressor* me) } } - *rleRawSize += (uint64_t)pchan->width * (uint64_t)pchan->bytes_per_element; + *rleRawSize += (uint64_t) pchan->width * + (uint64_t) pchan->bytes_per_element; } break; @@ -467,8 +488,8 @@ DwaCompressor_compress (DwaCompressor* me) // { - size_t scanlineSize = - (size_t)pchan->width * (size_t)pchan->bytes_per_element; + size_t scanlineSize = (size_t) pchan->width * + (size_t) pchan->bytes_per_element; DctCoderChannelData* dcd = &(cd->_dctData); for (size_t y = 0; y < dcd->_size; ++y) { @@ -502,14 +523,14 @@ DwaCompressor_compress (DwaCompressor* me) size_t outSize; rv = exr_compress_buffer ( + me->_encode->context, 9, // TODO: use default??? the old call to zlib had 9 hardcoded me->_planarUncBuffer[UNKNOWN], *unknownUncompressedSize, outDataPtr, exr_compress_max_buffer_size (*unknownUncompressedSize), &outSize); - if (rv != EXR_ERR_SUCCESS) - return rv; + if (rv != EXR_ERR_SUCCESS) return rv; outDataPtr += outSize; *unknownCompressedSize = outSize; @@ -564,14 +585,14 @@ DwaCompressor_compress (DwaCompressor* me) size_t destLen; rv = exr_compress_buffer ( + me->_encode->context, 9, // TODO: use default??? the old call to zlib had 9 hardcoded me->_packedAcBuffer, sourceLen, outDataPtr, exr_compress_max_buffer_size (sourceLen), &destLen); - if (rv != EXR_ERR_SUCCESS) - return rv; + if (rv != EXR_ERR_SUCCESS) return rv; *acCompressedSize = destLen; break; @@ -608,6 +629,7 @@ DwaCompressor_compress (DwaCompressor* me) me->_encode->scratch_buffer_1, me->_packedDcBuffer, uncompBytes); rv = exr_compress_buffer ( + me->_encode->context, me->_zipLevel, me->_encode->scratch_buffer_1, uncompBytes, @@ -637,6 +659,7 @@ DwaCompressor_compress (DwaCompressor* me) *rleRawSize); rv = exr_compress_buffer ( + me->_encode->context, 9, // TODO: use default??? the old call to zlib had 9 hardcoded me->_rleBuffer, *rleUncompressedSize, @@ -684,35 +707,34 @@ DwaCompressor_uncompress ( { uint64_t headerSize = NUM_SIZES_SINGLE * sizeof (uint64_t); exr_result_t rv = EXR_ERR_SUCCESS; - uint64_t counters[NUM_SIZES_SINGLE]; - uint64_t version; - uint64_t unknownUncompressedSize; - uint64_t unknownCompressedSize; - uint64_t acCompressedSize; - uint64_t dcCompressedSize; - uint64_t rleCompressedSize; - uint64_t rleUncompressedSize; - uint64_t rleRawSize; + uint64_t counters[NUM_SIZES_SINGLE]; + uint64_t version; + uint64_t unknownUncompressedSize; + uint64_t unknownCompressedSize; + uint64_t acCompressedSize; + uint64_t dcCompressedSize; + uint64_t rleCompressedSize; + uint64_t rleUncompressedSize; + uint64_t rleRawSize; uint64_t totalAcUncompressedCount; uint64_t totalDcUncompressedCount; uint64_t acCompression; - size_t outBufferSize; - uint64_t compressedSize; + size_t outBufferSize; + uint64_t compressedSize; const uint8_t* dataPtr; uint64_t dataLeft; - uint8_t* outBufferEnd; - uint8_t* packedAcBufferEnd; - uint8_t* packedDcBufferEnd; + uint8_t* outBufferEnd; + uint8_t* packedAcBufferEnd; + uint8_t* packedDcBufferEnd; const uint8_t* compressedUnknownBuf; const uint8_t* compressedAcBuf; const uint8_t* compressedDcBuf; const uint8_t* compressedRleBuf; - if (iSize < headerSize) - return EXR_ERR_CORRUPT_CHUNK; + if (iSize < headerSize) return EXR_ERR_CORRUPT_CHUNK; // // Flip the counters from XDR to NATIVE @@ -741,7 +763,7 @@ DwaCompressor_uncompress ( acCompression = counters[AC_COMPRESSION]; compressedSize = unknownCompressedSize + acCompressedSize + - dcCompressedSize + rleCompressedSize; + dcCompressedSize + rleCompressedSize; dataPtr = inPtr + headerSize; dataLeft = iSize - headerSize; @@ -791,7 +813,7 @@ DwaCompressor_uncompress ( // the C++ classes used to have one buffer size for compress / uncompress // but here we want to do zero-ish copy... - outBufferEnd = me->_decode->unpacked_buffer; + outBufferEnd = me->_decode->unpacked_buffer; outBufferSize = me->_decode->unpacked_alloc_size; // @@ -818,10 +840,8 @@ DwaCompressor_uncompress ( compressedAcBuf = compressedUnknownBuf + (ptrdiff_t) (unknownCompressedSize); - compressedDcBuf = - compressedAcBuf + (ptrdiff_t) (acCompressedSize); - compressedRleBuf = - compressedDcBuf + (ptrdiff_t) (dcCompressedSize); + compressedDcBuf = compressedAcBuf + (ptrdiff_t) (acCompressedSize); + compressedRleBuf = compressedDcBuf + (ptrdiff_t) (dcCompressedSize); // // Sanity check that the version is something we expect. Right now, @@ -830,10 +850,7 @@ DwaCompressor_uncompress ( // start of the data block. // - if (version > 2) - { - return EXR_ERR_BAD_CHUNK_LEADER; - } + if (version > 2) { return EXR_ERR_BAD_CHUNK_LEADER; } rv = DwaCompressor_setupChannelData (me); @@ -849,6 +866,7 @@ DwaCompressor_uncompress ( } if (EXR_ERR_SUCCESS != exr_uncompress_buffer ( + me->_decode->context, compressedUnknownBuf, unknownCompressedSize, me->_planarUncBuffer[UNKNOWN], @@ -894,13 +912,13 @@ DwaCompressor_uncompress ( size_t destLen; rv = exr_uncompress_buffer ( + me->_decode->context, compressedAcBuf, acCompressedSize, me->_packedAcBuffer, totalAcUncompressedCount * sizeof (uint16_t), &destLen); - if (rv != EXR_ERR_SUCCESS) - return rv; + if (rv != EXR_ERR_SUCCESS) return rv; if (totalAcUncompressedCount * sizeof (uint16_t) != destLen) { @@ -909,9 +927,7 @@ DwaCompressor_uncompress ( } break; - default: - return EXR_ERR_CORRUPT_CHUNK; - break; + default: return EXR_ERR_CORRUPT_CHUNK; break; } } @@ -938,6 +954,7 @@ DwaCompressor_uncompress ( if (rv != EXR_ERR_SUCCESS) return rv; rv = exr_uncompress_buffer ( + me->_decode->context, compressedDcBuf, dcCompressedSize, me->_decode->scratch_buffer_1, @@ -954,10 +971,7 @@ DwaCompressor_uncompress ( else { // if the compressed size is 0, then the uncompressed size must also be zero - if (totalDcUncompressedCount != 0) - { - return EXR_ERR_CORRUPT_CHUNK; - } + if (totalDcUncompressedCount != 0) { return EXR_ERR_CORRUPT_CHUNK; } } // @@ -976,6 +990,7 @@ DwaCompressor_uncompress ( } if (EXR_ERR_SUCCESS != exr_uncompress_buffer ( + me->_decode->context, compressedRleBuf, rleCompressedSize, me->_rleBuffer, @@ -985,10 +1000,7 @@ DwaCompressor_uncompress ( return EXR_ERR_CORRUPT_CHUNK; } - if (dstLen != rleUncompressedSize) - { - return EXR_ERR_CORRUPT_CHUNK; - } + if (dstLen != rleUncompressedSize) { return EXR_ERR_CORRUPT_CHUNK; } if (internal_rle_decompress ( me->_planarUncBuffer[RLE], @@ -1017,7 +1029,8 @@ DwaCompressor_uncompress ( if ((y % chan->y_samples) != 0) continue; - rv = DctCoderChannelData_push_row (&(cd->_dctData), outBufferEnd); + rv = DctCoderChannelData_push_row ( + me->alloc_fn, me->free_fn, &(cd->_dctData), outBufferEnd); if (rv != EXR_ERR_SUCCESS) return rv; outBufferEnd += chan->width * chan->bytes_per_element; @@ -1057,7 +1070,8 @@ DwaCompressor_uncompress ( me->_channelData[rChan].chan->width, me->_channelData[rChan].chan->height); - if (rv == EXR_ERR_SUCCESS) rv = LossyDctDecoder_execute (&decoder); + if (rv == EXR_ERR_SUCCESS) + rv = LossyDctDecoder_execute (me->alloc_fn, me->free_fn, &decoder); packedAcBufferEnd += decoder._packedAcCount * sizeof (uint16_t); @@ -1110,13 +1124,14 @@ DwaCompressor_uncompress ( chan->height); if (rv == EXR_ERR_SUCCESS) - rv = LossyDctDecoder_execute (&decoder); + rv = LossyDctDecoder_execute ( + me->alloc_fn, me->free_fn, &decoder); packedAcBufferEnd += - (size_t)decoder._packedAcCount * sizeof (uint16_t); + (size_t) decoder._packedAcCount * sizeof (uint16_t); packedDcBufferEnd += - (size_t)decoder._packedDcCount * sizeof (uint16_t); + (size_t) decoder._packedDcCount * sizeof (uint16_t); if (rv != EXR_ERR_SUCCESS) { return rv; } } @@ -1178,8 +1193,9 @@ DwaCompressor_uncompress ( // { - int row = 0; - size_t dstScanlineSize = (size_t)chan->width * (size_t)pixelSize; + int row = 0; + size_t dstScanlineSize = + (size_t) chan->width * (size_t) pixelSize; for (int y = me->_min[1]; y <= me->_max[1]; ++y) { @@ -1209,9 +1225,7 @@ DwaCompressor_uncompress ( break; case NUM_COMPRESSOR_SCHEMES: - default: - return EXR_ERR_CORRUPT_CHUNK; - break; + default: return EXR_ERR_CORRUPT_CHUNK; break; } cd->processed = 1; @@ -1289,21 +1303,22 @@ DwaCompressor_initializeBuffers (DwaCompressor* me, size_t* bufferSize) // RLE, if gone horribly wrong, could double the size // of the source data. // - rleBufferSize += 2 * pixelCount * (uint64_t)curc->bytes_per_element; + rleBufferSize += + 2 * pixelCount * (uint64_t) curc->bytes_per_element; planarUncBufferSize[RLE] += - 2 * pixelCount * (uint64_t)curc->bytes_per_element; + 2 * pixelCount * (uint64_t) curc->bytes_per_element; break; case UNKNOWN: - unknownBufferSize += pixelCount * (uint64_t)curc->bytes_per_element; + unknownBufferSize += + pixelCount * (uint64_t) curc->bytes_per_element; planarUncBufferSize[UNKNOWN] += - pixelCount * (uint64_t)curc->bytes_per_element; + pixelCount * (uint64_t) curc->bytes_per_element; break; case NUM_COMPRESSOR_SCHEMES: - default: - return EXR_ERR_INVALID_ARGUMENT; + default: return EXR_ERR_INVALID_ARGUMENT; } } @@ -1360,9 +1375,8 @@ DwaCompressor_initializeBuffers (DwaCompressor* me, size_t* bufferSize) if (maxLossyDctAcSize * numLossyDctChans > me->_packedAcBufferSize) { me->_packedAcBufferSize = maxLossyDctAcSize * numLossyDctChans; - if (me->_packedAcBuffer != NULL) - internal_exr_free (me->_packedAcBuffer); - me->_packedAcBuffer = internal_exr_alloc (me->_packedAcBufferSize); + if (me->_packedAcBuffer != NULL) me->free_fn (me->_packedAcBuffer); + me->_packedAcBuffer = me->alloc_fn (me->_packedAcBufferSize); if (!me->_packedAcBuffer) return EXR_ERR_OUT_OF_MEMORY; memset (me->_packedAcBuffer, 0, me->_packedAcBufferSize); } @@ -1374,9 +1388,8 @@ DwaCompressor_initializeBuffers (DwaCompressor* me, size_t* bufferSize) if (maxLossyDctDcSize * numLossyDctChans > me->_packedDcBufferSize) { me->_packedDcBufferSize = maxLossyDctDcSize * numLossyDctChans; - if (me->_packedDcBuffer != NULL) - internal_exr_free (me->_packedDcBuffer); - me->_packedDcBuffer = internal_exr_alloc (me->_packedDcBufferSize); + if (me->_packedDcBuffer != NULL) me->free_fn (me->_packedDcBuffer); + me->_packedDcBuffer = me->alloc_fn (me->_packedDcBufferSize); if (!me->_packedDcBuffer) return EXR_ERR_OUT_OF_MEMORY; memset (me->_packedDcBuffer, 0, me->_packedDcBufferSize); } @@ -1384,8 +1397,8 @@ DwaCompressor_initializeBuffers (DwaCompressor* me, size_t* bufferSize) if (rleBufferSize > me->_rleBufferSize) { me->_rleBufferSize = rleBufferSize; - if (me->_rleBuffer != 0) internal_exr_free (me->_rleBuffer); - me->_rleBuffer = internal_exr_alloc (rleBufferSize); + if (me->_rleBuffer != 0) me->free_fn (me->_rleBuffer); + me->_rleBuffer = me->alloc_fn (rleBufferSize); if (!me->_rleBuffer) return EXR_ERR_OUT_OF_MEMORY; memset (me->_rleBuffer, 0, rleBufferSize); } @@ -1418,15 +1431,14 @@ DwaCompressor_initializeBuffers (DwaCompressor* me, size_t* bufferSize) { me->_planarUncBufferSize[i] = planarUncBufferSize[i]; if (me->_planarUncBuffer[i] != NULL) - internal_exr_free (me->_planarUncBuffer[i]); + me->free_fn (me->_planarUncBuffer[i]); if (planarUncBufferSize[i] > SIZE_MAX) { return EXR_ERR_OUT_OF_MEMORY; } - me->_planarUncBuffer[i] = - internal_exr_alloc (planarUncBufferSize[i]); + me->_planarUncBuffer[i] = me->alloc_fn (planarUncBufferSize[i]); if (!me->_planarUncBuffer[i]) return EXR_ERR_OUT_OF_MEMORY; } } @@ -1471,9 +1483,8 @@ DwaCompressor_writeRelevantChannelRules ( } } - if (nOut > 65535) - return EXR_ERR_OUT_OF_MEMORY; - *ruleSize = one_from_native16 ((uint16_t)nOut); + if (nOut > 65535) return EXR_ERR_OUT_OF_MEMORY; + *ruleSize = one_from_native16 ((uint16_t) nOut); *nWritten += nOut; *outPtr = curp; @@ -1499,16 +1510,10 @@ DwaCompressor_readChannelRules ( size_t nRules = 0, dataSize; const uint8_t* tmpPtr; - if (ruleSize < sizeof (uint16_t)) - { - return EXR_ERR_CORRUPT_CHUNK; - } + if (ruleSize < sizeof (uint16_t)) { return EXR_ERR_CORRUPT_CHUNK; } *outRuleSize = ruleSize; - if (*nAvail < ruleSize) - { - return EXR_ERR_CORRUPT_CHUNK; - } + if (*nAvail < ruleSize) { return EXR_ERR_CORRUPT_CHUNK; } readPtr += sizeof (uint16_t); *inPtr += ruleSize; @@ -1523,8 +1528,8 @@ DwaCompressor_readChannelRules ( { Classifier tmpc; memset (&tmpc, 0, sizeof (Classifier)); - rv = Classifier_read (&tmpc, &tmpPtr, &dataSize); - Classifier_destroy (&tmpc); + rv = Classifier_read (me->alloc_fn, &tmpc, &tmpPtr, &dataSize); + Classifier_destroy (me->free_fn, &tmpc); ++nRules; } @@ -1532,8 +1537,7 @@ DwaCompressor_readChannelRules ( if (rv == EXR_ERR_SUCCESS) { me->_channelRuleCount = nRules; - me->_channelRules = - internal_exr_alloc (sizeof (Classifier) * nRules); + me->_channelRules = me->alloc_fn (sizeof (Classifier) * nRules); dataSize = ruleSize; if (me->_channelRules) @@ -1542,7 +1546,10 @@ DwaCompressor_readChannelRules ( for (size_t i = 0; i < nRules; ++i) { Classifier_read ( - &(me->_channelRules[i]), &readPtr, &dataSize); + me->alloc_fn, + &(me->_channelRules[i]), + &readPtr, + &dataSize); } } else @@ -1566,7 +1573,7 @@ DwaCompressor_classifyChannels (DwaCompressor* me) // me->_cscChannelSets = - internal_exr_alloc (sizeof (CscChannelSet) * (size_t)me->_numChannels); + me->alloc_fn (sizeof (CscChannelSet) * (size_t) me->_numChannels); if (!me->_cscChannelSets) return EXR_ERR_OUT_OF_MEMORY; // @@ -1574,10 +1581,11 @@ DwaCompressor_classifyChannels (DwaCompressor* me) // compressed by which means. // prefixMap = - internal_exr_alloc (sizeof (CscPrefixMapItem) * (size_t)me->_numChannels); + me->alloc_fn (sizeof (CscPrefixMapItem) * (size_t) me->_numChannels); if (!prefixMap) return EXR_ERR_OUT_OF_MEMORY; - memset (prefixMap, 0, sizeof (CscPrefixMapItem) * (size_t)me->_numChannels); + memset ( + prefixMap, 0, sizeof (CscPrefixMapItem) * (size_t) me->_numChannels); for (int c = 0; c < me->_numChannels; ++c) { const exr_coding_channel_info_t* curc = me->_channelData[c].chan; @@ -1615,11 +1623,11 @@ DwaCompressor_classifyChannels (DwaCompressor* me) for (int c = 0; c < me->_numChannels; ++c) { - const exr_coding_channel_info_t* redc, * grnc, * bluc; - CscChannelSet* cset; - int red = prefixMap[c].idx[0]; - int grn = prefixMap[c].idx[1]; - int blu = prefixMap[c].idx[2]; + const exr_coding_channel_info_t *redc, *grnc, *bluc; + CscChannelSet* cset; + int red = prefixMap[c].idx[0]; + int grn = prefixMap[c].idx[1]; + int blu = prefixMap[c].idx[2]; if (prefixMap[c].name == NULL) break; @@ -1639,13 +1647,13 @@ DwaCompressor_classifyChannels (DwaCompressor* me) continue; } - cset = me->_cscChannelSets + me->_numCscChannelSets; - cset->idx[0] = red; - cset->idx[1] = grn; - cset->idx[2] = blu; + cset = me->_cscChannelSets + me->_numCscChannelSets; + cset->idx[0] = red; + cset->idx[1] = grn; + cset->idx[2] = blu; ++(me->_numCscChannelSets); } - internal_exr_free (prefixMap); + me->free_fn (prefixMap); return EXR_ERR_SUCCESS; } diff --git a/src/lib/OpenEXRCore/internal_dwa_decoder.h b/src/lib/OpenEXRCore/internal_dwa_decoder.h index 4d47dbd9c6..1656a64e4f 100644 --- a/src/lib/OpenEXRCore/internal_dwa_decoder.h +++ b/src/lib/OpenEXRCore/internal_dwa_decoder.h @@ -49,7 +49,7 @@ typedef struct _LossyDctDecoder DctCoderChannelData* _channel_decode_data[3]; int _channel_decode_data_count; - uint8_t _pad[4]; + uint8_t _pad[4]; } LossyDctDecoder; static exr_result_t LossyDctDecoder_base_construct ( @@ -83,7 +83,8 @@ static exr_result_t LossyDctDecoderCsc_construct ( int width, int height); -static exr_result_t LossyDctDecoder_execute (LossyDctDecoder* d); +static exr_result_t LossyDctDecoder_execute ( + void* (*alloc_fn) (size_t), void (*free_fn) (void*), LossyDctDecoder* d); // // Un-RLE the packed AC components into @@ -206,23 +207,24 @@ LossyDctDecoder_base_construct ( /**************************************/ exr_result_t -LossyDctDecoder_execute (LossyDctDecoder* d) +LossyDctDecoder_execute ( + void* (*alloc_fn) (size_t), void (*free_fn) (void*), LossyDctDecoder* d) { exr_result_t rv; int numComp = d->_channel_decode_data_count; DctCoderChannelData* chanData[3]; int lastNonZero = 0; int numBlocksX = (int) (ceilf ((float) d->_width / 8.0f)); - int numBlocksY = (int) (ceilf ((float) d->_height / 8.0f)); - int leftoverX = d->_width - (numBlocksX - 1) * 8; - int leftoverY = d->_height - (numBlocksY - 1) * 8; + int numBlocksY = (int) (ceilf ((float) d->_height / 8.0f)); + int leftoverX = d->_width - (numBlocksX - 1) * 8; + int leftoverY = d->_height - (numBlocksY - 1) * 8; int numFullBlocksX = (int) (floorf ((float) d->_width / 8.0f)); uint16_t* currAcComp = (uint16_t*) (d->_packedAc); uint16_t* acCompEnd = (uint16_t*) (d->_packedAcEnd); uint16_t* currDcComp[3]; - uint8_t* rowBlockHandle; + uint8_t* rowBlockHandle; uint16_t* rowBlock[3]; for (int chan = 0; chan < numComp; ++chan) @@ -235,8 +237,9 @@ LossyDctDecoder_execute (LossyDctDecoder* d) // 8x8 half-float blocks // - rowBlockHandle = internal_exr_alloc ( - (size_t)numComp * (size_t)numBlocksX * 64 * sizeof (uint16_t) + _SSE_ALIGNMENT); + rowBlockHandle = alloc_fn ( + (size_t) numComp * (size_t) numBlocksX * 64 * sizeof (uint16_t) + + _SSE_ALIGNMENT); if (!rowBlockHandle) return EXR_ERR_OUT_OF_MEMORY; rowBlock[0] = (uint16_t*) rowBlockHandle; @@ -319,7 +322,7 @@ LossyDctDecoder_execute (LossyDctDecoder* d) d, &lastNonZero, &currAcComp, acCompEnd, halfZigData); if (rv != EXR_ERR_SUCCESS) { - internal_exr_free (rowBlockHandle); + free_fn (rowBlockHandle); return rv; } @@ -645,7 +648,7 @@ LossyDctDecoder_execute (LossyDctDecoder* d) } } - internal_exr_free (rowBlockHandle); + free_fn (rowBlockHandle); return EXR_ERR_SUCCESS; } @@ -685,7 +688,7 @@ LossyDctDecoder_unRleAc ( int dctComp = 1; uint16_t* acComp = *currAcComp; uint16_t val; - int lnz = 0; + int lnz = 0; uint64_t ac_count = 0; // @@ -695,10 +698,7 @@ LossyDctDecoder_unRleAc ( while (dctComp < 64) { - if (acComp >= packedAcEnd) - { - return EXR_ERR_CORRUPT_CHUNK; - } + if (acComp >= packedAcEnd) { return EXR_ERR_CORRUPT_CHUNK; } val = *acComp; if (val == 0xff00) { @@ -735,6 +735,6 @@ LossyDctDecoder_unRleAc ( d->_packedAcCount += ac_count; *lastNonZero = lnz; - *currAcComp = acComp; + *currAcComp = acComp; return EXR_ERR_SUCCESS; } diff --git a/src/lib/OpenEXRCore/internal_dwa_encoder.h b/src/lib/OpenEXRCore/internal_dwa_encoder.h index 6c5468cc9b..ae1caf9406 100644 --- a/src/lib/OpenEXRCore/internal_dwa_encoder.h +++ b/src/lib/OpenEXRCore/internal_dwa_encoder.h @@ -74,7 +74,8 @@ static exr_result_t LossyDctEncoderCsc_construct ( int width, int height); -static exr_result_t LossyDctEncoder_execute (LossyDctEncoder* e); +static exr_result_t LossyDctEncoder_execute ( + void* (*alloc_fn) (size_t), void (*free_fn) (void*), LossyDctEncoder* e); static void LossyDctEncoder_rleAc (LossyDctEncoder* e, uint16_t* block, uint16_t** acPtr); @@ -272,10 +273,10 @@ countSetBits (uint16_t src) static inline uint16_t quantize (float dctval, float errorTolerance) { - uint16_t tmp; + uint16_t tmp; // pre-quantize float -> half and back - uint16_t src = float_to_half (dctval); - float srcFloat = half_to_float (src); + uint16_t src = float_to_half (dctval); + float srcFloat = half_to_float (src); int numSetBits = countSetBits (src); const uint16_t* closest = closestData + closestDataOffset[src]; @@ -303,9 +304,10 @@ quantize (float dctval, float errorTolerance) // Other numbers of channels are somewhat unexpected at this point // exr_result_t -LossyDctEncoder_execute (LossyDctEncoder* e) +LossyDctEncoder_execute ( + void* (*alloc_fn) (size_t), void (*free_fn) (void*), LossyDctEncoder* e) { - int numComp = e->_channel_encode_data_count; + int numComp = e->_channel_encode_data_count; DctCoderChannelData* chanData[3]; int numBlocksX = (int) (ceilf ((float) e->_width / 8.0f)); @@ -314,10 +316,10 @@ LossyDctEncoder_execute (LossyDctEncoder* e) uint16_t halfZigCoef[64]; uint16_t halfCoef[64]; - uint16_t* currAcComp = (uint16_t*) e->_packedAc; - int tmpHalfBufferElements = 0; - uint16_t* tmpHalfBuffer = NULL; - uint16_t* tmpHalfBufferPtr = NULL; + uint16_t* currAcComp = (uint16_t*) e->_packedAc; + int tmpHalfBufferElements = 0; + uint16_t* tmpHalfBuffer = NULL; + uint16_t* tmpHalfBufferPtr = NULL; e->_numAcComp = 0; e->_numDcComp = 0; @@ -336,8 +338,8 @@ LossyDctEncoder_execute (LossyDctEncoder* e) if (tmpHalfBufferElements) { - tmpHalfBuffer = (uint16_t*) internal_exr_alloc ( - (size_t)tmpHalfBufferElements * sizeof (uint16_t)); + tmpHalfBuffer = (uint16_t*) alloc_fn ( + (size_t) tmpHalfBufferElements * sizeof (uint16_t)); if (!tmpHalfBuffer) return EXR_ERR_OUT_OF_MEMORY; tmpHalfBufferPtr = tmpHalfBuffer; } @@ -390,7 +392,7 @@ LossyDctEncoder_execute (LossyDctEncoder* e) { for (int blockx = 0; blockx < numBlocksX; ++blockx) { - uint16_t h; + uint16_t h; const float* quantTable; for (int chan = 0; chan < numComp; ++chan) @@ -461,9 +463,8 @@ LossyDctEncoder_execute (LossyDctEncoder* e) for (int i = 0; i < 64; ++i) { - halfCoef[i] = quantize ( - chanData[chan]->_dctData[i], - quantTable[i]); + halfCoef[i] = + quantize (chanData[chan]->_dctData[i], quantTable[i]); } toZigZag (halfZigCoef, halfCoef); @@ -492,7 +493,7 @@ LossyDctEncoder_execute (LossyDctEncoder* e) } // blockx } // blocky - if (tmpHalfBuffer) internal_exr_free (tmpHalfBuffer); + if (tmpHalfBuffer) free_fn (tmpHalfBuffer); return EXR_ERR_SUCCESS; } @@ -588,7 +589,7 @@ LossyDctEncoder_rleAc (LossyDctEncoder* e, uint16_t* block, uint16_t** acPtr) // Signal normal run // - *curAC++ = (uint16_t)0xff00 | runLen; + *curAC++ = (uint16_t) 0xff00 | runLen; e->_numAcComp++; } diff --git a/src/lib/OpenEXRCore/internal_dwa_simd.h b/src/lib/OpenEXRCore/internal_dwa_simd.h index 32ffbcba6b..74287b398c 100644 --- a/src/lib/OpenEXRCore/internal_dwa_simd.h +++ b/src/lib/OpenEXRCore/internal_dwa_simd.h @@ -368,8 +368,7 @@ convertFloatToHalf64_f16c (uint16_t* dst, float* src) // #ifdef IMF_HAVE_GCC_INLINEASM_X86_64 - __asm__ ( - "vmovaps (%0), %%ymm0 \n" + __asm__ ("vmovaps (%0), %%ymm0 \n" "vmovaps 0x20(%0), %%ymm1 \n" "vmovaps 0x40(%0), %%ymm2 \n" "vmovaps 0x60(%0), %%ymm3 \n" @@ -868,13 +867,13 @@ dctInverse8x8DcOnly (float* data) static inline void dctInverse8x8_scalar (float* data, int zeroedRows) { - const float a = 0.35355362513961314f;//.5f * cosf (3.14159f / 4.0f); - const float b = 0.4903926563794112f;//.5f * cosf (3.14159f / 16.0f); - const float c = 0.4619398297234211f;//.5f * cosf (3.14159f / 8.0f); - const float d = 0.4157349443626743f;//.5f * cosf (3.f * 3.14159f / 16.0f); - const float e = 0.2777854612564676f;//.5f * cosf (5.f * 3.14159f / 16.0f); - const float f = 0.19134217585694352f;//.5f * cosf (3.f * 3.14159f / 8.0f); - const float g = 0.09754573032714427f;//.5f * cosf (7.f * 3.14159f / 16.0f); + const float a = 0.35355362513961314f; //.5f * cosf (3.14159f / 4.0f); + const float b = 0.4903926563794112f; //.5f * cosf (3.14159f / 16.0f); + const float c = 0.4619398297234211f; //.5f * cosf (3.14159f / 8.0f); + const float d = 0.4157349443626743f; //.5f * cosf (3.f * 3.14159f / 16.0f); + const float e = 0.2777854612564676f; //.5f * cosf (5.f * 3.14159f / 16.0f); + const float f = 0.19134217585694352f; //.5f * cosf (3.f * 3.14159f / 8.0f); + const float g = 0.09754573032714427f; //.5f * cosf (7.f * 3.14159f / 16.0f); float alpha[4], beta[4], theta[4], gamma[4]; @@ -1638,24 +1637,24 @@ dctInverse8x8_sse2_7 (float* data) * [ a -c a -f ] [ g -e d -b ] */ static const float sAvxCoef[32] __attribute__ ((aligned (_SSE_ALIGNMENT))) = { - 3.535536e-01f, 3.535536e-01f, - 3.535536e-01f, 3.535536e-01f, /* a a a a */ - 4.619398e-01f, 1.913422e-01f, - -1.913422e-01f, -4.619398e-01f, /* c f -f -c */ - 3.535536e-01f, -3.535536e-01f, - -3.535536e-01f, 3.535536e-01f, /* a -a -a a */ - 1.913422e-01f, -4.619398e-01f, - 4.619398e-01f, -1.913422e-01f, /* f -c c -f */ - - 4.903927e-01f, 4.157349e-01f, - 2.777855e-01f, 9.754573e-02f, /* b d e g */ - 4.157349e-01f, -9.754573e-02f, - -4.903927e-01f, -2.777855e-01f, /* d -g -b -e */ - 2.777855e-01f, -4.903927e-01f, - 9.754573e-02f, 4.157349e-01f, /* e -b g d */ - 9.754573e-02f, -2.777855e-01f, - 4.157349e-01f, -4.903927e-01f /* g -e d -b */ - }; + 3.535536e-01f, 3.535536e-01f, + 3.535536e-01f, 3.535536e-01f, /* a a a a */ + 4.619398e-01f, 1.913422e-01f, + -1.913422e-01f, -4.619398e-01f, /* c f -f -c */ + 3.535536e-01f, -3.535536e-01f, + -3.535536e-01f, 3.535536e-01f, /* a -a -a a */ + 1.913422e-01f, -4.619398e-01f, + 4.619398e-01f, -1.913422e-01f, /* f -c c -f */ + + 4.903927e-01f, 4.157349e-01f, + 2.777855e-01f, 9.754573e-02f, /* b d e g */ + 4.157349e-01f, -9.754573e-02f, + -4.903927e-01f, -2.777855e-01f, /* d -g -b -e */ + 2.777855e-01f, -4.903927e-01f, + 9.754573e-02f, 4.157349e-01f, /* e -b g d */ + 9.754573e-02f, -2.777855e-01f, + 4.157349e-01f, -4.903927e-01f /* g -e d -b */ +}; #endif #define ROW0(_X) _X @@ -1874,20 +1873,20 @@ dctForward8x8 (float* data) float* srcPtr = data; float* dstPtr = data; - const float c1 = 0.9807853127588224f;//cosf (3.14159f * 1.0f / 16.0f); - const float c2 = 0.9238796594468422f;//cosf (3.14159f * 2.0f / 16.0f); - const float c3 = 0.8314698887253485f;//cosf (3.14159f * 3.0f / 16.0f); - const float c4 = 0.7071072502792263f;//cosf (3.14159f * 4.0f / 16.0f); - const float c5 = 0.5555709225129352f;//cosf (3.14159f * 5.0f / 16.0f); - const float c6 = 0.38268435171388704f;//cosf (3.14159f * 6.0f / 16.0f); - const float c7 = 0.19509146065428853f;//cosf (3.14159f * 7.0f / 16.0f); - - const float c1Half = 0.4903926563794112f;//.5f * c1; - const float c2Half = 0.4619398297234211f;//.5f * c2; - const float c3Half = 0.4157349443626743f;//.5f * c3; - const float c5Half = 0.2777854612564676f;//.5f * c5; - const float c6Half = 0.19134217585694352f;//.5f * c6; - const float c7Half = 0.09754573032714427f;//.5f * c7; + const float c1 = 0.9807853127588224f; //cosf (3.14159f * 1.0f / 16.0f); + const float c2 = 0.9238796594468422f; //cosf (3.14159f * 2.0f / 16.0f); + const float c3 = 0.8314698887253485f; //cosf (3.14159f * 3.0f / 16.0f); + const float c4 = 0.7071072502792263f; //cosf (3.14159f * 4.0f / 16.0f); + const float c5 = 0.5555709225129352f; //cosf (3.14159f * 5.0f / 16.0f); + const float c6 = 0.38268435171388704f; //cosf (3.14159f * 6.0f / 16.0f); + const float c7 = 0.19509146065428853f; //cosf (3.14159f * 7.0f / 16.0f); + + const float c1Half = 0.4903926563794112f; //.5f * c1; + const float c2Half = 0.4619398297234211f; //.5f * c2; + const float c3Half = 0.4157349443626743f; //.5f * c3; + const float c5Half = 0.2777854612564676f; //.5f * c5; + const float c6Half = 0.19134217585694352f; //.5f * c6; + const float c7Half = 0.09754573032714427f; //.5f * c7; // // First pass - do a 1D DCT over the rows and write the @@ -2257,7 +2256,8 @@ dctForward8x8 (float* data) // Should be initialized in initializeFuncs() // -static void (*convertFloatToHalf64) (uint16_t*, float*) = convertFloatToHalf64_scalar; +static void (*convertFloatToHalf64) (uint16_t*, float*) = + convertFloatToHalf64_scalar; // // Function pointer for dispatching a fromHalfZigZag_ impl @@ -2282,7 +2282,7 @@ static void initializeFuncs (void) { static int done = 0; - int f16c = 0, avx = 0, sse2 = 0; + int f16c = 0, avx = 0, sse2 = 0; if (done) return; done = 1; diff --git a/src/lib/OpenEXRCore/internal_huf.c b/src/lib/OpenEXRCore/internal_huf.c index 9e7dc18ef3..b6c7b0077c 100644 --- a/src/lib/OpenEXRCore/internal_huf.c +++ b/src/lib/OpenEXRCore/internal_huf.c @@ -48,7 +48,13 @@ hufCode (uint64_t code) } static inline exr_result_t -outputBits (int nBits, uint64_t bits, uint64_t* c, int* lc, uint8_t** outptr, uint8_t *outend) +outputBits ( + int nBits, + uint64_t bits, + uint64_t* c, + int* lc, + uint8_t** outptr, + uint8_t* outend) { uint8_t* out = *outptr; *c <<= nBits; @@ -435,12 +441,12 @@ hufPackEncTable ( uint32_t im, // i : min hcode index uint32_t iM, // i : max hcode index uint8_t** pcode, // o : ptr to packed table (updated) - uint8_t* pend) // i : max size of table + uint8_t* pend) // i : max size of table { exr_result_t rv; - uint8_t* out = *pcode; - uint64_t c = 0; - int lc = 0; + uint8_t* out = *pcode; + uint64_t c = 0; + int lc = 0; for (; im <= iM; im++) { @@ -463,7 +469,8 @@ hufPackEncTable ( { rv = outputBits (6, LONG_ZEROCODE_RUN, &c, &lc, &out, pend); if (rv != EXR_ERR_SUCCESS) return rv; - rv = outputBits (8, zerun - SHORTEST_LONG_RUN, &c, &lc, &out, pend); + rv = outputBits ( + 8, zerun - SHORTEST_LONG_RUN, &c, &lc, &out, pend); if (rv != EXR_ERR_SUCCESS) return rv; } else @@ -509,7 +516,7 @@ hufUnpackEncTable ( uint32_t lc = 0; uint64_t l, zerun; - memset (hcode, 0, sizeof(uint64_t) * HUF_ENCSIZE); + memset (hcode, 0, sizeof (uint64_t) * HUF_ENCSIZE); for (; im <= iM; im++) { nr = (((uintptr_t) p) - ((uintptr_t) *pcode)); @@ -576,8 +583,15 @@ hufClearDecTable (HufDec* hdecod) static exr_result_t hufBuildDecTable ( - const uint64_t* hcode, uint32_t im, uint32_t iM, HufDec* hdecod) + const struct _internal_exr_context* pctxt, + const uint64_t* hcode, + uint32_t im, + uint32_t iM, + HufDec* hdecod) { + void* (*alloc_fn) (size_t) = pctxt ? pctxt->alloc_fn : internal_exr_alloc; + void (*free_fn) (void*) = pctxt ? pctxt->free_fn : internal_exr_free; + // // Init hashtable & loop on all codes. // Assumes that hufClearDecTable(hdecod) has already been called. @@ -622,8 +636,7 @@ hufBuildDecTable ( if (pl->p) { uint32_t* p = pl->p; - pl->p = (uint32_t*) internal_exr_alloc ( - sizeof (uint32_t) * pl->lit); + pl->p = (uint32_t*) alloc_fn (sizeof (uint32_t) * pl->lit); if (pl->p) { @@ -631,9 +644,9 @@ hufBuildDecTable ( pl->p[i] = p[i]; } - internal_exr_free (p); + free_fn (p); } - else { pl->p = (uint32_t*) internal_exr_alloc (sizeof (uint32_t)); } + else { pl->p = (uint32_t*) alloc_fn (sizeof (uint32_t)); } if (!pl->p) return EXR_ERR_OUT_OF_MEMORY; @@ -647,7 +660,8 @@ hufBuildDecTable ( HufDec* pl = hdecod + (c << (HUF_DECBITS - l)); - for (uint64_t i = ((uint64_t)1) << (HUF_DECBITS - l); i > 0; i--, pl++) + for (uint64_t i = ((uint64_t) 1) << (HUF_DECBITS - l); i > 0; + i--, pl++) { if (pl->len || pl->p) { @@ -672,13 +686,14 @@ hufBuildDecTable ( // static void -hufFreeDecTable (HufDec* hdecod) // io: Decoding table +hufFreeDecTable (const struct _internal_exr_context* pctxt, HufDec* hdecod) { + void (*free_fn) (void*) = pctxt ? pctxt->free_fn : internal_exr_free; for (int i = 0; i < HUF_DECSIZE; i++) { if (hdecod[i].p) { - internal_exr_free (hdecod[i].p); + free_fn (hdecod[i].p); hdecod[i].p = NULL; } } @@ -709,8 +724,10 @@ sendCode ( hufLength (sCode) * runCount) { rv = outputCode (sCode, c, lc, out, outend); - if (rv == EXR_ERR_SUCCESS) rv = outputCode (runCode, c, lc, out, outend); - if (rv == EXR_ERR_SUCCESS) rv = outputBits (8, (uint64_t) runCount, c, lc, out, outend); + if (rv == EXR_ERR_SUCCESS) + rv = outputCode (runCode, c, lc, out, outend); + if (rv == EXR_ERR_SUCCESS) + rv = outputBits (8, (uint64_t) runCount, c, lc, out, outend); } else { @@ -718,8 +735,7 @@ sendCode ( while (runCount-- >= 0) { rv = outputCode (sCode, c, lc, out, outend); - if (rv != EXR_ERR_SUCCESS) - break; + if (rv != EXR_ERR_SUCCESS) break; } } return rv; @@ -761,8 +777,7 @@ hufEncode ( else { rv = sendCode (hcode[s], cs, hcode[rlc], &c, &lc, &out, outend); - if (rv != EXR_ERR_SUCCESS) - break; + if (rv != EXR_ERR_SUCCESS) break; cs = 0; } @@ -773,7 +788,8 @@ hufEncode ( // Send remaining code // - if (rv == EXR_ERR_SUCCESS) rv = sendCode (hcode[s], cs, hcode[rlc], &c, &lc, &out, outend); + if (rv == EXR_ERR_SUCCESS) + rv = sendCode (hcode[s], cs, hcode[rlc], &c, &lc, &out, outend); if (rv == EXR_ERR_SUCCESS) { @@ -784,9 +800,8 @@ hufEncode ( } c = (((uintptr_t) out) - ((uintptr_t) outStart)) * 8 + (uint64_t) (lc); - if (c > (uint64_t)UINT32_MAX) - return EXR_ERR_ARGUMENT_OUT_OF_RANGE; - *outbytes = (uint32_t)c; + if (c > (uint64_t) UINT32_MAX) return EXR_ERR_ARGUMENT_OUT_OF_RANGE; + *outbytes = (uint32_t) c; } return rv; @@ -811,7 +826,7 @@ hufEncode ( { \ if (po == rlc) \ { \ - uint8_t cs; \ + uint8_t cs; \ uint16_t s; \ if (lc < 8) \ { \ @@ -933,7 +948,7 @@ hufDecode ( i = (8 - ni) & 7; c >>= i; - lc -= (int)i; + lc -= (int) i; while (lc > 0) { @@ -1245,9 +1260,9 @@ fasthuf_initialize ( const struct _internal_exr_context* pctxt, FastHufDecoder* fhd, const uint8_t** table, - uint64_t numBytes, - uint32_t minSymbol, - uint32_t maxSymbol, + uint64_t numBytes, + uint32_t minSymbol, + uint32_t maxSymbol, int rleSymbol) { // @@ -1275,7 +1290,7 @@ fasthuf_initialize ( uint64_t currBits = 0; int currBitCount = 0; - uint64_t codeLen; + uint64_t codeLen; const uint8_t* topByte = *table + numBytes; uint64_t mapping[MAX_CODE_LEN + 1]; @@ -1324,9 +1339,11 @@ fasthuf_initialize ( { if (codeLen == 0) continue; - if (codeLen < fhd->_minCodeLength) fhd->_minCodeLength = (uint8_t)codeLen; + if (codeLen < fhd->_minCodeLength) + fhd->_minCodeLength = (uint8_t) codeLen; - if (codeLen > fhd->_maxCodeLength) fhd->_maxCodeLength = (uint8_t)codeLen; + if (codeLen > fhd->_maxCodeLength) + fhd->_maxCodeLength = (uint8_t) codeLen; codeCount[codeLen]++; } @@ -1361,7 +1378,7 @@ fasthuf_initialize ( } for (int i = 0; i < MAX_CODE_LEN; ++i) - fhd->_numSymbols += (uint32_t)codeCount[i]; + fhd->_numSymbols += (uint32_t) codeCount[i]; if ((size_t) fhd->_numSymbols > sizeof (fhd->_idToSymbol) / sizeof (int)) { @@ -1418,7 +1435,7 @@ fasthuf_initialize ( // for (int i = 0; i < MAX_CODE_LEN + 1; ++i) - mapping[i] = (uint64_t)-1; + mapping[i] = (uint64_t) -1; for (int i = fhd->_minCodeLength; i <= fhd->_maxCodeLength; ++i) mapping[i] = offset[i]; @@ -1448,7 +1465,7 @@ fasthuf_initialize ( "Huffman decode error (Invalid symbol in header)"); return EXR_ERR_CORRUPT_CHUNK; } - fhd->_idToSymbol[mapping[codeLen]] = (int)symbol; + fhd->_idToSymbol[mapping[codeLen]] = (int) symbol; mapping[codeLen]++; } else if (codeLen == (uint64_t) LONG_ZEROCODE_RUN) @@ -1516,11 +1533,11 @@ fasthuf_decode ( // Current position (byte/bit) in the src data stream // (after the first buffer fill) // - uint64_t buffer, bufferBack, dstIdx; - int bufferNumBits, bufferBackNumBits; + uint64_t buffer, bufferBack, dstIdx; + int bufferNumBits, bufferBackNumBits; const unsigned char* currByte = src + 2 * sizeof (uint64_t); - numSrcBits -= 8 * 2 * (int)sizeof (uint64_t); + numSrcBits -= 8 * 2 * (int) sizeof (uint64_t); // // 64-bit buffer holding the current bits in the stream @@ -1651,7 +1668,7 @@ fasthuf_decode ( return EXR_ERR_CORRUPT_CHUNK; } - if (dstIdx + (uint64_t)rleCount > numDstElems) + if (dstIdx + (uint64_t) rleCount > numDstElems) { if (pctxt) pctxt->print_error ( @@ -1672,16 +1689,16 @@ fasthuf_decode ( } for (int i = 0; i < rleCount; ++i) - dst[dstIdx + (uint64_t)i] = dst[dstIdx - 1]; + dst[dstIdx + (uint64_t) i] = dst[dstIdx - 1]; - dstIdx += (uint64_t)rleCount; + dstIdx += (uint64_t) rleCount; buffer = buffer << 8; bufferNumBits -= 8; } else { - dst[dstIdx] = (uint16_t)symbol; + dst[dstIdx] = (uint16_t) symbol; dstIdx++; } @@ -1711,7 +1728,7 @@ fasthuf_decode ( pctxt, EXR_ERR_CORRUPT_CHUNK, "Huffman decode error (%d bits of compressed data remains after filling expected output buffer)", - (int)numSrcBits); + (int) numSrcBits); return EXR_ERR_CORRUPT_CHUNK; } @@ -1754,18 +1771,18 @@ internal_huf_compress ( uint64_t sparebytes) { exr_result_t rv; - uint64_t* freq; - uint32_t* hlink; - uint64_t** fHeap; - uint64_t* scode; - uint32_t im = 0; - uint32_t iM = 0; - uint32_t tableLength, nBits, dataLength; - uint8_t* dataStart; - uint8_t* compressed = (uint8_t*) out; - uint8_t* tableStart = compressed + 20; - uint8_t* tableEnd = tableStart; - uint8_t* maxcompout = compressed + outsz; + uint64_t* freq; + uint32_t* hlink; + uint64_t** fHeap; + uint64_t* scode; + uint32_t im = 0; + uint32_t iM = 0; + uint32_t tableLength, nBits, dataLength; + uint8_t* dataStart; + uint8_t* compressed = (uint8_t*) out; + uint8_t* tableStart = compressed + 20; + uint8_t* tableEnd = tableStart; + uint8_t* maxcompout = compressed + outsz; if (nRaw == 0) { @@ -1773,8 +1790,7 @@ internal_huf_compress ( return EXR_ERR_SUCCESS; } - if (outsz < 20) - return EXR_ERR_INVALID_ARGUMENT; + if (outsz < 20) return EXR_ERR_INVALID_ARGUMENT; if (sparebytes != internal_exr_huf_compress_spare_bytes ()) return EXR_ERR_INVALID_ARGUMENT; @@ -1865,30 +1881,31 @@ internal_huf_decompress ( { FastHufDecoder* fhd = (FastHufDecoder*) spare; - rv = fasthuf_initialize (pctxt, fhd, &ptr, nCompressed - hufInfoBlockSize, im, iM, (int)iM); + rv = fasthuf_initialize ( + pctxt, fhd, &ptr, nCompressed - hufInfoBlockSize, im, iM, (int) iM); if (rv == EXR_ERR_SUCCESS) { - if ( (uint64_t)(ptr - compressed) + nBytes > nCompressed ) + if ((uint64_t) (ptr - compressed) + nBytes > nCompressed) return EXR_ERR_OUT_OF_MEMORY; rv = fasthuf_decode (pctxt, fhd, ptr, nBits, raw, nRaw); } } else { - uint64_t* freq = (uint64_t*) spare; - HufDec* hdec = (HufDec*) (freq + HUF_ENCSIZE); - uint64_t nLeft = nCompressed - 20; + uint64_t* freq = (uint64_t*) spare; + HufDec* hdec = (HufDec*) (freq + HUF_ENCSIZE); + uint64_t nLeft = nCompressed - 20; hufClearDecTable (hdec); hufUnpackEncTable (&ptr, &nLeft, im, iM, freq); if (nBits > 8 * nLeft) return EXR_ERR_CORRUPT_CHUNK; - rv = hufBuildDecTable (freq, im, iM, hdec); + rv = hufBuildDecTable (pctxt, freq, im, iM, hdec); if (rv == EXR_ERR_SUCCESS) rv = hufDecode (freq, hdec, ptr, nBits, iM, nRaw, raw); - hufFreeDecTable (hdec); + hufFreeDecTable (pctxt, hdec); } return rv; } diff --git a/src/lib/OpenEXRCore/internal_memory.h b/src/lib/OpenEXRCore/internal_memory.h index 6927ab8b73..0dc6eafd74 100644 --- a/src/lib/OpenEXRCore/internal_memory.h +++ b/src/lib/OpenEXRCore/internal_memory.h @@ -18,7 +18,8 @@ internal_exr_alloc (size_t bytes); __attribute__ ((malloc)) #endif void* -internal_exr_alloc_aligned (void **tofreeptr, size_t bytes, size_t align); +internal_exr_alloc_aligned ( + void* (*alloc_fn) (size_t), void** tofreeptr, size_t bytes, size_t align); void internal_exr_free (void* ptr); diff --git a/src/lib/OpenEXRCore/internal_piz.c b/src/lib/OpenEXRCore/internal_piz.c index e0fcd64cf9..6c2750b166 100644 --- a/src/lib/OpenEXRCore/internal_piz.c +++ b/src/lib/OpenEXRCore/internal_piz.c @@ -525,7 +525,8 @@ internal_exr_apply_piz (exr_encode_pipeline_t* encode) { if (rv == EXR_ERR_ARGUMENT_OUT_OF_RANGE) { - memcpy (encode->compressed_buffer, encode->packed_buffer, packedbytes); + memcpy ( + encode->compressed_buffer, encode->packed_buffer, packedbytes); nOut = packedbytes; } } @@ -538,7 +539,8 @@ internal_exr_apply_piz (exr_encode_pipeline_t* encode) } else { - memcpy (encode->compressed_buffer, encode->packed_buffer, packedbytes); + memcpy ( + encode->compressed_buffer, encode->packed_buffer, packedbytes); nOut = packedbytes; } } diff --git a/src/lib/OpenEXRCore/internal_pxr24.c b/src/lib/OpenEXRCore/internal_pxr24.c index ab89d03adb..43e52c982c 100644 --- a/src/lib/OpenEXRCore/internal_pxr24.c +++ b/src/lib/OpenEXRCore/internal_pxr24.c @@ -89,9 +89,9 @@ float_to_float24 (float f) static exr_result_t apply_pxr24_impl (exr_encode_pipeline_t* encode) { - uint8_t* out = encode->scratch_buffer_1; - uint64_t nOut = 0; - const uint8_t* lastIn = encode->packed_buffer; + uint8_t* out = encode->scratch_buffer_1; + uint64_t nOut = 0; + const uint8_t* lastIn = encode->packed_buffer; size_t compbufsz; exr_result_t rv; @@ -217,12 +217,13 @@ apply_pxr24_impl (exr_encode_pipeline_t* encode) } rv = exr_compress_buffer ( + encode->context, -1, encode->scratch_buffer_1, nOut, encode->compressed_buffer, encode->compressed_alloc_size, - &compbufsz ); + &compbufsz); if (rv == EXR_ERR_SUCCESS) { @@ -276,6 +277,7 @@ undo_pxr24_impl ( if (scratch_size < uncompressed_size) return EXR_ERR_INVALID_ARGUMENT; rstat = exr_uncompress_buffer ( + decode->context, compressed_data, comp_buf_size, scratch_data, diff --git a/src/lib/OpenEXRCore/internal_rle.c b/src/lib/OpenEXRCore/internal_rle.c index 4236641144..fc9620aee6 100644 --- a/src/lib/OpenEXRCore/internal_rle.c +++ b/src/lib/OpenEXRCore/internal_rle.c @@ -71,7 +71,7 @@ reorder_and_predict (void* scratch, const void* packed, uint64_t packedbytes) int8_t* t2 = t1 + (packedbytes + 1) / 2; const int8_t* in = packed; const int8_t* stop = in + packedbytes; - int d, p; + int d, p; while (in < stop) { @@ -79,9 +79,9 @@ reorder_and_predict (void* scratch, const void* packed, uint64_t packedbytes) if (in < stop) *(t2++) = *(in++); } - t1 = scratch; - stop = t1 + packedbytes; - p = *(t1++); + t1 = scratch; + stop = t1 + packedbytes; + p = *(t1++); while (t1 < stop) { d = (int) (*t1) - p + (128 + 256); diff --git a/src/lib/OpenEXRCore/internal_structs.c b/src/lib/OpenEXRCore/internal_structs.c index 4aa84eab3f..c1922b3b01 100644 --- a/src/lib/OpenEXRCore/internal_structs.c +++ b/src/lib/OpenEXRCore/internal_structs.c @@ -192,10 +192,7 @@ internal_exr_destroy_parts (struct _internal_exr_context* ctxt) /* the first one is always the one that is part of the file */ if (cur != &(ctxt->first_part)) { dofree (cur); } - else - { - memset (cur, 0, sizeof (struct _internal_exr_part)); - } + else { memset (cur, 0, sizeof (struct _internal_exr_part)); } } if (ctxt->num_parts > 1) dofree (ctxt->parts); @@ -335,8 +332,8 @@ internal_exr_alloc_context ( void* memptr; exr_result_t rv; struct _internal_exr_context* ret; - int gmaxw, gmaxh; - size_t extra_data; + int gmaxw, gmaxh; + size_t extra_data; *out = NULL; if (initializers->read_fn || initializers->write_fn) @@ -416,7 +413,8 @@ internal_exr_alloc_context ( ret->disable_chunk_reconstruct = (initializers->flags & EXR_CONTEXT_FLAG_DISABLE_CHUNK_RECONSTRUCTION); - ret->legacy_header = (initializers->flags & EXR_CONTEXT_FLAG_WRITE_LEGACY_HEADER); + ret->legacy_header = + (initializers->flags & EXR_CONTEXT_FLAG_WRITE_LEGACY_HEADER); ret->file_size = -1; ret->max_name_length = EXR_SHORTNAME_MAXLEN; diff --git a/src/lib/OpenEXRCore/internal_structs.h b/src/lib/OpenEXRCore/internal_structs.h index 8948cb303a..8e934961b5 100644 --- a/src/lib/OpenEXRCore/internal_structs.h +++ b/src/lib/OpenEXRCore/internal_structs.h @@ -20,7 +20,7 @@ #endif #ifndef __STDC_FORMAT_MACROS -#define __STDC_FORMAT_MACROS +# define __STDC_FORMAT_MACROS #endif #include @@ -335,13 +335,13 @@ internal_exr_unlock (const struct _internal_exr_context* c) const struct _internal_exr_context* pctxt = EXR_CCTXT (c); \ if (!pctxt) return EXR_ERR_MISSING_CONTEXT_ARG; \ if (pi < 0 || pi >= pctxt->num_parts) \ - return ( \ - (void) (EXR_RETURN_WRITE (pctxt)), \ - pctxt->print_error ( \ - pctxt, \ - EXR_ERR_ARGUMENT_OUT_OF_RANGE, \ - "Part index (%d) out of range", \ - pi)) + return ( \ + (void) (EXR_RETURN_WRITE (pctxt)), \ + pctxt->print_error ( \ + pctxt, \ + EXR_ERR_ARGUMENT_OUT_OF_RANGE, \ + "Part index (%d) out of range", \ + pi)) #define EXR_PROMOTE_READ_CONST_CONTEXT_AND_PART_OR_ERROR(c, pi) \ const struct _internal_exr_context* pctxt = EXR_CCTXT (c); \ @@ -357,19 +357,17 @@ internal_exr_unlock (const struct _internal_exr_context* c) pi); \ part = pctxt->parts[pi] - #define EXR_PROMOTE_READ_CONST_CONTEXT_OR_ERROR_NO_PART(c, pi) \ const struct _internal_exr_context* pctxt = EXR_CCTXT (c); \ if (!pctxt) return EXR_ERR_MISSING_CONTEXT_ARG; \ if (pctxt->mode != EXR_CONTEXT_READ) \ return pctxt->standard_error (pctxt, EXR_ERR_NOT_OPEN_READ); \ if (pi < 0 || pi >= pctxt->num_parts) \ - return pctxt->print_error ( \ - pctxt, \ - EXR_ERR_ARGUMENT_OUT_OF_RANGE, \ - "Part index (%d) out of range", \ - pi) - + return pctxt->print_error ( \ + pctxt, \ + EXR_ERR_ARGUMENT_OUT_OF_RANGE, \ + "Part index (%d) out of range", \ + pi) void internal_exr_update_default_handlers (exr_context_initializer_t* inits); diff --git a/src/lib/OpenEXRCore/internal_win32_file_impl.h b/src/lib/OpenEXRCore/internal_win32_file_impl.h index 4582eb5f61..cda37161d9 100644 --- a/src/lib/OpenEXRCore/internal_win32_file_impl.h +++ b/src/lib/OpenEXRCore/internal_win32_file_impl.h @@ -228,10 +228,7 @@ default_read_func ( error_cb, "Unable to read requested data"); } - else - { - retsz = nread; - } + else { retsz = nread; } } return retsz; diff --git a/src/lib/OpenEXRCore/internal_xdr.h b/src/lib/OpenEXRCore/internal_xdr.h index 90d78581fd..2c394b2cb5 100644 --- a/src/lib/OpenEXRCore/internal_xdr.h +++ b/src/lib/OpenEXRCore/internal_xdr.h @@ -162,7 +162,7 @@ one_to_native_float (float v) union { uint32_t i; - float f; + float f; } coerce; coerce.f = v; coerce.i = one_to_native32 (coerce.i); @@ -175,7 +175,7 @@ one_from_native_float (float v) union { uint32_t i; - float f; + float f; } coerce; coerce.f = v; coerce.i = one_from_native32 (coerce.i); diff --git a/src/lib/OpenEXRCore/internal_zip.c b/src/lib/OpenEXRCore/internal_zip.c index de3816e7f8..628438c5c0 100644 --- a/src/lib/OpenEXRCore/internal_zip.c +++ b/src/lib/OpenEXRCore/internal_zip.c @@ -29,7 +29,6 @@ # include #endif - /**************************************/ #ifdef IMF_HAVE_SSE4_1 @@ -87,7 +86,7 @@ reconstruct (uint8_t* buf, uint64_t outSize) const uint8x16_t c = vdupq_n_u8 (-128); const uint8x16_t shuffleMask = vdupq_n_u8 (15); const uint8x16_t zero = vdupq_n_u8 (0); - uint8_t * vBuf; + uint8_t* vBuf; uint8x16_t vPrev; uint8_t prev; @@ -111,7 +110,8 @@ reconstruct (uint8_t* buf, uint64_t outSize) d = vaddq_u8 (d, vextq_u8 (zero, d, 16 - 8)); d = vaddq_u8 (d, vPrev); - vst1q_u8 (vBuf, d); vBuf += sizeof (uint8x16_t); + vst1q_u8 (vBuf, d); + vBuf += sizeof (uint8x16_t); // Broadcast the high byte in our result to all lanes of the prev // value for the next iteration. @@ -180,18 +180,22 @@ interleave (uint8_t* out, const uint8_t* source, uint64_t outSize) { static const uint64_t bytesPerChunk = 2 * sizeof (uint8x16_t); const uint64_t vOutSize = outSize / bytesPerChunk; - const uint8_t* v1 = source; - const uint8_t* v2 = source + (outSize + 1) / 2; + const uint8_t* v1 = source; + const uint8_t* v2 = source + (outSize + 1) / 2; for (uint64_t i = 0; i < vOutSize; ++i) { - uint8x16_t a = vld1q_u8 (v1); v1 += sizeof (uint8x16_t); - uint8x16_t b = vld1q_u8 (v2); v2 += sizeof (uint8x16_t); + uint8x16_t a = vld1q_u8 (v1); + v1 += sizeof (uint8x16_t); + uint8x16_t b = vld1q_u8 (v2); + v2 += sizeof (uint8x16_t); uint8x16_t lo = vzip1q_u8 (a, b); uint8x16_t hi = vzip2q_u8 (a, b); - vst1q_u8 (out, lo); out += sizeof (uint8x16_t); - vst1q_u8 (out, hi); out += sizeof (uint8x16_t); + vst1q_u8 (out, lo); + out += sizeof (uint8x16_t); + vst1q_u8 (out, hi); + out += sizeof (uint8x16_t); } for (uint64_t i = vOutSize * bytesPerChunk; i < outSize; ++i) @@ -226,10 +230,8 @@ interleave (uint8_t* out, const uint8_t* source, uint64_t outSize) /**************************************/ -void internal_zip_reconstruct_bytes ( - uint8_t* out, - uint8_t* source, - uint64_t count) +void +internal_zip_reconstruct_bytes (uint8_t* out, uint8_t* source, uint64_t count) { reconstruct (source, count); interleave (out, source, count); @@ -237,10 +239,9 @@ void internal_zip_reconstruct_bytes ( /**************************************/ -void internal_zip_deconstruct_bytes ( - uint8_t* scratch, - const uint8_t* source, - uint64_t count) +void +internal_zip_deconstruct_bytes ( + uint8_t* scratch, const uint8_t* source, uint64_t count) { int p; uint8_t* t1 = scratch; @@ -273,19 +274,21 @@ void internal_zip_deconstruct_bytes ( static exr_result_t undo_zip_impl ( - const void* compressed_data, - uint64_t comp_buf_size, - void* uncompressed_data, - uint64_t uncompressed_size, - void* scratch_data, - uint64_t scratch_size) + exr_decode_pipeline_t* decode, + const void* compressed_data, + uint64_t comp_buf_size, + void* uncompressed_data, + uint64_t uncompressed_size, + void* scratch_data, + uint64_t scratch_size) { - size_t actual_out_bytes; + size_t actual_out_bytes; exr_result_t res; if (scratch_size < uncompressed_size) return EXR_ERR_INVALID_ARGUMENT; res = exr_uncompress_buffer ( + decode->context, compressed_data, comp_buf_size, scratch_data, @@ -296,7 +299,8 @@ undo_zip_impl ( { if (actual_out_bytes == uncompressed_size) { - internal_zip_reconstruct_bytes (uncompressed_data, scratch_data, actual_out_bytes); + internal_zip_reconstruct_bytes ( + uncompressed_data, scratch_data, actual_out_bytes); } else res = EXR_ERR_CORRUPT_CHUNK; @@ -316,9 +320,8 @@ internal_exr_undo_zip ( uint64_t uncompressed_size) { exr_result_t rv; - uint64_t scratchbufsz = uncompressed_size; - if ( comp_buf_size > scratchbufsz ) - scratchbufsz = comp_buf_size; + uint64_t scratchbufsz = uncompressed_size; + if (comp_buf_size > scratchbufsz) scratchbufsz = comp_buf_size; rv = internal_decode_alloc_buffer ( decode, @@ -328,6 +331,7 @@ internal_exr_undo_zip ( scratchbufsz); if (rv != EXR_ERR_SUCCESS) return rv; return undo_zip_impl ( + decode, compressed_data, comp_buf_size, uncompressed_data, @@ -336,26 +340,24 @@ internal_exr_undo_zip ( decode->scratch_alloc_size_1); } - /**************************************/ static exr_result_t apply_zip_impl (exr_encode_pipeline_t* encode) { - int level; - size_t compbufsz; - exr_result_t rv; + int level; + size_t compbufsz; + exr_result_t rv; rv = exr_get_zip_compression_level ( encode->context, encode->part_index, &level); if (rv != EXR_ERR_SUCCESS) return rv; internal_zip_deconstruct_bytes ( - encode->scratch_buffer_1, - encode->packed_buffer, - encode->packed_bytes); + encode->scratch_buffer_1, encode->packed_buffer, encode->packed_bytes); rv = exr_compress_buffer ( + encode->context, level, encode->scratch_buffer_1, encode->packed_bytes, @@ -382,10 +384,13 @@ apply_zip_impl (exr_encode_pipeline_t* encode) pctxt->print_error ( pctxt, rv, - "Unable to compress buffer %" PRIu64 " -> %" PRIu64 " @ level %d", - encode->packed_bytes, (uint64_t)encode->compressed_alloc_size, level); + "Unable to compress buffer %" PRIu64 " -> %" PRIu64 + " @ level %d", + encode->packed_bytes, + (uint64_t) encode->compressed_alloc_size, + level); } - + return rv; } @@ -407,7 +412,8 @@ internal_exr_apply_zip (exr_encode_pipeline_t* encode) pctxt->print_error ( pctxt, rv, - "Unable to allocate scratch buffer for deflate of %" PRIu64 " bytes", + "Unable to allocate scratch buffer for deflate of %" PRIu64 + " bytes", encode->packed_bytes); return rv; } diff --git a/src/lib/OpenEXRCore/memory.c b/src/lib/OpenEXRCore/memory.c index 5000edcd01..e4ba166976 100644 --- a/src/lib/OpenEXRCore/memory.c +++ b/src/lib/OpenEXRCore/memory.c @@ -43,22 +43,19 @@ internal_exr_alloc (size_t bytes) /**************************************/ void* -internal_exr_alloc_aligned (void **tofreeptr, size_t bytes, size_t align) +internal_exr_alloc_aligned ( + void* (*alloc_fn) (size_t), void** tofreeptr, size_t bytes, size_t align) { - void *ret; - if (align == 1 || align > 4096) - { - align = 0; - } + void* ret; + if (align == 1 || align > 4096) { align = 0; } - ret = internal_exr_alloc (bytes + align); + ret = alloc_fn (bytes + align); *tofreeptr = ret; if (ret) { - uintptr_t off = ((uintptr_t)ret) & (align - 1); - if (off) - off = align - off; - ret = (((uint8_t *)ret) + off); + uintptr_t off = ((uintptr_t) ret) & (align - 1); + if (off) off = align - off; + ret = (((uint8_t*) ret) + off); } return ret; } diff --git a/src/lib/OpenEXRCore/openexr_compression.h b/src/lib/OpenEXRCore/openexr_compression.h index 08470d4aa5..67ae45004b 100644 --- a/src/lib/OpenEXRCore/openexr_compression.h +++ b/src/lib/OpenEXRCore/openexr_compression.h @@ -6,7 +6,7 @@ #ifndef OPENEXR_CORE_COMPRESSION_H #define OPENEXR_CORE_COMPRESSION_H -#include "openexr_errors.h" +#include "openexr_context.h" #include @@ -28,20 +28,22 @@ size_t exr_compress_max_buffer_size (size_t in_bytes); * data. This may include some extra padding for headers / scratch */ EXR_EXPORT exr_result_t exr_compress_buffer ( - int level, - const void *in, - size_t in_bytes, - void *out, - size_t out_bytes_avail, - size_t *actual_out); + exr_const_context_t ctxt, + int level, + const void* in, + size_t in_bytes, + void* out, + size_t out_bytes_avail, + size_t* actual_out); EXR_EXPORT exr_result_t exr_uncompress_buffer ( - const void *in, - size_t in_bytes, - void *out, - size_t out_bytes_avail, - size_t *actual_out); + exr_const_context_t ctxt, + const void* in, + size_t in_bytes, + void* out, + size_t out_bytes_avail, + size_t* actual_out); #ifdef __cplusplus } /* extern "C" */ diff --git a/src/lib/OpenEXRCore/openexr_conf.h b/src/lib/OpenEXRCore/openexr_conf.h index 441eaf080d..e4c7e322c7 100644 --- a/src/lib/OpenEXRCore/openexr_conf.h +++ b/src/lib/OpenEXRCore/openexr_conf.h @@ -19,9 +19,9 @@ // where we need to switch depending on whether we are compiling // internally or not # if defined(OPENEXRCORE_EXPORTS) -# define EXR_EXPORT __declspec(dllexport) +# define EXR_EXPORT __declspec (dllexport) # else -# define EXR_EXPORT __declspec(dllimport) +# define EXR_EXPORT __declspec (dllimport) # endif #else diff --git a/src/lib/OpenEXRCore/openexr_context.h b/src/lib/OpenEXRCore/openexr_context.h index f353771154..0ab5c436d7 100644 --- a/src/lib/OpenEXRCore/openexr_context.h +++ b/src/lib/OpenEXRCore/openexr_context.h @@ -347,12 +347,11 @@ typedef struct _exr_context_initializer_v3 /** @brief Writes an old-style, sorted header with minimal information */ #define EXR_CONTEXT_FLAG_WRITE_LEGACY_HEADER (1 << 3) +/* clang-format off */ /** @brief Simple macro to initialize the context initializer with default values. */ #define EXR_DEFAULT_CONTEXT_INITIALIZER \ - { \ - sizeof (exr_context_initializer_t), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ - 0, -2, -1.f, 0, {0, 0, 0, 0} \ - } + { sizeof (exr_context_initializer_t), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, -1.f, 0, { 0, 0, 0, 0 } } +/* clang-format on */ /** @} */ /* context function pointer declarations */ diff --git a/src/lib/OpenEXRCore/openexr_version.h b/src/lib/OpenEXRCore/openexr_version.h index a01b9a1f0d..11f5020e72 100644 --- a/src/lib/OpenEXRCore/openexr_version.h +++ b/src/lib/OpenEXRCore/openexr_version.h @@ -6,11 +6,10 @@ // ImfVersion.h contains version information about the file format. #pragma once #ifndef INCLUDED_OPENEXR_VERSION_H -#define INCLUDED_OPENEXR_VERSION_H +# define INCLUDED_OPENEXR_VERSION_H -#define OPENEXR_VERSION_MAJOR 3 -#define OPENEXR_VERSION_MINOR 2 -#define OPENEXR_VERSION_PATCH 0 +# define OPENEXR_VERSION_MAJOR 3 +# define OPENEXR_VERSION_MINOR 2 +# define OPENEXR_VERSION_PATCH 0 #endif - diff --git a/src/lib/OpenEXRCore/pack.c b/src/lib/OpenEXRCore/pack.c index cd034c631c..b435e4aee0 100644 --- a/src/lib/OpenEXRCore/pack.c +++ b/src/lib/OpenEXRCore/pack.c @@ -49,10 +49,7 @@ default_pack (exr_encode_pipeline_t* encode) ((uint64_t) (y / encc->y_samples) * (uint64_t) encc->user_line_stride); } - else - { - cdata += (uint64_t) y * (uint64_t) encc->user_line_stride; - } + else { cdata += (uint64_t) y * (uint64_t) encc->user_line_stride; } pixincrement = encc->user_pixel_stride; switch (encc->data_type) diff --git a/src/lib/OpenEXRCore/parse_header.c b/src/lib/OpenEXRCore/parse_header.c index 9ff21923f0..b85273a378 100644 --- a/src/lib/OpenEXRCore/parse_header.c +++ b/src/lib/OpenEXRCore/parse_header.c @@ -25,8 +25,8 @@ silent_error ( exr_result_t code, const char* msg) { - (void)pctxt; - (void)msg; + (void) pctxt; + (void) msg; return code; } @@ -34,7 +34,7 @@ static exr_result_t silent_standard_error ( const struct _internal_exr_context* pctxt, exr_result_t code) { - (void)pctxt; + (void) pctxt; return code; } @@ -45,8 +45,8 @@ silent_print_error ( const char* msg, ...) { - (void)pctxt; - (void)msg; + (void) pctxt; + (void) msg; return code; } @@ -123,10 +123,7 @@ scratch_seq_read (struct _internal_exr_seq_scratch* scr, void* buf, uint64_t sz) outbuf += nread; nCopied += (uint64_t) nread; } - else - { - break; - } + else { break; } } else { @@ -168,7 +165,7 @@ static exr_result_t scratch_seq_skip (struct _internal_exr_seq_scratch* scr, int32_t sz) { uint64_t nCopied = 0; - uint64_t notdone = (uint64_t)sz; + uint64_t notdone = (uint64_t) sz; exr_result_t rv = -1; while (notdone > 0) @@ -211,7 +208,7 @@ scratch_seq_skip (struct _internal_exr_seq_scratch* scr, int32_t sz) } if (rv == -1) { - if (nCopied == (uint64_t)sz) + if (nCopied == (uint64_t) sz) rv = EXR_ERR_SUCCESS; else rv = EXR_ERR_READ_IO; @@ -2593,10 +2590,7 @@ internal_exr_parse_header (struct _internal_exr_context* ctxt) ? EXR_LONGNAME_MAXLEN : EXR_SHORTNAME_MAXLEN; } - else - { - ctxt->max_name_length = EXR_LONGNAME_MAXLEN; - } + else { ctxt->max_name_length = EXR_LONGNAME_MAXLEN; } ctxt->has_nonimage_data = (flags & EXR_NON_IMAGE_FLAG) ? 1 : 0; ctxt->is_multipart = (flags & EXR_MULTI_PART_FLAG) ? 1 : 0; if (ctxt->is_singlepart_tiled) diff --git a/src/lib/OpenEXRCore/part_attr.c b/src/lib/OpenEXRCore/part_attr.c index 43697aba6f..f635d6c393 100644 --- a/src/lib/OpenEXRCore/part_attr.c +++ b/src/lib/OpenEXRCore/part_attr.c @@ -549,10 +549,7 @@ exr_copy_unset_attributes ( { rv = copy_attr (ctxt, pctxt, part, srca, &update_tiles); } - else - { - rv = EXR_ERR_SUCCESS; - } + else { rv = EXR_ERR_SUCCESS; } } if (update_tiles) @@ -1252,7 +1249,8 @@ exr_attr_set_channels ( EXR_PROMOTE_LOCKED_CONTEXT_AND_PART_OR_ERROR (ctxt, part_index); if (name && 0 == strcmp (name, EXR_REQ_CHANNELS_STR)) - return EXR_UNLOCK_AND_RETURN_PCTXT (exr_set_channels (ctxt, part_index, channels)); + return EXR_UNLOCK_AND_RETURN_PCTXT ( + exr_set_channels (ctxt, part_index, channels)); /* do not support updating channels during update operation... */ if (pctxt->mode != EXR_CONTEXT_WRITE) @@ -1868,7 +1866,8 @@ exr_attr_set_string ( EXR_PROMOTE_LOCKED_CONTEXT_AND_PART_OR_ERROR (ctxt, part_index); if (name && !strcmp (name, EXR_REQ_NAME_STR)) - return EXR_UNLOCK_AND_RETURN_PCTXT (exr_set_name (ctxt, part_index, name)); + return EXR_UNLOCK_AND_RETURN_PCTXT ( + exr_set_name (ctxt, part_index, name)); if (name && !strcmp (name, EXR_REQ_TYPE_STR)) return EXR_UNLOCK_AND_RETURN_PCTXT (pctxt->print_error ( diff --git a/src/lib/OpenEXRCore/string.c b/src/lib/OpenEXRCore/string.c index aa68b64c88..81b3b84e22 100644 --- a/src/lib/OpenEXRCore/string.c +++ b/src/lib/OpenEXRCore/string.c @@ -189,7 +189,7 @@ exr_attr_string_set_with_length ( char* sstr; s->length = len; - sstr = EXR_CONST_CAST (char*, s->str); + sstr = EXR_CONST_CAST (char*, s->str); if (len > 0) { #ifdef _MSC_VER diff --git a/src/lib/OpenEXRCore/unpack.c b/src/lib/OpenEXRCore/unpack.c index 75c016d1fd..b88c98974c 100644 --- a/src/lib/OpenEXRCore/unpack.c +++ b/src/lib/OpenEXRCore/unpack.c @@ -31,7 +31,7 @@ half_to_float8 (float* out, const uint16_t* src) } #endif -#if (defined(__x86_64__) || defined(_M_X64)) && defined(__AVX__) && \ +#if (defined(__x86_64__) || defined(_M_X64)) && defined(__AVX__) && \ (defined(__F16C__) || defined(__GNUC__) || defined(__clang__)) # if defined(__F16C__) diff --git a/src/lib/OpenEXRCore/write_header.c b/src/lib/OpenEXRCore/write_header.c index 955760d08b..aa148485a0 100644 --- a/src/lib/OpenEXRCore/write_header.c +++ b/src/lib/OpenEXRCore/write_header.c @@ -634,8 +634,8 @@ internal_exr_write_header (struct _internal_exr_context* ctxt) { for (int a = 0; a < curp->attributes.num_attributes; ++a) { - exr_attribute_t *curattr = curp->attributes.sorted_entries[a]; - if (0 == (flags & (EXR_MULTI_PART_FLAG |EXR_NON_IMAGE_FLAG)) && + exr_attribute_t* curattr = curp->attributes.sorted_entries[a]; + if (0 == (flags & (EXR_MULTI_PART_FLAG | EXR_NON_IMAGE_FLAG)) && 1 == ctxt->num_parts) { if (0 == strcmp (curattr->name, "type") || diff --git a/src/test/OpenEXRCoreTest/buffer.cpp b/src/test/OpenEXRCoreTest/buffer.cpp index 041befe0a4..e3fcdf96b9 100644 --- a/src/test/OpenEXRCoreTest/buffer.cpp +++ b/src/test/OpenEXRCoreTest/buffer.cpp @@ -29,9 +29,9 @@ void testBufferCompression (const std::string& tempdir) buf = {'O', 'p', 'e', 'n', 'E', 'X', 'R'}; cbuf.resize( exr_compress_max_buffer_size (buf.size()) ); size_t outsz; - EXRCORE_TEST_RVAL (exr_compress_buffer (9, buf.data(), buf.size(), &cbuf[0], cbuf.size(), &outsz)); + EXRCORE_TEST_RVAL (exr_compress_buffer (nullptr, 9, buf.data(), buf.size(), &cbuf[0], cbuf.size(), &outsz)); std::cout << "compressed size: " << outsz << std::endl; - EXRCORE_TEST_RVAL (exr_uncompress_buffer (cbuf.data(), outsz, &buf[0], buf.size(), &outsz)); + EXRCORE_TEST_RVAL (exr_uncompress_buffer (nullptr, cbuf.data(), outsz, &buf[0], buf.size(), &outsz)); std::cout << "uncompressed size: " << outsz << std::endl; if (buf[0] != 'O') EXRCORE_TEST_FAIL(buf[0] != 'O'); diff --git a/src/test/OpenEXRTest/testIDManifest.cpp b/src/test/OpenEXRTest/testIDManifest.cpp index 0841260a1e..135b931ca9 100644 --- a/src/test/OpenEXRTest/testIDManifest.cpp +++ b/src/test/OpenEXRTest/testIDManifest.cpp @@ -141,7 +141,8 @@ doReadWriteManifest (const IDManifest& mfst, const string& fn, bool dump) vector compressed (compressedDataSize); exr_compress_buffer ( - 6, + nullptr, + 4, str.str ().c_str (), sourceDataSize, compressed.data (),