diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2015-06-19 14:00:18 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2015-06-19 14:34:11 +0300 |
commit | 9d796df4f6376809f14ca0dd0484f63df9c72494 (patch) | |
tree | 2a01240bcdd82af1bc6c1507f41abf5df20c95a7 /source/blender/imbuf | |
parent | 0d3555fe2ea39e896a4679598723add20609f0c8 (diff) |
Support half float file format storage for Multilayer EXR
Quite straightforward implementation -- all the conversion magic is
happening in IMB_exr_write_channels() and remained changes are only
needed to pass information whether channels is to be converted to
half float or not.
Regular file output will use full-float for Z pass, which matches
behavior of the single layer EXR files. But when saving happens
with File Output node then all the passes are respecting half float
settings because it's not possible to distinguish whether we're
saving Z pass or not.
Reviewers: juicyfruit, campbellbarton
Reviewed By: campbellbarton
Subscribers: maxon, effstops, fsiddi
Differential Revision: https://developer.blender.org/D1353
Diffstat (limited to 'source/blender/imbuf')
-rw-r--r-- | source/blender/imbuf/intern/openexr/openexr_api.cpp | 60 | ||||
-rw-r--r-- | source/blender/imbuf/intern/openexr/openexr_multi.h | 6 | ||||
-rw-r--r-- | source/blender/imbuf/intern/openexr/openexr_stub.cpp | 3 |
3 files changed, 57 insertions, 12 deletions
diff --git a/source/blender/imbuf/intern/openexr/openexr_api.cpp b/source/blender/imbuf/intern/openexr/openexr_api.cpp index e805d3dc25c..72288149846 100644 --- a/source/blender/imbuf/intern/openexr/openexr_api.cpp +++ b/source/blender/imbuf/intern/openexr/openexr_api.cpp @@ -654,6 +654,8 @@ typedef struct ExrHandle { ListBase channels; /* flattened out, ExrChannel */ ListBase layers; /* hierarchical, pointing in end to ExrChannel */ + + int num_half_channels; /* used during filr save, allows faster temporary buffers allocation */ } ExrHandle; /* flattened out channel */ @@ -666,6 +668,7 @@ typedef struct ExrChannel { float *rect; /* first pointer to write in */ char chan_id; /* quick lookup of channel char */ int view_id; /* quick lookup of channel view */ + bool use_half_float; /* when saving use half float for file storage */ } ExrChannel; @@ -783,7 +786,10 @@ static const char *imb_exr_insert_view_name(const char *passname, const char *vi /* adds flattened ExrChannels */ /* xstride, ystride and rect can be done in set_channel too, for tile writing */ /* passname does not include view */ -void IMB_exr_add_channel(void *handle, const char *layname, const char *passname, const char *viewname, int xstride, int ystride, float *rect) +void IMB_exr_add_channel(void *handle, + const char *layname, const char *passname, const char *viewname, + int xstride, int ystride, float *rect, + bool use_half_float) { ExrHandle *data = (ExrHandle *)handle; ExrChannel *echan; @@ -823,6 +829,11 @@ void IMB_exr_add_channel(void *handle, const char *layname, const char *passname echan->xstride = xstride; echan->ystride = ystride; echan->rect = rect; + echan->use_half_float = use_half_float; + + if (echan->use_half_float) { + data->num_half_channels++; + } exr_printf("added channel %s\n", echan->name); BLI_addtail(&data->channels, echan); @@ -840,8 +851,10 @@ int IMB_exr_begin_write(void *handle, const char *filename, int width, int heigh bool is_singlelayer, is_multilayer, is_multiview; - for (echan = (ExrChannel *)data->channels.first; echan; echan = echan->next) - header.channels().insert(echan->name, Channel(Imf::FLOAT)); + for (echan = (ExrChannel *)data->channels.first; echan; echan = echan->next) { + header.channels().insert(echan->name, + Channel(echan->use_half_float ? Imf::HALF : Imf::FLOAT)); + } openexr_header_compression(&header, compress); BKE_stamp_info_callback(&header, stamp, openexr_header_metadata_callback); @@ -911,6 +924,8 @@ void IMB_exrtile_begin_write(void *handle, const char *filename, int mipmap, int /* assign channels */ for (echan = (ExrChannel *)data->channels.first; echan; echan = echan->next) { + /* Tiles are expected to be saved with full float currently. */ + BLI_assert(echan->use_half_float == 0); echan->m->internal_name = echan->m->name; echan->m->part_number = echan->view_id; @@ -965,7 +980,7 @@ int IMB_exr_begin_read(void *handle, const char *filename, int *width, int *heig GetChannelsInMultiPartFile(*data->ifile, channels); for (size_t i = 0; i < channels.size(); i++) { - IMB_exr_add_channel(data, NULL, channels[i].name.c_str(), channels[i].view.c_str(), 0, 0, NULL); + IMB_exr_add_channel(data, NULL, channels[i].name.c_str(), channels[i].view.c_str(), 0, 0, NULL, false); echan = (ExrChannel *)data->channels.last; echan->m->name = channels[i].name; @@ -1062,12 +1077,33 @@ void IMB_exr_write_channels(void *handle) ExrChannel *echan; if (data->channels.first) { - for (echan = (ExrChannel *)data->channels.first; echan; echan = echan->next) { - /* last scanline, stride negative */ - float *rect = echan->rect + echan->xstride * (data->height - 1) * data->width; + const size_t num_pixels = ((size_t)data->width) * data->height; + half *rect_half = NULL, *current_rect_half; + + /* We allocate teporary storage for half pixels for all the channels at once. */ + if (data->num_half_channels != 0) { + rect_half = (half*)MEM_mallocN(sizeof(half) * data->num_half_channels * num_pixels, __func__); + current_rect_half = rect_half; + } - frameBuffer.insert(echan->name, Slice(Imf::FLOAT, (char *)rect, - echan->xstride * sizeof(float), -echan->ystride * sizeof(float))); + for (echan = (ExrChannel *)data->channels.first; echan; echan = echan->next) { + /* Writting starts from last scanline, stride negative. */ + if (echan->use_half_float) { + float *rect = echan->rect; + half *cur = current_rect_half; + for (size_t i = 0; i < num_pixels; ++i, ++cur) { + *cur = rect[i * echan->xstride]; + } + half *rect_to_write = current_rect_half + (data->height - 1) * data->width; + frameBuffer.insert(echan->name, Slice(Imf::HALF, (char *)rect_to_write, + sizeof(half), -data->width * sizeof(half))); + current_rect_half += num_pixels; + } + else { + float *rect = echan->rect + echan->xstride * (data->height - 1) * data->width; + frameBuffer.insert(echan->name, Slice(Imf::FLOAT, (char *)rect, + echan->xstride * sizeof(float), -echan->ystride * sizeof(float))); + } } data->ofile->setFrameBuffer(frameBuffer); @@ -1077,6 +1113,10 @@ void IMB_exr_write_channels(void *handle) catch (const std::exception& exc) { std::cerr << "OpenEXR-writePixels: ERROR: " << exc.what() << std::endl; } + /* Free temporary buffers. */ + if (rect_half != NULL) { + MEM_freeN(rect_half); + } } else { printf("Error: attempt to save MultiLayer without layers.\n"); @@ -1518,7 +1558,7 @@ static ExrHandle *imb_exr_begin_read_mem(IStream &file_stream, MultiPartInputFil imb_exr_get_views(*data->ifile, *data->multiView); for (size_t i = 0; i < channels.size(); i++) { - IMB_exr_add_channel(data, NULL, channels[i].name.c_str(), channels[i].view.c_str(), 0, 0, NULL); + IMB_exr_add_channel(data, NULL, channels[i].name.c_str(), channels[i].view.c_str(), 0, 0, NULL, false); echan = (ExrChannel *)data->channels.last; echan->m->name = channels[i].name; diff --git a/source/blender/imbuf/intern/openexr/openexr_multi.h b/source/blender/imbuf/intern/openexr/openexr_multi.h index 77fa420322e..97e8042caa6 100644 --- a/source/blender/imbuf/intern/openexr/openexr_multi.h +++ b/source/blender/imbuf/intern/openexr/openexr_multi.h @@ -52,7 +52,11 @@ struct StampData; void *IMB_exr_get_handle(void); void *IMB_exr_get_handle_name(const char *name); -void IMB_exr_add_channel(void *handle, const char *layname, const char *passname, const char *view, int xstride, int ystride, float *rect); +void IMB_exr_add_channel(void *handle, + const char *layname, const char *passname, const char *view, + int xstride, int ystride, + float *rect, + bool use_half_float); int IMB_exr_begin_read(void *handle, const char *filename, int *width, int *height); int IMB_exr_begin_write(void *handle, const char *filename, int width, int height, int compress, const struct StampData *stamp); diff --git a/source/blender/imbuf/intern/openexr/openexr_stub.cpp b/source/blender/imbuf/intern/openexr/openexr_stub.cpp index 1a2ae7a97e1..3ad23456b9f 100644 --- a/source/blender/imbuf/intern/openexr/openexr_stub.cpp +++ b/source/blender/imbuf/intern/openexr/openexr_stub.cpp @@ -35,7 +35,8 @@ void *IMB_exr_get_handle (void) {return NULL;} void *IMB_exr_get_handle_name (const char * /*name*/) { return NULL;} void IMB_exr_add_channel (void * /*handle*/, const char * /*layname*/, const char * /*passname*/, const char * /*view*/, - int /*xstride*/, int /*ystride*/, float * /*rect*/) { } + int /*xstride*/, int /*ystride*/, float * /*rect*/, + bool /*use_half_float*/) { } int IMB_exr_begin_read (void * /*handle*/, const char * /*filename*/, int * /*width*/, int * /*height*/) { return 0;} int IMB_exr_begin_write (void * /*handle*/, const char * /*filename*/, int /*width*/, int /*height*/, int /*compress*/, const struct StampData * /*stamp*/) { return 0;} |