Skip to content

Commit

Permalink
Add a helper function to check if an extension is supported by file t…
Browse files Browse the repository at this point in the history
…ransform (AcademySoftwareFoundation#1962)

* Add FileTransform::IsFormatExtensionSupported()

Signed-off-by: Mei Chu <[email protected]>

* IsFormatExtensionSupported ignores dot at start.

Signed-off-by: Mei Chu <[email protected]>

* Add C++ tests for new function.

Signed-off-by: Mei Chu <[email protected]>

* Fix a small bug in the new C++ tests.

Signed-off-by: Mei Chu <[email protected]>

* Add python binding, python tests and address feedbacks.

Signed-off-by: Mei Chu <[email protected]>

* Change extension accepted to IsFormatExtensionSupported() to case-insensitive.

Signed-off-by: Mei Chu <[email protected]>

* Bugfix to account for single dot input.

Signed-off-by: Mei Chu <[email protected]>

* Cleanup isFormatExtensionSupported() structure a bit to look nicer.

Signed-off-by: Mei Chu <[email protected]>

* Reset whitespace cleanup. Also add a guard against invalid pointer.

Signed-off-by: Mei Chu <[email protected]>

---------

Signed-off-by: Mei Chu <[email protected]>
Co-authored-by: Doug Walker <[email protected]>
  • Loading branch information
meimchu and doug-walker authored Apr 3, 2024
1 parent b87d80d commit 67a26e4
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
7 changes: 7 additions & 0 deletions include/OpenColorIO/OpenColorTransforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,13 @@ class OCIOEXPORT FileTransform : public Transform
static const char * GetFormatNameByIndex(int index);
/// Get the LUT reader extension at index, return empty string if an invalid index is specified.
static const char * GetFormatExtensionByIndex(int index);
/// Returns true if the extension corresponds to a format supported by FileTransform.
/// The argument is case-insensitive, and a leading dot, if present, is ignored.
/// Note that FileTransform will attempt all format readers on a given file until it is
/// successful, even files that contain an unsupported extension or no extension.
/// However, this function is useful for applications that want to know which files are likely
/// to be LUT files, based on their extension.
static bool IsFormatExtensionSupported(const char * extension);

FileTransform & operator=(const FileTransform &) = delete;
/// Do not use (needed only for pybind11).
Expand Down
31 changes: 31 additions & 0 deletions src/OpenColorIO/transforms/FileTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ const char * FileTransform::GetFormatExtensionByIndex(int index)
return FormatRegistry::GetInstance().getFormatExtensionByIndex(FORMAT_CAPABILITY_READ, index);
}

bool FileTransform::IsFormatExtensionSupported(const char * extension)
{
return FormatRegistry::GetInstance().isFormatExtensionSupported(extension);
}

std::ostream& operator<< (std::ostream& os, const FileTransform& t)
{
os << "<FileTransform ";
Expand Down Expand Up @@ -522,6 +527,32 @@ const char * FormatRegistry::getFormatExtensionByIndex(int capability, int index
return "";
}

bool FormatRegistry::isFormatExtensionSupported(const char * extension) const
{
// Early return false with an input of just the dot or invalid pointer.
if (!extension || !*extension || 0 == strcmp(extension, "."))
{
return false;
}

// If dot is present at the start, pointer arithmetic increment up by one to ignore that dot.
FileFormatVectorMap::const_iterator iter;
if (extension[0] == '.')
{
iter = m_formatsByExtension.find(StringUtils::Lower(extension + 1));
}
else
{
iter = m_formatsByExtension.find(StringUtils::Lower(extension));
}

if (iter != m_formatsByExtension.end())
{
return true;
}
return false;
}

///////////////////////////////////////////////////////////////////////////

FileFormat::~FileFormat()
Expand Down
1 change: 1 addition & 0 deletions src/OpenColorIO/transforms/FileTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class FormatRegistry
int getNumFormats(int capability) const noexcept;
const char * getFormatNameByIndex(int capability, int index) const noexcept;
const char * getFormatExtensionByIndex(int capability, int index) const noexcept;
bool isFormatExtensionSupported(const char * extension) const;
private:
FormatRegistry();
~FormatRegistry();
Expand Down
4 changes: 3 additions & 1 deletion src/bindings/python/transforms/PyFileTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ void bindPyFileTransform(py::module & m)
.def("getInterpolation", &FileTransform::getInterpolation,
DOC(FileTransform, getInterpolation))
.def("setInterpolation", &FileTransform::setInterpolation, "interpolation"_a,
DOC(FileTransform, setInterpolation));
DOC(FileTransform, setInterpolation))
.def_static("IsFormatExtensionSupported", &FileTransform::IsFormatExtensionSupported, "extension"_a,
DOC(FileTransform, IsFormatExtensionSupported));

defRepr(clsFileTransform);

Expand Down
14 changes: 14 additions & 0 deletions tests/cpu/transforms/FileTransform_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,20 @@ OCIO_ADD_TEST(FileTransform, format_by_index)
ValidateFormatByIndex(formatRegistry, OCIO::FORMAT_CAPABILITY_READ);
}

OCIO_ADD_TEST(FileTransform, is_format_extension_supported)
{
OCIO::FormatRegistry & formatRegistry = OCIO::FormatRegistry::GetInstance();
OCIO_CHECK_EQUAL(formatRegistry.isFormatExtensionSupported("foo"), false);
OCIO_CHECK_EQUAL(formatRegistry.isFormatExtensionSupported("bar"), false);
OCIO_CHECK_EQUAL(formatRegistry.isFormatExtensionSupported("."), false);
OCIO_CHECK_ASSERT(formatRegistry.isFormatExtensionSupported("cdl"));
OCIO_CHECK_ASSERT(formatRegistry.isFormatExtensionSupported(".cdl"));
OCIO_CHECK_ASSERT(formatRegistry.isFormatExtensionSupported("Cdl"));
OCIO_CHECK_ASSERT(formatRegistry.isFormatExtensionSupported(".Cdl"));
OCIO_CHECK_ASSERT(formatRegistry.isFormatExtensionSupported("3dl"));
OCIO_CHECK_ASSERT(formatRegistry.isFormatExtensionSupported(".3dl"));
}

OCIO_ADD_TEST(FileTransform, validate)
{
OCIO::FileTransformRcPtr tr = OCIO::FileTransform::Create();
Expand Down
14 changes: 14 additions & 0 deletions tests/python/FileTransformTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ def test_cdlstyle(self):
with self.assertRaises(TypeError):
self.tr.setCDLStyle(invalid)

def test_is_format_extension_supported(self):
"""
Test the IsFormatExtensionSupported() method.
"""
self.assertFalse(self.tr.IsFormatExtensionSupported('foo'))
self.assertFalse(self.tr.IsFormatExtensionSupported('bar'))
self.assertFalse(self.tr.IsFormatExtensionSupported('.'))
self.assertTrue(self.tr.IsFormatExtensionSupported('cdl'))
self.assertTrue(self.tr.IsFormatExtensionSupported('.cdl'))
self.assertTrue(self.tr.IsFormatExtensionSupported('Cdl'))
self.assertTrue(self.tr.IsFormatExtensionSupported('.Cdl'))
self.assertTrue(self.tr.IsFormatExtensionSupported('3dl'))
self.assertTrue(self.tr.IsFormatExtensionSupported('.3dl'))

def test_getformats(self):
"""
Test the getFormats() method.
Expand Down

0 comments on commit 67a26e4

Please sign in to comment.