#pragma once #include #include #include #include #include "aare/Frame.hpp" #include "aare/NDArray.hpp" namespace py = pybind11; // Pass image data back to python as a numpy array // template // py::array return_image_data(pl::ImageData *image) { // py::capsule free_when_done(image, [](void *f) { // pl::ImageData *foo = // reinterpret_cast *>(f); // delete foo; // }); // return py::array_t( // image->shape(), // shape // image->byte_strides(), // C-style contiguous strides for double // image->data(), // the data pointer // free_when_done); // numpy array references this parent // } // template py::array return_vector(std::vector *vec) { // py::capsule free_when_done(vec, [](void *f) { // std::vector *foo = reinterpret_cast *>(f); // delete foo; // }); // return py::array_t({vec->size()}, // shape // {sizeof(T)}, // C-style contiguous strides for double // vec->data(), // the data pointer // free_when_done); // numpy array references this parent // } // template py::array do_read(Reader &r, size_t n_frames) { // py::array image; // if (n_frames == 0) // n_frames = r.total_frames(); // std::array shape{static_cast(n_frames), r.rows(), // r.cols()}; // const uint8_t item_size = r.bytes_per_pixel(); // if (item_size == 1) { // image = py::array_t( // shape); // } else if (item_size == 2) { // image = // py::array_t( // shape); // } else if (item_size == 4) { // image = // py::array_t( // shape); // } // r.read_into(reinterpret_cast(image.mutable_data()), n_frames); // return image; // } py::array return_frame(pl::Frame *ptr) { py::capsule free_when_done(ptr, [](void *f) { pl::Frame *foo = reinterpret_cast(f); delete foo; }); const uint8_t item_size = ptr->bytes_per_pixel(); std::vector shape; for (auto val : ptr->shape()) if (val > 1) shape.push_back(val); std::vector strides; if (shape.size() == 1) strides.push_back(item_size); else if (shape.size() == 2) { strides.push_back(item_size * shape[1]); strides.push_back(item_size); } if (item_size == 1) return py::array_t( shape, strides, reinterpret_cast(ptr->data()), free_when_done); else if (item_size == 2) return py::array_t(shape, strides, reinterpret_cast(ptr->data()), free_when_done); else if (item_size == 4) return py::array_t(shape, strides, reinterpret_cast(ptr->data()), free_when_done); return {}; } // todo rewrite generic template auto get_shape_3d(py::array_t arr) { return pl::Shape<3>{arr.shape(0), arr.shape(1), arr.shape(2)}; } template auto make_span_3d(py::array_t arr) { return pl::DataSpan(arr.mutable_data(), get_shape_3d(arr)); } template auto get_shape_2d(py::array_t arr) { return pl::Shape<2>{arr.shape(0), arr.shape(1)}; } template auto make_span_2d(py::array_t arr) { return pl::DataSpan(arr.mutable_data(), get_shape_2d(arr)); }