@@ -243,13 +243,127 @@ STATIC mp_obj_t bitmaptools_obj_rotozoom(size_t n_args, const mp_obj_t *pos_args
243
243
MP_DEFINE_CONST_FUN_OBJ_KW (bitmaptools_rotozoom_obj , 0 , bitmaptools_obj_rotozoom );
244
244
// requires at least 2 arguments (destination bitmap and source bitmap)
245
245
246
+ //|
247
+ //| def fill_region(
248
+ //| dest_bitmap: displayio.Bitmap,
249
+ //| x1: int, y1: int,
250
+ //| x2: int, y2: int,
251
+ //| value: int) -> None:
252
+ //| """Draws the color value into the destination bitmap within the
253
+ //| rectangular region bounded by (x1,y1) and (x2,y2), exclusive.
254
+ //|
255
+ //| :param bitmap dest_bitmap: Destination bitmap that will be written into
256
+ //| :param int x1: x-pixel position of the first corner of the rectangular fill region
257
+ //| :param int y1: y-pixel position of the first corner of the rectangular fill region
258
+ //| :param int x2: x-pixel position of the second corner of the rectangular fill region (exclusive)
259
+ //| :param int y2: y-pixel position of the second corner of the rectangular fill region (exclusive)
260
+ //| :param int value: Bitmap palette index that will be written into the rectangular
261
+ //| fill region in the destination bitmap"""
262
+ //| ...
263
+ //|
264
+ STATIC mp_obj_t bitmaptools_obj_fill_region (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ){
265
+ enum {ARG_dest_bitmap , ARG_x1 , ARG_y1 , ARG_x2 , ARG_y2 , ARG_value };
266
+
267
+ static const mp_arg_t allowed_args [] = {
268
+ {MP_QSTR_dest_bitmap , MP_ARG_REQUIRED | MP_ARG_OBJ },
269
+ {MP_QSTR_x1 , MP_ARG_REQUIRED | MP_ARG_INT },
270
+ {MP_QSTR_y1 , MP_ARG_REQUIRED | MP_ARG_INT },
271
+ {MP_QSTR_x2 , MP_ARG_REQUIRED | MP_ARG_INT },
272
+ {MP_QSTR_y2 , MP_ARG_REQUIRED | MP_ARG_INT },
273
+ {MP_QSTR_value , MP_ARG_REQUIRED | MP_ARG_INT },
274
+ };
275
+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
276
+ mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
277
+
278
+ displayio_bitmap_t * destination = MP_OBJ_TO_PTR (args [ARG_dest_bitmap ].u_obj ); // the destination bitmap
279
+
280
+ uint32_t value , color_depth ;
281
+ value = args [ARG_value ].u_int ;
282
+ color_depth = (1 << destination -> bits_per_value );
283
+ if (color_depth <= value ) {
284
+ mp_raise_ValueError (translate ("out of range of target" ));
285
+ }
286
+
287
+ int16_t x1 = args [ARG_x1 ].u_int ;
288
+ int16_t y1 = args [ARG_y1 ].u_int ;
289
+ int16_t x2 = args [ARG_x2 ].u_int ;
290
+ int16_t y2 = args [ARG_y2 ].u_int ;
291
+
292
+ common_hal_bitmaptools_fill_region (destination , x1 , y1 , x2 , y2 , value );
293
+
294
+ return mp_const_none ;
295
+ }
296
+
297
+ MP_DEFINE_CONST_FUN_OBJ_KW (bitmaptools_fill_region_obj , 0 , bitmaptools_obj_fill_region );
298
+ // requires all 6 arguments
299
+
300
+ //|
301
+ //| def draw_line(
302
+ //| dest_bitmap: displayio.Bitmap,
303
+ //| x1: int, y1: int,
304
+ //| x2: int, y2: int,
305
+ //| value: int) -> None:
306
+ //| """Draws a line into a bitmap specified two endpoints (x1,y1) and (x2,y2).
307
+ //|
308
+ //| :param bitmap dest_bitmap: Destination bitmap that will be written into
309
+ //| :param int x1: x-pixel position of the line's first endpoint
310
+ //| :param int y1: y-pixel position of the line's first endpoint
311
+ //| :param int x2: x-pixel position of the line's second endpoint
312
+ //| :param int y2: y-pixel position of the line's second endpoint
313
+ //| :param int value: Bitmap palette index that will be written into the
314
+ //| line in the destination bitmap"""
315
+ //| ...
316
+ //|
317
+ STATIC mp_obj_t bitmaptools_obj_draw_line (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ){
318
+ enum {ARG_dest_bitmap , ARG_x1 , ARG_y1 , ARG_x2 , ARG_y2 , ARG_value };
319
+
320
+ static const mp_arg_t allowed_args [] = {
321
+ {MP_QSTR_dest_bitmap , MP_ARG_REQUIRED | MP_ARG_OBJ },
322
+ {MP_QSTR_x1 , MP_ARG_REQUIRED | MP_ARG_INT },
323
+ {MP_QSTR_y1 , MP_ARG_REQUIRED | MP_ARG_INT },
324
+ {MP_QSTR_x2 , MP_ARG_REQUIRED | MP_ARG_INT },
325
+ {MP_QSTR_y2 , MP_ARG_REQUIRED | MP_ARG_INT },
326
+ {MP_QSTR_value , MP_ARG_REQUIRED | MP_ARG_INT },
327
+ };
328
+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
329
+ mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
330
+
331
+ displayio_bitmap_t * destination = MP_OBJ_TO_PTR (args [ARG_dest_bitmap ].u_obj ); // the destination bitmap
332
+
333
+ uint32_t value , color_depth ;
334
+ value = args [ARG_value ].u_int ;
335
+ color_depth = (1 << destination -> bits_per_value );
336
+ if (color_depth <= value ) {
337
+ mp_raise_ValueError (translate ("out of range of target" ));
338
+ }
339
+
340
+ int16_t x1 = args [ARG_x1 ].u_int ;
341
+ int16_t y1 = args [ARG_y1 ].u_int ;
342
+ int16_t x2 = args [ARG_x2 ].u_int ;
343
+ int16_t y2 = args [ARG_y2 ].u_int ;
344
+
345
+ // verify points are within the bitmap boundary (inclusive)
346
+ if ( (x1 < 0 ) || (x2 < 0 ) || (y1 < 0 ) || (y2 < 0 ) ||
347
+ (x1 >= destination -> width ) || (x2 >= destination -> width ) ||
348
+ (y1 >= destination -> height ) || (y2 >= destination -> height ) ) {
349
+ mp_raise_ValueError (translate ("out of range of target" ));
350
+ }
351
+
352
+ common_hal_bitmaptools_draw_line (destination , x1 , y1 , x2 , y2 , value );
353
+
354
+ return mp_const_none ;
355
+ }
356
+
357
+ MP_DEFINE_CONST_FUN_OBJ_KW (bitmaptools_draw_line_obj , 0 , bitmaptools_obj_draw_line );
358
+ // requires all 6 arguments
246
359
247
360
STATIC const mp_rom_map_elem_t bitmaptools_module_globals_table [] = {
248
361
{ MP_ROM_QSTR (MP_QSTR_rotozoom ), MP_ROM_PTR (& bitmaptools_rotozoom_obj ) },
362
+ { MP_ROM_QSTR (MP_QSTR_fill_region ), MP_ROM_PTR (& bitmaptools_fill_region_obj ) },
363
+ { MP_ROM_QSTR (MP_QSTR_draw_line ), MP_ROM_PTR (& bitmaptools_draw_line_obj ) },
249
364
};
250
365
STATIC MP_DEFINE_CONST_DICT (bitmaptools_module_globals , bitmaptools_module_globals_table );
251
366
252
-
253
367
const mp_obj_module_t bitmaptools_module = {
254
368
.base = {& mp_type_module },
255
369
.globals = (mp_obj_dict_t * )& bitmaptools_module_globals ,
0 commit comments