diff --git a/src/libOpenImageIO/imageio.cpp b/src/libOpenImageIO/imageio.cpp index 606289ab67..0a53c1d28e 100644 --- a/src/libOpenImageIO/imageio.cpp +++ b/src/libOpenImageIO/imageio.cpp @@ -59,6 +59,10 @@ int limit_imagesize_MB(std::min(32 * 1024, int imageinput_strict(0); ustring font_searchpath(Sysutil::getenv("OPENIMAGEIO_FONTS")); ustring plugin_searchpath(OIIO_DEFAULT_PLUGIN_SEARCHPATH); +ustring + allowed_input_format_list; // list of input formats to be allowed at runtime +ustring + allowed_output_format_list; // list of output formats to be allowed at runtime std::string format_list; // comma-separated list of all formats std::string input_format_list; // comma-separated list of readable formats std::string output_format_list; // comma-separated list of writable formats @@ -439,6 +443,14 @@ attribute(string_view name, TypeDesc type, const void* val) oiio_try_all_readers = *(const int*)val; return true; } + if (name == "allowed_input_formats" && type == TypeString) { + allowed_input_format_list = ustring(*(const char**)val); + return true; + } + if (name == "allowed_output_formats" && type == TypeString) { + allowed_output_format_list = ustring(*(const char**)val); + return true; + } return false; } @@ -676,6 +688,20 @@ getattribute(string_view name, TypeDesc type, void* val) *(float*)val = IB_total_image_read_time; return true; } + if (name == "allowed_input_formats" && type == TypeString) { + if (allowed_input_format_list.empty()) { + return false; + } + *(ustring*)val = allowed_input_format_list; + return true; + } + if (name == "allowed_output_formats" && type == TypeString) { + if (allowed_output_format_list.empty()) { + return false; + } + *(ustring*)val = allowed_output_format_list; + return true; + } return false; } diff --git a/src/libOpenImageIO/imageioplugin.cpp b/src/libOpenImageIO/imageioplugin.cpp index 328a71664b..9511c89f29 100644 --- a/src/libOpenImageIO/imageioplugin.cpp +++ b/src/libOpenImageIO/imageioplugin.cpp @@ -537,6 +537,28 @@ ImageOutput::create(string_view filename, Filesystem::IOProxy* ioproxy, format = filename; } + ustring comma_sep_allowed_output_formats; + if (OIIO::getattribute("allowed_output_formats", TypeString, + &comma_sep_allowed_output_formats)) { + std::vector allowed_output_formats; + Strutil::split(comma_sep_allowed_output_formats, allowed_output_formats, + ",", -1); + + bool isValidFormat = 0; + for (std::string allowed_format : allowed_output_formats) { + if (allowed_format == format) { + isValidFormat = 1; + break; + } + } + + if (!isValidFormat) { + OIIO::errorfmt( + "ImageOutput::create() called: output image format not found in list of allowed formats"); + return out; + } + } + ImageOutput::Creator create_function = nullptr; { // scope the lock: std::unique_lock lock(imageio_mutex); @@ -630,6 +652,29 @@ ImageInput::create(string_view filename, bool do_open, const ImageSpec* config, format = filename; } + ustring comma_sep_allowed_input_formats; + if (OIIO::getattribute("allowed_input_formats", TypeString, + &comma_sep_allowed_input_formats)) { + std::vector allowed_input_formats; + Strutil::split(comma_sep_allowed_input_formats, allowed_input_formats, + ",", -1); + + bool isValidFormat = 0; + for (std::string allowed_format : allowed_input_formats) { + if (allowed_format == format) { + isValidFormat = 1; + break; + } + } + + if (!isValidFormat) { + OIIO::errorfmt( + "ImageInput::create() called: input image format not found in list of allowed formats"); + return in; + } + } + + ImageInput::Creator create_function = nullptr; { // scope the lock: std::unique_lock lock(imageio_mutex);