File size: 20,475 Bytes
714e7c4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 | #include "mplutils.h"
#include "numpy_cpp.h"
#include "py_converters.h"
#include "_backend_agg.h"
typedef struct
{
PyObject_HEAD
RendererAgg *x;
Py_ssize_t shape[3];
Py_ssize_t strides[3];
Py_ssize_t suboffsets[3];
} PyRendererAgg;
static PyTypeObject PyRendererAggType;
typedef struct
{
PyObject_HEAD
BufferRegion *x;
Py_ssize_t shape[3];
Py_ssize_t strides[3];
Py_ssize_t suboffsets[3];
} PyBufferRegion;
static PyTypeObject PyBufferRegionType;
/**********************************************************************
* BufferRegion
* */
static PyObject *PyBufferRegion_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyBufferRegion *self;
self = (PyBufferRegion *)type->tp_alloc(type, 0);
self->x = NULL;
return (PyObject *)self;
}
static void PyBufferRegion_dealloc(PyBufferRegion *self)
{
delete self->x;
Py_TYPE(self)->tp_free((PyObject *)self);
}
static PyObject *PyBufferRegion_to_string(PyBufferRegion *self, PyObject *args)
{
char const* msg =
"BufferRegion.to_string is deprecated since Matplotlib 3.7 and will "
"be removed two minor releases later; use np.asarray(region) instead.";
if (PyErr_WarnEx(PyExc_DeprecationWarning, msg, 1)) {
return NULL;
}
return PyBytes_FromStringAndSize((const char *)self->x->get_data(),
(Py_ssize_t) self->x->get_height() * self->x->get_stride());
}
/* TODO: This doesn't seem to be used internally. Remove? */
static PyObject *PyBufferRegion_set_x(PyBufferRegion *self, PyObject *args)
{
int x;
if (!PyArg_ParseTuple(args, "i:set_x", &x)) {
return NULL;
}
self->x->get_rect().x1 = x;
Py_RETURN_NONE;
}
static PyObject *PyBufferRegion_set_y(PyBufferRegion *self, PyObject *args)
{
int y;
if (!PyArg_ParseTuple(args, "i:set_y", &y)) {
return NULL;
}
self->x->get_rect().y1 = y;
Py_RETURN_NONE;
}
static PyObject *PyBufferRegion_get_extents(PyBufferRegion *self, PyObject *args)
{
agg::rect_i rect = self->x->get_rect();
return Py_BuildValue("IIII", rect.x1, rect.y1, rect.x2, rect.y2);
}
static PyObject *PyBufferRegion_to_string_argb(PyBufferRegion *self, PyObject *args)
{
char const* msg =
"BufferRegion.to_string_argb is deprecated since Matplotlib 3.7 and "
"will be removed two minor releases later; use "
"np.take(region, [2, 1, 0, 3], axis=2) instead.";
if (PyErr_WarnEx(PyExc_DeprecationWarning, msg, 1)) {
return NULL;
}
PyObject *bufobj;
uint8_t *buf;
Py_ssize_t height, stride;
height = self->x->get_height();
stride = self->x->get_stride();
bufobj = PyBytes_FromStringAndSize(NULL, height * stride);
buf = (uint8_t *)PyBytes_AS_STRING(bufobj);
CALL_CPP_CLEANUP("to_string_argb", (self->x->to_string_argb(buf)), Py_DECREF(bufobj));
return bufobj;
}
int PyBufferRegion_get_buffer(PyBufferRegion *self, Py_buffer *buf, int flags)
{
Py_INCREF(self);
buf->obj = (PyObject *)self;
buf->buf = self->x->get_data();
buf->len = (Py_ssize_t)self->x->get_width() * (Py_ssize_t)self->x->get_height() * 4;
buf->readonly = 0;
buf->format = (char *)"B";
buf->ndim = 3;
self->shape[0] = self->x->get_height();
self->shape[1] = self->x->get_width();
self->shape[2] = 4;
buf->shape = self->shape;
self->strides[0] = self->x->get_width() * 4;
self->strides[1] = 4;
self->strides[2] = 1;
buf->strides = self->strides;
buf->suboffsets = NULL;
buf->itemsize = 1;
buf->internal = NULL;
return 1;
}
static PyTypeObject *PyBufferRegion_init_type()
{
static PyMethodDef methods[] = {
{ "to_string", (PyCFunction)PyBufferRegion_to_string, METH_NOARGS, NULL },
{ "to_string_argb", (PyCFunction)PyBufferRegion_to_string_argb, METH_NOARGS, NULL },
{ "set_x", (PyCFunction)PyBufferRegion_set_x, METH_VARARGS, NULL },
{ "set_y", (PyCFunction)PyBufferRegion_set_y, METH_VARARGS, NULL },
{ "get_extents", (PyCFunction)PyBufferRegion_get_extents, METH_NOARGS, NULL },
{ NULL }
};
static PyBufferProcs buffer_procs;
buffer_procs.bf_getbuffer = (getbufferproc)PyBufferRegion_get_buffer;
PyBufferRegionType.tp_name = "matplotlib.backends._backend_agg.BufferRegion";
PyBufferRegionType.tp_basicsize = sizeof(PyBufferRegion);
PyBufferRegionType.tp_dealloc = (destructor)PyBufferRegion_dealloc;
PyBufferRegionType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
PyBufferRegionType.tp_methods = methods;
PyBufferRegionType.tp_new = PyBufferRegion_new;
PyBufferRegionType.tp_as_buffer = &buffer_procs;
return &PyBufferRegionType;
}
/**********************************************************************
* RendererAgg
* */
static PyObject *PyRendererAgg_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyRendererAgg *self;
self = (PyRendererAgg *)type->tp_alloc(type, 0);
self->x = NULL;
return (PyObject *)self;
}
static int PyRendererAgg_init(PyRendererAgg *self, PyObject *args, PyObject *kwds)
{
unsigned int width;
unsigned int height;
double dpi;
int debug = 0;
if (!PyArg_ParseTuple(args, "IId|i:RendererAgg", &width, &height, &dpi, &debug)) {
return -1;
}
if (dpi <= 0.0) {
PyErr_SetString(PyExc_ValueError, "dpi must be positive");
return -1;
}
if (width >= 1 << 16 || height >= 1 << 16) {
PyErr_Format(
PyExc_ValueError,
"Image size of %dx%d pixels is too large. "
"It must be less than 2^16 in each direction.",
width, height);
return -1;
}
CALL_CPP_INIT("RendererAgg", self->x = new RendererAgg(width, height, dpi))
return 0;
}
static void PyRendererAgg_dealloc(PyRendererAgg *self)
{
delete self->x;
Py_TYPE(self)->tp_free((PyObject *)self);
}
static PyObject *PyRendererAgg_draw_path(PyRendererAgg *self, PyObject *args)
{
GCAgg gc;
py::PathIterator path;
agg::trans_affine trans;
PyObject *faceobj = NULL;
agg::rgba face;
if (!PyArg_ParseTuple(args,
"O&O&O&|O:draw_path",
&convert_gcagg,
&gc,
&convert_path,
&path,
&convert_trans_affine,
&trans,
&faceobj)) {
return NULL;
}
if (!convert_face(faceobj, gc, &face)) {
return NULL;
}
CALL_CPP("draw_path", (self->x->draw_path(gc, path, trans, face)));
Py_RETURN_NONE;
}
static PyObject *PyRendererAgg_draw_text_image(PyRendererAgg *self, PyObject *args)
{
numpy::array_view<agg::int8u, 2> image;
double x;
double y;
double angle;
GCAgg gc;
if (!PyArg_ParseTuple(args,
"O&dddO&:draw_text_image",
&image.converter_contiguous,
&image,
&x,
&y,
&angle,
&convert_gcagg,
&gc)) {
return NULL;
}
CALL_CPP("draw_text_image", (self->x->draw_text_image(gc, image, x, y, angle)));
Py_RETURN_NONE;
}
PyObject *PyRendererAgg_draw_markers(PyRendererAgg *self, PyObject *args)
{
GCAgg gc;
py::PathIterator marker_path;
agg::trans_affine marker_path_trans;
py::PathIterator path;
agg::trans_affine trans;
PyObject *faceobj = NULL;
agg::rgba face;
if (!PyArg_ParseTuple(args,
"O&O&O&O&O&|O:draw_markers",
&convert_gcagg,
&gc,
&convert_path,
&marker_path,
&convert_trans_affine,
&marker_path_trans,
&convert_path,
&path,
&convert_trans_affine,
&trans,
&faceobj)) {
return NULL;
}
if (!convert_face(faceobj, gc, &face)) {
return NULL;
}
CALL_CPP("draw_markers",
(self->x->draw_markers(gc, marker_path, marker_path_trans, path, trans, face)));
Py_RETURN_NONE;
}
static PyObject *PyRendererAgg_draw_image(PyRendererAgg *self, PyObject *args)
{
GCAgg gc;
double x;
double y;
numpy::array_view<agg::int8u, 3> image;
if (!PyArg_ParseTuple(args,
"O&ddO&:draw_image",
&convert_gcagg,
&gc,
&x,
&y,
&image.converter_contiguous,
&image)) {
return NULL;
}
x = mpl_round(x);
y = mpl_round(y);
gc.alpha = 1.0;
CALL_CPP("draw_image", (self->x->draw_image(gc, x, y, image)));
Py_RETURN_NONE;
}
static PyObject *
PyRendererAgg_draw_path_collection(PyRendererAgg *self, PyObject *args)
{
GCAgg gc;
agg::trans_affine master_transform;
py::PathGenerator paths;
numpy::array_view<const double, 3> transforms;
numpy::array_view<const double, 2> offsets;
agg::trans_affine offset_trans;
numpy::array_view<const double, 2> facecolors;
numpy::array_view<const double, 2> edgecolors;
numpy::array_view<const double, 1> linewidths;
DashesVector dashes;
numpy::array_view<const uint8_t, 1> antialiaseds;
PyObject *ignored;
PyObject *offset_position; // offset position is no longer used
if (!PyArg_ParseTuple(args,
"O&O&O&O&O&O&O&O&O&O&O&OO:draw_path_collection",
&convert_gcagg,
&gc,
&convert_trans_affine,
&master_transform,
&convert_pathgen,
&paths,
&convert_transforms,
&transforms,
&convert_points,
&offsets,
&convert_trans_affine,
&offset_trans,
&convert_colors,
&facecolors,
&convert_colors,
&edgecolors,
&linewidths.converter,
&linewidths,
&convert_dashes_vector,
&dashes,
&antialiaseds.converter,
&antialiaseds,
&ignored,
&offset_position)) {
return NULL;
}
CALL_CPP("draw_path_collection",
(self->x->draw_path_collection(gc,
master_transform,
paths,
transforms,
offsets,
offset_trans,
facecolors,
edgecolors,
linewidths,
dashes,
antialiaseds)));
Py_RETURN_NONE;
}
static PyObject *PyRendererAgg_draw_quad_mesh(PyRendererAgg *self, PyObject *args)
{
GCAgg gc;
agg::trans_affine master_transform;
unsigned int mesh_width;
unsigned int mesh_height;
numpy::array_view<const double, 3> coordinates;
numpy::array_view<const double, 2> offsets;
agg::trans_affine offset_trans;
numpy::array_view<const double, 2> facecolors;
bool antialiased;
numpy::array_view<const double, 2> edgecolors;
if (!PyArg_ParseTuple(args,
"O&O&IIO&O&O&O&O&O&:draw_quad_mesh",
&convert_gcagg,
&gc,
&convert_trans_affine,
&master_transform,
&mesh_width,
&mesh_height,
&coordinates.converter,
&coordinates,
&convert_points,
&offsets,
&convert_trans_affine,
&offset_trans,
&convert_colors,
&facecolors,
&convert_bool,
&antialiased,
&convert_colors,
&edgecolors)) {
return NULL;
}
CALL_CPP("draw_quad_mesh",
(self->x->draw_quad_mesh(gc,
master_transform,
mesh_width,
mesh_height,
coordinates,
offsets,
offset_trans,
facecolors,
antialiased,
edgecolors)));
Py_RETURN_NONE;
}
static PyObject *
PyRendererAgg_draw_gouraud_triangle(PyRendererAgg *self, PyObject *args)
{
GCAgg gc;
numpy::array_view<const double, 2> points;
numpy::array_view<const double, 2> colors;
agg::trans_affine trans;
if (!PyArg_ParseTuple(args,
"O&O&O&O&|O:draw_gouraud_triangle",
&convert_gcagg,
&gc,
&points.converter,
&points,
&colors.converter,
&colors,
&convert_trans_affine,
&trans)) {
return NULL;
}
if (points.dim(0) != 3 || points.dim(1) != 2) {
PyErr_Format(PyExc_ValueError,
"points must have shape (3, 2), "
"got (%" NPY_INTP_FMT ", %" NPY_INTP_FMT ")",
points.dim(0), points.dim(1));
return NULL;
}
if (colors.dim(0) != 3 || colors.dim(1) != 4) {
PyErr_Format(PyExc_ValueError,
"colors must have shape (3, 4), "
"got (%" NPY_INTP_FMT ", %" NPY_INTP_FMT ")",
colors.dim(0), colors.dim(1));
return NULL;
}
CALL_CPP("draw_gouraud_triangle", (self->x->draw_gouraud_triangle(gc, points, colors, trans)));
Py_RETURN_NONE;
}
static PyObject *
PyRendererAgg_draw_gouraud_triangles(PyRendererAgg *self, PyObject *args)
{
GCAgg gc;
numpy::array_view<const double, 3> points;
numpy::array_view<const double, 3> colors;
agg::trans_affine trans;
if (!PyArg_ParseTuple(args,
"O&O&O&O&|O:draw_gouraud_triangles",
&convert_gcagg,
&gc,
&points.converter,
&points,
&colors.converter,
&colors,
&convert_trans_affine,
&trans)) {
return NULL;
}
if (points.size() && !check_trailing_shape(points, "points", 3, 2)) {
return NULL;
}
if (colors.size() && !check_trailing_shape(colors, "colors", 3, 4)) {
return NULL;
}
if (points.size() != colors.size()) {
PyErr_Format(PyExc_ValueError,
"points and colors arrays must be the same length, got "
"%" NPY_INTP_FMT " points and %" NPY_INTP_FMT "colors",
points.dim(0), colors.dim(0));
return NULL;
}
CALL_CPP("draw_gouraud_triangles", self->x->draw_gouraud_triangles(gc, points, colors, trans));
Py_RETURN_NONE;
}
int PyRendererAgg_get_buffer(PyRendererAgg *self, Py_buffer *buf, int flags)
{
Py_INCREF(self);
buf->obj = (PyObject *)self;
buf->buf = self->x->pixBuffer;
buf->len = (Py_ssize_t)self->x->get_width() * (Py_ssize_t)self->x->get_height() * 4;
buf->readonly = 0;
buf->format = (char *)"B";
buf->ndim = 3;
self->shape[0] = self->x->get_height();
self->shape[1] = self->x->get_width();
self->shape[2] = 4;
buf->shape = self->shape;
self->strides[0] = self->x->get_width() * 4;
self->strides[1] = 4;
self->strides[2] = 1;
buf->strides = self->strides;
buf->suboffsets = NULL;
buf->itemsize = 1;
buf->internal = NULL;
return 1;
}
static PyObject *PyRendererAgg_clear(PyRendererAgg *self, PyObject *args)
{
CALL_CPP("clear", self->x->clear());
Py_RETURN_NONE;
}
static PyObject *PyRendererAgg_copy_from_bbox(PyRendererAgg *self, PyObject *args)
{
agg::rect_d bbox;
BufferRegion *reg;
PyObject *regobj;
if (!PyArg_ParseTuple(args, "O&:copy_from_bbox", &convert_rect, &bbox)) {
return 0;
}
CALL_CPP("copy_from_bbox", (reg = self->x->copy_from_bbox(bbox)));
regobj = PyBufferRegion_new(&PyBufferRegionType, NULL, NULL);
((PyBufferRegion *)regobj)->x = reg;
return regobj;
}
static PyObject *PyRendererAgg_restore_region(PyRendererAgg *self, PyObject *args)
{
PyBufferRegion *regobj;
int xx1 = 0, yy1 = 0, xx2 = 0, yy2 = 0, x = 0, y = 0;
if (!PyArg_ParseTuple(args,
"O!|iiiiii:restore_region",
&PyBufferRegionType,
®obj,
&xx1,
&yy1,
&xx2,
&yy2,
&x,
&y)) {
return 0;
}
if (PySequence_Size(args) == 1) {
CALL_CPP("restore_region", self->x->restore_region(*(regobj->x)));
} else {
CALL_CPP("restore_region", self->x->restore_region(*(regobj->x), xx1, yy1, xx2, yy2, x, y));
}
Py_RETURN_NONE;
}
static PyTypeObject *PyRendererAgg_init_type()
{
static PyMethodDef methods[] = {
{"draw_path", (PyCFunction)PyRendererAgg_draw_path, METH_VARARGS, NULL},
{"draw_markers", (PyCFunction)PyRendererAgg_draw_markers, METH_VARARGS, NULL},
{"draw_text_image", (PyCFunction)PyRendererAgg_draw_text_image, METH_VARARGS, NULL},
{"draw_image", (PyCFunction)PyRendererAgg_draw_image, METH_VARARGS, NULL},
{"draw_path_collection", (PyCFunction)PyRendererAgg_draw_path_collection, METH_VARARGS, NULL},
{"draw_quad_mesh", (PyCFunction)PyRendererAgg_draw_quad_mesh, METH_VARARGS, NULL},
{"draw_gouraud_triangle", (PyCFunction)PyRendererAgg_draw_gouraud_triangle, METH_VARARGS, NULL},
{"draw_gouraud_triangles", (PyCFunction)PyRendererAgg_draw_gouraud_triangles, METH_VARARGS, NULL},
{"clear", (PyCFunction)PyRendererAgg_clear, METH_NOARGS, NULL},
{"copy_from_bbox", (PyCFunction)PyRendererAgg_copy_from_bbox, METH_VARARGS, NULL},
{"restore_region", (PyCFunction)PyRendererAgg_restore_region, METH_VARARGS, NULL},
{NULL}
};
static PyBufferProcs buffer_procs;
buffer_procs.bf_getbuffer = (getbufferproc)PyRendererAgg_get_buffer;
PyRendererAggType.tp_name = "matplotlib.backends._backend_agg.RendererAgg";
PyRendererAggType.tp_basicsize = sizeof(PyRendererAgg);
PyRendererAggType.tp_dealloc = (destructor)PyRendererAgg_dealloc;
PyRendererAggType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
PyRendererAggType.tp_methods = methods;
PyRendererAggType.tp_init = (initproc)PyRendererAgg_init;
PyRendererAggType.tp_new = PyRendererAgg_new;
PyRendererAggType.tp_as_buffer = &buffer_procs;
return &PyRendererAggType;
}
static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "_backend_agg" };
PyMODINIT_FUNC PyInit__backend_agg(void)
{
import_array();
PyObject *m;
if (!(m = PyModule_Create(&moduledef))
|| prepare_and_add_type(PyRendererAgg_init_type(), m)
// BufferRegion is not constructible from Python, thus not added to the module.
|| PyType_Ready(PyBufferRegion_init_type())
) {
Py_XDECREF(m);
return NULL;
}
return m;
}
|