@@ -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
+ //| 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
+
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: Union[int, None] = None,
309
+ //| skip_source2_index: Union[int, None] = None,
285
310
//| ) -> None:
286
311
//| """Alpha blend the two source bitmaps into the destination.
287
312
//|
@@ -294,13 +319,16 @@ 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 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
297
325
//|
298
326
//| For the L8 colorspace, the bitmaps must have a bits-per-value of 8.
299
327
//| For the RGB colorspaces, they must have a bits-per-value of 16."""
300
328
//|
301
329
302
330
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 };
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 };
304
332
305
333
static const mp_arg_t allowed_args [] = {
306
334
{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,
309
337
{MP_QSTR_colorspace , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = NULL }},
310
338
{MP_QSTR_factor_1 , MP_ARG_OBJ , {.u_obj = MP_ROM_NONE }},
311
339
{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 } },
312
343
};
313
344
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
314
345
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,
321
352
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
353
323
354
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 );
324
356
325
357
if (destination -> width != source1 -> width
326
358
|| destination -> height != source1 -> height
@@ -352,7 +384,30 @@ STATIC mp_obj_t bitmaptools_alphablend(size_t n_args, const mp_obj_t *pos_args,
352
384
mp_raise_ValueError (translate ("Unsupported colorspace" ));
353
385
}
354
386
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 );
356
411
357
412
return mp_const_none ;
358
413
}
@@ -1088,6 +1143,7 @@ STATIC const mp_rom_map_elem_t bitmaptools_module_globals_table[] = {
1088
1143
{ MP_ROM_QSTR (MP_QSTR_draw_circle ), MP_ROM_PTR (& bitmaptools_draw_circle_obj ) },
1089
1144
{ MP_ROM_QSTR (MP_QSTR_blit ), MP_ROM_PTR (& bitmaptools_blit_obj ) },
1090
1145
{ 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 ) },
1091
1147
{ MP_ROM_QSTR (MP_QSTR_DitherAlgorithm ), MP_ROM_PTR (& bitmaptools_dither_algorithm_type ) },
1092
1148
};
1093
1149
STATIC MP_DEFINE_CONST_DICT (bitmaptools_module_globals , bitmaptools_module_globals_table );
0 commit comments