Skip to content

Commit 67a26e4

Browse files
meimchudoug-walker
andauthored
Add a helper function to check if an extension is supported by file transform (#1962)
* Add FileTransform::IsFormatExtensionSupported() Signed-off-by: Mei Chu <meimchu@gmail.com> * IsFormatExtensionSupported ignores dot at start. Signed-off-by: Mei Chu <meimchu@gmail.com> * Add C++ tests for new function. Signed-off-by: Mei Chu <meimchu@gmail.com> * Fix a small bug in the new C++ tests. Signed-off-by: Mei Chu <meimchu@gmail.com> * Add python binding, python tests and address feedbacks. Signed-off-by: Mei Chu <meimchu@gmail.com> * Change extension accepted to IsFormatExtensionSupported() to case-insensitive. Signed-off-by: Mei Chu <meimchu@gmail.com> * Bugfix to account for single dot input. Signed-off-by: Mei Chu <meimchu@gmail.com> * Cleanup isFormatExtensionSupported() structure a bit to look nicer. Signed-off-by: Mei Chu <meimchu@gmail.com> * Reset whitespace cleanup. Also add a guard against invalid pointer. Signed-off-by: Mei Chu <meimchu@gmail.com> --------- Signed-off-by: Mei Chu <meimchu@gmail.com> Co-authored-by: Doug Walker <doug.walker@autodesk.com>
1 parent b87d80d commit 67a26e4

File tree

6 files changed

+70
-1
lines changed

6 files changed

+70
-1
lines changed

include/OpenColorIO/OpenColorTransforms.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,13 @@ class OCIOEXPORT FileTransform : public Transform
10701070
static const char * GetFormatNameByIndex(int index);
10711071
/// Get the LUT reader extension at index, return empty string if an invalid index is specified.
10721072
static const char * GetFormatExtensionByIndex(int index);
1073+
/// Returns true if the extension corresponds to a format supported by FileTransform.
1074+
/// The argument is case-insensitive, and a leading dot, if present, is ignored.
1075+
/// Note that FileTransform will attempt all format readers on a given file until it is
1076+
/// successful, even files that contain an unsupported extension or no extension.
1077+
/// However, this function is useful for applications that want to know which files are likely
1078+
/// to be LUT files, based on their extension.
1079+
static bool IsFormatExtensionSupported(const char * extension);
10731080

10741081
FileTransform & operator=(const FileTransform &) = delete;
10751082
/// Do not use (needed only for pybind11).

src/OpenColorIO/transforms/FileTransform.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ const char * FileTransform::GetFormatExtensionByIndex(int index)
158158
return FormatRegistry::GetInstance().getFormatExtensionByIndex(FORMAT_CAPABILITY_READ, index);
159159
}
160160

161+
bool FileTransform::IsFormatExtensionSupported(const char * extension)
162+
{
163+
return FormatRegistry::GetInstance().isFormatExtensionSupported(extension);
164+
}
165+
161166
std::ostream& operator<< (std::ostream& os, const FileTransform& t)
162167
{
163168
os << "<FileTransform ";
@@ -522,6 +527,32 @@ const char * FormatRegistry::getFormatExtensionByIndex(int capability, int index
522527
return "";
523528
}
524529

530+
bool FormatRegistry::isFormatExtensionSupported(const char * extension) const
531+
{
532+
// Early return false with an input of just the dot or invalid pointer.
533+
if (!extension || !*extension || 0 == strcmp(extension, "."))
534+
{
535+
return false;
536+
}
537+
538+
// If dot is present at the start, pointer arithmetic increment up by one to ignore that dot.
539+
FileFormatVectorMap::const_iterator iter;
540+
if (extension[0] == '.')
541+
{
542+
iter = m_formatsByExtension.find(StringUtils::Lower(extension + 1));
543+
}
544+
else
545+
{
546+
iter = m_formatsByExtension.find(StringUtils::Lower(extension));
547+
}
548+
549+
if (iter != m_formatsByExtension.end())
550+
{
551+
return true;
552+
}
553+
return false;
554+
}
555+
525556
///////////////////////////////////////////////////////////////////////////
526557

527558
FileFormat::~FileFormat()

src/OpenColorIO/transforms/FileTransform.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class FormatRegistry
137137
int getNumFormats(int capability) const noexcept;
138138
const char * getFormatNameByIndex(int capability, int index) const noexcept;
139139
const char * getFormatExtensionByIndex(int capability, int index) const noexcept;
140+
bool isFormatExtensionSupported(const char * extension) const;
140141
private:
141142
FormatRegistry();
142143
~FormatRegistry();

src/bindings/python/transforms/PyFileTransform.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ void bindPyFileTransform(py::module & m)
7272
.def("getInterpolation", &FileTransform::getInterpolation,
7373
DOC(FileTransform, getInterpolation))
7474
.def("setInterpolation", &FileTransform::setInterpolation, "interpolation"_a,
75-
DOC(FileTransform, setInterpolation));
75+
DOC(FileTransform, setInterpolation))
76+
.def_static("IsFormatExtensionSupported", &FileTransform::IsFormatExtensionSupported, "extension"_a,
77+
DOC(FileTransform, IsFormatExtensionSupported));
7678

