91#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(i386) || defined(__ia64__) || defined(__x86_64__)
92#define TINYEXR_X86_OR_X64_CPU 1
94#define TINYEXR_X86_OR_X64_CPU 0
97#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || TINYEXR_X86_OR_X64_CPU
98#define TINYEXR_LITTLE_ENDIAN 1
100#define TINYEXR_LITTLE_ENDIAN 0
105#ifndef TINYEXR_USE_MINIZ
106#define TINYEXR_USE_MINIZ (1)
110#ifndef TINYEXR_USE_STB_ZLIB
111#define TINYEXR_USE_STB_ZLIB (0)
115#ifndef TINYEXR_USE_NANOZLIB
116#define TINYEXR_USE_NANOZLIB (0)
120#ifndef TINYEXR_USE_PIZ
121#define TINYEXR_USE_PIZ (1)
124#ifndef TINYEXR_USE_ZFP
125#define TINYEXR_USE_ZFP (0)
129#ifndef TINYEXR_USE_THREAD
130#define TINYEXR_USE_THREAD (0)
134#ifndef TINYEXR_USE_OPENMP
136#define TINYEXR_USE_OPENMP (1)
138#define TINYEXR_USE_OPENMP (0)
142#define TINYEXR_SUCCESS (0)
143#define TINYEXR_ERROR_INVALID_MAGIC_NUMBER (-1)
144#define TINYEXR_ERROR_INVALID_EXR_VERSION (-2)
145#define TINYEXR_ERROR_INVALID_ARGUMENT (-3)
146#define TINYEXR_ERROR_INVALID_DATA (-4)
147#define TINYEXR_ERROR_INVALID_FILE (-5)
148#define TINYEXR_ERROR_INVALID_PARAMETER (-6)
149#define TINYEXR_ERROR_CANT_OPEN_FILE (-7)
150#define TINYEXR_ERROR_UNSUPPORTED_FORMAT (-8)
151#define TINYEXR_ERROR_INVALID_HEADER (-9)
152#define TINYEXR_ERROR_UNSUPPORTED_FEATURE (-10)
153#define TINYEXR_ERROR_CANT_WRITE_FILE (-11)
154#define TINYEXR_ERROR_SERIALIZATION_FAILED (-12)
155#define TINYEXR_ERROR_LAYER_NOT_FOUND (-13)
156#define TINYEXR_ERROR_DATA_TOO_LARGE (-14)
161#define TINYEXR_PIXELTYPE_UINT (0)
162#define TINYEXR_PIXELTYPE_HALF (1)
163#define TINYEXR_PIXELTYPE_FLOAT (2)
165#define TINYEXR_MAX_HEADER_ATTRIBUTES (1024)
166#define TINYEXR_MAX_CUSTOM_ATTRIBUTES (128)
168#define TINYEXR_COMPRESSIONTYPE_NONE (0)
169#define TINYEXR_COMPRESSIONTYPE_RLE (1)
170#define TINYEXR_COMPRESSIONTYPE_ZIPS (2)
171#define TINYEXR_COMPRESSIONTYPE_ZIP (3)
172#define TINYEXR_COMPRESSIONTYPE_PIZ (4)
173#define TINYEXR_COMPRESSIONTYPE_ZFP (128)
175#define TINYEXR_ZFP_COMPRESSIONTYPE_RATE (0)
176#define TINYEXR_ZFP_COMPRESSIONTYPE_PRECISION (1)
177#define TINYEXR_ZFP_COMPRESSIONTYPE_ACCURACY (2)
179#define TINYEXR_TILE_ONE_LEVEL (0)
180#define TINYEXR_TILE_MIPMAP_LEVELS (1)
181#define TINYEXR_TILE_RIPMAP_LEVELS (2)
183#define TINYEXR_TILE_ROUND_DOWN (0)
184#define TINYEXR_TILE_ROUND_UP (1)
330extern int LoadEXR(
float** out_rgba,
int* width,
int* height,
331 const char*
filename,
const char** err);
340 const char*
filename,
const char* layer_name,
355 int* num_layers,
const char** err);
385 const int components,
const int save_as_fp16,
386 unsigned char** buffer,
const char** err);
400extern int SaveEXR(
const float* data,
const int width,
const int height,
401 const int components,
const int save_as_fp16,
402 const char*
filename,
const char** err);
430 const unsigned char* memory,
size_t size);
436 const char*
filename,
const char** err);
443 const unsigned char* memory,
size_t size,
463 const unsigned char* memory,
464 size_t size,
const char** err);
474 const char*
filename,
const char** err);
485 const unsigned char* memory,
486 const size_t size,
const char** err);
498 unsigned int num_parts,
512 unsigned int num_parts,
513 const unsigned char* memory,
514 const size_t size,
const char** err);
534 unsigned char** memory,
const char** err);
545 unsigned int num_parts,
546 const char*
filename,
const char** err);
558 unsigned int num_parts,
559 unsigned char** memory,
const char** err);
591 const unsigned char* memory,
size_t size,
600#ifdef TINYEXR_IMPLEMENTATION
601#ifndef TINYEXR_IMPLEMENTATION_DEFINED
602#define TINYEXR_IMPLEMENTATION_DEFINED
606#ifndef WIN32_LEAN_AND_MEAN
607#define WIN32_LEAN_AND_MEAN
614#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
615#define TINYEXR_USE_WIN32_MMAP (1)
618#elif defined(__linux__) || defined(__unix__)
623#define TINYEXR_USE_POSIX_MMAP (1)
640#if __cplusplus > 199711L || (defined(_MSC_VER) && _MSC_VER >= 1900)
641#define TINYEXR_HAS_CXX11 (1)
645#if TINYEXR_USE_THREAD
651#define TINYEXR_HAS_CXX11 (0)
654#if TINYEXR_USE_OPENMP
658#if defined(TINYEXR_USE_MINIZ) && (TINYEXR_USE_MINIZ == 1)
666#if defined(TINYEXR_USE_NANOZLIB) && (TINYEXR_USE_NANOZLIB == 1)
667#define NANOZLIB_IMPLEMENTATION
671#if TINYEXR_USE_STB_ZLIB
676extern "C" int stbi_zlib_decode_buffer(
char* obuffer,
int olen,
const char* ibuffer,
int ilen);
678extern "C" unsigned char* stbi_zlib_compress(
unsigned char* data,
int data_len,
int* out_len,
int quality);
684#pragma clang diagnostic push
685#pragma clang diagnostic ignored "-Weverything"
691#pragma clang diagnostic pop
699#define TINYEXR_CHECK_AND_RETURN_MSG(cond, msg, err) \
703 std::ostringstream ss_e; \
704 ss_e << __func__ << "():" << __LINE__ << msg << "\n"; \
705 (*err) += ss_e.str(); \
712#define TINYEXR_CHECK_AND_RETURN_C(cond, retcode) \
721#if __cplusplus > 199711L
723typedef uint64_t tinyexr_uint64;
724typedef int64_t tinyexr_int64;
729#pragma clang diagnostic push
730#pragma clang diagnostic ignored "-Wc++11-long-long"
732typedef unsigned long long tinyexr_uint64;
733typedef long long tinyexr_int64;
735#pragma clang diagnostic pop
748static void SetErrorMessage(
const std::string& msg,
const char** err)
752 (*err) = _strdup(msg.c_str());
754 (*err) = strdup(msg.c_str());
760static void SetWarningMessage(
const std::string &msg,
const char **warn) {
763 (*warn) = _strdup(msg.c_str());
765 (*warn) = strdup(msg.c_str());
771static const unsigned int kEXRVersionSize = 8;
773static void cpy2(
unsigned short* dst_val,
const unsigned short* src_val)
775 unsigned char* dst =
reinterpret_cast<unsigned char*
>(dst_val);
776 const unsigned char* src =
reinterpret_cast<const unsigned char*
>(src_val);
782static void swap2(
unsigned short* val)
784#if TINYEXR_LITTLE_ENDIAN
787 unsigned short tmp = *val;
788 unsigned char* dst =
reinterpret_cast<unsigned char*
>(val);
789 unsigned char* src =
reinterpret_cast<unsigned char*
>(&tmp);
797#pragma clang diagnostic push
798#pragma clang diagnostic ignored "-Wunused-function"
802#pragma GCC diagnostic push
803#pragma GCC diagnostic ignored "-Wunused-function"
805static void cpy4(
int* dst_val,
const int* src_val)
807 unsigned char* dst =
reinterpret_cast<unsigned char*
>(dst_val);
808 const unsigned char* src =
reinterpret_cast<const unsigned char*
>(src_val);
816static void cpy4(
unsigned int* dst_val,
const unsigned int* src_val)
818 unsigned char* dst =
reinterpret_cast<unsigned char*
>(dst_val);
819 const unsigned char* src =
reinterpret_cast<const unsigned char*
>(src_val);
827static void cpy4(
float* dst_val,
const float* src_val)
829 unsigned char* dst =
reinterpret_cast<unsigned char*
>(dst_val);
830 const unsigned char* src =
reinterpret_cast<const unsigned char*
>(src_val);
838#pragma clang diagnostic pop
842#pragma GCC diagnostic pop
845static void swap4(
unsigned int* val)
847#if TINYEXR_LITTLE_ENDIAN
850 unsigned int tmp = *val;
851 unsigned char* dst =
reinterpret_cast<unsigned char*
>(val);
852 unsigned char* src =
reinterpret_cast<unsigned char*
>(&tmp);
861static void swap4(
int* val)
863#if TINYEXR_LITTLE_ENDIAN
867 unsigned char* dst =
reinterpret_cast<unsigned char*
>(val);
868 unsigned char* src =
reinterpret_cast<unsigned char*
>(&tmp);
877static void swap4(
float* val)
879#if TINYEXR_LITTLE_ENDIAN
883 unsigned char* dst =
reinterpret_cast<unsigned char*
>(val);
884 unsigned char* src =
reinterpret_cast<unsigned char*
>(&tmp);
894static void cpy8(tinyexr::tinyexr_uint64 *dst_val,
const tinyexr::tinyexr_uint64 *src_val) {
895 unsigned char *dst =
reinterpret_cast<unsigned char *
>(dst_val);
896 const unsigned char *src =
reinterpret_cast<const unsigned char *
>(src_val);
909static void swap8(tinyexr::tinyexr_uint64* val)
911#if TINYEXR_LITTLE_ENDIAN
914 tinyexr::tinyexr_uint64 tmp = (*val);
915 unsigned char* dst =
reinterpret_cast<unsigned char*
>(val);
916 unsigned char* src =
reinterpret_cast<unsigned char*
>(&tmp);
934#if TINYEXR_LITTLE_ENDIAN
935 unsigned int Mantissa : 23;
936 unsigned int Exponent : 8;
937 unsigned int Sign : 1;
939 unsigned int Sign : 1;
940 unsigned int Exponent : 8;
941 unsigned int Mantissa : 23;
947#pragma clang diagnostic push
948#pragma clang diagnostic ignored "-Wpadded"
954#if TINYEXR_LITTLE_ENDIAN
955 unsigned int Mantissa : 10;
956 unsigned int Exponent : 5;
957 unsigned int Sign : 1;
959 unsigned int Sign : 1;
960 unsigned int Exponent : 5;
961 unsigned int Mantissa : 10;
967#pragma clang diagnostic pop
970static FP32 half_to_float(FP16 h)
972 static const FP32 magic = { 113 << 23 };
973 static const unsigned int shifted_exp = 0x7c00
977 o.u = (h.u & 0x7fffU) << 13U;
978 unsigned int exp_ = shifted_exp & o.u;
979 o.u += (127 - 15) << 23;
982 if (exp_ == shifted_exp)
983 o.u += (128 - 16) << 23;
990 o.u |= (h.u & 0x8000U) << 16U;
994static FP16 float_to_half_full(FP32 f)
999 if (f.s.Exponent == 0)
1001 else if (f.s.Exponent == 255)
1004 o.s.Mantissa = f.s.Mantissa ? 0x200 : 0;
1008 int newexp = f.s.Exponent - 127 + 15;
1011 else if (newexp <= 0)
1013 if ((14 - newexp) <= 24)
1015 unsigned int mant = f.s.Mantissa | 0x800000;
1016 o.s.Mantissa = mant >> (14 - newexp);
1017 if ((mant >> (13 - newexp)) & 1)
1021 o.s.Exponent =
static_cast<unsigned int>(newexp);
1022 o.s.Mantissa = f.s.Mantissa >> 13;
1023 if (f.s.Mantissa & 0x1000)
1028 o.s.Sign = f.s.Sign;
1047#pragma clang diagnostic push
1049#if __has_warning("-Wzero-as-null-pointer-constant")
1050#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
1055static const char* ReadString(std::string* s,
const char* ptr,
size_t len)
1058 const char* p = ptr;
1059 const char* q = ptr;
1060 while ((
size_t(q - ptr) < len) && (*q) != 0) {
1064 if (
size_t(q - ptr) >= len) {
1069 (*s) = std::string(p, q);
1074static bool ReadAttribute(std::string* name, std::string* type,
1075 std::vector<unsigned char>* data,
size_t* marker_size,
1076 const char* marker,
size_t size)
1078 size_t name_len = strnlen(marker,
size);
1079 if (name_len ==
size) {
1083 *name = std::string(marker, name_len);
1085 marker += name_len + 1;
1086 size -= name_len + 1;
1088 size_t type_len = strnlen(marker,
size);
1089 if (type_len ==
size) {
1092 *type = std::string(marker, type_len);
1094 marker += type_len + 1;
1095 size -= type_len + 1;
1097 if (
size <
sizeof(uint32_t)) {
1102 memcpy(&data_len, marker,
sizeof(uint32_t));
1103 tinyexr::swap4(
reinterpret_cast<unsigned int*
>(&data_len));
1105 if (data_len == 0) {
1106 if ((*type).compare(
"string") == 0) {
1109 marker +=
sizeof(uint32_t);
1110 size -=
sizeof(uint32_t);
1112 *marker_size = name_len + 1 + type_len + 1 +
sizeof(uint32_t);
1123 marker +=
sizeof(uint32_t);
1124 size -=
sizeof(uint32_t);
1126 if (
size < data_len) {
1130 data->resize(
static_cast<size_t>(data_len));
1131 memcpy(&data->at(0), marker,
static_cast<size_t>(data_len));
1133 *marker_size = name_len + 1 + type_len + 1 +
sizeof(uint32_t) + data_len;
1137static void WriteAttributeToMemory(std::vector<unsigned char>* out,
1138 const char* name,
const char* type,
1139 const unsigned char* data,
int len)
1141 out->insert(out->end(), name, name + strlen(name) + 1);
1142 out->insert(out->end(), type, type + strlen(type) + 1);
1145 tinyexr::swap4(&outLen);
1146 out->insert(out->end(),
reinterpret_cast<unsigned char*
>(&outLen),
1147 reinterpret_cast<unsigned char*
>(&outLen) +
sizeof(
int));
1148 out->insert(out->end(), data, data + len);
1151typedef struct TChannelInfo {
1154 int requested_pixel_type;
1157 unsigned char p_linear;
1158 unsigned char pad[3];
1169 std::vector<tinyexr::ChannelInfo> channels;
1170 std::vector<EXRAttribute> attributes;
1172 Box2iInfo data_window;
1174 Box2iInfo display_window;
1175 float screen_window_center[2];
1176 float screen_window_width;
1177 float pixel_aspect_ratio;
1185 int tile_level_mode;
1186 int tile_rounding_mode;
1188 unsigned int header_len;
1190 int compression_type;
1202 data_window.min_x = 0;
1203 data_window.min_y = 0;
1204 data_window.max_x = 0;
1205 data_window.max_y = 0;
1207 display_window.min_x = 0;
1208 display_window.min_y = 0;
1209 display_window.max_x = 0;
1210 display_window.max_y = 0;
1211 screen_window_center[0] = 0.0f;
1212 screen_window_center[1] = 0.0f;
1213 screen_window_width = 0.0f;
1214 pixel_aspect_ratio = 0.0f;
1222 tile_level_mode = 0;
1223 tile_rounding_mode = 0;
1226 compression_type = 0;
1233static bool ReadChannelInfo(std::vector<ChannelInfo>& channels,
1234 const std::vector<unsigned char>& data)
1236 const char* p =
reinterpret_cast<const char*
>(&data.at(0));
1243 info.requested_pixel_type = 0;
1245 tinyexr_int64 data_len =
static_cast<tinyexr_int64
>(data.size()) - (p -
reinterpret_cast<const char*
>(data.data()));
1250 p = ReadString(&info.name, p,
size_t(data_len));
1251 if ((p == NULL) && (info.name.empty())) {
1256 const unsigned char* data_end =
reinterpret_cast<const unsigned char*
>(p) + 16;
1257 if (data_end >= (data.data() + data.size())) {
1261 memcpy(&info.pixel_type, p,
sizeof(
int));
1263 info.p_linear =
static_cast<unsigned char>(p[0]);
1265 memcpy(&info.x_sampling, p,
sizeof(
int));
1267 memcpy(&info.y_sampling, p,
sizeof(
int));
1270 tinyexr::swap4(&info.pixel_type);
1271 tinyexr::swap4(&info.x_sampling);
1272 tinyexr::swap4(&info.y_sampling);
1274 channels.push_back(info);
1280static void WriteChannelInfo(std::vector<unsigned char>& data,
1281 const std::vector<ChannelInfo>& channels)
1286 for (
size_t c = 0; c < channels.size(); c++) {
1287 sz += channels[c].name.length() + 1;
1290 data.resize(sz + 1);
1292 unsigned char* p = &data.at(0);
1294 for (
size_t c = 0; c < channels.size(); c++) {
1295 memcpy(p, channels[c].name.c_str(), channels[c].name.length());
1296 p += channels[c].name.length();
1300 int pixel_type = channels[c].requested_pixel_type;
1301 int x_sampling = channels[c].x_sampling;
1302 int y_sampling = channels[c].y_sampling;
1303 tinyexr::swap4(&pixel_type);
1304 tinyexr::swap4(&x_sampling);
1305 tinyexr::swap4(&y_sampling);
1307 memcpy(p, &pixel_type,
sizeof(
int));
1310 (*p) = channels[c].p_linear;
1313 memcpy(p, &x_sampling,
sizeof(
int));
1316 memcpy(p, &y_sampling,
sizeof(
int));
1323static bool CompressZip(
unsigned char* dst,
1324 tinyexr::tinyexr_uint64& compressedSize,
1325 const unsigned char* src,
unsigned long src_size)
1327 std::vector<unsigned char> tmpBuf(src_size);
1338 const char* srcPtr =
reinterpret_cast<const char*
>(src);
1341 char* t1 =
reinterpret_cast<char*
>(&tmpBuf.at(0));
1342 char* t2 =
reinterpret_cast<char*
>(&tmpBuf.at(0)) + (src_size + 1) / 2;
1343 const char* stop = srcPtr + src_size;
1347 *(t1++) = *(srcPtr++);
1352 *(t2++) = *(srcPtr++);
1363 unsigned char* t = &tmpBuf.at(0) + 1;
1364 unsigned char* stop = &tmpBuf.at(0) + src_size;
1368 int d =
int(t[0]) - p + (128 + 256);
1370 t[0] =
static_cast<unsigned char>(d);
1375#if defined(TINYEXR_USE_MINIZ) && (TINYEXR_USE_MINIZ == 1)
1382 dst, &outSize,
static_cast<const unsigned char*
>(&tmpBuf.at(0)),
1388 compressedSize = outSize;
1389#elif defined(TINYEXR_USE_STB_ZLIB) && (TINYEXR_USE_STB_ZLIB == 1)
1391 unsigned char* ret = stbi_zlib_compress(
const_cast<unsigned char*
>(&tmpBuf.at(0)), src_size, &outSize, 8);
1395 memcpy(dst, ret, outSize);
1398 compressedSize = outSize;
1399#elif defined(TINYEXR_USE_NANOZLIB) && (TINYEXR_USE_NANOZLIB == 1)
1400 uint64_t dstSize = nanoz_compressBound(
static_cast<uint64_t
>(src_size));
1402 unsigned char* ret = nanoz_compress(&tmpBuf.at(0), src_size, &outSize, 8);
1407 memcpy(dst, ret, outSize);
1410 compressedSize = outSize;
1413 int ret =
compress(dst, &outSize,
static_cast<const Bytef*
>(&tmpBuf.at(0)),
1419 compressedSize = outSize;
1424 if (compressedSize >= src_size) {
1425 compressedSize = src_size;
1426 memcpy(dst, src, src_size);
1432static bool DecompressZip(
unsigned char* dst,
1433 unsigned long* uncompressed_size ,
1434 const unsigned char* src,
unsigned long src_size)
1436 if ((*uncompressed_size) == src_size) {
1438 memcpy(dst, src, src_size);
1441 std::vector<unsigned char> tmpBuf(*uncompressed_size);
1443#if defined(TINYEXR_USE_MINIZ) && (TINYEXR_USE_MINIZ == 1)
1444 int ret =
mz_uncompress(&tmpBuf.at(0), uncompressed_size, src, src_size);
1448#elif TINYEXR_USE_STB_ZLIB
1449 int ret = stbi_zlib_decode_buffer(
reinterpret_cast<char*
>(&tmpBuf.at(0)),
1450 *uncompressed_size,
reinterpret_cast<const char*
>(src), src_size);
1454#elif defined(TINYEXR_USE_NANOZLIB) && (TINYEXR_USE_NANOZLIB == 1)
1455 uint64_t dest_size = (*uncompressed_size);
1456 uint64_t uncomp_size { 0 };
1457 nanoz_status_t ret = nanoz_uncompress(src, src_size, dest_size, &tmpBuf.at(0), &uncomp_size);
1458 if (NANOZ_SUCCESS != ret) {
1461 if ((*uncompressed_size) != uncomp_size) {
1465 int ret =
uncompress(&tmpBuf.at(0), uncompressed_size, src, src_size);
1478 unsigned char* t = &tmpBuf.at(0) + 1;
1479 unsigned char* stop = &tmpBuf.at(0) + (*uncompressed_size);
1482 int d =
int(t[-1]) +
int(t[0]) - 128;
1483 t[0] =
static_cast<unsigned char>(d);
1490 const char* t1 =
reinterpret_cast<const char*
>(&tmpBuf.at(0));
1491 const char* t2 =
reinterpret_cast<const char*
>(&tmpBuf.at(0)) + (*uncompressed_size + 1) / 2;
1492 char* s =
reinterpret_cast<char*
>(dst);
1493 char* stop = s + (*uncompressed_size);
1514#pragma clang diagnostic push
1515#pragma clang diagnostic ignored "-Wsign-conversion"
1516#if __has_warning("-Wextra-semi-stmt")
1517#pragma clang diagnostic ignored "-Wextra-semi-stmt"
1522#pragma warning(push)
1523#pragma warning(disable : 4204)
1526#pragma warning(disable : 4244)
1528#pragma warning(disable : 4267)
1530#pragma warning(disable : 4996)
1535const int MIN_RUN_LENGTH = 3;
1536const int MAX_RUN_LENGTH = 127;
1543static int rleCompress(
int inLength,
const char in[],
signed char out[])
1545 const char* inEnd = in + inLength;
1546 const char* runStart = in;
1547 const char* runEnd = in + 1;
1548 signed char* outWrite = out;
1550 while (runStart < inEnd) {
1551 while (runEnd < inEnd && *runStart == *runEnd && runEnd - runStart - 1 < MAX_RUN_LENGTH) {
1555 if (runEnd - runStart >= MIN_RUN_LENGTH) {
1560 *outWrite++ =
static_cast<char>(runEnd - runStart) - 1;
1561 *outWrite++ = *(
reinterpret_cast<const signed char*
>(runStart));
1568 while (runEnd < inEnd && ((runEnd + 1 >= inEnd || *runEnd != *(runEnd + 1)) || (runEnd + 2 >= inEnd || *(runEnd + 1) != *(runEnd + 2))) && runEnd - runStart < MAX_RUN_LENGTH) {
1572 *outWrite++ =
static_cast<char>(runStart - runEnd);
1574 while (runStart < runEnd) {
1575 *outWrite++ = *(
reinterpret_cast<const signed char*
>(runStart++));
1582 return static_cast<int>(outWrite - out);
1591static int rleUncompress(
int inLength,
int maxLength,
const signed char in[],
1594 char* outStart = out;
1596 while (inLength > 0) {
1598 int count = -(
static_cast<int>(*in++));
1599 inLength -=
count + 1;
1602 if ((0 > (maxLength -=
count)) || (inLength < 0))
1605 memcpy(out, in,
count);
1612 if ((0 > (maxLength -=
count + 1)) || (inLength < 0))
1615 memset(out, *
reinterpret_cast<const char*
>(in),
count + 1);
1622 return static_cast<int>(out - outStart);
1626#pragma clang diagnostic pop
1631static bool CompressRle(
unsigned char* dst,
1632 tinyexr::tinyexr_uint64& compressedSize,
1633 const unsigned char* src,
unsigned long src_size)
1635 std::vector<unsigned char> tmpBuf(src_size);
1646 const char* srcPtr =
reinterpret_cast<const char*
>(src);
1649 char* t1 =
reinterpret_cast<char*
>(&tmpBuf.at(0));
1650 char* t2 =
reinterpret_cast<char*
>(&tmpBuf.at(0)) + (src_size + 1) / 2;
1651 const char* stop = srcPtr + src_size;
1655 *(t1++) = *(srcPtr++);
1660 *(t2++) = *(srcPtr++);
1671 unsigned char* t = &tmpBuf.at(0) + 1;
1672 unsigned char* stop = &tmpBuf.at(0) + src_size;
1676 int d =
int(t[0]) - p + (128 + 256);
1678 t[0] =
static_cast<unsigned char>(d);
1684 int outSize = rleCompress(
static_cast<int>(src_size),
1685 reinterpret_cast<const char*
>(&tmpBuf.at(0)),
1686 reinterpret_cast<signed char*
>(dst));
1687 TINYEXR_CHECK_AND_RETURN_C(outSize > 0,
false);
1689 compressedSize =
static_cast<tinyexr::tinyexr_uint64
>(outSize);
1693 if (compressedSize >= src_size) {
1694 compressedSize = src_size;
1695 memcpy(dst, src, src_size);
1701static bool DecompressRle(
unsigned char* dst,
1702 const unsigned long uncompressed_size,
1703 const unsigned char* src,
unsigned long src_size)
1705 if (uncompressed_size == src_size) {
1707 memcpy(dst, src, src_size);
1713 if (src_size <= 2) {
1717 std::vector<unsigned char> tmpBuf(uncompressed_size);
1719 int ret = rleUncompress(
static_cast<int>(src_size),
1720 static_cast<int>(uncompressed_size),
1721 reinterpret_cast<const signed char*
>(src),
1722 reinterpret_cast<char*
>(&tmpBuf.at(0)));
1723 if (ret !=
static_cast<int>(uncompressed_size)) {
1734 unsigned char* t = &tmpBuf.at(0) + 1;
1735 unsigned char* stop = &tmpBuf.at(0) + uncompressed_size;
1738 int d =
int(t[-1]) +
int(t[0]) - 128;
1739 t[0] =
static_cast<unsigned char>(d);
1746 const char* t1 =
reinterpret_cast<const char*
>(&tmpBuf.at(0));
1747 const char* t2 =
reinterpret_cast<const char*
>(&tmpBuf.at(0)) + (uncompressed_size + 1) / 2;
1748 char* s =
reinterpret_cast<char*
>(dst);
1749 char* stop = s + uncompressed_size;
1770#pragma clang diagnostic push
1771#pragma clang diagnostic ignored "-Wc++11-long-long"
1772#pragma clang diagnostic ignored "-Wold-style-cast"
1773#pragma clang diagnostic ignored "-Wpadded"
1774#pragma clang diagnostic ignored "-Wsign-conversion"
1775#pragma clang diagnostic ignored "-Wc++11-extensions"
1776#pragma clang diagnostic ignored "-Wconversion"
1777#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
1779#if __has_warning("-Wcast-qual")
1780#pragma clang diagnostic ignored "-Wcast-qual"
1783#if __has_warning("-Wextra-semi-stmt")
1784#pragma clang diagnostic ignored "-Wextra-semi-stmt"
1798struct PIZChannelData {
1799 unsigned short* start;
1800 unsigned short* end;
1824inline void wenc14(
unsigned short a,
unsigned short b,
unsigned short& l,
1827 short as =
static_cast<short>(a);
1828 short bs =
static_cast<short>(b);
1830 short ms = (as + bs) >> 1;
1833 l =
static_cast<unsigned short>(ms);
1834 h =
static_cast<unsigned short>(ds);
1837inline void wdec14(
unsigned short l,
unsigned short h,
unsigned short& a,
1840 short ls =
static_cast<short>(l);
1841 short hs =
static_cast<short>(h);
1844 int ai = ls + (hi & 1) + (hi >> 1);
1846 short as =
static_cast<short>(ai);
1847 short bs =
static_cast<short>(ai - hi);
1849 a =
static_cast<unsigned short>(as);
1850 b =
static_cast<unsigned short>(bs);
1859const int NBITS = 16;
1860const int A_OFFSET = 1 << (NBITS - 1);
1861const int M_OFFSET = 1 << (NBITS - 1);
1862const int MOD_MASK = (1 << NBITS) - 1;
1864inline void wenc16(
unsigned short a,
unsigned short b,
unsigned short& l,
1867 int ao = (a + A_OFFSET) & MOD_MASK;
1868 int m = ((ao + b) >> 1);
1872 m = (m + M_OFFSET) & MOD_MASK;
1876 l =
static_cast<unsigned short>(m);
1877 h =
static_cast<unsigned short>(d);
1880inline void wdec16(
unsigned short l,
unsigned short h,
unsigned short& a,
1885 int bb = (m - (d >> 1)) & MOD_MASK;
1886 int aa = (d + bb - A_OFFSET) & MOD_MASK;
1887 b =
static_cast<unsigned short>(bb);
1888 a =
static_cast<unsigned short>(aa);
1895static void wav2Encode(
1903 bool w14 = (mx < (1 << 14));
1904 int n = (nx > ny) ? ny : nx;
1913 unsigned short* py = in;
1914 unsigned short* ey = in + oy * (ny - p2);
1919 unsigned short i00, i01, i10, i11;
1925 for (; py <= ey; py += oy2) {
1926 unsigned short* px = py;
1927 unsigned short* ex = py + ox * (nx - p2);
1933 for (; px <= ex; px += ox2) {
1934 unsigned short* p01 = px + ox1;
1935 unsigned short* p10 = px + oy1;
1936 unsigned short* p11 = p10 + ox1;
1943 wenc14(*px, *p01, i00, i01);
1944 wenc14(*p10, *p11, i10, i11);
1945 wenc14(i00, i10, *px, *p10);
1946 wenc14(i01, i11, *p01, *p11);
1948 wenc16(*px, *p01, i00, i01);
1949 wenc16(*p10, *p11, i10, i11);
1950 wenc16(i00, i10, *px, *p10);
1951 wenc16(i01, i11, *p01, *p11);
1960 unsigned short* p10 = px + oy1;
1963 wenc14(*px, *p10, i00, *p10);
1965 wenc16(*px, *p10, i00, *p10);
1976 unsigned short* px = py;
1977 unsigned short* ex = py + ox * (nx - p2);
1979 for (; px <= ex; px += ox2) {
1980 unsigned short* p01 = px + ox1;
1983 wenc14(*px, *p01, i00, *p01);
1985 wenc16(*px, *p01, i00, *p01);
2004static void wav2Decode(
2012 bool w14 = (mx < (1 << 14));
2013 int n = (nx > ny) ? ny : nx;
2033 unsigned short* py = in;
2034 unsigned short* ey = in + oy * (ny - p2);
2039 unsigned short i00, i01, i10, i11;
2045 for (; py <= ey; py += oy2) {
2046 unsigned short* px = py;
2047 unsigned short* ex = py + ox * (nx - p2);
2053 for (; px <= ex; px += ox2) {
2054 unsigned short* p01 = px + ox1;
2055 unsigned short* p10 = px + oy1;
2056 unsigned short* p11 = p10 + ox1;
2063 wdec14(*px, *p10, i00, i10);
2064 wdec14(*p01, *p11, i01, i11);
2065 wdec14(i00, i01, *px, *p01);
2066 wdec14(i10, i11, *p10, *p11);
2068 wdec16(*px, *p10, i00, i10);
2069 wdec16(*p01, *p11, i01, i11);
2070 wdec16(i00, i01, *px, *p01);
2071 wdec16(i10, i11, *p10, *p11);
2080 unsigned short* p10 = px + oy1;
2083 wdec14(*px, *p10, i00, *p10);
2085 wdec16(*px, *p10, i00, *p10);
2096 unsigned short* px = py;
2097 unsigned short* ex = py + ox * (nx - p2);
2099 for (; px <= ex; px += ox2) {
2100 unsigned short* p01 = px + ox1;
2103 wdec14(*px, *p01, i00, *p01);
2105 wdec16(*px, *p01, i00, *p01);
2132const int HUF_ENCBITS = 16;
2133const int HUF_DECBITS = 14;
2135const int HUF_ENCSIZE = (1 << HUF_ENCBITS) + 1;
2136const int HUF_DECSIZE = 1 << HUF_DECBITS;
2137const int HUF_DECMASK = HUF_DECSIZE - 1;
2141 unsigned int len : 8;
2142 unsigned int lit : 24;
2146inline long long hufLength(
long long code) {
return code & 63; }
2148inline long long hufCode(
long long code) {
return code >> 6; }
2150inline void outputBits(
int nBits,
long long bits,
long long& c,
int& lc,
2159 *out++ =
static_cast<char>((c >> (lc -= 8)));
2162inline long long getBits(
int nBits,
long long& c,
int& lc,
const char*& in)
2164 while (lc < nBits) {
2165 c = (c << 8) | *(reinterpret_cast<const unsigned char*>(in++));
2170 return (c >> lc) & ((1 << nBits) - 1);
2193static void hufCanonicalCodeTable(
long long hcode[HUF_ENCSIZE])
2203 for (
int i = 0; i <= 58; ++i)
2206 for (
int i = 0; i < HUF_ENCSIZE; ++i)
2217 for (
int i = 58; i > 0; --i) {
2218 long long nc = ((c + n[i]) >> 1);
2230 for (
int i = 0; i < HUF_ENCSIZE; ++i) {
2231 int l =
static_cast<int>(hcode[i]);
2234 hcode[i] = l | (n[l]++ << 6);
2247struct FHeapCompare {
2248 bool operator()(
long long* a,
long long* b) {
return *a > *b; }
2251static bool hufBuildEncTable(
2277 std::vector<int> hlink(HUF_ENCSIZE);
2278 std::vector<long long*> fHeap(HUF_ENCSIZE);
2287 for (
int i = *im; i < HUF_ENCSIZE; i++) {
2291 fHeap[nf] = &frq[i];
2305 fHeap[nf] = &frq[*iM];
2336 std::make_heap(&fHeap[0], &fHeap[nf], FHeapCompare());
2338 std::vector<long long> scode(HUF_ENCSIZE);
2339 memset(scode.data(), 0,
sizeof(
long long) * HUF_ENCSIZE);
2348 int mm = fHeap[0] - frq;
2349 std::pop_heap(&fHeap[0], &fHeap[nf], FHeapCompare());
2352 int m = fHeap[0] - frq;
2353 std::pop_heap(&fHeap[0], &fHeap[nf], FHeapCompare());
2356 std::push_heap(&fHeap[0], &fHeap[nf], FHeapCompare());
2377 for (
int j = m;; j = hlink[j]) {
2380 TINYEXR_CHECK_AND_RETURN_C(scode[j] <= 58,
false);
2382 if (hlink[j] == j) {
2396 for (
int j = mm;; j = hlink[j]) {
2399 TINYEXR_CHECK_AND_RETURN_C(scode[j] <= 58,
false);
2412 hufCanonicalCodeTable(scode.data());
2413 memcpy(frq, scode.data(),
sizeof(
long long) * HUF_ENCSIZE);
2433const int SHORT_ZEROCODE_RUN = 59;
2434const int LONG_ZEROCODE_RUN = 63;
2435const int SHORTEST_LONG_RUN = 2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN;
2436const int LONGEST_LONG_RUN = 255 + SHORTEST_LONG_RUN;
2438static void hufPackEncTable(
2439 const long long* hcode,
2448 for (; im <= iM; im++) {
2449 int l = hufLength(hcode[im]);
2454 while ((im < iM) && (zerun < LONGEST_LONG_RUN)) {
2455 if (hufLength(hcode[im + 1]) > 0)
2462 if (zerun >= SHORTEST_LONG_RUN) {
2463 outputBits(6, LONG_ZEROCODE_RUN, c, lc, p);
2464 outputBits(8, zerun - SHORTEST_LONG_RUN, c, lc, p);
2466 outputBits(6, SHORT_ZEROCODE_RUN + zerun - 2, c, lc, p);
2472 outputBits(6, l, c, lc, p);
2476 *p++ = (
unsigned char)(c << (8 - lc));
2485static bool hufUnpackEncTable(
2492 memset(hcode, 0,
sizeof(
long long) * HUF_ENCSIZE);
2494 const char* p = *pcode;
2498 for (; im <= iM; im++) {
2499 if (p - *pcode >= ni) {
2503 long long l = hcode[im] = getBits(6, c, lc, p);
2505 if (l == (
long long)LONG_ZEROCODE_RUN) {
2506 if (p - *pcode > ni) {
2510 int zerun = getBits(8, c, lc, p) + SHORTEST_LONG_RUN;
2512 if (im + zerun > iM + 1) {
2520 }
else if (l >= (
long long)SHORT_ZEROCODE_RUN) {
2521 int zerun = l - SHORT_ZEROCODE_RUN + 2;
2523 if (im + zerun > iM + 1) {
2534 *pcode =
const_cast<char*
>(p);
2536 hufCanonicalCodeTable(hcode);
2549static void hufClearDecTable(HufDec* hdecod)
2552 for (
int i = 0; i < HUF_DECSIZE; i++) {
2568static bool hufBuildDecTable(
const long long* hcode,
2579 for (; im <= iM; im++) {
2580 long long c = hufCode(hcode[im]);
2581 int l = hufLength(hcode[im]);
2594 if (l > HUF_DECBITS) {
2599 HufDec* pl = hdecod + (c >> (l - HUF_DECBITS));
2614 unsigned int* p = pl->p;
2615 pl->p =
new unsigned int[pl->lit];
2617 for (
unsigned int i = 0; i < pl->lit - 1u; ++i)
2622 pl->p =
new unsigned int[1];
2625 pl->p[pl->lit - 1] = im;
2631 HufDec* pl = hdecod + (c << (HUF_DECBITS - l));
2633 for (
long long i = 1ULL << (HUF_DECBITS - l); i > 0; i--, pl++) {
2634 if (pl->len || pl->p) {
2657static void hufFreeDecTable(HufDec* hdecod)
2659 for (
int i = 0; i < HUF_DECSIZE; i++) {
2661 delete[] hdecod[i].p;
2671inline void outputCode(
long long code,
long long& c,
int& lc,
char*& out)
2673 outputBits(hufLength(code), hufCode(code), c, lc, out);
2676inline void sendCode(
long long sCode,
int runCount,
long long runCode,
2677 long long& c,
int& lc,
char*& out)
2686 if (hufLength(sCode) + hufLength(runCode) + 8 < hufLength(sCode) * runCount) {
2687 outputCode(sCode, c, lc, out);
2688 outputCode(runCode, c, lc, out);
2689 outputBits(8, runCount, c, lc, out);
2691 while (runCount-- >= 0)
2692 outputCode(sCode, c, lc, out);
2701 (
const long long* hcode,
2702 const unsigned short* in,
2707 char* outStart = out;
2717 for (
int i = 1; i < ni; i++) {
2722 if (s == in[i] && cs < 255) {
2725 sendCode(hcode[s], cs, hcode[rlc], c, lc, out);
2736 sendCode(hcode[s], cs, hcode[rlc], c, lc, out);
2739 *out = (c << (8 - lc)) & 0xff;
2741 return (out - outStart) * 8 + lc;
2754#define getChar(c, lc, in) \
2756 c = (c << 8) | *(unsigned char*)(in++); \
2761#define getCode(po, rlc, c, lc, in, out, ob, oe) \
2765 getChar(c, lc, in); \
2769 unsigned char cs = (c >> lc); \
2771 if (out + cs > oe) \
2775 unsigned short s = out[-1]; \
2779 } else if (out < oe) { \
2786static bool getCode(
int po,
int rlc,
long long& c,
int& lc,
const char*& in,
2787 const char* in_end,
unsigned short*& out,
2788 const unsigned short* ob,
const unsigned short* oe)
2804 unsigned char cs = (c >> lc);
2813 unsigned short s = out[-1];
2817 }
else if (out < oe) {
2830static bool hufDecode(
const long long* hcode,
2831 const HufDec* hdecod,
2836 unsigned short* out)
2840 unsigned short* outb = out;
2841 unsigned short* oe = out + no;
2842 const char* ie = in + (ni + 7) / 8;
2855 while (lc >= HUF_DECBITS) {
2856 const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
2871 if (!getCode(pl.lit, rlc, c, lc, in, ie, out, outb, oe)) {
2886 for (j = 0; j < pl.lit; j++) {
2887 int l = hufLength(hcode[pl.p[j]]);
2889 while (lc < l && in < ie)
2893 if (hufCode(hcode[pl.p[j]]) == ((c >> (lc - l)) & (((
long long)(1) << l) - 1))) {
2899 if (!getCode(pl.p[j], rlc, c, lc, in, ie, out, outb, oe)) {
2919 int i = (8 - ni) & 7;
2924 const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
2928 if (!getCode(pl.lit, rlc, c, lc, in, ie, out, outb, oe)) {
2937 if (out - outb != no) {
2945static void countFrequencies(std::vector<long long>& freq,
2946 const unsigned short data[],
int n)
2948 for (
int i = 0; i < HUF_ENCSIZE; ++i)
2951 for (
int i = 0; i < n; ++i)
2955static void writeUInt(
char buf[4],
unsigned int i)
2957 unsigned char* b = (
unsigned char*)
buf;
2965static unsigned int readUInt(
const char buf[4])
2967 const unsigned char* b = (
const unsigned char*)
buf;
2969 return (b[0] & 0x000000ff) | ((b[1] << 8) & 0x0000ff00) | ((b[2] << 16) & 0x00ff0000) | ((b[3] << 24) & 0xff000000);
2976static int hufCompress(
const unsigned short raw[],
int nRaw,
2982 std::vector<long long> freq(HUF_ENCSIZE);
2984 countFrequencies(freq, raw, nRaw);
2988 hufBuildEncTable(freq.data(), &im, &iM);
2990 char* tableStart = compressed + 20;
2991 char* tableEnd = tableStart;
2992 hufPackEncTable(freq.data(), im, iM, &tableEnd);
2993 int tableLength = tableEnd - tableStart;
2995 char* dataStart = tableEnd;
2996 int nBits = hufEncode(freq.data(), raw, nRaw, iM, dataStart);
2997 int data_length = (nBits + 7) / 8;
2999 writeUInt(compressed, im);
3000 writeUInt(compressed + 4, iM);
3001 writeUInt(compressed + 8, tableLength);
3002 writeUInt(compressed + 12, nBits);
3003 writeUInt(compressed + 16, 0);
3005 return dataStart + data_length - compressed;
3008static bool hufUncompress(
const char compressed[],
int nCompressed,
3009 std::vector<unsigned short>* raw)
3011 if (nCompressed == 0) {
3012 if (raw->size() != 0)
3018 int im = readUInt(compressed);
3019 int iM = readUInt(compressed + 4);
3021 int nBits = readUInt(compressed + 12);
3023 if (im < 0 || im >= HUF_ENCSIZE || iM < 0 || iM >= HUF_ENCSIZE)
3026 const char* ptr = compressed + 20;
3041 std::vector<long long> freq(HUF_ENCSIZE);
3042 std::vector<HufDec> hdec(HUF_DECSIZE);
3044 hufClearDecTable(&hdec.at(0));
3046 hufUnpackEncTable(&ptr, nCompressed - (ptr - compressed), im, iM,
3050 if (nBits > 8 * (nCompressed - (ptr - compressed))) {
3054 hufBuildDecTable(&freq.at(0), im, iM, &hdec.at(0));
3055 hufDecode(&freq.at(0), &hdec.at(0), ptr, nBits, iM, raw->size(),
3064 hufFreeDecTable(&hdec.at(0));
3074const int USHORT_RANGE = (1 << 16);
3075const int BITMAP_SIZE = (USHORT_RANGE >> 3);
3077static void bitmapFromData(
const unsigned short data[],
int nData,
3078 unsigned char bitmap[BITMAP_SIZE],
3079 unsigned short& minNonZero,
3080 unsigned short& maxNonZero)
3082 for (
int i = 0; i < BITMAP_SIZE; ++i)
3085 for (
int i = 0; i < nData; ++i)
3086 bitmap[data[i] >> 3] |= (1 << (data[i] & 7));
3091 minNonZero = BITMAP_SIZE - 1;
3094 for (
int i = 0; i < BITMAP_SIZE; ++i) {
3104static unsigned short forwardLutFromBitmap(
3105 const unsigned char bitmap[BITMAP_SIZE],
unsigned short lut[USHORT_RANGE])
3109 for (
int i = 0; i < USHORT_RANGE; ++i) {
3110 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
3119static unsigned short reverseLutFromBitmap(
3120 const unsigned char bitmap[BITMAP_SIZE],
unsigned short lut[USHORT_RANGE])
3124 for (
int i = 0; i < USHORT_RANGE; ++i) {
3125 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
3131 while (k < USHORT_RANGE)
3137static void applyLut(
const unsigned short lut[USHORT_RANGE],
3138 unsigned short data[],
int nData)
3140 for (
int i = 0; i < nData; ++i)
3141 data[i] = lut[data[i]];
3145#pragma clang diagnostic pop
3152static bool CompressPiz(
unsigned char* outPtr,
unsigned int* outSize,
3153 const unsigned char* inPtr,
size_t inSize,
3154 const std::vector<ChannelInfo>& channelInfo,
3155 int data_width,
int num_lines)
3157 std::vector<unsigned char> bitmap(BITMAP_SIZE);
3158 unsigned short minNonZero;
3159 unsigned short maxNonZero;
3161#if !TINYEXR_LITTLE_ENDIAN
3167 std::vector<unsigned short> tmpBuffer(inSize /
sizeof(
unsigned short));
3169 std::vector<PIZChannelData> channelData(channelInfo.size());
3170 unsigned short* tmpBufferEnd = &tmpBuffer.at(0);
3172 for (
size_t c = 0; c < channelData.size(); c++) {
3173 PIZChannelData& cd = channelData[c];
3175 cd.start = tmpBufferEnd;
3182 size_t pixelSize =
sizeof(
int);
3184 pixelSize =
sizeof(short);
3187 cd.size =
static_cast<int>(pixelSize /
sizeof(short));
3189 tmpBufferEnd += cd.nx * cd.ny * cd.size;
3192 const unsigned char* ptr = inPtr;
3193 for (
int y = 0;
y < num_lines; ++
y) {
3194 for (
size_t i = 0; i < channelData.size(); ++i) {
3195 PIZChannelData& cd = channelData[i];
3200 size_t n =
static_cast<size_t>(cd.nx * cd.size);
3201 memcpy(cd.end, ptr, n *
sizeof(
unsigned short));
3202 ptr += n *
sizeof(
unsigned short);
3207 bitmapFromData(&tmpBuffer.at(0),
static_cast<int>(tmpBuffer.size()),
3208 bitmap.data(), minNonZero, maxNonZero);
3210 std::vector<unsigned short> lut(USHORT_RANGE);
3211 unsigned short maxValue = forwardLutFromBitmap(bitmap.data(), lut.data());
3212 applyLut(lut.data(), &tmpBuffer.at(0),
static_cast<int>(tmpBuffer.size()));
3218 char*
buf =
reinterpret_cast<char*
>(outPtr);
3220 memcpy(
buf, &minNonZero,
sizeof(
unsigned short));
3221 buf +=
sizeof(
unsigned short);
3222 memcpy(
buf, &maxNonZero,
sizeof(
unsigned short));
3223 buf +=
sizeof(
unsigned short);
3225 if (minNonZero <= maxNonZero) {
3226 memcpy(
buf,
reinterpret_cast<char*
>(&bitmap[0] + minNonZero),
3227 maxNonZero - minNonZero + 1);
3228 buf += maxNonZero - minNonZero + 1;
3235 for (
size_t i = 0; i < channelData.size(); ++i) {
3236 PIZChannelData& cd = channelData[i];
3238 for (
int j = 0; j < cd.size; ++j) {
3239 wav2Encode(cd.start + j, cd.nx, cd.size, cd.ny, cd.nx * cd.size,
3250 char* lengthPtr =
buf;
3252 memcpy(
buf, &zero,
sizeof(
int));
3255 int length = hufCompress(&tmpBuffer.at(0),
static_cast<int>(tmpBuffer.size()),
buf);
3256 memcpy(lengthPtr, &length,
sizeof(
int));
3258 (*outSize) =
static_cast<unsigned int>(
3259 (
reinterpret_cast<unsigned char*
>(
buf) - outPtr) +
static_cast<unsigned int>(length));
3263 if ((*outSize) >= inSize) {
3264 (*outSize) =
static_cast<unsigned int>(inSize);
3265 memcpy(outPtr, inPtr, inSize);
3270static bool DecompressPiz(
unsigned char* outPtr,
const unsigned char* inPtr,
3271 size_t tmpBufSizeInBytes,
size_t inLen,
int num_channels,
3275 if (inLen == tmpBufSizeInBytes) {
3277 memcpy(outPtr, inPtr, inLen);
3281 std::vector<unsigned char> bitmap(BITMAP_SIZE);
3282 unsigned short minNonZero;
3283 unsigned short maxNonZero;
3285#if !TINYEXR_LITTLE_ENDIAN
3290 memset(bitmap.data(), 0, BITMAP_SIZE);
3298 const unsigned char* ptr = inPtr;
3300 tinyexr::cpy2(&minNonZero,
reinterpret_cast<const unsigned short*
>(ptr));
3302 tinyexr::cpy2(&maxNonZero,
reinterpret_cast<const unsigned short*
>(ptr + 2));
3306 if (maxNonZero >= BITMAP_SIZE) {
3315 if (minNonZero <= maxNonZero) {
3316 if (((maxNonZero - minNonZero + 1) + readLen) > inLen) {
3321 memcpy(
reinterpret_cast<char*
>(&bitmap[0] + minNonZero), ptr,
3322 maxNonZero - minNonZero + 1);
3323 ptr += maxNonZero - minNonZero + 1;
3324 readLen += maxNonZero - minNonZero + 1;
3327 if ((minNonZero == (BITMAP_SIZE - 1)) && (maxNonZero == 0)) {
3335 std::vector<unsigned short> lut(USHORT_RANGE);
3336 memset(lut.data(), 0,
sizeof(
unsigned short) * USHORT_RANGE);
3337 unsigned short maxValue = reverseLutFromBitmap(bitmap.data(), lut.data());
3343 if ((readLen + 4) > inLen) {
3350 tinyexr::cpy4(&length,
reinterpret_cast<const int*
>(ptr));
3353 if (
size_t((ptr - inPtr) + length) > inLen) {
3357 std::vector<unsigned short> tmpBuffer(tmpBufSizeInBytes /
sizeof(
unsigned short));
3358 hufUncompress(
reinterpret_cast<const char*
>(ptr), length, &tmpBuffer);
3364 std::vector<PIZChannelData> channelData(
static_cast<size_t>(num_channels));
3366 unsigned short* tmpBufferEnd = &tmpBuffer.at(0);
3368 for (
size_t i = 0; i < static_cast<size_t>(num_channels); ++i) {
3371 size_t pixelSize =
sizeof(
int);
3373 pixelSize =
sizeof(short);
3376 channelData[i].start = tmpBufferEnd;
3377 channelData[i].end = channelData[i].start;
3378 channelData[i].nx = data_width;
3379 channelData[i].ny = num_lines;
3381 channelData[i].size =
static_cast<int>(pixelSize /
sizeof(short));
3383 tmpBufferEnd += channelData[i].nx * channelData[i].ny * channelData[i].size;
3386 for (
size_t i = 0; i < channelData.size(); ++i) {
3387 PIZChannelData& cd = channelData[i];
3389 for (
int j = 0; j < cd.size; ++j) {
3390 wav2Decode(cd.start + j, cd.nx, cd.size, cd.ny, cd.nx * cd.size,
3399 applyLut(lut.data(), &tmpBuffer.at(0),
static_cast<int>(tmpBufSizeInBytes /
sizeof(
unsigned short)));
3401 for (
int y = 0;
y < num_lines;
y++) {
3402 for (
size_t i = 0; i < channelData.size(); ++i) {
3403 PIZChannelData& cd = channelData[i];
3408 size_t n =
static_cast<size_t>(cd.nx * cd.size);
3409 memcpy(outPtr, cd.end,
static_cast<size_t>(n *
sizeof(
unsigned short)));
3410 outPtr += n *
sizeof(
unsigned short);
3421struct ZFPCompressionParam {
3423 unsigned int precision;
3424 unsigned int __pad0;
3427 unsigned int __pad1;
3429 ZFPCompressionParam()
3438static bool FindZFPCompressionParam(ZFPCompressionParam* param,
3440 int num_attributes, std::string* err)
3442 bool foundType =
false;
3444 for (
int i = 0; i < num_attributes; i++) {
3445 if ((strcmp(attributes[i].name,
"zfpCompressionType") == 0)) {
3446 if (attributes[i].
size == 1) {
3447 param->type =
static_cast<int>(attributes[i].
value[0]);
3452 (*err) +=
"zfpCompressionType attribute must be uchar(1 byte) type.\n";
3461 (*err) +=
"`zfpCompressionType` attribute not found.\n";
3467 for (
int i = 0; i < num_attributes; i++) {
3468 if ((strcmp(attributes[i].name,
"zfpCompressionRate") == 0) && (attributes[i].
size == 8)) {
3469 param->rate = *(
reinterpret_cast<double*
>(attributes[i].
value));
3475 (*err) +=
"`zfpCompressionRate` attribute not found.\n";
3479 for (
int i = 0; i < num_attributes; i++) {
3480 if ((strcmp(attributes[i].name,
"zfpCompressionPrecision") == 0) && (attributes[i].
size == 4)) {
3481 param->rate = *(
reinterpret_cast<int*
>(attributes[i].
value));
3487 (*err) +=
"`zfpCompressionPrecision` attribute not found.\n";
3491 for (
int i = 0; i < num_attributes; i++) {
3492 if ((strcmp(attributes[i].name,
"zfpCompressionTolerance") == 0) && (attributes[i].
size == 8)) {
3493 param->tolerance = *(
reinterpret_cast<double*
>(attributes[i].
value));
3499 (*err) +=
"`zfpCompressionTolerance` attribute not found.\n";
3503 (*err) +=
"Unknown value specified for `zfpCompressionType`.\n";
3511static bool DecompressZfp(
float* dst,
int dst_width,
int dst_num_lines,
3512 size_t num_channels,
const unsigned char* src,
3513 unsigned long src_size,
3514 const ZFPCompressionParam& param)
3516 size_t uncompressed_size = size_t(dst_width) * size_t(dst_num_lines) * num_channels;
3518 if (uncompressed_size == src_size) {
3520 memcpy(dst, src, src_size);
3523 zfp_stream* zfp = NULL;
3524 zfp_field* field = NULL;
3526 TINYEXR_CHECK_AND_RETURN_C((dst_width % 4) == 0,
false);
3527 TINYEXR_CHECK_AND_RETURN_C((dst_num_lines % 4) == 0,
false);
3529 if ((
size_t(dst_width) & 3U) || (
size_t(dst_num_lines) & 3U)) {
3533 field = zfp_field_2d(
reinterpret_cast<void*
>(
const_cast<unsigned char*
>(src)),
3534 zfp_type_float,
static_cast<unsigned int>(dst_width),
3535 static_cast<unsigned int>(dst_num_lines) *
static_cast<unsigned int>(num_channels));
3536 zfp = zfp_stream_open(NULL);
3539 zfp_stream_set_rate(zfp, param.rate, zfp_type_float, 2,
3542 zfp_stream_set_precision(zfp, param.precision);
3544 zfp_stream_set_accuracy(zfp, param.tolerance);
3549 size_t buf_size = zfp_stream_maximum_size(zfp, field);
3550 std::vector<unsigned char>
buf(buf_size);
3551 memcpy(&
buf.at(0), src, src_size);
3553 bitstream*
stream = stream_open(&
buf.at(0), buf_size);
3554 zfp_stream_set_bit_stream(zfp,
stream);
3555 zfp_stream_rewind(zfp);
3557 size_t image_size = size_t(dst_width) * size_t(dst_num_lines);
3559 for (
size_t c = 0; c < size_t(num_channels); c++) {
3561 for (
size_t y = 0;
y < size_t(dst_num_lines);
y += 4) {
3562 for (
size_t x = 0;
x < size_t(dst_width);
x += 4) {
3564 zfp_decode_block_float_2(zfp, fblock);
3565 for (
size_t j = 0; j < 4; j++) {
3566 for (
size_t i = 0; i < 4; i++) {
3567 dst[c * image_size + ((
y + j) * size_t(dst_width) + (
x + i))] = fblock[j * 4 + i];
3574 zfp_field_free(field);
3575 zfp_stream_close(zfp);
3582static bool CompressZfp(std::vector<unsigned char>* outBuf,
3583 unsigned int* outSize,
const float* inPtr,
int width,
3584 int num_lines,
int num_channels,
3585 const ZFPCompressionParam& param)
3587 zfp_stream* zfp = NULL;
3588 zfp_field* field = NULL;
3590 TINYEXR_CHECK_AND_RETURN_C((width % 4) == 0,
false);
3591 TINYEXR_CHECK_AND_RETURN_C((num_lines % 4) == 0,
false);
3593 if ((
size_t(width) & 3U) || (
size_t(num_lines) & 3U)) {
3598 field = zfp_field_2d(
reinterpret_cast<void*
>(
const_cast<float*
>(inPtr)),
3599 zfp_type_float,
static_cast<unsigned int>(width),
3600 static_cast<unsigned int>(num_lines * num_channels));
3602 zfp = zfp_stream_open(NULL);
3605 zfp_stream_set_rate(zfp, param.rate, zfp_type_float, 2, 0);
3607 zfp_stream_set_precision(zfp, param.precision);
3609 zfp_stream_set_accuracy(zfp, param.tolerance);
3614 size_t buf_size = zfp_stream_maximum_size(zfp, field);
3616 outBuf->resize(buf_size);
3618 bitstream*
stream = stream_open(&outBuf->at(0), buf_size);
3619 zfp_stream_set_bit_stream(zfp,
stream);
3620 zfp_field_free(field);
3622 size_t image_size = size_t(width) * size_t(num_lines);
3624 for (
size_t c = 0; c < size_t(num_channels); c++) {
3626 for (
size_t y = 0;
y < size_t(num_lines);
y += 4) {
3627 for (
size_t x = 0;
x < size_t(width);
x += 4) {
3629 for (
size_t j = 0; j < 4; j++) {
3630 for (
size_t i = 0; i < 4; i++) {
3631 fblock[j * 4 + i] = inPtr[c * image_size + ((
y + j) * size_t(width) + (
x + i))];
3634 zfp_encode_block_float_2(zfp, fblock);
3639 zfp_stream_flush(zfp);
3640 (*outSize) =
static_cast<unsigned int>(zfp_stream_compressed_size(zfp));
3642 zfp_stream_close(zfp);
3654#define TINYEXR_DIMENSION_THRESHOLD (1024 * 8192)
3657static bool DecodePixelData(
unsigned char** out_images,
3658 const int* requested_pixel_types,
3659 const unsigned char* data_ptr,
size_t data_len,
3660 int compression_type,
int line_order,
int width,
3661 int height,
int x_stride,
int y,
int line_no,
3662 int num_lines,
size_t pixel_data_size,
3663 size_t num_attributes,
3666 const std::vector<size_t>& channel_offset_list)
3670 if ((width == 0) || (num_lines == 0) || (pixel_data_size == 0)) {
3676 std::vector<unsigned char> outBuf(
static_cast<size_t>(
3677 static_cast<size_t>(width * num_lines) * pixel_data_size));
3678 size_t tmpBufLen = outBuf.size();
3680 bool ret = tinyexr::DecompressPiz(
3681 reinterpret_cast<unsigned char*
>(&outBuf.at(0)), data_ptr, tmpBufLen,
3682 data_len,
static_cast<int>(num_channels), channels, width, num_lines);
3698 for (
size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
3700 for (
size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
3701 const unsigned short* line_ptr =
reinterpret_cast<unsigned short*
>(
3702 &outBuf.at(v * pixel_data_size *
static_cast<size_t>(width) + channel_offset_list[c] *
static_cast<size_t>(width)));
3703 for (
size_t u = 0; u < static_cast<size_t>(width); u++) {
3709 tinyexr::cpy2(&(hf.u), line_ptr + u);
3711 tinyexr::swap2(
reinterpret_cast<unsigned short*
>(&hf.u));
3714 unsigned short* image =
reinterpret_cast<unsigned short**
>(out_images)[c];
3715 if (line_order == 0) {
3716 image += (
static_cast<size_t>(line_no) + v) *
static_cast<size_t>(x_stride) + u;
3718 image +=
static_cast<size_t>(
3719 (height - 1 - (line_no +
static_cast<int>(v))))
3720 *
static_cast<size_t>(x_stride)
3725 FP32 f32 = half_to_float(hf);
3726 float* image =
reinterpret_cast<float**
>(out_images)[c];
3728 if (line_order == 0) {
3729 offset = (
static_cast<size_t>(line_no) + v) *
static_cast<size_t>(x_stride) + u;
3731 offset =
static_cast<size_t>(
3732 (height - 1 - (line_no +
static_cast<int>(v))))
3733 *
static_cast<size_t>(x_stride)
3744 for (
size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
3745 const unsigned int* line_ptr =
reinterpret_cast<unsigned int*
>(
3746 &outBuf.at(v * pixel_data_size *
static_cast<size_t>(width) + channel_offset_list[c] *
static_cast<size_t>(width)));
3747 for (
size_t u = 0; u < static_cast<size_t>(width); u++) {
3750 tinyexr::cpy4(&val, line_ptr + u);
3752 tinyexr::swap4(&val);
3754 unsigned int* image =
reinterpret_cast<unsigned int**
>(out_images)[c];
3755 if (line_order == 0) {
3756 image += (
static_cast<size_t>(line_no) + v) *
static_cast<size_t>(x_stride) + u;
3758 image +=
static_cast<size_t>(
3759 (height - 1 - (line_no +
static_cast<int>(v))))
3760 *
static_cast<size_t>(x_stride)
3768 for (
size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
3769 const float* line_ptr =
reinterpret_cast<float*
>(&outBuf.at(
3770 v * pixel_data_size *
static_cast<size_t>(width) + channel_offset_list[c] *
static_cast<size_t>(width)));
3771 for (
size_t u = 0; u < static_cast<size_t>(width); u++) {
3774 tinyexr::cpy4(&val, line_ptr + u);
3776 tinyexr::swap4(
reinterpret_cast<unsigned int*
>(&val));
3778 float* image =
reinterpret_cast<float**
>(out_images)[c];
3779 if (line_order == 0) {
3780 image += (
static_cast<size_t>(line_no) + v) *
static_cast<size_t>(x_stride) + u;
3782 image +=
static_cast<size_t>(
3783 (height - 1 - (line_no +
static_cast<int>(v))))
3784 *
static_cast<size_t>(x_stride)
3800 std::vector<unsigned char> outBuf(
static_cast<size_t>(width) *
static_cast<size_t>(num_lines) * pixel_data_size);
3802 unsigned long dstLen =
static_cast<unsigned long>(outBuf.size());
3803 TINYEXR_CHECK_AND_RETURN_C(dstLen > 0,
false);
3804 if (!tinyexr::DecompressZip(
3805 reinterpret_cast<unsigned char*
>(&outBuf.at(0)), &dstLen, data_ptr,
3806 static_cast<unsigned long>(data_len))) {
3820 for (
size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
3822 for (
size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
3823 const unsigned short* line_ptr =
reinterpret_cast<unsigned short*
>(
3824 &outBuf.at(v *
static_cast<size_t>(pixel_data_size) *
static_cast<size_t>(width) + channel_offset_list[c] *
static_cast<size_t>(width)));
3825 for (
size_t u = 0; u < static_cast<size_t>(width); u++) {
3829 tinyexr::cpy2(&(hf.u), line_ptr + u);
3831 tinyexr::swap2(
reinterpret_cast<unsigned short*
>(&hf.u));
3834 unsigned short* image =
reinterpret_cast<unsigned short**
>(out_images)[c];
3835 if (line_order == 0) {
3836 image += (
static_cast<size_t>(line_no) + v) *
static_cast<size_t>(x_stride) + u;
3838 image += (
static_cast<size_t>(height) - 1U - (
static_cast<size_t>(line_no) + v)) *
static_cast<size_t>(x_stride) + u;
3842 tinyexr::FP32 f32 = half_to_float(hf);
3843 float* image =
reinterpret_cast<float**
>(out_images)[c];
3845 if (line_order == 0) {
3846 offset = (
static_cast<size_t>(line_no) + v) *
static_cast<size_t>(x_stride) + u;
3848 offset = (
static_cast<size_t>(height) - 1U - (
static_cast<size_t>(line_no) + v)) *
static_cast<size_t>(x_stride) + u;
3859 for (
size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
3860 const unsigned int* line_ptr =
reinterpret_cast<unsigned int*
>(
3861 &outBuf.at(v * pixel_data_size *
static_cast<size_t>(width) + channel_offset_list[c] *
static_cast<size_t>(width)));
3862 for (
size_t u = 0; u < static_cast<size_t>(width); u++) {
3865 tinyexr::cpy4(&val, line_ptr + u);
3867 tinyexr::swap4(&val);
3869 unsigned int* image =
reinterpret_cast<unsigned int**
>(out_images)[c];
3870 if (line_order == 0) {
3871 image += (
static_cast<size_t>(line_no) + v) *
static_cast<size_t>(x_stride) + u;
3873 image += (
static_cast<size_t>(height) - 1U - (
static_cast<size_t>(line_no) + v)) *
static_cast<size_t>(x_stride) + u;
3880 for (
size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
3881 const float* line_ptr =
reinterpret_cast<float*
>(
3882 &outBuf.at(v * pixel_data_size *
static_cast<size_t>(width) + channel_offset_list[c] *
static_cast<size_t>(width)));
3883 for (
size_t u = 0; u < static_cast<size_t>(width); u++) {
3886 tinyexr::cpy4(&val, line_ptr + u);
3888 tinyexr::swap4(
reinterpret_cast<unsigned int*
>(&val));
3890 float* image =
reinterpret_cast<float**
>(out_images)[c];
3891 if (line_order == 0) {
3892 image += (
static_cast<size_t>(line_no) + v) *
static_cast<size_t>(x_stride) + u;
3894 image += (
static_cast<size_t>(height) - 1U - (
static_cast<size_t>(line_no) + v)) *
static_cast<size_t>(x_stride) + u;
3905 std::vector<unsigned char> outBuf(
static_cast<size_t>(width) *
static_cast<size_t>(num_lines) * pixel_data_size);
3907 unsigned long dstLen =
static_cast<unsigned long>(outBuf.size());
3912 if (!tinyexr::DecompressRle(
3913 reinterpret_cast<unsigned char*
>(&outBuf.at(0)), dstLen, data_ptr,
3914 static_cast<unsigned long>(data_len))) {
3928 for (
size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
3930 for (
size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
3931 const unsigned short* line_ptr =
reinterpret_cast<unsigned short*
>(
3932 &outBuf.at(v *
static_cast<size_t>(pixel_data_size) *
static_cast<size_t>(width) + channel_offset_list[c] *
static_cast<size_t>(width)));
3933 for (
size_t u = 0; u < static_cast<size_t>(width); u++) {
3937 tinyexr::cpy2(&(hf.u), line_ptr + u);
3939 tinyexr::swap2(
reinterpret_cast<unsigned short*
>(&hf.u));
3942 unsigned short* image =
reinterpret_cast<unsigned short**
>(out_images)[c];
3943 if (line_order == 0) {
3944 image += (
static_cast<size_t>(line_no) + v) *
static_cast<size_t>(x_stride) + u;
3946 image += (
static_cast<size_t>(height) - 1U - (
static_cast<size_t>(line_no) + v)) *
static_cast<size_t>(x_stride) + u;
3950 tinyexr::FP32 f32 = half_to_float(hf);
3951 float* image =
reinterpret_cast<float**
>(out_images)[c];
3952 if (line_order == 0) {
3953 image += (
static_cast<size_t>(line_no) + v) *
static_cast<size_t>(x_stride) + u;
3955 image += (
static_cast<size_t>(height) - 1U - (
static_cast<size_t>(line_no) + v)) *
static_cast<size_t>(x_stride) + u;
3964 for (
size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
3965 const unsigned int* line_ptr =
reinterpret_cast<unsigned int*
>(
3966 &outBuf.at(v * pixel_data_size *
static_cast<size_t>(width) + channel_offset_list[c] *
static_cast<size_t>(width)));
3967 for (
size_t u = 0; u < static_cast<size_t>(width); u++) {
3970 tinyexr::cpy4(&val, line_ptr + u);
3972 tinyexr::swap4(&val);
3974 unsigned int* image =
reinterpret_cast<unsigned int**
>(out_images)[c];
3975 if (line_order == 0) {
3976 image += (
static_cast<size_t>(line_no) + v) *
static_cast<size_t>(x_stride) + u;
3978 image += (
static_cast<size_t>(height) - 1U - (
static_cast<size_t>(line_no) + v)) *
static_cast<size_t>(x_stride) + u;
3985 for (
size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
3986 const float* line_ptr =
reinterpret_cast<float*
>(
3987 &outBuf.at(v * pixel_data_size *
static_cast<size_t>(width) + channel_offset_list[c] *
static_cast<size_t>(width)));
3988 for (
size_t u = 0; u < static_cast<size_t>(width); u++) {
3991 tinyexr::cpy4(&val, line_ptr + u);
3993 tinyexr::swap4(
reinterpret_cast<unsigned int*
>(&val));
3995 float* image =
reinterpret_cast<float**
>(out_images)[c];
3996 if (line_order == 0) {
3997 image += (
static_cast<size_t>(line_no) + v) *
static_cast<size_t>(x_stride) + u;
3999 image += (
static_cast<size_t>(height) - 1U - (
static_cast<size_t>(line_no) + v)) *
static_cast<size_t>(x_stride) + u;
4010 tinyexr::ZFPCompressionParam zfp_compression_param;
4012 if (!tinyexr::FindZFPCompressionParam(&zfp_compression_param, attributes,
4013 int(num_attributes), &e)) {
4019 std::vector<unsigned char> outBuf(
static_cast<size_t>(width) *
static_cast<size_t>(num_lines) * pixel_data_size);
4021 unsigned long dstLen = outBuf.size();
4022 TINYEXR_CHECK_AND_RETURN_C(dstLen > 0,
false);
4023 tinyexr::DecompressZfp(
reinterpret_cast<float*
>(&outBuf.at(0)), width,
4024 num_lines, num_channels, data_ptr,
4025 static_cast<unsigned long>(data_len),
4026 zfp_compression_param);
4038 for (
size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
4042 for (
size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
4043 const float* line_ptr =
reinterpret_cast<float*
>(
4044 &outBuf.at(v * pixel_data_size *
static_cast<size_t>(width) + channel_offset_list[c] *
static_cast<size_t>(width)));
4045 for (
size_t u = 0; u < static_cast<size_t>(width); u++) {
4047 tinyexr::cpy4(&val, line_ptr + u);
4049 tinyexr::swap4(
reinterpret_cast<unsigned int*
>(&val));
4051 float* image =
reinterpret_cast<float**
>(out_images)[c];
4052 if (line_order == 0) {
4053 image += (
static_cast<size_t>(line_no) + v) *
static_cast<size_t>(x_stride) + u;
4055 image += (
static_cast<size_t>(height) - 1U - (
static_cast<size_t>(line_no) + v)) *
static_cast<size_t>(x_stride) + u;
4066 (void)num_attributes;
4071 for (
size_t c = 0; c < num_channels; c++) {
4072 for (
size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
4074 const unsigned short* line_ptr =
reinterpret_cast<const unsigned short*
>(
4075 data_ptr + v * pixel_data_size * size_t(width) + channel_offset_list[c] *
static_cast<size_t>(width));
4078 unsigned short* outLine =
reinterpret_cast<unsigned short*
>(out_images[c]);
4079 if (line_order == 0) {
4080 outLine += (size_t(
y) + v) *
size_t(x_stride);
4082 outLine += (size_t(height) - 1 - (size_t(
y) + v)) * size_t(x_stride);
4085 for (
int u = 0; u < width; u++) {
4089 tinyexr::cpy2(&(hf.u), line_ptr + u);
4091 tinyexr::swap2(
reinterpret_cast<unsigned short*
>(&hf.u));
4096 float* outLine =
reinterpret_cast<float*
>(out_images[c]);
4097 if (line_order == 0) {
4098 outLine += (size_t(
y) + v) *
size_t(x_stride);
4100 outLine += (size_t(height) - 1 - (size_t(
y) + v)) * size_t(x_stride);
4103 if (
reinterpret_cast<const unsigned char*
>(line_ptr + width) > (data_ptr + data_len)) {
4108 for (
int u = 0; u < width; u++) {
4113 tinyexr::cpy2(&(hf.u), line_ptr + u);
4115 tinyexr::swap2(
reinterpret_cast<unsigned short*
>(&hf.u));
4117 tinyexr::FP32 f32 = half_to_float(hf);
4125 const float* line_ptr =
reinterpret_cast<const float*
>(
4126 data_ptr + v * pixel_data_size * size_t(width) + channel_offset_list[c] *
static_cast<size_t>(width));
4128 float* outLine =
reinterpret_cast<float*
>(out_images[c]);
4129 if (line_order == 0) {
4130 outLine += (size_t(
y) + v) *
size_t(x_stride);
4132 outLine += (size_t(height) - 1 - (size_t(
y) + v)) * size_t(x_stride);
4135 if (
reinterpret_cast<const unsigned char*
>(line_ptr + width) > (data_ptr + data_len)) {
4140 for (
int u = 0; u < width; u++) {
4142 tinyexr::cpy4(&val, line_ptr + u);
4144 tinyexr::swap4(
reinterpret_cast<unsigned int*
>(&val));
4149 const unsigned int* line_ptr =
reinterpret_cast<const unsigned int*
>(
4150 data_ptr + v * pixel_data_size * size_t(width) + channel_offset_list[c] *
static_cast<size_t>(width));
4152 unsigned int* outLine =
reinterpret_cast<unsigned int*
>(out_images[c]);
4153 if (line_order == 0) {
4154 outLine += (size_t(
y) + v) *
size_t(x_stride);
4156 outLine += (size_t(height) - 1 - (size_t(
y) + v)) * size_t(x_stride);
4159 if (
reinterpret_cast<const unsigned char*
>(line_ptr + width) > (data_ptr + data_len)) {
4164 for (
int u = 0; u < width; u++) {
4167 tinyexr::cpy4(&val, line_ptr + u);
4169 tinyexr::swap4(
reinterpret_cast<unsigned int*
>(&val));
4181static bool DecodeTiledPixelData(
4182 unsigned char** out_images,
int* width,
int* height,
4183 const int* requested_pixel_types,
const unsigned char* data_ptr,
4184 size_t data_len,
int compression_type,
int line_order,
int data_width,
4185 int data_height,
int tile_offset_x,
int tile_offset_y,
int tile_size_x,
4186 int tile_size_y,
size_t pixel_data_size,
size_t num_attributes,
4189 const std::vector<size_t>& channel_offset_list)
4192 if (tile_size_x * tile_offset_x > data_width || tile_size_y * tile_offset_y > data_height) {
4197 if ((tile_offset_x + 1) * tile_size_x >= data_width) {
4198 (*width) = data_width - (tile_offset_x * tile_size_x);
4200 (*width) = tile_size_x;
4203 if ((tile_offset_y + 1) * tile_size_y >= data_height) {
4204 (*height) = data_height - (tile_offset_y * tile_size_y);
4206 (*height) = tile_size_y;
4210 return DecodePixelData(out_images, requested_pixel_types, data_ptr, data_len,
4211 compression_type, line_order, (*width), tile_size_y,
4213 (*height), pixel_data_size, num_attributes, attributes,
4214 num_channels, channels, channel_offset_list);
4217static bool ComputeChannelLayout(std::vector<size_t>* channel_offset_list,
4218 int* pixel_data_size,
size_t* channel_offset,
4222 channel_offset_list->resize(
static_cast<size_t>(num_channels));
4224 (*pixel_data_size) = 0;
4225 (*channel_offset) = 0;
4227 for (
size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
4228 (*channel_offset_list)[c] = (*channel_offset);
4230 (*pixel_data_size) +=
sizeof(
unsigned short);
4231 (*channel_offset) +=
sizeof(
unsigned short);
4233 (*pixel_data_size) +=
sizeof(float);
4234 (*channel_offset) +=
sizeof(float);
4236 (*pixel_data_size) +=
sizeof(
unsigned int);
4237 (*channel_offset) +=
sizeof(
unsigned int);
4247static unsigned char** AllocateImage(
int num_channels,
4249 const int* requested_pixel_types,
4250 int data_width,
int data_height,
bool* success)
4252 unsigned char** images =
reinterpret_cast<unsigned char**
>(
static_cast<float**
>(
4253 malloc(
sizeof(
float*) *
static_cast<size_t>(num_channels))));
4255 for (
size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
4261 for (
size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
4262 size_t data_len =
static_cast<size_t>(data_width) *
static_cast<size_t>(data_height);
4268 images[c] =
reinterpret_cast<unsigned char*
>(
static_cast<unsigned short*
>(
4269 malloc(
sizeof(
unsigned short) * data_len)));
4271 images[c] =
reinterpret_cast<unsigned char*
>(
4272 static_cast<float*
>(malloc(
sizeof(
float) * data_len)));
4281 images[c] =
reinterpret_cast<unsigned char*
>(
4282 static_cast<float*
>(malloc(
sizeof(
float) * data_len)));
4286 images[c] =
reinterpret_cast<unsigned char*
>(
4287 static_cast<unsigned int*
>(malloc(
sizeof(
unsigned int) * data_len)));
4296 for (
size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
4316static inline std::wstring UTF8ToWchar(
const std::string& str)
4318 int wstr_size = MultiByteToWideChar(CP_UTF8, 0, str.data(), (
int)str.size(), NULL, 0);
4319 std::wstring wstr(wstr_size, 0);
4320 MultiByteToWideChar(CP_UTF8, 0, str.data(), (
int)str.size(), &wstr[0],
4326static int ParseEXRHeader(HeaderInfo* info,
bool* empty_header,
4328 const unsigned char*
buf,
size_t size)
4330 const char* marker =
reinterpret_cast<const char*
>(&
buf[0]);
4333 (*empty_header) =
false;
4337 if (
size > 0 && marker[0] ==
'\0') {
4340 (*empty_header) =
true;
4357 bool has_channels =
false;
4358 bool has_compression =
false;
4359 bool has_data_window =
false;
4360 bool has_display_window =
false;
4361 bool has_line_order =
false;
4362 bool has_pixel_aspect_ratio =
false;
4363 bool has_screen_window_center =
false;
4364 bool has_screen_window_width =
false;
4365 bool has_name =
false;
4366 bool has_type =
false;
4371 info->data_window.min_x = 0;
4372 info->data_window.min_y = 0;
4373 info->data_window.max_x = 0;
4374 info->data_window.max_y = 0;
4375 info->line_order = 0;
4376 info->display_window.min_x = 0;
4377 info->display_window.min_y = 0;
4378 info->display_window.max_x = 0;
4379 info->display_window.max_y = 0;
4380 info->screen_window_center[0] = 0.0f;
4381 info->screen_window_center[1] = 0.0f;
4382 info->screen_window_width = -1.0f;
4383 info->pixel_aspect_ratio = -1.0f;
4386 info->tile_size_x = -1;
4387 info->tile_size_y = -1;
4388 info->tile_level_mode = -1;
4389 info->tile_rounding_mode = -1;
4391 info->attributes.clear();
4394 size_t orig_size =
size;
4398 (*err) +=
"Insufficient data size for attributes.\n";
4401 }
else if (marker[0] ==
'\0') {
4406 std::string attr_name;
4407 std::string attr_type;
4408 std::vector<unsigned char> data;
4410 if (!tinyexr::ReadAttribute(&attr_name, &attr_type, &data, &marker_size,
4413 (*err) +=
"Failed to read attribute.\n";
4417 marker += marker_size;
4418 size -= marker_size;
4422 unsigned int x_size, y_size;
4423 unsigned char tile_mode;
4424 if (data.size() != 9) {
4426 (*err) +=
"(ParseEXRHeader) Invalid attribute data size. Attribute data size must be 9.\n";
4431 memcpy(&x_size, &data.at(0),
sizeof(
int));
4432 memcpy(&y_size, &data.at(4),
sizeof(
int));
4433 tile_mode = data[8];
4434 tinyexr::swap4(&x_size);
4435 tinyexr::swap4(&y_size);
4437 if (x_size >
static_cast<unsigned int>(std::numeric_limits<int>::max()) || y_size >
static_cast<unsigned int>(std::numeric_limits<int>::max())) {
4439 (*err) =
"Tile sizes were invalid.";
4444 info->tile_size_x =
static_cast<int>(x_size);
4445 info->tile_size_y =
static_cast<int>(y_size);
4448 info->tile_level_mode = tile_mode & 0x3;
4449 info->tile_rounding_mode = (tile_mode >> 4) & 0x1;
4451 }
else if (attr_name.compare(
"compression") == 0) {
4462 (*err) =
"PIZ compression is not supported.";
4473 (*err) =
"ZFP compression is not supported.";
4481 (*err) =
"Unknown compression type.";
4486 info->compression_type =
static_cast<int>(data[0]);
4487 has_compression =
true;
4489 }
else if (attr_name.compare(
"channels") == 0) {
4497 if (!ReadChannelInfo(info->channels, data)) {
4499 (*err) +=
"Failed to parse channel info.\n";
4504 if (info->channels.size() < 1) {
4506 (*err) +=
"# of channels is zero.\n";
4511 has_channels =
true;
4513 }
else if (attr_name.compare(
"dataWindow") == 0) {
4514 if (data.size() >= 16) {
4515 memcpy(&info->data_window.min_x, &data.at(0),
sizeof(
int));
4516 memcpy(&info->data_window.min_y, &data.at(4),
sizeof(
int));
4517 memcpy(&info->data_window.max_x, &data.at(8),
sizeof(
int));
4518 memcpy(&info->data_window.max_y, &data.at(12),
sizeof(
int));
4519 tinyexr::swap4(&info->data_window.min_x);
4520 tinyexr::swap4(&info->data_window.min_y);
4521 tinyexr::swap4(&info->data_window.max_x);
4522 tinyexr::swap4(&info->data_window.max_y);
4523 has_data_window =
true;
4525 }
else if (attr_name.compare(
"displayWindow") == 0) {
4526 if (data.size() >= 16) {
4527 memcpy(&info->display_window.min_x, &data.at(0),
sizeof(
int));
4528 memcpy(&info->display_window.min_y, &data.at(4),
sizeof(
int));
4529 memcpy(&info->display_window.max_x, &data.at(8),
sizeof(
int));
4530 memcpy(&info->display_window.max_y, &data.at(12),
sizeof(
int));
4531 tinyexr::swap4(&info->display_window.min_x);
4532 tinyexr::swap4(&info->display_window.min_y);
4533 tinyexr::swap4(&info->display_window.max_x);
4534 tinyexr::swap4(&info->display_window.max_y);
4536 has_display_window =
true;
4538 }
else if (attr_name.compare(
"lineOrder") == 0) {
4539 if (data.size() >= 1) {
4540 info->line_order =
static_cast<int>(data[0]);
4541 has_line_order =
true;
4543 }
else if (attr_name.compare(
"pixelAspectRatio") == 0) {
4544 if (data.size() >=
sizeof(
float)) {
4545 memcpy(&info->pixel_aspect_ratio, &data.at(0),
sizeof(
float));
4546 tinyexr::swap4(&info->pixel_aspect_ratio);
4547 has_pixel_aspect_ratio =
true;
4549 }
else if (attr_name.compare(
"screenWindowCenter") == 0) {
4550 if (data.size() >= 8) {
4551 memcpy(&info->screen_window_center[0], &data.at(0),
sizeof(
float));
4552 memcpy(&info->screen_window_center[1], &data.at(4),
sizeof(
float));
4553 tinyexr::swap4(&info->screen_window_center[0]);
4554 tinyexr::swap4(&info->screen_window_center[1]);
4555 has_screen_window_center =
true;
4557 }
else if (attr_name.compare(
"screenWindowWidth") == 0) {
4558 if (data.size() >=
sizeof(
float)) {
4559 memcpy(&info->screen_window_width, &data.at(0),
sizeof(
float));
4560 tinyexr::swap4(&info->screen_window_width);
4562 has_screen_window_width =
true;
4564 }
else if (attr_name.compare(
"chunkCount") == 0) {
4565 if (data.size() >=
sizeof(
int)) {
4566 memcpy(&info->chunk_count, &data.at(0),
sizeof(
int));
4567 tinyexr::swap4(&info->chunk_count);
4569 }
else if (attr_name.compare(
"name") == 0) {
4570 if (!data.empty() && data[0]) {
4572 size_t len = strlen(
reinterpret_cast<const char*
>(&data[0]));
4573 info->name.resize(len);
4574 info->name.assign(
reinterpret_cast<const char*
>(&data[0]), len);
4577 }
else if (attr_name.compare(
"type") == 0) {
4578 if (!data.empty() && data[0]) {
4580 size_t len = strlen(
reinterpret_cast<const char*
>(&data[0]));
4581 info->type.resize(len);
4582 info->type.assign(
reinterpret_cast<const char*
>(&data[0]), len);
4590 strncpy_s(attrib.
name, attr_name.c_str(), 255);
4591 strncpy_s(attrib.
type, attr_type.c_str(), 255);
4593 strncpy(attrib.
name, attr_name.c_str(), 255);
4594 strncpy(attrib.
type, attr_type.c_str(), 255);
4596 attrib.
name[255] =
'\0';
4597 attrib.
type[255] =
'\0';
4599 attrib.
size =
static_cast<int>(data.size());
4600 attrib.
value =
static_cast<unsigned char*
>(malloc(data.size()));
4601 memcpy(
reinterpret_cast<char*
>(attrib.
value), &data.at(0),
4603 info->attributes.push_back(attrib);
4610 std::stringstream ss_err;
4612 if (!has_compression) {
4613 ss_err <<
"\"compression\" attribute not found in the header."
4617 if (!has_channels) {
4618 ss_err <<
"\"channels\" attribute not found in the header." << std::endl;
4621 if (!has_line_order) {
4622 ss_err <<
"\"lineOrder\" attribute not found in the header." << std::endl;
4625 if (!has_display_window) {
4626 ss_err <<
"\"displayWindow\" attribute not found in the header."
4630 if (!has_data_window) {
4631 ss_err <<
"\"dataWindow\" attribute not found in the header or invalid."
4635 if (!has_pixel_aspect_ratio) {
4636 ss_err <<
"\"pixelAspectRatio\" attribute not found in the header."
4640 if (!has_screen_window_width) {
4641 ss_err <<
"\"screenWindowWidth\" attribute not found in the header."
4645 if (!has_screen_window_center) {
4646 ss_err <<
"\"screenWindowCenter\" attribute not found in the header."
4652 ss_err <<
"\"name\" attribute not found in the header."
4656 ss_err <<
"\"type\" attribute not found in the header."
4661 if (!(ss_err.str().empty())) {
4663 (*err) += ss_err.str();
4670 info->header_len =
static_cast<unsigned int>(orig_size -
size);
4676static bool ConvertHeader(
EXRHeader* exr_header,
const HeaderInfo& info, std::string* warn, std::string* err)
4693 exr_header->
tiled = info.tiled;
4701 if (!info.type.empty()) {
4703 if (info.type ==
"scanlineimage") {
4704 if (exr_header->
tiled) {
4706 (*err) +=
"(ConvertHeader) tiled bit must be off for `scanlineimage` type.\n";
4710 }
else if (info.type ==
"tiledimage") {
4711 if (!exr_header->
tiled) {
4713 (*err) +=
"(ConvertHeader) tiled bit must be on for `tiledimage` type.\n";
4717 }
else if (info.type ==
"deeptile") {
4719 if (!exr_header->
tiled) {
4721 (*err) +=
"(ConvertHeader) tiled bit must be on for `deeptile` type.\n";
4725 }
else if (info.type ==
"deepscanline") {
4727 if (exr_header->
tiled) {
4729 (*err) +=
"(ConvertHeader) tiled bit must be off for `deepscanline` type.\n";
4735 std::stringstream ss;
4736 ss <<
"(ConvertHeader) Unsupported or unknown info.type: " << info.type <<
"\n";
4737 (*warn) += ss.str();
4746 exr_header->
num_channels =
static_cast<int>(info.channels.size());
4750 for (
size_t c = 0; c < static_cast<size_t>(exr_header->
num_channels); c++) {
4752 strncpy_s(exr_header->
channels[c].
name, info.channels[c].name.c_str(), 255);
4754 strncpy(exr_header->
channels[c].
name, info.channels[c].name.c_str(), 255);
4766 malloc(
sizeof(
int) *
static_cast<size_t>(exr_header->
num_channels)));
4767 for (
size_t c = 0; c < static_cast<size_t>(exr_header->
num_channels); c++) {
4768 exr_header->
pixel_types[c] = info.channels[c].pixel_type;
4773 malloc(
sizeof(
int) *
static_cast<size_t>(exr_header->
num_channels)));
4774 for (
size_t c = 0; c < static_cast<size_t>(exr_header->
num_channels); c++) {
4815 std::vector<std::vector<std::vector<tinyexr::tinyexr_uint64>>> offsets;
4821static int LevelIndex(
int lx,
int ly,
int tile_level_mode,
int num_x_levels)
4823 switch (tile_level_mode) {
4831 return lx + ly * num_x_levels;
4839static int LevelSize(
int toplevel_size,
int level,
int tile_rounding_mode)
4845 int b =
static_cast<int>(1u <<
static_cast<unsigned int>(level));
4846 int level_size = toplevel_size / b;
4851 return std::max(level_size, 1);
4855 const OffsetData& offset_data,
4856 const std::vector<size_t>& channel_offset_list,
4857 int pixel_data_size,
4858 const unsigned char* head,
const size_t size,
4864 int num_y_tiles =
int(offset_data.offsets[
size_t(level_index)].size());
4865 if (num_y_tiles < 1) {
4868 int num_x_tiles =
int(offset_data.offsets[
size_t(level_index)][0].size());
4869 if (num_x_tiles < 1) {
4872 int num_tiles = num_x_tiles * num_y_tiles;
4878 EF_INVALID_DATA = 1,
4879 EF_INSUFFICIENT_DATA = 2,
4880 EF_FAILED_TO_DECODE = 4
4882#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
4883 std::atomic<unsigned> error_flag(EF_SUCCESS);
4885 unsigned error_flag(EF_SUCCESS);
4894 (*err) +=
"Failed to decode tile data.\n";
4900 calloc(
static_cast<size_t>(num_tiles),
sizeof(
EXRTile)));
4902#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
4903 std::vector<std::thread> workers;
4904 std::atomic<int> tile_count(0);
4906 int num_threads = std::max(1,
int(std::thread::hardware_concurrency()));
4907 if (num_threads >
int(num_tiles)) {
4908 num_threads =
int(num_tiles);
4911 for (
int t = 0; t < num_threads; t++) {
4912 workers.emplace_back(std::thread([&]() {
4914 while ((tile_idx = tile_count++) < num_tiles) {
4917#if TINYEXR_USE_OPENMP
4918#pragma omp parallel for
4920 for (
int tile_idx = 0; tile_idx < num_tiles; tile_idx++) {
4923 bool alloc_success =
false;
4924 exr_image->
tiles[tile_idx].
images = tinyexr::AllocateImage(
4925 num_channels, exr_header->
channels,
4929 if (!alloc_success) {
4930 error_flag |= EF_INVALID_DATA;
4934 int x_tile = tile_idx % num_x_tiles;
4935 int y_tile = tile_idx / num_x_tiles;
4939 tinyexr::tinyexr_uint64
offset = offset_data.offsets[size_t(level_index)][size_t(y_tile)][size_t(x_tile)];
4942 error_flag |= EF_INSUFFICIENT_DATA;
4946 size_t data_size = size_t(
size - (
offset +
sizeof(
int) * 5));
4947 const unsigned char* data_ptr =
reinterpret_cast<const unsigned char*
>(head +
offset);
4949 int tile_coordinates[4];
4950 memcpy(tile_coordinates, data_ptr,
sizeof(
int) * 4);
4951 tinyexr::swap4(&tile_coordinates[0]);
4952 tinyexr::swap4(&tile_coordinates[1]);
4953 tinyexr::swap4(&tile_coordinates[2]);
4954 tinyexr::swap4(&tile_coordinates[3]);
4956 if (tile_coordinates[2] != exr_image->
level_x) {
4958 error_flag |= EF_INVALID_DATA;
4961 if (tile_coordinates[3] != exr_image->
level_y) {
4963 error_flag |= EF_INVALID_DATA;
4968 memcpy(&data_len, data_ptr + 16,
4970 tinyexr::swap4(&data_len);
4972 if (data_len < 2 ||
size_t(data_len) > data_size) {
4974 error_flag |= EF_INSUFFICIENT_DATA;
4980 bool ret = tinyexr::DecodeTiledPixelData(
4988 tile_coordinates[0], tile_coordinates[1], exr_header->
tile_size_x,
4989 exr_header->
tile_size_y,
static_cast<size_t>(pixel_data_size),
4993 exr_header->
channels, channel_offset_list);
4997 error_flag |= EF_FAILED_TO_DECODE;
5002 exr_image->
tiles[tile_idx].
level_x = tile_coordinates[2];
5003 exr_image->
tiles[tile_idx].
level_y = tile_coordinates[3];
5005#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
5010 for (
auto& t : workers) {
5020 exr_image->
num_tiles =
static_cast<int>(num_tiles);
5025 if (error_flag & EF_INSUFFICIENT_DATA) {
5026 (*err) +=
"Insufficient data length.\n";
5028 if (error_flag & EF_FAILED_TO_DECODE) {
5029 (*err) +=
"Failed to decode tile data.\n";
5036 const OffsetData& offset_data,
5037 const unsigned char* head,
const size_t size,
5042 int num_scanline_blocks = 1;
5044 num_scanline_blocks = 16;
5046 num_scanline_blocks = 32;
5048 num_scanline_blocks = 16;
5051 tinyexr::ZFPCompressionParam zfp_compression_param;
5052 if (!FindZFPCompressionParam(&zfp_compression_param,
5062 (*err) +=
"Invalid data window.\n";
5067 tinyexr_int64 data_width =
static_cast<tinyexr_int64
>(exr_header->
data_window.
max_x) -
static_cast<tinyexr_int64
>(exr_header->
data_window.
min_x) +
static_cast<tinyexr_int64
>(1);
5068 tinyexr_int64 data_height =
static_cast<tinyexr_int64
>(exr_header->
data_window.
max_y) -
static_cast<tinyexr_int64
>(exr_header->
data_window.
min_y) +
static_cast<tinyexr_int64
>(1);
5070 if (data_width <= 0) {
5072 (*err) +=
"Invalid data window width.\n";
5077 if (data_height <= 0) {
5079 (*err) +=
"Invalid data window height.\n";
5086 if ((data_width > TINYEXR_DIMENSION_THRESHOLD) || (data_height > TINYEXR_DIMENSION_THRESHOLD)) {
5088 std::stringstream ss;
5089 ss <<
"data_with or data_height too large. data_width: " << data_width
5091 <<
"data_height = " << data_height << std::endl;
5096 if (exr_header->
tiled) {
5097 if ((exr_header->
tile_size_x > TINYEXR_DIMENSION_THRESHOLD) || (exr_header->
tile_size_y > TINYEXR_DIMENSION_THRESHOLD)) {
5099 std::stringstream ss;
5100 ss <<
"tile with or tile height too large. tile width: " << exr_header->
tile_size_x
5102 <<
"tile height = " << exr_header->
tile_size_y << std::endl;
5110 const std::vector<tinyexr::tinyexr_uint64>& offsets = offset_data.offsets[0][0];
5111 size_t num_blocks = offsets.size();
5113 std::vector<size_t> channel_offset_list;
5114 int pixel_data_size = 0;
5115 size_t channel_offset = 0;
5116 if (!tinyexr::ComputeChannelLayout(&channel_offset_list, &pixel_data_size,
5117 &channel_offset, num_channels,
5120 (*err) +=
"Failed to compute channel layout.\n";
5125#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
5126 std::atomic<bool> invalid_data(
false);
5128 bool invalid_data(
false);
5131 if (exr_header->
tiled) {
5135 std::stringstream ss;
5136 ss <<
"Invalid tile size x : " << exr_header->
tile_size_x <<
"\n";
5144 std::stringstream ss;
5145 ss <<
"Invalid tile size y : " << exr_header->
tile_size_y <<
"\n";
5152 for (
int level = 0; level < offset_data.num_x_levels; ++level) {
5154 level_image = exr_image;
5161 if (level_image->
width < 1) {
5167 if (level_image->
height < 1) {
5174 int ret = DecodeTiledLevel(level_image, exr_header,
5176 channel_offset_list,
5185 for (
int level_y = 0; level_y < offset_data.num_y_levels; ++level_y)
5186 for (
int level_x = 0; level_x < offset_data.num_x_levels; ++level_x) {
5188 level_image = exr_image;
5196 if (level_image->
width < 1) {
5201 if (level_image->
height < 1) {
5205 level_image->
level_x = level_x;
5206 level_image->
level_y = level_y;
5208 int ret = DecodeTiledLevel(level_image, exr_header,
5210 channel_offset_list,
5221 size_t total_data_len = size_t(data_width) * size_t(data_height) * size_t(num_channels);
5222 const bool total_data_len_overflown =
sizeof(
void*) == 8 ? (total_data_len >= 0x4000000000) :
false;
5223 if ((total_data_len == 0) || total_data_len_overflown) {
5225 std::stringstream ss;
5226 ss <<
"Image data size is zero or too large: width = " << data_width
5227 <<
", height = " << data_height <<
", channels = " << num_channels
5234 bool alloc_success =
false;
5235 exr_image->
images = tinyexr::AllocateImage(
5237 int(data_width),
int(data_height), &alloc_success);
5239 if (!alloc_success) {
5241 std::stringstream ss;
5242 ss <<
"Failed to allocate memory for Images. Maybe EXR header is corrupted or Image data size is too large: width = " << data_width
5243 <<
", height = " << data_height <<
", channels = " << num_channels
5250#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
5251 std::vector<std::thread> workers;
5252 std::atomic<int> y_count(0);
5254 int num_threads = std::max(1,
int(std::thread::hardware_concurrency()));
5255 if (num_threads >
int(num_blocks)) {
5256 num_threads =
int(num_blocks);
5259 for (
int t = 0; t < num_threads; t++) {
5260 workers.emplace_back(std::thread([&]() {
5262 while ((
y = y_count++) <
int(num_blocks)) {
5266#if TINYEXR_USE_OPENMP
5267#pragma omp parallel for
5272 size_t y_idx =
static_cast<size_t>(
y);
5274 if (offsets[y_idx] +
sizeof(
int) * 2 >
size) {
5275 invalid_data =
true;
5280 size_t data_size = size_t(
size - (offsets[y_idx] +
sizeof(
int) * 2));
5281 const unsigned char* data_ptr =
reinterpret_cast<const unsigned char*
>(head + offsets[y_idx]);
5284 memcpy(&line_no, data_ptr,
sizeof(
int));
5286 memcpy(&data_len, data_ptr + 4,
sizeof(
int));
5287 tinyexr::swap4(&line_no);
5288 tinyexr::swap4(&data_len);
5290 if (
size_t(data_len) > data_size) {
5291 invalid_data =
true;
5293 }
else if ((line_no > (2 << 20)) || (line_no < -(2 << 20))) {
5296 invalid_data =
true;
5297 }
else if (data_len == 0) {
5300 invalid_data =
true;
5303 int end_line_no = (std::min)(line_no + num_scanline_blocks,
5306 int num_lines = end_line_no - line_no;
5308 if (num_lines <= 0) {
5309 invalid_data =
true;
5317 tinyexr_int64 lno =
static_cast<tinyexr_int64
>(line_no) -
static_cast<tinyexr_int64
>(exr_header->
data_window.
min_y);
5318 if (lno > std::numeric_limits<int>::max()) {
5320 }
else if (lno < -std::numeric_limits<int>::max()) {
5327 invalid_data =
true;
5329 if (!tinyexr::DecodePixelData(
5331 data_ptr,
static_cast<size_t>(data_len),
5333 int(data_width),
int(data_height),
int(data_width),
y, line_no,
5334 num_lines,
static_cast<size_t>(pixel_data_size),
5335 static_cast<size_t>(
5339 exr_header->
channels, channel_offset_list)) {
5340 invalid_data =
true;
5347#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
5352 for (
auto& t : workers) {
5362 (*err) +=
"Invalid/Corrupted data found when decoding pixels.\n";
5366 for (
size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
5367 if (exr_image->
images[c]) {
5368 free(exr_image->
images[c]);
5369 exr_image->
images[c] = NULL;
5385 exr_image->
width =
int(data_width);
5392static bool ReconstructLineOffsets(
5393 std::vector<tinyexr::tinyexr_uint64>* offsets,
size_t n,
5394 const unsigned char* head,
const unsigned char* marker,
const size_t size)
5396 if (head >= marker) {
5399 if (offsets->size() != n) {
5403 for (
size_t i = 0; i < n; i++) {
5404 size_t offset =
static_cast<size_t>(marker - head);
5406 if ((
offset +
sizeof(tinyexr::tinyexr_uint64)) >=
size) {
5411 unsigned int data_len;
5413 memcpy(&
y, marker,
sizeof(
int));
5414 memcpy(&data_len, marker + 4,
sizeof(
unsigned int));
5416 if (data_len >=
size) {
5421 tinyexr::swap4(&data_len);
5425 marker += data_len + 8;
5431static int FloorLog2(
unsigned x)
5444static int CeilLog2(
unsigned x)
5461static int RoundLog2(
int x,
int tile_rounding_mode)
5463 return (tile_rounding_mode ==
TINYEXR_TILE_ROUND_DOWN) ? FloorLog2(
static_cast<unsigned>(
x)) : CeilLog2(static_cast<unsigned>(
x));
5466static int CalculateNumXLevels(
const EXRHeader* exr_header)
5483 int w = max_x - min_x + 1;
5484 int h = max_y - min_y + 1;
5491 int w = max_x - min_x + 1;
5503static int CalculateNumYLevels(
const EXRHeader* exr_header)
5520 int w = max_x - min_x + 1;
5521 int h = max_y - min_y + 1;
5528 int h = max_y - min_y + 1;
5540static bool CalculateNumTiles(std::vector<int>& numTiles,
5543 int tile_rounding_mode)
5545 for (
unsigned i = 0; i < numTiles.size(); i++) {
5546 int l = LevelSize(toplevel_size,
int(i), tile_rounding_mode);
5550 TINYEXR_CHECK_AND_RETURN_C(l <= std::numeric_limits<int>::max() -
size + 1,
false);
5552 numTiles[i] = (l +
size - 1) /
size;
5557static bool PrecalculateTileInfo(std::vector<int>& num_x_tiles,
5558 std::vector<int>& num_y_tiles,
5566 int num_x_levels = CalculateNumXLevels(exr_header);
5568 if (num_x_levels < 0) {
5572 int num_y_levels = CalculateNumYLevels(exr_header);
5574 if (num_y_levels < 0) {
5578 num_x_tiles.resize(
size_t(num_x_levels));
5579 num_y_tiles.resize(
size_t(num_y_levels));
5581 if (!CalculateNumTiles(num_x_tiles,
5588 if (!CalculateNumTiles(num_y_tiles,
5598static void InitSingleResolutionOffsets(OffsetData& offset_data,
size_t num_blocks)
5600 offset_data.offsets.resize(1);
5601 offset_data.offsets[0].resize(1);
5602 offset_data.offsets[0][0].resize(num_blocks);
5603 offset_data.num_x_levels = 1;
5604 offset_data.num_y_levels = 1;
5609static int InitTileOffsets(OffsetData& offset_data,
5611 const std::vector<int>& num_x_tiles,
5612 const std::vector<int>& num_y_tiles)
5614 int num_tile_blocks = 0;
5615 offset_data.num_x_levels =
static_cast<int>(num_x_tiles.size());
5616 offset_data.num_y_levels =
static_cast<int>(num_y_tiles.size());
5620 TINYEXR_CHECK_AND_RETURN_C(offset_data.num_x_levels == offset_data.num_y_levels, 0);
5621 offset_data.offsets.resize(
size_t(offset_data.num_x_levels));
5623 for (
unsigned int l = 0; l < offset_data.offsets.size(); ++l) {
5624 offset_data.offsets[l].resize(
size_t(num_y_tiles[l]));
5626 for (
unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) {
5627 offset_data.offsets[l][dy].resize(
size_t(num_x_tiles[l]));
5628 num_tile_blocks += num_x_tiles[l];
5635 offset_data.offsets.resize(
static_cast<size_t>(offset_data.num_x_levels) *
static_cast<size_t>(offset_data.num_y_levels));
5637 for (
int ly = 0; ly < offset_data.num_y_levels; ++ly) {
5638 for (
int lx = 0; lx < offset_data.num_x_levels; ++lx) {
5639 int l = ly * offset_data.num_x_levels + lx;
5640 offset_data.offsets[size_t(l)].resize(
size_t(num_y_tiles[
size_t(ly)]));
5642 for (
size_t dy = 0; dy < offset_data.offsets[size_t(l)].size(); ++dy) {
5643 offset_data.offsets[size_t(l)][dy].resize(
size_t(num_x_tiles[
size_t(lx)]));
5644 num_tile_blocks += num_x_tiles[size_t(lx)];
5653 return num_tile_blocks;
5656static bool IsAnyOffsetsAreInvalid(
const OffsetData& offset_data)
5658 for (
unsigned int l = 0; l < offset_data.offsets.size(); ++l)
5659 for (
unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy)
5660 for (
unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx)
5661 if (
reinterpret_cast<const tinyexr::tinyexr_int64&
>(offset_data.offsets[l][dy][dx]) <= 0)
5667static bool isValidTile(
const EXRHeader* exr_header,
5668 const OffsetData& offset_data,
5669 int dx,
int dy,
int lx,
int ly)
5671 if (lx < 0 || ly < 0 || dx < 0 || dy < 0)
5673 int num_x_levels = offset_data.num_x_levels;
5674 int num_y_levels = offset_data.num_y_levels;
5678 if (lx == 0 && ly == 0 && offset_data.offsets.size() > 0 && offset_data.offsets[0].size() >
static_cast<size_t>(dy) && offset_data.offsets[0][
size_t(dy)].size() >
static_cast<size_t>(dx)) {
5686 if (lx < num_x_levels && ly < num_y_levels && offset_data.offsets.size() >
static_cast<size_t>(lx) && offset_data.offsets[
size_t(lx)].size() >
static_cast<size_t>(dy) && offset_data.offsets[
size_t(lx)][
size_t(dy)].size() >
static_cast<size_t>(dx)) {
5693 size_t idx =
static_cast<size_t>(lx) +
static_cast<size_t>(ly) *
static_cast<size_t>(num_x_levels);
5694 if (lx < num_x_levels && ly < num_y_levels && (offset_data.offsets.size() > idx) && offset_data.offsets[idx].size() >
static_cast<size_t>(dy) && offset_data.offsets[idx][
size_t(dy)].size() >
static_cast<size_t>(dx)) {
5709static bool ReconstructTileOffsets(OffsetData& offset_data,
5711 const unsigned char* head,
const unsigned char* marker,
const size_t size,
5712 bool isMultiPartFile,
5715 int numXLevels = offset_data.num_x_levels;
5716 for (
unsigned int l = 0; l < offset_data.offsets.size(); ++l) {
5717 for (
unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) {
5718 for (
unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) {
5719 tinyexr::tinyexr_uint64 tileOffset = tinyexr::tinyexr_uint64(marker - head);
5721 if (isMultiPartFile) {
5722 if ((marker +
sizeof(
int)) >= (head +
size)) {
5727 marker +=
sizeof(
int);
5730 if ((marker + 4 *
sizeof(
int)) >= (head +
size)) {
5735 memcpy(&tileX, marker,
sizeof(
int));
5736 tinyexr::swap4(&tileX);
5737 marker +=
sizeof(
int);
5740 memcpy(&tileY, marker,
sizeof(
int));
5741 tinyexr::swap4(&tileY);
5742 marker +=
sizeof(
int);
5745 memcpy(&levelX, marker,
sizeof(
int));
5746 tinyexr::swap4(&levelX);
5747 marker +=
sizeof(
int);
5750 memcpy(&levelY, marker,
sizeof(
int));
5751 tinyexr::swap4(&levelY);
5752 marker +=
sizeof(
int);
5755 if ((marker + 2 *
sizeof(tinyexr::tinyexr_int64)) >= (head +
size)) {
5758 tinyexr::tinyexr_int64 packed_offset_table_size;
5759 memcpy(&packed_offset_table_size, marker,
sizeof(tinyexr::tinyexr_int64));
5760 tinyexr::swap8(
reinterpret_cast<tinyexr::tinyexr_uint64*
>(&packed_offset_table_size));
5761 marker +=
sizeof(tinyexr::tinyexr_int64);
5763 tinyexr::tinyexr_int64 packed_sample_size;
5764 memcpy(&packed_sample_size, marker,
sizeof(tinyexr::tinyexr_int64));
5765 tinyexr::swap8(
reinterpret_cast<tinyexr::tinyexr_uint64*
>(&packed_sample_size));
5766 marker +=
sizeof(tinyexr::tinyexr_int64);
5769 marker += packed_offset_table_size + packed_sample_size + 8;
5771 if (marker >= (head +
size)) {
5777 if ((marker +
sizeof(uint32_t)) >= (head +
size)) {
5782 memcpy(&dataSize, marker,
sizeof(uint32_t));
5783 tinyexr::swap4(&dataSize);
5784 marker +=
sizeof(uint32_t);
5788 if (marker >= (head +
size)) {
5793 if (!isValidTile(exr_header, offset_data,
5794 tileX, tileY, levelX, levelY)) {
5798 int level_idx = LevelIndex(levelX, levelY, exr_header->
tile_level_mode, numXLevels);
5799 if (level_idx < 0) {
5803 if (
size_t(level_idx) >= offset_data.offsets.size()) {
5807 if (
size_t(tileY) >= offset_data.offsets[
size_t(level_idx)].size()) {
5811 if (
size_t(tileX) >= offset_data.offsets[
size_t(level_idx)][
size_t(tileY)].size()) {
5815 offset_data.offsets[size_t(level_idx)][size_t(tileY)][size_t(tileX)] = tileOffset;
5823static int ReadOffsets(OffsetData& offset_data,
5824 const unsigned char* head,
5825 const unsigned char*& marker,
5829 for (
unsigned int l = 0; l < offset_data.offsets.size(); ++l) {
5830 for (
unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) {
5831 for (
unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) {
5832 tinyexr::tinyexr_uint64
offset;
5833 if ((marker +
sizeof(tinyexr_uint64)) >= (head +
size)) {
5834 tinyexr::SetErrorMessage(
"Insufficient data size in offset table.", err);
5838 memcpy(&
offset, marker,
sizeof(tinyexr::tinyexr_uint64));
5841 tinyexr::SetErrorMessage(
"Invalid offset value in DecodeEXRImage.", err);
5844 marker +=
sizeof(tinyexr::tinyexr_uint64);
5845 offset_data.offsets[l][dy][dx] =
offset;
5853 const unsigned char* head,
5854 const unsigned char* marker,
const size_t size,
5857 if (exr_image == NULL || exr_header == NULL || head == NULL || marker == NULL || (
size <= tinyexr::kEXRVersionSize)) {
5858 tinyexr::SetErrorMessage(
"Invalid argument for DecodeEXRImage().", err);
5862 int num_scanline_blocks = 1;
5864 num_scanline_blocks = 16;
5866 num_scanline_blocks = 32;
5868 num_scanline_blocks = 16;
5873 tinyexr::SetErrorMessage(
"Invalid data width value", err);
5876 tinyexr_int64 data_width =
static_cast<tinyexr_int64
>(exr_header->
data_window.
max_x) -
static_cast<tinyexr_int64
>(exr_header->
data_window.
min_x) +
static_cast<tinyexr_int64
>(1);
5877 if (data_width <= 0) {
5878 tinyexr::SetErrorMessage(
"Invalid data window width value", err);
5883 tinyexr::SetErrorMessage(
"Invalid data height value", err);
5886 tinyexr_int64 data_height =
static_cast<tinyexr_int64
>(exr_header->
data_window.
max_y) -
static_cast<tinyexr_int64
>(exr_header->
data_window.
min_y) +
static_cast<tinyexr_int64
>(1);
5888 if (data_height <= 0) {
5889 tinyexr::SetErrorMessage(
"Invalid data window height value", err);
5895 if (data_width > TINYEXR_DIMENSION_THRESHOLD) {
5896 tinyexr::SetErrorMessage(
"data width too large.", err);
5899 if (data_height > TINYEXR_DIMENSION_THRESHOLD) {
5900 tinyexr::SetErrorMessage(
"data height too large.", err);
5905 if (exr_header->
tiled) {
5906 if (exr_header->
tile_size_x > TINYEXR_DIMENSION_THRESHOLD) {
5907 tinyexr::SetErrorMessage(
"tile width too large.", err);
5910 if (exr_header->
tile_size_y > TINYEXR_DIMENSION_THRESHOLD) {
5911 tinyexr::SetErrorMessage(
"tile height too large.", err);
5917 OffsetData offset_data;
5918 size_t num_blocks = 0;
5921 if (exr_header->
tiled) {
5923 std::vector<int> num_x_tiles, num_y_tiles;
5924 if (!PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_header)) {
5925 tinyexr::SetErrorMessage(
"Failed to precalculate tile info.", err);
5928 num_blocks = size_t(InitTileOffsets(offset_data, exr_header, num_x_tiles, num_y_tiles));
5930 if (exr_header->
chunk_count !=
static_cast<int>(num_blocks)) {
5931 tinyexr::SetErrorMessage(
"Invalid offset table size.", err);
5937 int ret = ReadOffsets(offset_data, head, marker,
size, err);
5940 if (IsAnyOffsetsAreInvalid(offset_data)) {
5941 if (!ReconstructTileOffsets(offset_data, exr_header,
5945 tinyexr::SetErrorMessage(
"Invalid Tile Offsets data.", err);
5951 num_blocks =
static_cast<size_t>(exr_header->
chunk_count);
5952 InitSingleResolutionOffsets(offset_data, num_blocks);
5954 num_blocks =
static_cast<size_t>(data_height) /
static_cast<size_t>(num_scanline_blocks);
5955 if (num_blocks *
static_cast<size_t>(num_scanline_blocks) <
static_cast<size_t>(data_height)) {
5959 InitSingleResolutionOffsets(offset_data, num_blocks);
5962 if (!exr_header->
tiled) {
5963 std::vector<tinyexr::tinyexr_uint64>& offsets = offset_data.offsets[0][0];
5964 for (
size_t y = 0;
y < num_blocks;
y++) {
5965 tinyexr::tinyexr_uint64
offset;
5967 if ((marker +
sizeof(tinyexr_uint64)) >= (head +
size)) {
5968 tinyexr::SetErrorMessage(
"Insufficient data size in offset table.", err);
5972 memcpy(&
offset, marker,
sizeof(tinyexr::tinyexr_uint64));
5975 tinyexr::SetErrorMessage(
"Invalid offset value in DecodeEXRImage.", err);
5978 marker +=
sizeof(tinyexr::tinyexr_uint64);
5984 for (
size_t y = 0;
y < num_blocks;
y++) {
5985 if (offsets[
y] <= 0) {
5992 bool ret = ReconstructLineOffsets(&offsets, num_blocks, head, marker,
size);
5997 tinyexr::SetErrorMessage(
5998 "Cannot reconstruct lineOffset table in DecodeEXRImage.", err);
6007 int ret = DecodeChunk(exr_image, exr_header, offset_data, head,
size, &e);
6011 tinyexr::SetErrorMessage(e, err);
6019 for (
size_t c = 0; c < size_t(exr_header->
num_channels); c++) {
6020 if (exr_image->
images[c]) {
6021 free(exr_image->
images[c]);
6022 exr_image->
images[c] = NULL;
6026 exr_image->
images = NULL;
6035static void GetLayers(
const EXRHeader& exr_header,
6036 std::vector<std::string>& layer_names)
6042 layer_names.clear();
6045 const size_t pos = full_name.find_last_of(
'.');
6046 if (pos != std::string::npos && pos != 0 && pos + 1 < full_name.size()) {
6047 full_name.erase(pos);
6048 if (std::find(layer_names.begin(), layer_names.end(), full_name) == layer_names.end())
6049 layer_names.push_back(full_name);
6054struct LayerChannel {
6055 explicit LayerChannel(
size_t i, std::string n)
6064static void ChannelsInLayer(
const EXRHeader& exr_header,
6065 const std::string& layer_name,
6066 std::vector<LayerChannel>& channels)
6073 if (layer_name.empty()) {
6074 const size_t pos = ch_name.find_last_of(
'.');
6075 if (pos != std::string::npos && pos < ch_name.size()) {
6078 ch_name = ch_name.substr(pos + 1);
6081 const size_t pos = ch_name.find(layer_name +
'.');
6082 if (pos == std::string::npos)
6085 ch_name = ch_name.substr(layer_name.size() + 1);
6088 LayerChannel ch(
size_t(c), ch_name);
6089 channels.push_back(ch);
6105 tinyexr::SetErrorMessage(
"Invalid EXR header.", err);
6110 tinyexr::SetErrorMessage(
6111 "Loading multipart or DeepImage is not supported in LoadEXR() API",
6123 std::vector<std::string> layer_vec;
6124 tinyexr::GetLayers(exr_header, layer_vec);
6126 (*num_layers) =
int(layer_vec.size());
6127 (*layer_names) =
static_cast<const char**
>(
6128 malloc(
sizeof(
const char*) *
static_cast<size_t>(layer_vec.size())));
6129 for (
size_t c = 0; c < static_cast<size_t>(layer_vec.size()); c++) {
6131 (*layer_names)[c] = _strdup(layer_vec[c].c_str());
6133 (*layer_names)[c] = strdup(layer_vec[c].c_str());
6141int LoadEXR(
float** out_rgba,
int* width,
int* height,
const char*
filename,
6149 const char*
filename,
const char* layername,
6152 if (out_rgba == NULL) {
6153 tinyexr::SetErrorMessage(
"Invalid argument for LoadEXR()", err);
6166 std::stringstream ss;
6167 ss <<
"Failed to open EXR file or read version info from EXR file. code("
6169 tinyexr::SetErrorMessage(ss.str(), err);
6174 tinyexr::SetErrorMessage(
6175 "Loading multipart or DeepImage is not supported in LoadEXR() API",
6211 std::vector<std::string> layer_names;
6212 tinyexr::GetLayers(exr_header, layer_names);
6214 std::vector<tinyexr::LayerChannel> channels;
6215 tinyexr::ChannelsInLayer(
6216 exr_header, layername == NULL ?
"" : std::string(layername), channels);
6218 if (channels.size() < 1) {
6219 if (layername == NULL) {
6220 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);
6223 tinyexr::SetErrorMessage(
"Layer Not Found", err);
6230 size_t ch_count = channels.size() < 4 ? channels.size() : 4;
6231 for (
size_t c = 0; c < ch_count; c++) {
6232 const tinyexr::LayerChannel& ch = channels[c];
6234 if (ch.name ==
"R") {
6235 idxR =
int(ch.index);
6236 }
else if (ch.name ==
"G") {
6237 idxG =
int(ch.index);
6238 }
else if (ch.name ==
"B") {
6239 idxB =
int(ch.index);
6240 }
else if (ch.name ==
"A") {
6241 idxA =
int(ch.index);
6245 if (channels.size() == 1) {
6246 int chIdx =
int(channels.front().index);
6249 (*out_rgba) =
reinterpret_cast<float*
>(
6250 malloc(4 *
sizeof(
float) *
static_cast<size_t>(exr_image.
width) *
static_cast<size_t>(exr_image.
height)));
6252 if (exr_header.
tiled) {
6253 const size_t tile_size_x =
static_cast<size_t>(exr_header.
tile_size_x);
6254 const size_t tile_size_y =
static_cast<size_t>(exr_header.
tile_size_y);
6255 for (
int it = 0; it < exr_image.
num_tiles; it++) {
6256 for (
size_t j = 0; j < tile_size_y; j++) {
6257 for (
size_t i = 0; i < tile_size_x; i++) {
6258 const size_t ii =
static_cast<size_t>(exr_image.
tiles[it].
offset_x) * tile_size_x + i;
6259 const size_t jj =
static_cast<size_t>(exr_image.
tiles[it].
offset_y) * tile_size_y + j;
6260 const size_t idx = ii + jj *
static_cast<size_t>(exr_image.
width);
6263 if (ii >=
static_cast<size_t>(exr_image.
width)) {
6266 if (jj >=
static_cast<size_t>(exr_image.
height)) {
6269 const size_t srcIdx = i + j * tile_size_x;
6271 (*out_rgba)[4 * idx + 0] =
reinterpret_cast<float**
>(src)[chIdx][srcIdx];
6272 (*out_rgba)[4 * idx + 1] =
reinterpret_cast<float**
>(src)[chIdx][srcIdx];
6273 (*out_rgba)[4 * idx + 2] =
reinterpret_cast<float**
>(src)[chIdx][srcIdx];
6274 (*out_rgba)[4 * idx + 3] =
reinterpret_cast<float**
>(src)[chIdx][srcIdx];
6279 const size_t pixel_size =
static_cast<size_t>(exr_image.
width) *
static_cast<size_t>(exr_image.
height);
6280 for (
size_t i = 0; i < pixel_size; i++) {
6281 const float val =
reinterpret_cast<float**
>(exr_image.
images)[chIdx][i];
6282 (*out_rgba)[4 * i + 0] = val;
6283 (*out_rgba)[4 * i + 1] = val;
6284 (*out_rgba)[4 * i + 2] = val;
6285 (*out_rgba)[4 * i + 3] = val;
6292 tinyexr::SetErrorMessage(
"R channel not found", err);
6300 tinyexr::SetErrorMessage(
"G channel not found", err);
6307 tinyexr::SetErrorMessage(
"B channel not found", err);
6313 (*out_rgba) =
reinterpret_cast<float*
>(
6314 malloc(4 *
sizeof(
float) *
static_cast<size_t>(exr_image.
width) *
static_cast<size_t>(exr_image.
height)));
6315 if (exr_header.
tiled) {
6316 const size_t tile_size_x =
static_cast<size_t>(exr_header.
tile_size_x);
6317 const size_t tile_size_y =
static_cast<size_t>(exr_header.
tile_size_y);
6318 for (
int it = 0; it < exr_image.
num_tiles; it++) {
6319 for (
size_t j = 0; j < tile_size_y; j++) {
6320 for (
size_t i = 0; i < tile_size_x; i++) {
6321 const size_t ii =
static_cast<size_t>(exr_image.
tiles[it].
offset_x) * tile_size_x + i;
6322 const size_t jj =
static_cast<size_t>(exr_image.
tiles[it].
offset_y) * tile_size_y + j;
6323 const size_t idx = ii + jj *
static_cast<size_t>(exr_image.
width);
6326 if (ii >=
static_cast<size_t>(exr_image.
width)) {
6329 if (jj >=
static_cast<size_t>(exr_image.
height)) {
6332 const size_t srcIdx = i + j * tile_size_x;
6334 (*out_rgba)[4 * idx + 0] =
reinterpret_cast<float**
>(src)[idxR][srcIdx];
6335 (*out_rgba)[4 * idx + 1] =
reinterpret_cast<float**
>(src)[idxG][srcIdx];
6336 (*out_rgba)[4 * idx + 2] =
reinterpret_cast<float**
>(src)[idxB][srcIdx];
6338 (*out_rgba)[4 * idx + 3] =
reinterpret_cast<float**
>(src)[idxA][srcIdx];
6340 (*out_rgba)[4 * idx + 3] = 1.0;
6346 const size_t pixel_size =
static_cast<size_t>(exr_image.
width) *
static_cast<size_t>(exr_image.
height);
6347 for (
size_t i = 0; i < pixel_size; i++) {
6348 (*out_rgba)[4 * i + 0] =
reinterpret_cast<float**
>(exr_image.
images)[idxR][i];
6349 (*out_rgba)[4 * i + 1] =
reinterpret_cast<float**
>(exr_image.
images)[idxG][i];
6350 (*out_rgba)[4 * i + 2] =
reinterpret_cast<float**
>(exr_image.
images)[idxB][i];
6352 (*out_rgba)[4 * i + 3] =
reinterpret_cast<float**
>(exr_image.
images)[idxA][i];
6354 (*out_rgba)[4 * i + 3] = 1.0;
6360 (*width) = exr_image.
width;
6361 (*height) = exr_image.
height;
6394 const unsigned char* memory,
size_t size,
6397 if (memory == NULL || exr_header == NULL) {
6398 tinyexr::SetErrorMessage(
6399 "Invalid argument. `memory` or `exr_header` argument is null in "
6400 "ParseEXRHeaderFromMemory()",
6407 if (
size < tinyexr::kEXRVersionSize) {
6408 tinyexr::SetErrorMessage(
"Insufficient header/data size.\n", err);
6412 const unsigned char* marker = memory + tinyexr::kEXRVersionSize;
6413 size_t marker_size =
size - tinyexr::kEXRVersionSize;
6415 tinyexr::HeaderInfo info;
6420 std::string err_str;
6421 ret = ParseEXRHeader(&info, NULL, version, &err_str, marker, marker_size);
6424 if (err && !err_str.empty()) {
6425 tinyexr::SetErrorMessage(err_str, err);
6432 std::string err_str;
6434 if (!ConvertHeader(exr_header, info, &warn, &err_str)) {
6436 for (
size_t i = 0; i < info.attributes.size(); i++) {
6437 if (info.attributes[i].value) {
6438 free(info.attributes[i].value);
6441 if (err && !err_str.empty()) {
6442 tinyexr::SetErrorMessage(err_str, err);
6455 const unsigned char* memory,
size_t size,
6458 if (out_rgba == NULL || memory == NULL) {
6459 tinyexr::SetErrorMessage(
"Invalid argument for LoadEXRFromMemory", err);
6471 std::stringstream ss;
6472 ss <<
"Failed to parse EXR version. code(" << ret <<
")";
6473 tinyexr::SetErrorMessage(ss.str(), err);
6503 }
else if (strcmp(exr_header.
channels[c].
name,
"G") == 0) {
6505 }
else if (strcmp(exr_header.
channels[c].
name,
"B") == 0) {
6507 }
else if (strcmp(exr_header.
channels[c].
name,
"A") == 0) {
6516 (*out_rgba) =
reinterpret_cast<float*
>(
6517 malloc(4 *
sizeof(
float) *
static_cast<size_t>(exr_image.
width) *
static_cast<size_t>(exr_image.
height)));
6519 if (exr_header.
tiled) {
6520 const size_t tile_size_x =
static_cast<size_t>(exr_header.
tile_size_x);
6521 const size_t tile_size_y =
static_cast<size_t>(exr_header.
tile_size_y);
6522 for (
int it = 0; it < exr_image.
num_tiles; it++) {
6523 for (
size_t j = 0; j < tile_size_y; j++) {
6524 for (
size_t i = 0; i < tile_size_x; i++) {
6525 const size_t ii =
static_cast<size_t>(exr_image.
tiles[it].
offset_x) * tile_size_x + i;
6526 const size_t jj =
static_cast<size_t>(exr_image.
tiles[it].
offset_y) * tile_size_y + j;
6527 const size_t idx = ii + jj *
static_cast<size_t>(exr_image.
width);
6530 if (ii >=
static_cast<size_t>(exr_image.
width)) {
6533 if (jj >=
static_cast<size_t>(exr_image.
height)) {
6536 const size_t srcIdx = i + j * tile_size_x;
6538 (*out_rgba)[4 * idx + 0] =
reinterpret_cast<float**
>(src)[0][srcIdx];
6539 (*out_rgba)[4 * idx + 1] =
reinterpret_cast<float**
>(src)[0][srcIdx];
6540 (*out_rgba)[4 * idx + 2] =
reinterpret_cast<float**
>(src)[0][srcIdx];
6541 (*out_rgba)[4 * idx + 3] =
reinterpret_cast<float**
>(src)[0][srcIdx];
6546 const size_t pixel_size =
static_cast<size_t>(exr_image.
width) *
static_cast<size_t>(exr_image.
height);
6547 for (
size_t i = 0; i < pixel_size; i++) {
6548 const float val =
reinterpret_cast<float**
>(exr_image.
images)[0][i];
6549 (*out_rgba)[4 * i + 0] = val;
6550 (*out_rgba)[4 * i + 1] = val;
6551 (*out_rgba)[4 * i + 2] = val;
6552 (*out_rgba)[4 * i + 3] = val;
6560 tinyexr::SetErrorMessage(
"R channel not found", err);
6567 tinyexr::SetErrorMessage(
"G channel not found", err);
6573 tinyexr::SetErrorMessage(
"B channel not found", err);
6578 (*out_rgba) =
reinterpret_cast<float*
>(
6579 malloc(4 *
sizeof(
float) *
static_cast<size_t>(exr_image.
width) *
static_cast<size_t>(exr_image.
height)));
6581 if (exr_header.
tiled) {
6582 const size_t tile_size_x =
static_cast<size_t>(exr_header.
tile_size_x);
6583 const size_t tile_size_y =
static_cast<size_t>(exr_header.
tile_size_y);
6584 for (
int it = 0; it < exr_image.
num_tiles; it++) {
6585 for (
size_t j = 0; j < tile_size_y; j++)
6586 for (
size_t i = 0; i < tile_size_x; i++) {
6587 const size_t ii =
static_cast<size_t>(exr_image.
tiles[it].
offset_x) * tile_size_x + i;
6588 const size_t jj =
static_cast<size_t>(exr_image.
tiles[it].
offset_y) * tile_size_y + j;
6589 const size_t idx = ii + jj *
static_cast<size_t>(exr_image.
width);
6592 if (ii >=
static_cast<size_t>(exr_image.
width)) {
6595 if (jj >=
static_cast<size_t>(exr_image.
height)) {
6598 const size_t srcIdx = i + j * tile_size_x;
6600 (*out_rgba)[4 * idx + 0] =
reinterpret_cast<float**
>(src)[idxR][srcIdx];
6601 (*out_rgba)[4 * idx + 1] =
reinterpret_cast<float**
>(src)[idxG][srcIdx];
6602 (*out_rgba)[4 * idx + 2] =
reinterpret_cast<float**
>(src)[idxB][srcIdx];
6604 (*out_rgba)[4 * idx + 3] =
reinterpret_cast<float**
>(src)[idxA][srcIdx];
6606 (*out_rgba)[4 * idx + 3] = 1.0;
6611 const size_t pixel_size =
static_cast<size_t>(exr_image.
width) *
static_cast<size_t>(exr_image.
height);
6612 for (
size_t i = 0; i < pixel_size; i++) {
6613 (*out_rgba)[4 * i + 0] =
reinterpret_cast<float**
>(exr_image.
images)[idxR][i];
6614 (*out_rgba)[4 * i + 1] =
reinterpret_cast<float**
>(exr_image.
images)[idxG][i];
6615 (*out_rgba)[4 * i + 2] =
reinterpret_cast<float**
>(exr_image.
images)[idxB][i];
6617 (*out_rgba)[4 * i + 3] =
reinterpret_cast<float**
>(exr_image.
images)[idxA][i];
6619 (*out_rgba)[4 * i + 3] = 1.0;
6625 (*width) = exr_image.
width;
6626 (*height) = exr_image.
height;
6637struct MemoryMappedFile {
6638 unsigned char* data;
6640#ifdef TINYEXR_USE_WIN32_MMAP
6641 HANDLE windows_file;
6642 HANDLE windows_file_mapping;
6643#elif defined(TINYEXR_USE_POSIX_MMAP)
6644 int posix_descriptor;
6650 MemoryMappedFile(
const char*
filename)
6654#ifdef TINYEXR_USE_WIN32_MMAP
6655 windows_file_mapping = NULL;
6656 windows_file = CreateFileW(tinyexr::UTF8ToWchar(
filename).c_str(),
6661 FILE_ATTRIBUTE_READONLY,
6663 if (windows_file == INVALID_HANDLE_VALUE) {
6667 windows_file_mapping = CreateFileMapping(windows_file,
6673 if (windows_file_mapping == NULL) {
6677 data =
reinterpret_cast<unsigned char*
>(
6678 MapViewOfFile(windows_file_mapping,
6687 LARGE_INTEGER windows_file_size = { };
6688 if (!GetFileSizeEx(windows_file, &windows_file_size) ||
static_cast<ULONGLONG
>(windows_file_size.QuadPart) > std::numeric_limits<size_t>::max()) {
6689 UnmapViewOfFile(data);
6693 size =
static_cast<size_t>(windows_file_size.QuadPart);
6694#elif defined(TINYEXR_USE_POSIX_MMAP)
6695 posix_descriptor = open(
filename, O_RDONLY);
6696 if (posix_descriptor == -1) {
6701 if (fstat(posix_descriptor, &info) < 0) {
6711#pragma clang diagnostic push
6712#pragma clang diagnostic ignored "-Wtautological-type-limit-compare"
6714 if (info.st_size < 0 || info.st_size > std::numeric_limits<ssize_t>::max()) {
6718#pragma clang diagnostic pop
6720 size =
static_cast<size_t>(info.st_size);
6722 data =
reinterpret_cast<unsigned char*
>(
6723 mmap(0,
size, PROT_READ, MAP_SHARED, posix_descriptor, 0));
6724 if (data == MAP_FAILED) {
6737 if (fseek(fp, 0, SEEK_END) != 0) {
6741 const long ftell_result = ftell(fp);
6742 if (ftell_result < 0) {
6747 size =
static_cast<size_t>(ftell_result);
6748 if (fseek(fp, 0, SEEK_SET) != 0) {
6754 data =
reinterpret_cast<unsigned char*
>(malloc(
size));
6760 size_t read_bytes = fread(data, 1,
size, fp);
6761 if (read_bytes !=
size) {
6775#ifdef TINYEXR_USE_WIN32_MMAP
6777 (void)UnmapViewOfFile(data);
6781 if (windows_file_mapping != NULL) {
6782 (void)CloseHandle(windows_file_mapping);
6785 if (windows_file != INVALID_HANDLE_VALUE) {
6786 (void)CloseHandle(windows_file);
6788#elif defined(TINYEXR_USE_POSIX_MMAP)
6790 (void)munmap(data,
size);
6794 if (posix_descriptor != -1) {
6795 (void)close(posix_descriptor);
6808#if TINYEXR_HAS_CXX11
6810#pragma clang diagnostic push
6811#pragma clang diagnostic ignored "-Wc++98-compat"
6813 MemoryMappedFile(
const MemoryMappedFile&) =
delete;
6814 MemoryMappedFile& operator=(
const MemoryMappedFile&) =
delete;
6815 MemoryMappedFile(MemoryMappedFile&& other)
noexcept =
delete;
6816 MemoryMappedFile& operator=(MemoryMappedFile&& other)
noexcept =
delete;
6818#pragma clang diagnostic pop
6823 bool valid()
const {
return data; }
6827 const char*
filename,
const char** err)
6829 if (exr_image == NULL) {
6830 tinyexr::SetErrorMessage(
"Invalid argument for LoadEXRImageFromFile", err);
6835 if (!file.valid()) {
6836 tinyexr::SetErrorMessage(
"Cannot read file " + std::string(
filename), err);
6840 if (file.size < 16) {
6841 tinyexr::SetErrorMessage(
"File size too short : " + std::string(
filename),
6851 const unsigned char* memory,
const size_t size,
6854 if (exr_image == NULL || memory == NULL || (
size < tinyexr::kEXRVersionSize)) {
6855 tinyexr::SetErrorMessage(
"Invalid argument for LoadEXRImageFromMemory",
6861 tinyexr::SetErrorMessage(
"EXRHeader variable is not initialized.", err);
6865 const unsigned char* head = memory;
6866 const unsigned char* marker =
reinterpret_cast<const unsigned char*
>(
6868 return tinyexr::DecodeEXRImage(exr_image, exr_header, head, marker,
size,
6875#pragma clang diagnostic push
6876#pragma clang diagnostic ignored "-Wsign-conversion"
6881static bool EncodePixelData( std::vector<unsigned char>& out_data,
6882 const unsigned char*
const* images,
6883 int compression_type,
6890 size_t pixel_data_size,
6891 const std::vector<ChannelInfo>& channels,
6892 const std::vector<size_t>& channel_offset_list,
6894 const void* compression_param = 0)
6896 size_t buf_size =
static_cast<size_t>(width) *
static_cast<size_t>(num_lines) *
static_cast<size_t>(pixel_data_size);
6900 std::vector<unsigned char>
buf(buf_size);
6902 size_t start_y =
static_cast<size_t>(line_no);
6903 for (
size_t c = 0; c < channels.size(); c++) {
6906 for (
int y = 0;
y < num_lines;
y++) {
6908 float* line_ptr =
reinterpret_cast<float*
>(&
buf.at(
6909 static_cast<size_t>(pixel_data_size *
size_t(
y) *
size_t(width)) + channel_offset_list[c] *
static_cast<size_t>(width)));
6910 for (
int x = 0;
x < width;
x++) {
6912 h16.u =
reinterpret_cast<const unsigned short* const*
>(
6913 images)[c][(
y + start_y) * size_t(x_stride) + size_t(
x)];
6915 tinyexr::FP32 f32 = half_to_float(h16);
6917 tinyexr::swap4(&f32.f);
6920 tinyexr::cpy4(line_ptr +
x, &(f32.f));
6924 for (
int y = 0;
y < num_lines;
y++) {
6926 unsigned short* line_ptr =
reinterpret_cast<unsigned short*
>(
6927 &
buf.at(
static_cast<size_t>(pixel_data_size *
y * width) + channel_offset_list[c] *
static_cast<size_t>(width)));
6928 for (
int x = 0;
x < width;
x++) {
6929 unsigned short val =
reinterpret_cast<const unsigned short* const*
>(
6930 images)[c][(
y + start_y) * x_stride +
x];
6932 tinyexr::swap2(&val);
6935 tinyexr::cpy2(line_ptr +
x, &val);
6940 (*err) +=
"Invalid requested_pixel_type.\n";
6947 for (
int y = 0;
y < num_lines;
y++) {
6949 unsigned short* line_ptr =
reinterpret_cast<unsigned short*
>(
6950 &
buf.at(
static_cast<size_t>(pixel_data_size *
y * width) + channel_offset_list[c] *
static_cast<size_t>(width)));
6951 for (
int x = 0;
x < width;
x++) {
6953 f32.f =
reinterpret_cast<const float* const*
>(
6954 images)[c][(
y + start_y) * x_stride +
x];
6957 h16 = float_to_half_full(f32);
6959 tinyexr::swap2(
reinterpret_cast<unsigned short*
>(&h16.u));
6962 tinyexr::cpy2(line_ptr +
x, &(h16.u));
6966 for (
int y = 0;
y < num_lines;
y++) {
6968 float* line_ptr =
reinterpret_cast<float*
>(&
buf.at(
6969 static_cast<size_t>(pixel_data_size *
y * width) + channel_offset_list[c] *
static_cast<size_t>(width)));
6970 for (
int x = 0;
x < width;
x++) {
6971 float val =
reinterpret_cast<const float* const*
>(
6972 images)[c][(
y + start_y) * x_stride +
x];
6974 tinyexr::swap4(&val);
6977 tinyexr::cpy4(line_ptr +
x, &val);
6982 (*err) +=
"Invalid requested_pixel_type.\n";
6987 for (
int y = 0;
y < num_lines;
y++) {
6989 unsigned int* line_ptr =
reinterpret_cast<unsigned int*
>(&
buf.at(
6990 static_cast<size_t>(pixel_data_size *
y * width) + channel_offset_list[c] *
static_cast<size_t>(width)));
6991 for (
int x = 0;
x < width;
x++) {
6992 unsigned int val =
reinterpret_cast<const unsigned int* const*
>(
6993 images)[c][(
y + start_y) * x_stride +
x];
6995 tinyexr::swap4(&val);
6998 tinyexr::cpy4(line_ptr +
x, &val);
7008 out_data.insert(out_data.end(),
buf.begin(),
buf.end());
7011#if defined(TINYEXR_USE_MINIZ) && (TINYEXR_USE_MINIZ == 1)
7013 static_cast<unsigned long>(
buf.size())));
7014#elif TINYEXR_USE_STB_ZLIB
7017 std::vector<unsigned char> block(256 + 2 *
buf.size());
7018#elif defined(TINYEXR_USE_NANOZLIB) && (TINYEXR_USE_NANOZLIB == 1)
7019 std::vector<unsigned char> block(nanoz_compressBound(
7020 static_cast<unsigned long>(
buf.size())));
7022 std::vector<unsigned char> block(
7025 tinyexr::tinyexr_uint64 outSize = block.size();
7027 if (!tinyexr::CompressZip(&block.at(0), outSize,
7028 reinterpret_cast<const unsigned char*
>(&
buf.at(0)),
7029 static_cast<unsigned long>(
buf.size()))) {
7031 (*err) +=
"Zip compresssion failed.\n";
7039 unsigned int data_len =
static_cast<unsigned int>(outSize);
7041 out_data.insert(out_data.end(), block.begin(), block.begin() + data_len);
7045 std::vector<unsigned char> block((
buf.size() * 3) / 2);
7047 tinyexr::tinyexr_uint64 outSize = block.size();
7049 if (!tinyexr::CompressRle(&block.at(0), outSize,
7050 reinterpret_cast<const unsigned char*
>(&
buf.at(0)),
7051 static_cast<unsigned long>(
buf.size()))) {
7053 (*err) +=
"RLE compresssion failed.\n";
7061 unsigned int data_len =
static_cast<unsigned int>(outSize);
7062 out_data.insert(out_data.end(), block.begin(), block.begin() + data_len);
7066 unsigned int bufLen = 8192 +
static_cast<unsigned int>(2 *
static_cast<unsigned int>(
buf.size()));
7067 std::vector<unsigned char> block(bufLen);
7068 unsigned int outSize =
static_cast<unsigned int>(block.size());
7070 if (!CompressPiz(&block.at(0), &outSize,
7071 reinterpret_cast<const unsigned char*
>(&
buf.at(0)),
7072 buf.size(), channels, width, num_lines)) {
7074 (*err) +=
"PIZ compresssion failed.\n";
7082 unsigned int data_len = outSize;
7083 out_data.insert(out_data.end(), block.begin(), block.begin() + data_len);
7087 (*err) +=
"PIZ compression is disabled in this build.\n";
7093 const ZFPCompressionParam* zfp_compression_param =
reinterpret_cast<const ZFPCompressionParam*
>(compression_param);
7094 std::vector<unsigned char> block;
7095 unsigned int outSize;
7097 tinyexr::CompressZfp(
7098 &block, &outSize,
reinterpret_cast<const float*
>(&
buf.at(0)),
7099 width, num_lines,
static_cast<int>(channels.size()), *zfp_compression_param);
7104 unsigned int data_len = outSize;
7105 out_data.insert(out_data.end(), block.begin(), block.begin() + data_len);
7109 (*err) +=
"ZFP compression is disabled in this build.\n";
7111 (void)compression_param;
7121static int EncodeTiledLevel(
const EXRImage* level_image,
const EXRHeader* exr_header,
7122 const std::vector<tinyexr::ChannelInfo>& channels,
7123 std::vector<std::vector<unsigned char>>& data_list,
7125 int num_x_tiles,
int num_y_tiles,
7126 const std::vector<size_t>& channel_offset_list,
7127 int pixel_data_size,
7128 const void* compression_param,
7131 int num_tiles = num_x_tiles * num_y_tiles;
7132 if (num_tiles != level_image->
num_tiles) {
7134 (*err) +=
"Invalid number of tiles in argument.\n";
7141 (*err) +=
"Failed to encode tile data.\n";
7146#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
7147 std::atomic<bool> invalid_data(
false);
7149 bool invalid_data(
false);
7152#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
7153 std::vector<std::thread> workers;
7154 std::atomic<int> tile_count(0);
7156 int num_threads = std::max(1,
int(std::thread::hardware_concurrency()));
7157 if (num_threads >
int(num_tiles)) {
7158 num_threads =
int(num_tiles);
7161 for (
int t = 0; t < num_threads; t++) {
7162 workers.emplace_back(std::thread([&]() {
7164 while ((i = tile_count++) < num_tiles) {
7169#if TINYEXR_USE_OPENMP
7170#pragma omp parallel for
7172 for (
int i = 0; i < num_tiles; i++) {
7175 size_t tile_idx =
static_cast<size_t>(i);
7176 size_t data_idx = tile_idx + start_index;
7178 int x_tile = i % num_x_tiles;
7179 int y_tile = i / num_x_tiles;
7183 const unsigned char*
const* images =
static_cast<const unsigned char* const*
>(tile.
images);
7185 data_list[data_idx].resize(5 *
sizeof(
int));
7186 size_t data_header_size = data_list[data_idx].size();
7187 bool ret = EncodePixelData(data_list[data_idx],
7198 channel_offset_list,
7199 err, compression_param);
7201 invalid_data =
true;
7204 if (data_list[data_idx].
size() <= data_header_size) {
7205 invalid_data =
true;
7209 int data_len =
static_cast<int>(data_list[data_idx].size() - data_header_size);
7211 memcpy(&data_list[data_idx][0], &x_tile,
sizeof(
int));
7212 memcpy(&data_list[data_idx][4], &y_tile,
sizeof(
int));
7213 memcpy(&data_list[data_idx][8], &level_image->
level_x,
sizeof(
int));
7214 memcpy(&data_list[data_idx][12], &level_image->
level_y,
sizeof(
int));
7215 memcpy(&data_list[data_idx][16], &data_len,
sizeof(
int));
7217 swap4(
reinterpret_cast<int*
>(&data_list[data_idx][0]));
7218 swap4(
reinterpret_cast<int*
>(&data_list[data_idx][4]));
7219 swap4(
reinterpret_cast<int*
>(&data_list[data_idx][8]));
7220 swap4(
reinterpret_cast<int*
>(&data_list[data_idx][12]));
7221 swap4(
reinterpret_cast<int*
>(&data_list[data_idx][16]));
7223#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
7228 for (
auto& t : workers) {
7237 (*err) +=
"Failed to encode tile data.\n";
7244static int NumScanlines(
int compression_type)
7246 int num_scanlines = 1;
7254 return num_scanlines;
7258 const std::vector<ChannelInfo>& channels,
7260 tinyexr_uint64 chunk_offset,
7262 OffsetData& offset_data,
7263 std::vector<std::vector<unsigned char>>& data_list,
7264 tinyexr_uint64& total_size,
7269 data_list.resize(num_blocks);
7271 std::vector<size_t> channel_offset_list(
7274 int pixel_data_size = 0;
7276 size_t channel_offset = 0;
7277 for (
size_t c = 0; c < static_cast<size_t>(exr_header->
num_channels); c++) {
7278 channel_offset_list[c] = channel_offset;
7280 pixel_data_size +=
sizeof(
unsigned short);
7281 channel_offset +=
sizeof(
unsigned short);
7283 pixel_data_size +=
sizeof(float);
7284 channel_offset +=
sizeof(float);
7286 pixel_data_size +=
sizeof(
unsigned int);
7287 channel_offset +=
sizeof(
unsigned int);
7290 (*err) +=
"Invalid requested_pixel_type.\n";
7297 const void* compression_param = 0;
7299 tinyexr::ZFPCompressionParam zfp_compression_param;
7305 bool ret = tinyexr::FindZFPCompressionParam(
7311 zfp_compression_param.type = 0;
7312 zfp_compression_param.rate = 2;
7314 compression_param = &zfp_compression_param;
7318 tinyexr_uint64
offset = chunk_offset;
7319 tinyexr_uint64 doffset = is_multipart ? 4u : 0u;
7321 if (exr_image->
tiles) {
7322 const EXRImage* level_image = exr_image;
7323 size_t block_idx = 0;
7326 for (
int level_index = 0; level_index < num_levels; ++level_index) {
7329 (*err) +=
"Invalid number of tiled levels for EncodeChunk\n";
7334 int level_index_from_image = LevelIndex(level_image->
level_x, level_image->
level_y,
7336 if (level_index_from_image < 0) {
7338 (*err) +=
"Invalid tile level mode\n";
7343 if (level_index_from_image != level_index) {
7345 (*err) +=
"Incorrect level ordering in tiled image\n";
7349 int num_y_tiles =
int(offset_data.offsets[level_index].size());
7350 if (num_y_tiles <= 0) {
7352 (*err) +=
"Invalid Y tile size\n";
7357 int num_x_tiles =
int(offset_data.offsets[level_index][0].size());
7358 if (num_x_tiles <= 0) {
7360 (*err) +=
"Invalid X tile size\n";
7366 int ret = EncodeTiledLevel(level_image,
7373 channel_offset_list,
7378 if (!e.empty() && err) {
7384 for (
size_t j = 0; j < static_cast<size_t>(num_y_tiles); ++j)
7385 for (
size_t i = 0; i < static_cast<size_t>(num_x_tiles); ++i) {
7386 offset_data.offsets[level_index][j][i] =
offset;
7387 swap8(
reinterpret_cast<tinyexr_uint64*
>(&offset_data.offsets[level_index][j][i]));
7388 offset += data_list[block_idx].size() + doffset;
7397 std::vector<tinyexr::tinyexr_uint64>& offsets = offset_data.offsets[0][0];
7399#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
7400 std::atomic<bool> invalid_data(
false);
7401 std::vector<std::thread> workers;
7402 std::atomic<int> block_count(0);
7404 int num_threads = std::min(std::max(1,
int(std::thread::hardware_concurrency())), num_blocks);
7406 for (
int t = 0; t < num_threads; t++) {
7407 workers.emplace_back(std::thread([&]() {
7409 while ((i = block_count++) < num_blocks) {
7412 bool invalid_data(
false);
7413#if TINYEXR_USE_OPENMP
7414#pragma omp parallel for
7416 for (
int i = 0; i < num_blocks; i++) {
7419 int start_y = num_scanlines * i;
7420 int end_Y = (std::min)(num_scanlines * (i + 1), exr_image->
height);
7421 int num_lines = end_Y - start_y;
7423 const unsigned char*
const* images =
static_cast<const unsigned char* const*
>(exr_image->
images);
7425 data_list[i].resize(2 *
sizeof(
int));
7426 size_t data_header_size = data_list[i].size();
7428 bool ret = EncodePixelData(data_list[i],
7439 channel_offset_list,
7443 invalid_data =
true;
7446 if (data_list[i].
size() <= data_header_size) {
7447 invalid_data =
true;
7450 int data_len =
static_cast<int>(data_list[i].size() - data_header_size);
7451 memcpy(&data_list[i][0], &start_y,
sizeof(
int));
7452 memcpy(&data_list[i][4], &data_len,
sizeof(
int));
7454 swap4(
reinterpret_cast<int*
>(&data_list[i][0]));
7455 swap4(
reinterpret_cast<int*
>(&data_list[i][4]));
7456#if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
7461 for (
auto& t : workers) {
7470 (*err) +=
"Failed to encode scanline data.\n";
7475 for (
size_t i = 0; i < static_cast<size_t>(num_blocks); i++) {
7477 tinyexr::swap8(
reinterpret_cast<tinyexr::tinyexr_uint64*
>(&offsets[i]));
7478 offset += data_list[i].size() + doffset;
7481 total_size =
static_cast<size_t>(
offset);
7487static size_t SaveEXRNPartImageToMemory(
const EXRImage* exr_images,
7489 unsigned int num_parts,
7490 unsigned char** memory_out,
const char** err)
7492 if (exr_images == NULL || exr_headers == NULL || num_parts == 0 || memory_out == NULL) {
7493 SetErrorMessage(
"Invalid argument for SaveEXRNPartImageToMemory",
7498 for (
unsigned int i = 0; i < num_parts; ++i) {
7499 if (exr_headers[i]->compression_type < 0) {
7500 SetErrorMessage(
"Invalid argument for SaveEXRNPartImageToMemory",
7506 SetErrorMessage(
"PIZ compression is not supported in this build",
7513 SetErrorMessage(
"ZFP compression is not supported in this build",
7520 for (
int c = 0; c < exr_headers[i]->
num_channels; ++c) {
7522 SetErrorMessage(
"Pixel type must be FLOAT for ZFP compression",
7532 std::vector<unsigned char> memory;
7536 const char header[] = { 0x76, 0x2f, 0x31, 0x01 };
7537 memory.insert(memory.end(), header, header + 4);
7542 int long_name = exr_headers[0]->
long_name;
7544 char marker[] = { 2, 0, 0, 0 };
7551 if (num_parts == 1 && exr_images[0].tiles) {
7559 if (num_parts > 1) {
7562 memory.insert(memory.end(), marker, marker + 4);
7565 int total_chunk_count = 0;
7566 std::vector<int> chunk_count(num_parts);
7567 std::vector<OffsetData> offset_data(num_parts);
7568 for (
unsigned int i = 0; i < num_parts; ++i) {
7569 if (!exr_images[i].tiles) {
7570 int num_scanlines = NumScanlines(exr_headers[i]->compression_type);
7571 chunk_count[i] = (exr_images[i].
height + num_scanlines - 1) / num_scanlines;
7572 InitSingleResolutionOffsets(offset_data[i], chunk_count[i]);
7573 total_chunk_count += chunk_count[i];
7576 std::vector<int> num_x_tiles, num_y_tiles;
7577 if (!PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_headers[i])) {
7578 SetErrorMessage(
"Failed to precalculate Tile info",
7582 int ntiles = InitTileOffsets(offset_data[i], exr_headers[i], num_x_tiles, num_y_tiles);
7584 chunk_count[i] = ntiles;
7586 SetErrorMessage(
"Failed to compute Tile offsets",
7590 total_chunk_count += chunk_count[i];
7595 std::vector<std::vector<tinyexr::ChannelInfo>> channels(num_parts);
7597 std::set<std::string> partnames;
7598 for (
unsigned int i = 0; i < num_parts; ++i) {
7601 std::vector<unsigned char> data;
7603 for (
int c = 0; c < exr_headers[i]->
num_channels; c++) {
7604 tinyexr::ChannelInfo info;
7608 info.x_sampling = 1;
7609 info.y_sampling = 1;
7610 info.name = std::string(exr_headers[i]->channels[c].name);
7611 channels[i].push_back(info);
7614 tinyexr::WriteChannelInfo(data, channels[i]);
7616 tinyexr::WriteAttributeToMemory(&memory,
"channels",
"chlist", &data.at(0),
7617 static_cast<int>(data.size()));
7623 WriteAttributeToMemory(
7624 &memory,
"compression",
"compression",
7625 reinterpret_cast<const unsigned char*
>(&comp), 1);
7629 int data[4] = { 0, 0, exr_images[i].
width - 1, exr_images[i].
height - 1 };
7634 WriteAttributeToMemory(
7635 &memory,
"dataWindow",
"box2i",
7636 reinterpret_cast<const unsigned char*
>(data),
sizeof(
int) * 4);
7638 int data0[4] = { 0, 0, exr_images[0].
width - 1, exr_images[0].
height - 1 };
7644 WriteAttributeToMemory(
7645 &memory,
"displayWindow",
"box2i",
7646 reinterpret_cast<const unsigned char*
>(data0),
sizeof(
int) * 4);
7650 unsigned char line_order = 0;
7651 WriteAttributeToMemory(&memory,
"lineOrder",
"lineOrder",
7657 float aspectRatio = 1.0f;
7658 swap4(&aspectRatio);
7659 WriteAttributeToMemory(
7660 &memory,
"pixelAspectRatio",
"float",
7661 reinterpret_cast<const unsigned char*
>(&aspectRatio),
sizeof(
float));
7665 float center[2] = { 0.0f, 0.0f };
7668 WriteAttributeToMemory(
7669 &memory,
"screenWindowCenter",
"v2f",
7670 reinterpret_cast<const unsigned char*
>(center), 2 *
sizeof(
float));
7676 WriteAttributeToMemory(&memory,
"screenWindowWidth",
"float",
7677 reinterpret_cast<const unsigned char*
>(&w),
7681 if (exr_images[i].tiles) {
7682 unsigned char tile_mode =
static_cast<unsigned char>(exr_headers[i]->
tile_level_mode & 0x3);
7683 if (exr_headers[i]->tile_rounding_mode)
7684 tile_mode |= (1u << 4u);
7686 unsigned int datai[3] = { 0, 0, 0 };
7687 unsigned char* data =
reinterpret_cast<unsigned char*
>(&datai[0]);
7688 datai[0] =
static_cast<unsigned int>(exr_headers[i]->
tile_size_x);
7689 datai[1] =
static_cast<unsigned int>(exr_headers[i]->
tile_size_y);
7690 data[8] = tile_mode;
7691 swap4(
reinterpret_cast<unsigned int*
>(&data[0]));
7692 swap4(
reinterpret_cast<unsigned int*
>(&data[4]));
7693 WriteAttributeToMemory(
7694 &memory,
"tiles",
"tiledesc",
7695 reinterpret_cast<const unsigned char*
>(data), 9);
7699 if (num_parts > 1) {
7703 if ((len = strlen(exr_headers[i]->name)) > 0) {
7704#if TINYEXR_HAS_CXX11
7705 partnames.emplace(exr_headers[i]->name);
7707 partnames.insert(std::string(exr_headers[i]->name));
7709 if (partnames.size() != i + 1) {
7710 SetErrorMessage(
"'name' attributes must be unique for a multi-part file", err);
7713 WriteAttributeToMemory(
7714 &memory,
"name",
"string",
7715 reinterpret_cast<const unsigned char*
>(exr_headers[i]->name),
7716 static_cast<int>(len));
7718 SetErrorMessage(
"Invalid 'name' attribute for a multi-part file", err);
7724 const char* type =
"scanlineimage";
7725 if (exr_images[i].tiles)
7726 type =
"tiledimage";
7727 WriteAttributeToMemory(
7728 &memory,
"type",
"string",
7729 reinterpret_cast<const unsigned char*
>(type),
7730 static_cast<int>(strlen(type)));
7734 WriteAttributeToMemory(
7735 &memory,
"chunkCount",
"int",
7736 reinterpret_cast<const unsigned char*
>(&chunk_count[i]),
7742 if (exr_headers[i]->num_custom_attributes > 0) {
7744 tinyexr::WriteAttributeToMemory(
7745 &memory, exr_headers[i]->custom_attributes[j].name,
7746 exr_headers[i]->custom_attributes[j].type,
7747 reinterpret_cast<const unsigned char*
>(
7748 exr_headers[i]->custom_attributes[j].value),
7749 exr_headers[i]->custom_attributes[j].
size);
7754 memory.push_back(0);
7758 if (num_parts > 1) {
7760 memory.push_back(0);
7763 tinyexr_uint64 chunk_offset = memory.size() + size_t(total_chunk_count) *
sizeof(tinyexr_uint64);
7765 tinyexr_uint64 total_size = 0;
7766 std::vector<std::vector<std::vector<unsigned char>>> data_lists(num_parts);
7767 for (
unsigned int i = 0; i < num_parts; ++i) {
7769 int ret = EncodeChunk(&exr_images[i], exr_headers[i],
7781 tinyexr::SetErrorMessage(e, err);
7785 chunk_offset = total_size;
7789 if (total_size == 0) {
7790 tinyexr::SetErrorMessage(
"Output memory size is zero", err);
7793 (*memory_out) =
static_cast<unsigned char*
>(malloc(
size_t(total_size)));
7796 memcpy((*memory_out), &memory[0], memory.size());
7797 unsigned char* memory_ptr = *memory_out + memory.size();
7798 size_t sum = memory.size();
7801 for (
unsigned int i = 0; i < num_parts; ++i) {
7802 if (exr_images[i].tiles) {
7803 const EXRImage* level_image = &exr_images[i];
7805 for (
int level_index = 0; level_index < num_levels; ++level_index) {
7806 for (
size_t j = 0; j < offset_data[i].offsets[level_index].size(); ++j) {
7807 size_t num_bytes =
sizeof(tinyexr_uint64) * offset_data[i].offsets[level_index][j].
size();
7809 if (sum > total_size) {
7810 tinyexr::SetErrorMessage(
"Invalid offset bytes in Tiled Part image.", err);
7815 reinterpret_cast<unsigned char*
>(&offset_data[i].offsets[level_index][j][0]),
7817 memory_ptr += num_bytes;
7822 size_t num_bytes =
sizeof(tinyexr::tinyexr_uint64) *
static_cast<size_t>(chunk_count[i]);
7824 if (sum > total_size) {
7825 tinyexr::SetErrorMessage(
"Invalid offset bytes in Part image.", err);
7828 std::vector<tinyexr::tinyexr_uint64>& offsets = offset_data[i].offsets[0][0];
7829 memcpy(memory_ptr,
reinterpret_cast<unsigned char*
>(&offsets[0]), num_bytes);
7830 memory_ptr += num_bytes;
7835 for (
unsigned int i = 0; i < num_parts; ++i) {
7836 for (
size_t j = 0; j < static_cast<size_t>(chunk_count[i]); ++j) {
7837 if (num_parts > 1) {
7839 if (sum > total_size) {
7840 tinyexr::SetErrorMessage(
"Buffer overrun in reading Part image chunk data.", err);
7843 unsigned int part_number = i;
7844 swap4(&part_number);
7845 memcpy(memory_ptr, &part_number, 4);
7848 sum += data_lists[i][j].size();
7849 if (sum > total_size) {
7850 tinyexr::SetErrorMessage(
"Buffer overrun in reading Part image chunk data.", err);
7853 memcpy(memory_ptr, &data_lists[i][j][0], data_lists[i][j].
size());
7854 memory_ptr += data_lists[i][j].size();
7858 if (sum != total_size) {
7859 tinyexr::SetErrorMessage(
"Corrupted Part image chunk data.", err);
7863 return size_t(total_size);
7867#pragma clang diagnostic pop
7874 unsigned char** memory_out,
const char** err)
7876 return tinyexr::SaveEXRNPartImageToMemory(exr_image, &exr_header, 1, memory_out, err);
7880 const char*
filename,
const char** err)
7883 tinyexr::SetErrorMessage(
"Invalid argument for SaveEXRImageToFile", err);
7889 tinyexr::SetErrorMessage(
"PIZ compression is not supported in this build",
7897 tinyexr::SetErrorMessage(
"ZFP compression is not supported in this build",
7905#if defined(_MSC_VER) || (defined(MINGW_HAS_SECURE_API) && MINGW_HAS_SECURE_API)
7906 errno_t errcode = _wfopen_s(&fp, tinyexr::UTF8ToWchar(
filename).c_str(), L
"wb");
7908 tinyexr::SetErrorMessage(
"Cannot write a file: " + std::string(
filename),
7920 tinyexr::SetErrorMessage(
"Cannot write a file: " + std::string(
filename),
7925 unsigned char* mem = NULL;
7927 if (mem_size == 0) {
7932 size_t written_size = 0;
7933 if ((mem_size > 0) && mem) {
7934 written_size = fwrite(mem, 1, mem_size, fp);
7940 if (written_size != mem_size) {
7941 tinyexr::SetErrorMessage(
"Cannot write a file", err);
7950 unsigned int num_parts,
7951 unsigned char** memory_out,
const char** err)
7953 if (exr_images == NULL || exr_headers == NULL || num_parts < 2 || memory_out == NULL) {
7954 tinyexr::SetErrorMessage(
"Invalid argument for SaveEXRNPartImageToMemory",
7958 return tinyexr::SaveEXRNPartImageToMemory(exr_images, exr_headers, num_parts, memory_out, err);
7963 unsigned int num_parts,
7967 if (exr_images == NULL || exr_headers == NULL || num_parts < 2) {
7968 tinyexr::SetErrorMessage(
"Invalid argument for SaveEXRMultipartImageToFile",
7975#if defined(_MSC_VER) || (defined(MINGW_HAS_SECURE_API) && MINGW_HAS_SECURE_API)
7976 errno_t errcode = _wfopen_s(&fp, tinyexr::UTF8ToWchar(
filename).c_str(), L
"wb");
7978 tinyexr::SetErrorMessage(
"Cannot write a file: " + std::string(
filename),
7990 tinyexr::SetErrorMessage(
"Cannot write a file: " + std::string(
filename),
7995 unsigned char* mem = NULL;
7997 if (mem_size == 0) {
8002 size_t written_size = 0;
8003 if ((mem_size > 0) && mem) {
8004 written_size = fwrite(mem, 1, mem_size, fp);
8010 if (written_size != mem_size) {
8011 tinyexr::SetErrorMessage(
"Cannot write a file", err);
8020 if (deep_image == NULL) {
8021 tinyexr::SetErrorMessage(
"Invalid argument for LoadDeepEXR", err);
8026 if (!file.valid()) {
8027 tinyexr::SetErrorMessage(
"Cannot read file " + std::string(
filename), err);
8031 if (file.size == 0) {
8032 tinyexr::SetErrorMessage(
"File size is zero : " + std::string(
filename),
8037 const char* head =
reinterpret_cast<const char*
>(file.data);
8038 const char* marker =
reinterpret_cast<const char*
>(file.data);
8042 const char header[] = { 0x76, 0x2f, 0x31, 0x01 };
8044 if (memcmp(marker, header, 4) != 0) {
8045 tinyexr::SetErrorMessage(
"Invalid magic number", err);
8055 if (marker[0] != 2 || marker[1] != 8 || marker[2] != 0 || marker[3] != 0) {
8056 tinyexr::SetErrorMessage(
"Unsupported version or scanline", err);
8067 int num_scanline_blocks = 1;
8068 int compression_type = -1;
8069 int num_channels = -1;
8070 std::vector<tinyexr::ChannelInfo> channels;
8073 size_t size = file.size - tinyexr::kEXRVersionSize;
8077 }
else if (marker[0] ==
'\0') {
8083 std::string attr_name;
8084 std::string attr_type;
8085 std::vector<unsigned char> data;
8087 if (!tinyexr::ReadAttribute(&attr_name, &attr_type, &data, &marker_size,
8089 std::stringstream ss;
8090 ss <<
"Failed to parse attribute\n";
8091 tinyexr::SetErrorMessage(ss.str(), err);
8094 marker += marker_size;
8095 size -= marker_size;
8097 if (attr_name.compare(
"compression") == 0) {
8098 compression_type = data[0];
8100 std::stringstream ss;
8101 ss <<
"Unsupported compression type : " << compression_type;
8102 tinyexr::SetErrorMessage(ss.str(), err);
8107 num_scanline_blocks = 16;
8110 }
else if (attr_name.compare(
"channels") == 0) {
8118 if (!tinyexr::ReadChannelInfo(channels, data)) {
8119 tinyexr::SetErrorMessage(
"Failed to parse channel info", err);
8123 num_channels =
static_cast<int>(channels.size());
8125 if (num_channels < 1) {
8126 tinyexr::SetErrorMessage(
"Invalid channels format", err);
8130 }
else if (attr_name.compare(
"dataWindow") == 0) {
8131 memcpy(&dx, &data.at(0),
sizeof(
int));
8132 memcpy(&dy, &data.at(4),
sizeof(
int));
8133 memcpy(&dw, &data.at(8),
sizeof(
int));
8134 memcpy(&dh, &data.at(12),
sizeof(
int));
8135 tinyexr::swap4(&dx);
8136 tinyexr::swap4(&dy);
8137 tinyexr::swap4(&dw);
8138 tinyexr::swap4(&dh);
8140 }
else if (attr_name.compare(
"displayWindow") == 0) {
8145 memcpy(&
x, &data.at(0),
sizeof(
int));
8146 memcpy(&
y, &data.at(4),
sizeof(
int));
8147 memcpy(&w, &data.at(8),
sizeof(
int));
8148 memcpy(&h, &data.at(12),
sizeof(
int));
8162 int data_width = dw - dx + 1;
8163 int data_height = dh - dy + 1;
8166 int num_blocks = data_height / num_scanline_blocks;
8167 if (num_blocks * num_scanline_blocks < data_height) {
8171 std::vector<tinyexr::tinyexr_int64> offsets(
static_cast<size_t>(num_blocks));
8174 tinyexr::tinyexr_int64
offset;
8175 memcpy(&
offset, marker,
sizeof(tinyexr::tinyexr_int64));
8176 tinyexr::swap8(
reinterpret_cast<tinyexr::tinyexr_uint64*
>(&
offset));
8177 marker +=
sizeof(tinyexr::tinyexr_int64);
8188 tinyexr::SetErrorMessage(
"Unsupported compression format", err);
8192 deep_image->
image =
static_cast<float***
>(
8193 malloc(
sizeof(
float**) *
static_cast<size_t>(num_channels)));
8194 for (
int c = 0; c < num_channels; c++) {
8195 deep_image->
image[c] =
static_cast<float**
>(
8196 malloc(
sizeof(
float*) *
static_cast<size_t>(data_height)));
8197 for (
int y = 0;
y < data_height;
y++) {
8202 malloc(
sizeof(
int*) *
static_cast<size_t>(data_height)));
8203 for (
int y = 0;
y < data_height;
y++) {
8205 malloc(
sizeof(
int) *
static_cast<size_t>(data_width)));
8209 const unsigned char* data_ptr =
reinterpret_cast<const unsigned char*
>(head + offsets[
y]);
8218 tinyexr::tinyexr_int64 packedOffsetTableSize;
8219 tinyexr::tinyexr_int64 packedSampleDataSize;
8220 tinyexr::tinyexr_int64 unpackedSampleDataSize;
8221 memcpy(&line_no, data_ptr,
sizeof(
int));
8222 memcpy(&packedOffsetTableSize, data_ptr + 4,
8223 sizeof(tinyexr::tinyexr_int64));
8224 memcpy(&packedSampleDataSize, data_ptr + 12,
8225 sizeof(tinyexr::tinyexr_int64));
8226 memcpy(&unpackedSampleDataSize, data_ptr + 20,
8227 sizeof(tinyexr::tinyexr_int64));
8229 tinyexr::swap4(&line_no);
8231 reinterpret_cast<tinyexr::tinyexr_uint64*
>(&packedOffsetTableSize));
8233 reinterpret_cast<tinyexr::tinyexr_uint64*
>(&packedSampleDataSize));
8235 reinterpret_cast<tinyexr::tinyexr_uint64*
>(&unpackedSampleDataSize));
8237 std::vector<int> pixelOffsetTable(
static_cast<size_t>(data_width));
8241 unsigned long dstLen =
static_cast<unsigned long>(pixelOffsetTable.size() *
sizeof(
int));
8242 if (!tinyexr::DecompressZip(
8243 reinterpret_cast<unsigned char*
>(&pixelOffsetTable.at(0)),
8244 &dstLen, data_ptr + 28,
8245 static_cast<unsigned long>(packedOffsetTableSize))) {
8250 for (
size_t i = 0; i < static_cast<size_t>(data_width); i++) {
8255 std::vector<unsigned char> sample_data(
8256 static_cast<size_t>(unpackedSampleDataSize));
8260 unsigned long dstLen =
static_cast<unsigned long>(unpackedSampleDataSize);
8262 if (!tinyexr::DecompressZip(
8263 reinterpret_cast<unsigned char*
>(&sample_data.at(0)), &dstLen,
8264 data_ptr + 28 + packedOffsetTableSize,
8265 static_cast<unsigned long>(packedSampleDataSize))) {
8273 int sampleSize = -1;
8274 std::vector<int> channel_offset_list(
static_cast<size_t>(num_channels));
8276 int channel_offset = 0;
8277 for (
size_t i = 0; i < static_cast<size_t>(num_channels); i++) {
8278 channel_offset_list[i] = channel_offset;
8280 channel_offset += 4;
8282 channel_offset += 2;
8284 channel_offset += 4;
8286 tinyexr::SetErrorMessage(
"Invalid pixel_type in chnnels.", err);
8290 sampleSize = channel_offset;
8294 TINYEXR_CHECK_AND_RETURN_C(
static_cast<size_t>(
8295 pixelOffsetTable[
static_cast<size_t>(data_width - 1)] * sampleSize)
8296 == sample_data.size(),
8298 int samples_per_line =
static_cast<int>(sample_data.size()) / sampleSize;
8308 tinyexr::tinyexr_uint64 data_offset = 0;
8309 for (
size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
8310 deep_image->
image[c][
y] =
static_cast<float*
>(
8311 malloc(
sizeof(
float) *
static_cast<size_t>(samples_per_line)));
8313 if (channels[c].pixel_type == 0) {
8316 unsigned int* src_ptr =
reinterpret_cast<unsigned int*
>(
8317 &sample_data.at(
size_t(data_offset) +
x *
sizeof(
int)));
8318 tinyexr::cpy4(&ui, src_ptr);
8319 deep_image->
image[c][
y][
x] =
static_cast<float>(ui);
8321 data_offset +=
sizeof(
unsigned int) *
static_cast<size_t>(samples_per_line);
8322 }
else if (channels[c].pixel_type == 1) {
8325 const unsigned short* src_ptr =
reinterpret_cast<unsigned short*
>(
8326 &sample_data.at(
size_t(data_offset) +
x *
sizeof(
short)));
8327 tinyexr::cpy2(&(f16.u), src_ptr);
8328 tinyexr::FP32 f32 = half_to_float(f16);
8329 deep_image->
image[c][
y][
x] = f32.f;
8331 data_offset +=
sizeof(short) *
static_cast<size_t>(samples_per_line);
8335 const float* src_ptr =
reinterpret_cast<float*
>(
8336 &sample_data.at(
size_t(data_offset) +
x *
sizeof(
float)));
8337 tinyexr::cpy4(&f, src_ptr);
8338 deep_image->
image[c][
y][
x] = f;
8340 data_offset +=
sizeof(float) *
static_cast<size_t>(samples_per_line);
8346 deep_image->
width = data_width;
8347 deep_image->
height = data_height;
8350 malloc(
sizeof(
const char*) *
static_cast<size_t>(num_channels)));
8351 for (
size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
8353 deep_image->
channel_names[c] = _strdup(channels[c].name.c_str());
8355 deep_image->
channel_names[c] = strdup(channels[c].name.c_str());
8365 if (exr_image == NULL) {
8369 exr_image->
width = 0;
8373 exr_image->
images = NULL;
8374 exr_image->
tiles = NULL;
8385 free(
reinterpret_cast<void*
>(
const_cast<char*
>(msg)));
8392 if (exr_header == NULL) {
8396 memset(exr_header, 0,
sizeof(
EXRHeader));
8401 if (exr_header == NULL) {
8434 if (exr_header == NULL) {
8437 memset(exr_header->
name, 0, 256);
8439 size_t len = std::min(strlen(name),
size_t(255));
8441 memcpy(exr_header->
name, name, len);
8448 if (exr_image == NULL)
8453 const EXRImage* level_image = exr_image;
8454 while ((level_image = level_image->
next_level))
8461 if (exr_image == NULL) {
8472 free(exr_image->
images[i]);
8480 if (exr_image->
tiles) {
8481 for (
int tid = 0; tid < exr_image->
num_tiles; tid++) {
8491 free(exr_image->
tiles);
8498 const char*
filename,
const char** err)
8500 if (exr_header == NULL || exr_version == NULL ||
filename == NULL) {
8501 tinyexr::SetErrorMessage(
"Invalid argument for ParseEXRHeaderFromFile",
8507 if (!file.valid()) {
8508 tinyexr::SetErrorMessage(
"Cannot read file " + std::string(
filename), err);
8519 const unsigned char* memory,
size_t size,
8522 if (memory == NULL || exr_headers == NULL || num_headers == NULL || exr_version == NULL) {
8524 tinyexr::SetErrorMessage(
8525 "Invalid argument for ParseEXRMultipartHeaderFromMemory", err);
8529 if (
size < tinyexr::kEXRVersionSize) {
8530 tinyexr::SetErrorMessage(
"Data size too short", err);
8534 const unsigned char* marker = memory + tinyexr::kEXRVersionSize;
8535 size_t marker_size =
size - tinyexr::kEXRVersionSize;
8537 std::vector<tinyexr::HeaderInfo> infos;
8540 tinyexr::HeaderInfo info;
8543 std::string err_str;
8544 bool empty_header =
false;
8545 int ret = ParseEXRHeader(&info, &empty_header, exr_version, &err_str,
8546 marker, marker_size);
8551 for (
size_t i = 0; i < info.attributes.size(); i++) {
8552 if (info.attributes[i].value) {
8553 free(info.attributes[i].value);
8557 tinyexr::SetErrorMessage(err_str, err);
8567 if (info.chunk_count == 0) {
8570 for (
size_t i = 0; i < info.attributes.size(); i++) {
8571 if (info.attributes[i].value) {
8572 free(info.attributes[i].value);
8576 tinyexr::SetErrorMessage(
8577 "`chunkCount' attribute is not found in the header.", err);
8581 infos.push_back(info);
8584 marker += info.header_len;
8585 size -= info.header_len;
8589 (*exr_headers) =
static_cast<EXRHeader**
>(malloc(
sizeof(
EXRHeader*) * infos.size()));
8593 for (
size_t i = 0; i < infos.size(); i++) {
8595 memset(exr_header, 0,
sizeof(
EXRHeader));
8599 if (!ConvertHeader(exr_header, infos[i], &warn, &_err)) {
8602 for (
size_t k = 0; k < infos[i].attributes.size(); k++) {
8603 if (infos[i].attributes[k].value) {
8604 free(infos[i].attributes[k].value);
8608 if (!_err.empty()) {
8609 tinyexr::SetErrorMessage(
8618 (*exr_headers)[i] = exr_header;
8621 (*num_headers) =
static_cast<int>(infos.size());
8628 const char*
filename,
const char** err)
8630 if (exr_headers == NULL || num_headers == NULL || exr_version == NULL ||
filename == NULL) {
8631 tinyexr::SetErrorMessage(
8632 "Invalid argument for ParseEXRMultipartHeaderFromFile()", err);
8637 if (!file.valid()) {
8638 tinyexr::SetErrorMessage(
"Cannot read file " + std::string(
filename), err);
8643 exr_headers, num_headers, exr_version, file.data, file.size, err);
8649 if (version == NULL || memory == NULL) {
8653 if (
size < tinyexr::kEXRVersionSize) {
8657 const unsigned char* marker = memory;
8661 const char header[] = { 0x76, 0x2f, 0x31, 0x01 };
8663 if (memcmp(marker, header, 4) != 0) {
8669 version->
tiled =
false;
8677 if (marker[0] != 2) {
8681 if (version == NULL) {
8687 if (marker[1] & 0x2) {
8688 version->
tiled =
true;
8690 if (marker[1] & 0x4) {
8693 if (marker[1] & 0x8) {
8696 if (marker[1] & 0x10) {
8712#if defined(_MSC_VER) || (defined(MINGW_HAS_SECURE_API) && MINGW_HAS_SECURE_API)
8713 errno_t err = _wfopen_s(&fp, tinyexr::UTF8ToWchar(
filename).c_str(), L
"rb");
8733 unsigned char buf[tinyexr::kEXRVersionSize];
8734 size_t ret = fread(&
buf[0], 1, tinyexr::kEXRVersionSize, fp);
8737 if (ret != tinyexr::kEXRVersionSize) {
8746 unsigned int num_parts,
8747 const unsigned char* memory,
8748 const size_t size,
const char** err)
8750 if (exr_images == NULL || exr_headers == NULL || num_parts == 0 || memory == NULL || (
size <= tinyexr::kEXRVersionSize)) {
8751 tinyexr::SetErrorMessage(
8752 "Invalid argument for LoadEXRMultipartImageFromMemory()", err);
8757 size_t total_header_size = 0;
8758 for (
unsigned int i = 0; i < num_parts; i++) {
8759 if (exr_headers[i]->header_len == 0) {
8760 tinyexr::SetErrorMessage(
"EXRHeader variable is not initialized.", err);
8764 total_header_size += exr_headers[i]->
header_len;
8767 const char* marker =
reinterpret_cast<const char*
>(
8768 memory + total_header_size + 4 + 4);
8783 std::vector<tinyexr::OffsetData> chunk_offset_table_list;
8784 chunk_offset_table_list.reserve(num_parts);
8785 for (
size_t i = 0; i < static_cast<size_t>(num_parts); i++) {
8786 chunk_offset_table_list.resize(chunk_offset_table_list.size() + 1);
8787 tinyexr::OffsetData& offset_data = chunk_offset_table_list.back();
8789 tinyexr::InitSingleResolutionOffsets(offset_data,
size_t(exr_headers[i]->chunk_count));
8790 std::vector<tinyexr::tinyexr_uint64>& offset_table = offset_data.offsets[0][0];
8792 for (
size_t c = 0; c < offset_table.size(); c++) {
8793 tinyexr::tinyexr_uint64
offset;
8794 memcpy(&
offset, marker, 8);
8798 tinyexr::SetErrorMessage(
"Invalid offset size in EXR header chunks.",
8803 offset_table[c] =
offset + 4;
8808 std::vector<int> num_x_tiles, num_y_tiles;
8809 if (!tinyexr::PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_headers[i])) {
8810 tinyexr::SetErrorMessage(
"Invalid tile info.", err);
8813 int num_blocks = InitTileOffsets(offset_data, exr_headers[i], num_x_tiles, num_y_tiles);
8814 if (num_blocks != exr_headers[i]->chunk_count) {
8815 tinyexr::SetErrorMessage(
"Invalid offset table size.", err);
8819 for (
unsigned int l = 0; l < offset_data.offsets.size(); ++l) {
8820 for (
unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) {
8821 for (
unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) {
8822 tinyexr::tinyexr_uint64
offset;
8823 memcpy(&
offset, marker,
sizeof(tinyexr::tinyexr_uint64));
8826 tinyexr::SetErrorMessage(
"Invalid offset size in EXR header chunks.",
8830 offset_data.offsets[l][dy][dx] =
offset + 4;
8831 marker +=
sizeof(tinyexr::tinyexr_uint64);
8839 for (
size_t i = 0; i < static_cast<size_t>(num_parts); i++) {
8840 tinyexr::OffsetData& offset_data = chunk_offset_table_list[i];
8843 for (
unsigned int l = 0; l < offset_data.offsets.size(); ++l)
8844 for (
unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy)
8845 for (
unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) {
8847 const unsigned char* part_number_addr = memory + offset_data.offsets[l][dy][dx] - 4;
8848 unsigned int part_no;
8849 memcpy(&part_no, part_number_addr,
sizeof(
unsigned int));
8850 tinyexr::swap4(&part_no);
8853 tinyexr::SetErrorMessage(
"Invalid `part number' in EXR header chunks.",
8860 int ret = tinyexr::DecodeChunk(&exr_images[i], exr_headers[i], offset_data,
8864 tinyexr::SetErrorMessage(e, err);
8875 unsigned int num_parts,
const char*
filename,
8878 if (exr_images == NULL || exr_headers == NULL || num_parts == 0) {
8879 tinyexr::SetErrorMessage(
8880 "Invalid argument for LoadEXRMultipartImageFromFile", err);
8885 if (!file.valid()) {
8886 tinyexr::SetErrorMessage(
"Cannot read file " + std::string(
filename), err);
8891 file.data, file.size, err);
8894int SaveEXRToMemory(
const float* data,
int width,
int height,
int components,
8895 const int save_as_fp16,
unsigned char** outbuf,
const char** err)
8898 if ((components == 1) || components == 3 || components == 4) {
8901 std::stringstream ss;
8902 ss <<
"Unsupported component value : " << components << std::endl;
8904 tinyexr::SetErrorMessage(ss.str(), err);
8911 if ((width < 16) && (height < 16)) {
8923 std::vector<float> images[4];
8925 if (components == 1) {
8926 images[0].resize(
static_cast<size_t>(width * height));
8927 memcpy(images[0].data(), data,
sizeof(
float) *
size_t(width * height));
8929 images[0].resize(
static_cast<size_t>(width * height));
8930 images[1].resize(
static_cast<size_t>(width * height));
8931 images[2].resize(
static_cast<size_t>(width * height));
8932 images[3].resize(
static_cast<size_t>(width * height));
8935 for (
size_t i = 0; i < static_cast<size_t>(width * height); i++) {
8936 images[0][i] = data[
static_cast<size_t>(components) * i + 0];
8937 images[1][i] = data[
static_cast<size_t>(components) * i + 1];
8938 images[2][i] = data[
static_cast<size_t>(components) * i + 2];
8939 if (components == 4) {
8940 images[3][i] = data[
static_cast<size_t>(components) * i + 3];
8945 float* image_ptr[4] = { 0, 0, 0, 0 };
8946 if (components == 4) {
8947 image_ptr[0] = &(images[3].at(0));
8948 image_ptr[1] = &(images[2].at(0));
8949 image_ptr[2] = &(images[1].at(0));
8950 image_ptr[3] = &(images[0].at(0));
8951 }
else if (components == 3) {
8952 image_ptr[0] = &(images[2].at(0));
8953 image_ptr[1] = &(images[1].at(0));
8954 image_ptr[2] = &(images[0].at(0));
8955 }
else if (components == 1) {
8956 image_ptr[0] = &(images[0].at(0));
8959 image.
images =
reinterpret_cast<unsigned char**
>(image_ptr);
8960 image.
width = width;
8967 if (components == 4) {
8983 }
else if (components == 3) {
9006 malloc(
sizeof(
int) *
static_cast<size_t>(header.
num_channels)));
9008 malloc(
sizeof(
int) *
static_cast<size_t>(header.
num_channels)));
9012 if (save_as_fp16 > 0) {
9020 unsigned char* mem_buf;
9023 if (mem_size == 0) {
9031 if (mem_size >
size_t(std::numeric_limits<int>::max())) {
9036 (*outbuf) = mem_buf;
9038 return int(mem_size);
9041int SaveEXR(
const float* data,
int width,
int height,
int components,
9042 const int save_as_fp16,
const char* outfilename,
const char** err)
9044 if ((components == 1) || components == 3 || components == 4) {
9047 std::stringstream ss;
9048 ss <<
"Unsupported component value : " << components << std::endl;
9050 tinyexr::SetErrorMessage(ss.str(), err);
9057 if ((width < 16) && (height < 16)) {
9069 std::vector<float> images[4];
9070 const size_t pixel_count =
static_cast<size_t>(width) *
static_cast<size_t>(height);
9072 if (components == 1) {
9073 images[0].resize(pixel_count);
9074 memcpy(images[0].data(), data,
sizeof(
float) * pixel_count);
9076 images[0].resize(pixel_count);
9077 images[1].resize(pixel_count);
9078 images[2].resize(pixel_count);
9079 images[3].resize(pixel_count);
9082 for (
size_t i = 0; i < pixel_count; i++) {
9083 images[0][i] = data[
static_cast<size_t>(components) * i + 0];
9084 images[1][i] = data[
static_cast<size_t>(components) * i + 1];
9085 images[2][i] = data[
static_cast<size_t>(components) * i + 2];
9086 if (components == 4) {
9087 images[3][i] = data[
static_cast<size_t>(components) * i + 3];
9092 float* image_ptr[4] = { 0, 0, 0, 0 };
9093 if (components == 4) {
9094 image_ptr[0] = &(images[3].at(0));
9095 image_ptr[1] = &(images[2].at(0));
9096 image_ptr[2] = &(images[1].at(0));
9097 image_ptr[3] = &(images[0].at(0));
9098 }
else if (components == 3) {
9099 image_ptr[0] = &(images[2].at(0));
9100 image_ptr[1] = &(images[1].at(0));
9101 image_ptr[2] = &(images[0].at(0));
9102 }
else if (components == 1) {
9103 image_ptr[0] = &(images[0].at(0));
9106 image.
images =
reinterpret_cast<unsigned char**
>(image_ptr);
9107 image.
width = width;
9114 if (components == 4) {
9130 }
else if (components == 3) {
9153 malloc(
sizeof(
int) *
static_cast<size_t>(header.
num_channels)));
9155 malloc(
sizeof(
int) *
static_cast<size_t>(header.
num_channels)));
9159 if (save_as_fp16 > 0) {
9178#pragma clang diagnostic pop
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len)
mz_ulong mz_compressBound(mz_ulong source_len)
int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len)
const char ** channel_names
struct TEXRImage * next_level
int LoadEXRFromMemory(float **out_rgba, int *width, int *height, const unsigned char *memory, size_t size, const char **err)
int ParseEXRHeaderFromMemory(EXRHeader *header, const EXRVersion *version, const unsigned char *memory, size_t size, const char **err)
struct TEXRBox2i EXRBox2i
int FreeEXRHeader(EXRHeader *exr_header)
#define TINYEXR_ERROR_UNSUPPORTED_FEATURE
#define TINYEXR_COMPRESSIONTYPE_ZFP
int FreeEXRImage(EXRImage *exr_image)
int IsEXRFromMemory(const unsigned char *memory, size_t size)
int ParseEXRMultipartHeaderFromFile(EXRHeader ***headers, int *num_headers, const EXRVersion *version, const char *filename, const char **err)
#define TINYEXR_ERROR_INVALID_ARGUMENT
struct TEXRVersion EXRVersion
#define TINYEXR_COMPRESSIONTYPE_PIZ
int LoadDeepEXR(DeepImage *out_image, const char *filename, const char **err)
int SaveEXRToMemory(const float *data, const int width, const int height, const int components, const int save_as_fp16, unsigned char **buffer, const char **err)
#define TINYEXR_ERROR_DATA_TOO_LARGE
#define TINYEXR_ZFP_COMPRESSIONTYPE_PRECISION
#define TINYEXR_COMPRESSIONTYPE_ZIPS
#define TINYEXR_TILE_MIPMAP_LEVELS
#define TINYEXR_TILE_RIPMAP_LEVELS
#define TINYEXR_ERROR_INVALID_MAGIC_NUMBER
#define TINYEXR_COMPRESSIONTYPE_RLE
int ParseEXRVersionFromMemory(EXRVersion *version, const unsigned char *memory, size_t size)
#define TINYEXR_PIXELTYPE_UINT
int SaveEXR(const float *data, const int width, const int height, const int components, const int save_as_fp16, const char *filename, const char **err)
int ParseEXRVersionFromFile(EXRVersion *version, const char *filename)
void FreeEXRErrorMessage(const char *msg)
#define TINYEXR_ZFP_COMPRESSIONTYPE_RATE
int LoadEXRMultipartImageFromMemory(EXRImage *images, const EXRHeader **headers, unsigned int num_parts, const unsigned char *memory, const size_t size, const char **err)
int LoadEXR(float **out_rgba, int *width, int *height, const char *filename, const char **err)
#define TINYEXR_ERROR_SERIALIZATION_FAILED
int LoadEXRWithLayer(float **out_rgba, int *width, int *height, const char *filename, const char *layer_name, const char **err)
struct TEXRImage EXRImage
int LoadEXRImageFromMemory(EXRImage *image, const EXRHeader *header, const unsigned char *memory, const size_t size, const char **err)
#define TINYEXR_ZFP_COMPRESSIONTYPE_ACCURACY
#define TINYEXR_ERROR_INVALID_EXR_VERSION
#define TINYEXR_COMPRESSIONTYPE_NONE
#define TINYEXR_PIXELTYPE_HALF
#define TINYEXR_MAX_CUSTOM_ATTRIBUTES
int ParseEXRHeaderFromFile(EXRHeader *header, const EXRVersion *version, const char *filename, const char **err)
int EXRLayers(const char *filename, const char **layer_names[], int *num_layers, const char **err)
int IsEXR(const char *filename)
#define TINYEXR_ERROR_LAYER_NOT_FOUND
size_t SaveEXRImageToMemory(const EXRImage *image, const EXRHeader *exr_header, unsigned char **memory, const char **err)
#define TINYEXR_TILE_ROUND_UP
#define TINYEXR_COMPRESSIONTYPE_ZIP
int ParseEXRMultipartHeaderFromMemory(EXRHeader ***headers, int *num_headers, const EXRVersion *version, const unsigned char *memory, size_t size, const char **err)
#define TINYEXR_ERROR_CANT_OPEN_FILE
int EXRNumLevels(const EXRImage *exr_image)
struct TEXRAttribute EXRAttribute
#define TINYEXR_ERROR_INVALID_FILE
#define TINYEXR_ERROR_INVALID_DATA
struct TEXRHeader EXRHeader
struct TEXRMultiPartImage EXRMultiPartImage
int LoadEXRMultipartImageFromFile(EXRImage *images, const EXRHeader **headers, unsigned int num_parts, const char *filename, const char **err)
#define TINYEXR_MAX_HEADER_ATTRIBUTES
void InitEXRHeader(EXRHeader *exr_header)
struct TEXRMultiPartHeader EXRMultiPartHeader
size_t SaveEXRMultipartImageToMemory(const EXRImage *images, const EXRHeader **exr_headers, unsigned int num_parts, unsigned char **memory, const char **err)
#define TINYEXR_PIXELTYPE_FLOAT
struct TEXRChannelInfo EXRChannelInfo
#define TINYEXR_TILE_ROUND_DOWN
#define TINYEXR_ERROR_UNSUPPORTED_FORMAT
void InitEXRImage(EXRImage *exr_image)
#define TINYEXR_ERROR_INVALID_HEADER
int SaveEXRImageToFile(const EXRImage *image, const EXRHeader *exr_header, const char *filename, const char **err)
#define TINYEXR_ERROR_CANT_WRITE_FILE
#define TINYEXR_TILE_ONE_LEVEL
struct TDeepImage DeepImage
int SaveEXRMultipartImageToFile(const EXRImage *images, const EXRHeader **exr_headers, unsigned int num_parts, const char *filename, const char **err)
int LoadEXRImageFromFile(EXRImage *image, const EXRHeader *header, const char *filename, const char **err)
void EXRSetNameAttr(EXRHeader *exr_header, const char *name)