@@ -158,45 +158,36 @@ int convert_rect(PyObject *rectobj, void *rectp)
158
158
rect->x2 = 0.0 ;
159
159
rect->y2 = 0.0 ;
160
160
} 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
+ }
164
166
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 ) {
166
170
PyErr_SetString (PyExc_ValueError, " Invalid bounding box" );
171
+ Py_DECREF (rect_arr);
167
172
return 0 ;
168
173
}
169
174
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);
195
179
return 0 ;
196
180
}
197
181
}
198
- }
199
182
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
+ }
200
191
return 1 ;
201
192
}
202
193
0 commit comments