Skip to content

Commit a6cdf77

Browse files
authored
fix(ImageInput): incorrect IOProxy logic related to valid_file (#4839)
Inside ImageInput::create(), there's a section where the created ImageInput tries to do a quick call to its valid_file() method to do an inexpensive check that the file appears to be the right file format corresponding to the ImageInput subclass. However, it always called the variety of valid_file() that takes a string filename, ignoring the fact that create() itself may have been passed an IOProxy, which should always take precedence over trying to open a real file. --------- Signed-off-by: Larry Gritz <[email protected]>
1 parent 0a8c875 commit a6cdf77

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

src/libOpenImageIO/imageioplugin.cpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,13 @@ ImageInput::create(string_view filename, bool do_open, const ImageSpec* config,
630630
format = filename;
631631
}
632632

633+
// Mostly for backward compatibility, if we were not given an ioproxy,
634+
// check if one was passed via the configuration hints.
635+
if (!ioproxy && config) {
636+
if (auto p = config->find_attribute("oiio:ioproxy", TypeDesc::PTR))
637+
ioproxy = p->get<Filesystem::IOProxy*>();
638+
}
639+
633640
ImageInput::Creator create_function = nullptr;
634641
{ // scope the lock:
635642
std::unique_lock<std::recursive_mutex> lock(imageio_mutex);
@@ -662,11 +669,15 @@ ImageInput::create(string_view filename, bool do_open, const ImageSpec* config,
662669
// deal with it robustly.
663670
formats_tried.push_back(create_function);
664671
in = std::unique_ptr<ImageInput>(create_function());
665-
if (!do_open && in && in->valid_file(filename)) {
666-
// Special case: we don't need to return the file
667-
// already opened, and this ImageInput says that the
668-
// file is the right type.
669-
return in;
672+
if (!do_open && in) {
673+
// We created the ImageInput but we don't need a full open yet
674+
if ((ioproxy && in->supports("ioproxy") && in->valid_file(ioproxy))
675+
|| (!ioproxy && !filename.empty() && in->valid_file(filename))) {
676+
// Special case: we don't need to return the file
677+
// already opened, and this ImageInput says that the
678+
// file is the right type with a fast valid_file test.
679+
return in;
680+
}
670681
}
671682
ImageSpec tmpspec;
672683
bool ok = false;
@@ -730,16 +741,22 @@ ImageInput::create(string_view filename, bool do_open, const ImageSpec* config,
730741
}
731742
if (!in)
732743
continue;
733-
if (!do_open && !ioproxy && !in->valid_file(filename)) {
734-
// Since we didn't need to open it, we just checked whether
735-
// it was a valid file, and it's not. Try the next one.
736-
if (pvt::oiio_print_debug > 1)
737-
OIIO::debugfmt(
738-
"ImageInput::create: \"{}\" did not open using format \"{}\" {} [valid_file was false].\n",
739-
filename, plugin->first, in->format_name());
740-
in.reset();
741-
continue;
744+
if (!do_open) {
745+
if ((ioproxy && in->supports("ioproxy")
746+
&& !in->valid_file(ioproxy))
747+
|| (!ioproxy && !filename.empty()
748+
&& !in->valid_file(filename))) {
749+
// Since we didn't need to open it, we just checked whether
750+
// it was a valid file, and it's not. Try the next one.
751+
if (pvt::oiio_print_debug > 1)
752+
OIIO::debugfmt(
753+
"ImageInput::create: \"{}\" did not open using format \"{}\" {} [valid_file was false].\n",
754+
filename, plugin->first, in->format_name());
755+
in.reset();
756+
continue;
757+
}
742758
}
759+
743760
// We either need to open it, or we already know it appears
744761
// to be a file of the right type.
745762
in->set_ioproxy(ioproxy);

0 commit comments

Comments
 (0)