From 050bc2a0465ceffc0c3b5c3f3b2f46c50a2505f9 Mon Sep 17 00:00:00 2001 From: Larry Gritz Date: Sat, 30 May 2020 14:08:39 -0700 Subject: [PATCH] TypeDesc additional helpers: Pointer, Vector2, Float4, Vector2i (#2592) Signed-off-by: Larry Gritz --- src/doc/imageioapi.rst | 4 ++- src/doc/pythonbindings.rst | 5 +-- src/include/OpenImageIO/typedesc.h | 20 +++++++++++- src/libutil/typedesc.cpp | 32 +++++++++++-------- src/python/py_typedesc.cpp | 14 ++++++-- testsuite/python-typedesc/ref/out.txt | 16 ++++++++++ .../python-typedesc/src/test_typedesc.py | 8 +++++ 7 files changed, 79 insertions(+), 20 deletions(-) diff --git a/src/doc/imageioapi.rst b/src/doc/imageioapi.rst index 87b0500c4c..081219ca19 100644 --- a/src/doc/imageioapi.rst +++ b/src/doc/imageioapi.rst @@ -42,7 +42,9 @@ in the outer OpenImageIO scope: TypeUnknown TypeFloat TypeColor TypePoint TypeVector TypeNormal TypeMatrix33 TypeMatrix44 TypeMatrix TypeHalf TypeInt TypeUInt TypeInt16 TypeUInt16 TypeInt8 TypeUInt8 - TypeString TypeTimeCode TypeKeyCode TypeFloat4 TypeRational + TypeFloat2 TypeVector2 TypeFloat4 TypeVector2i + TypeString TypeTimeCode TypeKeyCode + TypeRational TypePointer The only types commonly used to store *pixel values* in image files are scalars of ``UINT8``, ``UINT16``, `float`, and ``half`` diff --git a/src/doc/pythonbindings.rst b/src/doc/pythonbindings.rst index 2ee85d2266..6f8ff40309 100644 --- a/src/doc/pythonbindings.rst +++ b/src/doc/pythonbindings.rst @@ -121,8 +121,9 @@ described in detail in Section :ref:`sec-typedesc`, is replicated for Python. .. py:data:: TypeUnknown TypeString TypeFloat TypeHalf TypeInt TypeUInt TypeInt16 TypeUInt16 TypeColor TypePoint TypeVector TypeNormal - TypeFloat4 TypeMatrix TypeMatrix33 - TypeTimeCode TypeKeyCode TypeRational + TypeFloat2 TypeVector2 TypeFloat4 TypeVector2i + TypeMatrix TypeMatrix33 + TypeTimeCode TypeKeyCode TypeRational TypePointer Pre-constructed `TypeDesc` objects for some common types, available in the outer OpenImageIO scope. diff --git a/src/include/OpenImageIO/typedesc.h b/src/include/OpenImageIO/typedesc.h index 6e875145cb..b227a58d13 100644 --- a/src/include/OpenImageIO/typedesc.h +++ b/src/include/OpenImageIO/typedesc.h @@ -27,6 +27,11 @@ #include #include +// Define symbols that let client applications determine if newly added +// features are supported. +#define OIIO_TYPEDESC_VECTOR2 1 + + OIIO_NAMESPACE_BEGIN @@ -292,6 +297,11 @@ struct OIIO_API TypeDesc { || (this->is_sized_array() && b.is_unsized_array())); } + /// Is this a 2-vector aggregate (of the given type, float by default)? + constexpr bool is_vec2 (BASETYPE b=FLOAT) const noexcept { + return this->aggregate == VEC2 && this->basetype == b && !is_array(); + } + /// Is this a 3-vector aggregate (of the given type, float by default)? constexpr bool is_vec3 (BASETYPE b=FLOAT) const noexcept { return this->aggregate == VEC3 && this->basetype == b && !is_array(); @@ -348,6 +358,10 @@ static constexpr TypeDesc TypeNormal (TypeDesc::FLOAT, TypeDesc::VEC3, TypeDesc: static constexpr TypeDesc TypeMatrix33 (TypeDesc::FLOAT, TypeDesc::MATRIX33); static constexpr TypeDesc TypeMatrix44 (TypeDesc::FLOAT, TypeDesc::MATRIX44); static constexpr TypeDesc TypeMatrix = TypeMatrix44; +static constexpr TypeDesc TypeFloat2 (TypeDesc::FLOAT, TypeDesc::VEC2); +static constexpr TypeDesc TypeVector2 (TypeDesc::FLOAT, TypeDesc::VEC2, TypeDesc::VECTOR); +static constexpr TypeDesc TypeFloat4 (TypeDesc::FLOAT, TypeDesc::VEC4); +static constexpr TypeDesc TypeVector4 = TypeFloat4; static constexpr TypeDesc TypeString (TypeDesc::STRING); static constexpr TypeDesc TypeInt (TypeDesc::INT); static constexpr TypeDesc TypeUInt (TypeDesc::UINT); @@ -357,11 +371,12 @@ static constexpr TypeDesc TypeInt16 (TypeDesc::INT16); static constexpr TypeDesc TypeUInt16 (TypeDesc::UINT16); static constexpr TypeDesc TypeInt8 (TypeDesc::INT8); static constexpr TypeDesc TypeUInt8 (TypeDesc::UINT8); +static constexpr TypeDesc TypeVector2i(TypeDesc::INT, TypeDesc::VEC2); static constexpr TypeDesc TypeHalf (TypeDesc::HALF); static constexpr TypeDesc TypeTimeCode (TypeDesc::UINT, TypeDesc::SCALAR, TypeDesc::TIMECODE, 2); static constexpr TypeDesc TypeKeyCode (TypeDesc::INT, TypeDesc::SCALAR, TypeDesc::KEYCODE, 7); -static constexpr TypeDesc TypeFloat4 (TypeDesc::FLOAT, TypeDesc::VEC4); static constexpr TypeDesc TypeRational(TypeDesc::INT, TypeDesc::VEC2, TypeDesc::RATIONAL); +static constexpr TypeDesc TypePointer(TypeDesc::PTR); @@ -413,6 +428,9 @@ template struct TypeDescFromC { static const constexpr TypeDe template struct TypeDescFromC { static const constexpr TypeDesc value() { return TypeDesc::STRING; } }; #ifdef INCLUDED_IMATHVEC_H template<> struct TypeDescFromC { static const constexpr TypeDesc value() { return TypeVector; } }; +template<> struct TypeDescFromC { static const constexpr TypeDesc value() { return TypeVector2; } }; +template<> struct TypeDescFromC { static const constexpr TypeDesc value() { return TypeVector4; } }; +template<> struct TypeDescFromC { static const constexpr TypeDesc value() { return TypeVector2i; } }; #endif #ifdef INCLUDED_IMATHCOLOR_H template<> struct TypeDescFromC { static const constexpr TypeDesc value() { return TypeColor; } }; diff --git a/src/libutil/typedesc.cpp b/src/libutil/typedesc.cpp index ce381cec8c..30d3eb4497 100644 --- a/src/libutil/typedesc.cpp +++ b/src/libutil/typedesc.cpp @@ -170,22 +170,24 @@ TypeDesc::c_str() const std::string result; if (aggregate == SCALAR) result = basetype_name[basetype]; - else if (aggregate == MATRIX44 && basetype == FLOAT) - result = "matrix"; - else if (aggregate == MATRIX33 && basetype == FLOAT) - result = "matrix33"; - else if (aggregate == VEC4 && basetype == FLOAT && vecsemantics == NOXFORM) - result = "float4"; + // else if (aggregate == MATRIX44 && basetype == FLOAT) + // result = "matrix"; + // else if (aggregate == MATRIX33 && basetype == FLOAT) + // result = "matrix33"; + // else if (aggregate == VEC2 && basetype == FLOAT && vecsemantics == NOXFORM) + // result = "float2"; + // else if (aggregate == VEC4 && basetype == FLOAT && vecsemantics == NOXFORM) + // result = "float4"; else if (vecsemantics == NOXFORM) { - const char* agg = ""; switch (aggregate) { - case VEC2: agg = "vec2"; break; - case VEC3: agg = "vec3"; break; - case VEC4: agg = "vec4"; break; - case MATRIX33: agg = "matrix33"; break; - case MATRIX44: agg = "matrix"; break; + case VEC2: result = "float2"; break; + case VEC3: result = "float3"; break; + case VEC4: result = "float4"; break; + case MATRIX33: result = "matrix33"; break; + case MATRIX44: result = "matrix"; break; } - result = std::string(agg) + basetype_code[basetype]; + if (basetype != FLOAT) + result += basetype_code[basetype]; } else { // Special names for vector semantics const char* vec = ""; @@ -276,6 +278,10 @@ TypeDesc::fromstring(string_view typestring) t = TypeMatrix33; else if (type == "matrix" || type == "matrix44") t = TypeMatrix44; + else if (type == "vector2") + t = TypeVector2; + else if (type == "vector4") + t = TypeVector4; else if (type == "timecode") t = TypeTimeCode; else if (type == "rational") diff --git a/src/python/py_typedesc.cpp b/src/python/py_typedesc.cpp index b50b4f755a..8911ffec37 100644 --- a/src/python/py_typedesc.cpp +++ b/src/python/py_typedesc.cpp @@ -113,6 +113,7 @@ declare_typedesc(py::module& m) }) .def("equivalent", &TypeDesc::equivalent) .def("unarray", &TypeDesc::unarray) + .def("is_vec2", &TypeDesc::is_vec2) .def("is_vec3", &TypeDesc::is_vec3) .def("is_vec4", &TypeDesc::is_vec4) @@ -143,7 +144,11 @@ declare_typedesc(py::module& m) .def_readonly_static("TypeTimeCode", &TypeTimeCode) .def_readonly_static("TypeKeyCode", &TypeKeyCode) .def_readonly_static("TypeRational", &TypeRational) - .def_readonly_static("TypeFloat4", &TypeFloat4); + .def_readonly_static("TypeFloat2", &TypeFloat2) + .def_readonly_static("TypeVector2", &TypeVector2) + .def_readonly_static("TypeVector2i", &TypeVector2i) + .def_readonly_static("TypeFloat4", &TypeFloat4) + .def_readonly_static("TypeVector4", &TypeVector4); // Declare that a BASETYPE is implicitly convertible to a TypeDesc. // This keeps us from having to separately declare func(TypeDesc) @@ -155,7 +160,6 @@ declare_typedesc(py::module& m) // foo(TypeUInt8). py::implicitly_convertible(); -#if 1 // Global constants of common TypeDescs m.attr("TypeUnknown") = TypeUnknown; m.attr("TypeFloat") = TypeFloat; @@ -178,9 +182,13 @@ declare_typedesc(py::module& m) m.attr("TypeMatrix44") = TypeMatrix44; m.attr("TypeTimeCode") = TypeTimeCode; m.attr("TypeKeyCode") = TypeKeyCode; + m.attr("TypeFloat2") = TypeFloat2; + m.attr("TypeVector2") = TypeVector2; m.attr("TypeFloat4") = TypeFloat4; + m.attr("TypeVector4") = TypeVector4; + m.attr("TypeVector2i") = TypeVector2i; m.attr("TypeRational") = TypeRational; -#endif + m.attr("TypePointer") = TypePointer; } } // namespace PyOpenImageIO diff --git a/testsuite/python-typedesc/ref/out.txt b/testsuite/python-typedesc/ref/out.txt index 9343155428..25613da754 100644 --- a/testsuite/python-typedesc/ref/out.txt +++ b/testsuite/python-typedesc/ref/out.txt @@ -146,8 +146,16 @@ type 'TypeKeyCode' c_str "keycode" type 'TypeRational' c_str "rational2i" +type 'TypeFloat2' + c_str "float2" +type 'TypeVector2' + c_str "vector2" type 'TypeFloat4' c_str "float4" +type 'TypeVector4' + c_str "float4" +type 'TypeVector2i' + c_str "float2i" type 'TypeHalf' c_str "half" @@ -189,8 +197,16 @@ type 'TypeTimeCode' c_str "timecode" type 'TypeKeyCode' c_str "keycode" +type 'TypeFloat2' + c_str "float2" +type 'TypeVector2' + c_str "vector2" type 'TypeFloat4' c_str "float4" +type 'TypeVector4' + c_str "float4" +type 'TypeVector2i' + c_str "float2i" type 'TypeHalf' c_str "half" type 'TypeRational' diff --git a/testsuite/python-typedesc/src/test_typedesc.py b/testsuite/python-typedesc/src/test_typedesc.py index 3798aecd36..5e92b70744 100755 --- a/testsuite/python-typedesc/src/test_typedesc.py +++ b/testsuite/python-typedesc/src/test_typedesc.py @@ -145,7 +145,11 @@ def breakdown_test(t, name="", verbose=True): breakdown_test (oiio.TypeDesc.TypeTimeCode, "TypeTimeCode", verbose=False) breakdown_test (oiio.TypeDesc.TypeKeyCode, "TypeKeyCode", verbose=False) breakdown_test (oiio.TypeDesc.TypeRational, "TypeRational", verbose=False) + breakdown_test (oiio.TypeDesc.TypeFloat2, "TypeFloat2", verbose=False) + breakdown_test (oiio.TypeDesc.TypeVector2, "TypeVector2", verbose=False) breakdown_test (oiio.TypeDesc.TypeFloat4, "TypeFloat4", verbose=False) + breakdown_test (oiio.TypeDesc.TypeVector4, "TypeVector4", verbose=False) + breakdown_test (oiio.TypeDesc.TypeVector2i, "TypeVector2i", verbose=False) breakdown_test (oiio.TypeDesc.TypeHalf, "TypeHalf", verbose=False) print ("") @@ -169,7 +173,11 @@ def breakdown_test(t, name="", verbose=True): breakdown_test (oiio.TypeMatrix44, "TypeMatrix44", verbose=False) breakdown_test (oiio.TypeTimeCode, "TypeTimeCode", verbose=False) breakdown_test (oiio.TypeKeyCode, "TypeKeyCode", verbose=False) + breakdown_test (oiio.TypeFloat2, "TypeFloat2", verbose=False) + breakdown_test (oiio.TypeVector2, "TypeVector2", verbose=False) breakdown_test (oiio.TypeFloat4, "TypeFloat4", verbose=False) + breakdown_test (oiio.TypeVector4, "TypeVector4", verbose=False) + breakdown_test (oiio.TypeVector2i, "TypeVector2i", verbose=False) breakdown_test (oiio.TypeHalf, "TypeHalf", verbose=False) breakdown_test (oiio.TypeRational, "TypeRational", verbose=False) breakdown_test (oiio.TypeUInt, "TypeUInt", verbose=False)