7779
defRepr(clsFileTransform);
7880

tests/cpu/transforms/FileTransform_tests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,20 @@ OCIO_ADD_TEST(FileTransform, format_by_index)
290290
ValidateFormatByIndex(formatRegistry, OCIO::FORMAT_CAPABILITY_READ);
291291
}
292292

293+
OCIO_ADD_TEST(FileTransform, is_format_extension_supported)
294+
{
295+
OCIO::FormatRegistry & formatRegistry = OCIO::FormatRegistry::GetInstance();
296+
OCIO_CHECK_EQUAL(formatRegistry.isFormatExtensionSupported("foo"), false);
297+
OCIO_CHECK_EQUAL(formatRegistry.isFormatExtensionSupported("bar"), false);
298+
OCIO_CHECK_EQUAL(formatRegistry.isFormatExtensionSupported("."), false);
299+
OCIO_CHECK_ASSERT(formatRegistry.isFormatExtensionSupported("cdl"));
300+
OCIO_CHECK_ASSERT(formatRegistry.isFormatExtensionSupported(".cdl"));
301+
OCIO_CHECK_ASSERT(formatRegistry.isFormatExtensionSupported("Cdl"));
302+
OCIO_CHECK_ASSERT(formatRegistry.isFormatExtensionSupported(".Cdl"));
303+
OCIO_CHECK_ASSERT(formatRegistry.isFormatExtensionSupported("3dl"));
304+
OCIO_CHECK_ASSERT(formatRegistry.isFormatExtensionSupported(".3dl"));
305+
}
306+
293307
OCIO_ADD_TEST(FileTransform, validate)
294308
{
295309
OCIO::FileTransformRcPtr tr = OCIO::FileTransform::Create();

tests/python/FileTransformTest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ def test_cdlstyle(self):
8585
with self.assertRaises(TypeError):
8686
self.tr.setCDLStyle(invalid)
8787

88+
def test_is_format_extension_supported(self):
89+
"""
90+
Test the IsFormatExtensionSupported() method.
91+
"""
92+
self.assertFalse(self.tr.IsFormatExtensionSupported('foo'))
93+
self.assertFalse(self.tr.IsFormatExtensionSupported('bar'))
94+
self.assertFalse(self.tr.IsFormatExtensionSupported('.'))
95+
self.assertTrue(self.tr.IsFormatExtensionSupported('cdl'))
96+
self.assertTrue(self.tr.IsFormatExtensionSupported('.cdl'))
97+
self.assertTrue(self.tr.IsFormatExtensionSupported('Cdl'))
98+
self.assertTrue(self.tr.IsFormatExtensionSupported('.Cdl'))
99+
self.assertTrue(self.tr.IsFormatExtensionSupported('3dl'))
100+
self.assertTrue(self.tr.IsFormatExtensionSupported('.3dl'))
101+
88102
def test_getformats(self):
89103
"""
90104
Test the getFormats() method.

0 commit comments

Comments
 (0)