Skip to content

feat: global attribute to enable/disable formats at runtime #4792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/libOpenImageIO/imageio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Comment on lines +699 to +701
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still believe that we should not return false (which usually indicates that it's an invalid query -- like an attribute name that doesn't exist) for an attribute that exists but happens to be the empty string.

*(ustring*)val = allowed_output_format_list;
return true;
}
return false;
}

Expand Down
45 changes: 45 additions & 0 deletions src/libOpenImageIO/imageioplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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) {
Comment on lines +540 to +548
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To solve the "empty list" problem and also eliminate all these string allocations and copies, I would suggest this change here:

Suggested change
ustring comma_sep_allowed_output_formats;
if (OIIO::getattribute("allowed_output_formats", TypeString,
&comma_sep_allowed_output_formats)) {
std::vector<std::string> 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) {
auto allowed_output_formats = Strutil::splitsv(OIIO::get_string_attribute("allowed_output_formats"), ",", -1);
if (allowed_output_formats.size()) {
bool isValidFormat = 0;
for (auto 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<std::recursive_mutex> lock(imageio_mutex);
Expand Down Expand Up @@ -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<std::string> 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<std::recursive_mutex> lock(imageio_mutex);
Expand Down
Loading