|
19 | 19 |
|
20 | 20 | #include <fmt/format.h>
|
21 | 21 | #include <fmt/ranges.h>
|
22 |
| -#include <pybind11/numpy.h> |
| 22 | +#include <Python.h> |
| 23 | +#include <pybind11/buffer_info.h> |
23 | 24 | #include <pybind11/operators.h>
|
24 | 25 | #include <pybind11/pybind11.h>
|
| 26 | +#include <pybind11/pytypes.h> |
25 | 27 | #include <pybind11/stl.h>
|
26 | 28 |
|
27 | 29 | #include <cucim/cuimage.h>
|
@@ -445,11 +447,30 @@ py::object py_read_region(const CuImage& cuimg,
|
445 | 447 | {
|
446 | 448 | py::gil_scoped_acquire scope_guard;
|
447 | 449 |
|
448 |
| - auto arr = pybind11::array_t<int64_t, py::array::c_style | py::array::forcecast>::ensure(location); |
449 |
| - if (arr) // fast copy |
| 450 | + py::object mv_obj = py::none(); |
| 451 | + try |
450 | 452 | {
|
451 |
| - py::buffer_info buf = arr.request(); |
452 |
| - int64_t* data_array = static_cast<int64_t*>(buf.ptr); |
| 453 | + mv_obj = py::cast<py::object>(py::memoryview(location)); |
| 454 | + } |
| 455 | + catch (const std::exception& e) |
| 456 | + { |
| 457 | + } |
| 458 | + |
| 459 | + if (!mv_obj.is_none()) // fast copy |
| 460 | + { |
| 461 | + py::memoryview mv(mv_obj); |
| 462 | + py::buffer_info buf(PyMemoryView_GET_BUFFER(mv.ptr()), false); |
| 463 | + |
| 464 | + if (buf.format != py::format_descriptor<int64_t>::format()) |
| 465 | + { |
| 466 | + throw std::invalid_argument("Expected int64 array-like"); |
| 467 | + } |
| 468 | + if (PyBuffer_IsContiguous(buf.view(), 'C') == 0) |
| 469 | + { |
| 470 | + throw std::invalid_argument("Expected C-contiguous array-like"); |
| 471 | + } |
| 472 | + |
| 473 | + const int64_t* data_array = static_cast<const int64_t*>(buf.ptr); |
453 | 474 | ssize_t data_size = buf.size;
|
454 | 475 | locations.reserve(data_size);
|
455 | 476 | locations.insert(locations.end(), &data_array[0], &data_array[data_size]);
|
|
0 commit comments