Skip to content

Commit

Permalink
Change how array converter functions are defined
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom committed Oct 17, 2014
1 parent ba40160 commit 6d07179
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 58 deletions.
32 changes: 16 additions & 16 deletions src/_backend_agg_wrapper.cpp
Expand Up @@ -206,7 +206,7 @@ static PyObject *PyRendererAgg_draw_text_image(PyRendererAgg *self, PyObject *ar

if (!PyArg_ParseTuple(args,
"O&dddO&:draw_text_image",
&numpy::convert_array_contiguous<agg::int8u, 2>,
&image.converter_contiguous,
&image,
&x,
&y,
Expand Down Expand Up @@ -274,7 +274,7 @@ static PyObject *PyRendererAgg_draw_image(PyRendererAgg *self, PyObject *args, P
&gc,
&x,
&y,
&numpy::convert_array_contiguous<agg::int8u, 3>,
&image.converter_contiguous,
&image,
&w,
&h,
Expand Down Expand Up @@ -319,21 +319,21 @@ PyRendererAgg_draw_path_collection(PyRendererAgg *self, PyObject *args, PyObject
&convert_trans_affine,
&master_transform,
&pathobj,
&numpy::convert_array<const double, 3>,
&transforms.converter,
&transforms,
&numpy::convert_array<const double, 2>,
&offsets.converter,
&offsets,
&convert_trans_affine,
&offset_trans,
&numpy::convert_array<const double, 2>,
&facecolors.converter,
&facecolors,
&numpy::convert_array<const double, 2>,
&edgecolors.converter,
&edgecolors,
&numpy::convert_array<const double, 1>,
&linewidths.converter,
&linewidths,
&convert_dashes_vector,
&dashes,
&numpy::convert_array<const uint8_t, 1>,
&antialiaseds.converter,
&antialiaseds,
&ignored,
&convert_offset_position,
Expand Down Expand Up @@ -388,16 +388,16 @@ static PyObject *PyRendererAgg_draw_quad_mesh(PyRendererAgg *self, PyObject *arg
&master_transform,
&mesh_width,
&mesh_height,
&numpy::convert_array<const double, 3>,
&coordinates.converter,
&coordinates,
&numpy::convert_array<const double, 2>,
&offsets.converter,
&offsets,
&convert_trans_affine,
&offset_trans,
&numpy::convert_array<const double, 2>,
&facecolors.converter,
&facecolors,
&antialiased,
&numpy::convert_array<const double, 2>,
&edgecolors.converter,
&edgecolors)) {
return NULL;
}
Expand Down Expand Up @@ -429,9 +429,9 @@ PyRendererAgg_draw_gouraud_triangle(PyRendererAgg *self, PyObject *args, PyObjec
"O&O&O&O&|O:draw_gouraud_triangle",
&convert_gcagg,
&gc,
&numpy::convert_array<const double, 2>,
&points.converter,
&points,
&numpy::convert_array<const double, 2>,
&colors.converter,
&colors,
&convert_trans_affine,
&trans)) {
Expand All @@ -455,9 +455,9 @@ PyRendererAgg_draw_gouraud_triangles(PyRendererAgg *self, PyObject *args, PyObje
"O&O&O&O&|O:draw_gouraud_triangles",
&convert_gcagg,
&gc,
&numpy::convert_array<const double, 3>,
&points.converter,
&points,
&numpy::convert_array<const double, 3>,
&colors.converter,
&colors,
&convert_trans_affine,
&trans)) {
Expand Down
2 changes: 1 addition & 1 deletion src/_gtkagg.cpp
Expand Up @@ -31,7 +31,7 @@ static PyObject *Py_agg_to_gtk_drawable(PyObject *self, PyObject *args, PyObject
if (!PyArg_ParseTuple(args,
"OO&O&:agg_to_gtk_drawable",
&py_drawable,
&numpy::convert_array<agg::int8u, 3>,
&buffer.converter,
&buffer,
&convert_rect,
&rect)) {
Expand Down
20 changes: 10 additions & 10 deletions src/_image_wrapper.cpp
Expand Up @@ -530,9 +530,9 @@ static PyObject *image_fromarray(PyObject *self, PyObject *args, PyObject *kwds)
numpy::array_view<const double, 2> grey_array;
Image *result = NULL;

if (numpy::convert_array<const double, 3>(array, &color_array)) {
if (color_array.converter(array, &color_array)) {
CALL_CPP("fromarray", result = from_color_array(color_array, isoutput));
} else if (numpy::convert_array<const double, 2>(array, &grey_array)) {
} else if (grey_array.converter(array, &grey_array)) {
CALL_CPP("fromarray", result = from_grey_array(grey_array, isoutput));
} else {
PyErr_SetString(PyExc_ValueError, "invalid array");
Expand Down Expand Up @@ -561,7 +561,7 @@ static PyObject *image_frombyte(PyObject *self, PyObject *args, PyObject *kwds)
kwds,
"O&|i:frombyte",
(char **)names,
&numpy::convert_array_contiguous<const uint8_t, 3>,
&array.converter,
&array,
&isoutput)) {
return NULL;
Expand Down Expand Up @@ -644,11 +644,11 @@ static PyObject *image_pcolor(PyObject *self, PyObject *args, PyObject *kwds)

if (!PyArg_ParseTuple(args,
"O&O&O&II(ffff)i:pcolor",
&numpy::convert_array_contiguous<const float, 1>,
&x.converter,
&x,
&numpy::convert_array_contiguous<const float, 1>,
&y.converter,
&y,
&numpy::convert_array_contiguous<const agg::int8u, 3>,
&d.converter_contiguous,
&d,
&rows,
&cols,
Expand Down Expand Up @@ -686,19 +686,19 @@ static PyObject *image_pcolor2(PyObject *self, PyObject *args, PyObject *kwds)

if (!PyArg_ParseTuple(args,
"O&O&O&II(ffff)O&:pcolor2",
&numpy::convert_array_contiguous<const double, 1>,
&x.converter,
&x,
&numpy::convert_array_contiguous<const double, 1>,
&y.converter,
&y,
&numpy::convert_array<const agg::int8u, 3>,
&d.converter_contiguous,
&d,
&rows,
&cols,
&bounds[0],
&bounds[1],
&bounds[2],
&bounds[3],
&numpy::convert_array<const agg::int8u, 1>,
&bg.converter,
&bg)) {
return NULL;
}
Expand Down
20 changes: 10 additions & 10 deletions src/_path_wrapper.cpp
Expand Up @@ -67,7 +67,7 @@ static PyObject *Py_points_in_path(PyObject *self, PyObject *args, PyObject *kwd

if (!PyArg_ParseTuple(args,
"O&dO&O&:points_in_path",
&numpy::convert_array<const double, 2>,
&points.converter,
&points,
&r,
&convert_path,
Expand Down Expand Up @@ -126,7 +126,7 @@ static PyObject *Py_points_on_path(PyObject *self, PyObject *args, PyObject *kwd

if (!PyArg_ParseTuple(args,
"O&dO&O&:points_on_path",
&numpy::convert_array<const double, 2>,
&points.converter,
&points,
&r,
&convert_path,
Expand Down Expand Up @@ -191,7 +191,7 @@ static PyObject *Py_update_path_extents(PyObject *self, PyObject *args, PyObject
&trans,
&convert_rect,
&rect,
&numpy::convert_array<double, 1>,
&minpos.converter,
&minpos,
&ignore)) {
return NULL;
Expand Down Expand Up @@ -261,9 +261,9 @@ static PyObject *Py_get_path_collection_extents(PyObject *self, PyObject *args,
&convert_trans_affine,
&master_transform,
&pathsobj,
&numpy::convert_array<const double, 3>,
&transforms.converter,
&transforms,
&numpy::convert_array<const double, 2>,
&offsets.converter,
&offsets,
&convert_trans_affine,
&offset_trans)) {
Expand Down Expand Up @@ -317,9 +317,9 @@ static PyObject *Py_point_in_path_collection(PyObject *self, PyObject *args, PyO
&convert_trans_affine,
&master_transform,
&pathsobj,
&numpy::convert_array<const double, 3>,
&transforms.converter,
&transforms,
&numpy::convert_array<const double, 2>,
&offsets.converter,
&offsets,
&convert_trans_affine,
&offset_trans,
Expand Down Expand Up @@ -422,7 +422,7 @@ static PyObject *Py_affine_transform(PyObject *self, PyObject *args, PyObject *k

if (!PyArg_ParseTuple(args,
"O&O&:affine_transform",
&numpy::convert_array<const double, 2>,
&vertices.converter,
&vertices,
&convert_trans_affine,
&trans)) {
Expand All @@ -449,7 +449,7 @@ static PyObject *Py_count_bboxes_overlapping_bbox(PyObject *self, PyObject *args
"O&O&:count_bboxes_overlapping_bbox",
&convert_rect,
&bbox,
&numpy::convert_array<const double, 3>,
&bboxes.converter,
&bboxes)) {
return NULL;
}
Expand Down Expand Up @@ -662,7 +662,7 @@ extern "C" {
{"convert_path_to_polygons", (PyCFunction)Py_convert_path_to_polygons, METH_VARARGS, Py_convert_path_to_polygons__doc__},
{"cleanup_path", (PyCFunction)Py_cleanup_path, METH_VARARGS, Py_cleanup_path__doc__},
{"convert_to_svg", (PyCFunction)Py_convert_to_svg, METH_VARARGS, Py_convert_to_svg__doc__},
{NULL, NULL, NULL, NULL}
{NULL}
};

struct module_state
Expand Down
2 changes: 1 addition & 1 deletion src/_png.cpp
Expand Up @@ -79,7 +79,7 @@ static PyObject *Py_write_png(PyObject *self, PyObject *args, PyObject *kwds)
kwds,
"O&O|d:write_png",
(char **)names,
&numpy::convert_array<unsigned char, 3>,
&buffer.converter,
&buffer,
&filein,
&dpi)) {
Expand Down
73 changes: 53 additions & 20 deletions src/numpy_cpp.h
Expand Up @@ -309,7 +309,43 @@ class array_view_accessors<AV, T, 3>
self->m_shape + 1,
self->m_strides + 1);
}


};

// /* These are converter functions designed for use with the "O&"
// functionality of PyArg_ParseTuple and friends. */

// template<class T>
// class array_converter {
// public:
// int operator()(PyObject *obj, void *arrp)
// {
// T *arr = (T *)arrp;

// if (!arr->set(obj)) {
// return 0;
// }

// return 1;
// }
// };

// template <class T>
// class array_converter_contiguous {
// public:
// int operator()(PyObject *obj, void *arrp)
// {
// T *arr = (T *)arrp;

// if (!arr->set(obj, true)) {
// return 0;
// }

// return 1;
// }
// };

}

static npy_intp zeros[] = { 0, 0, 0 };
Expand All @@ -328,6 +364,7 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>

public:
typedef T value_type;

enum {
ndim = ND
};
Expand Down Expand Up @@ -446,35 +483,31 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
Py_INCREF(m_arr);
return (PyObject *)m_arr;
}
};

/* These are converter functions designed for use with the "O&"
functionality of PyArg_ParseTuple and friends. */
static int converter(PyObject *obj, void *arrp)
{
array_view<T, ND> *arr = (array_view<T, ND> *)arrp;

template <class T, int ND>
int convert_array(PyObject *obj, void *arrp)
{
numpy::array_view<T, ND> *arr = (numpy::array_view<T, ND> *)arrp;
if (!arr->set(obj)) {
return 0;
}

if (!arr->set(obj)) {
return 0;
return 1;
}

return 1;
}
static int converter_contiguous(PyObject *obj, void *arrp)
{
array_view<T, ND> *arr = (array_view<T, ND> *)arrp;

template <class T, int ND>
int convert_array_contiguous(PyObject *obj, void *arrp)
{
numpy::array_view<T, ND> *arr = (numpy::array_view<T, ND> *)arrp;
if (!arr->set(obj, true)) {
return 0;
}

if (!arr->set(obj, true)) {
return 0;
return 1;
}

return 1;
}
};

} // namespace numpy


#endif

0 comments on commit 6d07179

Please sign in to comment.