Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3fde41c

Browse files
committed
Convert some Agg array_view to pybind11
This only does the simple ones that are not shared with the path extension. Those more complex ones will be done separately.
1 parent 9cbff61 commit 3fde41c

File tree

2 files changed

+19
-33
lines changed

2 files changed

+19
-33
lines changed

src/_backend_agg.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <cmath>
1212
#include <algorithm>
13+
#include <functional>
1314

1415
#include "agg_alpha_mask_u8.h"
1516
#include "agg_conv_curve.h"
@@ -732,7 +733,7 @@ inline void RendererAgg::draw_text_image(GCAgg &gc, ImageArray &image, int x, in
732733
rendererBase.reset_clipping(true);
733734
if (angle != 0.0) {
734735
agg::rendering_buffer srcbuf(
735-
image.data(), (unsigned)image.shape(1),
736+
image.mutable_data(0, 0), (unsigned)image.shape(1),
736737
(unsigned)image.shape(0), (unsigned)image.shape(1));
737738
agg::pixfmt_gray8 pixf_img(srcbuf);
738739

@@ -832,8 +833,9 @@ inline void RendererAgg::draw_image(GCAgg &gc,
832833
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans, gc.snap_mode);
833834

834835
agg::rendering_buffer buffer;
835-
buffer.attach(
836-
image.data(), (unsigned)image.shape(1), (unsigned)image.shape(0), -(int)image.shape(1) * 4);
836+
buffer.attach(image.mutable_data(0, 0, 0),
837+
(unsigned)image.shape(1), (unsigned)image.shape(0),
838+
-(int)image.shape(1) * 4);
837839
pixfmt pixf(buffer);
838840

839841
if (has_clippath) {
@@ -1249,8 +1251,8 @@ inline void RendererAgg::draw_gouraud_triangles(GCAgg &gc,
12491251
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans, gc.snap_mode);
12501252

12511253
for (int i = 0; i < points.shape(0); ++i) {
1252-
typename PointArray::sub_t point = points.subarray(i);
1253-
typename ColorArray::sub_t color = colors.subarray(i);
1254+
auto point = std::bind(points, i, std::placeholders::_1, std::placeholders::_2);
1255+
auto color = std::bind(colors, i, std::placeholders::_1, std::placeholders::_2);
12541256

12551257
_draw_gouraud_triangle(point, color, trans, has_clippath);
12561258
}

src/_backend_agg_wrapper.cpp

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,14 @@ PyRendererAgg_draw_path(RendererAgg *self,
5959

6060
static void
6161
PyRendererAgg_draw_text_image(RendererAgg *self,
62-
py::array_t<agg::int8u, py::array::c_style> image_obj,
62+
py::array_t<agg::int8u, py::array::c_style | py::array::forcecast> image_obj,
6363
double x,
6464
double y,
6565
double angle,
6666
GCAgg &gc)
6767
{
68-
numpy::array_view<agg::int8u, 2> image;
69-
70-
if (!image.converter_contiguous(image_obj.ptr(), &image)) {
71-
throw py::error_already_set();
72-
}
68+
// TODO: This really shouldn't be mutable, but Agg's renderer buffers aren't const.
69+
auto image = image_obj.mutable_unchecked<2>();
7370

7471
self->draw_text_image(gc, image, x, y, angle);
7572
}
@@ -98,13 +95,10 @@ PyRendererAgg_draw_image(RendererAgg *self,
9895
GCAgg &gc,
9996
double x,
10097
double y,
101-
py::array_t<agg::int8u, py::array::c_style> image_obj)
98+
py::array_t<agg::int8u, py::array::c_style | py::array::forcecast> image_obj)
10299
{
103-
numpy::array_view<agg::int8u, 3> image;
104-
105-
if (!image.set(image_obj.ptr())) {
106-
throw py::error_already_set();
107-
}
100+
// TODO: This really shouldn't be mutable, but Agg's renderer buffers aren't const.
101+
auto image = image_obj.mutable_unchecked<3>();
108102

109103
x = mpl_round(x);
110104
y = mpl_round(y);
@@ -179,21 +173,18 @@ PyRendererAgg_draw_quad_mesh(RendererAgg *self,
179173
agg::trans_affine master_transform,
180174
unsigned int mesh_width,
181175
unsigned int mesh_height,
182-
py::object coordinates_obj,
176+
py::array_t<double, py::array::c_style | py::array::forcecast> coordinates_obj,
183177
py::object offsets_obj,
184178
agg::trans_affine offset_trans,
185179
py::object facecolors_obj,
186180
bool antialiased,
187181
py::object edgecolors_obj)
188182
{
189-
numpy::array_view<const double, 3> coordinates;
190183
numpy::array_view<const double, 2> offsets;
191184
numpy::array_view<const double, 2> facecolors;
192185
numpy::array_view<const double, 2> edgecolors;
193186

194-
if (!coordinates.converter(coordinates_obj.ptr(), &coordinates)) {
195-
throw py::error_already_set();
196-
}
187+
auto coordinates = coordinates_obj.mutable_unchecked<3>();
197188
if (!convert_points(offsets_obj.ptr(), &offsets)) {
198189
throw py::error_already_set();
199190
}
@@ -219,19 +210,12 @@ PyRendererAgg_draw_quad_mesh(RendererAgg *self,
219210
static void
220211
PyRendererAgg_draw_gouraud_triangles(RendererAgg *self,
221212
GCAgg &gc,
222-
py::object points_obj,
223-
py::object colors_obj,
213+
py::array_t<double> points_obj,
214+
py::array_t<double> colors_obj,
224215
agg::trans_affine trans)
225216
{
226-
numpy::array_view<const double, 3> points;
227-
numpy::array_view<const double, 3> colors;
228-
229-
if (!points.converter(points_obj.ptr(), &points)) {
230-
throw py::error_already_set();
231-
}
232-
if (!colors.converter(colors_obj.ptr(), &colors)) {
233-
throw py::error_already_set();
234-
}
217+
auto points = points_obj.unchecked<3>();
218+
auto colors = colors_obj.unchecked<3>();
235219

236220
self->draw_gouraud_triangles(gc, points, colors, trans);
237221
}

0 commit comments

Comments
 (0)