From 67a26e4a383d2125bae437432cdad535d55c751b Mon Sep 17 00:00:00 2001 From: Mei Chu <33743518+meimchu@users.noreply.github.com> Date: Wed, 3 Apr 2024 15:57:09 -0700 Subject: [PATCH] Add a helper function to check if an extension is supported by file transform (#1962) * Add FileTransform::IsFormatExtensionSupported() Signed-off-by: Mei Chu * IsFormatExtensionSupported ignores dot at start. Signed-off-by: Mei Chu * Add C++ tests for new function. Signed-off-by: Mei Chu * Fix a small bug in the new C++ tests. Signed-off-by: Mei Chu * Add python binding, python tests and address feedbacks. Signed-off-by: Mei Chu * Change extension accepted to IsFormatExtensionSupported() to case-insensitive. Signed-off-by: Mei Chu * Bugfix to account for single dot input. Signed-off-by: Mei Chu * Cleanup isFormatExtensionSupported() structure a bit to look nicer. Signed-off-by: Mei Chu * Reset whitespace cleanup. Also add a guard against invalid pointer. Signed-off-by: Mei Chu --------- Signed-off-by: Mei Chu Co-authored-by: Doug Walker --- include/OpenColorIO/OpenColorTransforms.h | 7 +++++ src/OpenColorIO/transforms/FileTransform.cpp | 31 +++++++++++++++++++ src/OpenColorIO/transforms/FileTransform.h | 1 + .../python/transforms/PyFileTransform.cpp | 4 ++- tests/cpu/transforms/FileTransform_tests.cpp | 14 +++++++++ tests/python/FileTransformTest.py | 14 +++++++++ 6 files changed, 70 insertions(+), 1 deletion(-) diff --git a/include/OpenColorIO/OpenColorTransforms.h b/include/OpenColorIO/OpenColorTransforms.h index 19bf1496a4..e37c59e4c2 100644 --- a/include/OpenColorIO/OpenColorTransforms.h +++ b/include/OpenColorIO/OpenColorTransforms.h @@ -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). diff --git a/src/OpenColorIO/transforms/FileTransform.cpp b/src/OpenColorIO/transforms/FileTransform.cpp index 9aeae8df43..faf1d343e3 100755 --- a/src/OpenColorIO/transforms/FileTransform.cpp +++ b/src/OpenColorIO/transforms/FileTransform.cpp @@ -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 << "