| 384 | #endif |
| 385 | |
| 386 | inline pybind11::ndarray<pybind11::numpy> buffer_to_numpy(const Buffer& self) |
| 387 | { |
| 388 | size_t bufferSize = self.getSize(); |
| 389 | void* cpuData = new uint8_t[bufferSize]; |
| 390 | self.getBlob(cpuData, 0, bufferSize); |
| 391 | |
| 392 | pybind11::capsule owner(cpuData, [](void* p) noexcept { delete[] reinterpret_cast<uint8_t*>(p); }); |
| 393 | |
| 394 | if (auto dtype = resourceFormatToDtype(self.getFormat())) |
| 395 | { |
| 396 | uint32_t channelCount = getFormatChannelCount(self.getFormat()); |
| 397 | if (channelCount == 1) |
| 398 | { |
| 399 | pybind11::size_t shape[1] = {self.getElementCount()}; |
| 400 | return pybind11::ndarray<pybind11::numpy>(cpuData, 1, shape, owner, nullptr, *dtype, pybind11::device::cpu::value); |
| 401 | } |
| 402 | else |
| 403 | { |
| 404 | pybind11::size_t shape[2] = {self.getElementCount(), channelCount}; |
| 405 | return pybind11::ndarray<pybind11::numpy>(cpuData, 2, shape, owner, nullptr, *dtype, pybind11::device::cpu::value); |
| 406 | } |
| 407 | } |
| 408 | else |
| 409 | { |
| 410 | pybind11::size_t shape[1] = {bufferSize}; |
| 411 | return pybind11::ndarray<pybind11::numpy>( |
| 412 | cpuData, 1, shape, owner, nullptr, pybind11::dtype<uint8_t>(), pybind11::device::cpu::value |
| 413 | ); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | inline void buffer_from_numpy(Buffer& self, pybind11::ndarray<pybind11::numpy> data) |
| 418 | { |
nothing calls this directly
no test coverage detected