diff --git a/tutorials/common/image/tinyexr.h b/tutorials/common/image/tinyexr.h index fd964646d7..a3aa50db54 100644 --- a/tutorials/common/image/tinyexr.h +++ b/tutorials/common/image/tinyexr.h @@ -104,7 +104,7 @@ extern "C" { #endif // Use miniz or not to decode ZIP format pixel. Linking with zlib -// required if this flas is 0 and TINYEXR_USE_STB_ZLIB is 0. +// required if this flag is 0 and TINYEXR_USE_STB_ZLIB is 0. #ifndef TINYEXR_USE_MINIZ #define TINYEXR_USE_MINIZ (1) #endif @@ -114,7 +114,12 @@ extern "C" { #define TINYEXR_USE_STB_ZLIB (0) #endif -// Disable PIZ comporession when applying cpplint. +// Use nanozlib. +#ifndef TINYEXR_USE_NANOZLIB +#define TINYEXR_USE_NANOZLIB (0) +#endif + +// Disable PIZ compression when applying cpplint. #ifndef TINYEXR_USE_PIZ #define TINYEXR_USE_PIZ (1) #endif @@ -126,7 +131,11 @@ extern "C" { #ifndef TINYEXR_USE_THREAD #define TINYEXR_USE_THREAD (0) // No threaded loading. -// http://computation.llnl.gov/projects/floating-point-compression +#else +// When using threading a reduced custom upperbound can be specified by setting TINYEXR_MAX_THREADS +#ifndef TINYEXR_MAX_THREADS // if not defined define it as 0 meaning upper limit is taken from hardware_concurrency() +#define TINYEXR_MAX_THREADS (0) +#endif #endif #ifndef TINYEXR_USE_OPENMP @@ -137,6 +146,41 @@ extern "C" { #endif #endif +#ifndef TINYEXR_USE_COMPILER_FP16 +#define TINYEXR_USE_COMPILER_FP16 (0) +#endif + +#if TINYEXR_USE_COMPILER_FP16 +#ifndef _MSC_VER +#if defined( __GNUC__ ) || defined( __clang__ ) +#if defined( __SSE2__ ) +#if ( __GNUC__ > 11 ) || ( __clang_major__ > 14 ) +#ifndef __STDC_WANT_IEC_60559_TYPES_EXT__ +#define __STDC_WANT_IEC_60559_TYPES_EXT__ +#endif +#include +#include +#define TINYEXR_FP16_COMPILER_TYPE _Float16 +#endif +#endif +#if defined( __ARM_NEON__ ) || defined( __ARM_NEON ) +#define TINYEXR_FP16_COMPILER_TYPE __fp16 +#endif +#endif +#else +#if (defined(_M_IX86) || defined(_M_X64)) && defined(__AVX2__) +#include +#define TINYEXR_FP16_COMPILER_TYPE uint16_t +#endif +#endif +#endif + +#ifdef TINYEXR_FP16_COMPILER_TYPE +#define TINYEXR_HAS_FP16_COMPILER_TYPE (1) +#else +#define TINYEXR_HAS_FP16_COMPILER_TYPE (0) +#endif + #define TINYEXR_SUCCESS (0) #define TINYEXR_ERROR_INVALID_MAGIC_NUMBER (-1) #define TINYEXR_ERROR_INVALID_EXR_VERSION (-2) @@ -185,11 +229,11 @@ typedef struct TEXRVersion { int version; // this must be 2 // tile format image; // not zero for only a single-part "normal" tiled file (according to spec.) - int tiled; + int tiled; int long_name; // long name attribute // deep image(EXR 2.0); // for a multi-part file, indicates that at least one part is of type deep* (according to spec.) - int non_image; + int non_image; int multipart; // multi-part(EXR 2.0) } EXRVersion; @@ -381,7 +425,7 @@ extern int IsEXRFromMemory(const unsigned char *memory, size_t size); // error extern int SaveEXRToMemory(const float *data, const int width, const int height, const int components, const int save_as_fp16, - const unsigned char **buffer, const char **err); + unsigned char **buffer, const char **err); // @deprecated { Not recommended, but handy to use. } // Saves single-frame OpenEXR image to a buffer. Assume EXR image contains RGB(A) channels. @@ -607,18 +651,27 @@ extern int LoadEXRFromMemory(float **out_rgba, int *width, int *height, #ifndef NOMINMAX #define NOMINMAX #endif -#include // for UTF-8 +#include // for UTF-8 and memory-mapping + +#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP) +#define TINYEXR_USE_WIN32_MMAP (1) +#endif +#elif defined(__linux__) || defined(__unix__) +#include // for open() +#include // for memory-mapping +#include // for stat +#include // for close() +#define TINYEXR_USE_POSIX_MMAP (1) #endif #include -#include #include #include #include #include -// #include // debug +//#include // debug #include #include @@ -636,13 +689,15 @@ extern int LoadEXRFromMemory(float **out_rgba, int *width, int *height, #include #endif +#else // __cplusplus > 199711L +#define TINYEXR_HAS_CXX11 (0) #endif // __cplusplus > 199711L #if TINYEXR_USE_OPENMP #include #endif -#if TINYEXR_USE_MINIZ +#if defined(TINYEXR_USE_MINIZ) && (TINYEXR_USE_MINIZ==1) #include #else // Issue #46. Please include your own zlib-compatible API header before @@ -650,6 +705,11 @@ extern int LoadEXRFromMemory(float **out_rgba, int *width, int *height, //#include "zlib.h" #endif +#if defined(TINYEXR_USE_NANOZLIB) && (TINYEXR_USE_NANOZLIB==1) +#define NANOZLIB_IMPLEMENTATION +#include "nanozlib.h" +#endif + #if TINYEXR_USE_STB_ZLIB // Since we don't know where a project has stb_image.h and stb_image_write.h // and whether they are in the include path, we don't include them here, and @@ -660,6 +720,7 @@ extern "C" int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuf extern "C" unsigned char *stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality); #endif + #if TINYEXR_USE_ZFP #ifdef __clang__ @@ -675,6 +736,27 @@ extern "C" unsigned char *stbi_zlib_compress(unsigned char *data, int data_len, #endif +// cond: conditional expression +// msg: std::string +// err: std::string* +#define TINYEXR_CHECK_AND_RETURN_MSG(cond, msg, err) do { \ + if (!(cond)) { \ + if (!err) { \ + std::ostringstream ss_e; \ + ss_e << __func__ << "():" << __LINE__ << msg << "\n"; \ + (*err) += ss_e.str(); \ + } \ + return false;\ + } \ + } while(0) + +// no error message. +#define TINYEXR_CHECK_AND_RETURN_C(cond, retcode) do { \ + if (!(cond)) { \ + return retcode; \ + } \ + } while(0) + namespace tinyexr { #if __cplusplus > 199711L @@ -728,7 +810,7 @@ static void SetWarningMessage(const std::string &msg, const char **warn) { static const int kEXRVersionSize = 8; -static void cpy2(unsigned short *dst_val, const unsigned short *src_val) { +static void inline cpy2(unsigned short *dst_val, const unsigned short *src_val) { unsigned char *dst = reinterpret_cast(dst_val); const unsigned char *src = reinterpret_cast(src_val); @@ -736,8 +818,8 @@ static void cpy2(unsigned short *dst_val, const unsigned short *src_val) { dst[1] = src[1]; } -static void swap2(unsigned short *val) { -#ifdef TINYEXR_LITTLE_ENDIAN +static void inline swap2(unsigned short *val) { +#if TINYEXR_LITTLE_ENDIAN (void)val; #else unsigned short tmp = *val; @@ -758,7 +840,7 @@ static void swap2(unsigned short *val) { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" #endif -static void cpy4(int *dst_val, const int *src_val) { +static void inline cpy4(int *dst_val, const int *src_val) { unsigned char *dst = reinterpret_cast(dst_val); const unsigned char *src = reinterpret_cast(src_val); @@ -768,7 +850,7 @@ static void cpy4(int *dst_val, const int *src_val) { dst[3] = src[3]; } -static void cpy4(unsigned int *dst_val, const unsigned int *src_val) { +static void inline cpy4(unsigned int *dst_val, const unsigned int *src_val) { unsigned char *dst = reinterpret_cast(dst_val); const unsigned char *src = reinterpret_cast(src_val); @@ -778,7 +860,7 @@ static void cpy4(unsigned int *dst_val, const unsigned int *src_val) { dst[3] = src[3]; } -static void cpy4(float *dst_val, const float *src_val) { +static void inline cpy4(float *dst_val, const float *src_val) { unsigned char *dst = reinterpret_cast(dst_val); const unsigned char *src = reinterpret_cast(src_val); @@ -795,8 +877,8 @@ static void cpy4(float *dst_val, const float *src_val) { #pragma GCC diagnostic pop #endif -static void swap4(unsigned int *val) { -#ifdef TINYEXR_LITTLE_ENDIAN +static void inline swap4(unsigned int *val) { +#if TINYEXR_LITTLE_ENDIAN (void)val; #else unsigned int tmp = *val; @@ -810,8 +892,8 @@ static void swap4(unsigned int *val) { #endif } -static void swap4(int *val) { -#ifdef TINYEXR_LITTLE_ENDIAN +static void inline swap4(int *val) { +#if TINYEXR_LITTLE_ENDIAN (void)val; #else int tmp = *val; @@ -825,8 +907,8 @@ static void swap4(int *val) { #endif } -static void swap4(float *val) { -#ifdef TINYEXR_LITTLE_ENDIAN +static void inline swap4(float *val) { +#if TINYEXR_LITTLE_ENDIAN (void)val; #else float tmp = *val; @@ -841,7 +923,7 @@ static void swap4(float *val) { } #if 0 -static void cpy8(tinyexr::tinyexr_uint64 *dst_val, const tinyexr::tinyexr_uint64 *src_val) { +static void inline cpy8(tinyexr::tinyexr_uint64 *dst_val, const tinyexr::tinyexr_uint64 *src_val) { unsigned char *dst = reinterpret_cast(dst_val); const unsigned char *src = reinterpret_cast(src_val); @@ -856,8 +938,8 @@ static void cpy8(tinyexr::tinyexr_uint64 *dst_val, const tinyexr::tinyexr_uint64 } #endif -static void swap8(tinyexr::tinyexr_uint64 *val) { -#ifdef TINYEXR_LITTLE_ENDIAN +static void inline swap8(tinyexr::tinyexr_uint64 *val) { +#if TINYEXR_LITTLE_ENDIAN (void)val; #else tinyexr::tinyexr_uint64 tmp = (*val); @@ -876,6 +958,11 @@ static void swap8(tinyexr::tinyexr_uint64 *val) { } // https://gist.github.com/rygorous/2156668 +#if TINYEXR_HAS_FP16_COMPILER_TYPE && (TINYEXR_USE_COMPILER_FP16 > 0) +union FP32 { + float f; +}; +#else union FP32 { unsigned int u; float f; @@ -891,12 +978,21 @@ union FP32 { #endif } s; }; +#endif #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wpadded" #endif +#if TINYEXR_HAS_FP16_COMPILER_TYPE && (TINYEXR_USE_COMPILER_FP16 > 0) +union FP16 { + TINYEXR_FP16_COMPILER_TYPE f; + unsigned short u; +}; + +#else + union FP16 { unsigned short u; struct { @@ -911,11 +1007,32 @@ union FP16 { #endif } s; }; +#endif #ifdef __clang__ #pragma clang diagnostic pop #endif +#if TINYEXR_HAS_FP16_COMPILER_TYPE && (TINYEXR_USE_COMPILER_FP16 > 0) +static inline FP32 half_to_float(FP16 h) { + FP32 o; +#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) && defined(__AVX2__) + o.f =_mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(static_cast (h.u)))); +#else + o.f = static_cast (h.f); +#endif + return o; +} +static inline FP16 float_to_half_full(FP32 f) { + FP16 o; +#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) && defined(__AVX2__) + o.f = static_cast (_mm_cvtsi128_si32(_mm_cvtps_ph(_mm_set_ss(f.f), _MM_FROUND_CUR_DIRECTION))); +#else + o.f = static_cast (f.f); +#endif + return o; +} +#else static FP32 half_to_float(FP16 h) { static const FP32 magic = {113 << 23}; static const unsigned int shifted_exp = 0x7c00 @@ -975,7 +1092,7 @@ static FP16 float_to_half_full(FP32 f) { o.s.Sign = f.s.Sign; return o; } - +#endif // NOTE: From OpenEXR code // #define IMF_INCREASING_Y 0 // #define IMF_DECREASING_Y 1 @@ -1182,6 +1299,7 @@ static bool ReadChannelInfo(std::vector &channels, break; } ChannelInfo info; + info.requested_pixel_type = 0; tinyexr_int64 data_len = static_cast(data.size()) - (p - reinterpret_cast(data.data())); @@ -1313,7 +1431,7 @@ static bool CompressZip(unsigned char *dst, } } -#if TINYEXR_USE_MINIZ +#if defined(TINYEXR_USE_MINIZ) && (TINYEXR_USE_MINIZ==1) // // Compress the data using miniz // @@ -1327,7 +1445,7 @@ static bool CompressZip(unsigned char *dst, } compressedSize = outSize; -#elif TINYEXR_USE_STB_ZLIB +#elif defined(TINYEXR_USE_STB_ZLIB) && (TINYEXR_USE_STB_ZLIB==1) int outSize; unsigned char* ret = stbi_zlib_compress(const_cast(&tmpBuf.at(0)), src_size, &outSize, 8); if (!ret) { @@ -1336,6 +1454,18 @@ static bool CompressZip(unsigned char *dst, memcpy(dst, ret, outSize); free(ret); + compressedSize = outSize; +#elif defined(TINYEXR_USE_NANOZLIB) && (TINYEXR_USE_NANOZLIB==1) + uint64_t dstSize = nanoz_compressBound(static_cast(src_size)); + int outSize{0}; + unsigned char *ret = nanoz_compress(&tmpBuf.at(0), src_size, &outSize, /* quality */8); + if (!ret) { + return false; + } + + memcpy(dst, ret, outSize); + free(ret); + compressedSize = outSize; #else uLong outSize = compressBound(static_cast(src_size)); @@ -1368,7 +1498,7 @@ static bool DecompressZip(unsigned char *dst, } std::vector tmpBuf(*uncompressed_size); -#if TINYEXR_USE_MINIZ +#if defined(TINYEXR_USE_MINIZ) && (TINYEXR_USE_MINIZ==1) int ret = mz_uncompress(&tmpBuf.at(0), uncompressed_size, src, src_size); if (MZ_OK != ret) { @@ -1380,6 +1510,17 @@ static bool DecompressZip(unsigned char *dst, if (ret < 0) { return false; } +#elif defined(TINYEXR_USE_NANOZLIB) && (TINYEXR_USE_NANOZLIB==1) + uint64_t dest_size = (*uncompressed_size); + uint64_t uncomp_size{0}; + nanoz_status_t ret = + nanoz_uncompress(src, src_size, dest_size, &tmpBuf.at(0), &uncomp_size); + if (NANOZ_SUCCESS != ret) { + return false; + } + if ((*uncompressed_size) != uncomp_size) { + return false; + } #else int ret = uncompress(&tmpBuf.at(0), uncompressed_size, src, src_size); if (Z_OK != ret) { @@ -1507,7 +1648,7 @@ static int rleCompress(int inLength, const char in[], signed char out[]) { // // Uncompress an array of bytes compressed with rleCompress(). -// Returns the length of the oncompressed data, or 0 if the +// Returns the length of the uncompressed data, or 0 if the // length of the uncompressed data would be more than maxLength. // @@ -1548,7 +1689,7 @@ static int rleUncompress(int inLength, int maxLength, const signed char in[], // End of RLE code from OpenEXR ----------------------------------- -static void CompressRle(unsigned char *dst, +static bool CompressRle(unsigned char *dst, tinyexr::tinyexr_uint64 &compressedSize, const unsigned char *src, unsigned long src_size) { std::vector tmpBuf(src_size); @@ -1603,7 +1744,7 @@ static void CompressRle(unsigned char *dst, int outSize = rleCompress(static_cast(src_size), reinterpret_cast(&tmpBuf.at(0)), reinterpret_cast(dst)); - assert(outSize > 0); + TINYEXR_CHECK_AND_RETURN_C(outSize > 0, false); compressedSize = static_cast(outSize); @@ -1613,6 +1754,8 @@ static void CompressRle(unsigned char *dst, compressedSize = src_size; memcpy(dst, src, src_size); } + + return true; } static bool DecompressRle(unsigned char *dst, @@ -2152,7 +2295,7 @@ struct FHeapCompare { bool operator()(long long *a, long long *b) { return *a > *b; } }; -static void hufBuildEncTable( +static bool hufBuildEncTable( long long *frq, // io: input frequencies [HUF_ENCSIZE], output table int *im, // o: min frq index int *iM) // o: max frq index @@ -2280,7 +2423,7 @@ static void hufBuildEncTable( for (int j = m;; j = hlink[j]) { scode[j]++; - assert(scode[j] <= 58); + TINYEXR_CHECK_AND_RETURN_C(scode[j] <= 58, false); if (hlink[j] == j) { // @@ -2299,7 +2442,7 @@ static void hufBuildEncTable( for (int j = mm;; j = hlink[j]) { scode[j]++; - assert(scode[j] <= 58); + TINYEXR_CHECK_AND_RETURN_C(scode[j] <= 58, false); if (hlink[j] == j) break; } @@ -2313,6 +2456,8 @@ static void hufBuildEncTable( hufCanonicalCodeTable(scode.data()); memcpy(frq, scode.data(), sizeof(long long) * HUF_ENCSIZE); + + return true; } // @@ -2510,7 +2655,7 @@ static bool hufBuildDecTable(const long long *hcode, // i : encoding table unsigned int *p = pl->p; pl->p = new unsigned int[pl->lit]; - for (unsigned int i = 0; i < pl->lit - 1; ++i) pl->p[i] = p[i]; + for (unsigned int i = 0; i < pl->lit - 1u; ++i) pl->p[i] = p[i]; delete[] p; } else { @@ -3024,7 +3169,6 @@ static bool CompressPiz(unsigned char *outPtr, unsigned int *outSize, #if !TINYEXR_LITTLE_ENDIAN // @todo { PIZ compression on BigEndian architecture. } - assert(0); return false; #endif @@ -3150,27 +3294,52 @@ static bool DecompressPiz(unsigned char *outPtr, const unsigned char *inPtr, #if !TINYEXR_LITTLE_ENDIAN // @todo { PIZ compression on BigEndian architecture. } - assert(0); return false; #endif memset(bitmap.data(), 0, BITMAP_SIZE); + if (inLen < 4) { + return false; + } + + size_t readLen = 0; + const unsigned char *ptr = inPtr; // minNonZero = *(reinterpret_cast(ptr)); tinyexr::cpy2(&minNonZero, reinterpret_cast(ptr)); // maxNonZero = *(reinterpret_cast(ptr + 2)); tinyexr::cpy2(&maxNonZero, reinterpret_cast(ptr + 2)); ptr += 4; + readLen += 4; if (maxNonZero >= BITMAP_SIZE) { return false; } + //printf("maxNonZero = %d\n", maxNonZero); + //printf("minNonZero = %d\n", minNonZero); + //printf("len = %d\n", (maxNonZero - minNonZero + 1)); + //printf("BITMAPSIZE - min = %d\n", (BITMAP_SIZE - minNonZero)); + if (minNonZero <= maxNonZero) { + if (((maxNonZero - minNonZero + 1) + readLen) > inLen) { + // Input too short + return false; + } + memcpy(reinterpret_cast(&bitmap[0] + minNonZero), ptr, maxNonZero - minNonZero + 1); ptr += maxNonZero - minNonZero + 1; + readLen += maxNonZero - minNonZero + 1; + } else { + // Issue 194 + if ((minNonZero == (BITMAP_SIZE - 1)) && (maxNonZero == 0)) { + // OK. all pixels are zero. And no need to read `bitmap` data. + } else { + // invalid minNonZero/maxNonZero combination. + return false; + } } std::vector lut(USHORT_RANGE); @@ -3181,7 +3350,11 @@ static bool DecompressPiz(unsigned char *outPtr, const unsigned char *inPtr, // Huffman decoding // - int length; + if ((readLen + 4) > inLen) { + return false; + } + + int length=0; // length = *(reinterpret_cast(ptr)); tinyexr::cpy4(&length, reinterpret_cast(ptr)); @@ -3362,8 +3535,8 @@ static bool DecompressZfp(float *dst, int dst_width, int dst_num_lines, zfp_stream *zfp = NULL; zfp_field *field = NULL; - assert((dst_width % 4) == 0); - assert((dst_num_lines % 4) == 0); + TINYEXR_CHECK_AND_RETURN_C((dst_width % 4) == 0, false); + TINYEXR_CHECK_AND_RETURN_C((dst_num_lines % 4) == 0, false); if ((size_t(dst_width) & 3U) || (size_t(dst_num_lines) & 3U)) { return false; @@ -3384,7 +3557,7 @@ static bool DecompressZfp(float *dst, int dst_width, int dst_num_lines, } else if (param.type == TINYEXR_ZFP_COMPRESSIONTYPE_ACCURACY) { zfp_stream_set_accuracy(zfp, param.tolerance); } else { - assert(0); + return false; } size_t buf_size = zfp_stream_maximum_size(zfp, field); @@ -3428,8 +3601,8 @@ static bool CompressZfp(std::vector *outBuf, zfp_stream *zfp = NULL; zfp_field *field = NULL; - assert((width % 4) == 0); - assert((num_lines % 4) == 0); + TINYEXR_CHECK_AND_RETURN_C((width % 4) == 0, false); + TINYEXR_CHECK_AND_RETURN_C((num_lines % 4) == 0, false); if ((size_t(width) & 3U) || (size_t(num_lines) & 3U)) { return false; @@ -3449,7 +3622,7 @@ static bool CompressZfp(std::vector *outBuf, } else if (param.type == TINYEXR_ZFP_COMPRESSIONTYPE_ACCURACY) { zfp_stream_set_accuracy(zfp, param.tolerance); } else { - assert(0); + return false; } size_t buf_size = zfp_stream_maximum_size(zfp, field); @@ -3586,7 +3759,7 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, } } } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_UINT) { - assert(requested_pixel_types[c] == TINYEXR_PIXELTYPE_UINT); + TINYEXR_CHECK_AND_RETURN_C(requested_pixel_types[c] == TINYEXR_PIXELTYPE_UINT, false); for (size_t v = 0; v < static_cast(num_lines); v++) { const unsigned int *line_ptr = reinterpret_cast( @@ -3615,7 +3788,7 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, } } } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) { - assert(requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT); + TINYEXR_CHECK_AND_RETURN_C(requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT, false); for (size_t v = 0; v < static_cast(num_lines); v++) { const float *line_ptr = reinterpret_cast(&outBuf.at( v * pixel_data_size * static_cast(width) + @@ -3642,11 +3815,10 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, } } } else { - assert(0); + return false; } } #else - assert(0 && "PIZ is enabled in this build"); return false; #endif @@ -3658,7 +3830,7 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, pixel_data_size); unsigned long dstLen = static_cast(outBuf.size()); - assert(dstLen > 0); + TINYEXR_CHECK_AND_RETURN_C(dstLen > 0, false); if (!tinyexr::DecompressZip( reinterpret_cast(&outBuf.at(0)), &dstLen, data_ptr, static_cast(data_len))) { @@ -3725,7 +3897,7 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, } } } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_UINT) { - assert(requested_pixel_types[c] == TINYEXR_PIXELTYPE_UINT); + TINYEXR_CHECK_AND_RETURN_C(requested_pixel_types[c] == TINYEXR_PIXELTYPE_UINT, false); for (size_t v = 0; v < static_cast(num_lines); v++) { const unsigned int *line_ptr = reinterpret_cast( @@ -3754,7 +3926,7 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, } } } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) { - assert(requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT); + TINYEXR_CHECK_AND_RETURN_C(requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT, false); for (size_t v = 0; v < static_cast(num_lines); v++) { const float *line_ptr = reinterpret_cast( &outBuf.at(v * pixel_data_size * static_cast(width) + @@ -3781,7 +3953,6 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, } } } else { - assert(0); return false; } } @@ -3859,7 +4030,7 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, } } } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_UINT) { - assert(requested_pixel_types[c] == TINYEXR_PIXELTYPE_UINT); + TINYEXR_CHECK_AND_RETURN_C(requested_pixel_types[c] == TINYEXR_PIXELTYPE_UINT, false); for (size_t v = 0; v < static_cast(num_lines); v++) { const unsigned int *line_ptr = reinterpret_cast( @@ -3888,7 +4059,7 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, } } } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) { - assert(requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT); + TINYEXR_CHECK_AND_RETURN_C(requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT, false); for (size_t v = 0; v < static_cast(num_lines); v++) { const float *line_ptr = reinterpret_cast( &outBuf.at(v * pixel_data_size * static_cast(width) + @@ -3915,7 +4086,6 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, } } } else { - assert(0); return false; } } @@ -3926,7 +4096,6 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, if (!tinyexr::FindZFPCompressionParam(&zfp_compression_param, attributes, int(num_attributes), &e)) { // This code path should not be reachable. - assert(0); return false; } @@ -3936,7 +4105,7 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, pixel_data_size); unsigned long dstLen = outBuf.size(); - assert(dstLen > 0); + TINYEXR_CHECK_AND_RETURN_C(dstLen > 0, false); tinyexr::DecompressZfp(reinterpret_cast(&outBuf.at(0)), width, num_lines, num_channels, data_ptr, static_cast(data_len), @@ -3953,9 +4122,9 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, // pixel sample data for channel n for scanline 1 // ... for (size_t c = 0; c < static_cast(num_channels); c++) { - assert(channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT); + TINYEXR_CHECK_AND_RETURN_C(channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT, false); if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) { - assert(requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT); + TINYEXR_CHECK_AND_RETURN_C(requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT, false); for (size_t v = 0; v < static_cast(num_lines); v++) { const float *line_ptr = reinterpret_cast( &outBuf.at(v * pixel_data_size * static_cast(width) + @@ -3981,7 +4150,6 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, } } } else { - assert(0); return false; } } @@ -3989,7 +4157,6 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, (void)attributes; (void)num_attributes; (void)num_channels; - assert(0); return false; #endif } else if (compression_type == TINYEXR_COMPRESSIONTYPE_NONE) { @@ -4039,7 +4206,7 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, for (int u = 0; u < width; u++) { tinyexr::FP16 hf; - // address may not be aliged. use byte-wise copy for safety.#76 + // address may not be aligned. use byte-wise copy for safety.#76 // hf.u = line_ptr[u]; tinyexr::cpy2(&(hf.u), line_ptr + u); @@ -4050,7 +4217,6 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, outLine[u] = f32.f; } } else { - assert(0); return false; } } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) { @@ -4094,12 +4260,13 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, (size_t(height) - 1 - (size_t(y) + v)) * size_t(x_stride); } + if (reinterpret_cast(line_ptr + width) > + (data_ptr + data_len)) { + // Corrupted data + return false; + } + for (int u = 0; u < width; u++) { - if (reinterpret_cast(line_ptr + u) >= - (data_ptr + data_len)) { - // Corrupsed data? - return false; - } unsigned int val; tinyexr::cpy4(&val, line_ptr + u); @@ -4119,7 +4286,7 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, static bool DecodeTiledPixelData( unsigned char **out_images, int *width, int *height, const int *requested_pixel_types, const unsigned char *data_ptr, - size_t data_len, int compression_type, int line_order, int data_width, + size_t data_len, int compression_type, int data_width, int data_height, int tile_offset_x, int tile_offset_y, int tile_size_x, int tile_size_y, size_t pixel_data_size, size_t num_attributes, const EXRAttribute *attributes, size_t num_channels, @@ -4145,8 +4312,9 @@ static bool DecodeTiledPixelData( } // Image size = tile size. + // Line order within tiles is always increasing. return DecodePixelData(out_images, requested_pixel_types, data_ptr, data_len, - compression_type, line_order, (*width), tile_size_y, + compression_type, /* line_order*/ 0, (*width), tile_size_y, /* stride */ tile_size_x, /* y */ 0, /* line_no */ 0, (*height), pixel_data_size, num_attributes, attributes, num_channels, channels, channel_offset_list); @@ -4180,14 +4348,21 @@ static bool ComputeChannelLayout(std::vector *channel_offset_list, return true; } +// TODO: Simply return nullptr when failed to allocate? static unsigned char **AllocateImage(int num_channels, const EXRChannelInfo *channels, const int *requested_pixel_types, - int data_width, int data_height) { + int data_width, int data_height, bool *success) { unsigned char **images = reinterpret_cast(static_cast( malloc(sizeof(float *) * static_cast(num_channels)))); + for (size_t c = 0; c < static_cast(num_channels); c++) { + images[c] = NULL; + } + + bool valid = true; + for (size_t c = 0; c < static_cast(num_channels); c++) { size_t data_len = static_cast(data_width) * static_cast(data_height); @@ -4203,7 +4378,9 @@ static unsigned char **AllocateImage(int num_channels, images[c] = reinterpret_cast( static_cast(malloc(sizeof(float) * data_len))); } else { - assert(0); + images[c] = NULL; // just in case. + valid = false; + break; } } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) { // pixel_data_size += sizeof(float); @@ -4216,7 +4393,26 @@ static unsigned char **AllocateImage(int num_channels, images[c] = reinterpret_cast( static_cast(malloc(sizeof(unsigned int) * data_len))); } else { - assert(0); + images[c] = NULL; // just in case. + valid = false; + break; + } + } + + if (!valid) { + for (size_t c = 0; c < static_cast(num_channels); c++) { + if (images[c]) { + free(images[c]); + images[c] = NULL; + } + } + + if (success) { + (*success) = false; + } + } else { + if (success) { + (*success) = true; } } @@ -4339,7 +4535,6 @@ static int ParseEXRHeader(HeaderInfo *info, bool *empty_header, return TINYEXR_ERROR_INVALID_DATA; } - assert(data.size() == 9); memcpy(&x_size, &data.at(0), sizeof(int)); memcpy(&y_size, &data.at(4), sizeof(int)); tile_mode = data[8]; @@ -4722,10 +4917,11 @@ static bool ConvertHeader(EXRHeader *exr_header, const HeaderInfo &info, std::st struct OffsetData { OffsetData() : num_x_levels(0), num_y_levels(0) {} std::vector > > offsets; - int num_x_levels; - int num_y_levels; + int num_x_levels; + int num_y_levels; }; +// -1 = error static int LevelIndex(int lx, int ly, int tile_level_mode, int num_x_levels) { switch (tile_level_mode) { case TINYEXR_TILE_ONE_LEVEL: @@ -4738,13 +4934,15 @@ static int LevelIndex(int lx, int ly, int tile_level_mode, int num_x_levels) { return lx + ly * num_x_levels; default: - assert(false); + return -1; } return 0; } static int LevelSize(int toplevel_size, int level, int tile_rounding_mode) { - assert(level >= 0); + if (level < 0) { + return -1; + } int b = static_cast(1u << static_cast(level)); int level_size = toplevel_size / b; @@ -4765,9 +4963,13 @@ static int DecodeTiledLevel(EXRImage* exr_image, const EXRHeader* exr_header, int level_index = LevelIndex(exr_image->level_x, exr_image->level_y, exr_header->tile_level_mode, offset_data.num_x_levels); int num_y_tiles = int(offset_data.offsets[size_t(level_index)].size()); - assert(num_y_tiles); + if (num_y_tiles < 1) { + return TINYEXR_ERROR_INVALID_DATA; + } int num_x_tiles = int(offset_data.offsets[size_t(level_index)][0].size()); - assert(num_x_tiles); + if (num_x_tiles < 1) { + return TINYEXR_ERROR_INVALID_DATA; + } int num_tiles = num_x_tiles * num_y_tiles; int err_code = TINYEXR_SUCCESS; @@ -4783,7 +4985,7 @@ static int DecodeTiledLevel(EXRImage* exr_image, const EXRHeader* exr_header, #else unsigned error_flag(EF_SUCCESS); #endif - + // Although the spec says : "...the data window is subdivided into an array of smaller rectangles...", // the IlmImf library allows the dimensions of the tile to be larger (or equal) than the dimensions of the data window. #if 0 @@ -4796,17 +4998,19 @@ static int DecodeTiledLevel(EXRImage* exr_image, const EXRHeader* exr_header, } #endif exr_image->tiles = static_cast( - calloc(sizeof(EXRTile), static_cast(num_tiles))); + calloc(static_cast(num_tiles), sizeof(EXRTile))); #if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0) std::vector workers; std::atomic tile_count(0); int num_threads = std::max(1, int(std::thread::hardware_concurrency())); +#if (TINYEXR_MAX_THREADS > 0) + num_threads = std::min(num_threads,TINYEXR_MAX_THREADS); +#endif if (num_threads > int(num_tiles)) { num_threads = int(num_tiles); } - for (int t = 0; t < num_threads; t++) { workers.emplace_back(std::thread([&]() { @@ -4820,10 +5024,16 @@ static int DecodeTiledLevel(EXRImage* exr_image, const EXRHeader* exr_header, for (int tile_idx = 0; tile_idx < num_tiles; tile_idx++) { #endif // Allocate memory for each tile. + bool alloc_success = false; exr_image->tiles[tile_idx].images = tinyexr::AllocateImage( num_channels, exr_header->channels, exr_header->requested_pixel_types, exr_header->tile_size_x, - exr_header->tile_size_y); + exr_header->tile_size_y, &alloc_success); + + if (!alloc_success) { + error_flag |= EF_INVALID_DATA; + continue; + } int x_tile = tile_idx % num_x_tiles; int y_tile = tile_idx / num_x_tiles; @@ -4833,7 +5043,7 @@ static int DecodeTiledLevel(EXRImage* exr_image, const EXRHeader* exr_header, tinyexr::tinyexr_uint64 offset = offset_data.offsets[size_t(level_index)][size_t(y_tile)][size_t(x_tile)]; if (offset + sizeof(int) * 5 > size) { // Insufficient data size. - error_flag |= EF_INSUFFICIENT_DATA; + error_flag |= EF_INSUFFICIENT_DATA; continue; } @@ -4879,7 +5089,6 @@ static int DecodeTiledLevel(EXRImage* exr_image, const EXRHeader* exr_header, &(exr_image->tiles[tile_idx].height), exr_header->requested_pixel_types, data_ptr, static_cast(data_len), exr_header->compression_type, - exr_header->line_order, exr_image->width, exr_image->height, tile_coordinates[0], tile_coordinates[1], exr_header->tile_size_x, exr_header->tile_size_y, static_cast(pixel_data_size), @@ -4899,7 +5108,7 @@ static int DecodeTiledLevel(EXRImage* exr_image, const EXRHeader* exr_header, exr_image->tiles[tile_idx].level_y = tile_coordinates[3]; #if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0) - } + } })); } // num_thread loop @@ -4959,10 +5168,24 @@ static int DecodeChunk(EXRImage *exr_image, const EXRHeader *exr_header, return TINYEXR_ERROR_INVALID_DATA; } - int data_width = - exr_header->data_window.max_x - exr_header->data_window.min_x + 1; - int data_height = - exr_header->data_window.max_y - exr_header->data_window.min_y + 1; + tinyexr_int64 data_width = + static_cast(exr_header->data_window.max_x) - static_cast(exr_header->data_window.min_x) + static_cast(1); + tinyexr_int64 data_height = + static_cast(exr_header->data_window.max_y) - static_cast(exr_header->data_window.min_y) + static_cast(1); + + if (data_width <= 0) { + if (err) { + (*err) += "Invalid data window width.\n"; + } + return TINYEXR_ERROR_INVALID_DATA; + } + + if (data_height <= 0) { + if (err) { + (*err) += "Invalid data window height.\n"; + } + return TINYEXR_ERROR_INVALID_DATA; + } // Do not allow too large data_width and data_height. header invalid? { @@ -5042,8 +5265,17 @@ static int DecodeChunk(EXRImage *exr_image, const EXRHeader *exr_header, } level_image->width = LevelSize(exr_header->data_window.max_x - exr_header->data_window.min_x + 1, level, exr_header->tile_rounding_mode); + if (level_image->width < 1) { + return TINYEXR_ERROR_INVALID_DATA; + } + level_image->height = LevelSize(exr_header->data_window.max_y - exr_header->data_window.min_y + 1, level, exr_header->tile_rounding_mode); + + if (level_image->height < 1) { + return TINYEXR_ERROR_INVALID_DATA; + } + level_image->level_x = level; level_image->level_y = level; @@ -5069,8 +5301,16 @@ static int DecodeChunk(EXRImage *exr_image, const EXRHeader *exr_header, level_image->width = LevelSize(exr_header->data_window.max_x - exr_header->data_window.min_x + 1, level_x, exr_header->tile_rounding_mode); + if (level_image->width < 1) { + return TINYEXR_ERROR_INVALID_DATA; + } + level_image->height = LevelSize(exr_header->data_window.max_y - exr_header->data_window.min_y + 1, level_y, exr_header->tile_rounding_mode); + if (level_image->height < 1) { + return TINYEXR_ERROR_INVALID_DATA; + } + level_image->level_x = level_x; level_image->level_y = level_y; @@ -5101,19 +5341,33 @@ static int DecodeChunk(EXRImage *exr_image, const EXRHeader *exr_header, return TINYEXR_ERROR_INVALID_DATA; } + bool alloc_success = false; exr_image->images = tinyexr::AllocateImage( num_channels, exr_header->channels, exr_header->requested_pixel_types, - data_width, data_height); + int(data_width), int(data_height), &alloc_success); + + if (!alloc_success) { + if (err) { + std::stringstream ss; + ss << "Failed to allocate memory for Images. Maybe EXR header is corrupted or Image data size is too large: width = " << data_width + << ", height = " << data_height << ", channels = " << num_channels + << std::endl; + (*err) += ss.str(); + } + return TINYEXR_ERROR_INVALID_DATA; + } #if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0) std::vector workers; std::atomic y_count(0); int num_threads = std::max(1, int(std::thread::hardware_concurrency())); +#if (TINYEXR_MAX_THREADS > 0) + num_threads = std::min(num_threads,TINYEXR_MAX_THREADS); +#endif if (num_threads > int(num_blocks)) { num_threads = int(num_blocks); } - for (int t = 0; t < num_threads; t++) { workers.emplace_back(std::thread([&]() { int y = 0; @@ -5188,11 +5442,12 @@ static int DecodeChunk(EXRImage *exr_image, const EXRHeader *exr_header, if (line_no < 0) { invalid_data = true; } else { + // Line order is increasing because we read in line offset table order. if (!tinyexr::DecodePixelData( exr_image->images, exr_header->requested_pixel_types, data_ptr, static_cast(data_len), - exr_header->compression_type, exr_header->line_order, - data_width, data_height, data_width, y, line_no, + exr_header->compression_type, /* line_order*/ 0, + int(data_width), int(data_height), int(data_width), y, line_no, num_lines, static_cast(pixel_data_size), static_cast( exr_header->num_custom_attributes), @@ -5221,7 +5476,15 @@ static int DecodeChunk(EXRImage *exr_image, const EXRHeader *exr_header, if (invalid_data) { if (err) { - (*err) += "Invalid data found when decoding pixels.\n"; + (*err) += "Invalid/Corrupted data found when decoding pixels.\n"; + } + + // free alloced image. + for (size_t c = 0; c < static_cast(num_channels); c++) { + if (exr_image->images[c]) { + free(exr_image->images[c]); + exr_image->images[c] = NULL; + } } return TINYEXR_ERROR_INVALID_DATA; } @@ -5236,8 +5499,8 @@ static int DecodeChunk(EXRImage *exr_image, const EXRHeader *exr_header, { exr_image->num_channels = num_channels; - exr_image->width = data_width; - exr_image->height = data_height; + exr_image->width = int(data_width); + exr_image->height = int(data_height); } return TINYEXR_SUCCESS; @@ -5246,8 +5509,12 @@ static int DecodeChunk(EXRImage *exr_image, const EXRHeader *exr_header, static bool ReconstructLineOffsets( std::vector *offsets, size_t n, const unsigned char *head, const unsigned char *marker, const size_t size) { - assert(head < marker); - assert(offsets->size() == n); + if (head >= marker) { + return false; + } + if (offsets->size() != n) { + return false; + } for (size_t i = 0; i < n; i++) { size_t offset = static_cast(marker - head); @@ -5343,7 +5610,7 @@ static int CalculateNumXLevels(const EXRHeader* exr_header) { default: - assert(false); + return -1; } return num; @@ -5381,25 +5648,29 @@ static int CalculateNumYLevels(const EXRHeader* exr_header) { default: - assert(false); + return -1; } return num; } -static void CalculateNumTiles(std::vector& numTiles, +static bool CalculateNumTiles(std::vector& numTiles, int toplevel_size, int size, int tile_rounding_mode) { for (unsigned i = 0; i < numTiles.size(); i++) { int l = LevelSize(toplevel_size, int(i), tile_rounding_mode); - assert(l <= std::numeric_limits::max() - size + 1); + if (l < 0) { + return false; + } + TINYEXR_CHECK_AND_RETURN_C(l <= std::numeric_limits::max() - size + 1, false); numTiles[i] = (l + size - 1) / size; } + return true; } -static void PrecalculateTileInfo(std::vector& num_x_tiles, +static bool PrecalculateTileInfo(std::vector& num_x_tiles, std::vector& num_y_tiles, const EXRHeader* exr_header) { int min_x = exr_header->data_window.min_x; @@ -5408,20 +5679,35 @@ static void PrecalculateTileInfo(std::vector& num_x_tiles, int max_y = exr_header->data_window.max_y; int num_x_levels = CalculateNumXLevels(exr_header); + + if (num_x_levels < 0) { + return false; + } + int num_y_levels = CalculateNumYLevels(exr_header); + if (num_y_levels < 0) { + return false; + } + num_x_tiles.resize(size_t(num_x_levels)); num_y_tiles.resize(size_t(num_y_levels)); - CalculateNumTiles(num_x_tiles, + if (!CalculateNumTiles(num_x_tiles, max_x - min_x + 1, exr_header->tile_size_x, - exr_header->tile_rounding_mode); + exr_header->tile_rounding_mode)) { + return false; + } - CalculateNumTiles(num_y_tiles, + if (!CalculateNumTiles(num_y_tiles, max_y - min_y + 1, exr_header->tile_size_y, - exr_header->tile_rounding_mode); + exr_header->tile_rounding_mode)) { + return false; + } + + return true; } static void InitSingleResolutionOffsets(OffsetData& offset_data, size_t num_blocks) { @@ -5433,6 +5719,7 @@ static void InitSingleResolutionOffsets(OffsetData& offset_data, size_t num_bloc } // Return sum of tile blocks. +// 0 = error static int InitTileOffsets(OffsetData& offset_data, const EXRHeader* exr_header, const std::vector& num_x_tiles, @@ -5443,7 +5730,7 @@ static int InitTileOffsets(OffsetData& offset_data, switch (exr_header->tile_level_mode) { case TINYEXR_TILE_ONE_LEVEL: case TINYEXR_TILE_MIPMAP_LEVELS: - assert(offset_data.num_x_levels == offset_data.num_y_levels); + TINYEXR_CHECK_AND_RETURN_C(offset_data.num_x_levels == offset_data.num_y_levels, 0); offset_data.offsets.resize(size_t(offset_data.num_x_levels)); for (unsigned int l = 0; l < offset_data.offsets.size(); ++l) { @@ -5474,7 +5761,7 @@ static int InitTileOffsets(OffsetData& offset_data, break; default: - assert(false); + return 0; } return num_tile_blocks; } @@ -5542,9 +5829,9 @@ static bool isValidTile(const EXRHeader* exr_header, return false; } -static void ReconstructTileOffsets(OffsetData& offset_data, +static bool ReconstructTileOffsets(OffsetData& offset_data, const EXRHeader* exr_header, - const unsigned char* head, const unsigned char* marker, const size_t /*size*/, + const unsigned char* head, const unsigned char* marker, const size_t size, bool isMultiPartFile, bool isDeep) { int numXLevels = offset_data.num_x_levels; @@ -5553,11 +5840,20 @@ static void ReconstructTileOffsets(OffsetData& offset_data, for (unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) { tinyexr::tinyexr_uint64 tileOffset = tinyexr::tinyexr_uint64(marker - head); + if (isMultiPartFile) { + if ((marker + sizeof(int)) >= (head + size)) { + return false; + } + //int partNumber; marker += sizeof(int); } + if ((marker + 4 * sizeof(int)) >= (head + size)) { + return false; + } + int tileX; memcpy(&tileX, marker, sizeof(int)); tinyexr::swap4(&tileX); @@ -5579,6 +5875,9 @@ static void ReconstructTileOffsets(OffsetData& offset_data, marker += sizeof(int); if (isDeep) { + if ((marker + 2 * sizeof(tinyexr::tinyexr_int64)) >= (head + size)) { + return false; + } tinyexr::tinyexr_int64 packed_offset_table_size; memcpy(&packed_offset_table_size, marker, sizeof(tinyexr::tinyexr_int64)); tinyexr::swap8(reinterpret_cast(&packed_offset_table_size)); @@ -5592,24 +5891,55 @@ static void ReconstructTileOffsets(OffsetData& offset_data, // next Int64 is unpacked sample size - skip that too marker += packed_offset_table_size + packed_sample_size + 8; + if (marker >= (head + size)) { + return false; + } + } else { - int dataSize; - memcpy(&dataSize, marker, sizeof(int)); + if ((marker + sizeof(uint32_t)) >= (head + size)) { + return false; + } + + uint32_t dataSize; + memcpy(&dataSize, marker, sizeof(uint32_t)); tinyexr::swap4(&dataSize); - marker += sizeof(int); + marker += sizeof(uint32_t); + marker += dataSize; + + if (marker >= (head + size)) { + return false; + } } if (!isValidTile(exr_header, offset_data, - tileX, tileY, levelX, levelY)) - return; + tileX, tileY, levelX, levelY)) { + return false; + } int level_idx = LevelIndex(levelX, levelY, exr_header->tile_level_mode, numXLevels); + if (level_idx < 0) { + return false; + } + + if (size_t(level_idx) >= offset_data.offsets.size()) { + return false; + } + + if (size_t(tileY) >= offset_data.offsets[size_t(level_idx)].size()) { + return false; + } + + if (size_t(tileX) >= offset_data.offsets[size_t(level_idx)][size_t(tileY)].size()) { + return false; + } + offset_data.offsets[size_t(level_idx)][size_t(tileY)][size_t(tileX)] = tileOffset; } } } + return true; } // marker output is also @@ -5667,8 +5997,12 @@ static int DecodeEXRImage(EXRImage *exr_image, const EXRHeader *exr_header, tinyexr::SetErrorMessage("Invalid data width value", err); return TINYEXR_ERROR_INVALID_DATA; } - int data_width = - exr_header->data_window.max_x - exr_header->data_window.min_x + 1; + tinyexr_int64 data_width = + static_cast(exr_header->data_window.max_x) - static_cast(exr_header->data_window.min_x) + static_cast(1); + if (data_width <= 0) { + tinyexr::SetErrorMessage("Invalid data window width value", err); + return TINYEXR_ERROR_INVALID_DATA; + } if (exr_header->data_window.max_y < exr_header->data_window.min_y || exr_header->data_window.max_y - exr_header->data_window.min_y == @@ -5676,8 +6010,13 @@ static int DecodeEXRImage(EXRImage *exr_image, const EXRHeader *exr_header, tinyexr::SetErrorMessage("Invalid data height value", err); return TINYEXR_ERROR_INVALID_DATA; } - int data_height = - exr_header->data_window.max_y - exr_header->data_window.min_y + 1; + tinyexr_int64 data_height = + static_cast(exr_header->data_window.max_y) - static_cast(exr_header->data_window.min_y) + static_cast(1); + + if (data_height <= 0) { + tinyexr::SetErrorMessage("Invalid data window height value", err); + return TINYEXR_ERROR_INVALID_DATA; + } // Do not allow too large data_width and data_height. header invalid? { @@ -5710,7 +6049,10 @@ static int DecodeEXRImage(EXRImage *exr_image, const EXRHeader *exr_header, if (exr_header->tiled) { { std::vector num_x_tiles, num_y_tiles; - PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_header); + if (!PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_header)) { + tinyexr::SetErrorMessage("Failed to precalculate tile info.", err); + return TINYEXR_ERROR_INVALID_DATA; + } num_blocks = size_t(InitTileOffsets(offset_data, exr_header, num_x_tiles, num_y_tiles)); if (exr_header->chunk_count > 0) { if (exr_header->chunk_count != static_cast(num_blocks)) { @@ -5723,9 +6065,13 @@ static int DecodeEXRImage(EXRImage *exr_image, const EXRHeader *exr_header, int ret = ReadOffsets(offset_data, head, marker, size, err); if (ret != TINYEXR_SUCCESS) return ret; if (IsAnyOffsetsAreInvalid(offset_data)) { - ReconstructTileOffsets(offset_data, exr_header, + if (!ReconstructTileOffsets(offset_data, exr_header, head, marker, size, - exr_header->multipart, exr_header->non_image); + exr_header->multipart, exr_header->non_image)) { + + tinyexr::SetErrorMessage("Invalid Tile Offsets data.", err); + return TINYEXR_ERROR_INVALID_DATA; + } } } else if (exr_header->chunk_count > 0) { // Use `chunkCount` attribute. @@ -5845,11 +6191,14 @@ static void ChannelsInLayer(const EXRHeader &exr_header, const std::string &layer_name, std::vector &channels) { channels.clear(); + //std::cout << "layer_name = " << layer_name << "\n"; for (int c = 0; c < exr_header.num_channels; c++) { + //std::cout << "chan[" << c << "] = " << exr_header.channels[c].name << "\n"; std::string ch_name(exr_header.channels[c].name); if (layer_name.empty()) { const size_t pos = ch_name.find_last_of('.'); if (pos != std::string::npos && pos < ch_name.size()) { + if (pos != 0) continue; ch_name = ch_name.substr(pos + 1); } } else { @@ -5986,8 +6335,14 @@ int LoadEXRWithLayer(float **out_rgba, int *width, int *height, tinyexr::ChannelsInLayer( exr_header, layername == NULL ? "" : std::string(layername), channels); + if (channels.size() < 1) { - tinyexr::SetErrorMessage("Layer Not Found", err); + if (layername == NULL) { + tinyexr::SetErrorMessage("Layer Not Found. Seems EXR contains channels with layer(e.g. `diffuse.R`). if you are using LoadEXR(), please try LoadEXRWithLayer(). LoadEXR() cannot load EXR having channels with layer.", err); + + } else { + tinyexr::SetErrorMessage("Layer Not Found", err); + } FreeEXRHeader(&exr_header); FreeEXRImage(&exr_image); return TINYEXR_ERROR_LAYER_NOT_FOUND; @@ -6017,25 +6372,27 @@ int LoadEXRWithLayer(float **out_rgba, int *width, int *height, static_cast(exr_image.height))); if (exr_header.tiled) { + const size_t tile_size_x = static_cast(exr_header.tile_size_x); + const size_t tile_size_y = static_cast(exr_header.tile_size_y); for (int it = 0; it < exr_image.num_tiles; it++) { - for (int j = 0; j < exr_header.tile_size_y; j++) { - for (int i = 0; i < exr_header.tile_size_x; i++) { - const int ii = exr_image.tiles[it].offset_x * - static_cast(exr_header.tile_size_x) + - i; - const int jj = exr_image.tiles[it].offset_y * - static_cast(exr_header.tile_size_y) + - j; - const int idx = ii + jj * static_cast(exr_image.width); + for (size_t j = 0; j < tile_size_y; j++) { + for (size_t i = 0; i < tile_size_x; i++) { + const size_t ii = + static_cast(exr_image.tiles[it].offset_x) * tile_size_x + + i; + const size_t jj = + static_cast(exr_image.tiles[it].offset_y) * tile_size_y + + j; + const size_t idx = ii + jj * static_cast(exr_image.width); // out of region check. - if (ii >= exr_image.width) { + if (ii >= static_cast(exr_image.width)) { continue; } - if (jj >= exr_image.height) { + if (jj >= static_cast(exr_image.height)) { continue; } - const int srcIdx = i + j * exr_header.tile_size_x; + const size_t srcIdx = i + j * tile_size_x; unsigned char **src = exr_image.tiles[it].images; (*out_rgba)[4 * idx + 0] = reinterpret_cast(src)[chIdx][srcIdx]; @@ -6049,7 +6406,9 @@ int LoadEXRWithLayer(float **out_rgba, int *width, int *height, } } } else { - for (int i = 0; i < exr_image.width * exr_image.height; i++) { + const size_t pixel_size = static_cast(exr_image.width) * + static_cast(exr_image.height); + for (size_t i = 0; i < pixel_size; i++) { const float val = reinterpret_cast(exr_image.images)[chIdx][i]; (*out_rgba)[4 * i + 0] = val; @@ -6087,23 +6446,29 @@ int LoadEXRWithLayer(float **out_rgba, int *width, int *height, malloc(4 * sizeof(float) * static_cast(exr_image.width) * static_cast(exr_image.height))); if (exr_header.tiled) { + const size_t tile_size_x = static_cast(exr_header.tile_size_x); + const size_t tile_size_y = static_cast(exr_header.tile_size_y); for (int it = 0; it < exr_image.num_tiles; it++) { - for (int j = 0; j < exr_header.tile_size_y; j++) { - for (int i = 0; i < exr_header.tile_size_x; i++) { - const int ii = - exr_image.tiles[it].offset_x * exr_header.tile_size_x + i; - const int jj = - exr_image.tiles[it].offset_y * exr_header.tile_size_y + j; - const int idx = ii + jj * exr_image.width; + for (size_t j = 0; j < tile_size_y; j++) { + for (size_t i = 0; i < tile_size_x; i++) { + const size_t ii = + static_cast(exr_image.tiles[it].offset_x) * + tile_size_x + + i; + const size_t jj = + static_cast(exr_image.tiles[it].offset_y) * + tile_size_y + + j; + const size_t idx = ii + jj * static_cast(exr_image.width); // out of region check. - if (ii >= exr_image.width) { + if (ii >= static_cast(exr_image.width)) { continue; } - if (jj >= exr_image.height) { + if (jj >= static_cast(exr_image.height)) { continue; } - const int srcIdx = i + j * exr_header.tile_size_x; + const size_t srcIdx = i + j * tile_size_x; unsigned char **src = exr_image.tiles[it].images; (*out_rgba)[4 * idx + 0] = reinterpret_cast(src)[idxR][srcIdx]; @@ -6121,7 +6486,9 @@ int LoadEXRWithLayer(float **out_rgba, int *width, int *height, } } } else { - for (int i = 0; i < exr_image.width * exr_image.height; i++) { + const size_t pixel_size = static_cast(exr_image.width) * + static_cast(exr_image.height); + for (size_t i = 0; i < pixel_size; i++) { (*out_rgba)[4 * i + 0] = reinterpret_cast(exr_image.images)[idxR][i]; (*out_rgba)[4 * i + 1] = @@ -6253,6 +6620,7 @@ int LoadEXRFromMemory(float **out_rgba, int *width, int *height, ret = ParseEXRHeaderFromMemory(&exr_header, &exr_version, memory, size, err); if (ret != TINYEXR_SUCCESS) { + FreeEXRHeader(&exr_header); return ret; } @@ -6266,6 +6634,8 @@ int LoadEXRFromMemory(float **out_rgba, int *width, int *height, InitEXRImage(&exr_image); ret = LoadEXRImageFromMemory(&exr_image, &exr_header, memory, size, err); if (ret != TINYEXR_SUCCESS) { + FreeEXRHeader(&exr_header); + FreeEXRImage(&exr_image); return ret; } @@ -6295,23 +6665,29 @@ int LoadEXRFromMemory(float **out_rgba, int *width, int *height, static_cast(exr_image.height))); if (exr_header.tiled) { + const size_t tile_size_x = static_cast(exr_header.tile_size_x); + const size_t tile_size_y = static_cast(exr_header.tile_size_y); for (int it = 0; it < exr_image.num_tiles; it++) { - for (int j = 0; j < exr_header.tile_size_y; j++) { - for (int i = 0; i < exr_header.tile_size_x; i++) { - const int ii = - exr_image.tiles[it].offset_x * exr_header.tile_size_x + i; - const int jj = - exr_image.tiles[it].offset_y * exr_header.tile_size_y + j; - const int idx = ii + jj * exr_image.width; + for (size_t j = 0; j < tile_size_y; j++) { + for (size_t i = 0; i < tile_size_x; i++) { + const size_t ii = + static_cast(exr_image.tiles[it].offset_x) * + tile_size_x + + i; + const size_t jj = + static_cast(exr_image.tiles[it].offset_y) * + tile_size_y + + j; + const size_t idx = ii + jj * static_cast(exr_image.width); // out of region check. - if (ii >= exr_image.width) { + if (ii >= static_cast(exr_image.width)) { continue; } - if (jj >= exr_image.height) { + if (jj >= static_cast(exr_image.height)) { continue; } - const int srcIdx = i + j * exr_header.tile_size_x; + const size_t srcIdx = i + j * tile_size_x; unsigned char **src = exr_image.tiles[it].images; (*out_rgba)[4 * idx + 0] = reinterpret_cast(src)[0][srcIdx]; @@ -6325,7 +6701,9 @@ int LoadEXRFromMemory(float **out_rgba, int *width, int *height, } } } else { - for (int i = 0; i < exr_image.width * exr_image.height; i++) { + const size_t pixel_size = static_cast(exr_image.width) * + static_cast(exr_image.height); + for (size_t i = 0; i < pixel_size; i++) { const float val = reinterpret_cast(exr_image.images)[0][i]; (*out_rgba)[4 * i + 0] = val; (*out_rgba)[4 * i + 1] = val; @@ -6361,23 +6739,29 @@ int LoadEXRFromMemory(float **out_rgba, int *width, int *height, static_cast(exr_image.height))); if (exr_header.tiled) { + const size_t tile_size_x = static_cast(exr_header.tile_size_x); + const size_t tile_size_y = static_cast(exr_header.tile_size_y); for (int it = 0; it < exr_image.num_tiles; it++) { - for (int j = 0; j < exr_header.tile_size_y; j++) - for (int i = 0; i < exr_header.tile_size_x; i++) { - const int ii = - exr_image.tiles[it].offset_x * exr_header.tile_size_x + i; - const int jj = - exr_image.tiles[it].offset_y * exr_header.tile_size_y + j; - const int idx = ii + jj * exr_image.width; + for (size_t j = 0; j < tile_size_y; j++) + for (size_t i = 0; i < tile_size_x; i++) { + const size_t ii = + static_cast(exr_image.tiles[it].offset_x) * + tile_size_x + + i; + const size_t jj = + static_cast(exr_image.tiles[it].offset_y) * + tile_size_y + + j; + const size_t idx = ii + jj * static_cast(exr_image.width); // out of region check. - if (ii >= exr_image.width) { + if (ii >= static_cast(exr_image.width)) { continue; } - if (jj >= exr_image.height) { + if (jj >= static_cast(exr_image.height)) { continue; } - const int srcIdx = i + j * exr_header.tile_size_x; + const size_t srcIdx = i + j * tile_size_x; unsigned char **src = exr_image.tiles[it].images; (*out_rgba)[4 * idx + 0] = reinterpret_cast(src)[idxR][srcIdx]; @@ -6394,7 +6778,9 @@ int LoadEXRFromMemory(float **out_rgba, int *width, int *height, } } } else { - for (int i = 0; i < exr_image.width * exr_image.height; i++) { + const size_t pixel_size = static_cast(exr_image.width) * + static_cast(exr_image.height); + for (size_t i = 0; i < pixel_size; i++) { (*out_rgba)[4 * i + 0] = reinterpret_cast(exr_image.images)[idxR][i]; (*out_rgba)[4 * i + 1] = @@ -6420,6 +6806,200 @@ int LoadEXRFromMemory(float **out_rgba, int *width, int *height, return TINYEXR_SUCCESS; } +// Represents a read-only file mapped to an address space in memory. +// If no memory-mapping API is available, falls back to allocating a buffer +// with a copy of the file's data. +struct MemoryMappedFile { + unsigned char *data; // To the start of the file's data. + size_t size; // The size of the file in bytes. +#ifdef TINYEXR_USE_WIN32_MMAP + HANDLE windows_file; + HANDLE windows_file_mapping; +#elif defined(TINYEXR_USE_POSIX_MMAP) + int posix_descriptor; +#endif + + // MemoryMappedFile's constructor tries to map memory to a file. + // If this succeeds, valid() will return true and all fields + // are usable; otherwise, valid() will return false. + MemoryMappedFile(const char *filename) { + data = NULL; + size = 0; +#ifdef TINYEXR_USE_WIN32_MMAP + windows_file_mapping = NULL; + windows_file = + CreateFileW(tinyexr::UTF8ToWchar(filename).c_str(), // lpFileName + GENERIC_READ, // dwDesiredAccess + FILE_SHARE_READ, // dwShareMode + NULL, // lpSecurityAttributes + OPEN_EXISTING, // dwCreationDisposition + FILE_ATTRIBUTE_READONLY, // dwFlagsAndAttributes + NULL); // hTemplateFile + if (windows_file == INVALID_HANDLE_VALUE) { + return; + } + + windows_file_mapping = CreateFileMapping(windows_file, // hFile + NULL, // lpFileMappingAttributes + PAGE_READONLY, // flProtect + 0, // dwMaximumSizeHigh + 0, // dwMaximumSizeLow + NULL); // lpName + if (windows_file_mapping == NULL) { + return; + } + + data = reinterpret_cast( + MapViewOfFile(windows_file_mapping, // hFileMappingObject + FILE_MAP_READ, // dwDesiredAccess + 0, // dwFileOffsetHigh + 0, // dwFileOffsetLow + 0)); // dwNumberOfBytesToMap + if (!data) { + return; + } + + LARGE_INTEGER windows_file_size = {}; + if (!GetFileSizeEx(windows_file, &windows_file_size) || + static_cast(windows_file_size.QuadPart) > + std::numeric_limits::max()) { + UnmapViewOfFile(data); + data = NULL; + return; + } + size = static_cast(windows_file_size.QuadPart); +#elif defined(TINYEXR_USE_POSIX_MMAP) + posix_descriptor = open(filename, O_RDONLY); + if (posix_descriptor == -1) { + return; + } + + struct stat info; + if (fstat(posix_descriptor, &info) < 0) { + return; + } + // Make sure st_size is in the valid range for a size_t. The second case + // can only fail if a POSIX implementation defines off_t to be a larger + // type than size_t - for instance, compiling with _FILE_OFFSET_BITS=64 + // on a 32-bit system. On current 64-bit systems, this check can never + // fail, so we turn off clang's Wtautological-type-limit-compare warning + // around this code. +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wtautological-type-limit-compare" +#endif + if (info.st_size < 0 || + info.st_size > std::numeric_limits::max()) { + return; + } +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + size = static_cast(info.st_size); + + data = reinterpret_cast( + mmap(0, size, PROT_READ, MAP_SHARED, posix_descriptor, 0)); + if (data == MAP_FAILED) { + data = nullptr; + return; + } +#else + FILE *fp = fopen(filename, "rb"); + if (!fp) { + return; + } + + // Calling fseek(fp, 0, SEEK_END) isn't strictly-conforming C code, but + // since neither the WIN32 nor POSIX APIs are available in this branch, this + // is a reasonable fallback option. + if (fseek(fp, 0, SEEK_END) != 0) { + fclose(fp); + return; + } + const long ftell_result = ftell(fp); + if (ftell_result < 0) { + // Error from ftell + fclose(fp); + return; + } + size = static_cast(ftell_result); + if (fseek(fp, 0, SEEK_SET) != 0) { + fclose(fp); + size = 0; + return; + } + + data = reinterpret_cast(malloc(size)); + if (!data) { + size = 0; + fclose(fp); + return; + } + size_t read_bytes = fread(data, 1, size, fp); + if (read_bytes != size) { + // TODO: Try to read data until reading `size` bytes. + fclose(fp); + size = 0; + data = nullptr; + return; + } + fclose(fp); +#endif + } + + // MemoryMappedFile's destructor closes all its handles. + ~MemoryMappedFile() { +#ifdef TINYEXR_USE_WIN32_MMAP + if (data) { + (void)UnmapViewOfFile(data); + data = NULL; + } + + if (windows_file_mapping != NULL) { + (void)CloseHandle(windows_file_mapping); + } + + if (windows_file != INVALID_HANDLE_VALUE) { + (void)CloseHandle(windows_file); + } +#elif defined(TINYEXR_USE_POSIX_MMAP) + if (data) { + (void)munmap(data, size); + data = NULL; + } + + if (posix_descriptor != -1) { + (void)close(posix_descriptor); + } +#else + if (data) { + (void)free(data); + } + data = NULL; +#endif + } + + // A MemoryMappedFile cannot be copied or moved. + // Only check for this when compiling with C++11 or higher, since deleted + // function definitions were added then. +#if TINYEXR_HAS_CXX11 +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++98-compat" +#endif + MemoryMappedFile(const MemoryMappedFile &) = delete; + MemoryMappedFile &operator=(const MemoryMappedFile &) = delete; + MemoryMappedFile(MemoryMappedFile &&other) noexcept = delete; + MemoryMappedFile &operator=(MemoryMappedFile &&other) noexcept = delete; +#ifdef __clang__ +#pragma clang diagnostic pop +#endif +#endif + + // Returns whether this was successfully opened. + bool valid() const { return data; } +}; + int LoadEXRImageFromFile(EXRImage *exr_image, const EXRHeader *exr_header, const char *filename, const char **err) { if (exr_image == NULL) { @@ -6427,50 +7007,19 @@ int LoadEXRImageFromFile(EXRImage *exr_image, const EXRHeader *exr_header, return TINYEXR_ERROR_INVALID_ARGUMENT; } - FILE *fp = NULL; -#ifdef _WIN32 -#if defined(_MSC_VER) || (defined(MINGW_HAS_SECURE_API) && MINGW_HAS_SECURE_API) // MSVC, MinGW GCC, or Clang. - errno_t errcode = - _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"rb"); - if (errcode != 0) { - tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); - // TODO(syoyo): return wfopen_s erro code - return TINYEXR_ERROR_CANT_OPEN_FILE; - } -#else - // Unknown compiler or MinGW without MINGW_HAS_SECURE_API. - fp = fopen(filename, "rb"); -#endif -#else - fp = fopen(filename, "rb"); -#endif - if (!fp) { + MemoryMappedFile file(filename); + if (!file.valid()) { tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); return TINYEXR_ERROR_CANT_OPEN_FILE; } - size_t filesize; - // Compute size - fseek(fp, 0, SEEK_END); - filesize = static_cast(ftell(fp)); - fseek(fp, 0, SEEK_SET); - - if (filesize < 16) { - tinyexr::SetErrorMessage("File size too short " + std::string(filename), + if (file.size < 16) { + tinyexr::SetErrorMessage("File size too short : " + std::string(filename), err); return TINYEXR_ERROR_INVALID_FILE; } - std::vector buf(filesize); // @todo { use mmap } - { - size_t ret; - ret = fread(&buf[0], 1, filesize, fp); - assert(ret == filesize); - fclose(fp); - (void)ret; - } - - return LoadEXRImageFromMemory(exr_image, exr_header, &buf.at(0), filesize, + return LoadEXRImageFromMemory(exr_image, exr_header, file.data, file.size, err); } @@ -6507,7 +7056,7 @@ namespace tinyexr // out_data must be allocated initially with the block-header size // of the current image(-part) type -static bool EncodePixelData(/* out */ std::vector& out_data, +static bool EncodePixelData(/* out */ std::vector& out_data, const unsigned char* const* images, int compression_type, int /*line_order*/, @@ -6651,13 +7200,16 @@ static bool EncodePixelData(/* out */ std::vector& out_data, } else if ((compression_type == TINYEXR_COMPRESSIONTYPE_ZIPS) || (compression_type == TINYEXR_COMPRESSIONTYPE_ZIP)) { -#if TINYEXR_USE_MINIZ +#if defined(TINYEXR_USE_MINIZ) && (TINYEXR_USE_MINIZ==1) std::vector block(mz_compressBound( static_cast(buf.size()))); #elif TINYEXR_USE_STB_ZLIB // there is no compressBound() function, so we use a value that // is grossly overestimated, but should always work std::vector block(256 + 2 * buf.size()); +#elif defined(TINYEXR_USE_NANOZLIB) && (TINYEXR_USE_NANOZLIB == 1) + std::vector block(nanoz_compressBound( + static_cast(buf.size()))); #else std::vector block( compressBound(static_cast(buf.size()))); @@ -6686,9 +7238,14 @@ static bool EncodePixelData(/* out */ std::vector& out_data, tinyexr::tinyexr_uint64 outSize = block.size(); - tinyexr::CompressRle(&block.at(0), outSize, + if (!tinyexr::CompressRle(&block.at(0), outSize, reinterpret_cast(&buf.at(0)), - static_cast(buf.size())); + static_cast(buf.size()))) { + if (err) { + (*err) += "RLE compresssion failed.\n"; + } + return false; + } // 4 byte: scan line // 4 byte: data size @@ -6705,9 +7262,14 @@ static bool EncodePixelData(/* out */ std::vector& out_data, std::vector block(bufLen); unsigned int outSize = static_cast(block.size()); - CompressPiz(&block.at(0), &outSize, + if (!CompressPiz(&block.at(0), &outSize, reinterpret_cast(&buf.at(0)), - buf.size(), channels, width, num_lines); + buf.size(), channels, width, num_lines)) { + if (err) { + (*err) += "PIZ compresssion failed.\n"; + } + return false; + } // 4 byte: scan line // 4 byte: data size @@ -6761,14 +7323,19 @@ static int EncodeTiledLevel(const EXRImage* level_image, const EXRHeader* exr_he const void* compression_param, // must be set if zfp compression is enabled std::string* err) { int num_tiles = num_x_tiles * num_y_tiles; - assert(num_tiles == level_image->num_tiles); + if (num_tiles != level_image->num_tiles) { + if (err) { + (*err) += "Invalid number of tiles in argument.\n"; + } + return TINYEXR_ERROR_INVALID_ARGUMENT; + } if ((exr_header->tile_size_x > level_image->width || exr_header->tile_size_y > level_image->height) && level_image->level_x == 0 && level_image->level_y == 0) { if (err) { (*err) += "Failed to encode tile data.\n"; - } - return TINYEXR_ERROR_INVALID_DATA; + } + return TINYEXR_ERROR_INVALID_DATA; } @@ -6783,6 +7350,9 @@ static int EncodeTiledLevel(const EXRImage* level_image, const EXRHeader* exr_he std::atomic tile_count(0); int num_threads = std::max(1, int(std::thread::hardware_concurrency())); +#if (TINYEXR_MAX_THREADS > 0) + num_threads = std::min(num_threads,TINYEXR_MAX_THREADS); +#endif if (num_threads > int(num_tiles)) { num_threads = int(num_tiles); } @@ -6808,13 +7378,13 @@ static int EncodeTiledLevel(const EXRImage* level_image, const EXRHeader* exr_he int y_tile = i / num_x_tiles; EXRTile& tile = level_image->tiles[tile_idx]; - + const unsigned char* const* images = static_cast(tile.images); data_list[data_idx].resize(5*sizeof(int)); size_t data_header_size = data_list[data_idx].size(); - bool ret = EncodePixelData(data_list[data_idx], + bool ret = EncodePixelData(data_list[data_idx], images, exr_header->compression_type, 0, // increasing y @@ -6831,7 +7401,11 @@ static int EncodeTiledLevel(const EXRImage* level_image, const EXRHeader* exr_he invalid_data = true; continue; } - assert(data_list[data_idx].size() > data_header_size); + if (data_list[data_idx].size() <= data_header_size) { + invalid_data = true; + continue; + } + int data_len = static_cast(data_list[data_idx].size() - data_header_size); //tileX, tileY, levelX, levelY // pixel_data_size(int) memcpy(&data_list[data_idx][0], &x_tile, sizeof(int)); @@ -6860,7 +7434,7 @@ static int EncodeTiledLevel(const EXRImage* level_image, const EXRHeader* exr_he if (invalid_data) { if (err) { - (*err) += "Failed to encode tile data.\n"; + (*err) += "Failed to encode tile data.\n"; } return TINYEXR_ERROR_INVALID_DATA; } @@ -6911,7 +7485,10 @@ static int EncodeChunk(const EXRImage* exr_image, const EXRHeader* exr_header, pixel_data_size += sizeof(unsigned int); channel_offset += sizeof(unsigned int); } else { - assert(0); + if (err) { + (*err) += "Invalid requested_pixel_type.\n"; + } + return TINYEXR_ERROR_INVALID_DATA; } } } @@ -6956,6 +7533,13 @@ static int EncodeChunk(const EXRImage* exr_image, const EXRHeader* exr_header, int level_index_from_image = LevelIndex(level_image->level_x, level_image->level_y, exr_header->tile_level_mode, offset_data.num_x_levels); + if (level_index_from_image < 0) { + if (err) { + (*err) += "Invalid tile level mode\n"; + } + return TINYEXR_ERROR_INVALID_DATA; + } + if (level_index_from_image != level_index) { if (err) { (*err) += "Incorrect level ordering in tiled image\n"; @@ -6963,9 +7547,20 @@ static int EncodeChunk(const EXRImage* exr_image, const EXRHeader* exr_header, return TINYEXR_ERROR_INVALID_DATA; } int num_y_tiles = int(offset_data.offsets[level_index].size()); - assert(num_y_tiles); + if (num_y_tiles <= 0) { + if (err) { + (*err) += "Invalid Y tile size\n"; + } + return TINYEXR_ERROR_INVALID_DATA; + } + int num_x_tiles = int(offset_data.offsets[level_index][0].size()); - assert(num_x_tiles); + if (num_x_tiles <= 0) { + if (err) { + (*err) += "Invalid X tile size\n"; + } + return TINYEXR_ERROR_INVALID_DATA; + } std::string e; int ret = EncodeTiledLevel(level_image, @@ -6996,7 +7591,7 @@ static int EncodeChunk(const EXRImage* exr_image, const EXRHeader* exr_header, } level_image = level_image->next_level; } - assert(static_cast(block_idx) == num_blocks); + TINYEXR_CHECK_AND_RETURN_C(static_cast(block_idx) == num_blocks, TINYEXR_ERROR_INVALID_DATA); total_size = offset; } else { // scanlines std::vector& offsets = offset_data.offsets[0][0]; @@ -7007,7 +7602,9 @@ static int EncodeChunk(const EXRImage* exr_image, const EXRHeader* exr_header, std::atomic block_count(0); int num_threads = std::min(std::max(1, int(std::thread::hardware_concurrency())), num_blocks); - +#if (TINYEXR_MAX_THREADS > 0) + num_threads = std::min(num_threads,TINYEXR_MAX_THREADS); +#endif for (int t = 0; t < num_threads; t++) { workers.emplace_back(std::thread([&]() { int i = 0; @@ -7031,7 +7628,7 @@ static int EncodeChunk(const EXRImage* exr_image, const EXRHeader* exr_header, data_list[i].resize(2*sizeof(int)); size_t data_header_size = data_list[i].size(); - bool ret = EncodePixelData(data_list[i], + bool ret = EncodePixelData(data_list[i], images, exr_header->compression_type, 0, // increasing y @@ -7047,9 +7644,12 @@ static int EncodeChunk(const EXRImage* exr_image, const EXRHeader* exr_header, compression_param); if (!ret) { invalid_data = true; - continue; // "break" cannot be used with OpenMP + continue; // "break" cannot be used with OpenMP + } + if (data_list[i].size() <= data_header_size) { + invalid_data = true; + continue; // "break" cannot be used with OpenMP } - assert(data_list[i].size() > data_header_size); int data_len = static_cast(data_list[i].size() - data_header_size); memcpy(&data_list[i][0], &start_y, sizeof(int)); memcpy(&data_list[i][4], &data_len, sizeof(int)); @@ -7111,21 +7711,24 @@ static size_t SaveEXRNPartImageToMemory(const EXRImage* exr_images, return 0; } #endif -#if !TINYEXR_USE_ZFP if (exr_headers[i]->compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) { +#if !TINYEXR_USE_ZFP SetErrorMessage("ZFP compression is not supported in this build", err); return 0; - } #else - for (int c = 0; c < exr_header->num_channels; ++c) { - if (exr_headers[i]->requested_pixel_types[c] != TINYEXR_PIXELTYPE_FLOAT) { - SetErrorMessage("Pixel type must be FLOAT for ZFP compression", - err); - return 0; + // All channels must be fp32. + // No fp16 support in ZFP atm(as of 2023 June) + // https://github.com/LLNL/fpzip/issues/2 + for (int c = 0; c < exr_headers[i]->num_channels; ++c) { + if (exr_headers[i]->requested_pixel_types[c] != TINYEXR_PIXELTYPE_FLOAT) { + SetErrorMessage("Pixel type must be FLOAT for ZFP compression", + err); + return 0; + } } - } #endif + } } } @@ -7175,9 +7778,20 @@ static size_t SaveEXRNPartImageToMemory(const EXRImage* exr_images, } else { { std::vector num_x_tiles, num_y_tiles; - PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_headers[i]); - chunk_count[i] = - InitTileOffsets(offset_data[i], exr_headers[i], num_x_tiles, num_y_tiles); + if (!PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_headers[i])) { + SetErrorMessage("Failed to precalculate Tile info", + err); + return TINYEXR_ERROR_INVALID_DATA; + } + int ntiles = InitTileOffsets(offset_data[i], exr_headers[i], num_x_tiles, num_y_tiles); + if (ntiles > 0) { + chunk_count[i] = ntiles; + } else { + SetErrorMessage("Failed to compute Tile offsets", + err); + return TINYEXR_ERROR_INVALID_DATA; + + } total_chunk_count += chunk_count[i]; } } @@ -7291,7 +7905,11 @@ static size_t SaveEXRNPartImageToMemory(const EXRImage* exr_images, { size_t len = 0; if ((len = strlen(exr_headers[i]->name)) > 0) { +#if TINYEXR_HAS_CXX11 partnames.emplace(exr_headers[i]->name); +#else + partnames.insert(std::string(exr_headers[i]->name)); +#endif if (partnames.size() != i + 1) { SetErrorMessage("'name' attributes must be unique for a multi-part file", err); return 0; @@ -7373,7 +7991,7 @@ static size_t SaveEXRNPartImageToMemory(const EXRImage* exr_images, // Allocating required memory if (total_size == 0) { // something went wrong tinyexr::SetErrorMessage("Output memory size is zero", err); - return 0; + return TINYEXR_ERROR_INVALID_DATA; } (*memory_out) = static_cast(malloc(size_t(total_size))); @@ -7381,7 +7999,6 @@ static size_t SaveEXRNPartImageToMemory(const EXRImage* exr_images, memcpy((*memory_out), &memory[0], memory.size()); unsigned char* memory_ptr = *memory_out + memory.size(); size_t sum = memory.size(); - ((void)(sum)); // Writing offset data for chunks for (unsigned int i = 0; i < num_parts; ++i) { @@ -7393,7 +8010,11 @@ static size_t SaveEXRNPartImageToMemory(const EXRImage* exr_images, for (size_t j = 0; j < offset_data[i].offsets[level_index].size(); ++j) { size_t num_bytes = sizeof(tinyexr_uint64) * offset_data[i].offsets[level_index][j].size(); sum += num_bytes; - assert(sum <= total_size); + if (sum > total_size) { + tinyexr::SetErrorMessage("Invalid offset bytes in Tiled Part image.", err); + return TINYEXR_ERROR_INVALID_DATA; + } + memcpy(memory_ptr, reinterpret_cast(&offset_data[i].offsets[level_index][j][0]), num_bytes); @@ -7404,7 +8025,10 @@ static size_t SaveEXRNPartImageToMemory(const EXRImage* exr_images, } else { size_t num_bytes = sizeof(tinyexr::tinyexr_uint64) * static_cast(chunk_count[i]); sum += num_bytes; - assert(sum <= total_size); + if (sum > total_size) { + tinyexr::SetErrorMessage("Invalid offset bytes in Part image.", err); + return TINYEXR_ERROR_INVALID_DATA; + } std::vector& offsets = offset_data[i].offsets[0][0]; memcpy(memory_ptr, reinterpret_cast(&offsets[0]), num_bytes); memory_ptr += num_bytes; @@ -7416,19 +8040,30 @@ static size_t SaveEXRNPartImageToMemory(const EXRImage* exr_images, for (size_t j = 0; j < static_cast(chunk_count[i]); ++j) { if (num_parts > 1) { sum += 4; - assert(sum <= total_size); + if (sum > total_size) { + tinyexr::SetErrorMessage("Buffer overrun in reading Part image chunk data.", err); + return TINYEXR_ERROR_INVALID_DATA; + } unsigned int part_number = i; swap4(&part_number); memcpy(memory_ptr, &part_number, 4); memory_ptr += 4; } sum += data_lists[i][j].size(); - assert(sum <= total_size); + if (sum > total_size) { + tinyexr::SetErrorMessage("Buffer overrun in reading Part image chunk data.", err); + return TINYEXR_ERROR_INVALID_DATA; + } memcpy(memory_ptr, &data_lists[i][j][0], data_lists[i][j].size()); memory_ptr += data_lists[i][j].size(); } } - assert(sum == total_size); + + if (sum != total_size) { + tinyexr::SetErrorMessage("Corrupted Part image chunk data.", err); + return TINYEXR_ERROR_INVALID_DATA; + } + return size_t(total_size); // OK } @@ -7494,6 +8129,7 @@ int SaveEXRImageToFile(const EXRImage *exr_image, const EXRHeader *exr_header, unsigned char *mem = NULL; size_t mem_size = SaveEXRImageToMemory(exr_image, exr_header, &mem, err); if (mem_size == 0) { + fclose(fp); return TINYEXR_ERROR_SERIALIZATION_FAILED; } @@ -7517,7 +8153,7 @@ size_t SaveEXRMultipartImageToMemory(const EXRImage* exr_images, const EXRHeader** exr_headers, unsigned int num_parts, unsigned char** memory_out, const char** err) { - if (exr_images == NULL || exr_headers == NULL || num_parts < 2 || + if (exr_images == NULL || exr_headers == NULL || num_parts == 0 || memory_out == NULL) { tinyexr::SetErrorMessage("Invalid argument for SaveEXRNPartImageToMemory", err); @@ -7531,7 +8167,7 @@ int SaveEXRMultipartImageToFile(const EXRImage* exr_images, unsigned int num_parts, const char* filename, const char** err) { - if (exr_images == NULL || exr_headers == NULL || num_parts < 2) { + if (exr_images == NULL || exr_headers == NULL || num_parts == 0) { tinyexr::SetErrorMessage("Invalid argument for SaveEXRMultipartImageToFile", err); return TINYEXR_ERROR_INVALID_ARGUMENT; @@ -7563,6 +8199,7 @@ int SaveEXRMultipartImageToFile(const EXRImage* exr_images, unsigned char *mem = NULL; size_t mem_size = SaveEXRMultipartImageToMemory(exr_images, exr_headers, num_parts, &mem, err); if (mem_size == 0) { + fclose(fp); return TINYEXR_ERROR_SERIALIZATION_FAILED; } @@ -7588,58 +8225,20 @@ int LoadDeepEXR(DeepImage *deep_image, const char *filename, const char **err) { return TINYEXR_ERROR_INVALID_ARGUMENT; } -#ifdef _WIN32 - FILE *fp = NULL; -#if defined(_MSC_VER) || (defined(MINGW_HAS_SECURE_API) && MINGW_HAS_SECURE_API) // MSVC, MinGW GCC, or Clang. - errno_t errcode = - _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"rb"); - if (errcode != 0) { - tinyexr::SetErrorMessage("Cannot read a file " + std::string(filename), - err); - return TINYEXR_ERROR_CANT_OPEN_FILE; - } -#else - // Unknown compiler or MinGW without MINGW_HAS_SECURE_API. - fp = fopen(filename, "rb"); -#endif - if (!fp) { - tinyexr::SetErrorMessage("Cannot read a file " + std::string(filename), - err); - return TINYEXR_ERROR_CANT_OPEN_FILE; - } -#else - FILE *fp = fopen(filename, "rb"); - if (!fp) { - tinyexr::SetErrorMessage("Cannot read a file " + std::string(filename), - err); + MemoryMappedFile file(filename); + if (!file.valid()) { + tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); return TINYEXR_ERROR_CANT_OPEN_FILE; } -#endif - size_t filesize; - // Compute size - fseek(fp, 0, SEEK_END); - filesize = static_cast(ftell(fp)); - fseek(fp, 0, SEEK_SET); - - if (filesize == 0) { - fclose(fp); + if (file.size == 0) { tinyexr::SetErrorMessage("File size is zero : " + std::string(filename), err); return TINYEXR_ERROR_INVALID_FILE; } - std::vector buf(filesize); // @todo { use mmap } - { - size_t ret; - ret = fread(&buf[0], 1, filesize, fp); - assert(ret == filesize); - (void)ret; - } - fclose(fp); - - const char *head = &buf[0]; - const char *marker = &buf[0]; + const char *head = reinterpret_cast(file.data); + const char *marker = reinterpret_cast(file.data); // Header check. { @@ -7674,7 +8273,7 @@ int LoadDeepEXR(DeepImage *deep_image, const char *filename, const char **err) { std::vector channels; // Read attributes - size_t size = filesize - tinyexr::kEXRVersionSize; + size_t size = file.size - tinyexr::kEXRVersionSize; for (;;) { if (0 == size) { return TINYEXR_ERROR_INVALID_DATA; @@ -7757,11 +8356,11 @@ int LoadDeepEXR(DeepImage *deep_image, const char *filename, const char **err) { } } - assert(dx >= 0); - assert(dy >= 0); - assert(dw >= 0); - assert(dh >= 0); - assert(num_channels >= 1); + TINYEXR_CHECK_AND_RETURN_C(dx >= 0, TINYEXR_ERROR_INVALID_DATA); + TINYEXR_CHECK_AND_RETURN_C(dy >= 0, TINYEXR_ERROR_INVALID_DATA); + TINYEXR_CHECK_AND_RETURN_C(dw >= 0, TINYEXR_ERROR_INVALID_DATA); + TINYEXR_CHECK_AND_RETURN_C(dh >= 0, TINYEXR_ERROR_INVALID_DATA); + TINYEXR_CHECK_AND_RETURN_C(num_channels >= 1, TINYEXR_ERROR_INVALID_DATA); int data_width = dw - dx + 1; int data_height = dh - dy + 1; @@ -7859,7 +8458,7 @@ int LoadDeepEXR(DeepImage *deep_image, const char *filename, const char **err) { return false; } - assert(dstLen == pixelOffsetTable.size() * sizeof(int)); + TINYEXR_CHECK_AND_RETURN_C(dstLen == pixelOffsetTable.size() * sizeof(int), TINYEXR_ERROR_INVALID_DATA); for (size_t i = 0; i < static_cast(data_width); i++) { deep_image->offset_table[y][i] = pixelOffsetTable[i]; } @@ -7878,7 +8477,7 @@ int LoadDeepEXR(DeepImage *deep_image, const char *filename, const char **err) { static_cast(packedSampleDataSize))) { return false; } - assert(dstLen == static_cast(unpackedSampleDataSize)); + TINYEXR_CHECK_AND_RETURN_C(dstLen == static_cast(unpackedSampleDataSize), TINYEXR_ERROR_INVALID_DATA); } } @@ -7897,16 +8496,17 @@ int LoadDeepEXR(DeepImage *deep_image, const char *filename, const char **err) { TINYEXR_PIXELTYPE_FLOAT) { // float channel_offset += 4; } else { - assert(0); + tinyexr::SetErrorMessage("Invalid pixel_type in chnnels.", err); + return TINYEXR_ERROR_INVALID_DATA; } } sampleSize = channel_offset; } - assert(sampleSize >= 2); + TINYEXR_CHECK_AND_RETURN_C(sampleSize >= 2, TINYEXR_ERROR_INVALID_DATA); - assert(static_cast( + TINYEXR_CHECK_AND_RETURN_C(static_cast( pixelOffsetTable[static_cast(data_width - 1)] * - sampleSize) == sample_data.size()); + sampleSize) == sample_data.size(), TINYEXR_ERROR_INVALID_DATA); int samples_per_line = static_cast(sample_data.size()) / sampleSize; // @@ -8105,48 +8705,13 @@ int ParseEXRHeaderFromFile(EXRHeader *exr_header, const EXRVersion *exr_version, return TINYEXR_ERROR_INVALID_ARGUMENT; } - FILE *fp = NULL; -#ifdef _WIN32 -#if defined(_MSC_VER) || (defined(MINGW_HAS_SECURE_API) && MINGW_HAS_SECURE_API) // MSVC, MinGW GCC, or Clang. - errno_t errcode = - _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"rb"); - if (errcode != 0) { - tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); - return TINYEXR_ERROR_INVALID_FILE; - } -#else - // Unknown compiler or MinGW without MINGW_HAS_SECURE_API. - fp = fopen(filename, "rb"); -#endif -#else - fp = fopen(filename, "rb"); -#endif - if (!fp) { + MemoryMappedFile file(filename); + if (!file.valid()) { tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); return TINYEXR_ERROR_CANT_OPEN_FILE; } - size_t filesize; - // Compute size - fseek(fp, 0, SEEK_END); - filesize = static_cast(ftell(fp)); - fseek(fp, 0, SEEK_SET); - - std::vector buf(filesize); // @todo { use mmap } - { - size_t ret; - ret = fread(&buf[0], 1, filesize, fp); - assert(ret == filesize); - fclose(fp); - - if (ret != filesize) { - tinyexr::SetErrorMessage("fread() error on " + std::string(filename), - err); - return TINYEXR_ERROR_INVALID_FILE; - } - } - - return ParseEXRHeaderFromMemory(exr_header, exr_version, &buf.at(0), filesize, + return ParseEXRHeaderFromMemory(exr_header, exr_version, file.data, file.size, err); } @@ -8226,7 +8791,7 @@ int ParseEXRMultipartHeaderFromMemory(EXRHeader ***exr_headers, (*exr_headers) = static_cast(malloc(sizeof(EXRHeader *) * infos.size())); - + int retcode = TINYEXR_SUCCESS; for (size_t i = 0; i < infos.size(); i++) { @@ -8238,7 +8803,7 @@ int ParseEXRMultipartHeaderFromMemory(EXRHeader ***exr_headers, if (!ConvertHeader(exr_header, infos[i], &warn, &_err)) { // Free malloc-allocated memory here. - for (size_t k = 0; k < infos[i].attributes.size(); i++) { + for (size_t k = 0; k < infos[i].attributes.size(); k++) { if (infos[i].attributes[k].value) { free(infos[i].attributes[k].value); } @@ -8272,48 +8837,14 @@ int ParseEXRMultipartHeaderFromFile(EXRHeader ***exr_headers, int *num_headers, return TINYEXR_ERROR_INVALID_ARGUMENT; } - FILE *fp = NULL; -#ifdef _WIN32 -#if defined(_MSC_VER) || (defined(MINGW_HAS_SECURE_API) && MINGW_HAS_SECURE_API) // MSVC, MinGW GCC, or Clang. - errno_t errcode = - _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"rb"); - if (errcode != 0) { - tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); - return TINYEXR_ERROR_INVALID_FILE; - } -#else - // Unknown compiler or MinGW without MINGW_HAS_SECURE_API. - fp = fopen(filename, "rb"); -#endif -#else - fp = fopen(filename, "rb"); -#endif - if (!fp) { + MemoryMappedFile file(filename); + if (!file.valid()) { tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); return TINYEXR_ERROR_CANT_OPEN_FILE; } - size_t filesize; - // Compute size - fseek(fp, 0, SEEK_END); - filesize = static_cast(ftell(fp)); - fseek(fp, 0, SEEK_SET); - - std::vector buf(filesize); // @todo { use mmap } - { - size_t ret; - ret = fread(&buf[0], 1, filesize, fp); - assert(ret == filesize); - fclose(fp); - - if (ret != filesize) { - tinyexr::SetErrorMessage("`fread' error. file may be corrupted.", err); - return TINYEXR_ERROR_INVALID_FILE; - } - } - return ParseEXRMultipartHeaderFromMemory( - exr_headers, num_headers, exr_version, &buf.at(0), filesize, err); + exr_headers, num_headers, exr_version, file.data, file.size, err); } int ParseEXRVersionFromMemory(EXRVersion *version, const unsigned char *memory, @@ -8397,16 +8928,10 @@ int ParseEXRVersionFromFile(EXRVersion *version, const char *filename) { return TINYEXR_ERROR_CANT_OPEN_FILE; } - size_t file_size; - // Compute size - fseek(fp, 0, SEEK_END); - file_size = static_cast(ftell(fp)); - fseek(fp, 0, SEEK_SET); - - if (file_size < tinyexr::kEXRVersionSize) { - return TINYEXR_ERROR_INVALID_FILE; - } - + // Try to read kEXRVersionSize bytes; if the file is shorter than + // kEXRVersionSize, this will produce an error. This avoids a call to + // fseek(fp, 0, SEEK_END), which is not required to be supported by C + // implementations. unsigned char buf[tinyexr::kEXRVersionSize]; size_t ret = fread(&buf[0], 1, tinyexr::kEXRVersionSize, fp); fclose(fp); @@ -8484,7 +9009,10 @@ int LoadEXRMultipartImageFromMemory(EXRImage *exr_images, } else { { std::vector num_x_tiles, num_y_tiles; - tinyexr::PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_headers[i]); + if (!tinyexr::PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_headers[i])) { + tinyexr::SetErrorMessage("Invalid tile info.", err); + return TINYEXR_ERROR_INVALID_DATA; + } int num_blocks = InitTileOffsets(offset_data, exr_headers[i], num_x_tiles, num_y_tiles); if (num_blocks != exr_headers[i]->chunk_count) { tinyexr::SetErrorMessage("Invalid offset table size.", err); @@ -8503,7 +9031,7 @@ int LoadEXRMultipartImageFromMemory(EXRImage *exr_images, return TINYEXR_ERROR_INVALID_DATA; } offset_data.offsets[l][dy][dx] = offset + 4; // +4 to skip 'part number' - marker += sizeof(tinyexr::tinyexr_uint64); // = 8 + marker += sizeof(tinyexr::tinyexr_uint64); // = 8 } } } @@ -8514,7 +9042,7 @@ int LoadEXRMultipartImageFromMemory(EXRImage *exr_images, for (size_t i = 0; i < static_cast(num_parts); i++) { tinyexr::OffsetData &offset_data = chunk_offset_table_list[i]; - // First check 'part number' is identitical to 'i' + // First check 'part number' is identical to 'i' for (unsigned int l = 0; l < offset_data.offsets.size(); ++l) for (unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) for (unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) { @@ -8556,48 +9084,18 @@ int LoadEXRMultipartImageFromFile(EXRImage *exr_images, return TINYEXR_ERROR_INVALID_ARGUMENT; } - FILE *fp = NULL; -#ifdef _WIN32 -#if defined(_MSC_VER) || (defined(MINGW_HAS_SECURE_API) && MINGW_HAS_SECURE_API) // MSVC, MinGW GCC, or Clang. - errno_t errcode = - _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"rb"); - if (errcode != 0) { + MemoryMappedFile file(filename); + if (!file.valid()) { tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); return TINYEXR_ERROR_CANT_OPEN_FILE; } -#else - // Unknown compiler or MinGW without MINGW_HAS_SECURE_API. - fp = fopen(filename, "rb"); -#endif -#else - fp = fopen(filename, "rb"); -#endif - if (!fp) { - tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err); - return TINYEXR_ERROR_CANT_OPEN_FILE; - } - - size_t filesize; - // Compute size - fseek(fp, 0, SEEK_END); - filesize = static_cast(ftell(fp)); - fseek(fp, 0, SEEK_SET); - - std::vector buf(filesize); // @todo { use mmap } - { - size_t ret; - ret = fread(&buf[0], 1, filesize, fp); - assert(ret == filesize); - fclose(fp); - (void)ret; - } return LoadEXRMultipartImageFromMemory(exr_images, exr_headers, num_parts, - &buf.at(0), filesize, err); + file.data, file.size, err); } int SaveEXRToMemory(const float *data, int width, int height, int components, - const int save_as_fp16, const unsigned char **outbuf, const char **err) { + const int save_as_fp16, unsigned char **outbuf, const char **err) { if ((components == 1) || components == 3 || components == 4) { // OK @@ -8636,13 +9134,19 @@ int SaveEXRToMemory(const float *data, int width, int height, int components, images[3].resize(static_cast(width * height)); // Split RGB(A)RGB(A)RGB(A)... into R, G and B(and A) layers - for (size_t i = 0; i < static_cast(width * height); i++) { - images[0][i] = data[static_cast(components) * i + 0]; - images[1][i] = data[static_cast(components) * i + 1]; - images[2][i] = data[static_cast(components) * i + 2]; - if (components == 4) { - images[3][i] = data[static_cast(components) * i + 3]; - } + if (components == 4) { + for (size_t i = 0; i < static_cast(width * height); i++) { + images[0][i] = data[static_cast(components) * i + 0]; + images[1][i] = data[static_cast(components) * i + 1]; + images[2][i] = data[static_cast(components) * i + 2]; + images[3][i] = data[static_cast(components) * i + 3]; + } + } else { + for (size_t i = 0; i < static_cast(width * height); i++) { + images[0][i] = data[static_cast(components) * i + 0]; + images[1][i] = data[static_cast(components) * i + 1]; + images[2][i] = data[static_cast(components) * i + 2]; + } } } @@ -8774,24 +9278,32 @@ int SaveEXR(const float *data, int width, int height, int components, image.num_channels = components; std::vector images[4]; + const size_t pixel_count = + static_cast(width) * static_cast(height); if (components == 1) { - images[0].resize(static_cast(width * height)); - memcpy(images[0].data(), data, sizeof(float) * size_t(width * height)); + images[0].resize(pixel_count); + memcpy(images[0].data(), data, sizeof(float) * pixel_count); } else { - images[0].resize(static_cast(width * height)); - images[1].resize(static_cast(width * height)); - images[2].resize(static_cast(width * height)); - images[3].resize(static_cast(width * height)); + images[0].resize(pixel_count); + images[1].resize(pixel_count); + images[2].resize(pixel_count); + images[3].resize(pixel_count); // Split RGB(A)RGB(A)RGB(A)... into R, G and B(and A) layers - for (size_t i = 0; i < static_cast(width * height); i++) { - images[0][i] = data[static_cast(components) * i + 0]; - images[1][i] = data[static_cast(components) * i + 1]; - images[2][i] = data[static_cast(components) * i + 2]; - if (components == 4) { - images[3][i] = data[static_cast(components) * i + 3]; - } + if (components == 4) { + for (size_t i = 0; i < pixel_count; i++) { + images[0][i] = data[static_cast(components) * i + 0]; + images[1][i] = data[static_cast(components) * i + 1]; + images[2][i] = data[static_cast(components) * i + 2]; + images[3][i] = data[static_cast(components) * i + 3]; + } + } else { + for (size_t i = 0; i < pixel_count; i++) { + images[0][i] = data[static_cast(components) * i + 0]; + images[1][i] = data[static_cast(components) * i + 1]; + images[2][i] = data[static_cast(components) * i + 2]; + } } } @@ -8874,9 +9386,6 @@ int SaveEXR(const float *data, int width, int height, int components, } int ret = SaveEXRImageToFile(&image, &header, outfilename, err); - if (ret != TINYEXR_SUCCESS) { - return ret; - } free(header.channels); free(header.pixel_types); @@ -8886,7 +9395,7 @@ int SaveEXR(const float *data, int width, int height, int components, } #ifdef __clang__ -// zero-as-null-ppinter-constant +// zero-as-null-pointer-constant #pragma clang diagnostic pop #endif