Skip to content

Commit

Permalink
TypeDesc additional helpers: Pointer, Vector2, Float4, Vector2i (#2592)
Browse files Browse the repository at this point in the history
Signed-off-by: Larry Gritz <[email protected]>
  • Loading branch information
lgritz committed May 30, 2020
1 parent 0cac283 commit 050bc2a
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 20 deletions.
4 changes: 3 additions & 1 deletion src/doc/imageioapi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down
5 changes: 3 additions & 2 deletions src/doc/pythonbindings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 19 additions & 1 deletion src/include/OpenImageIO/typedesc.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
#include <OpenImageIO/oiioversion.h>
#include <OpenImageIO/string_view.h>

// Define symbols that let client applications determine if newly added
// features are supported.
#define OIIO_TYPEDESC_VECTOR2 1



OIIO_NAMESPACE_BEGIN

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);



Expand Down Expand Up @@ -413,6 +428,9 @@ template<size_t S> struct TypeDescFromC<char[S]> { static const constexpr TypeDe
template<size_t S> struct TypeDescFromC<const char[S]> { static const constexpr TypeDesc value() { return TypeDesc::STRING; } };
#ifdef INCLUDED_IMATHVEC_H
template<> struct TypeDescFromC<Imath::V3f> { static const constexpr TypeDesc value() { return TypeVector; } };
template<> struct TypeDescFromC<Imath::V2f> { static const constexpr TypeDesc value() { return TypeVector2; } };
template<> struct TypeDescFromC<Imath::V4f> { static const constexpr TypeDesc value() { return TypeVector4; } };
template<> struct TypeDescFromC<Imath::V2i> { static const constexpr TypeDesc value() { return TypeVector2i; } };
#endif
#ifdef INCLUDED_IMATHCOLOR_H
template<> struct TypeDescFromC<Imath::Color3f> { static const constexpr TypeDesc value() { return TypeColor; } };
Expand Down
32 changes: 19 additions & 13 deletions src/libutil/typedesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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")
Expand Down
14 changes: 11 additions & 3 deletions src/python/py_typedesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -155,7 +160,6 @@ declare_typedesc(py::module& m)
// foo(TypeUInt8).
py::implicitly_convertible<py::str, TypeDesc>();

#if 1
// Global constants of common TypeDescs
m.attr("TypeUnknown") = TypeUnknown;
m.attr("TypeFloat") = TypeFloat;
Expand All @@ -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
16 changes: 16 additions & 0 deletions testsuite/python-typedesc/ref/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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'
Expand Down
8 changes: 8 additions & 0 deletions testsuite/python-typedesc/src/test_typedesc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ("")

Expand All @@ -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)
Expand Down

0 comments on commit 050bc2a

Please sign in to comment.