From 3f1bc2a00174cd754f04c758b28f51acbca658b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20R=C3=BCnz?= Date: Thu, 23 May 2019 15:37:17 +0100 Subject: [PATCH 1/2] Overload `CreateImage` to support numpy (RGBA uint8) images --- python/nanovg.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/python/nanovg.cpp b/python/nanovg.cpp index 344d6512dd..eb0027388e 100644 --- a/python/nanovg.cpp +++ b/python/nanovg.cpp @@ -119,6 +119,16 @@ void register_nanovg(py::module &m) { .def("SkewY", &nvgSkewY, "angle"_a) .def("Scale", &nvgScale, "x"_a, "y"_a) .def("CreateImage", &nvgCreateImage, "filename"_a, "imageFlags"_a) + .def("CreateImage", + [](NVGcontext *ctx, const py::array_t& image, int flags){ + if(image.ndim() != 3 || image.shape(2) != 4) + throw std::invalid_argument("Unsupported image type, expected RGBA data."); + return nvgCreateImageRGBA(ctx, + static_cast(image.shape(1)), + static_cast(image.shape(0)), + flags, + image.data()); + }, "image"_a, "imageFlags"_a) .def("DeleteImage", &nvgDeleteImage, "image"_a) .def("LinearGradient", &nvgLinearGradient, "sx"_a, "sy"_a, "ex"_a, "ey"_a, "icol"_a, "ocol"_a) From d795dd988f1bef38934cb6a541f47ab4a3aef4ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20R=C3=BCnz?= Date: Thu, 23 May 2019 16:05:19 +0100 Subject: [PATCH 2/2] Ensure dense packing --- python/nanovg.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/nanovg.cpp b/python/nanovg.cpp index eb0027388e..26a5e8be6a 100644 --- a/python/nanovg.cpp +++ b/python/nanovg.cpp @@ -120,7 +120,8 @@ void register_nanovg(py::module &m) { .def("Scale", &nvgScale, "x"_a, "y"_a) .def("CreateImage", &nvgCreateImage, "filename"_a, "imageFlags"_a) .def("CreateImage", - [](NVGcontext *ctx, const py::array_t& image, int flags){ + [](NVGcontext *ctx, const py::array_t& image, int flags){ if(image.ndim() != 3 || image.shape(2) != 4) throw std::invalid_argument("Unsupported image type, expected RGBA data."); return nvgCreateImageRGBA(ctx,