26#include <OpenEXR/OpenEXRConfig.h>
27#define COMBINED_OPENEXR_VERSION \
28 ((10000 * OPENEXR_VERSION_MAJOR) + (100 * OPENEXR_VERSION_MINOR) + OPENEXR_VERSION_PATCH)
30#if COMBINED_OPENEXR_VERSION >= 20599
32# include <Imath/half.h>
33# include <OpenEXR/ImfFrameBuffer.h>
34# define exr_file_offset_t uint64_t
37# include <OpenEXR/half.h>
38# define exr_file_offset_t Int64
41#include <OpenEXR/Iex.h>
42#include <OpenEXR/ImfArray.h>
43#include <OpenEXR/ImfAttribute.h>
44#include <OpenEXR/ImfChannelList.h>
45#include <OpenEXR/ImfChromaticities.h>
46#include <OpenEXR/ImfCompression.h>
47#include <OpenEXR/ImfCompressionAttribute.h>
48#include <OpenEXR/ImfIO.h>
49#include <OpenEXR/ImfInputFile.h>
50#include <OpenEXR/ImfIntAttribute.h>
51#include <OpenEXR/ImfOutputFile.h>
52#include <OpenEXR/ImfPixelType.h>
53#include <OpenEXR/ImfPreviewImage.h>
54#include <OpenEXR/ImfRgbaFile.h>
55#include <OpenEXR/ImfStandardAttributes.h>
56#include <OpenEXR/ImfStringAttribute.h>
57#include <OpenEXR/ImfVersion.h>
60#include <OpenEXR/ImfInputPart.h>
61#include <OpenEXR/ImfMultiPartInputFile.h>
62#include <OpenEXR/ImfMultiPartOutputFile.h>
63#include <OpenEXR/ImfMultiView.h>
64#include <OpenEXR/ImfOutputPart.h>
65#include <OpenEXR/ImfPartHelper.h>
66#include <OpenEXR/ImfPartType.h>
67#include <OpenEXR/ImfTiledOutputPart.h>
112using namespace Imath;
125 {1.0f, 0.0f}, {0.0f, 1.0f}, {0.0f, 0.0f}, {1.0f / 3.0f, 1.0f / 3.0f}};
128 {0.7347f, 0.2653f}, {0.0f, 1.0f}, {0.0001f, -0.077f}, {0.32168f, 0.33767f}};
134 IMemStream(
uchar *exrbuf,
size_t exrsize) : IStream(
"<memory>"), _exrpos(0), _exrsize(exrsize)
139 bool read(
char c[],
int n)
override
141 if (n + _exrpos <= _exrsize) {
142 memcpy(c, (
void *)(&_exrbuf[_exrpos]), n);
176 throw IEX_NAMESPACE::InputExc(
"file not found");
181 if (_mmap_file ==
nullptr) {
182 throw IEX_NAMESPACE::InputExc(
"BLI_mmap_open failed");
196 bool read(
char c[],
int n)
override
198 if (_exrpos + n > _exrsize) {
199 throw Iex::InputExc(
"Unexpected end of file.");
203 throw Iex::InputExc(
"Error reading file.");
208 return _exrpos < _exrsize;
236 ifs.open(wfilepath, std::ios_base::binary);
239 ifs.open(filepath, std::ios_base::binary);
243 Iex::throwErrnoExc();
247 bool read(
char c[],
int n)
override
250 throw Iex::InputExc(
"Unexpected end of file.");
255 return check_error();
260 return std::streamoff(ifs.tellg());
279 Iex::throwErrnoExc();
299 void write(
const char c[],
int n)
override
337 ofs.open(wfilepath, std::ios_base::binary);
340 ofs.open(filepath, std::ios_base::binary);
344 Iex::throwErrnoExc();
348 void write(
const char c[],
int n)
override
357 return std::streamoff(ofs.tellp());
371 Iex::throwErrnoExc();
374 throw Iex::ErrnoExc(
"File output failed.");
387 return Imf::isImfMagic((
const char *)mem);
397 constexpr int x0 = 100, y0 = 0;
398 constexpr int x1 = 90, y1 = 45;
399 q = y0 + (q - x0) * (y1 - y0) / (x1 - x0);
413 const float margin = (level <= 50) ? (2048.0f + 32.0f * level) :
414 (3624.0f + 128.0f * (level - 50));
415 return HALF_MAX - margin;
432 switch (compression) {
434 header->compression() = NO_COMPRESSION;
437 header->compression() = PXR24_COMPRESSION;
440 header->compression() = ZIP_COMPRESSION;
443 header->compression() = PIZ_COMPRESSION;
446 header->compression() = RLE_COMPRESSION;
449 header->compression() = ZIPS_COMPRESSION;
452 header->compression() = B44_COMPRESSION;
455 header->compression() = B44A_COMPRESSION;
457#if OPENEXR_VERSION_MAJOR > 2 || (OPENEXR_VERSION_MAJOR >= 2 && OPENEXR_VERSION_MINOR >= 2)
459 header->compression() = DWAA_COMPRESSION;
463 header->compression() = DWAB_COMPRESSION;
467#if COMBINED_OPENEXR_VERSION >= 30400
471 header->compression() = HTJ2K32_COMPRESSION;
475 header->compression() = ZIP_COMPRESSION;
482 switch (header.compression()) {
485 case RLE_COMPRESSION:
487 case ZIPS_COMPRESSION:
489 case ZIP_COMPRESSION:
491 case PIZ_COMPRESSION:
493 case PXR24_COMPRESSION:
495 case B44_COMPRESSION:
497 case B44A_COMPRESSION:
499 case DWAA_COMPRESSION:
501 case DWAB_COMPRESSION:
503#if COMBINED_OPENEXR_VERSION >= 30400
504 case HTJ2K256_COMPRESSION:
505 case HTJ2K32_COMPRESSION:
508#if COMBINED_OPENEXR_VERSION >= 30500
509 case LJ2K_COMPRESSION:
511 case ZSTD_COMPRESSION:
514 case NUM_COMPRESSION_METHODS:
528 return is_multi &&
STR_ELEM(name,
"name",
"view");
539 name,
"compression",
"colorInteropID",
"Software",
"BlenderMultiChannel",
"type") ||
540 (is_multi &&
STR_ELEM(name,
"name",
"view"));
560 if (ppm[0] > 0.0 && ppm[1] > 0.0) {
562 addXDensity(*header, ppm[0] * 0.0254);
578 if (aces_colorspace &&
STREQ(aces_colorspace, ibuf_colorspace)) {
586 header->insert(
"colorInteropID", TypedAttribute<std::string>(interop_id));
617 const int channels = ibuf->
channels;
619 const int width = ibuf->
x;
620 const int height = ibuf->
y;
627 const int comp_r = 0;
628 const int comp_g = channels >= 2 ? 1 : 0;
629 const int comp_b = channels >= 3 ? 2 : 0;
630 const int comp_a = channels >= 4 ? 3 : 0;
636 const float *from = float_data +
int64_t(channels) * (ibuf->
y - 1 -
y) * width;
637 for (
int x = 0;
x < ibuf->
x;
x++) {
638 row_buffer[
x] =
float4(from[comp_r], from[comp_g], from[comp_b], from[comp_a]);
642 &row_buffer.
data()->x, &to_row->
r, 4 * width, -half_max_val, half_max_val);
648 for (
int v = 0;
v < 256;
v++) {
655 for (
int i = ibuf->
y - 1; i >= 0; i--) {
658 for (
int j = ibuf->
x; j > 0; j--) {
659 dst->
r = color_to_half[from[0]];
660 dst->
g = color_to_half[from[1]];
661 dst->
b = color_to_half[from[2]];
662 dst->
a = is_alpha ? alpha_to_half[from[3]] : 0x3c00;
682 Channel channel(half_precision ?
HALF :
FLOAT);
683 header.channels().insert(
"R", channel);
684 header.channels().insert(
"G", channel);
685 header.channels().insert(
"B", channel);
687 header.channels().insert(
"A", channel);
697 const int width = ibuf->
x;
698 const int height = ibuf->
y;
699 const int channels = ibuf->
channels;
701 if (half_precision) {
705 int ystride = xstride * width;
706 frameBuffer.insert(
"R", Slice(
HALF, (
char *)&to->
r, xstride, ystride));
707 frameBuffer.insert(
"G", Slice(
HALF, (
char *)&to->
g, xstride, ystride));
708 frameBuffer.insert(
"B", Slice(
HALF, (
char *)&to->
b, xstride, ystride));
710 frameBuffer.insert(
"A", Slice(
HALF, (
char *)&to->
a, xstride, ystride));
716 int xstride =
sizeof(
float) * channels;
717 int ystride = -xstride * width;
720 const float *src_data = ibuf->
float_data() +
int64_t(channels) * (height - 1) * width;
721 frameBuffer.insert(
"R", Slice(
FLOAT, (
char *)src_data, xstride, ystride));
723 "G", Slice(
FLOAT, (
char *)(src_data + (channels >= 2 ? 1 : 0)), xstride, ystride));
725 "B", Slice(
FLOAT, (
char *)(src_data + (channels >= 3 ? 2 : 0)), xstride, ystride));
727 frameBuffer.insert(
"A", Slice(
FLOAT, (
char *)(src_data + 3), xstride, ystride));
744 OutputFile file(file_stream, header);
749 file.setFrameBuffer(frameBuffer);
750 file.writePixels(ibuf->
y);
753 catch (
const std::exception &exc) {
776 OutputFile file(mem_stream, header);
781 file.setFrameBuffer(frameBuffer);
782 file.writePixels(ibuf->
y);
783 return std::move(mem_stream.
buffer);
785 catch (
const std::exception &exc) {
853 MultiPartInputFile *
ifile =
nullptr;
895 ExrWriteHandle *handle = MEM_new<ExrWriteHandle>(
"ExrWriteHandle");
904 handle->
views.emplace_back(viewname);
910 for (StringVector::const_iterator i = views.begin(); count < views.size(); ++i) {
926 for (
int p = 0; p < file.parts(); p++) {
928 if (file.header(p).hasView()) {
929 const std::string &view = file.header(p).view();
931 views.push_back(view);
936 if (hasMultiView(file.header(p))) {
937 StringVector multiview = multiView(file.header(p));
938 for (
const std::string &view : multiview) {
940 views.push_back(view);
960 std::string part_name;
962 part_name = layerpassname;
964 if (part_name.empty()) {
965 part_name = viewname;
968 part_name = part_name +
"-" + viewname;
978 for (
size_t channel = 0; channel < channelnames.
size(); channel++) {
980 std::string full_name = layerpassname;
982 if (full_name.empty()) {
983 full_name = viewname;
986 full_name = full_name +
"." + viewname;
989 if (full_name.empty()) {
990 full_name = channelnames[channel];
993 full_name = full_name +
"." + channelnames[channel];
999 echan.
name = full_name;
1002 echan.
view = viewname;
1008 echan.
rect =
const_cast<float *
>(rect + channel);
1021 header.insert(
"BlenderMultiChannel", StringAttribute(
"Blender V2.55.1 and newer"));
1024 addMultiView(header, handle->
views);
1029 [](
void *
data,
const char *propname,
char *prop,
int ) {
1032 header->insert(propname, StringAttribute(prop));
1039 const char *filepath,
1042 const double ppm[2],
1048 CLOG_ERROR(&
LOG,
"Attempt to save MultiLayer without layers.");
1056 Header header(width, height);
1058 handle->
width = width;
1092 Header part_header = header;
1106 if (!echan.
view.empty()) {
1107 part_header.insert(
"view", StringAttribute(echan.
view));
1109 part_header.insert(
"type", StringAttribute(SCANLINEIMAGE));
1118 part_headers.
append(std::move(part_header));
1122 part_headers.
last().channels().insert(
1131 handle->
mpofile =
new MultiPartOutputFile(
1140 catch (
const std::exception &exc) {
1149 delete handle->
ofile;
1165 ExrReadHandle *handle = MEM_new<ExrReadHandle>(
"ExrReadHandle");
1173 const Box2i dw = handle->
ifile->header(0).dataWindow();
1174 handle->
width = dw.max.x - dw.min.x + 1;
1175 handle->
height = dw.max.y - dw.min.y + 1;
1177 catch (
const std::exception &exc) {
1187 delete handle->
ifile;
1190 handle->
ifile =
nullptr;
1203 CLOG_ERROR(&
LOG,
"Attempt to save MultiLayer without layers.");
1207 const size_t num_pixels = size_t(handle->
width) * handle->
height;
1208 const size_t num_parts = (handle->
mpofile) ? handle->
mpofile->parts() : 1;
1210 for (
size_t part_num = 0; part_num < num_parts; part_num++) {
1211 const std::string &part_id = (handle->
mpofile) ? handle->
mpofile->header(part_num).name() :
"";
1213 int num_half_channels = 0;
1216 num_half_channels++;
1221 uint16_t *current_rect_half =
nullptr;
1222 if (num_half_channels > 0) {
1223 rect_half.
resize(
size_t(num_half_channels) * num_pixels);
1224 current_rect_half = rect_half.
data();
1236 const float *src_float = echan.
rect;
1242 gathered_floats[i++] = src_float[
index * echan.
xstride];
1245 current_rect_half + range.first(),
1254 Slice(Imf::HALF, (
char *)rect_to_write,
sizeof(
half), -handle->
width *
sizeof(
half)));
1255 current_rect_half += num_pixels;
1259 frameBuffer.insert(echan.
name,
1262 echan.
xstride *
sizeof(
float),
1263 -echan.
ystride *
sizeof(
float)));
1269 OutputPart part(*handle->
mpofile, part_num);
1270 part.setFrameBuffer(frameBuffer);
1271 part.writePixels(handle->
height);
1274 handle->
ofile->setFrameBuffer(frameBuffer);
1278 catch (
const std::exception &exc) {
1291 int numparts = handle->
ifile->parts();
1294 const StringAttribute *ta = handle->
ifile->header(0).findTypedAttribute<StringAttribute>(
1295 "BlenderMultiChannel");
1298 short flip = (ta &&
STRPREFIX(ta->value().c_str(),
"Blender V2.43"));
1301 "\nIMB_exr_read_channels\n%s %-6s %-22s "
1302 "\"%s\"\n---------------------------------------------------------------------",
1308 for (
int i = 0; i < numparts; i++) {
1310 bool part_has_buffer =
false;
1313 part_has_buffer =
true;
1317 if (!part_has_buffer) {
1323 InputPart
in(*handle->
ifile, i);
1325 Box2i dw = header.dataWindow();
1336 "%d %-6s %-22s \"%s\"\n",
1343 float *rect = echan.
rect;
1349 rect -= echan.
xstride * (dw.min.x - dw.min.y * handle->
width);
1356 rect -= echan.
xstride * (dw.min.x + dw.min.y * handle->
width);
1360 Slice(Imf::FLOAT, (
char *)rect, xstride, ystride));
1365 in.setFrameBuffer(frameBuffer);
1366 CLOG_DEBUG(&
LOG,
"readPixels:readPixels[%d]: min.y: %d, max.y: %d", i, dw.min.y, dw.max.y);
1367 in.readPixels(dw.min.y, dw.max.y);
1369 catch (
const std::exception &exc) {
1379 catch (
const std::exception &exc) {
1404 out.append({.layer = lay.
name,
1418 for (
const std::string &view : handle->
views) {
1426 const char *src_colorspace,
1427 const bool predivide)
1429 if (handle ==
nullptr || entries.
is_empty()) {
1435 const int width = handle->
width;
1436 const int height = handle->
height;
1442 if (lay.
name != entry.layer) {
1451 if (entry.ibuf ==
nullptr) {
1453 if (entry.ibuf ==
nullptr) {
1459 entry.ibuf =
nullptr;
1465 BLI_assert(entry.ibuf->x == width && entry.ibuf->y == height);
1472 float *
data = entry.ibuf->float_data_for_write();
1473 for (
int a = 0; a < pass.
totchan; a++) {
1475 if (echan.
offset >= entry.ibuf->channels) {
1478 echan.
xstride = entry.ibuf->channels;
1479 echan.
ystride = width * entry.ibuf->channels;
1497 for (
int a = 0; a < pass.
totchan; a++) {
1504 if (entry.ibuf ==
nullptr) {
1512 if (src_colorspace) {
1519 entry.ibuf->channels,
1548 if (rgb ==
nullptr &&
ELEM(info.chan_id,
"RGBA",
"RGB")) {
1560 chosen->
ibuf = ibuf;
1569 delete handle->
ifile;
1580 const char delims[] = {
'.',
'\0'};
1589 return int(end - *token);
1594 const char *channelname,
1595 const bool has_xyz_channels)
1602 else if (echan.
chan_id ==
'Y' && !has_xyz_channels) {
1605 else if (
ELEM(echan.
chan_id,
'R',
'G',
'B',
'A',
'V',
'X',
'Y',
'Z')) {
1615 const char *channelname,
1631 bool has_xyz_channels)
1635 const char *name = echan.
name.c_str();
1636 const char *end = name + strlen(name);
1656 CLOG_ERROR(&
LOG,
"Multilayer read: bad channel name: %s", name);
1678 if (
ELEM(chan_id,
'X',
'Y',
'Z',
'R',
'G',
'B',
'U',
'V',
'A')) {
1710 CLOG_ERROR(&
LOG,
"Multilayer read: bad channel name: %s", name);
1723 BLI_strncpy_utf8(layname, name, std::min(layname_maxncpy,
int(end - name) + 1));
1735 if (lay.
name == layname) {
1740 handle->
layers.append_as();
1749 if (pass.
name == passname) {
1755 pass.
name = passname;
1757 if (
STREQ(passname,
"Combined")) {
1758 lay.
passes.prepend(std::move(pass));
1759 return &lay.
passes.first();
1762 lay.
passes.append(std::move(pass));
1763 return &lay.
passes.last();
1768 bool x_found =
false;
1769 bool y_found =
false;
1770 bool z_found =
false;
1783 return x_found && y_found && z_found;
1796 for (
int p = 0; p < file.parts(); p++) {
1797 const ChannelList &c = file.header(p).channels();
1809 const bool has_multiple_views_in_part = hasMultiView(file.header(p));
1810 StringVector views_in_part;
1811 if (has_multiple_views_in_part) {
1812 views_in_part = multiView(file.header(p));
1815 if (file.header(p).hasView()) {
1816 part_view = file.header(p).view();
1821 if (file.header(p).hasName()) {
1822 part_name = file.header(p).name();
1826 if (!has_multiple_views_in_part) {
1827 if (part_name.
endswith(
"." + part_view)) {
1830 else if (part_name.
endswith(
"-" + part_view)) {
1837 for (ChannelList::ConstIterator i = c.begin(); i != c.end(); i++) {
1839 echan.
name = std::string(i.name());
1842 if (has_multiple_views_in_part) {
1843 echan.
view = viewFromChannelName(echan.
name, views_in_part);
1847 echan.
view = part_view;
1853 echan.
name = part_name +
"." + echan.
name;
1858 channels.
append(std::move(echan));
1868 return !handle->
layers.is_empty();
1876 catch (
const std::exception &exc) {
1878 handle->
views.clear();
1884 handle->
views.clear();
1896 const char *view = echan.
view.c_str();
1897 std::string internal_name = passname;
1899 if (view[0] !=
'\0') {
1936 memset(lookup, 0,
sizeof(lookup));
1938 bool known_channel_layout =
false;
1942 known_channel_layout =
true;
1946 lookup[
uint(
'R')] = 0;
1947 lookup[
uint(
'G')] = 1;
1948 lookup[
uint(
'B')] = 2;
1949 lookup[
uint(
'A')] = 3;
1954 lookup[
uint(
'X')] = 0;
1955 lookup[
uint(
'Y')] = 1;
1956 lookup[
uint(
'Z')] = 2;
1957 lookup[
uint(
'W')] = 3;
1960 lookup[
uint(
'U')] = 0;
1961 lookup[
uint(
'V')] = 1;
1962 lookup[
uint(
'A')] = 2;
1966 for (
int a = 0; a < pass.
totchan; a++) {
1968 const int offset = known_channel_layout ? lookup[
uint(echan.
chan_id)] : a;
1985 int numparts = file.parts();
1986 if (numparts == 1 && hasMultiView(file.header(0))) {
1987 const StringVector views = multiView(file.header(0));
1989 CLOG_DEBUG(&
LOG,
"Default view: %s", defaultViewName(views).c_str());
1990 for (
const std::string &view : views) {
1994 else if (numparts > 1) {
1996 for (
int i = 0; i < numparts; i++) {
1997 if (file.header(i).hasView()) {
1998 CLOG_DEBUG(&
LOG,
"Part %d: view = \"%s\"", i, file.header(i).view().c_str());
2003 for (
int j = 0; j < numparts; j++) {
2004 const ChannelList &channels = file.header(j).channels();
2005 for (ChannelList::ConstIterator i = channels.begin(); i != channels.end(); ++i) {
2006 const Channel &channel = i.channel();
2007 CLOG_DEBUG(&
LOG,
"Found channel %s of type %d", i.name(), channel.type);
2015 const ChannelList &channels = file.header(0).channels();
2017 for (ChannelList::ConstIterator i = channels.begin(); i != channels.end(); ++i) {
2019 const char *
str = i.name();
2030static int exr_has_rgb(MultiPartInputFile &file,
const char *rgb_channels[3])
2034 static const char *channel_names[] = {
2035 "V",
"R",
"Red",
"G",
"Green",
"B",
"Blue",
"AR",
"RA",
"AG",
"GA",
"AB",
"BA",
nullptr};
2037 const Header &header = file.header(0);
2038 int num_channels = 0;
2040 for (
int i = 0; channel_names[i]; i++) {
2042 std::string lower_case_name = std::string(channel_names[i]);
2043 std::transform(lower_case_name.begin(),
2044 lower_case_name.end(),
2045 lower_case_name.begin(),
2046 [](
uchar c) { return std::tolower(c); });
2048 if (header.channels().findChannel(channel_names[i]) ||
2049 header.channels().findChannel(lower_case_name))
2051 rgb_channels[num_channels++] = channel_names[i];
2052 if (num_channels == 3) {
2058 return num_channels;
2066 const Header &header = file.header(0);
2067 return header.channels().findChannel(
"Y") !=
nullptr;
2072 const Header &header = file.header(0);
2073 return header.channels().findChannel(
"BY") !=
nullptr &&
2074 header.channels().findChannel(
"RY") !=
nullptr;
2079 const Header &header = file.header(0);
2080 return !(header.channels().findChannel(
"A") ==
nullptr);
2085 const Header &header = file.header(0);
2086 return (header.channels().findChannel(
"X") !=
nullptr ||
2087 header.channels().findChannel(
"x") !=
nullptr) &&
2088 (header.channels().findChannel(
"Y") !=
nullptr ||
2089 header.channels().findChannel(
"y") !=
nullptr) &&
2090 (header.channels().findChannel(
"Z") !=
nullptr ||
2091 header.channels().findChannel(
"z") !=
nullptr);
2096 const Header &header = file.header(0);
2097 return header.channels().begin() != header.channels().end();
2102 const Header &header = file.header(0);
2103 return header.channels().begin().name();
2108 const ChannelList &channels = header.channels();
2109 for (ChannelList::ConstIterator i = channels.begin(); i != channels.end(); ++i) {
2110 const Channel &channel = i.channel();
2111 if (channel.type !=
HALF) {
2120 const ChannelList &channels = file.header(0).channels();
2121 std::set<std::string> layerNames;
2125 channels.layers(layerNames);
2127 return !layerNames.empty();
2132 for (
int p = 0; p < file.parts(); p++) {
2133 if (hasMultiView(file.header(p))) {
2143 return file.parts() > 1;
2164 catch (
const std::exception &exc) {
2176 const float tolerance_v = 0.000001f;
2177 return (test_v < (ref_v + tolerance_v)) && (test_v > (ref_v - tolerance_v));
2182 const Imf::Chromaticities &
b)
2199 const IntAttribute *header_aces_container = header.findTypedAttribute<IntAttribute>(
2200 "acesImageContainerFlag");
2201 const ChromaticitiesAttribute *header_chromaticities =
2202 header.findTypedAttribute<ChromaticitiesAttribute>(
"chromaticities");
2204 if ((header_aces_container && header_aces_container->value() == 1) ||
2205 (header_chromaticities &&
2210 if (known_colorspace) {
2216 const StringAttribute *header_interop_id = header.findTypedAttribute<StringAttribute>(
2220 if (header_interop_id && !header_interop_id->value().empty()) {
2222 header_interop_id->value());
2231 if (header_chromaticities &&
2255 for (
int p = 0; p < file.parts(); p++) {
2258 return file.header(p);
2263 return file.header(0);
2269 const Header &header = file.header(0);
2270 if (!hasXDensity(header)) {
2273 ppm[0] = double(xDensity(header)) / 0.0254;
2274 ppm[1] = ppm[0] * double(header.pixelAspectRatio());
2277 catch (
const std::exception &exc) {
2293 int display_size[2],
2294 int display_offset[2],
2298 const Header &header = file.header(0);
2299 const Box2i data_window = header.dataWindow();
2300 const Box2i display_window = header.displayWindow();
2301 display_size[0] = display_window.size()[0] + 1;
2302 display_size[1] = display_window.size()[1] + 1;
2303 display_offset[0] = display_window.min[0];
2304 display_offset[1] = display_window.min[1];
2305 data_offset[0] = data_window.min[0] - display_window.min[0];
2306 data_offset[1] = data_window.min[1] - display_window.min[1];
2309 catch (
const std::exception &exc) {
2316 display_size[0] = display_size[1] = 0;
2317 display_offset[0] = display_offset[1] = 0;
2318 data_offset[0] = data_offset[1] = 0;
2322 int display_size[2],
2323 int display_offset[2],
2339 if (handle ==
nullptr) {
2352 MultiPartInputFile *file =
nullptr;
2356 file =
new MultiPartInputFile(*membuf);
2364 const Box2i dw = file->header(0).dataWindow();
2366 ExrReadHandle *handle = MEM_new<ExrReadHandle>(
"ExrReadHandle");
2368 handle->
ifile = file;
2369 handle->
width = dw.max.x - dw.min.x + 1;
2370 handle->
height = dw.max.y - dw.min.y + 1;
2373 catch (
const std::exception &exc) {
2390 ImBuf *ibuf =
nullptr;
2392 MultiPartInputFile *file =
nullptr;
2400 file =
new MultiPartInputFile(*membuf);
2402 const Header &file_header = file->header(0);
2403 Box2i dw = file_header.dataWindow();
2404 const size_t width = dw.max.x - dw.min.x + 1;
2405 const size_t height = dw.max.y - dw.min.y + 1;
2407 CLOG_DEBUG(&
LOG,
"Image data window %d %d %d %d", dw.min.x, dw.min.y, dw.max.x, dw.max.y);
2432 if (defer_multilayer) {
2439 Header::ConstIterator iter;
2442 for (iter = file_header.begin(); iter != file_header.end(); iter++) {
2447 const StringAttribute *attr = file_header.findTypedAttribute<StringAttribute>(
2458 if (!defer_multilayer && is_multi) {
2460 ExrReadHandle *handle = MEM_new<ExrReadHandle>(
"ExrReadHandle");
2461 handle->
ifile = file;
2463 handle->
width = int(width);
2464 handle->
height = int(height);
2474 else if (!defer_multilayer) {
2476 const char *rgb_channels[3];
2477 const int num_rgb_channels =
exr_has_rgb(*file, rgb_channels);
2483 size_t xstride =
sizeof(
float[4]);
2484 size_t ystride = -xstride * width;
2493 first += 4 * (height - 1) * width;
2495 if (num_rgb_channels > 0) {
2496 for (
int i = 0; i < num_rgb_channels; i++) {
2498 Slice(Imf::FLOAT, (
char *)(first + i), xstride, ystride));
2503 Slice(Imf::FLOAT, (
char *)first, xstride, ystride));
2505 Slice(Imf::FLOAT, (
char *)(first + 1), xstride, ystride));
2507 Slice(Imf::FLOAT, (
char *)(first + 2), xstride, ystride));
2509 else if (has_luma) {
2511 Slice(Imf::FLOAT, (
char *)first, xstride, ystride));
2514 Slice(Imf::FLOAT, (
char *)(first + 1), xstride, ystride, 1, 1, 0.5f));
2517 Slice(Imf::FLOAT, (
char *)(first + 2), xstride, ystride, 1, 1, 0.5f));
2519 else if (has_channels) {
2521 Slice(Imf::FLOAT, (
char *)first, xstride, ystride, 1, 1));
2526 Slice(Imf::FLOAT, (
char *)(first + 3), xstride, ystride, 1, 1, 1.0f));
2528 InputPart
in(*file, 0);
2529 in.setFrameBuffer(frameBuffer);
2530 in.readPixels(dw.min.y, dw.max.y);
2540 if (flag & IM_rect) {
2546 if (num_rgb_channels == 0 && has_luma &&
exr_has_chroma(*file)) {
2547 for (
size_t a = 0; a < size_t(ibuf->
x) * ibuf->
y; a++) {
2548 float *
color = float_data + a * 4;
2558 else if (!has_xyz && num_rgb_channels <= 1) {
2560 for (
size_t a = 0; a < size_t(ibuf->
x) * ibuf->
y; a++) {
2561 float *
color = float_data + a * 4;
2579 catch (
const std::exception &exc) {
2597 const size_t max_thumb_size,
2602 ImBuf *ibuf =
nullptr;
2603 IStream *stream =
nullptr;
2604 Imf::RgbaInputFile *file =
nullptr;
2622 file =
new RgbaInputFile(*stream, 1);
2624 if (!file->isComplete()) {
2630 Imath::Box2i dw = file->dataWindow();
2631 int source_w = dw.max.x - dw.min.x + 1;
2632 int source_h = dw.max.y - dw.min.y + 1;
2633 *r_width = source_w;
2634 *r_height = source_h;
2636 const Header &file_header = file->header();
2639 if (file_header.hasPreviewImage()) {
2640 const Imf::PreviewImage &preview = file->header().previewImage();
2642 (
uint8_t *)preview.pixels(),
nullptr, preview.width(), preview.height(), 4);
2653 float scale_factor = std::min(
float(max_thumb_size) /
float(source_w),
2654 float(max_thumb_size) /
float(source_h));
2655 int dest_w = std::max(
int(source_w * scale_factor), 1);
2656 int dest_h = std::max(
int(source_h * scale_factor), 1);
2661 Imf::Array<Imf::Rgba> pixels(source_w);
2665 for (
int h = 0; h < dest_h; h++) {
2668 int source_y = int(
float(h) / scale_factor) + dw.min.y;
2669 file->setFrameBuffer(&pixels[0] - dw.min.x - source_y * source_w, 1, source_w);
2670 file->readPixels(source_y);
2672 for (
int w = 0;
w < dest_w;
w++) {
2674 int source_x = int(std::min<int>((
w / scale_factor), dw.max.x - 1));
2675 float *dest_px = &float_data[(h * dest_w +
w) * 4];
2676 dest_px[0] = pixels[source_x].r;
2677 dest_px[1] = pixels[source_x].g;
2678 dest_px[2] = pixels[source_x].b;
2679 dest_px[3] = pixels[source_x].a;
2683 if (file->lineOrder() == INCREASING_Y) {
2693 catch (
const std::exception &exc) {
2713 Imf::staticInitialize();
2720 Imf::setGlobalThreadCount(0);
#define IDP_string_get(prop)
File and directory operations.
#define BLI_YCC_ITU_BT709
#define SNPRINTF_UTF8(dst, format,...)
#define STRNCPY_UTF8(dst, src)
#define CLOG_ERROR(clg_ref,...)
#define CLOG_DEBUG(clg_ref,...)
#define CLOG_CHECK(clg_ref, verbose_level,...)
#define RE_PASSNAME_COMBINED
#define OPENEXR_CODEC_MASK
Read Blender memory allocation and freeing.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
IFileStream(const char *filepath)
void seekg(exr_file_offset_t pos) override
bool read(char c[], int n) override
exr_file_offset_t tellg() override
bool read(char c[], int n) override
exr_file_offset_t tellg() override
IMMapStream(const char *filepath)
void seekg(exr_file_offset_t pos) override
bool read(char c[], int n) override
exr_file_offset_t tellg() override
void seekg(exr_file_offset_t pos) override
IMemStream(uchar *exrbuf, size_t exrsize)
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr bool is_empty() const
void write(const char c[], int n) override
exr_file_offset_t tellp() override
void seekp(exr_file_offset_t pos) override
OFileStream(const char *filepath)
constexpr const char * c_str() const
constexpr StringRefNull()
constexpr StringRef drop_known_suffix(StringRef suffix) const
void append(const T &value)
const T & last(const int64_t n=0) const
void resize(const int64_t new_size)
constexpr bool startswith(StringRef prefix) const
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr bool endswith(StringRef suffix) const
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
void free(Subdiv *subdiv)
uint16_t float_to_half(float v)
T clamp(const T &a, const T &min, const T &max)
T safe_divide(const T &a, const T &b)
void float_to_half_clamp_array(const float *src, uint16_t *dst, size_t length, float min_value, float max_value)
void parallel_for_each(Range &&range, const Function &function)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
void IMB_colormanagement_assign_float_colorspace(ImBuf *ibuf, const char *name)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int char char int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
Vector< uint8_t > imb_save_buffer_openexr(ImBuf *ibuf, ImBufFlags)
static ExrLayer * imb_exr_get_layer(ExrReadHandle *handle, const char *layname)
static bool exr_has_xyz_channels(ExrReadHandle *exr_handle)
static bool exr_has_chroma(MultiPartInputFile &file)
static StringVector imb_exr_get_views(MultiPartInputFile &file)
static int exr_has_rgb(MultiPartInputFile &file, const char *rgb_channels[3])
static const ColorSpace * imb_exr_part_colorspace(const Header &header)
static bool imb_exr_is_multilayer_file(MultiPartInputFile &file)
static void openexr_header_metadata_colorspace(Header *header, const ColorSpace *colorspace)
static const char * exr_rgba_channelname(MultiPartInputFile &file, const char *chan)
static void save_setup_framebuffer(const ImBuf *ibuf, bool half_precision, bool is_alpha, Vector< RGBAHalf > &half_pixels, FrameBuffer &frameBuffer)
int2 IMB_exr_get_size(ExrReadHandle *handle)
ExrReadHandle * IMB_exr_open(const char *filepath)
size_t BLI_mmap_get_length(const BLI_mmap_file *file) ATTR_WARN_UNUSED_RESULT
bool IMB_chan_id_is_color(StringRef chan_id)
BLI_mmap_file * BLI_mmap_open(int fd) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
void ycc_to_rgb(float y, float cb, float cr, float *r_r, float *r_g, float *r_b, int colorspace)
static void openexr_header_metadata_pixelinfo(Header *header, const double ppm[2])
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void IMB_exr_write_pass(ExrWriteHandle *handle, StringRefNull layerpassname, StringRefNull channelnames, StringRefNull viewname, StringRefNull colorspace, size_t xstride, size_t ystride, const float *rect, bool use_half_float)
static int imb_exr_get_multiView_id(StringVector &views, const std::string &name)
static void exr_print_filecontents(MultiPartInputFile &file)
static void openexr_header_metadata_multi(ExrWriteHandle *handle, Header &header, const StampData *stamp)
static void save_setup_header(const ImBuf *ibuf, bool half_precision, bool is_alpha, Header &header)
static bool exr_has_multipart_file(MultiPartInputFile &file)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
bool IMB_exr_write_end(ExrWriteHandle *handle, const char *filepath, int width, int height, const double ppm[2], int compress, int quality, const StampData *stamp)
static bool openexr_metadata_skip_read(const char *name, const bool is_multi)
@ COLOR_ROLE_ACES_INTERCHANGE
@ COLOR_ROLE_SCENE_LINEAR
ATTR_WARN_UNUSED_RESULT const BMVert * v
bool imb_save_openexr(ImBuf *ibuf, const char *filepath, ImBufFlags)
static bool exr_has_xyz(MultiPartInputFile &file)
static int imb_exr_split_channel_name(ExrChannel &echan, char *layname, char *passname, bool has_xyz_channels)
static const Header & imb_exr_colorspace_header(const MultiPartInputFile &file)
ExrReadHandle * IMB_exr_open_multilayer(const char *filepath)
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static int openexr_header_get_compression(const Header &header)
ImBuf * imb_load_openexr(const uchar *mem, size_t size, ImBufFlags flags, ImFileColorSpace &r_colorspace)
char BLI_toupper_ascii(const char c) ATTR_WARN_UNUSED_RESULT
void BLI_mmap_free(BLI_mmap_file *file) ATTR_NONNULL(1)
static bool exr_has_multiview(MultiPartInputFile &file)
static void imb_exr_pass_name_from_channel(char *passname, const ExrChannel &echan, const char *channelname, const bool has_xyz_channels)
void IMB_exr_get_display_window(ExrReadHandle *handle, int display_size[2], int display_offset[2], int data_offset[2])
static int openexr_compression_for_data_colorspace(const int compression)
VecBase< float, 4 > float4
float BLI_color_from_srgb_table[256]
int BLI_system_thread_count()
static float compression_half_max(const int compression, const int quality)
static void openexr_header_metadata_global(Header *header, const IDProperty *metadata)
static int imb_exr_split_token(const char *str, const char *end, const char **token)
void IMB_freeImBuf(ImBuf *ibuf)
VecBase< int32_t, 2 > int2
const char * imb_file_extensions_openexr[]
ImColorMode IMB_color_mode_from_channels(int channels)
void IMB_exr_write_view(ExrWriteHandle *handle, const char *viewname)
const char * IMB_colormanagement_role_colorspace_name_get(int role)
BMesh const char void * data
Vector< StringRefNull > IMB_exr_get_views(ExrReadHandle *handle)
static bool imb_exr_multilayer_ensure_channels_parsed(ExrReadHandle *handle)
static Imf::Chromaticities CHROMATICITIES_XYZ_E
void IMB_flipy(ImBuf *ibuf)
const char * IMB_colormanagement_colorspace_get_name(const ColorSpace *colorspace)
bool IMB_exr_get_ppm(ExrReadHandle *handle, double ppm[2])
static bool imb_exr_multi_read_single_pass(ExrReadHandle *handle, ImBuf *ibuf)
bool BLI_mmap_read(BLI_mmap_file *file, void *dest, size_t offset, size_t length) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
static void imb_exr_read_channels(ExrReadHandle *handle)
ExrWriteHandle * IMB_exr_write_begin(bool write_multipart=false)
void IMB_exr_close(ExrReadHandle *handle)
static void convert_input_to_half_rgba(const ImBuf *ibuf, RGBAHalf *dst)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
size_t BLI_file_size(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static bool exr_has_channels(MultiPartInputFile &file)
static bool imb_check_chromaticity_matches(const Imf::Chromaticities &a, const Imf::Chromaticities &b)
const ColorSpace * IMB_colormanagement_space_from_interop_id(StringRefNull interop_id)
static void imb_exr_pass_name_from_channel_name(char *passname, const ExrChannel &, const char *channelname, const bool)
bool IMB_colormanagement_space_is_data(const ColorSpace *colorspace)
void BKE_stamp_info_callback(void *data, StampData *stamp_data, StampCallback callback, bool noskip)
StringRefNull IMB_colormanagement_space_get_interop_id(const ColorSpace *colorspace)
static const char * exr_unknown_channel_name(MultiPartInputFile &file)
static int openexr_jpg_like_quality_to_dwa_compression_level(int q)
static Vector< ExrChannel > exr_channels_in_multi_part_file(const MultiPartInputFile &file)
static void get_exr_display_window(const MultiPartInputFile &file, int display_size[2], int display_offset[2], int data_offset[2])
void IMB_byte_from_float(ImBuf *ibuf)
const ColorSpace * IMB_colormanagement_space_get_named(const char *name)
ExrReadHandle * IMB_exr_open_multilayer_from_memory(const uchar *mem, size_t size)
ImBuf * IMB_allocFromBuffer(const uint8_t *byte_buffer, const float *float_buffer, unsigned int w, unsigned int h, unsigned int channels)
bool imb_is_a_openexr(const uchar *mem, const size_t size)
ImBuf * imb_load_filepath_thumbnail_openexr(const char *filepath, const ImBufFlags, const size_t max_thumb_size, ImFileColorSpace &r_colorspace, size_t *r_width, size_t *r_height)
static bool imb_check_chromaticity_val(float test_v, float ref_v)
int bool bool bool size_t size_t size_t BLI_str_partition_ex(const char *str, const char *end, const char delim[], const char **sep, const char **suf, bool from_right) ATTR_NONNULL(1
static void imb_exr_write_channels(ExrWriteHandle *handle)
static bool imb_exr_is_multi(MultiPartInputFile &file)
bool IMB_alloc_float_pixels(ImBuf *ibuf, unsigned int channels, bool initialize_pixels=true)
static Imf::Chromaticities CHROMATICITIES_ACES_2065_1
static bool exr_has_luma(MultiPartInputFile &file)
int BLI_strcaseeq(const char *a, const char *b) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void IMB_colormanagement_transform_float(float *buffer, int width, int height, int channels, const char *from_colorspace, const char *to_colorspace, bool predivide)
static void imb_exr_set_ibuf_display_window(MultiPartInputFile &file, ImBuf *ibuf)
static bool exr_is_half_float(const Header &header)
static ExrPass * imb_exr_get_pass(ExrLayer &lay, const char *passname)
Vector< ExrPassInfo > IMB_exr_get_passes(ExrReadHandle *handle)
static bool exr_get_ppm(MultiPartInputFile &file, double ppm[2])
static void openexr_header_compression(Header *header, int compression, int quality)
static bool exr_has_alpha(MultiPartInputFile &file)
const char * BKE_blender_version_string()
void IMB_exr_read_passes(ExrReadHandle *handle, MutableSpan< ExrPassInfo > entries, const char *src_colorspace=nullptr, bool predivide=false)
void IMB_metadata_set_field(IDProperty *metadata, const char *key, const char *value)
ocio::ColorSpace ColorSpace
static bool openexr_metadata_skip_write(const char *name, const bool is_multi)
static void imb_exr_set_known_colorspace(const Header &header, ImFileColorSpace &r_colorspace)
#define exr_file_offset_t
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
std::string internal_name
const ColorSpace * colorspace
std::string internal_name
ExrChannel * chan[EXR_PASS_MAXCHAN]
char chan_id[EXR_PASS_MAXCHAN+1]
Vector< ExrLayer > layers
MultiPartInputFile * ifile
Vector< ExrChannel > channels
MultiPartOutputFile * mpofile
Vector< ExrChannel > channels
OFileStream * ofile_stream
bool has_layer_pass_names
const ColorSpace * colorspace
const ColorSpace * colorspace
ImbFormatOptions foptions
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
const uint8_t * byte_data() const
IDProperty * metadata_for_write()
const IDProperty * metadata() const
const float * float_data() const
float * float_data_for_write()
bool can_contain_alpha() const
char metadata_colorspace[IM_MAX_SPACE]
void ensure_size(exr_file_offset_t size)
exr_file_offset_t tellp() override
void seekp(exr_file_offset_t pos) override
void write(const char c[], int n) override
void reserve(int new_size)
void resize(int new_size)
wchar_t * alloc_utf16_from_8(const char *in8, size_t add)