@@ -167,39 +167,148 @@ Size mp_obj_to_size(mp_obj_t obj)
167
167
mp_raise_TypeError (MP_ERROR_TEXT (" Size must be length 2" ));
168
168
}
169
169
170
- // Check the type of the ndarray
171
- if (ndarray->dtype == NDARRAY_UINT8)
170
+ // Compute the size, checking the type of the ndarray
171
+ Size size;
172
+ switch (ndarray->dtype )
172
173
{
173
- uint8_t *data = (uint8_t *)ndarray->array ;
174
- return Size (data[0 ], data[1 ]);
174
+ case NDARRAY_UINT8:
175
+ size.width = ((uint8_t *) ndarray->array )[0 ];
176
+ size.height = ((uint8_t *) ndarray->array )[1 ];
177
+ break ;
178
+ case NDARRAY_INT8:
179
+ size.width = ((int8_t *) ndarray->array )[0 ];
180
+ size.height = ((int8_t *) ndarray->array )[1 ];
181
+ break ;
182
+ case NDARRAY_UINT16:
183
+ size.width = ((uint16_t *) ndarray->array )[0 ];
184
+ size.height = ((uint16_t *) ndarray->array )[1 ];
185
+ break ;
186
+ case NDARRAY_INT16:
187
+ size.width = ((int16_t *) ndarray->array )[0 ];
188
+ size.height = ((int16_t *) ndarray->array )[1 ];
189
+ break ;
190
+ case NDARRAY_FLOAT:
191
+ size.width = ((float *) ndarray->array )[0 ];
192
+ size.height = ((float *) ndarray->array )[1 ];
193
+ break ;
194
+ case NDARRAY_BOOL:
195
+ size.width = ((bool *) ndarray->array )[0 ];
196
+ size.height = ((bool *) ndarray->array )[1 ];
197
+ break ;
198
+ default :
199
+ mp_raise_TypeError (MP_ERROR_TEXT (" Unsupported ndarray type" ));
200
+ break ;
175
201
}
176
- else if (ndarray->dtype == NDARRAY_INT8)
202
+
203
+ return size;
204
+ }
205
+
206
+ Point mp_obj_to_point (mp_obj_t obj)
207
+ {
208
+ // Check for None object
209
+ if (obj == mp_const_none)
177
210
{
178
- int8_t *data = ( int8_t *)ndarray-> array ;
179
- return Size (data[ 0 ], data[ 1 ] );
211
+ // Create an empty Point object
212
+ return Point ( );
180
213
}
181
- else if (ndarray->dtype == NDARRAY_UINT16)
214
+
215
+ // Assume the object is a ndarray, or can be converted to one. Will raise an
216
+ // exception if not
217
+ ndarray_obj_t *ndarray = ndarray_from_mp_obj (obj, 0 );
218
+
219
+ // Validate the length of the ndarray
220
+ if (ndarray->len != 2 )
182
221
{
183
- uint16_t *data = (uint16_t *)ndarray->array ;
184
- return Size (data[0 ], data[1 ]);
222
+ mp_raise_TypeError (MP_ERROR_TEXT (" Point must be length 2" ));
185
223
}
186
- else if (ndarray->dtype == NDARRAY_INT16)
224
+
225
+ // Compute the point, checking the type of the ndarray
226
+ Point point;
227
+ switch (ndarray->dtype )
187
228
{
188
- int16_t *data = (int16_t *)ndarray->array ;
189
- return Size (data[0 ], data[1 ]);
229
+ case NDARRAY_UINT8:
230
+ point.x = ((uint8_t *) ndarray->array )[0 ];
231
+ point.y = ((uint8_t *) ndarray->array )[1 ];
232
+ break ;
233
+ case NDARRAY_INT8:
234
+ point.x = ((int8_t *) ndarray->array )[0 ];
235
+ point.y = ((int8_t *) ndarray->array )[1 ];
236
+ break ;
237
+ case NDARRAY_UINT16:
238
+ point.x = ((uint16_t *) ndarray->array )[0 ];
239
+ point.y = ((uint16_t *) ndarray->array )[1 ];
240
+ break ;
241
+ case NDARRAY_INT16:
242
+ point.x = ((int16_t *) ndarray->array )[0 ];
243
+ point.y = ((int16_t *) ndarray->array )[1 ];
244
+ break ;
245
+ case NDARRAY_FLOAT:
246
+ point.x = ((float *) ndarray->array )[0 ];
247
+ point.y = ((float *) ndarray->array )[1 ];
248
+ break ;
249
+ case NDARRAY_BOOL:
250
+ point.x = ((bool *) ndarray->array )[0 ];
251
+ point.y = ((bool *) ndarray->array )[1 ];
252
+ break ;
253
+ default :
254
+ mp_raise_TypeError (MP_ERROR_TEXT (" Unsupported ndarray type" ));
255
+ break ;
190
256
}
191
- else if (ndarray->dtype == NDARRAY_FLOAT)
257
+
258
+ return point;
259
+ }
260
+
261
+ Scalar mp_obj_to_scalar (mp_obj_t obj)
262
+ {
263
+ // Check for None object
264
+ if (obj == mp_const_none)
192
265
{
193
- float *data = ( float *)ndarray-> array ;
194
- return Size (data[ 0 ], data[ 1 ] );
266
+ // Create an empty Scalar object
267
+ return Scalar ( );
195
268
}
196
- else if (ndarray->dtype == NDARRAY_BOOL)
269
+
270
+ // Assume the object is a ndarray, or can be converted to one. Will raise an
271
+ // exception if not
272
+ ndarray_obj_t *ndarray = ndarray_from_mp_obj (obj, 0 );
273
+
274
+ // Validate the length of the ndarray
275
+ if (ndarray->len > 4 )
197
276
{
198
- bool *data = (bool *)ndarray->array ;
199
- return Size (data[0 ], data[1 ]);
277
+ mp_raise_TypeError (MP_ERROR_TEXT (" Scalar must be length 4 or less" ));
200
278
}
201
- else
279
+
280
+ // Compute the scalar, checking the type of the ndarray
281
+ Scalar scalar;
282
+ switch (ndarray->dtype )
202
283
{
203
- mp_raise_TypeError (MP_ERROR_TEXT (" Unsupported ndarray type" ));
284
+ case NDARRAY_UINT8:
285
+ for (size_t i = 0 ; i < ndarray->len ; i++)
286
+ scalar[i] = ((uint8_t *) ndarray->array )[i];
287
+ break ;
288
+ case NDARRAY_INT8:
289
+ for (size_t i = 0 ; i < ndarray->len ; i++)
290
+ scalar[i] = ((int8_t *) ndarray->array )[i];
291
+ break ;
292
+ case NDARRAY_UINT16:
293
+ for (size_t i = 0 ; i < ndarray->len ; i++)
294
+ scalar[i] = ((uint16_t *) ndarray->array )[i];
295
+ break ;
296
+ case NDARRAY_INT16:
297
+ for (size_t i = 0 ; i < ndarray->len ; i++)
298
+ scalar[i] = ((int16_t *) ndarray->array )[i];
299
+ break ;
300
+ case NDARRAY_FLOAT:
301
+ for (size_t i = 0 ; i < ndarray->len ; i++)
302
+ scalar[i] = ((float *) ndarray->array )[i];
303
+ break ;
304
+ case NDARRAY_BOOL:
305
+ for (size_t i = 0 ; i < ndarray->len ; i++)
306
+ scalar[i] = ((bool *) ndarray->array )[i];
307
+ break ;
308
+ default :
309
+ mp_raise_TypeError (MP_ERROR_TEXT (" Unsupported ndarray type" ));
310
+ break ;
204
311
}
312
+
313
+ return scalar;
205
314
}
0 commit comments