From d20600171acffaa67b32a32c274364d3071eafef Mon Sep 17 00:00:00 2001 From: Dustin Spicuzza Date: Tue, 15 Sep 2026 00:52:35 -0400 Subject: [PATCH] Update to cvnp 721e84e3cd3f5abd932c6a34cc8dd62096908550 --- subprojects/robotpy-cscore/cscore/cvnp/cvnp.h | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/subprojects/robotpy-cscore/cscore/cvnp/cvnp.h b/subprojects/robotpy-cscore/cscore/cvnp/cvnp.h index c78a9d112..12189e914 100644 --- a/subprojects/robotpy-cscore/cscore/cvnp/cvnp.h +++ b/subprojects/robotpy-cscore/cscore/cvnp/cvnp.h @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -61,9 +62,18 @@ namespace cvnp if (a.size() != mat_size) throw std::runtime_error("Bad size"); - _Tp* arrayValues = (_Tp*) a.data(0); - for (size_t i = 0; i < mat_size; ++i) - out_matrix.val[i] = arrayValues[i]; + using ExpectedArray = pybind11::array_t<_Tp, pybind11::array::c_style>; + const auto dtype = a.dtype(); + const auto byte_order = dtype.byteorder(); + if (!ExpectedArray::check_(a) || dtype.itemsize() != sizeof(_Tp) + || (byte_order != '=' && byte_order != '|')) + throw std::invalid_argument("Incompatible numpy array dtype or layout"); + + for (pybind11::ssize_t axis = 0; axis < a.ndim(); ++axis) + if (a.strides(axis) <= 0) + throw std::invalid_argument("Incompatible numpy array stride"); + + std::memcpy(out_matrix.val, a.data(), mat_size * sizeof(_Tp)); } } // namespace cvnp