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

Skip to content

Convert Agg extension to pybind11 #27011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/_backend_agg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ RendererAgg::RendererAgg(unsigned int width, unsigned int height, double dpi)
lastclippath(NULL),
_fill_color(agg::rgba(1, 1, 1, 0))
{
if (dpi <= 0.0) {
throw std::range_error("dpi must be positive");
}

if (width >= 1 << 16 || height >= 1 << 16) {
throw std::range_error(
"Image size of " + std::to_string(width) + "x" + std::to_string(height) +
" pixels is too large. It must be less than 2^16 in each direction.");
}

unsigned stride(width * 4);

pixBuffer = new agg::int8u[NUMBYTES];
Expand Down
29 changes: 24 additions & 5 deletions src/_backend_agg.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
#ifndef MPL_BACKEND_AGG_H
#define MPL_BACKEND_AGG_H

#include <pybind11/pybind11.h>

#include <cmath>
#include <algorithm>
#include <functional>

#include "agg_alpha_mask_u8.h"
#include "agg_conv_curve.h"
Expand Down Expand Up @@ -40,6 +43,8 @@
#include "array.h"
#include "agg_workaround.h"

namespace py = pybind11;

/**********************************************************************/

// a helper class to pass agg::buffer objects around.
Expand Down Expand Up @@ -728,7 +733,7 @@
rendererBase.reset_clipping(true);
if (angle != 0.0) {
agg::rendering_buffer srcbuf(
image.data(), (unsigned)image.shape(1),
image.mutable_data(0, 0), (unsigned)image.shape(1),
(unsigned)image.shape(0), (unsigned)image.shape(1));
agg::pixfmt_gray8 pixf_img(srcbuf);

Expand Down Expand Up @@ -828,8 +833,9 @@
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans, gc.snap_mode);

agg::rendering_buffer buffer;
buffer.attach(
image.data(), (unsigned)image.shape(1), (unsigned)image.shape(0), -(int)image.shape(1) * 4);
buffer.attach(image.mutable_data(0, 0, 0),
(unsigned)image.shape(1), (unsigned)image.shape(0),
-(int)image.shape(1) * 4);
pixfmt pixf(buffer);

if (has_clippath) {
Expand Down Expand Up @@ -1226,14 +1232,27 @@
ColorArray &colors,
agg::trans_affine &trans)
{
if (points.shape(0) && !check_trailing_shape(points, "points", 3, 2)) {
throw py::error_already_set();
}
if (colors.shape(0) && !check_trailing_shape(colors, "colors", 3, 4)) {
throw py::error_already_set();
}
if (points.shape(0) != colors.shape(0)) {
throw py::value_error(
"points and colors arrays must be the same length, got " +
std::to_string(points.shape(0)) + " points and " +

Check warning on line 1244 in src/_backend_agg.h

View check run for this annotation

Codecov / codecov/patch

src/_backend_agg.h#L1243-L1244

Added lines #L1243 - L1244 were not covered by tests
std::to_string(colors.shape(0)) + "colors");
}

theRasterizer.reset_clipping();
rendererBase.reset_clipping(true);
set_clipbox(gc.cliprect, theRasterizer);
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans, gc.snap_mode);

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

_draw_gouraud_triangle(point, color, trans, has_clippath);
}
Expand Down
Loading
Loading