Skip to content

Commit

Permalink
Python: support uint8 attributes in and out (#3378)
Browse files Browse the repository at this point in the history
This is necessary for dealing correctly with ICCProfile ImageSpec
attributes, which are simply arrays of uint8's.
  • Loading branch information
lgritz committed Apr 1, 2022
1 parent 040b2a9 commit 004a76a
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 10 deletions.
34 changes: 32 additions & 2 deletions src/python/py_oiio.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ template<> struct PyTypeForCType<int> { typedef py::int_ type; };
template<> struct PyTypeForCType<unsigned int> { typedef py::int_ type; };
template<> struct PyTypeForCType<short> { typedef py::int_ type; };
template<> struct PyTypeForCType<unsigned short> { typedef py::int_ type; };
template<> struct PyTypeForCType<char> { typedef py::int_ type; };
template<> struct PyTypeForCType<unsigned char> { typedef py::int_ type; };
template<> struct PyTypeForCType<int64_t> { typedef py::int_ type; };
template<> struct PyTypeForCType<float> { typedef py::float_ type; };
template<> struct PyTypeForCType<half> { typedef py::float_ type; };
Expand Down Expand Up @@ -323,6 +325,12 @@ py_buffer_to_stdvector(std::vector<T>& vals, const py::buffer& obj)
} else if (std::is_same<T, unsigned int>::value
&& binfo.format.basetype == TypeDesc::UINT) {
vals.emplace_back(T(binfo.dataval<unsigned int>(i)));
} else if (std::is_same<T, unsigned char>::value
&& binfo.format.basetype == TypeDesc::UINT8) {
vals.emplace_back(T(binfo.dataval<unsigned char>(i)));
} else if (std::is_same<T, unsigned short>::value
&& binfo.format.basetype == TypeDesc::UINT16) {
vals.emplace_back(T(binfo.dataval<unsigned short>(i)));
} else {
// FIXME? Other cases?
vals.emplace_back(T(42));
Expand Down Expand Up @@ -368,7 +376,7 @@ py_to_stdvector(std::vector<T>& vals, const py::object& obj)
return py_indexable_pod_to_stdvector(vals, obj.cast<py::list>());
}
// Apparently a str can masquerade as a buffer object, so make sure to
// exclude that from teh buffer case.
// exclude that from the buffer case.
if (py::isinstance<py::buffer>(obj) && !py::isinstance<py::str>(obj)) {
return py_buffer_to_stdvector(vals, obj.cast<py::buffer>());
}
Expand Down Expand Up @@ -402,6 +410,18 @@ C_to_tuple(const T* vals, size_t size)
}


template<>
inline py::tuple
C_to_tuple(cspan<unsigned char> vals)
{
size_t size = vals.size();
py::tuple result(size);
for (size_t i = 0; i < size; ++i)
result[i] = static_cast<unsigned char>(vals[i]);
return result;
}


