@@ -201,6 +201,61 @@ Size mp_obj_to_size(mp_obj_t obj)
201201 return size;
202202}
203203
204+ Size2f mp_obj_to_size2f (mp_obj_t obj)
205+ {
206+ // Check for None object
207+ if (obj == mp_const_none)
208+ {
209+ // Create an empty Size2f object
210+ return Size2f ();
211+ }
212+
213+ // Assume the object is a ndarray, or can be converted to one. Will raise an
214+ // exception if not
215+ ndarray_obj_t *ndarray = ndarray_from_mp_obj (obj, 0 );
216+
217+ // Validate the length of the ndarray
218+ if (ndarray->len != 2 )
219+ {
220+ mp_raise_TypeError (MP_ERROR_TEXT (" Size2f must be length 2" ));
221+ }
222+
223+ // Compute the size, checking the type of the ndarray
224+ Size2f size;
225+ switch (ndarray->dtype )
226+ {
227+ case NDARRAY_UINT8:
228+ size.width = ((uint8_t *) ndarray->array )[0 ];
229+ size.height = ((uint8_t *) ndarray->array )[1 ];
230+ break ;
231+ case NDARRAY_INT8:
232+ size.width = ((int8_t *) ndarray->array )[0 ];
233+ size.height = ((int8_t *) ndarray->array )[1 ];
234+ break ;
235+ case NDARRAY_UINT16:
236+ size.width = ((uint16_t *) ndarray->array )[0 ];
237+ size.height = ((uint16_t *) ndarray->array )[1 ];
238+ break ;
239+ case NDARRAY_INT16:
240+ size.width = ((int16_t *) ndarray->array )[0 ];
241+ size.height = ((int16_t *) ndarray->array )[1 ];
242+ break ;
243+ case NDARRAY_FLOAT:
244+ size.width = ((float *) ndarray->array )[0 ];
245+ size.height = ((float *) ndarray->array )[1 ];
246+ break ;
247+ case NDARRAY_BOOL:
248+ size.width = ((bool *) ndarray->array )[0 ];
249+ size.height = ((bool *) ndarray->array )[1 ];
250+ break ;
251+ default :
252+ mp_raise_TypeError (MP_ERROR_TEXT (" Unsupported ndarray type" ));
253+ break ;
254+ }
255+
256+ return size;
257+ }
258+
204259Point mp_obj_to_point (mp_obj_t obj)
205260{
206261 // Check for None object
@@ -256,6 +311,61 @@ Point mp_obj_to_point(mp_obj_t obj)
256311 return point;
257312}
258313
314+ Point2f mp_obj_to_point2f (mp_obj_t obj)
315+ {
316+ // Check for None object
317+ if (obj == mp_const_none)
318+ {
319+ // Create an empty Point2f object
320+ return Point2f ();
321+ }
322+
323+ // Assume the object is a ndarray, or can be converted to one. Will raise an
324+ // exception if not
325+ ndarray_obj_t *ndarray = ndarray_from_mp_obj (obj, 0 );
326+
327+ // Validate the length of the ndarray
328+ if (ndarray->len != 2 )
329+ {
330+ mp_raise_TypeError (MP_ERROR_TEXT (" Point2f must be length 2" ));
331+ }
332+
333+ // Compute the point, checking the type of the ndarray
334+ Point2f point;
335+ switch (ndarray->dtype )
336+ {
337+ case NDARRAY_UINT8:
338+ point.x = ((uint8_t *) ndarray->array )[0 ];
339+ point.y = ((uint8_t *) ndarray->array )[1 ];
340+ break ;
341+ case NDARRAY_INT8:
342+ point.x = ((int8_t *) ndarray->array )[0 ];
343+ point.y = ((int8_t *) ndarray->array )[1 ];
344+ break ;
345+ case NDARRAY_UINT16:
346+ point.x = ((uint16_t *) ndarray->array )[0 ];
347+ point.y = ((uint16_t *) ndarray->array )[1 ];
348+ break ;
349+ case NDARRAY_INT16:
350+ point.x = ((int16_t *) ndarray->array )[0 ];
351+ point.y = ((int16_t *) ndarray->array )[1 ];
352+ break ;
353+ case NDARRAY_FLOAT:
354+ point.x = ((float *) ndarray->array )[0 ];
355+ point.y = ((float *) ndarray->array )[1 ];
356+ break ;
357+ case NDARRAY_BOOL:
358+ point.x = ((bool *) ndarray->array )[0 ];
359+ point.y = ((bool *) ndarray->array )[1 ];
360+ break ;
361+ default :
362+ mp_raise_TypeError (MP_ERROR_TEXT (" Unsupported ndarray type" ));
363+ break ;
364+ }
365+
366+ return point;
367+ }
368+
259369Scalar mp_obj_to_scalar (mp_obj_t obj)
260370{
261371 // Check for None object
0 commit comments