diff --git a/.travis.yml b/.travis.yml index e99ff6cb5bdc..e5029c94f1fc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,12 +6,14 @@ env: - secure: E7OCdqhZ+PlwJcn+Hd6ns9TDJgEUXiUNEI0wu7xjxB2vBRRIKtZMbuaZjd+iKDqCKuVOJKu0ClBUYxmgmpLicTwi34CfTUYt6D4uhrU+8hBBOn1iiK51cl/aBvlUUrqaRLVhukNEBGZcyqAjXSA/Qsnp2iELEmAfOUa92ZYo1sk= - BUILD_DOCS=false - TEST_ARGS=--no-pep8 + - NUMPY=numpy language: python matrix: include: - python: 2.6 + env: NUMPY=numpy==1.6 - python: 2.7 - python: 3.3 - python: 3.4 @@ -21,7 +23,7 @@ matrix: env: BUILD_DOCS=true install: - - pip install -q --use-mirrors nose python-dateutil numpy pep8 pyparsing pillow + - pip install -q --use-mirrors nose python-dateutil $NUMPY pep8 pyparsing pillow - sudo apt-get update && sudo apt-get -qq install inkscape libav-tools - if [[ $BUILD_DOCS == true ]]; then pip install sphinx numpydoc linkchecker; fi - python setup.py install diff --git a/lib/matplotlib/delaunay/_delaunay.cpp b/lib/matplotlib/delaunay/_delaunay.cpp index ab10b8b3998d..452e415d149d 100644 --- a/lib/matplotlib/delaunay/_delaunay.cpp +++ b/lib/matplotlib/delaunay/_delaunay.cpp @@ -8,6 +8,11 @@ #include "natneighbors.h" #include "numpy/noprefix.h" +// support numpy 1.6 - this macro got renamed and deprecated at once in 1.7 +#ifndef NPY_ARRAY_IN_ARRAY +#define NPY_ARRAY_IN_ARRAY NPY_IN_ARRAY +#endif + using namespace std; extern "C" { @@ -135,23 +140,23 @@ static PyObject* getMesh(int npoints, double *x, double *y) dim[0] = length; dim[1] = 2; - edge_db = PyArray_SimpleNew(2, dim, PyArray_INT); + edge_db = PyArray_SimpleNew(2, dim, NPY_INT); if (!edge_db) goto fail; - edge_db_ptr = (int*)PyArray_DATA(edge_db); + edge_db_ptr = (int*)PyArray_DATA((PyArrayObject*)edge_db); dim[0] = numtri; - vertices = PyArray_SimpleNew(2, dim, PyArray_DOUBLE); + vertices = PyArray_SimpleNew(2, dim, NPY_DOUBLE); if (!vertices) goto fail; - vertices_ptr = (double*)PyArray_DATA(vertices); + vertices_ptr = (double*)PyArray_DATA((PyArrayObject*)vertices); dim[1] = 3; - tri_edges = PyArray_SimpleNew(2, dim, PyArray_INT); + tri_edges = PyArray_SimpleNew(2, dim, NPY_INT); if (!tri_edges) goto fail; - tri_edges_ptr = (int*)PyArray_DATA(tri_edges); + tri_edges_ptr = (int*)PyArray_DATA((PyArrayObject*)tri_edges); - tri_nbrs = PyArray_SimpleNew(2, dim, PyArray_INT); + tri_nbrs = PyArray_SimpleNew(2, dim, NPY_INT); if (!tri_nbrs) goto fail; - tri_nbrs_ptr = (int*)PyArray_DATA(tri_nbrs); + tri_nbrs_ptr = (int*)PyArray_DATA((PyArrayObject*)tri_nbrs); for (i=0; i<(3*numtri); i++) { tri_edges_ptr[i] = tri_nbrs_ptr[i] = -1; @@ -224,9 +229,9 @@ static PyObject *linear_planes(int ntriangles, double *x, double *y, double *z, dims[0] = ntriangles; dims[1] = 3; - planes = PyArray_SimpleNew(2, dims, PyArray_DOUBLE); + planes = PyArray_SimpleNew(2, dims, NPY_DOUBLE); if (!planes) return NULL; - planes_ptr = (double *)PyArray_DATA(planes); + planes_ptr = (double *)PyArray_DATA((PyArrayObject*)planes); for (i=0; i(edge_set.size()), 2}; - _edges = (PyArrayObject*)PyArray_SimpleNew(2, dims, PyArray_INT); + _edges = (PyArrayObject*)PyArray_SimpleNew(2, dims, NPY_INT); int* edges_ptr = (int*)PyArray_DATA(_edges); for (EdgeSet::const_iterator it = edge_set.begin(); it != edge_set.end(); ++it) { *edges_ptr++ = it->start; @@ -343,7 +343,7 @@ void Triangulation::calculate_neighbors() // Create _neighbors array with shape (ntri,3) and initialise all to -1. npy_intp dims[2] = {_ntri,3}; - _neighbors = (PyArrayObject*)PyArray_SimpleNew(2, dims, PyArray_INT); + _neighbors = (PyArrayObject*)PyArray_SimpleNew(2, dims, NPY_INT); int* neighbors_ptr = (int*)PyArray_DATA(_neighbors); std::fill(neighbors_ptr, neighbors_ptr + 3*_ntri, -1); @@ -386,7 +386,7 @@ Py::Object Triangulation::calculate_plane_coefficients(const Py::Tuple &args) args.verify_length(1); PyArrayObject* z = (PyArrayObject*)PyArray_ContiguousFromObject( - args[0].ptr(), PyArray_DOUBLE, 1, 1); + args[0].ptr(), NPY_DOUBLE, 1, 1); if (z == 0 || PyArray_DIM(z,0) != PyArray_DIM(_x,0)) { Py_XDECREF(z); throw Py::ValueError( @@ -401,7 +401,7 @@ Py::Object Triangulation::calculate_plane_coefficients(const Py::Tuple &args) npy_intp dims[2] = {_ntri, 3}; planes_array = (PyArrayObject*)PyArray_SimpleNew(2, dims, - PyArray_DOUBLE); + NPY_DOUBLE); double* planes = (double*)PyArray_DATA(planes_array); const int* tris = get_triangles_ptr(); const double* xs = (const double*)PyArray_DATA(_x); @@ -624,7 +624,7 @@ Py::Object Triangulation::set_mask(const Py::Tuple &args) if (args[0] != Py::None()) { _mask = (PyArrayObject*)PyArray_ContiguousFromObject( - args[0].ptr(), PyArray_BOOL, 1, 1); + args[0].ptr(), NPY_BOOL, 1, 1); if (_mask == 0 || PyArray_DIM(_mask,0) != PyArray_DIM(_triangles,0)) { Py_XDECREF(_mask); throw Py::ValueError( @@ -712,7 +712,7 @@ Py::Object TriContourGenerator::contour_to_segs(const Contour& contour) const ContourLine& line = contour[i]; npy_intp dims[2] = {static_cast(line.size()),2}; PyArrayObject* py_line = (PyArrayObject*)PyArray_SimpleNew( - 2, dims, PyArray_DOUBLE); + 2, dims, NPY_DOUBLE); double* p = (double*)PyArray_DATA(py_line); for (ContourLine::const_iterator it = line.begin(); it != line.end(); ++it) { *p++ = it->x; @@ -736,13 +736,13 @@ Py::Object TriContourGenerator::contour_to_segs_and_kinds(const Contour& contour // Create segs array for point coordinates. npy_intp segs_dims[2] = {n_points, 2}; PyArrayObject* segs = (PyArrayObject*)PyArray_SimpleNew( - 2, segs_dims, PyArray_DOUBLE); + 2, segs_dims, NPY_DOUBLE); double* segs_ptr = (double*)PyArray_DATA(segs); // Create kinds array for code types. npy_intp kinds_dims[1] = {n_points}; PyArrayObject* kinds = (PyArrayObject*)PyArray_SimpleNew( - 1, kinds_dims, PyArray_UBYTE); + 1, kinds_dims, NPY_UBYTE); unsigned char* kinds_ptr = (unsigned char*)PyArray_DATA(kinds); for (line = contour.begin(); line != contour.end(); ++line) { @@ -1367,9 +1367,9 @@ TrapezoidMapTriFinder::find_many(const Py::Tuple& args) // Check input arguments. PyArrayObject* x = (PyArrayObject*)PyArray_ContiguousFromObject( - args[0].ptr(), PyArray_DOUBLE, 0, 0); + args[0].ptr(), NPY_DOUBLE, 0, 0); PyArrayObject* y = (PyArrayObject*)PyArray_ContiguousFromObject( - args[1].ptr(), PyArray_DOUBLE, 0, 0); + args[1].ptr(), NPY_DOUBLE, 0, 0); bool ok = (x != 0 && y != 0 && PyArray_NDIM(x) == PyArray_NDIM(y)); int ndim = x == 0 ? 0 : PyArray_NDIM(x); for (int i = 0; ok && i < ndim; ++i) @@ -1383,7 +1383,7 @@ TrapezoidMapTriFinder::find_many(const Py::Tuple& args) // Create integer array to return. PyArrayObject* tri = (PyArrayObject*)PyArray_SimpleNew( - ndim, PyArray_DIMS(x), PyArray_INT); + ndim, PyArray_DIMS(x), NPY_INT); // Fill returned array. double* x_ptr = (double*)PyArray_DATA(x); @@ -2234,9 +2234,9 @@ Py::Object TriModule::new_triangulation(const Py::Tuple &args) // x and y. PyArrayObject* x = (PyArrayObject*)PyArray_ContiguousFromObject( - args[0].ptr(), PyArray_DOUBLE, 1, 1); + args[0].ptr(), NPY_DOUBLE, 1, 1); PyArrayObject* y = (PyArrayObject*)PyArray_ContiguousFromObject( - args[1].ptr(), PyArray_DOUBLE, 1, 1); + args[1].ptr(), NPY_DOUBLE, 1, 1); if (x == 0 || y == 0 || PyArray_DIM(x,0) != PyArray_DIM(y,0)) { Py_XDECREF(x); Py_XDECREF(y); @@ -2245,7 +2245,7 @@ Py::Object TriModule::new_triangulation(const Py::Tuple &args) // triangles. PyArrayObject* triangles = (PyArrayObject*)PyArray_ContiguousFromObject( - args[2].ptr(), PyArray_INT, 2, 2); + args[2].ptr(), NPY_INT, 2, 2); if (triangles == 0 || PyArray_DIM(triangles,1) != 3) { Py_XDECREF(x); Py_XDECREF(y); @@ -2258,7 +2258,7 @@ Py::Object TriModule::new_triangulation(const Py::Tuple &args) if (args[3].ptr() != 0 && args[3] != Py::None()) { mask = (PyArrayObject*)PyArray_ContiguousFromObject( - args[3].ptr(), PyArray_BOOL, 1, 1); + args[3].ptr(), NPY_BOOL, 1, 1); if (mask == 0 || PyArray_DIM(mask,0) != PyArray_DIM(triangles,0)) { Py_XDECREF(x); Py_XDECREF(y); @@ -2274,7 +2274,7 @@ Py::Object TriModule::new_triangulation(const Py::Tuple &args) if (args[4].ptr() != 0 && args[4] != Py::None()) { edges = (PyArrayObject*)PyArray_ContiguousFromObject( - args[4].ptr(), PyArray_INT, 2, 2); + args[4].ptr(), NPY_INT, 2, 2); if (edges == 0 || PyArray_DIM(edges,1) != 2) { Py_XDECREF(x); Py_XDECREF(y); @@ -2290,7 +2290,7 @@ Py::Object TriModule::new_triangulation(const Py::Tuple &args) if (args[5].ptr() != 0 && args[5] != Py::None()) { neighbors = (PyArrayObject*)PyArray_ContiguousFromObject( - args[5].ptr(), PyArray_INT, 2, 2); + args[5].ptr(), NPY_INT, 2, 2); if (neighbors == 0 || PyArray_DIM(neighbors,0) != PyArray_DIM(triangles,0) || PyArray_DIM(neighbors,1) != PyArray_DIM(triangles,1)) { @@ -2318,7 +2318,7 @@ Py::Object TriModule::new_tricontourgenerator(const Py::Tuple &args) throw Py::ValueError("Expecting a C++ Triangulation object"); PyArrayObject* z = (PyArrayObject*)PyArray_ContiguousFromObject( - args[1].ptr(), PyArray_DOUBLE, 1, 1); + args[1].ptr(), NPY_DOUBLE, 1, 1); if (z == 0 || PyArray_DIM(z,0) != ((Triangulation*)tri.ptr())->get_npoints()) { Py_XDECREF(z); diff --git a/setupext.py b/setupext.py index e2024440c406..9de60669e23e 100755 --- a/setupext.py +++ b/setupext.py @@ -824,6 +824,9 @@ def add_flags(self, ext): ext.define_macros.append(('PY_ARRAY_UNIQUE_SYMBOL', array_api_name)) ext.add_hook('include_dirs', self.include_dirs_hook) + ext.define_macros.append(('NPY_NO_DEPRECATED_API', + 'NPY_1_7_API_VERSION')) + def get_setup_requires(self): return ['numpy>=1.6'] diff --git a/src/_backend_agg.cpp b/src/_backend_agg.cpp index b088c97c9aa3..bb371b26212b 100644 --- a/src/_backend_agg.cpp +++ b/src/_backend_agg.cpp @@ -947,16 +947,16 @@ RendererAgg::draw_text_image(const Py::Tuple& args) if (PyArray_Check(image_obj.ptr())) { PyObject* image_array = PyArray_FromObject( - image_obj.ptr(), PyArray_UBYTE, 2, 2); + image_obj.ptr(), NPY_UBYTE, 2, 2); if (!image_array) { throw Py::ValueError( "First argument to draw_text_image must be a FT2Font.Image object or a Nx2 uint8 numpy array."); } image_obj = Py::Object(image_array, true); - buffer = (unsigned char *)PyArray_DATA(image_array); - width = PyArray_DIM(image_array, 1); - height = PyArray_DIM(image_array, 0); + buffer = (unsigned char *)PyArray_DATA((PyArrayObject*)image_array); + width = PyArray_DIM((PyArrayObject*)image_array, 1); + height = PyArray_DIM((PyArrayObject*)image_array, 0); } else { @@ -1500,7 +1500,7 @@ RendererAgg::_draw_path_collection_generic typedef agg::conv_curve curve_t; PyArrayObject* offsets = (PyArrayObject*)PyArray_FromObject - (offsets_obj.ptr(), PyArray_DOUBLE, 0, 2); + (offsets_obj.ptr(), NPY_DOUBLE, 0, 2); if (!offsets || (PyArray_NDIM(offsets) == 2 && PyArray_DIM(offsets, 1) != 2) || (PyArray_NDIM(offsets) == 1 && PyArray_DIM(offsets, 0) != 0)) @@ -1511,7 +1511,7 @@ RendererAgg::_draw_path_collection_generic Py::Object offsets_arr_obj((PyObject*)offsets, true); PyArrayObject* facecolors = (PyArrayObject*)PyArray_FromObject - (facecolors_obj.ptr(), PyArray_DOUBLE, 1, 2); + (facecolors_obj.ptr(), NPY_DOUBLE, 1, 2); if (!facecolors || (PyArray_NDIM(facecolors) == 1 && PyArray_DIM(facecolors, 0) != 0) || (PyArray_NDIM(facecolors) == 2 && PyArray_DIM(facecolors, 1) != 4)) @@ -1522,7 +1522,7 @@ RendererAgg::_draw_path_collection_generic Py::Object facecolors_arr_obj((PyObject*)facecolors, true); PyArrayObject* edgecolors = (PyArrayObject*)PyArray_FromObject - (edgecolors_obj.ptr(), PyArray_DOUBLE, 1, 2); + (edgecolors_obj.ptr(), NPY_DOUBLE, 1, 2); if (!edgecolors || (PyArray_NDIM(edgecolors) == 1 && PyArray_DIM(edgecolors, 0) != 0) || (PyArray_NDIM(edgecolors) == 2 && PyArray_DIM(edgecolors, 1) != 4)) @@ -1533,7 +1533,7 @@ RendererAgg::_draw_path_collection_generic Py::Object edgecolors_arr_obj((PyObject*)edgecolors, true); PyArrayObject* transforms_arr = (PyArrayObject*)PyArray_FromObject - (transforms_obj.ptr(), PyArray_DOUBLE, 1, 3); + (transforms_obj.ptr(), NPY_DOUBLE, 1, 3); if (!transforms_arr || (PyArray_NDIM(transforms_arr) == 1 && PyArray_DIM(transforms_arr, 0) != 0) || (PyArray_NDIM(transforms_arr) == 2) || @@ -1546,11 +1546,11 @@ RendererAgg::_draw_path_collection_generic } size_t Npaths = path_generator.num_paths(); - size_t Noffsets = offsets->dimensions[0]; + size_t Noffsets = PyArray_DIM(offsets, 0); size_t N = std::max(Npaths, Noffsets); - size_t Ntransforms = transforms_arr->dimensions[0]; - size_t Nfacecolors = facecolors->dimensions[0]; - size_t Nedgecolors = edgecolors->dimensions[0]; + size_t Ntransforms = PyArray_DIM(transforms_arr, 0); + size_t Nfacecolors = PyArray_DIM(facecolors, 0); + size_t Nedgecolors = PyArray_DIM(edgecolors, 0); size_t Nlinewidths = linewidths.length(); size_t Nlinestyles = std::min(linestyles_obj.length(), N); size_t Naa = antialiaseds.length(); @@ -1867,7 +1867,7 @@ class QuadMeshGenerator { PyArrayObject* coordinates_array = \ (PyArrayObject*)PyArray_ContiguousFromObject( - coordinates, PyArray_DOUBLE, 3, 3); + coordinates, NPY_DOUBLE, 3, 3); if (!coordinates_array) { @@ -1931,7 +1931,7 @@ RendererAgg::draw_quad_mesh(const Py::Tuple& args) else { npy_intp dims[] = { 0, 0 }; - edgecolors_obj = PyArray_SimpleNew(1, dims, PyArray_DOUBLE); + edgecolors_obj = PyArray_SimpleNew(1, dims, NPY_DOUBLE); } } @@ -2040,7 +2040,7 @@ RendererAgg::draw_gouraud_triangle(const Py::Tuple& args) bool has_clippath = render_clippath(gc.clippath, gc.clippath_trans); PyArrayObject* points = (PyArrayObject*)PyArray_ContiguousFromAny - (points_obj.ptr(), PyArray_DOUBLE, 2, 2); + (points_obj.ptr(), NPY_DOUBLE, 2, 2); if (!points || PyArray_DIM(points, 0) != 3 || PyArray_DIM(points, 1) != 2) { @@ -2050,7 +2050,7 @@ RendererAgg::draw_gouraud_triangle(const Py::Tuple& args) points_obj = Py::Object((PyObject*)points, true); PyArrayObject* colors = (PyArrayObject*)PyArray_ContiguousFromAny - (colors_obj.ptr(), PyArray_DOUBLE, 2, 2); + (colors_obj.ptr(), NPY_DOUBLE, 2, 2); if (!colors || PyArray_DIM(colors, 0) != 3 || PyArray_DIM(colors, 1) != 4) { @@ -2089,7 +2089,7 @@ RendererAgg::draw_gouraud_triangles(const Py::Tuple& args) bool has_clippath = render_clippath(gc.clippath, gc.clippath_trans); PyArrayObject* points = (PyArrayObject*)PyArray_FromObject - (points_obj.ptr(), PyArray_DOUBLE, 3, 3); + (points_obj.ptr(), NPY_DOUBLE, 3, 3); if (!points || PyArray_DIM(points, 1) != 3 || PyArray_DIM(points, 2) != 2) { @@ -2099,7 +2099,7 @@ RendererAgg::draw_gouraud_triangles(const Py::Tuple& args) points_obj = Py::Object((PyObject*)points, true); PyArrayObject* colors = (PyArrayObject*)PyArray_FromObject - (colors_obj.ptr(), PyArray_DOUBLE, 3, 3); + (colors_obj.ptr(), NPY_DOUBLE, 3, 3); if (!colors || PyArray_DIM(colors, 1) != 3 || PyArray_DIM(colors, 2) != 4) { diff --git a/src/_backend_gdk.c b/src/_backend_gdk.c index 152554c2187a..7d52c652586e 100644 --- a/src/_backend_gdk.c +++ b/src/_backend_gdk.c @@ -35,7 +35,7 @@ pixbuf_get_pixels_array(PyObject *self, PyObject *args) if (gdk_pixbuf_get_has_alpha(gdk_pixbuf)) dims[2] = 4; - array = (PyArrayObject *)PyArray_SimpleNewFromData(3, dims, PyArray_UBYTE, + array = (PyArrayObject *)PyArray_SimpleNewFromData(3, dims, NPY_UBYTE, (char *)gdk_pixbuf_get_pixels(gdk_pixbuf)); if (array == NULL) return NULL; diff --git a/src/_image.cpp b/src/_image.cpp index 73f2624c772c..4114d68e84e0 100644 --- a/src/_image.cpp +++ b/src/_image.cpp @@ -903,7 +903,7 @@ _image_module::fromarray(const Py::Tuple& args) Py::Object x = args[0]; int isoutput = Py::Int(args[1]); - PyArrayObject *A = (PyArrayObject *) PyArray_FromObject(x.ptr(), PyArray_DOUBLE, 2, 3); + PyArrayObject *A = (PyArrayObject *) PyArray_FromObject(x.ptr(), NPY_DOUBLE, 2, 3); if (A == NULL) { throw Py::ValueError("Array must be rank 2 or 3 of doubles"); @@ -912,8 +912,8 @@ _image_module::fromarray(const Py::Tuple& args) Image* imo = new Image; - imo->rowsIn = A->dimensions[0]; - imo->colsIn = A->dimensions[1]; + imo->rowsIn = PyArray_DIM(A, 0); + imo->colsIn = PyArray_DIM(A, 1); size_t NUMBYTES(imo->colsIn * imo->rowsIn * imo->BPP); agg::int8u *buffer = new agg::int8u[NUMBYTES]; @@ -939,14 +939,14 @@ _image_module::fromarray(const Py::Tuple& args) imo->rbufIn->attach(buffer, imo->colsIn, imo->rowsIn, imo->colsIn*imo->BPP); } - if (A->nd == 2) //assume luminance for now; + if (PyArray_NDIM(A) == 2) //assume luminance for now; { agg::int8u gray; for (size_t rownum = 0; rownum < imo->rowsIn; rownum++) { for (size_t colnum = 0; colnum < imo->colsIn; colnum++) { - double val = *(double *)(A->data + rownum * A->strides[0] + colnum * A->strides[1]); + double val = *(double *)PyArray_GETPTR2(A, rownum, colnum); gray = int(255 * val); *buffer++ = gray; // red @@ -956,15 +956,16 @@ _image_module::fromarray(const Py::Tuple& args) } } } - else if (A->nd == 3) // assume RGB + else if (PyArray_NDIM(A) == 3) // assume RGB { - if (A->dimensions[2] != 3 && A->dimensions[2] != 4) + if (PyArray_DIM(A, 2) != 3 && PyArray_DIM(A, 2) != 4) { - throw Py::ValueError(Printf("3rd dimension must be length 3 (RGB) or 4 (RGBA); found %d", A->dimensions[2]).str()); + throw Py::ValueError(Printf("3rd dimension must be length 3 (RGB) or 4 (RGBA); found %d", + PyArray_DIM(A, 2)).str()); } - int rgba = A->dimensions[2] == 4; + int rgba = PyArray_DIM(A, 2) == 4; double r, g, b, alpha; size_t offset = 0; @@ -972,14 +973,13 @@ _image_module::fromarray(const Py::Tuple& args) { for (size_t colnum = 0; colnum < imo->colsIn; colnum++) { - offset = rownum * A->strides[0] + colnum * A->strides[1]; - r = *(double *)(A->data + offset); - g = *(double *)(A->data + offset + A->strides[2]); - b = *(double *)(A->data + offset + 2 * A->strides[2]); + r = *(double*)PyArray_GETPTR3(A, rownum, colnum, 0); + g = *(double*)PyArray_GETPTR3(A, rownum, colnum, 1); + b = *(double*)PyArray_GETPTR3(A, rownum, colnum, 2); if (rgba) { - alpha = *(double *)(A->data + offset + 3 * A->strides[2]); + alpha = *(double*)PyArray_GETPTR3(A, rownum, colnum, 3); } else { @@ -1018,7 +1018,7 @@ _image_module::fromarray2(const Py::Tuple& args) Py::Object x = args[0]; int isoutput = Py::Int(args[1]); - PyArrayObject *A = (PyArrayObject *) PyArray_ContiguousFromObject(x.ptr(), PyArray_DOUBLE, 2, 3); + PyArrayObject *A = (PyArrayObject *) PyArray_ContiguousFromObject(x.ptr(), NPY_DOUBLE, 2, 3); if (A == NULL) { throw Py::ValueError("Array must be rank 2 or 3 of doubles"); @@ -1027,8 +1027,8 @@ _image_module::fromarray2(const Py::Tuple& args) Image* imo = new Image; - imo->rowsIn = A->dimensions[0]; - imo->colsIn = A->dimensions[1]; + imo->rowsIn = PyArray_DIM(A, 0); + imo->colsIn = PyArray_DIM(A, 1); size_t NUMBYTES(imo->colsIn * imo->rowsIn * imo->BPP); agg::int8u *buffer = new agg::int8u[NUMBYTES]; @@ -1054,50 +1054,45 @@ _image_module::fromarray2(const Py::Tuple& args) imo->rbufIn->attach(buffer, imo->colsIn, imo->rowsIn, imo->colsIn*imo->BPP); } - if (A->nd == 2) //assume luminance for now; + if (PyArray_NDIM(A) == 2) //assume luminance for now; { agg::int8u gray; - const size_t N = imo->rowsIn * imo->colsIn; - size_t i = 0; - while (i++ < N) - { - double val = *(double *)(A->data++); - - gray = int(255 * val); - *buffer++ = gray; // red - *buffer++ = gray; // green - *buffer++ = gray; // blue - *buffer++ = 255; // alpha + for (size_t row = 0; row < imo->rowsIn; row++) { + for (size_t col = 0; col < imo->colsIn; col++) { + const double val = *(double*)PyArray_GETPTR2(A, row, col); + gray = int(255 * val); + *buffer++ = gray; // red + *buffer++ = gray; // green + *buffer++ = gray; // blue + *buffer++ = 255; // alpha + } } - } - else if (A->nd == 3) // assume RGB + else if (PyArray_NDIM(A) == 3) // assume RGB { - if (A->dimensions[2] != 3 && A->dimensions[2] != 4) + if (PyArray_DIM(A, 2) != 3 && PyArray_DIM(A, 2) != 4) { - throw Py::ValueError(Printf("3rd dimension must be length 3 (RGB) or 4 (RGBA); found %d", A->dimensions[2]).str()); + throw Py::ValueError(Printf("3rd dimension must be length 3 (RGB) or 4 (RGBA); found %d", + PyArray_DIM(A, 2)).str()); } - int rgba = A->dimensions[2] == 4; + int rgba = PyArray_DIM(A, 2) == 4; double r, g, b, alpha; - const size_t N = imo->rowsIn * imo->colsIn; - for (size_t i = 0; i < N; ++i) - { - r = *(double *)(A->data++); - g = *(double *)(A->data++); - b = *(double *)(A->data++); - - if (rgba) - alpha = *(double *)(A->data++); - else - alpha = 1.0; - - *buffer++ = int(255 * r); // red - *buffer++ = int(255 * g); // green - *buffer++ = int(255 * b); // blue - *buffer++ = int(255 * alpha); // alpha - + for (size_t row = 0; row < imo->rowsIn; row++) { + for (size_t col = 0; col < imo->colsIn; col++) { + r = *(double*)PyArray_GETPTR3(A, row, col, 0); + g = *(double*)PyArray_GETPTR3(A, row, col, 1); + b = *(double*)PyArray_GETPTR3(A, row, col, 2); + if (rgba) + alpha = *(double*)PyArray_GETPTR3(A, row, col, 3); + else + alpha = 1.0; + *buffer++ = int(255 * r); // red + *buffer++ = int(255 * g); // green + *buffer++ = int(255 * b); // blue + *buffer++ = int(255 * alpha); // alpha + } } } else // error @@ -1126,28 +1121,28 @@ _image_module::frombyte(const Py::Tuple& args) Py::Object x = args[0]; int isoutput = Py::Int(args[1]); - PyArrayObject *A = (PyArrayObject *) PyArray_FromObject(x.ptr(), PyArray_UBYTE, 3, 3); + PyArrayObject *A = (PyArrayObject *) PyArray_FromObject(x.ptr(), NPY_UBYTE, 3, 3); if (A == NULL) { throw Py::ValueError("Array must have 3 dimensions"); } Py::Object A_obj((PyObject*)A, true); - if (A->dimensions[2] < 3 || A->dimensions[2] > 4) + if (PyArray_DIM(A, 2) < 3 || PyArray_DIM(A, 2) > 4) { throw Py::ValueError("Array dimension 3 must have size 3 or 4"); } Image* imo = new Image; - imo->rowsIn = A->dimensions[0]; - imo->colsIn = A->dimensions[1]; + imo->rowsIn = PyArray_DIM(A, 0); + imo->colsIn = PyArray_DIM(A, 1); agg::int8u *arrbuf; agg::int8u *buffer; agg::int8u *dstbuf; - arrbuf = reinterpret_cast(A->data); + arrbuf = reinterpret_cast(PyArray_DATA(A)); size_t NUMBYTES(imo->colsIn * imo->rowsIn * imo->BPP); buffer = dstbuf = new agg::int8u[NUMBYTES]; @@ -1157,9 +1152,9 @@ _image_module::frombyte(const Py::Tuple& args) throw Py::MemoryError("_image_module::frombyte could not allocate memory"); } - if PyArray_ISCONTIGUOUS(A) + if (PyArray_ISCONTIGUOUS(A)) { - if (A->dimensions[2] == 4) + if (PyArray_DIM(A, 2) == 4) { memmove(dstbuf, arrbuf, imo->rowsIn * imo->colsIn * 4); } @@ -1175,10 +1170,10 @@ _image_module::frombyte(const Py::Tuple& args) } } } - else if ((A->strides[1] == 4) && (A->strides[2] == 1)) + else if ((PyArray_STRIDE(A, 1) == 4) && (PyArray_STRIDE(A, 2) == 1)) { const size_t N = imo->colsIn * 4; - const size_t stride = A->strides[0]; + const size_t stride = PyArray_STRIDE(A, 0); for (size_t rownum = 0; rownum < imo->rowsIn; rownum++) { memmove(dstbuf, arrbuf, N); @@ -1186,9 +1181,9 @@ _image_module::frombyte(const Py::Tuple& args) dstbuf += N; } } - else if ((A->strides[1] == 3) && (A->strides[2] == 1)) + else if ((PyArray_STRIDE(A, 1) == 3) && (PyArray_STRIDE(A, 2) == 1)) { - const size_t stride = A->strides[0] - imo->colsIn * 3; + const size_t stride = PyArray_STRIDE(A, 0) - imo->colsIn * 3; for (size_t rownum = 0; rownum < imo->rowsIn; rownum++) { for (size_t colnum = 0; colnum < imo->colsIn; colnum++) @@ -1205,7 +1200,7 @@ _image_module::frombyte(const Py::Tuple& args) { PyArrayIterObject *iter; iter = (PyArrayIterObject *)PyArray_IterNew((PyObject *)A); - if (A->dimensions[2] == 4) + if (PyArray_DIM(A, 2) == 4) { while (iter->index < iter->size) { *dstbuf++ = *((unsigned char *)iter->dataptr); @@ -1610,34 +1605,34 @@ _image_module::pcolor(const Py::Tuple& args) float *arows = NULL; // Get numpy arrays - x = (PyArrayObject *) PyArray_ContiguousFromObject(xp.ptr(), PyArray_FLOAT, 1, 1); + x = (PyArrayObject *) PyArray_ContiguousFromObject(xp.ptr(), NPY_FLOAT, 1, 1); if (x == NULL) { _pcolor_cleanup(x, y, d, rowstarts, colstarts, acols, arows); throw Py::ValueError("x is of incorrect type (wanted 1D float)"); } - y = (PyArrayObject *) PyArray_ContiguousFromObject(yp.ptr(), PyArray_FLOAT, 1, 1); + y = (PyArrayObject *) PyArray_ContiguousFromObject(yp.ptr(), NPY_FLOAT, 1, 1); if (y == NULL) { _pcolor_cleanup(x, y, d, rowstarts, colstarts, acols, arows); throw Py::ValueError("y is of incorrect type (wanted 1D float)"); } - d = (PyArrayObject *) PyArray_ContiguousFromObject(dp.ptr(), PyArray_UBYTE, 3, 3); + d = (PyArrayObject *) PyArray_ContiguousFromObject(dp.ptr(), NPY_UBYTE, 3, 3); if (d == NULL) { _pcolor_cleanup(x, y, d, rowstarts, colstarts, acols, arows); throw Py::ValueError("data is of incorrect type (wanted 3D UInt8)"); } - if (d->dimensions[2] != 4) + if (PyArray_DIM(d, 2) != 4) { _pcolor_cleanup(x, y, d, rowstarts, colstarts, acols, arows); throw Py::ValueError("data must be in RGBA format"); } // Check dimensions match - int nx = x->dimensions[0]; - int ny = y->dimensions[0]; - if (nx != d->dimensions[1] || ny != d->dimensions[0]) + int nx = PyArray_DIM(x, 0); + int ny = PyArray_DIM(y, 0); + if (nx != PyArray_DIM(d, 1) || ny != PyArray_DIM(d, 0)) { _pcolor_cleanup(x, y, d, rowstarts, colstarts, acols, arows); throw Py::ValueError("data and axis dimensions do not match"); @@ -1676,8 +1671,8 @@ _image_module::pcolor(const Py::Tuple& args) unsigned int i, j; unsigned int * colstart = colstarts; unsigned int * rowstart = rowstarts; - float *xs1 = reinterpret_cast(x->data); - float *ys1 = reinterpret_cast(y->data); + float *xs1 = reinterpret_cast(PyArray_DATA(x)); + float *ys1 = reinterpret_cast(PyArray_DATA(y)); // Copy data to output buffer @@ -1687,9 +1682,9 @@ _image_module::pcolor(const Py::Tuple& args) size_t rowsize(cols*4); agg::int8u * position = buffer; agg::int8u * oldposition = NULL; - start = reinterpret_cast(d->data); - int s0 = d->strides[0]; - int s1 = d->strides[1]; + start = reinterpret_cast(PyArray_DATA(d)); + int s0 = PyArray_STRIDE(d, 0); + int s1 = PyArray_STRIDE(d, 1); if (interpolation == Image::NEAREST) { @@ -1851,46 +1846,46 @@ _image_module::pcolor2(const Py::Tuple& args) int* jcols = NULL; // Get numpy arrays - x = (PyArrayObject *) PyArray_ContiguousFromObject(xp.ptr(), PyArray_DOUBLE, 1, 1); + x = (PyArrayObject *) PyArray_ContiguousFromObject(xp.ptr(), NPY_DOUBLE, 1, 1); if (x == NULL) { _pcolor2_cleanup(x, y, d, bg, irows, jcols); throw Py::ValueError("x is of incorrect type (wanted 1D double)"); } - y = (PyArrayObject *) PyArray_ContiguousFromObject(yp.ptr(), PyArray_DOUBLE, 1, 1); + y = (PyArrayObject *) PyArray_ContiguousFromObject(yp.ptr(), NPY_DOUBLE, 1, 1); if (y == NULL) { _pcolor2_cleanup(x, y, d, bg, irows, jcols); throw Py::ValueError("y is of incorrect type (wanted 1D double)"); } - d = (PyArrayObject *) PyArray_ContiguousFromObject(dp.ptr(), PyArray_UBYTE, 3, 3); + d = (PyArrayObject *) PyArray_ContiguousFromObject(dp.ptr(), NPY_UBYTE, 3, 3); if (d == NULL) { _pcolor2_cleanup(x, y, d, bg, irows, jcols); throw Py::ValueError("data is of incorrect type (wanted 3D uint8)"); } - if (d->dimensions[2] != 4) + if (PyArray_DIM(d, 2) != 4) { _pcolor2_cleanup(x, y, d, bg, irows, jcols); throw Py::ValueError("data must be in RGBA format"); } // Check dimensions match - int nx = x->dimensions[0]; - int ny = y->dimensions[0]; - if (nx != d->dimensions[1] + 1 || ny != d->dimensions[0] + 1) + int nx = PyArray_DIM(x, 0); + int ny = PyArray_DIM(y, 0); + if (nx != PyArray_DIM(d, 1) + 1 || ny != PyArray_DIM(d, 0) + 1) { _pcolor2_cleanup(x, y, d, bg, irows, jcols); throw Py::ValueError("data and axis bin boundary dimensions are incompatible"); } - bg = (PyArrayObject *) PyArray_ContiguousFromObject(bgp.ptr(), PyArray_UBYTE, 1, 1); + bg = (PyArrayObject *) PyArray_ContiguousFromObject(bgp.ptr(), NPY_UBYTE, 1, 1); if (bg == NULL) { _pcolor2_cleanup(x, y, d, bg, irows, jcols); throw Py::ValueError("bg is of incorrect type (wanted 1D uint8)"); } - if (bg->dimensions[0] != 4) + if (PyArray_DIM(bg, 0) != 4) { _pcolor2_cleanup(x, y, d, bg, irows, jcols); throw Py::ValueError("bg must be in RGBA format"); @@ -1926,8 +1921,8 @@ _image_module::pcolor2(const Py::Tuple& args) // Calculate the pointer arrays to map input x to output x int i, j; - double *x0 = reinterpret_cast(x->data); - double *y0 = reinterpret_cast(y->data); + double *x0 = reinterpret_cast(PyArray_DATA(x)); + double *y0 = reinterpret_cast(PyArray_DATA(y)); double sx = cols / (x_right - x_left); double sy = rows / (y_top - y_bot); _bin_indices(jcols, cols, x0, nx, sx, x_left); @@ -1935,10 +1930,10 @@ _image_module::pcolor2(const Py::Tuple& args) // Copy data to output buffer agg::int8u * position = buffer; - unsigned char *start = reinterpret_cast(d->data); - unsigned char *bgptr = reinterpret_cast(bg->data); - int s0 = d->strides[0]; - int s1 = d->strides[1]; + unsigned char *start = reinterpret_cast(PyArray_DATA(d)); + unsigned char *bgptr = reinterpret_cast(PyArray_DATA(bg)); + int s0 = PyArray_STRIDE(d, 0); + int s1 = PyArray_STRIDE(d, 1); for (i = 0; i < rows; i++) { diff --git a/src/_macosx.m b/src/_macosx.m index aa326ef02483..40255426dbdb 100644 --- a/src/_macosx.m +++ b/src/_macosx.m @@ -1216,19 +1216,27 @@ static int _get_snap(GraphicsContext* self, enum e_snap_mode* mode) static int _transformation_converter(PyObject* object, void* pointer) { CGAffineTransform* matrix = (CGAffineTransform*)pointer; - if (!PyArray_Check(object) || PyArray_NDIM(object)!=2 - || PyArray_DIM(object, 0)!=3 || PyArray_DIM(object, 1)!=3) + if (!PyArray_Check(object)) + { + PyErr_SetString(PyExc_ValueError, + "transformation matrix is not a NumPy array"); + return 0; + } + PyArrayObject* aobject = (PyArrayObject*)object; + + if (PyArray_NDIM(aobject)!=2 || PyArray_DIM(aobject, 0)!=3 + || PyArray_DIM(aobject, 1)!=3) { PyErr_SetString(PyExc_ValueError, "transformation matrix is not a 3x3 NumPy array"); return 0; } - const double a = *(double*)PyArray_GETPTR2(object, 0, 0); - const double b = *(double*)PyArray_GETPTR2(object, 1, 0); - const double c = *(double*)PyArray_GETPTR2(object, 0, 1); - const double d = *(double*)PyArray_GETPTR2(object, 1, 1); - const double tx = *(double*)PyArray_GETPTR2(object, 0, 2); - const double ty = *(double*)PyArray_GETPTR2(object, 1, 2); + const double a = *(double*)PyArray_GETPTR2(aobject, 0, 0); + const double b = *(double*)PyArray_GETPTR2(aobject, 1, 0); + const double c = *(double*)PyArray_GETPTR2(aobject, 0, 1); + const double d = *(double*)PyArray_GETPTR2(aobject, 1, 1); + const double tx = *(double*)PyArray_GETPTR2(aobject, 0, 2); + const double ty = *(double*)PyArray_GETPTR2(aobject, 1, 2); *matrix = CGAffineTransformMake(a, b, c, d, tx, ty); return 1; } @@ -1240,9 +1248,12 @@ static int _transformation_converter(PyObject* object, void* pointer) PyObject* path_ids; PyObject* all_transforms; PyObject* offsets; + PyArrayObject* offsets_arr = 0; CGAffineTransform offset_transform; PyObject* facecolors; + PyArrayObject* facecolors_arr = 0; PyObject* edgecolors; + PyArrayObject* edgecolors_arr = 0; PyObject* linewidths; PyObject* linestyles; PyObject* antialiaseds; @@ -1347,29 +1358,29 @@ static int _transformation_converter(PyObject* object, void* pointer) /* ------------------- Check facecolors array ------------------------- */ - facecolors = PyArray_FromObject(facecolors, NPY_DOUBLE, 1, 2); - if (!facecolors || - (PyArray_NDIM(facecolors)==1 && PyArray_DIM(facecolors, 0)!=0) || - (PyArray_NDIM(facecolors)==2 && PyArray_DIM(facecolors, 1)!=4)) + facecolors_arr = (PyArrayObject*) PyArray_FromObject(facecolors, NPY_DOUBLE, 1, 2); + if (!facecolors_arr || + (PyArray_NDIM(facecolors_arr)==1 && PyArray_DIM(facecolors_arr, 0)!=0) || + (PyArray_NDIM(facecolors_arr)==2 && PyArray_DIM(facecolors_arr, 1)!=4)) { PyErr_SetString(PyExc_ValueError, "Facecolors must by a Nx4 numpy array or empty"); ok = 0; goto exit; } - Py_ssize_t Nfacecolors = PyArray_DIM(facecolors, 0); + Py_ssize_t Nfacecolors = PyArray_DIM(facecolors_arr, 0); /* ------------------- Check edgecolors array ------------------------- */ - edgecolors = PyArray_FromObject(edgecolors, NPY_DOUBLE, 1, 2); - if (!edgecolors || - (PyArray_NDIM(edgecolors)==1 && PyArray_DIM(edgecolors, 0)!=0) || - (PyArray_NDIM(edgecolors)==2 && PyArray_DIM(edgecolors, 1)!=4)) + edgecolors_arr = (PyArrayObject*) PyArray_FromObject(edgecolors, NPY_DOUBLE, 1, 2); + if (!edgecolors_arr || + (PyArray_NDIM(edgecolors_arr)==1 && PyArray_DIM(edgecolors_arr, 0)!=0) || + (PyArray_NDIM(edgecolors_arr)==2 && PyArray_DIM(edgecolors_arr, 1)!=4)) { PyErr_SetString(PyExc_ValueError, "Edgecolors must by a Nx4 numpy array or empty"); ok = 0; goto exit; } - Py_ssize_t Nedgecolors = PyArray_DIM(edgecolors, 0); + Py_ssize_t Nedgecolors = PyArray_DIM(edgecolors_arr, 0); /* -------------------------------------------------------------------- */ @@ -1378,35 +1389,35 @@ static int _transformation_converter(PyObject* object, void* pointer) /* ------------------- Check offsets array ---------------------------- */ - offsets = PyArray_FromObject(offsets, NPY_DOUBLE, 0, 2); + offsets_arr = (PyArrayObject*) PyArray_FromObject(offsets, NPY_DOUBLE, 0, 2); - if (!offsets || - (PyArray_NDIM(offsets)==2 && PyArray_DIM(offsets, 1)!=2) || - (PyArray_NDIM(offsets)==1 && PyArray_DIM(offsets, 0)!=0)) + if (!offsets_arr || + (PyArray_NDIM(offsets_arr)==2 && PyArray_DIM(offsets_arr, 1)!=2) || + (PyArray_NDIM(offsets_arr)==1 && PyArray_DIM(offsets_arr, 0)!=0)) { - Py_XDECREF(offsets); + Py_XDECREF(offsets_arr); PyErr_SetString(PyExc_ValueError, "Offsets array must be Nx2"); ok = 0; goto exit; } - const Py_ssize_t Noffsets = PyArray_DIM(offsets, 0); + const Py_ssize_t Noffsets = PyArray_DIM(offsets_arr, 0); if (Noffsets > 0) { toffsets = malloc(Noffsets*sizeof(CGPoint)); if (!toffsets) { - Py_DECREF(offsets); + Py_DECREF(offsets_arr); ok = 0; goto exit; } CGPoint point; for (i = 0; i < Noffsets; i++) { - point.x = (CGFloat) (*(double*)PyArray_GETPTR2(offsets, i, 0)); - point.y = (CGFloat) (*(double*)PyArray_GETPTR2(offsets, i, 1)); + point.x = (CGFloat) (*(double*)PyArray_GETPTR2(offsets_arr, i, 0)); + point.y = (CGFloat) (*(double*)PyArray_GETPTR2(offsets_arr, i, 1)); toffsets[i] = CGPointApplyAffineTransform(point, offset_transform); } } - Py_DECREF(offsets); + Py_DECREF(offsets_arr); /* ------------------- Check transforms ------------------------------- */ @@ -1571,10 +1582,10 @@ static int _transformation_converter(PyObject* object, void* pointer) if (Nedgecolors==1) { - const double r = *(double*)PyArray_GETPTR2(edgecolors, 0, 0); - const double g = *(double*)PyArray_GETPTR2(edgecolors, 0, 1); - const double b = *(double*)PyArray_GETPTR2(edgecolors, 0, 2); - const double a = *(double*)PyArray_GETPTR2(edgecolors, 0, 3); + const double r = *(double*)PyArray_GETPTR2(edgecolors_arr, 0, 0); + const double g = *(double*)PyArray_GETPTR2(edgecolors_arr, 0, 1); + const double b = *(double*)PyArray_GETPTR2(edgecolors_arr, 0, 2); + const double a = *(double*)PyArray_GETPTR2(edgecolors_arr, 0, 3); CGContextSetRGBStrokeColor(cr, r, g, b, a); self->color[0] = r; self->color[1] = g; @@ -1591,10 +1602,10 @@ static int _transformation_converter(PyObject* object, void* pointer) if (Nfacecolors==1) { - const double r = *(double*)PyArray_GETPTR2(facecolors, 0, 0); - const double g = *(double*)PyArray_GETPTR2(facecolors, 0, 1); - const double b = *(double*)PyArray_GETPTR2(facecolors, 0, 2); - const double a = *(double*)PyArray_GETPTR2(facecolors, 0, 3); + const double r = *(double*)PyArray_GETPTR2(facecolors_arr, 0, 0); + const double g = *(double*)PyArray_GETPTR2(facecolors_arr, 0, 1); + const double b = *(double*)PyArray_GETPTR2(facecolors_arr, 0, 2); + const double a = *(double*)PyArray_GETPTR2(facecolors_arr, 0, 3); CGContextSetRGBFillColor(cr, r, g, b, a); } @@ -1674,10 +1685,10 @@ static int _transformation_converter(PyObject* object, void* pointer) if (Nedgecolors > 1) { npy_intp fi = i % Nedgecolors; - const double r = *(double*)PyArray_GETPTR2(edgecolors, fi, 0); - const double g = *(double*)PyArray_GETPTR2(edgecolors, fi, 1); - const double b = *(double*)PyArray_GETPTR2(edgecolors, fi, 2); - const double a = *(double*)PyArray_GETPTR2(edgecolors, fi, 3); + const double r = *(double*)PyArray_GETPTR2(edgecolors_arr, fi, 0); + const double g = *(double*)PyArray_GETPTR2(edgecolors_arr, fi, 1); + const double b = *(double*)PyArray_GETPTR2(edgecolors_arr, fi, 2); + const double a = *(double*)PyArray_GETPTR2(edgecolors_arr, fi, 3); CGContextSetRGBStrokeColor(cr, r, g, b, a); } @@ -1686,10 +1697,10 @@ static int _transformation_converter(PyObject* object, void* pointer) if (Nfacecolors > 1) { npy_intp fi = i % Nfacecolors; - const double r = *(double*)PyArray_GETPTR2(facecolors, fi, 0); - const double g = *(double*)PyArray_GETPTR2(facecolors, fi, 1); - const double b = *(double*)PyArray_GETPTR2(facecolors, fi, 2); - const double a = *(double*)PyArray_GETPTR2(facecolors, fi, 3); + const double r = *(double*)PyArray_GETPTR2(facecolors_arr, fi, 0); + const double g = *(double*)PyArray_GETPTR2(facecolors_arr, fi, 1); + const double b = *(double*)PyArray_GETPTR2(facecolors_arr, fi, 2); + const double a = *(double*)PyArray_GETPTR2(facecolors_arr, fi, 3); CGContextSetRGBFillColor(cr, r, g, b, a); if (Nedgecolors > 0) CGContextDrawPath(cr, kCGPathFillStroke); else CGContextFillPath(cr); @@ -1718,8 +1729,8 @@ static int _transformation_converter(PyObject* object, void* pointer) exit: CGContextRestoreGState(cr); - Py_XDECREF(facecolors); - Py_XDECREF(edgecolors); + Py_XDECREF(facecolors_arr); + Py_XDECREF(edgecolors_arr); if (pattern) CGPatternRelease(pattern); if (patternSpace) CGColorSpaceRelease(patternSpace); if (transforms) free(transforms); @@ -1745,11 +1756,15 @@ static int _transformation_converter(PyObject* object, void* pointer) int meshWidth; int meshHeight; PyObject* coordinates; + PyArrayObject* coordinates_arr = 0; PyObject* offsets; + PyArrayObject* offsets_arr = 0; CGAffineTransform offset_transform; PyObject* facecolors; + PyArrayObject* facecolors_arr = 0; int antialiased; PyObject* edgecolors; + PyArrayObject* edgecolors_arr = 0; CGPoint *toffsets = NULL; @@ -1777,9 +1792,9 @@ static int _transformation_converter(PyObject* object, void* pointer) /* ------------------- Check coordinates array ------------------------ */ - coordinates = PyArray_FromObject(coordinates, NPY_DOUBLE, 3, 3); - if (!coordinates || - PyArray_NDIM(coordinates) != 3 || PyArray_DIM(coordinates, 2) != 2) + coordinates_arr = (PyArrayObject*) PyArray_FromObject(coordinates, NPY_DOUBLE, 3, 3); + if (!coordinates_arr || + PyArray_NDIM(coordinates_arr) != 3 || PyArray_DIM(coordinates_arr, 2) != 2) { PyErr_SetString(PyExc_ValueError, "Invalid coordinates array"); ok = 0; @@ -1788,43 +1803,43 @@ static int _transformation_converter(PyObject* object, void* pointer) /* ------------------- Check offsets array ---------------------------- */ - offsets = PyArray_FromObject(offsets, NPY_DOUBLE, 0, 2); + offsets_arr = (PyArrayObject*) PyArray_FromObject(offsets, NPY_DOUBLE, 0, 2); - if (!offsets || - (PyArray_NDIM(offsets)==2 && PyArray_DIM(offsets, 1)!=2) || - (PyArray_NDIM(offsets)==1 && PyArray_DIM(offsets, 0)!=0)) + if (!offsets_arr || + (PyArray_NDIM(offsets_arr)==2 && PyArray_DIM(offsets_arr, 1)!=2) || + (PyArray_NDIM(offsets_arr)==1 && PyArray_DIM(offsets_arr, 0)!=0)) { - Py_XDECREF(offsets); + Py_XDECREF(offsets_arr); PyErr_SetString(PyExc_ValueError, "Offsets array must be Nx2"); ok = 0; goto exit; } - const Py_ssize_t Noffsets = PyArray_DIM(offsets, 0); + const Py_ssize_t Noffsets = PyArray_DIM(offsets_arr, 0); if (Noffsets > 0) { int i; toffsets = malloc(Noffsets*sizeof(CGPoint)); if (!toffsets) { - Py_DECREF(offsets); + Py_DECREF(offsets_arr); ok = 0; goto exit; } CGPoint point; for (i = 0; i < Noffsets; i++) { - point.x = (CGFloat) (*(double*)PyArray_GETPTR2(offsets, i, 0)); - point.y = (CGFloat) (*(double*)PyArray_GETPTR2(offsets, i, 1)); + point.x = (CGFloat) (*(double*)PyArray_GETPTR2(offsets_arr, i, 0)); + point.y = (CGFloat) (*(double*)PyArray_GETPTR2(offsets_arr, i, 1)); toffsets[i] = CGPointApplyAffineTransform(point, offset_transform); } } - Py_DECREF(offsets); + Py_DECREF(offsets_arr); /* ------------------- Check facecolors array ------------------------- */ - facecolors = PyArray_FromObject(facecolors, NPY_DOUBLE, 1, 2); - if (!facecolors || - (PyArray_NDIM(facecolors)==1 && PyArray_DIM(facecolors, 0)!=0) || - (PyArray_NDIM(facecolors)==2 && PyArray_DIM(facecolors, 1)!=4)) + facecolors_arr = (PyArrayObject*) PyArray_FromObject(facecolors, NPY_DOUBLE, 1, 2); + if (!facecolors_arr || + (PyArray_NDIM(facecolors_arr)==1 && PyArray_DIM(facecolors_arr, 0)!=0) || + (PyArray_NDIM(facecolors_arr)==2 && PyArray_DIM(facecolors_arr, 1)!=4)) { PyErr_SetString(PyExc_ValueError, "facecolors must by a Nx4 numpy array or empty"); ok = 0; @@ -1833,10 +1848,10 @@ static int _transformation_converter(PyObject* object, void* pointer) /* ------------------- Check edgecolors array ------------------------- */ - edgecolors = PyArray_FromObject(edgecolors, NPY_DOUBLE, 1, 2); - if (!edgecolors || - (PyArray_NDIM(edgecolors)==1 && PyArray_DIM(edgecolors, 0)!=0) || - (PyArray_NDIM(edgecolors)==2 && PyArray_DIM(edgecolors, 1)!=4)) + edgecolors_arr = (PyArrayObject*) PyArray_FromObject(edgecolors, NPY_DOUBLE, 1, 2); + if (!edgecolors_arr || + (PyArray_NDIM(edgecolors_arr)==1 && PyArray_DIM(edgecolors_arr, 0)!=0) || + (PyArray_NDIM(edgecolors_arr)==2 && PyArray_DIM(edgecolors_arr, 1)!=4)) { PyErr_SetString(PyExc_ValueError, "edgecolors must by a Nx4 numpy array or empty"); ok = 0; @@ -1846,8 +1861,8 @@ static int _transformation_converter(PyObject* object, void* pointer) /* ------------------- Check the other arguments ---------------------- */ size_t Npaths = meshWidth * meshHeight; - size_t Nfacecolors = (size_t) PyArray_DIM(facecolors, 0); - size_t Nedgecolors = (size_t) PyArray_DIM(edgecolors, 0); + size_t Nfacecolors = (size_t) PyArray_DIM(facecolors_arr, 0); + size_t Nedgecolors = (size_t) PyArray_DIM(edgecolors_arr, 0); if ((Nfacecolors == 0 && Nedgecolors == 0) || Npaths == 0) { /* Nothing to do here */ @@ -1863,10 +1878,10 @@ static int _transformation_converter(PyObject* object, void* pointer) if (Nfacecolors==1) { - const double r = *(double*)PyArray_GETPTR2(facecolors, 0, 0); - const double g = *(double*)PyArray_GETPTR2(facecolors, 0, 1); - const double b = *(double*)PyArray_GETPTR2(facecolors, 0, 2); - const double a = *(double*)PyArray_GETPTR2(facecolors, 0, 3); + const double r = *(double*)PyArray_GETPTR2(facecolors_arr, 0, 0); + const double g = *(double*)PyArray_GETPTR2(facecolors_arr, 0, 1); + const double b = *(double*)PyArray_GETPTR2(facecolors_arr, 0, 2); + const double a = *(double*)PyArray_GETPTR2(facecolors_arr, 0, 3); CGContextSetRGBFillColor(cr, r, g, b, a); if (antialiased && Nedgecolors==0) { @@ -1875,10 +1890,10 @@ static int _transformation_converter(PyObject* object, void* pointer) } if (Nedgecolors==1) { - const double r = *(double*)PyArray_GETPTR2(edgecolors, 0, 0); - const double g = *(double*)PyArray_GETPTR2(edgecolors, 0, 1); - const double b = *(double*)PyArray_GETPTR2(edgecolors, 0, 2); - const double a = *(double*)PyArray_GETPTR2(edgecolors, 0, 3); + const double r = *(double*)PyArray_GETPTR2(edgecolors_arr, 0, 0); + const double g = *(double*)PyArray_GETPTR2(edgecolors_arr, 0, 1); + const double b = *(double*)PyArray_GETPTR2(edgecolors_arr, 0, 2); + const double a = *(double*)PyArray_GETPTR2(edgecolors_arr, 0, 3); CGContextSetRGBStrokeColor(cr, r, g, b, a); } @@ -1890,26 +1905,26 @@ static int _transformation_converter(PyObject* object, void* pointer) { CGPoint points[4]; - x = *(double*)PyArray_GETPTR3(coordinates, ih, iw, 0); - y = *(double*)PyArray_GETPTR3(coordinates, ih, iw, 1); + x = *(double*)PyArray_GETPTR3(coordinates_arr, ih, iw, 0); + y = *(double*)PyArray_GETPTR3(coordinates_arr, ih, iw, 1); if (isnan(x) || isnan(y)) continue; points[0].x = (CGFloat)x; points[0].y = (CGFloat)y; - x = *(double*)PyArray_GETPTR3(coordinates, ih, iw+1, 0); - y = *(double*)PyArray_GETPTR3(coordinates, ih, iw+1, 1); + x = *(double*)PyArray_GETPTR3(coordinates_arr, ih, iw+1, 0); + y = *(double*)PyArray_GETPTR3(coordinates_arr, ih, iw+1, 1); if (isnan(x) || isnan(y)) continue; points[1].x = (CGFloat)x; points[1].y = (CGFloat)y; - x = *(double*)PyArray_GETPTR3(coordinates, ih+1, iw+1, 0); - y = *(double*)PyArray_GETPTR3(coordinates, ih+1, iw+1, 1); + x = *(double*)PyArray_GETPTR3(coordinates_arr, ih+1, iw+1, 0); + y = *(double*)PyArray_GETPTR3(coordinates_arr, ih+1, iw+1, 1); if (isnan(x) || isnan(y)) continue; points[2].x = (CGFloat)x; points[2].y = (CGFloat)y; - x = *(double*)PyArray_GETPTR3(coordinates, ih+1, iw, 0); - y = *(double*)PyArray_GETPTR3(coordinates, ih+1, iw, 1); + x = *(double*)PyArray_GETPTR3(coordinates_arr, ih+1, iw, 0); + y = *(double*)PyArray_GETPTR3(coordinates_arr, ih+1, iw, 1); if (isnan(x) || isnan(y)) continue; points[3].x = (CGFloat)x; points[3].y = (CGFloat)y; @@ -1932,10 +1947,10 @@ static int _transformation_converter(PyObject* object, void* pointer) if (Nfacecolors > 1) { npy_intp fi = i % Nfacecolors; - const double r = *(double*)PyArray_GETPTR2(facecolors, fi, 0); - const double g = *(double*)PyArray_GETPTR2(facecolors, fi, 1); - const double b = *(double*)PyArray_GETPTR2(facecolors, fi, 2); - const double a = *(double*)PyArray_GETPTR2(facecolors, fi, 3); + const double r = *(double*)PyArray_GETPTR2(facecolors_arr, fi, 0); + const double g = *(double*)PyArray_GETPTR2(facecolors_arr, fi, 1); + const double b = *(double*)PyArray_GETPTR2(facecolors_arr, fi, 2); + const double a = *(double*)PyArray_GETPTR2(facecolors_arr, fi, 3); CGContextSetRGBFillColor(cr, r, g, b, a); if (antialiased && Nedgecolors==0) { @@ -1945,10 +1960,10 @@ static int _transformation_converter(PyObject* object, void* pointer) if (Nedgecolors > 1) { npy_intp fi = i % Nedgecolors; - const double r = *(double*)PyArray_GETPTR2(edgecolors, fi, 0); - const double g = *(double*)PyArray_GETPTR2(edgecolors, fi, 1); - const double b = *(double*)PyArray_GETPTR2(edgecolors, fi, 2); - const double a = *(double*)PyArray_GETPTR2(edgecolors, fi, 3); + const double r = *(double*)PyArray_GETPTR2(edgecolors_arr, fi, 0); + const double g = *(double*)PyArray_GETPTR2(edgecolors_arr, fi, 1); + const double b = *(double*)PyArray_GETPTR2(edgecolors_arr, fi, 2); + const double a = *(double*)PyArray_GETPTR2(edgecolors_arr, fi, 3); CGContextSetRGBStrokeColor(cr, r, g, b, a); } @@ -1977,8 +1992,9 @@ static int _transformation_converter(PyObject* object, void* pointer) exit: CGContextRestoreGState(cr); if (toffsets) free(toffsets); - Py_XDECREF(facecolors); - Py_XDECREF(coordinates); + Py_XDECREF(facecolors_arr); + Py_XDECREF(edgecolors_arr); + Py_XDECREF(coordinates_arr); if (!ok) return NULL; @@ -2256,7 +2272,9 @@ static CGFloat _get_device_scale(CGContextRef cr) { PyObject* coordinates; + PyArrayObject* coordinates_arr = 0; PyObject* colors; + PyArrayObject* colors_arr = 0; CGPoint points[3]; CGFloat intensity[3]; @@ -2274,30 +2292,30 @@ static CGFloat _get_device_scale(CGContextRef cr) /* ------------------- Check coordinates array ------------------------ */ - coordinates = PyArray_FromObject(coordinates, NPY_DOUBLE, 2, 2); - if (!coordinates || - PyArray_DIM(coordinates, 0) != 3 || PyArray_DIM(coordinates, 1) != 2) + coordinates_arr = (PyArrayObject*) PyArray_FromObject(coordinates, NPY_DOUBLE, 2, 2); + if (!coordinates_arr || + PyArray_DIM(coordinates_arr, 0) != 3 || PyArray_DIM(coordinates_arr, 1) != 2) { PyErr_SetString(PyExc_ValueError, "Invalid coordinates array"); - Py_XDECREF(coordinates); + Py_XDECREF(coordinates_arr); return NULL; } - points[0].x = *((double*)(PyArray_GETPTR2(coordinates, 0, 0))); - points[0].y = *((double*)(PyArray_GETPTR2(coordinates, 0, 1))); - points[1].x = *((double*)(PyArray_GETPTR2(coordinates, 1, 0))); - points[1].y = *((double*)(PyArray_GETPTR2(coordinates, 1, 1))); - points[2].x = *((double*)(PyArray_GETPTR2(coordinates, 2, 0))); - points[2].y = *((double*)(PyArray_GETPTR2(coordinates, 2, 1))); + points[0].x = *((double*)(PyArray_GETPTR2(coordinates_arr, 0, 0))); + points[0].y = *((double*)(PyArray_GETPTR2(coordinates_arr, 0, 1))); + points[1].x = *((double*)(PyArray_GETPTR2(coordinates_arr, 1, 0))); + points[1].y = *((double*)(PyArray_GETPTR2(coordinates_arr, 1, 1))); + points[2].x = *((double*)(PyArray_GETPTR2(coordinates_arr, 2, 0))); + points[2].y = *((double*)(PyArray_GETPTR2(coordinates_arr, 2, 1))); /* ------------------- Check colors array ----------------------------- */ - colors = PyArray_FromObject(colors, NPY_DOUBLE, 2, 2); - if (!colors || - PyArray_DIM(colors, 0) != 3 || PyArray_DIM(colors, 1) != 4) + colors_arr = (PyArrayObject*) PyArray_FromObject(colors, NPY_DOUBLE, 2, 2); + if (!colors_arr || + PyArray_DIM(colors_arr, 0) != 3 || PyArray_DIM(colors_arr, 1) != 4) { PyErr_SetString(PyExc_ValueError, "colors must by a 3x4 array"); - Py_DECREF(coordinates); - Py_XDECREF(colors); + Py_DECREF(coordinates_arr); + Py_XDECREF(colors_arr); return NULL; } @@ -2307,25 +2325,25 @@ static CGFloat _get_device_scale(CGContextRef cr) CGContextAddLineToPoint(cr, points[1].x, points[1].y); CGContextAddLineToPoint(cr, points[2].x, points[2].y); CGContextClip(cr); - intensity[0] = *((double*)(PyArray_GETPTR2(colors, 0, 3))); - intensity[1] = *((double*)(PyArray_GETPTR2(colors, 1, 3))); - intensity[2] = *((double*)(PyArray_GETPTR2(colors, 2, 3))); + intensity[0] = *((double*)(PyArray_GETPTR2(colors_arr, 0, 3))); + intensity[1] = *((double*)(PyArray_GETPTR2(colors_arr, 1, 3))); + intensity[2] = *((double*)(PyArray_GETPTR2(colors_arr, 2, 3))); if (_shade_alpha(cr, intensity, points)!=-1) { CGContextBeginTransparencyLayer(cr, NULL); CGContextSetBlendMode(cr, kCGBlendModeScreen); for (i = 0; i < 3; i++) { - intensity[0] = *((double*)(PyArray_GETPTR2(colors, 0, i))); - intensity[1] = *((double*)(PyArray_GETPTR2(colors, 1, i))); - intensity[2] = *((double*)(PyArray_GETPTR2(colors, 2, i))); + intensity[0] = *((double*)(PyArray_GETPTR2(colors_arr, 0, i))); + intensity[1] = *((double*)(PyArray_GETPTR2(colors_arr, 1, i))); + intensity[2] = *((double*)(PyArray_GETPTR2(colors_arr, 2, i))); if (!_shade_one_color(cr, intensity, points, i)) break; } CGContextEndTransparencyLayer(cr); } CGContextRestoreGState(cr); - Py_DECREF(coordinates); - Py_DECREF(colors); + Py_DECREF(coordinates_arr); + Py_DECREF(colors_arr); if (i < 3) /* break encountered */ { diff --git a/src/_path.cpp b/src/_path.cpp index 27bb4b563fc2..cfbc01b5162b 100644 --- a/src/_path.cpp +++ b/src/_path.cpp @@ -362,7 +362,7 @@ _path_module::points_in_path(const Py::Tuple& args) npy_intp n; PyArrayObject* points_array; - points_array = (PyArrayObject*)PyArray_FromObject(args[0].ptr(), PyArray_DOUBLE, 2, 2); + points_array = (PyArrayObject*)PyArray_FromObject(args[0].ptr(), NPY_DOUBLE, 2, 2); if (points_array == NULL || PyArray_DIM(points_array, 1) != 2) { throw Py::TypeError( "Argument 0 to points_in_path must be an Nx2 numpy array"); @@ -373,7 +373,7 @@ _path_module::points_in_path(const Py::Tuple& args) agg::trans_affine trans = py_to_agg_transformation_matrix(args[3].ptr(), false); n = PyArray_DIM(points_array, 0); - PyObject* result = PyArray_ZEROS(1, &n, PyArray_BOOL, 0); + PyObject* result = PyArray_ZEROS(1, &n, NPY_BOOL, 0); if (result == NULL) { throw Py::MemoryError("Could not allocate memory for result"); } @@ -382,7 +382,7 @@ _path_module::points_in_path(const Py::Tuple& args) PyArray_STRIDE(points_array, 0), PyArray_STRIDE(points_array, 1), n, r, path, trans, - (npy_bool *)PyArray_DATA(result)); + (npy_bool *)PyArray_DATA((PyArrayObject*)result)); Py_DECREF(points_array); return Py::Object(result, true);; @@ -460,7 +460,7 @@ _path_module::get_path_extents(const Py::Tuple& args) try { extents = (PyArrayObject*)PyArray_SimpleNew - (2, extent_dims, PyArray_DOUBLE); + (2, extent_dims, NPY_DOUBLE); if (extents == NULL) { throw Py::MemoryError("Could not allocate result array"); @@ -511,7 +511,7 @@ _path_module::update_path_extents(const Py::Tuple& args) try { input_minpos = (PyArrayObject*)PyArray_FromObject( - minpos_obj.ptr(), PyArray_DOUBLE, 1, 1); + minpos_obj.ptr(), NPY_DOUBLE, 1, 1); if (!input_minpos || PyArray_DIM(input_minpos, 0) != 2) { throw Py::TypeError( @@ -538,13 +538,13 @@ _path_module::update_path_extents(const Py::Tuple& args) try { extents = (PyArrayObject*)PyArray_SimpleNew - (2, extent_dims, PyArray_DOUBLE); + (2, extent_dims, NPY_DOUBLE); if (extents == NULL) { throw Py::MemoryError("Could not allocate result array"); } minpos = (PyArrayObject*)PyArray_SimpleNew - (1, minpos_dims, PyArray_DOUBLE); + (1, minpos_dims, NPY_DOUBLE); if (minpos == NULL) { throw Py::MemoryError("Could not allocate result array"); @@ -636,7 +636,7 @@ _path_module::get_path_collection_extents(const Py::Tuple& args) try { offsets = (PyArrayObject*)PyArray_FromObject( - offsets_obj.ptr(), PyArray_DOUBLE, 0, 2); + offsets_obj.ptr(), NPY_DOUBLE, 0, 2); if (!offsets || (PyArray_NDIM(offsets) == 2 && PyArray_DIM(offsets, 1) != 2) || (PyArray_NDIM(offsets) == 1 && PyArray_DIM(offsets, 0) != 0)) @@ -645,7 +645,7 @@ _path_module::get_path_collection_extents(const Py::Tuple& args) } size_t Npaths = paths.length(); - size_t Noffsets = offsets->dimensions[0]; + size_t Noffsets = PyArray_DIM(offsets, 0); size_t N = std::max(Npaths, Noffsets); size_t Ntransforms = std::min(transforms_obj.length(), N); size_t i; @@ -761,7 +761,7 @@ _path_module::point_in_path_collection(const Py::Tuple& args) bool data_offsets = (offset_position == "data"); PyArrayObject* offsets = (PyArrayObject*)PyArray_FromObject( - offsets_obj.ptr(), PyArray_DOUBLE, 0, 2); + offsets_obj.ptr(), NPY_DOUBLE, 0, 2); if (!offsets || (PyArray_NDIM(offsets) == 2 && PyArray_DIM(offsets, 1) != 2) || (PyArray_NDIM(offsets) == 1 && PyArray_DIM(offsets, 0) != 0)) @@ -771,7 +771,7 @@ _path_module::point_in_path_collection(const Py::Tuple& args) } size_t Npaths = paths.length(); - size_t Noffsets = offsets->dimensions[0]; + size_t Noffsets = PyArray_DIM(offsets, 0); size_t N = std::max(Npaths, Noffsets); size_t Ntransforms = std::min(transforms_obj.length(), N); size_t i; @@ -1120,18 +1120,19 @@ _path_module::clip_path_to_rect(const Py::Tuple &args) { size_t size = p->size(); dims[0] = (npy_intp)size + 1; - PyArrayObject* pyarray = (PyArrayObject*)PyArray_SimpleNew(2, dims, PyArray_DOUBLE); + PyArrayObject* pyarray = (PyArrayObject*)PyArray_SimpleNew(2, dims, NPY_DOUBLE); if (pyarray == NULL) { throw Py::MemoryError("Could not allocate result array"); } + double* const data = (double*)PyArray_DATA(pyarray); for (size_t i = 0; i < size; ++i) { - ((double *)pyarray->data)[2*i] = (*p)[i].x; - ((double *)pyarray->data)[2*i+1] = (*p)[i].y; + data[2*i] = (*p)[i].x; + data[2*i+1] = (*p)[i].y; } - ((double *)pyarray->data)[2*size] = (*p)[0].x; - ((double *)pyarray->data)[2*size+1] = (*p)[0].y; + data[2*size] = (*p)[0].x; + data[2*size+1] = (*p)[0].y; if (PyList_SetItem(py_results, p - results.begin(), (PyObject *)pyarray) == -1) { @@ -1163,7 +1164,7 @@ _path_module::affine_transform(const Py::Tuple& args) try { vertices = (PyArrayObject*)PyArray_FromObject - (vertices_obj.ptr(), PyArray_DOUBLE, 1, 2); + (vertices_obj.ptr(), NPY_DOUBLE, 1, 2); if (!vertices || (PyArray_NDIM(vertices) == 2 && PyArray_DIM(vertices, 0) != 0 && PyArray_DIM(vertices, 1) != 2) || @@ -1174,7 +1175,7 @@ _path_module::affine_transform(const Py::Tuple& args) } transform = (PyArrayObject*) PyArray_FromObject - (transform_obj.ptr(), PyArray_DOUBLE, 2, 2); + (transform_obj.ptr(), NPY_DOUBLE, 2, 2); if (!transform || PyArray_DIM(transform, 0) != 3 || PyArray_DIM(transform, 1) != 3) @@ -1203,7 +1204,7 @@ _path_module::affine_transform(const Py::Tuple& args) } result = (PyArrayObject*)PyArray_SimpleNew - (PyArray_NDIM(vertices), PyArray_DIMS(vertices), PyArray_DOUBLE); + (PyArray_NDIM(vertices), PyArray_DIMS(vertices), NPY_DOUBLE); if (result == NULL) { throw Py::MemoryError("Could not allocate memory for path"); @@ -1424,7 +1425,7 @@ _add_polygon(Py::List& polygons, const std::vector& polygon) npy_intp polygon_dims[] = { static_cast(polygon.size() / 2), 2, 0 }; PyArrayObject* polygon_array = NULL; polygon_array = (PyArrayObject*)PyArray_SimpleNew - (2, polygon_dims, PyArray_DOUBLE); + (2, polygon_dims, NPY_DOUBLE); if (!polygon_array) { throw Py::MemoryError("Error creating polygon array"); @@ -1641,14 +1642,14 @@ _path_module::cleanup_path(const Py::Tuple& args) try { vertices_obj = (PyArrayObject*)PyArray_SimpleNew - (2, dims, PyArray_DOUBLE); + (2, dims, NPY_DOUBLE); if (vertices_obj == NULL) { throw Py::MemoryError("Could not allocate result array"); } codes_obj = (PyArrayObject*)PyArray_SimpleNew - (1, dims, PyArray_UINT8); + (1, dims, NPY_UINT8); if (codes_obj == NULL) { throw Py::MemoryError("Could not allocate result array"); diff --git a/src/_png.cpp b/src/_png.cpp index 1679448225e9..265620fa8809 100644 --- a/src/_png.cpp +++ b/src/_png.cpp @@ -483,18 +483,22 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result, throw Py::MemoryError("Could not allocate image array"); } - for (png_uint_32 y = 0; y < height; y++) + const npy_intp ystride = PyArray_STRIDE(A, 0); + const npy_intp xstride = PyArray_STRIDE(A, 1); + const npy_intp pstride = PyArray_STRIDE(A, 2); + char* target_row = PyArray_BYTES(A); + for (png_uint_32 y = 0; y < height; y++, target_row += ystride) { png_byte* row = row_pointers[y]; - for (png_uint_32 x = 0; x < width; x++) + char* target_pixel = target_row; + for (png_uint_32 x = 0; x < width; x++, target_pixel += xstride) { - size_t offset = y * A->strides[0] + x * A->strides[1]; if (bit_depth == 16) { png_uint_16* ptr = &reinterpret_cast(row)[x * dimensions[2]]; for (png_uint_32 p = 0; p < (png_uint_32)dimensions[2]; p++) { - *(float*)(A->data + offset + p*A->strides[2]) = (float)(ptr[p]) / max_value; + *(float*)(target_pixel + p * pstride) = (float)(ptr[p]) / max_value; } } else @@ -502,7 +506,7 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result, png_byte* ptr = &(row[x * dimensions[2]]); for (png_uint_32 p = 0; p < (png_uint_32)dimensions[2]; p++) { - *(float*)(A->data + offset + p*A->strides[2]) = (float)(ptr[p]) / max_value; + *(float*)(target_pixel + p * pstride) = (float)(ptr[p]) / max_value; } } } @@ -526,12 +530,16 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result, throw Py::MemoryError("Could not allocate image array"); } - for (png_uint_32 y = 0; y < height; y++) + const npy_intp ystride = PyArray_STRIDE(A, 0); + const npy_intp xstride = PyArray_STRIDE(A, 1); + const npy_intp pstride = PyArray_STRIDE(A, 2); + char* target_row = PyArray_BYTES(A); + for (png_uint_32 y = 0; y < height; y++, target_row += ystride) { png_byte* row = row_pointers[y]; - for (png_uint_32 x = 0; x < width; x++) + char* target_pixel = target_row; + for (png_uint_32 x = 0; x < width; x++, target_pixel += xstride) { - size_t offset = y * A->strides[0] + x * A->strides[1]; if (bit_depth == 16) { png_uint_16* ptr = &reinterpret_cast(row)[x * dimensions[2]]; @@ -539,12 +547,12 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result, if (result_bit_depth == 16) { for (png_uint_32 p = 0; p < (png_uint_32)dimensions[2]; p++) { - *(png_uint_16*)(A->data + offset + p*A->strides[2]) = ptr[p]; + *(png_uint_16*)(target_pixel + p*pstride) = ptr[p]; } } else { for (png_uint_32 p = 0; p < (png_uint_32)dimensions[2]; p++) { - *(png_byte*)(A->data + offset + p*A->strides[2]) = ptr[p] >> 8; + *(png_byte*)(target_pixel + p*pstride) = ptr[p] >> 8; } } } @@ -554,12 +562,12 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result, if (result_bit_depth == 16) { for (png_uint_32 p = 0; p < (png_uint_32)dimensions[2]; p++) { - *(png_uint_16*)(A->data + offset + p*A->strides[2]) = ptr[p]; + *(png_uint_16*)(target_pixel + p*pstride) = ptr[p]; } } else { for (png_uint_32 p = 0; p < (png_uint_32)dimensions[2]; p++) { - *(png_byte*)(A->data + offset + p*A->strides[2]) = ptr[p]; + *(png_byte*)(target_pixel + p*pstride) = ptr[p]; } } } diff --git a/src/agg_py_path_iterator.h b/src/agg_py_path_iterator.h index 8449dd2054d8..07aeb4325cc0 100644 --- a/src/agg_py_path_iterator.h +++ b/src/agg_py_path_iterator.h @@ -49,21 +49,21 @@ class PathIterator Py::Object should_simplify_obj = path_obj.getAttr("should_simplify"); Py::Object simplify_threshold_obj = path_obj.getAttr("simplify_threshold"); - PyObject* vertices_arr = PyArray_FromObject(vertices_obj.ptr(), PyArray_DOUBLE, 2, 2); + PyObject* vertices_arr = PyArray_FromObject(vertices_obj.ptr(), NPY_DOUBLE, 2, 2); if (!vertices_arr) { throw Py::ValueError("Invalid vertices array."); } m_vertices = Py::Object(vertices_arr, true); - if (PyArray_DIM(m_vertices.ptr(), 1) != 2) + if (PyArray_DIM((PyArrayObject*)m_vertices.ptr(), 1) != 2) { throw Py::ValueError("Invalid vertices array."); } if (codes_obj.ptr() != Py_None) { - PyObject* codes_arr = PyArray_FromObject(codes_obj.ptr(), PyArray_UINT8, 1, 1); + PyObject* codes_arr = PyArray_FromObject(codes_obj.ptr(), NPY_UINT8, 1, 1); if (!codes_arr) { @@ -71,14 +71,15 @@ class PathIterator } m_codes = Py::Object(codes_arr, true); - if (PyArray_DIM(m_codes.ptr(), 0) != PyArray_DIM(m_vertices.ptr(), 0)) + if (PyArray_DIM((PyArrayObject*)m_codes.ptr(), 0) != + PyArray_DIM((PyArrayObject*)m_vertices.ptr(), 0)) { throw Py::ValueError("Codes array is wrong length"); } } m_should_simplify = should_simplify_obj.isTrue(); - m_total_vertices = PyArray_DIM(m_vertices.ptr(), 0); + m_total_vertices = PyArray_DIM((PyArrayObject*)m_vertices.ptr(), 0); m_simplify_threshold = Py::Float(simplify_threshold_obj); } @@ -93,13 +94,13 @@ class PathIterator const size_t idx = m_iterator++; - char* pair = (char*)PyArray_GETPTR2(m_vertices.ptr(), idx, 0); + char* pair = (char*)PyArray_GETPTR2((PyArrayObject*)m_vertices.ptr(), idx, 0); *x = *(double*)pair; - *y = *(double*)(pair + PyArray_STRIDE(m_vertices.ptr(), 1)); + *y = *(double*)(pair + PyArray_STRIDE((PyArrayObject*)m_vertices.ptr(), 1)); if (!m_codes.isNone()) { - return (unsigned)(*(char *)PyArray_GETPTR1(m_codes.ptr(), idx)); + return (unsigned)(*(char *)PyArray_GETPTR1((PyArrayObject*)m_codes.ptr(), idx)); } else { diff --git a/src/agg_py_transforms.cpp b/src/agg_py_transforms.cpp index 7e85ec1b7608..c9a1b501a7df 100644 --- a/src/agg_py_transforms.cpp +++ b/src/agg_py_transforms.cpp @@ -30,7 +30,7 @@ py_to_agg_transformation_matrix(PyObject* obj, bool errors = true) /** Try turning the object into an affine transform matrix. */ try { - matrix = (PyArrayObject*) PyArray_FromObject(obj, PyArray_DOUBLE, 2, 2); + matrix = (PyArrayObject*) PyArray_FromObject(obj, NPY_DOUBLE, 2, 2); if (!matrix) { PyErr_Clear(); throw std::exception(); @@ -99,7 +99,7 @@ py_convert_bbox(PyObject* bbox_obj, double& l, double& b, double& r, double& t) try { - bbox = (PyArrayObject*) PyArray_FromObject(bbox_obj, PyArray_DOUBLE, 2, 2); + bbox = (PyArrayObject*) PyArray_FromObject(bbox_obj, NPY_DOUBLE, 2, 2); if (!bbox || PyArray_NDIM(bbox) != 2 || PyArray_DIM(bbox, 0) != 2 || PyArray_DIM(bbox, 1) != 2) { diff --git a/src/cntr.c b/src/cntr.c index 4448714b6a59..3b6228f94152 100644 --- a/src/cntr.c +++ b/src/cntr.c @@ -1509,14 +1509,14 @@ build_cntr_list_v2(long *np, double *xp, double *yp, short *kp, dims[0] = np[i]; dims[1] = 2; kdims[0] = np[i]; - xyv = (PyArrayObject *) PyArray_SimpleNew(2, dims, PyArray_DOUBLE); + xyv = (PyArrayObject *) PyArray_SimpleNew(2, dims, NPY_DOUBLE); if (xyv == NULL) goto error; - kv = (PyArrayObject *) PyArray_SimpleNew(1, kdims, PyArray_UBYTE); + kv = (PyArrayObject *) PyArray_SimpleNew(1, kdims, NPY_UBYTE); if (kv == NULL) goto error; n = reorder(xpp, ypp, kpp, - (double *) xyv->data, - (unsigned char *) kv->data, + (double *) PyArray_DATA(xyv), + (unsigned char *) PyArray_DATA(kv), np[i]); if (n == -1) goto error; newshape.len = 2; @@ -1569,9 +1569,9 @@ __build_cntr_list_v2(long *np, double *xp, double *yp, short *kp, dims[0] = np[i]; dims[1] = 2; kdims[0] = np[i]; - xyv = (PyArrayObject *) PyArray_SimpleNew(2, dims, PyArray_DOUBLE); + xyv = (PyArrayObject *) PyArray_SimpleNew(2, dims, NPY_DOUBLE); if (xyv == NULL) goto error; - kv = (PyArrayObject *) PyArray_SimpleNew(1, kdims, PyArray_SHORT); + kv = (PyArrayObject *) PyArray_SimpleNew(1, kdims, NPY_SHORT); if (kv == NULL) goto error; for (j = 0; j < dims[0]; j++) @@ -1809,14 +1809,14 @@ Cntr_init(Cntr *self, PyObject *args, PyObject *kwds) } xpa = (PyArrayObject *) PyArray_ContiguousFromObject(xarg, - PyArray_DOUBLE, 2, 2); + NPY_DOUBLE, 2, 2); ypa = (PyArrayObject *) PyArray_ContiguousFromObject(yarg, - PyArray_DOUBLE, 2, 2); + NPY_DOUBLE, 2, 2); zpa = (PyArrayObject *) PyArray_ContiguousFromObject(zarg, - PyArray_DOUBLE, 2, 2); + NPY_DOUBLE, 2, 2); if (marg) mpa = (PyArrayObject *) PyArray_ContiguousFromObject(marg, - PyArray_BYTE, 2, 2); + NPY_BYTE, 2, 2); else mpa = NULL; @@ -1827,22 +1827,22 @@ Cntr_init(Cntr *self, PyObject *args, PyObject *kwds) "x, y, z must be castable to double."); goto error; } - iMax = zpa->dimensions[1]; - jMax = zpa->dimensions[0]; - if (xpa->dimensions[0] != jMax || xpa->dimensions[1] != iMax || - ypa->dimensions[0] != jMax || ypa->dimensions[1] != iMax || - (mpa && (mpa->dimensions[0] != jMax || mpa->dimensions[1] != iMax))) + iMax = PyArray_DIM(zpa, 1); + jMax = PyArray_DIM(zpa, 0); + if (PyArray_DIM(xpa, 0) != jMax || PyArray_DIM(xpa, 1) != iMax || + PyArray_DIM(ypa, 0) != jMax || PyArray_DIM(ypa, 1) != iMax || + (mpa && (PyArray_DIM(mpa, 0) != jMax || PyArray_DIM(mpa, 1) != iMax))) { PyErr_SetString(PyExc_ValueError, "Arguments x, y, z, mask (if present)" " must have the same dimensions."); goto error; } - if (mpa) mask = mpa->data; + if (mpa) mask = PyArray_DATA(mpa); else mask = NULL; - if ( cntr_init(self->site, iMax, jMax, (double *)xpa->data, - (double *)ypa->data, - (double *)zpa->data, mask)) + if ( cntr_init(self->site, iMax, jMax, (double *)PyArray_DATA(xpa), + (double *)PyArray_DATA(ypa), + (double *)PyArray_DATA(zpa), mask)) { PyErr_SetString(PyExc_MemoryError, "Memory allocation failure in cntr_init"); @@ -1890,14 +1890,16 @@ Cntr_get_cdata(Cntr *self) npy_intp dims[2]; int i, j; int ni, nj; + char *data; dims[0] = ni = self->site->imax; dims[1] = nj = self->site->jmax; - Cdata = (PyArrayObject *) PyArray_SimpleNew(2, dims, PyArray_SHORT); + Cdata = (PyArrayObject *) PyArray_SimpleNew(2, dims, NPY_SHORT); + data = PyArray_DATA(Cdata); for (j=0; jdata[j + i*nj] = self->site->data[i + j*ni]; + data[j + i*nj] = self->site->data[i + j*ni]; /* output is C-order, input is F-order */ /* for now we are ignoring the last ni+1 values */ return (PyObject *)Cdata; diff --git a/src/ft2font.cpp b/src/ft2font.cpp index 7f33d6798f26..aa5ba19038dc 100644 --- a/src/ft2font.cpp +++ b/src/ft2font.cpp @@ -397,7 +397,7 @@ FT2Image::py_as_array(const Py::Tuple & args) dimensions[1] = get_width(); //numcols - PyArrayObject *A = (PyArrayObject *) PyArray_SimpleNewFromData(2, dimensions, PyArray_UBYTE, _buffer); + PyArrayObject *A = (PyArrayObject *) PyArray_SimpleNewFromData(2, dimensions, NPY_UBYTE, _buffer); return Py::asObject((PyObject*)A); } @@ -636,13 +636,13 @@ FT2Font::get_path() npy_intp vertices_dims[2] = {count, 2}; vertices = (PyArrayObject*)PyArray_SimpleNew( - 2, vertices_dims, PyArray_DOUBLE); + 2, vertices_dims, NPY_DOUBLE); if (vertices == NULL) { throw; } npy_intp codes_dims[1] = {count}; codes = (PyArrayObject*)PyArray_SimpleNew( - 1, codes_dims, PyArray_UINT8); + 1, codes_dims, NPY_UINT8); if (codes == NULL) { throw; } diff --git a/src/qhull_wrap.c b/src/qhull_wrap.c index a295bc3148d3..3a24a7514551 100644 --- a/src/qhull_wrap.c +++ b/src/qhull_wrap.c @@ -175,14 +175,14 @@ delaunay_impl(int npoints, const double* x, const double* y, /* Allocate python arrays to return. */ dims[0] = ntri; dims[1] = 3; - triangles = (PyArrayObject*)PyArray_SimpleNew(ndim, dims, PyArray_INT); + triangles = (PyArrayObject*)PyArray_SimpleNew(ndim, dims, NPY_INT); if (triangles == NULL) { PyErr_SetString(PyExc_MemoryError, "Could not allocate triangles array in qhull.delaunay"); goto error; } - neighbors = (PyArrayObject*)PyArray_SimpleNew(ndim, dims, PyArray_INT); + neighbors = (PyArrayObject*)PyArray_SimpleNew(ndim, dims, NPY_INT); if (neighbors == NULL) { PyErr_SetString(PyExc_MemoryError, "Could not allocate neighbors array in qhull.delaunay"); @@ -267,9 +267,9 @@ delaunay(PyObject *self, PyObject *args) return NULL; } - xarray = (PyArrayObject*)PyArray_ContiguousFromObject(xarg, PyArray_DOUBLE, + xarray = (PyArrayObject*)PyArray_ContiguousFromObject(xarg, NPY_DOUBLE, 1, 1); - yarray = (PyArrayObject*)PyArray_ContiguousFromObject(yarg, PyArray_DOUBLE, + yarray = (PyArrayObject*)PyArray_ContiguousFromObject(yarg, NPY_DOUBLE, 1, 1); if (xarray == 0 || yarray == 0 || PyArray_DIM(xarray,0) != PyArray_DIM(yarray, 0)) {