@@ -273,6 +273,28 @@ STATIC mp_obj_t bitmaptools_obj_rotozoom(size_t n_args, const mp_obj_t *pos_args
273273}
274274
275275MP_DEFINE_CONST_FUN_OBJ_KW (bitmaptools_rotozoom_obj , 0 , bitmaptools_obj_rotozoom );
276+
277+ //| class BlendMode:
278+ //| """The blend mode for `alphablend` to operate use"""
279+ //|
280+ //| Normal: BlendMode
281+ //| """Blend with equal parts of the two source bitmaps"""
282+ //|
283+ //| Screen: BlendMode
284+ //| """Blend based on the value in each color channel. The result keeps the lighter colors and discards darker colors."""
285+ //|
286+ MAKE_ENUM_VALUE (bitmaptools_blendmode_type , bitmaptools_blendmode , Normal , BITMAPTOOLS_BLENDMODE_NORMAL );
287+ MAKE_ENUM_VALUE (bitmaptools_blendmode_type , bitmaptools_blendmode , Screen , BITMAPTOOLS_BLENDMODE_SCREEN );
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+
276298// requires at least 2 arguments (destination bitmap and source bitmap)
277299
278300//| def alphablend(
@@ -282,6 +304,9 @@ MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_rotozoom_obj, 0, bitmaptools_obj_rotozoom
282304//| colorspace: displayio.Colorspace,
283305//| factor1: float = 0.5,
284306//| factor2: Optional[float] = None,
307+ //| blendmode: Optional[BlendMode] = BlendMode.Normal,
308+ //| skip_source1_index: Union[int, None] = None,
309+ //| skip_source2_index: Union[int, None] = None,
285310//| ) -> None:
286311//| """Alpha blend the two source bitmaps into the destination.
287312//|
@@ -294,13 +319,16 @@ MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_rotozoom_obj, 0, bitmaptools_obj_rotozoom
294319//| :param float factor1: The proportion of bitmap 1 to mix in
295320//| :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.
296321//| :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 or luminance index in source_bitmap_1 that will not be blended, set to None to blend all pixels
324+ //| :param int skip_source2_index: Bitmap palette or luminance index in source_bitmap_2 that will not be blended, set to None to blend all pixels
297325//|
298326//| For the L8 colorspace, the bitmaps must have a bits-per-value of 8.
299327//| For the RGB colorspaces, they must have a bits-per-value of 16."""
300328//|
301329
302330STATIC 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 };
331+ 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 };
304332
305333 static const mp_arg_t allowed_args [] = {
306334 {MP_QSTR_dest_bitmap , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = NULL }},
@@ -309,6 +337,9 @@ STATIC mp_obj_t bitmaptools_alphablend(size_t n_args, const mp_obj_t *pos_args,
309337 {MP_QSTR_colorspace , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = NULL }},
310338 {MP_QSTR_factor_1 , MP_ARG_OBJ , {.u_obj = MP_ROM_NONE }},
311339 {MP_QSTR_factor_2 , MP_ARG_OBJ , {.u_obj = MP_ROM_NONE }},
340+ {MP_QSTR_blendmode , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = (void * )& bitmaptools_blendmode_Normal_obj }},
341+ {MP_QSTR_skip_source1_index , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
342+ {MP_QSTR_skip_source2_index , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
312343 };
313344 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
314345 mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
@@ -321,6 +352,7 @@ STATIC mp_obj_t bitmaptools_alphablend(size_t n_args, const mp_obj_t *pos_args,
321352 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 );
322353
323354 displayio_colorspace_t colorspace = (displayio_colorspace_t )cp_enum_value (& displayio_colorspace_type , args [ARG_colorspace ].u_obj , MP_QSTR_colorspace );
355+ bitmaptools_blendmode_t blendmode = (bitmaptools_blendmode_t )cp_enum_value (& bitmaptools_blendmode_type , args [ARG_blendmode ].u_obj , MP_QSTR_blendmode );
324356
325357 if (destination -> width != source1 -> width
326358 || destination -> height != source1 -> height
@@ -352,7 +384,30 @@ STATIC mp_obj_t bitmaptools_alphablend(size_t n_args, const mp_obj_t *pos_args,
352384 mp_raise_ValueError (translate ("Unsupported colorspace" ));
353385 }
354386
355- common_hal_bitmaptools_alphablend (destination , source1 , source2 , colorspace , factor1 , factor2 );
387+ uint32_t skip_source1_index ;
388+ bool skip_source1_index_none ; // flag whether skip_value was None
389+
390+ if (args [ARG_skip_source1_index ].u_obj == mp_const_none ) {
391+ skip_source1_index = 0 ;
392+ skip_source1_index_none = true;
393+ } else {
394+ skip_source1_index = mp_obj_get_int (args [ARG_skip_source1_index ].u_obj );
395+ skip_source1_index_none = false;
396+ }
397+
398+ uint32_t skip_source2_index ;
399+ bool skip_source2_index_none ; // flag whether skip_self_value was None
400+
401+ if (args [ARG_skip_source2_index ].u_obj == mp_const_none ) {
402+ skip_source2_index = 0 ;
403+ skip_source2_index_none = true;
404+ } else {
405+ skip_source2_index = mp_obj_get_int (args [ARG_skip_source2_index ].u_obj );
406+ skip_source2_index_none = false;
407+ }
408+
409+ common_hal_bitmaptools_alphablend (destination , source1 , source2 , colorspace , factor1 , factor2 , blendmode , skip_source1_index ,
410+ skip_source1_index_none , skip_source2_index , skip_source2_index_none );
356411
357412 return mp_const_none ;
358413}
@@ -1088,6 +1143,7 @@ STATIC const mp_rom_map_elem_t bitmaptools_module_globals_table[] = {
10881143 { MP_ROM_QSTR (MP_QSTR_draw_circle ), MP_ROM_PTR (& bitmaptools_draw_circle_obj ) },
10891144 { MP_ROM_QSTR (MP_QSTR_blit ), MP_ROM_PTR (& bitmaptools_blit_obj ) },
10901145 { MP_ROM_QSTR (MP_QSTR_dither ), MP_ROM_PTR (& bitmaptools_dither_obj ) },
1146+ { MP_ROM_QSTR (MP_QSTR_BlendMode ), MP_ROM_PTR (& bitmaptools_blendmode_type ) },
10911147 { MP_ROM_QSTR (MP_QSTR_DitherAlgorithm ), MP_ROM_PTR (& bitmaptools_dither_algorithm_type ) },
10921148};
10931149STATIC MP_DEFINE_CONST_DICT (bitmaptools_module_globals , bitmaptools_module_globals_table );
0 commit comments