@@ -243,13 +243,161 @@ 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
259
+ //| :param int y2: y-pixel position of the second corner of the rectangular fill region
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
+ // Ensure x1 < x2 and y1 < y2
293
+ if (x1 > x2 ) {
294
+ int16_t temp = x2 ;
295
+ x2 = x1 ;
296
+ x1 = temp ;
297
+ }
298
+ if (y1 > y2 ) {
299
+ int16_t temp = y2 ;
300
+ y2 = y1 ;
301
+ y1 = temp ;
302
+ }
303
+
304
+ // constrain to bitmap dimensions
305
+ if (x1 < 0 ) {
306
+ x1 = 0 ;
307
+ } else if (x1 > destination -> width ) {
308
+ x1 = destination -> width ;
309
+ }
310
+ if (x2 < 0 ) {
311
+ x2 = 0 ;
312
+ } else if (x2 > destination -> width ) {
313
+ x2 = destination -> width ;
314
+ }
315
+ if (y1 < 0 ) {
316
+ y1 = 0 ;
317
+ } else if (y1 > destination -> height ) {
318
+ y1 = destination -> height ;
319
+ }
320
+ if (y2 < 0 ) {
321
+ y2 = 0 ;
322
+ } else if (y2 > destination -> height ) {
323
+ y2 = destination -> height ;
324
+ }
325
+
326
+ common_hal_bitmaptools_fill_region (destination , x1 , y1 , x2 , y2 , value );
327
+
328
+ return mp_const_none ;
329
+ }
330
+
331
+ MP_DEFINE_CONST_FUN_OBJ_KW (bitmaptools_fill_region_obj , 0 , bitmaptools_obj_fill_region );
332
+ // requires all 6 arguments
333
+
334
+ //|
335
+ //| def draw_line(
336
+ //| dest_bitmap: displayio.Bitmap,
337
+ //| x1: int, y1: int,
338
+ //| x2: int, y2: int,
339
+ //| value: int) -> None:
340
+ //| """Draws a line into a bitmap specified two endpoints (x1,y1) and (x2,y2).
341
+ //|
342
+ //| :param bitmap dest_bitmap: Destination bitmap that will be written into
343
+ //| :param int x1: x-pixel position of the line's first endpoint
344
+ //| :param int y1: y-pixel position of the line's first endpoint
345
+ //| :param int x2: x-pixel position of the line's second endpoint
346
+ //| :param int y2: y-pixel position of the line's second endpoint
347
+ //| :param int value: Bitmap palette index that will be written into the
348
+ //| line in the destination bitmap"""
349
+ //| ...
350
+ //|
351
+ STATIC mp_obj_t bitmaptools_obj_draw_line (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ){
352
+ enum {ARG_dest_bitmap , ARG_x1 , ARG_y1 , ARG_x2 , ARG_y2 , ARG_value };
353
+
354
+ static const mp_arg_t allowed_args [] = {
355
+ {MP_QSTR_dest_bitmap , MP_ARG_REQUIRED | MP_ARG_OBJ },
356
+ {MP_QSTR_x1 , MP_ARG_REQUIRED | MP_ARG_INT },
357
+ {MP_QSTR_y1 , MP_ARG_REQUIRED | MP_ARG_INT },
358
+ {MP_QSTR_x2 , MP_ARG_REQUIRED | MP_ARG_INT },
359
+ {MP_QSTR_y2 , MP_ARG_REQUIRED | MP_ARG_INT },
360
+ {MP_QSTR_value , MP_ARG_REQUIRED | MP_ARG_INT },
361
+ };
362
+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
363
+ mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
364
+
365
+ displayio_bitmap_t * destination = MP_OBJ_TO_PTR (args [ARG_dest_bitmap ].u_obj ); // the destination bitmap
366
+
367
+ uint32_t value , color_depth ;
368
+ value = args [ARG_value ].u_int ;
369
+ color_depth = (1 << destination -> bits_per_value );
370
+ if (color_depth <= value ) {
371
+ mp_raise_ValueError (translate ("out of range of target" ));
372
+ }
373
+
374
+ int16_t x1 = args [ARG_x1 ].u_int ;
375
+ int16_t y1 = args [ARG_y1 ].u_int ;
376
+ int16_t x2 = args [ARG_x2 ].u_int ;
377
+ int16_t y2 = args [ARG_y2 ].u_int ;
378
+
379
+ // verify points are within the bitmap boundary (inclusive)
380
+ if ( (x1 < 0 ) || (x2 < 0 ) || (y1 < 0 ) || (y2 < 0 ) ||
381
+ (x1 >= destination -> width ) || (x2 >= destination -> width ) ||
382
+ (y1 >= destination -> height ) || (y2 >= destination -> height ) ) {
383
+ mp_raise_ValueError (translate ("out of range of target" ));
384
+ }
385
+
386
+ common_hal_bitmaptools_draw_line (destination , x1 , y1 , x2 , y2 , value );
387
+
388
+ return mp_const_none ;
389
+ }
390
+
391
+ MP_DEFINE_CONST_FUN_OBJ_KW (bitmaptools_draw_line_obj , 0 , bitmaptools_obj_draw_line );
392
+ // requires all 6 arguments
246
393
247
394
STATIC const mp_rom_map_elem_t bitmaptools_module_globals_table [] = {
248
395
{ MP_ROM_QSTR (MP_QSTR_rotozoom ), MP_ROM_PTR (& bitmaptools_rotozoom_obj ) },
396
+ { MP_ROM_QSTR (MP_QSTR_fill_region ), MP_ROM_PTR (& bitmaptools_fill_region_obj ) },
397
+ { MP_ROM_QSTR (MP_QSTR_draw_line ), MP_ROM_PTR (& bitmaptools_draw_line_obj ) },
249
398
};
250
399
STATIC MP_DEFINE_CONST_DICT (bitmaptools_module_globals , bitmaptools_module_globals_table );
251
400
252
-
253
401
const mp_obj_module_t bitmaptools_module = {
254
402
.base = {& mp_type_module },
255
403
.globals = (mp_obj_dict_t * )& bitmaptools_module_globals ,
0 commit comments