From 7758739102f2b0bc5f265537551383d36007ccef Mon Sep 17 00:00:00 2001 From: Sameer Sheorey Date: Tue, 3 Oct 2023 21:56:29 -0700 Subject: [PATCH] Throw error on non-contiguous Python buffer provided to Image ctor. --- cpp/pybind/geometry/image.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cpp/pybind/geometry/image.cpp b/cpp/pybind/geometry/image.cpp index 0a18be65e7f..70e63184e64 100644 --- a/cpp/pybind/geometry/image.cpp +++ b/cpp/pybind/geometry/image.cpp @@ -86,6 +86,13 @@ void pybind_image(py::module &m) { } height = (int)info.shape[0]; width = (int)info.shape[1]; + if (info.strides[1] != num_of_channels * bytes_per_channel || + info.strides[0] != + width * num_of_channels * bytes_per_channel) { + throw std::runtime_error( + "Image can only be initialized from a contiguous " + "buffer."); + } auto img = new Image(); img->Prepare(width, height, num_of_channels, bytes_per_channel); memcpy(img->data_.data(), info.ptr, img->data_.size());