| 124 | } |
| 125 | |
| 126 | py::array BuildNumpyArray(const Image& img, const void* data, bool writeable, py::capsule capsule) |
| 127 | { |
| 128 | auto dtype = PixelTypeToDType(img.GetPixelType()); |
| 129 | auto format = PixelTypeFormat(img.GetPixelType()); |
| 130 | auto shape = ComputeNumpyShape(img); |
| 131 | |
| 132 | std::vector<py::ssize_t> strides(shape.size()); |
| 133 | strides.back() = dtype.itemsize(); |
| 134 | for (py::ssize_t i = static_cast<py::ssize_t>(shape.size()) - 2; i >= 0; --i) |
| 135 | strides[i] = strides[i + 1] * shape[i + 1]; |
| 136 | |
| 137 | // pybind11's buffer_info stores a non-const void*. Mirror what the |
| 138 | // library itself does in its typed buffer_info(const T*, ...) overload: |
| 139 | // const_cast the pointer here and let the readonly flag enforce |
| 140 | // const-ness at the Python boundary. |
| 141 | py::array arr( |
| 142 | py::buffer_info( |
| 143 | const_cast<void*>(data), |
| 144 | dtype.itemsize(), |
| 145 | format, |
| 146 | shape.size(), |
| 147 | shape, |
| 148 | strides, |
| 149 | !writeable |
| 150 | ), |
| 151 | capsule |
| 152 | ); |
| 153 | |
| 154 | if (!writeable) |
| 155 | arr.attr("flags").attr("writeable") = py::bool_(false); |
| 156 | |
| 157 | return arr; |
| 158 | } |
| 159 | |
| 160 | py::array AsNumpyDirect(Image& img, bool writeable, mitk::TimeStepType time_step) |
| 161 | { |
no test coverage detected