@@ -158,45 +158,36 @@ int convert_rect(PyObject *rectobj, void *rectp)
158158 rect->x2 = 0.0 ;
159159 rect->y2 = 0.0 ;
160160 } else {
161- try
162- {
163- numpy::array_view<const double , 2 > rect_arr (rectobj);
161+ PyArrayObject *rect_arr = (PyArrayObject *)PyArray_ContiguousFromAny (
162+ rectobj, NPY_DOUBLE, 1 , 2 );
163+ if (rect_arr == NULL ) {
164+ return 0 ;
165+ }
164166
165- if (rect_arr.dim (0 ) != 2 || rect_arr.dim (1 ) != 2 ) {
167+ if (PyArray_NDIM (rect_arr) == 2 ) {
168+ if (PyArray_DIM (rect_arr, 0 ) != 2 ||
169+ PyArray_DIM (rect_arr, 1 ) != 2 ) {
166170 PyErr_SetString (PyExc_ValueError, " Invalid bounding box" );
171+ Py_DECREF (rect_arr);
167172 return 0 ;
168173 }
169174
170- rect->x1 = rect_arr (0 , 0 );
171- rect->y1 = rect_arr (0 , 1 );
172- rect->x2 = rect_arr (1 , 0 );
173- rect->y2 = rect_arr (1 , 1 );
174- }
175- catch (py::exception &)
176- {
177- PyErr_Clear ();
178-
179- try
180- {
181- numpy::array_view<const double , 1 > rect_arr (rectobj);
182-
183- if (rect_arr.dim (0 ) != 4 ) {
184- PyErr_SetString (PyExc_ValueError, " Invalid bounding box" );
185- return 0 ;
186- }
187-
188- rect->x1 = rect_arr (0 );
189- rect->y1 = rect_arr (1 );
190- rect->x2 = rect_arr (2 );
191- rect->y2 = rect_arr (3 );
192- }
193- catch (py::exception &)
194- {
175+ } else { // PyArray_NDIM(rect_arr) == 1
176+ if (PyArray_DIM (rect_arr, 0 ) != 4 ) {
177+ PyErr_SetString (PyExc_ValueError, " Invalid bounding box" );
178+ Py_DECREF (rect_arr);
195179 return 0 ;
196180 }
197181 }
198- }
199182
183+ double *buff = (double *)PyArray_DATA (rect_arr);
184+ rect->x1 = buff[0 ];
185+ rect->y1 = buff[1 ];
186+ rect->x2 = buff[2 ];
187+ rect->y2 = buff[3 ];
188+
189+ Py_DECREF (rect_arr);
190+ }
200191 return 1 ;
201192}
202193
@@ -336,27 +327,26 @@ int convert_trans_affine(PyObject *obj, void *transp)
336327 return 1 ;
337328 }
338329
339- try
340- {
341- numpy::array_view<const double , 2 > matrix (obj);
330+ PyArrayObject *array = (PyArrayObject *)PyArray_ContiguousFromAny (obj, NPY_DOUBLE, 2 , 2 );
331+ if (array == NULL ) {
332+ return 0 ;
333+ }
342334
343- if (matrix.dim (0 ) == 3 && matrix.dim (1 ) == 3 ) {
344- trans->sx = matrix (0 , 0 );
345- trans->shx = matrix (0 , 1 );
346- trans->tx = matrix (0 , 2 );
335+ if (PyArray_DIM (array, 0 ) == 3 && PyArray_DIM (array, 1 ) == 3 ) {
336+ double *buffer = (double *)PyArray_DATA (array);
337+ trans->sx = buffer[0 ];
338+ trans->shx = buffer[1 ];
339+ trans->tx = buffer[2 ];
347340
348- trans->shy = matrix ( 1 , 0 ) ;
349- trans->sy = matrix ( 1 , 1 ) ;
350- trans->ty = matrix ( 1 , 2 ) ;
341+ trans->shy = buffer[ 3 ] ;
342+ trans->sy = buffer[ 4 ] ;
343+ trans->ty = buffer[ 5 ] ;
351344
352- return 1 ;
353- }
354- }
355- catch (py::exception &)
356- {
357- return 0 ;
345+ Py_DECREF (array);
346+ return 1 ;
358347 }
359348
349+ Py_DECREF (array);
360350 PyErr_SetString (PyExc_ValueError, " Invalid affine transformation matrix" );
361351 return 0 ;
362352}
0 commit comments