// Special case for TypeDesc
template<>
inline py::tuple
Expand Down Expand Up @@ -452,6 +472,14 @@ attribute_typed(T& myobj, string_view name, TypeDesc type, const POBJ& dataobj)
myobj.attribute(name, type, &vals[0]);
return ok;
}
if (type.basetype == TypeDesc::UINT8) {
std::vector<unsigned char> vals;
bool ok = py_to_stdvector(vals, dataobj);
ok &= (vals.size() == type.numelements() * type.aggregate);
if (ok)
myobj.attribute(name, type, &vals[0]);
return ok;
}
if (type.basetype == TypeDesc::FLOAT) {
std::vector<float> vals;
bool ok = py_to_stdvector(vals, dataobj);
Expand Down Expand Up @@ -496,6 +524,8 @@ getattribute_typed(const T& obj, const std::string& name,
return C_to_val_or_tuple((const short*)data, type);
if (type.basetype == TypeDesc::UINT16)
return C_to_val_or_tuple((const unsigned short*)data, type);
if (type.basetype == TypeDesc::UINT8)
return C_to_val_or_tuple((const unsigned char*)data, type);
if (type.basetype == TypeDesc::FLOAT)
return C_to_val_or_tuple((const float*)data, type);
if (type.basetype == TypeDesc::DOUBLE)
Expand Down Expand Up @@ -596,7 +626,7 @@ case TypeDesc::TYPE: \
nvals)

switch (t.basetype) {
// ParamValue_convert_dispatch(UCHAR);
ParamValue_convert_dispatch(UCHAR);
// ParamValue_convert_dispatch(CHAR);
ParamValue_convert_dispatch(USHORT);
ParamValue_convert_dispatch(SHORT);
Expand Down
2 changes: 1 addition & 1 deletion testsuite/python-imageinput/ref/out-alt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Opened "../oiio-images/tahoe-gps.jpg" as a jpeg
Exif:PixelXDimension = 2048
Exif:PixelYDimension = 1536
Exif:WhiteBalance = 0
GPS:VersionID = None
GPS:VersionID = (2, 2, 0, 0)
GPS:LatitudeRef = "N"
GPS:Latitude = (39.0, 18.0, 24.399999618530273)
GPS:LongitudeRef = "W"
Expand Down
2 changes: 1 addition & 1 deletion testsuite/python-imageinput/ref/out-alt2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Opened "../oiio-images/tahoe-gps.jpg" as a jpeg
Exif:PixelXDimension = 2048
Exif:PixelYDimension = 1536
Exif:WhiteBalance = 0
GPS:VersionID = None
GPS:VersionID = (2, 2, 0, 0)
GPS:LatitudeRef = "N"
GPS:Latitude = (39.0, 18.0, 24.399999618530273)
GPS:LongitudeRef = "W"
Expand Down
2 changes: 1 addition & 1 deletion testsuite/python-imageinput/ref/out-py37-jpeg9d.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Opened "../oiio-images/tahoe-gps.jpg" as a jpeg
Exif:PixelXDimension = 2048
Exif:PixelYDimension = 1536
Exif:WhiteBalance = 0
GPS:VersionID = None
GPS:VersionID = (2, 2, 0, 0)
GPS:LatitudeRef = "N"
GPS:Latitude = (39.0, 18.0, 24.399999618530273)
GPS:LongitudeRef = "W"
Expand Down
2 changes: 1 addition & 1 deletion testsuite/python-imageinput/ref/out-python3-win.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Opened "D:/a/oiio/oiio/build/testsuite/oiio-images/tahoe-gps.jpg" as a jpeg
Exif:PixelXDimension = 2048
Exif:PixelYDimension = 1536
Exif:WhiteBalance = 0
GPS:VersionID = None
GPS:VersionID = (2, 2, 0, 0)
GPS:LatitudeRef = "N"
GPS:Latitude = (39.0, 18.0, 24.399999618530273)
GPS:LongitudeRef = "W"
Expand Down
2 changes: 1 addition & 1 deletion testsuite/python-imageinput/ref/out-python3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Opened "../oiio-images/tahoe-gps.jpg" as a jpeg
Exif:PixelXDimension = 2048
Exif:PixelYDimension = 1536
Exif:WhiteBalance = 0
GPS:VersionID = None
GPS:VersionID = (2, 2, 0, 0)
GPS:LatitudeRef = "N"
GPS:Latitude = (39.0, 18.0, 24.399999618530273)
GPS:LongitudeRef = "W"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Opened "../oiio-images/tahoe-gps.jpg" as a jpeg
Exif:PixelXDimension = 2048
Exif:PixelYDimension = 1536
Exif:WhiteBalance = 0
GPS:VersionID = None
GPS:VersionID = (2, 2, 0, 0)
GPS:LatitudeRef = "N"
GPS:Latitude = (39.0, 18.0, 24.399999618530273)
GPS:LongitudeRef = "W"
Expand Down
2 changes: 1 addition & 1 deletion testsuite/python-imageinput/ref/out-travis.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Opened "../oiio-images/tahoe-gps.jpg" as a jpeg
Exif:PixelXDimension = 2048
Exif:PixelYDimension = 1536
Exif:WhiteBalance = 0
GPS:VersionID = None
GPS:VersionID = (2, 2, 0, 0)
GPS:LatitudeRef = "N"
GPS:Latitude = (39.0, 18.0, 24.399999618530273)
GPS:LongitudeRef = "W"
Expand Down
2 changes: 1 addition & 1 deletion testsuite/python-imageinput/ref/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Opened "../oiio-images/tahoe-gps.jpg" as a jpeg
Exif:PixelXDimension = 2048
Exif:PixelYDimension = 1536
Exif:WhiteBalance = 0
GPS:VersionID = None
GPS:VersionID = (2, 2, 0, 0)
GPS:LatitudeRef = "N"
GPS:Latitude = (39.0, 18.0, 24.399999618530273)
GPS:LongitudeRef = "W"
Expand Down

0 comments on commit 004a76a

Please sign in to comment.