Skip to content

Commit 0f8b534

Browse files
committed
feat: global attribute to enable/disable formats at runtime
Signed-off-by: Rahul Baradol <[email protected]>
1 parent 4bb092e commit 0f8b534

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/libOpenImageIO/imageio.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ int limit_imagesize_MB(std::min(32 * 1024,
5959
int imageinput_strict(0);
6060
ustring font_searchpath(Sysutil::getenv("OPENIMAGEIO_FONTS"));
6161
ustring plugin_searchpath(OIIO_DEFAULT_PLUGIN_SEARCHPATH);
62+
ustring
63+
allowed_input_format_list; // list of input formats to be allowed at runtime
64+
ustring
65+
allowed_output_format_list; // list of output formats to be allowed at runtime
6266
std::string format_list; // comma-separated list of all formats
6367
std::string input_format_list; // comma-separated list of readable formats
6468
std::string output_format_list; // comma-separated list of writable formats
@@ -439,6 +443,14 @@ attribute(string_view name, TypeDesc type, const void* val)
439443
oiio_try_all_readers = *(const int*)val;
440444
return true;
441445
}
446+
if (name == "allowed_input_formats" && type == TypeString) {
447+
allowed_input_format_list = ustring(*(const char**)val);
448+
return true;
449+
}
450+
if (name == "allowed_output_formats" && type == TypeString) {
451+
allowed_output_format_list = ustring(*(const char**)val);
452+
return true;
453+
}
442454

443455
return false;
444456
}
@@ -676,6 +688,20 @@ getattribute(string_view name, TypeDesc type, void* val)
676688
*(float*)val = IB_total_image_read_time;
677689
return true;
678690
}
691+
if (name == "allowed_input_formats" && type == TypeString) {
692+
if (allowed_input_format_list.empty()) {
693+
return false;
694+
}
695+
*(ustring*)val = allowed_input_format_list;
696+
return true;
697+
}
698+
if (name == "allowed_output_formats" && type == TypeString) {
699+
if (allowed_output_format_list.empty()) {
700+
return false;
701+
}
702+
*(ustring*)val = allowed_output_format_list;
703+
return true;
704+
}
679705
return false;
680706
}
681707

src/libOpenImageIO/imageioplugin.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,28 @@ ImageOutput::create(string_view filename, Filesystem::IOProxy* ioproxy,
537537
format = filename;
538538
}
539539

540+
ustring comma_sep_allowed_output_formats;
541+
if (OIIO::getattribute("allowed_output_formats", TypeString,
542+
&comma_sep_allowed_output_formats)) {
543+
std::vector<std::string> allowed_output_formats;
544+
Strutil::split(comma_sep_allowed_output_formats, allowed_output_formats,
545+
",", -1);
546+
547+
bool isValidFormat = 0;
548+
for (std::string allowed_format : allowed_output_formats) {
549+
if (allowed_format == format) {
550+
isValidFormat = 1;
551+
break;
552+
}
553+
}
554+
555+
if (!isValidFormat) {
556+
OIIO::errorfmt(
557+
"ImageOutput::create() called: output image format not found in list of allowed formats");
558+
return out;
559+
}
560+
}
561+
540562
ImageOutput::Creator create_function = nullptr;
541563
{ // scope the lock:
542564
std::unique_lock<std::recursive_mutex> lock(imageio_mutex);
@@ -630,6 +652,29 @@ ImageInput::create(string_view filename, bool do_open, const ImageSpec* config,
630652
format = filename;
631653
}
632654

655+
ustring comma_sep_allowed_input_formats;
656+
if (OIIO::getattribute("allowed_input_formats", TypeString,
657+
&comma_sep_allowed_input_formats)) {
658+
std::vector<std::string> allowed_input_formats;
659+
Strutil::split(comma_sep_allowed_input_formats, allowed_input_formats,
660+
",", -1);
661+
662+
bool isValidFormat = 0;
663+
for (std::string allowed_format : allowed_input_formats) {
664+
if (allowed_format == format) {
665+
isValidFormat = 1;
666+
break;
667+
}
668+
}
669+
670+
if (!isValidFormat) {
671+
OIIO::errorfmt(
672+
"ImageInput::create() called: input image format not found in list of allowed formats");
673+
return in;
674+
}
675+
}
676+
677+
633678
ImageInput::Creator create_function = nullptr;
634679
{ // scope the lock:
635680
std::unique_lock<std::recursive_mutex> lock(imageio_mutex);

0 commit comments

Comments
 (0)