From 3bc6822caa7ca9a2b3a26eb450c35271353a5188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Mon, 22 May 2023 11:20:48 +0200 Subject: [PATCH] tinyexr: Sync with upstream 1.0.2 Fixes a bunch of bugs, notably addressing fuzzer reports (crashes). https://github.com/syoyo/tinyexr/releases/tag/v1.0.2 (cherry picked from commit c5834eb9720b73a9f83752e7dafbee1c19c059b6) --- thirdparty/README.md | 2 +- thirdparty/tinyexr/tinyexr.h | 1234 ++++++++++++++++++++++++---------- 2 files changed, 872 insertions(+), 364 deletions(-) diff --git a/thirdparty/README.md b/thirdparty/README.md index a861865fc9b..73759d15fd4 100644 --- a/thirdparty/README.md +++ b/thirdparty/README.md @@ -536,7 +536,7 @@ comments and a patch is provided in the squish/ folder. ## tinyexr - Upstream: https://github.com/syoyo/tinyexr -- Version: 1.0.1 (67010eae802211202d0797f4df2b809f4ba7442c, 2021) +- Version: 1.0.2 (02310c77e5156c36fedf6cf810c4071e3f83906f, 2023) - License: BSD-3-Clause Files extracted from upstream source: diff --git a/thirdparty/tinyexr/tinyexr.h b/thirdparty/tinyexr/tinyexr.h index 969f07ad79f..7482853bcb6 100644 --- a/thirdparty/tinyexr/tinyexr.h +++ b/thirdparty/tinyexr/tinyexr.h @@ -104,12 +104,17 @@ extern "C" { #endif // Use miniz or not to decode ZIP format pixel. Linking with zlib -// required if this flas 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 -// Disable PIZ comporession when applying cpplint. +// Use the ZIP implementation of stb_image.h and stb_image_write.h. +#ifndef TINYEXR_USE_STB_ZLIB +#define TINYEXR_USE_STB_ZLIB (0) +#endif + +// Disable PIZ compression when applying cpplint. #ifndef TINYEXR_USE_PIZ #define TINYEXR_USE_PIZ (1) #endif @@ -144,8 +149,9 @@ extern "C" { #define TINYEXR_ERROR_INVALID_HEADER (-9) #define TINYEXR_ERROR_UNSUPPORTED_FEATURE (-10) #define TINYEXR_ERROR_CANT_WRITE_FILE (-11) -#define TINYEXR_ERROR_SERIALZATION_FAILED (-12) +#define TINYEXR_ERROR_SERIALIZATION_FAILED (-12) #define TINYEXR_ERROR_LAYER_NOT_FOUND (-13) +#define TINYEXR_ERROR_DATA_TOO_LARGE (-14) // @note { OpenEXR file format: http://www.openexr.com/openexrfilelayout.pdf } @@ -175,19 +181,19 @@ extern "C" { #define TINYEXR_TILE_ROUND_DOWN (0) #define TINYEXR_TILE_ROUND_UP (1) -typedef struct _EXRVersion { +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; -typedef struct _EXRAttribute { +typedef struct TEXRAttribute { char name[256]; // name and type are up to 255 chars long. char type[256]; unsigned char *value; // uint8_t* @@ -195,7 +201,7 @@ typedef struct _EXRAttribute { int pad0; } EXRAttribute; -typedef struct _EXRChannelInfo { +typedef struct TEXRChannelInfo { char name[256]; // less than 255 bytes long int pixel_type; int x_sampling; @@ -204,7 +210,7 @@ typedef struct _EXRChannelInfo { unsigned char pad[3]; } EXRChannelInfo; -typedef struct _EXRTile { +typedef struct TEXRTile { int offset_x; int offset_y; int level_x; @@ -216,14 +222,14 @@ typedef struct _EXRTile { unsigned char **images; // image[channels][pixels] } EXRTile; -typedef struct _EXRBox2i { +typedef struct TEXRBox2i { int min_x; int min_y; int max_x; int max_y; } EXRBox2i; -typedef struct _EXRHeader { +typedef struct TEXRHeader { float pixel_aspect_ratio; int line_order; EXRBox2i data_window; @@ -272,16 +278,16 @@ typedef struct _EXRHeader { char name[256]; } EXRHeader; -typedef struct _EXRMultiPartHeader { +typedef struct TEXRMultiPartHeader { int num_headers; EXRHeader *headers; } EXRMultiPartHeader; -typedef struct _EXRImage { +typedef struct TEXRImage { EXRTile *tiles; // Tiled pixel data. The application must reconstruct image // from tiles manually. NULL if scanline format. - struct _EXRImage* next_level; // NULL if scanline format or image is the last level. + struct TEXRImage* next_level; // NULL if scanline format or image is the last level. int level_x; // x level index int level_y; // y level index @@ -296,13 +302,13 @@ typedef struct _EXRImage { } EXRImage; -typedef struct _EXRMultiPartImage { +typedef struct TEXRMultiPartImage { int num_images; EXRImage *images; } EXRMultiPartImage; -typedef struct _DeepImage { +typedef struct TDeepImage { const char **channel_names; float ***image; // image[channels][scanlines][samples] int **offset_table; // offset_table[scanline][offsets] @@ -346,15 +352,21 @@ extern int LoadEXRWithLayer(float **out_rgba, int *width, int *height, extern int EXRLayers(const char *filename, const char **layer_names[], int *num_layers, const char **err); -// @deprecated { to be removed. } +// @deprecated // Simple wrapper API for ParseEXRHeaderFromFile. // checking given file is a EXR file(by just look up header) // @return TINYEXR_SUCCEES for EXR image, TINYEXR_ERROR_INVALID_HEADER for // others extern int IsEXR(const char *filename); -// @deprecated { to be removed. } -// Saves single-frame OpenEXR image. Assume EXR image contains RGB(A) channels. +// Simple wrapper API for ParseEXRHeaderFromMemory. +// Check if given data is a EXR image(by just looking up a header section) +// @return TINYEXR_SUCCEES for EXR image, TINYEXR_ERROR_INVALID_HEADER for +// others +extern int IsEXRFromMemory(const unsigned char *memory, size_t size); + +// @deprecated +// Saves single-frame OpenEXR image to a buffer. Assume EXR image contains RGB(A) channels. // components must be 1(Grayscale), 3(RGB) or 4(RGBA). // Input image format is: `float x width x height`, or `float x RGB(A) x width x // hight` @@ -362,6 +374,25 @@ extern int IsEXR(const char *filename); // value. // Save image as fp32(FLOAT) format when `save_as_fp16` is 0. // Use ZIP compression by default. +// `buffer` is the pointer to write EXR data. +// Memory for `buffer` is allocated internally in SaveEXRToMemory. +// Returns the data size of EXR file when the value is positive(up to 2GB EXR data). +// Returns negative value and may set error string in `err` when there's an +// 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); + +// @deprecated { Not recommended, but handy to use. } +// Saves single-frame OpenEXR image to a buffer. Assume EXR image contains RGB(A) channels. +// components must be 1(Grayscale), 3(RGB) or 4(RGBA). +// Input image format is: `float x width x height`, or `float x RGB(A) x width x +// hight` +// Save image as fp16(HALF) format when `save_as_fp16` is positive non-zero +// value. +// Save image as fp32(FLOAT) format when `save_as_fp16` is 0. +// Use ZIP compression by default. +// Returns TINYEXR_SUCCEES(0) when success. // Returns negative value and may set error string in `err` when there's an // error extern int SaveEXR(const float *data, const int width, const int height, @@ -576,8 +607,15 @@ 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 +#define TINYEXR_USE_WIN32_MMAP (1) +#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 @@ -587,7 +625,7 @@ extern int LoadEXRFromMemory(float **out_rgba, int *width, int *height, #include #include -// #include // debug +//#include // debug #include #include @@ -605,6 +643,8 @@ 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 @@ -619,6 +659,16 @@ extern int LoadEXRFromMemory(float **out_rgba, int *width, int *height, //#include "zlib.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 +// instead declare the two relevant functions manually. +// from stb_image.h: +extern "C" int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen); +// from stb_image_write.h: +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__ @@ -673,6 +723,18 @@ static void SetErrorMessage(const std::string &msg, const char **err) { } } +#if 0 +static void SetWarningMessage(const std::string &msg, const char **warn) { + if (warn) { +#ifdef _WIN32 + (*warn) = _strdup(msg.c_str()); +#else + (*warn) = strdup(msg.c_str()); +#endif + } +} +#endif + static const int kEXRVersionSize = 8; static void cpy2(unsigned short *dst_val, const unsigned short *src_val) { @@ -1039,7 +1101,7 @@ static void WriteAttributeToMemory(std::vector *out, out->insert(out->end(), data, data + len); } -typedef struct { +typedef struct TChannelInfo { std::string name; // less than 255 bytes long int pixel_type; int requested_pixel_type; @@ -1129,6 +1191,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())); @@ -1209,7 +1272,7 @@ static void WriteChannelInfo(std::vector &data, (*p) = '\0'; } -static void CompressZip(unsigned char *dst, +static bool CompressZip(unsigned char *dst, tinyexr::tinyexr_uint64 &compressedSize, const unsigned char *src, unsigned long src_size) { std::vector tmpBuf(src_size); @@ -1269,15 +1332,28 @@ static void CompressZip(unsigned char *dst, int ret = mz_compress( dst, &outSize, static_cast(&tmpBuf.at(0)), src_size); - assert(ret == MZ_OK); - (void)ret; + if (ret != MZ_OK) { + return false; + } + + compressedSize = outSize; +#elif TINYEXR_USE_STB_ZLIB + int outSize; + unsigned char* ret = stbi_zlib_compress(const_cast(&tmpBuf.at(0)), src_size, &outSize, 8); + if (!ret) { + return false; + } + memcpy(dst, ret, outSize); + free(ret); compressedSize = outSize; #else uLong outSize = compressBound(static_cast(src_size)); int ret = compress(dst, &outSize, static_cast(&tmpBuf.at(0)), src_size); - assert(ret == Z_OK); + if (ret != Z_OK) { + return false; + } compressedSize = outSize; #endif @@ -1288,6 +1364,8 @@ static void CompressZip(unsigned char *dst, compressedSize = src_size; memcpy(dst, src, src_size); } + + return true; } static bool DecompressZip(unsigned char *dst, @@ -1306,6 +1384,12 @@ static bool DecompressZip(unsigned char *dst, if (MZ_OK != ret) { return false; } +#elif TINYEXR_USE_STB_ZLIB + int ret = stbi_zlib_decode_buffer(reinterpret_cast(&tmpBuf.at(0)), + *uncompressed_size, reinterpret_cast(src), src_size); + if (ret < 0) { + return false; + } #else int ret = uncompress(&tmpBuf.at(0), uncompressed_size, src, src_size); if (Z_OK != ret) { @@ -1433,7 +1517,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. // @@ -1456,7 +1540,7 @@ static int rleUncompress(int inLength, int maxLength, const signed char in[], int count = *in++; inLength -= 2; - if (0 > (maxLength -= count + 1)) return 0; + if ((0 > (maxLength -= count + 1)) || (inLength < 0)) return 0; memset(out, *reinterpret_cast(in), count + 1); out += count + 1; @@ -2436,7 +2520,7 @@ static bool hufBuildDecTable(const long long *hcode, // i : encoding table unsigned int *p = pl->p; pl->p = new unsigned int[pl->lit]; - for (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 { @@ -2691,7 +2775,7 @@ static bool hufDecode(const long long *hcode, // i : encoding table // Search long code // - int j; + unsigned int j; for (j = 0; j < pl.lit; j++) { int l = hufLength(hcode[pl.p[j]]); @@ -3082,21 +3166,41 @@ static bool DecompressPiz(unsigned char *outPtr, const unsigned char *inPtr, 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 { + return false; } std::vector lut(USHORT_RANGE); @@ -3109,6 +3213,10 @@ static bool DecompressPiz(unsigned char *outPtr, const unsigned char *inPtr, int length; + if ((readLen + 4) > inLen) { + return false; + } + // length = *(reinterpret_cast(ptr)); tinyexr::cpy4(&length, reinterpret_cast(ptr)); ptr += sizeof(int); @@ -3544,8 +3652,8 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, assert(requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT); 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(x_stride) + - channel_offset_list[c] * static_cast(x_stride))); + v * pixel_data_size * static_cast(width) + + channel_offset_list[c] * static_cast(width))); for (size_t u = 0; u < static_cast(width); u++) { float val; // val = line_ptr[u]; @@ -3572,7 +3680,7 @@ static bool DecodePixelData(/* out */ unsigned char **out_images, } } #else - assert(0 && "PIZ is enabled in this build"); + assert(0 && "PIZ is disabled in this build"); return false; #endif @@ -3965,7 +4073,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); @@ -4020,12 +4128,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); @@ -4106,14 +4215,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); @@ -4142,7 +4258,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; } } @@ -4258,6 +4393,13 @@ static int ParseEXRHeader(HeaderInfo *info, bool *empty_header, if ((version->tiled || version->multipart || version->non_image) && attr_name.compare("tiles") == 0) { unsigned int x_size, y_size; unsigned char tile_mode; + if (data.size() != 9) { + if (err) { + (*err) += "(ParseEXRHeader) Invalid attribute data size. Attribute data size must be 9.\n"; + } + 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)); @@ -4427,6 +4569,7 @@ static int ParseEXRHeader(HeaderInfo *info, bool *empty_header, #endif attrib.name[255] = '\0'; attrib.type[255] = '\0'; + //std::cout << "i = " << info->attributes.size() << ", dsize = " << data.size() << "\n"; attrib.size = static_cast(data.size()); attrib.value = static_cast(malloc(data.size())); memcpy(reinterpret_cast(attrib.value), &data.at(0), @@ -4493,6 +4636,7 @@ static int ParseEXRHeader(HeaderInfo *info, bool *empty_header, if (err) { (*err) += ss_err.str(); } + return TINYEXR_ERROR_INVALID_HEADER; } } @@ -4503,7 +4647,7 @@ static int ParseEXRHeader(HeaderInfo *info, bool *empty_header, } // C++ HeaderInfo to C EXRHeader conversion. -static void ConvertHeader(EXRHeader *exr_header, const HeaderInfo &info) { +static bool ConvertHeader(EXRHeader *exr_header, const HeaderInfo &info, std::string *warn, std::string *err) { exr_header->pixel_aspect_ratio = info.pixel_aspect_ratio; exr_header->screen_window_center[0] = info.screen_window_center[0]; exr_header->screen_window_center[1] = info.screen_window_center[1]; @@ -4527,19 +4671,49 @@ static void ConvertHeader(EXRHeader *exr_header, const HeaderInfo &info) { EXRSetNameAttr(exr_header, info.name.c_str()); + if (!info.type.empty()) { + bool valid = true; if (info.type == "scanlineimage") { - assert(!exr_header->tiled); + if (exr_header->tiled) { + if (err) { + (*err) += "(ConvertHeader) tiled bit must be off for `scanlineimage` type.\n"; + } + valid = false; + } } else if (info.type == "tiledimage") { - assert(exr_header->tiled); + if (!exr_header->tiled) { + if (err) { + (*err) += "(ConvertHeader) tiled bit must be on for `tiledimage` type.\n"; + } + valid = false; + } } else if (info.type == "deeptile") { exr_header->non_image = 1; - assert(exr_header->tiled); + if (!exr_header->tiled) { + if (err) { + (*err) += "(ConvertHeader) tiled bit must be on for `deeptile` type.\n"; + } + valid = false; + } } else if (info.type == "deepscanline") { exr_header->non_image = 1; - assert(!exr_header->tiled); + if (exr_header->tiled) { + if (err) { + (*err) += "(ConvertHeader) tiled bit must be off for `deepscanline` type.\n"; + } + //valid = false; + } } else { - assert(false); + if (warn) { + std::stringstream ss; + ss << "(ConvertHeader) Unsupported or unknown info.type: " << info.type << "\n"; + (*warn) += ss.str(); + } + } + + if (!valid) { + return false; } } @@ -4587,7 +4761,7 @@ static void ConvertHeader(EXRHeader *exr_header, const HeaderInfo &info) { exr_header->custom_attributes = static_cast(malloc( sizeof(EXRAttribute) * size_t(exr_header->num_custom_attributes))); - for (size_t i = 0; i < info.attributes.size(); i++) { + for (size_t i = 0; i < size_t(exr_header->num_custom_attributes); i++) { memcpy(exr_header->custom_attributes[i].name, info.attributes[i].name, 256); memcpy(exr_header->custom_attributes[i].type, info.attributes[i].type, @@ -4602,16 +4776,18 @@ static void ConvertHeader(EXRHeader *exr_header, const HeaderInfo &info) { } exr_header->header_len = info.header_len; + + return true; } 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; }; -int LevelIndex(int lx, int ly, int tile_level_mode, int num_x_levels) { +static int LevelIndex(int lx, int ly, int tile_level_mode, int num_x_levels) { switch (tile_level_mode) { case TINYEXR_TILE_ONE_LEVEL: return 0; @@ -4631,7 +4807,7 @@ int LevelIndex(int lx, int ly, int tile_level_mode, int num_x_levels) { static int LevelSize(int toplevel_size, int level, int tile_rounding_mode) { assert(level >= 0); - int b = (int)(1u << (unsigned)level); + int b = static_cast(1u << static_cast(level)); int level_size = toplevel_size / b; if (tile_rounding_mode == TINYEXR_TILE_ROUND_UP && level_size * b < toplevel_size) @@ -4649,9 +4825,9 @@ static int DecodeTiledLevel(EXRImage* exr_image, const EXRHeader* exr_header, int num_channels = exr_header->num_channels; 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[level_index].size(); + int num_y_tiles = int(offset_data.offsets[size_t(level_index)].size()); assert(num_y_tiles); - int num_x_tiles = (int)offset_data.offsets[level_index][0].size(); + int num_x_tiles = int(offset_data.offsets[size_t(level_index)][0].size()); assert(num_x_tiles); int num_tiles = num_x_tiles * num_y_tiles; @@ -4668,7 +4844,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 @@ -4705,20 +4881,26 @@ 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; // 16 byte: tile coordinates // 4 byte : data size // ~ : data(uncompressed or compressed) - tinyexr::tinyexr_uint64 offset = offset_data.offsets[level_index][y_tile][x_tile]; + 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; } @@ -4784,7 +4966,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 @@ -4986,9 +5168,21 @@ 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); + data_width, 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; @@ -5106,7 +5300,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; } @@ -5277,7 +5479,7 @@ static void CalculateNumTiles(std::vector& numTiles, int size, int tile_rounding_mode) { for (unsigned i = 0; i < numTiles.size(); i++) { - int l = LevelSize(toplevel_size, i, tile_rounding_mode); + int l = LevelSize(toplevel_size, int(i), tile_rounding_mode); assert(l <= std::numeric_limits::max() - size + 1); numTiles[i] = (l + size - 1) / size; @@ -5295,8 +5497,8 @@ static void PrecalculateTileInfo(std::vector& num_x_tiles, int num_x_levels = CalculateNumXLevels(exr_header); int num_y_levels = CalculateNumYLevels(exr_header); - num_x_tiles.resize(num_x_levels); - num_y_tiles.resize(num_y_levels); + num_x_tiles.resize(size_t(num_x_levels)); + num_y_tiles.resize(size_t(num_y_levels)); CalculateNumTiles(num_x_tiles, max_x - min_x + 1, @@ -5329,13 +5531,13 @@ static int InitTileOffsets(OffsetData& offset_data, case TINYEXR_TILE_ONE_LEVEL: case TINYEXR_TILE_MIPMAP_LEVELS: assert(offset_data.num_x_levels == offset_data.num_y_levels); - offset_data.offsets.resize(offset_data.num_x_levels); + offset_data.offsets.resize(size_t(offset_data.num_x_levels)); for (unsigned int l = 0; l < offset_data.offsets.size(); ++l) { - offset_data.offsets[l].resize(num_y_tiles[l]); + offset_data.offsets[l].resize(size_t(num_y_tiles[l])); for (unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) { - offset_data.offsets[l][dy].resize(num_x_tiles[l]); + offset_data.offsets[l][dy].resize(size_t(num_x_tiles[l])); num_tile_blocks += num_x_tiles[l]; } } @@ -5348,11 +5550,11 @@ static int InitTileOffsets(OffsetData& offset_data, for (int ly = 0; ly < offset_data.num_y_levels; ++ly) { for (int lx = 0; lx < offset_data.num_x_levels; ++lx) { int l = ly * offset_data.num_x_levels + lx; - offset_data.offsets[l].resize(num_y_tiles[ly]); + offset_data.offsets[size_t(l)].resize(size_t(num_y_tiles[size_t(ly)])); - for (size_t dy = 0; dy < offset_data.offsets[l].size(); ++dy) { - offset_data.offsets[l][dy].resize(num_x_tiles[lx]); - num_tile_blocks += num_x_tiles[lx]; + for (size_t dy = 0; dy < offset_data.offsets[size_t(l)].size(); ++dy) { + offset_data.offsets[size_t(l)][dy].resize(size_t(num_x_tiles[size_t(lx)])); + num_tile_blocks += num_x_tiles[size_t(lx)]; } } } @@ -5387,7 +5589,7 @@ static bool isValidTile(const EXRHeader* exr_header, ly == 0 && offset_data.offsets.size() > 0 && offset_data.offsets[0].size() > static_cast(dy) && - offset_data.offsets[0][dy].size() > static_cast(dx)) { + offset_data.offsets[0][size_t(dy)].size() > static_cast(dx)) { return true; } @@ -5398,8 +5600,8 @@ static bool isValidTile(const EXRHeader* exr_header, if (lx < num_x_levels && ly < num_y_levels && offset_data.offsets.size() > static_cast(lx) && - offset_data.offsets[lx].size() > static_cast(dy) && - offset_data.offsets[lx][dy].size() > static_cast(dx)) { + offset_data.offsets[size_t(lx)].size() > static_cast(dy) && + offset_data.offsets[size_t(lx)][size_t(dy)].size() > static_cast(dx)) { return true; } @@ -5412,7 +5614,7 @@ static bool isValidTile(const EXRHeader* exr_header, ly < num_y_levels && (offset_data.offsets.size() > idx) && offset_data.offsets[idx].size() > static_cast(dy) && - offset_data.offsets[idx][dy].size() > static_cast(dx)) { + offset_data.offsets[idx][size_t(dy)].size() > static_cast(dx)) { return true; } } @@ -5436,7 +5638,7 @@ static void ReconstructTileOffsets(OffsetData& offset_data, 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) { - tinyexr::tinyexr_uint64 tileOffset = marker - head; + tinyexr::tinyexr_uint64 tileOffset = tinyexr::tinyexr_uint64(marker - head); if (isMultiPartFile) { //int partNumber; @@ -5491,7 +5693,7 @@ static void ReconstructTileOffsets(OffsetData& offset_data, return; int level_idx = LevelIndex(levelX, levelY, exr_header->tile_level_mode, numXLevels); - offset_data.offsets[level_idx][tileY][tileX] = tileOffset; + offset_data.offsets[size_t(level_idx)][size_t(tileY)][size_t(tileX)] = tileOffset; } } } @@ -5596,7 +5798,7 @@ static int DecodeEXRImage(EXRImage *exr_image, const EXRHeader *exr_header, { std::vector num_x_tiles, num_y_tiles; PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_header); - num_blocks = InitTileOffsets(offset_data, exr_header, num_x_tiles, num_y_tiles); + 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)) { tinyexr::SetErrorMessage("Invalid offset table size.", err); @@ -5730,11 +5932,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 { @@ -5871,8 +6076,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; @@ -5902,25 +6113,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]; @@ -5934,7 +6147,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; @@ -5972,23 +6187,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]; @@ -6006,7 +6227,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] = @@ -6043,6 +6266,17 @@ int IsEXR(const char *filename) { return TINYEXR_SUCCESS; } +int IsEXRFromMemory(const unsigned char *memory, size_t size) { + EXRVersion exr_version; + + int ret = ParseEXRVersionFromMemory(&exr_version, memory, size); + if (ret != TINYEXR_SUCCESS) { + return ret; + } + + return TINYEXR_SUCCESS; +} + int ParseEXRHeaderFromMemory(EXRHeader *exr_header, const EXRVersion *version, const unsigned char *memory, size_t size, const char **err) { @@ -6067,16 +6301,35 @@ int ParseEXRHeaderFromMemory(EXRHeader *exr_header, const EXRVersion *version, tinyexr::HeaderInfo info; info.clear(); - std::string err_str; - int ret = ParseEXRHeader(&info, NULL, version, &err_str, marker, marker_size); + int ret; + { + std::string err_str; + ret = ParseEXRHeader(&info, NULL, version, &err_str, marker, marker_size); - if (ret != TINYEXR_SUCCESS) { - if (err && !err_str.empty()) { - tinyexr::SetErrorMessage(err_str, err); + if (ret != TINYEXR_SUCCESS) { + if (err && !err_str.empty()) { + tinyexr::SetErrorMessage(err_str, err); + } } } - ConvertHeader(exr_header, info); + { + std::string warn; + std::string err_str; + + if (!ConvertHeader(exr_header, info, &warn, &err_str)) { + // release mem + for (size_t i = 0; i < info.attributes.size(); i++) { + if (info.attributes[i].value) { + free(info.attributes[i].value); + } + } + if (err && !err_str.empty()) { + tinyexr::SetErrorMessage(err_str, err); + } + ret = TINYEXR_ERROR_INVALID_HEADER; + } + } exr_header->multipart = version->multipart ? 1 : 0; exr_header->non_image = version->non_image ? 1 : 0; @@ -6150,23 +6403,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]; @@ -6180,7 +6439,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; @@ -6216,23 +6477,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]; @@ -6249,7 +6516,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] = @@ -6275,6 +6544,193 @@ 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) { + 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); + return; + } + + data = reinterpret_cast(malloc(size)); + if (!data) { + fclose(fp); + return; + } + size_t read_bytes = fread(data, 1, size, fp); + assert(read_bytes == size); + fclose(fp); + (void)read_bytes; +#endif + assert(valid()); + } + + // 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) { @@ -6282,50 +6738,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); } @@ -6355,9 +6780,14 @@ int LoadEXRImageFromMemory(EXRImage *exr_image, const EXRHeader *exr_header, namespace tinyexr { +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wsign-conversion" +#endif + // 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*/, @@ -6369,6 +6799,7 @@ static bool EncodePixelData(/* out */ std::vector& out_data, size_t pixel_data_size, const std::vector& channels, const std::vector& channel_offset_list, + std::string *err, const void* compression_param = 0) // zfp compression param { size_t buf_size = static_cast(width) * @@ -6386,13 +6817,13 @@ static bool EncodePixelData(/* out */ std::vector& out_data, for (int y = 0; y < num_lines; y++) { // Assume increasing Y float *line_ptr = reinterpret_cast(&buf.at( - static_cast(pixel_data_size * y * width) + + static_cast(pixel_data_size * size_t(y) * size_t(width)) + channel_offset_list[c] * static_cast(width))); for (int x = 0; x < width; x++) { tinyexr::FP16 h16; h16.u = reinterpret_cast( - images)[c][(y + start_y) * x_stride + x]; + images)[c][(y + start_y) * size_t(x_stride) + size_t(x)]; tinyexr::FP32 f32 = half_to_float(h16); @@ -6421,7 +6852,10 @@ static bool EncodePixelData(/* out */ std::vector& out_data, } } } else { - assert(0); + if (err) { + (*err) += "Invalid requested_pixel_type.\n"; + } + return false; } } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) { @@ -6465,7 +6899,10 @@ static bool EncodePixelData(/* out */ std::vector& out_data, } } } else { - assert(0); + if (err) { + (*err) += "Invalid requested_pixel_type.\n"; + } + return false; } } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_UINT) { for (int y = 0; y < num_lines; y++) { @@ -6497,15 +6934,24 @@ static bool EncodePixelData(/* out */ std::vector& out_data, #if TINYEXR_USE_MINIZ 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()); #else std::vector block( compressBound(static_cast(buf.size()))); #endif tinyexr::tinyexr_uint64 outSize = block.size(); - tinyexr::CompressZip(&block.at(0), outSize, + if (!tinyexr::CompressZip(&block.at(0), outSize, reinterpret_cast(&buf.at(0)), - static_cast(buf.size())); + static_cast(buf.size()))) { + if (err) { + (*err) += "Zip compresssion failed.\n"; + } + return false; + } // 4 byte: scan line // 4 byte: data size @@ -6550,7 +6996,10 @@ static bool EncodePixelData(/* out */ std::vector& out_data, out_data.insert(out_data.end(), block.begin(), block.begin() + data_len); #else - assert(0); + if (err) { + (*err) += "PIZ compression is disabled in this build.\n"; + } + return false; #endif } else if (compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) { #if TINYEXR_USE_ZFP @@ -6569,11 +7018,13 @@ static bool EncodePixelData(/* out */ std::vector& out_data, out_data.insert(out_data.end(), block.begin(), block.begin() + data_len); #else + if (err) { + (*err) += "ZFP compression is disabled in this build.\n"; + } (void)compression_param; - assert(0); + return false; #endif } else { - assert(0); return false; } @@ -6637,13 +7088,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 @@ -6655,7 +7106,7 @@ static int EncodeTiledLevel(const EXRImage* level_image, const EXRHeader* exr_he pixel_data_size, channels, channel_offset_list, - compression_param); + err, compression_param); if (!ret) { invalid_data = true; continue; @@ -6689,7 +7140,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; } @@ -6772,7 +7223,7 @@ static int EncodeChunk(const EXRImage* exr_image, const EXRHeader* exr_header, if (exr_image->tiles) { const EXRImage* level_image = exr_image; size_t block_idx = 0; - tinyexr::tinyexr_uint64 block_data_size = 0; + //tinyexr::tinyexr_uint64 block_data_size = 0; int num_levels = (exr_header->tile_level_mode != TINYEXR_TILE_RIPMAP_LEVELS) ? offset_data.num_x_levels : (offset_data.num_x_levels * offset_data.num_y_levels); for (int level_index = 0; level_index < num_levels; ++level_index) { @@ -6791,9 +7242,9 @@ 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(); + int num_y_tiles = int(offset_data.offsets[level_index].size()); assert(num_y_tiles); - int num_x_tiles = (int)offset_data.offsets[level_index][0].size(); + int num_x_tiles = int(offset_data.offsets[level_index][0].size()); assert(num_x_tiles); std::string e; @@ -6820,7 +7271,7 @@ static int EncodeChunk(const EXRImage* exr_image, const EXRHeader* exr_header, offset_data.offsets[level_index][j][i] = offset; swap8(reinterpret_cast(&offset_data.offsets[level_index][j][i])); offset += data_list[block_idx].size() + doffset; - block_data_size += data_list[block_idx].size(); + //block_data_size += data_list[block_idx].size(); ++block_idx; } level_image = level_image->next_level; @@ -6860,7 +7311,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 @@ -6872,10 +7323,11 @@ static int EncodeChunk(const EXRImage* exr_image, const EXRHeader* exr_header, pixel_data_size, channels, channel_offset_list, + err, compression_param); if (!ret) { invalid_data = true; - continue; // "break" cannot be used with OpenMP + 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); @@ -7119,7 +7571,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; @@ -7203,7 +7659,7 @@ static size_t SaveEXRNPartImageToMemory(const EXRImage* exr_images, tinyexr::SetErrorMessage("Output memory size is zero", err); return 0; } - (*memory_out) = static_cast(malloc(total_size)); + (*memory_out) = static_cast(malloc(size_t(total_size))); // Writing header memcpy((*memory_out), &memory[0], memory.size()); @@ -7256,9 +7712,13 @@ static size_t SaveEXRNPartImageToMemory(const EXRImage* exr_images, } } assert(sum == total_size); - return total_size; // OK + return size_t(total_size); // OK } +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + } // tinyexr size_t SaveEXRImageToMemory(const EXRImage* exr_image, @@ -7317,7 +7777,8 @@ 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) { - return TINYEXR_ERROR_SERIALZATION_FAILED; + fclose(fp); + return TINYEXR_ERROR_SERIALIZATION_FAILED; } size_t written_size = 0; @@ -7386,7 +7847,8 @@ 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) { - return TINYEXR_ERROR_SERIALZATION_FAILED; + fclose(fp); + return TINYEXR_ERROR_SERIALIZATION_FAILED; } size_t written_size = 0; @@ -7411,58 +7873,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); + 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 - 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); - 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. { @@ -7497,7 +7921,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; @@ -7867,7 +8291,7 @@ void EXRSetNameAttr(EXRHeader* exr_header, const char* name) { } memset(exr_header->name, 0, 256); if (name != NULL) { - size_t len = std::min(strlen(name), (size_t)255); + size_t len = std::min(strlen(name), size_t(255)); if (len) { memcpy(exr_header->name, name, len); } @@ -7928,48 +8352,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); } @@ -8006,6 +8395,14 @@ int ParseEXRMultipartHeaderFromMemory(EXRHeader ***exr_headers, marker, marker_size); if (ret != TINYEXR_SUCCESS) { + + // Free malloc-allocated memory here. + for (size_t i = 0; i < info.attributes.size(); i++) { + if (info.attributes[i].value) { + free(info.attributes[i].value); + } + } + tinyexr::SetErrorMessage(err_str, err); return ret; } @@ -8017,6 +8414,14 @@ int ParseEXRMultipartHeaderFromMemory(EXRHeader ***exr_headers, // `chunkCount` must exist in the header. if (info.chunk_count == 0) { + + // Free malloc-allocated memory here. + for (size_t i = 0; i < info.attributes.size(); i++) { + if (info.attributes[i].value) { + free(info.attributes[i].value); + } + } + tinyexr::SetErrorMessage( "`chunkCount' attribute is not found in the header.", err); return TINYEXR_ERROR_INVALID_DATA; @@ -8032,11 +8437,32 @@ int ParseEXRMultipartHeaderFromMemory(EXRHeader ***exr_headers, // allocate memory for EXRHeader and create array of EXRHeader pointers. (*exr_headers) = static_cast(malloc(sizeof(EXRHeader *) * infos.size())); + + + int retcode = TINYEXR_SUCCESS; + for (size_t i = 0; i < infos.size(); i++) { EXRHeader *exr_header = static_cast(malloc(sizeof(EXRHeader))); memset(exr_header, 0, sizeof(EXRHeader)); - ConvertHeader(exr_header, infos[i]); + std::string warn; + std::string _err; + if (!ConvertHeader(exr_header, infos[i], &warn, &_err)) { + + // Free malloc-allocated memory here. + for (size_t k = 0; k < infos[i].attributes.size(); i++) { + if (infos[i].attributes[k].value) { + free(infos[i].attributes[k].value); + } + } + + if (!_err.empty()) { + tinyexr::SetErrorMessage( + _err, err); + } + // continue to converting headers + retcode = TINYEXR_ERROR_INVALID_HEADER; + } exr_header->multipart = exr_version->multipart ? 1 : 0; @@ -8045,7 +8471,7 @@ int ParseEXRMultipartHeaderFromMemory(EXRHeader ***exr_headers, (*num_headers) = static_cast(infos.size()); - return TINYEXR_SUCCESS; + return retcode; } int ParseEXRMultipartHeaderFromFile(EXRHeader ***exr_headers, int *num_headers, @@ -8058,48 +8484,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, @@ -8183,16 +8575,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); @@ -8250,7 +8636,7 @@ int LoadEXRMultipartImageFromMemory(EXRImage *exr_images, chunk_offset_table_list.resize(chunk_offset_table_list.size() + 1); tinyexr::OffsetData& offset_data = chunk_offset_table_list.back(); if (!exr_headers[i]->tiled || exr_headers[i]->tile_level_mode == TINYEXR_TILE_ONE_LEVEL) { - tinyexr::InitSingleResolutionOffsets(offset_data, exr_headers[i]->chunk_count); + tinyexr::InitSingleResolutionOffsets(offset_data, size_t(exr_headers[i]->chunk_count)); std::vector& offset_table = offset_data.offsets[0][0]; for (size_t c = 0; c < offset_table.size(); c++) { @@ -8289,7 +8675,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 } } } @@ -8300,7 +8686,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) { @@ -8342,48 +8728,19 @@ 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 SaveEXR(const float *data, int width, int height, int components, - const int save_as_fp16, const char *outfilename, const char **err) { +int SaveEXRToMemory(const float *data, int width, int height, int components, + const int save_as_fp16, const unsigned char **outbuf, const char **err) { + if ((components == 1) || components == 3 || components == 4) { // OK } else { @@ -8509,6 +8866,157 @@ int SaveEXR(const float *data, int width, int height, int components, } } + + unsigned char *mem_buf; + size_t mem_size = SaveEXRImageToMemory(&image, &header, &mem_buf, err); + + if (mem_size == 0) { + return TINYEXR_ERROR_SERIALIZATION_FAILED; + } + + free(header.channels); + free(header.pixel_types); + free(header.requested_pixel_types); + + if (mem_size > size_t(std::numeric_limits::max())) { + free(mem_buf); + return TINYEXR_ERROR_DATA_TOO_LARGE; + } + + (*outbuf) = mem_buf; + + return int(mem_size); +} + +int SaveEXR(const float *data, int width, int height, int components, + const int save_as_fp16, const char *outfilename, const char **err) { + if ((components == 1) || components == 3 || components == 4) { + // OK + } else { + std::stringstream ss; + ss << "Unsupported component value : " << components << std::endl; + + tinyexr::SetErrorMessage(ss.str(), err); + return TINYEXR_ERROR_INVALID_ARGUMENT; + } + + EXRHeader header; + InitEXRHeader(&header); + + if ((width < 16) && (height < 16)) { + // No compression for small image. + header.compression_type = TINYEXR_COMPRESSIONTYPE_NONE; + } else { + header.compression_type = TINYEXR_COMPRESSIONTYPE_ZIP; + } + + EXRImage image; + InitEXRImage(&image); + + 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(pixel_count); + memcpy(images[0].data(), data, sizeof(float) * pixel_count); + } else { + 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 < 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]; + if (components == 4) { + images[3][i] = data[static_cast(components) * i + 3]; + } + } + } + + float *image_ptr[4] = {0, 0, 0, 0}; + if (components == 4) { + image_ptr[0] = &(images[3].at(0)); // A + image_ptr[1] = &(images[2].at(0)); // B + image_ptr[2] = &(images[1].at(0)); // G + image_ptr[3] = &(images[0].at(0)); // R + } else if (components == 3) { + image_ptr[0] = &(images[2].at(0)); // B + image_ptr[1] = &(images[1].at(0)); // G + image_ptr[2] = &(images[0].at(0)); // R + } else if (components == 1) { + image_ptr[0] = &(images[0].at(0)); // A + } + + image.images = reinterpret_cast(image_ptr); + image.width = width; + image.height = height; + + header.num_channels = components; + header.channels = static_cast(malloc( + sizeof(EXRChannelInfo) * static_cast(header.num_channels))); + // Must be (A)BGR order, since most of EXR viewers expect this channel order. + if (components == 4) { +#ifdef _MSC_VER + strncpy_s(header.channels[0].name, "A", 255); + strncpy_s(header.channels[1].name, "B", 255); + strncpy_s(header.channels[2].name, "G", 255); + strncpy_s(header.channels[3].name, "R", 255); +#else + strncpy(header.channels[0].name, "A", 255); + strncpy(header.channels[1].name, "B", 255); + strncpy(header.channels[2].name, "G", 255); + strncpy(header.channels[3].name, "R", 255); +#endif + header.channels[0].name[strlen("A")] = '\0'; + header.channels[1].name[strlen("B")] = '\0'; + header.channels[2].name[strlen("G")] = '\0'; + header.channels[3].name[strlen("R")] = '\0'; + } else if (components == 3) { +#ifdef _MSC_VER + strncpy_s(header.channels[0].name, "B", 255); + strncpy_s(header.channels[1].name, "G", 255); + strncpy_s(header.channels[2].name, "R", 255); +#else + strncpy(header.channels[0].name, "B", 255); + strncpy(header.channels[1].name, "G", 255); + strncpy(header.channels[2].name, "R", 255); +#endif + header.channels[0].name[strlen("B")] = '\0'; + header.channels[1].name[strlen("G")] = '\0'; + header.channels[2].name[strlen("R")] = '\0'; + } else { +#ifdef _MSC_VER + strncpy_s(header.channels[0].name, "A", 255); +#else + strncpy(header.channels[0].name, "A", 255); +#endif + header.channels[0].name[strlen("A")] = '\0'; + } + + header.pixel_types = static_cast( + malloc(sizeof(int) * static_cast(header.num_channels))); + header.requested_pixel_types = static_cast( + malloc(sizeof(int) * static_cast(header.num_channels))); + for (int i = 0; i < header.num_channels; i++) { + header.pixel_types[i] = + TINYEXR_PIXELTYPE_FLOAT; // pixel type of input image + + if (save_as_fp16 > 0) { + header.requested_pixel_types[i] = + TINYEXR_PIXELTYPE_HALF; // save with half(fp16) pixel format + } else { + header.requested_pixel_types[i] = + TINYEXR_PIXELTYPE_FLOAT; // save with float(fp32) pixel format(i.e. + // no precision reduction) + } + } + int ret = SaveEXRImageToFile(&image, &header, outfilename, err); if (ret != TINYEXR_SUCCESS) { return ret; @@ -8522,7 +9030,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