diff options
Diffstat (limited to 'source/blender/imbuf/intern/openexr/openexr_api.cpp')
-rw-r--r-- | source/blender/imbuf/intern/openexr/openexr_api.cpp | 143 |
1 files changed, 77 insertions, 66 deletions
diff --git a/source/blender/imbuf/intern/openexr/openexr_api.cpp b/source/blender/imbuf/intern/openexr/openexr_api.cpp index 380ddaaefad..56188fbe98a 100644 --- a/source/blender/imbuf/intern/openexr/openexr_api.cpp +++ b/source/blender/imbuf/intern/openexr/openexr_api.cpp @@ -330,8 +330,12 @@ extern "C" { * Test presence of OpenEXR file. * \param mem: pointer to loaded OpenEXR bitstream */ -int imb_is_a_openexr(const unsigned char *mem) +bool imb_is_a_openexr(const unsigned char *mem, const size_t size) { + /* No define is exposed for this size. */ + if (size < 4) { + return false; + } return Imf::isImfMagic((const char *)mem); } @@ -407,10 +411,10 @@ static bool imb_save_openexr_half(ImBuf *ibuf, const char *name, const int flags { const int channels = ibuf->channels; const bool is_alpha = (channels >= 4) && (ibuf->planes == 32); - const bool is_zbuf = (flags & IB_zbuffloat) && ibuf->zbuf_float != NULL; /* summarize */ + const bool is_zbuf = (flags & IB_zbuffloat) && ibuf->zbuf_float != nullptr; /* summarize */ const int width = ibuf->x; const int height = ibuf->y; - OStream *file_stream = NULL; + OStream *file_stream = nullptr; try { Header header(width, height); @@ -514,10 +518,10 @@ static bool imb_save_openexr_float(ImBuf *ibuf, const char *name, const int flag { const int channels = ibuf->channels; const bool is_alpha = (channels >= 4) && (ibuf->planes == 32); - const bool is_zbuf = (flags & IB_zbuffloat) && ibuf->zbuf_float != NULL; /* summarize */ + const bool is_zbuf = (flags & IB_zbuffloat) && ibuf->zbuf_float != nullptr; /* summarize */ const int width = ibuf->x; const int height = ibuf->y; - OStream *file_stream = NULL; + OStream *file_stream = nullptr; try { Header header(width, height); @@ -551,7 +555,7 @@ static bool imb_save_openexr_float(ImBuf *ibuf, const char *name, const int flag int ystride = -xstride * width; /* last scanline, stride negative */ - float *rect[4] = {NULL, NULL, NULL, NULL}; + float *rect[4] = {nullptr, nullptr, nullptr, nullptr}; rect[0] = ibuf->rect_float + channels * (height - 1) * width; rect[1] = (channels >= 2) ? rect[0] + 1 : rect[0]; rect[2] = (channels >= 3) ? rect[0] + 2 : rect[0]; @@ -586,7 +590,7 @@ static bool imb_save_openexr_float(ImBuf *ibuf, const char *name, const int flag return true; } -int imb_save_openexr(struct ImBuf *ibuf, const char *name, int flags) +bool imb_save_openexr(struct ImBuf *ibuf, const char *name, int flags) { if (flags & IB_mem) { imb_addencodedbufferImBuf(ibuf); @@ -594,15 +598,15 @@ int imb_save_openexr(struct ImBuf *ibuf, const char *name, int flags) } if (ibuf->foptions.flag & OPENEXR_HALF) { - return (int)imb_save_openexr_half(ibuf, name, flags); + return imb_save_openexr_half(ibuf, name, flags); } /* when no float rect, we save as half (16 bits is sufficient) */ - if (ibuf->rect_float == NULL) { - return (int)imb_save_openexr_half(ibuf, name, flags); + if (ibuf->rect_float == nullptr) { + return imb_save_openexr_half(ibuf, name, flags); } - return (int)imb_save_openexr_float(ibuf, name, flags); + return imb_save_openexr_float(ibuf, name, flags); } /* ******* Nicer API, MultiLayer and with Tile file support ************************************ */ @@ -614,7 +618,7 @@ int imb_save_openexr(struct ImBuf *ibuf, const char *name, int flags) * - separated with a dot: the Layer name (like "Light1" or "Walls" or "Characters") */ -static ListBase exrhandles = {NULL, NULL}; +static ListBase exrhandles = {nullptr, nullptr}; typedef struct ExrHandle { struct ExrHandle *next, *prev; @@ -691,7 +695,7 @@ void *IMB_exr_get_handle_name(const char *name) { ExrHandle *data = (ExrHandle *)BLI_rfindstring(&exrhandles, name, offsetof(ExrHandle, name)); - if (data == NULL) { + if (data == nullptr) { data = (ExrHandle *)IMB_exr_get_handle(); BLI_strncpy(data->name, name, strlen(name) + 1); } @@ -699,7 +703,7 @@ void *IMB_exr_get_handle_name(const char *name) } /* multiview functions */ -} // extern "C" +} /* extern "C" */ extern "C" { @@ -754,8 +758,8 @@ static void imb_exr_insert_view_name(char *name_full, const char *passname, cons { BLI_assert(!ELEM(name_full, passname, viewname)); - if (viewname == NULL || viewname[0] == '\0') { - BLI_strncpy(name_full, passname, sizeof(((ExrChannel *)NULL)->name)); + if (viewname == nullptr || viewname[0] == '\0') { + BLI_strncpy(name_full, passname, sizeof(((ExrChannel *)nullptr)->name)); return; } @@ -882,11 +886,11 @@ int IMB_exr_begin_write(void *handle, delete data->ofile; delete data->ofile_stream; - data->ofile = NULL; - data->ofile_stream = NULL; + data->ofile = nullptr; + data->ofile_stream = nullptr; } - return (data->ofile != NULL); + return (data->ofile != nullptr); } /* only used for writing temp. render results (not image files) @@ -951,8 +955,8 @@ void IMB_exrtile_begin_write( delete data->mpofile; delete data->ofile_stream; - data->mpofile = NULL; - data->ofile_stream = NULL; + data->mpofile = nullptr; + data->ofile_stream = nullptr; } } @@ -973,8 +977,8 @@ int IMB_exr_begin_read(void *handle, const char *filename, int *width, int *heig delete data->ifile; delete data->ifile_stream; - data->ifile = NULL; - data->ifile_stream = NULL; + data->ifile = nullptr; + data->ifile_stream = nullptr; } if (data->ifile) { @@ -988,8 +992,14 @@ 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, false); + IMB_exr_add_channel(data, + nullptr, + channels[i].name.c_str(), + channels[i].view.c_str(), + 0, + 0, + nullptr, + false); echan = (ExrChannel *)data->channels.last; echan->m->name = channels[i].name; @@ -1074,7 +1084,7 @@ float *IMB_exr_channel_rect(void *handle, return echan->rect; } - return NULL; + return nullptr; } void IMB_exr_clear_channels(void *handle) @@ -1097,7 +1107,7 @@ void IMB_exr_write_channels(void *handle) if (data->channels.first) { const size_t num_pixels = ((size_t)data->width) * data->height; - half *rect_half = NULL, *current_rect_half = NULL; + half *rect_half = nullptr, *current_rect_half = nullptr; /* We allocate teporary storage for half pixels for all the channels at once. */ if (data->num_half_channels != 0) { @@ -1138,7 +1148,7 @@ void IMB_exr_write_channels(void *handle) std::cerr << "OpenEXR-writePixels: ERROR: " << exc.what() << std::endl; } /* Free temporary buffers. */ - if (rect_half != NULL) { + if (rect_half != nullptr) { MEM_freeN(rect_half); } } @@ -1210,7 +1220,7 @@ void IMB_exr_read_channels(void *handle) "BlenderMultiChannel"); /* 'previous multilayer attribute, flipped. */ - short flip = (ta && STREQLEN(ta->value().c_str(), "Blender V2.43", 13)); + short flip = (ta && STRPREFIX(ta->value().c_str(), "Blender V2.43")); exr_printf( "\nIMB_exr_read_channels\n%s %-6s %-22s " @@ -1323,7 +1333,7 @@ void IMB_exr_multilayer_convert(void *handle, pass->totchan, pass->chan_id, pass->view); - pass->rect = NULL; + pass->rect = nullptr; } } } @@ -1343,11 +1353,11 @@ void IMB_exr_close(void *handle) delete data->ofile_stream; delete data->multiView; - data->ifile = NULL; - data->ifile_stream = NULL; - data->ofile = NULL; - data->mpofile = NULL; - data->ofile_stream = NULL; + data->ifile = nullptr; + data->ifile_stream = nullptr; + data->ofile = nullptr; + data->mpofile = nullptr; + data->ofile_stream = nullptr; for (chan = (ExrChannel *)data->channels.first; chan; chan = chan->next) { delete chan->m; @@ -1428,7 +1438,7 @@ static int imb_exr_split_channel_name(ExrChannel *echan, char *layname, char *pa * like, MX or NZ, which is basically has structure of * <pass_prefix><component> * - * This is a bit silly, but see file from [#35658]. + * This is a bit silly, but see file from T35658. * * Here we do some magic to distinguish such cases. */ @@ -1491,7 +1501,7 @@ static ExrLayer *imb_exr_get_layer(ListBase *lb, char *layname) { ExrLayer *lay = (ExrLayer *)BLI_findstring(lb, layname, offsetof(ExrLayer, name)); - if (lay == NULL) { + if (lay == nullptr) { lay = (ExrLayer *)MEM_callocN(sizeof(ExrLayer), "exr layer"); BLI_addtail(lb, lay); BLI_strncpy(lay->name, layname, EXR_LAY_MAXNAME); @@ -1504,7 +1514,7 @@ static ExrPass *imb_exr_get_pass(ListBase *lb, char *passname) { ExrPass *pass = (ExrPass *)BLI_findstring(lb, passname, offsetof(ExrPass, name)); - if (pass == NULL) { + if (pass == nullptr) { pass = (ExrPass *)MEM_callocN(sizeof(ExrPass), "exr pass"); if (STREQ(passname, "Combined")) { @@ -1546,7 +1556,7 @@ static ExrHandle *imb_exr_begin_read_mem(IStream &file_stream, 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, false); + data, nullptr, channels[i].name.c_str(), channels[i].view.c_str(), 0, 0, nullptr, false); echan = (ExrChannel *)data->channels.last; echan->m->name = channels[i].name; @@ -1588,7 +1598,7 @@ static ExrHandle *imb_exr_begin_read_mem(IStream &file_stream, if (echan) { printf("error, too many channels in one pass: %s\n", echan->m->name.c_str()); IMB_exr_close(data); - return NULL; + return nullptr; } /* with some heuristics, try to merge the channels in buffers */ @@ -1610,7 +1620,7 @@ static ExrHandle *imb_exr_begin_read_mem(IStream &file_stream, memset(lookup, 0, sizeof(lookup)); /* we can have RGB(A), XYZ(W), UVA */ - if (pass->totchan == 3 || pass->totchan == 4) { + if (ELEM(pass->totchan, 3, 4)) { if (pass->chan[0]->chan_id == 'B' || pass->chan[1]->chan_id == 'B' || pass->chan[2]->chan_id == 'B') { lookup[(unsigned int)'R'] = 0; @@ -1723,7 +1733,7 @@ static int exr_has_rgb(MultiPartInputFile &file, const char *rgb_channels[3]) { /* Common names for RGB-like channels in order. */ static const char *channel_names[] = { - "R", "Red", "G", "Green", "B", "Blue", "AR", "RA", "AG", "GA", "AB", "BA", NULL}; + "R", "Red", "G", "Green", "B", "Blue", "AR", "RA", "AG", "GA", "AB", "BA", nullptr}; const Header &header = file.header(0); int num_channels = 0; @@ -1746,26 +1756,26 @@ static bool exr_has_luma(MultiPartInputFile &file) * optionally it could be also channels for chromas called BY and RY. */ const Header &header = file.header(0); - return header.channels().findChannel("Y") != NULL; + return header.channels().findChannel("Y") != nullptr; } static bool exr_has_chroma(MultiPartInputFile &file) { const Header &header = file.header(0); - return header.channels().findChannel("BY") != NULL && - header.channels().findChannel("RY") != NULL; + return header.channels().findChannel("BY") != nullptr && + header.channels().findChannel("RY") != nullptr; } static bool exr_has_zbuffer(MultiPartInputFile &file) { const Header &header = file.header(0); - return !(header.channels().findChannel("Z") == NULL); + return !(header.channels().findChannel("Z") == nullptr); } static bool exr_has_alpha(MultiPartInputFile &file) { const Header &header = file.header(0); - return !(header.channels().findChannel("A") == NULL); + return !(header.channels().findChannel("A") == nullptr); } static bool exr_is_half_float(MultiPartInputFile &file) @@ -1895,12 +1905,12 @@ struct ImBuf *imb_load_openexr(const unsigned char *mem, int flags, char colorspace[IM_MAX_SPACE]) { - struct ImBuf *ibuf = NULL; - IMemStream *membuf = NULL; - MultiPartInputFile *file = NULL; + struct ImBuf *ibuf = nullptr; + IMemStream *membuf = nullptr; + MultiPartInputFile *file = nullptr; - if (imb_is_a_openexr(mem) == 0) { - return NULL; + if (imb_is_a_openexr(mem, size) == 0) { + return nullptr; } colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_FLOAT); @@ -1918,7 +1928,7 @@ struct ImBuf *imb_load_openexr(const unsigned char *mem, // printf("OpenEXR-load: image data window %d %d %d %d\n", // dw.min.x, dw.min.y, dw.max.x, dw.max.y); - if (0) { // debug + if (false) { /* debug */ exr_print_filecontents(*file); } @@ -2022,17 +2032,18 @@ struct ImBuf *imb_load_openexr(const unsigned char *mem, in.setFrameBuffer(frameBuffer); in.readPixels(dw.min.y, dw.max.y); - // XXX, ImBuf has no nice way to deal with this. - // ideally IM_rect would be used when the caller wants a rect BUT - // at the moment all functions use IM_rect. - // Disabling this is ok because all functions should check - // if a rect exists and create one on demand. - // - // Disabling this because the sequencer frees immediate. - // - // if (flag & IM_rect) { - // IMB_rect_from_float(ibuf); - // } + /* XXX, ImBuf has no nice way to deal with this. + * ideally IM_rect would be used when the caller wants a rect BUT + * at the moment all functions use IM_rect. + * Disabling this is ok because all functions should check + * if a rect exists and create one on demand. + * + * Disabling this because the sequencer frees immediate. */ +#if 0 + if (flag & IM_rect) { + IMB_rect_from_float(ibuf); + } +#endif if (num_rgb_channels == 0 && has_luma && exr_has_chroma(*file)) { for (size_t a = 0; a < (size_t)ibuf->x * ibuf->y; a++) { @@ -2083,7 +2094,7 @@ struct ImBuf *imb_load_openexr(const unsigned char *mem, delete file; delete membuf; - return 0; + return nullptr; } } @@ -2102,4 +2113,4 @@ void imb_exitopenexr(void) setGlobalThreadCount(0); } -} // export "C" +} /* export "C" */ |