@@ -273,6 +273,28 @@ STATIC mp_obj_t bitmaptools_obj_rotozoom(size_t n_args, const mp_obj_t *pos_args
273
273
}
274
274
275
275
MP_DEFINE_CONST_FUN_OBJ_KW (bitmaptools_rotozoom_obj , 0 , bitmaptools_obj_rotozoom );
276
+
277
+ MAKE_ENUM_VALUE (bitmaptools_blendmode_type , bitmaptools_blendmode , NORMAL , BITMAPTOOLS_BLENDMODE_NORMAL );
278
+ MAKE_ENUM_VALUE (bitmaptools_blendmode_type , bitmaptools_blendmode , SCREEN , BITMAPTOOLS_BLENDMODE_SCREEN );
279
+
280
+ //| class BlendMode:
281
+ //| """The blend mode for `alphablend` to operate use"""
282
+ //|
283
+ //| NORMAL: Blendmode
284
+ //| """Blend with equal parts of the two source bitmaps"""
285
+ //|
286
+ //| SCREEN: Blendmode
287
+ //| """Blend based on the value in each color channel. The result keeps the lighter colors and discards darker colors."""
288
+ //|
289
+ MAKE_ENUM_MAP (bitmaptools_blendmode ) {
290
+ MAKE_ENUM_MAP_ENTRY (bitmaptools_blendmode , NORMAL ),
291
+ MAKE_ENUM_MAP_ENTRY (bitmaptools_blendmode , SCREEN ),
292
+ };
293
+ STATIC MP_DEFINE_CONST_DICT (bitmaptools_blendmode_locals_dict , bitmaptools_blendmode_locals_table );
294
+
295
+ MAKE_PRINTER (bitmaptools , bitmaptools_blendmode );
296
+ MAKE_ENUM_TYPE (bitmaptools , BlendMode , bitmaptools_blendmode );
297
+
276
298
// requires at least 2 arguments (destination bitmap and source bitmap)
277
299
278
300
//| def alphablend(
@@ -282,6 +304,9 @@ MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_rotozoom_obj, 0, bitmaptools_obj_rotozoom
282
304
//| colorspace: displayio.Colorspace,
283
305
//| factor1: float = 0.5,
284
306
//| factor2: Optional[float] = None,
307
+ //| blendmode: Optional[Blendmode] = Blendmode.NORMAL,
308
+ //| skip_source1_index: int,
309
+ //| skip_source2_index: int,
285
310
//| ) -> None:
286
311
//| """Alpha blend the two source bitmaps into the destination.
287
312
//|
@@ -294,13 +319,18 @@ MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_rotozoom_obj, 0, bitmaptools_obj_rotozoom
294
319
//| :param float factor1: The proportion of bitmap 1 to mix in
295
320
//| :param float factor2: The proportion of bitmap 2 to mix in. If specified as `None`, ``1-factor1`` is used. Usually the proportions should sum to 1.
296
321
//| :param displayio.Colorspace colorspace: The colorspace of the bitmaps. They must all have the same colorspace. Only the following colorspaces are permitted: ``L8``, ``RGB565``, ``RGB565_SWAPPED``, ``BGR565`` and ``BGR565_SWAPPED``.
322
+ //| :param bitmaptools.BlendMode blendmode: The blend mode to use. Default is NORMAL.
323
+ //| :param int skip_source1_index: bitmap palette index in the source that will not be blended,
324
+ //| set to None to blended all pixels
325
+ //| :param int skip_source2_index: bitmap palette index in the source that will not be blended,
326
+ //| set to None to blended all pixels
297
327
//|
298
328
//| For the L8 colorspace, the bitmaps must have a bits-per-value of 8.
299
329
//| For the RGB colorspaces, they must have a bits-per-value of 16."""
300
330
//|
301
331
302
332
STATIC mp_obj_t bitmaptools_alphablend (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
303
- enum {ARG_dest_bitmap , ARG_source_bitmap_1 , ARG_source_bitmap_2 , ARG_colorspace , ARG_factor_1 , ARG_factor_2 };
333
+ enum {ARG_dest_bitmap , ARG_source_bitmap_1 , ARG_source_bitmap_2 , ARG_colorspace , ARG_factor_1 , ARG_factor_2 , ARG_blendmode , ARG_skip_source1_index , ARG_skip_source2_index };
304
334
305
335
static const mp_arg_t allowed_args [] = {
306
336
{MP_QSTR_dest_bitmap , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = NULL }},
@@ -309,6 +339,9 @@ STATIC mp_obj_t bitmaptools_alphablend(size_t n_args, const mp_obj_t *pos_args,
309
339
{MP_QSTR_colorspace , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = NULL }},
310
340
{MP_QSTR_factor_1 , MP_ARG_OBJ , {.u_obj = MP_ROM_NONE }},
311
341
{MP_QSTR_factor_2 , MP_ARG_OBJ , {.u_obj = MP_ROM_NONE }},
342
+ {MP_QSTR_blendmode , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = (void * )& bitmaptools_blendmode_NORMAL_obj }},
343
+ {MP_QSTR_skip_source1_index , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
344
+ {MP_QSTR_skip_source2_index , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
312
345
};
313
346
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
314
347
mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
@@ -321,6 +354,7 @@ STATIC mp_obj_t bitmaptools_alphablend(size_t n_args, const mp_obj_t *pos_args,
321
354
mp_float_t factor2 = (args [ARG_factor_2 ].u_obj == mp_const_none ) ? 1 - factor1 : mp_obj_get_float (args [ARG_factor_2 ].u_obj );
322
355
323
356
displayio_colorspace_t colorspace = (displayio_colorspace_t )cp_enum_value (& displayio_colorspace_type , args [ARG_colorspace ].u_obj , MP_QSTR_colorspace );
357
+ bitmaptools_blendmode_t blendmode = (bitmaptools_blendmode_t )cp_enum_value (& bitmaptools_blendmode_type , args [ARG_blendmode ].u_obj , MP_QSTR_blendmode );
324
358
325
359
if (destination -> width != source1 -> width
326
360
|| destination -> height != source1 -> height
@@ -352,7 +386,30 @@ STATIC mp_obj_t bitmaptools_alphablend(size_t n_args, const mp_obj_t *pos_args,
352
386
mp_raise_ValueError (translate ("Unsupported colorspace" ));
353
387
}
354
388
355
- common_hal_bitmaptools_alphablend (destination , source1 , source2 , colorspace , factor1 , factor2 );
389
+ uint32_t skip_source1_index ;
390
+ bool skip_source1_index_none ; // flag whether skip_value was None
391
+
392
+ if (args [ARG_skip_source1_index ].u_obj == mp_const_none ) {
393
+ skip_source1_index = 0 ;
394
+ skip_source1_index_none = true;
395
+ } else {
396
+ skip_source1_index = mp_obj_get_int (args [ARG_skip_source1_index ].u_obj );
397
+ skip_source1_index_none = false;
398
+ }
399
+
400
+ uint32_t skip_source2_index ;
401
+ bool skip_source2_index_none ; // flag whether skip_self_value was None
402
+
403
+ if (args [ARG_skip_source2_index ].u_obj == mp_const_none ) {
404
+ skip_source2_index = 0 ;
405
+ skip_source2_index_none = true;
406
+ } else {
407
+ skip_source2_index = mp_obj_get_int (args [ARG_skip_source2_index ].u_obj );
408
+ skip_source2_index_none = false;
409
+ }
410
+
411
+ common_hal_bitmaptools_alphablend (destination , source1 , source2 , colorspace , factor1 , factor2 , blendmode , skip_source1_index ,
412
+ skip_source1_index_none , skip_source2_index , skip_source2_index_none );
356
413
357
414
return mp_const_none ;
358
415
}
@@ -1080,6 +1137,7 @@ STATIC const mp_rom_map_elem_t bitmaptools_module_globals_table[] = {
1080
1137
{ MP_ROM_QSTR (MP_QSTR_readinto ), MP_ROM_PTR (& bitmaptools_readinto_obj ) },
1081
1138
{ MP_ROM_QSTR (MP_QSTR_rotozoom ), MP_ROM_PTR (& bitmaptools_rotozoom_obj ) },
1082
1139
{ MP_ROM_QSTR (MP_QSTR_arrayblit ), MP_ROM_PTR (& bitmaptools_arrayblit_obj ) },
1140
+ { MP_ROM_QSTR (MP_QSTR_Blendmode ), MP_ROM_PTR (& bitmaptools_blendmode_type ) },
1083
1141
{ MP_ROM_QSTR (MP_QSTR_alphablend ), MP_ROM_PTR (& bitmaptools_alphablend_obj ) },
1084
1142
{ MP_ROM_QSTR (MP_QSTR_fill_region ), MP_ROM_PTR (& bitmaptools_fill_region_obj ) },
1085
1143
{ MP_ROM_QSTR (MP_QSTR_boundary_fill ), MP_ROM_PTR (& bitmaptools_boundary_fill_obj ) },
0 commit comments