diff --git a/python/src/ctb_raw_file.hpp b/python/src/ctb_raw_file.hpp index 575d449..a88a9d1 100644 --- a/python/src/ctb_raw_file.hpp +++ b/python/src/ctb_raw_file.hpp @@ -67,56 +67,54 @@ m.def("adc_sar_04_decode64to16", [](py::array_t input) { return output; }); +m.def( + "apply_custom_weights", + [](py::array_t &input, + py::array_t + &weights) { + -m.def("apply_custom_weights", [](py::array_t& input, py::array_t& weights) { - if (input.ndim() != 1) { - throw std::runtime_error("Only 1D arrays are supported at this moment"); - } + // Create new array with same shape as the input array (uninitialized values) + py::buffer_info buf = input.request(); + py::array_t output(buf.shape); - // Create a 1D output array with the same shape as the input - std::vector shape{input.shape(0)}; - py::array_t output(shape); + // Use NDViews to call into the C++ library + auto weights_view = make_view_1d(weights); + NDView input_view(input.mutable_data(), {input.size()}); + NDView output_view(output.mutable_data(), {output.size()}); - auto weights_view = make_view_1d(weights); + apply_custom_weights(input_view, output_view, weights_view); + return output; + }); - // Create a view of the input and output arrays - NDView input_view(input.mutable_data(), {input.shape(0)}); - NDView output_view(output.mutable_data(), {output.shape(0)}); +py::class_(m, "CtbRawFile") + .def(py::init()) + .def("read_frame", + [](CtbRawFile &self) { + size_t image_size = self.image_size_in_bytes(); + py::array image; + std::vector shape; + shape.reserve(2); + shape.push_back(1); + shape.push_back(image_size); - apply_custom_weights(input_view, output_view, weights_view); + py::array_t header(1); - return output; -}); + // always read bytes + image = py::array_t(shape); - py::class_(m, "CtbRawFile") - .def(py::init()) - .def("read_frame", - [](CtbRawFile &self) { - size_t image_size = self.image_size_in_bytes(); - py::array image; - std::vector shape; - shape.reserve(2); - shape.push_back(1); - shape.push_back(image_size); + self.read_into(reinterpret_cast(image.mutable_data()), + header.mutable_data()); - py::array_t header(1); + return py::make_tuple(header, image); + }) + .def("seek", &CtbRawFile::seek) + .def("tell", &CtbRawFile::tell) + .def("master", &CtbRawFile::master) - // always read bytes - image = py::array_t(shape); + .def_property_readonly("image_size_in_bytes", + &CtbRawFile::image_size_in_bytes) - self.read_into( - reinterpret_cast(image.mutable_data()), - header.mutable_data()); - - return py::make_tuple(header, image); - }) - .def("seek", &CtbRawFile::seek) - .def("tell", &CtbRawFile::tell) - .def("master", &CtbRawFile::master) - - .def_property_readonly("image_size_in_bytes", - &CtbRawFile::image_size_in_bytes) - - .def_property_readonly("frames_in_file", &CtbRawFile::frames_in_file); + .def_property_readonly("frames_in_file", &CtbRawFile::frames_in_file); }