Skip to content

Commit

Permalink
Fix inability for python to set timecode (AcademySoftwareFoundation#2279
Browse files Browse the repository at this point in the history
)

It actually boiled down to trouble with setting unsigned int array attribute
from Python int tuples.

Make sure to test this in testsuite/python-imagespec
  • Loading branch information
lgritz committed Jul 4, 2019
1 parent 7c050d7 commit 64c0340
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
12 changes: 11 additions & 1 deletion src/python/py_oiio.h
Expand Up @@ -105,9 +105,12 @@ py_indexable_pod_to_stdvector(std::vector<T>& vals, const PYT& obj)
} else if ((std::is_same<T, float>::value || std::is_same<T, int>::value)
&& py::isinstance<py::int_>(elem)) {
vals.emplace_back(elem.template cast<int>());
} else if (std::is_same<T, unsigned int>::value
&& py::isinstance<py::int_>(elem)) {
vals.emplace_back(elem.template cast<unsigned int>());
} else {
// FIXME? Other cases?
vals.emplace_back(T(0));
vals.emplace_back(T(42));
ok = false;
}
}
Expand Down Expand Up @@ -304,6 +307,13 @@ attribute_typed(T& myobj, string_view name, TypeDesc type, const POBJ& dataobj)
myobj.attribute(name, type, &vals[0]);
return;
}
if (type.basetype == TypeDesc::UINT) {
std::vector<unsigned int> vals;
py_to_stdvector(vals, dataobj);
if (vals.size() == type.numelements() * type.aggregate)
myobj.attribute(name, type, &vals[0]);
return;
}
if (type.basetype == TypeDesc::FLOAT) {
std::vector<float> vals;
py_to_stdvector(vals, dataobj);
Expand Down
13 changes: 9 additions & 4 deletions testsuite/python-imagespec/ref/out-python3.txt
Expand Up @@ -87,11 +87,12 @@ getattribute('foo_str') retrieves blah
getattribute('foo_vector') retrieves (1.0, 0.0, 11.0)
getattribute('foo_matrix') retrieves (1.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 3.0, 1.0)
getattribute('foo_no') retrieves None
getattribute('smpte:TimeCode') retrieves (18356486, 4294967295)
s['delfoo_float'] = 99.5
s['delfoo_int'] = 29
s['delfoo_str'] = egg

extra_attribs size is 8
extra_attribs size is 9
0 foo_str string blah
"blah"
1 foo_int int 14
Expand All @@ -102,11 +103,13 @@ extra_attribs size is 8
1, 0, 11
4 foo_matrix matrix (1.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 3.0, 1.0)
1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 1, 2, 3, 1
5 delfoo_str string egg
5 smpte:TimeCode timecode (18356486, 4294967295)
01:18:19:06
6 delfoo_str string egg
"egg"
6 delfoo_int int 29
7 delfoo_int int 29
29
7 delfoo_float float 99.5
8 delfoo_float float 99.5
99.5

seralize(xml):
Expand Down Expand Up @@ -143,6 +146,7 @@ seralize(xml):
<attrib name="foo_float" type="float">3.14</attrib>
<attrib name="foo_vector" type="vector">1, 0, 11</attrib>
<attrib name="foo_matrix" type="matrix">1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 1, 2, 3, 1</attrib>
<attrib name="smpte:TimeCode" type="timecode" description="01:18:19:06">01:18:19:06</attrib>
<attrib name="delfoo_str" type="string">egg</attrib>
<attrib name="delfoo_int" type="int">29</attrib>
<attrib name="delfoo_float" type="float">99.5</attrib>
Expand All @@ -163,6 +167,7 @@ serialize(text, human):
foo_matrix: 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 1, 2, 3, 1
foo_str: "blah"
foo_vector: 1, 0, 11
smpte:TimeCode: 01:18:19:06

Testing construction from ROI:
resolution (width,height,depth) = 640 480 1
Expand Down
13 changes: 9 additions & 4 deletions testsuite/python-imagespec/ref/out.txt
Expand Up @@ -87,11 +87,12 @@ getattribute('foo_str') retrieves blah
getattribute('foo_vector') retrieves (1.0, 0.0, 11.0)
getattribute('foo_matrix') retrieves (1.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 3.0, 1.0)
getattribute('foo_no') retrieves None
getattribute('smpte:TimeCode') retrieves (18356486L, 4294967295L)
s['delfoo_float'] = 99.5
s['delfoo_int'] = 29
s['delfoo_str'] = egg

extra_attribs size is 8
extra_attribs size is 9
0 foo_str string blah
"blah"
1 foo_int int 14
Expand All @@ -102,11 +103,13 @@ extra_attribs size is 8
1, 0, 11
4 foo_matrix matrix (1.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 3.0, 1.0)
1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 1, 2, 3, 1
5 delfoo_str string egg
5 smpte:TimeCode timecode (18356486L, 4294967295L)
01:18:19:06
6 delfoo_str string egg
"egg"
6 delfoo_int int 29
7 delfoo_int int 29
29
7 delfoo_float float 99.5
8 delfoo_float float 99.5
99.5

seralize(xml):
Expand Down Expand Up @@ -143,6 +146,7 @@ seralize(xml):
<attrib name="foo_float" type="float">3.14</attrib>
<attrib name="foo_vector" type="vector">1, 0, 11</attrib>
<attrib name="foo_matrix" type="matrix">1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 1, 2, 3, 1</attrib>
<attrib name="smpte:TimeCode" type="timecode" description="01:18:19:06">01:18:19:06</attrib>
<attrib name="delfoo_str" type="string">egg</attrib>
<attrib name="delfoo_int" type="int">29</attrib>
<attrib name="delfoo_float" type="float">99.5</attrib>
Expand All @@ -163,6 +167,7 @@ serialize(text, human):
foo_matrix: 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 1, 2, 3, 1
foo_str: "blah"
foo_vector: 1, 0, 11
smpte:TimeCode: 01:18:19:06

Testing construction from ROI:
resolution (width,height,depth) = 640 480 1
Expand Down
2 changes: 2 additions & 0 deletions testsuite/python-imagespec/src/test_imagespec.py
Expand Up @@ -89,6 +89,7 @@ def print_imagespec (spec, msg="") :
s.attribute ("foo_vector", oiio.TypeDesc.TypeVector, (1, 0, 11))
s.attribute ("foo_matrix", oiio.TypeDesc.TypeMatrix,
(1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 1, 2, 3, 1))
s.attribute ("smpte:TimeCode", oiio.TypeDesc.TypeTimeCode, (18356486, 4294967295))
s["delfoo_str"] = "egg"
s["delfoo_int"] = 29
s["delfoo_float"] = 99.5
Expand All @@ -108,6 +109,7 @@ def print_imagespec (spec, msg="") :
print ("getattribute('foo_vector') retrieves", s.getattribute("foo_vector"))
print ("getattribute('foo_matrix') retrieves", s.getattribute("foo_matrix"))
print ("getattribute('foo_no') retrieves", s.getattribute("foo_no"))
print ("getattribute('smpte:TimeCode') retrieves", s.getattribute("smpte:TimeCode"))
print ("s['delfoo_float'] =", s['delfoo_float'])
print ("s['delfoo_int'] =", s['delfoo_int'])
print ("s['delfoo_str'] =", s['delfoo_str'])
Expand Down

0 comments on commit 64c0340

Please sign in to comment.