8000 extmod/framebuf: Add "remap" option to blit. · micropython/micropython@2648da2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2648da2

Browse files
committed
extmod/framebuf: Add "remap" option to blit.
Add an optional keyword argument "remap" to the framebuf.blit method, which allows to pass an array specifying the color palette to be used for mapping the colors while blitting. This makes it possible to blit between framebufs with different color depths, and also to do palette effects.
1 parent aa7be82 commit 2648da2

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

extmod/modframebuf.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,14 +415,30 @@ STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args) {
415415
}
416416
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_line_obj, 6, 6, framebuf_line);
417417

418-
STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args) {
419-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
420-
mp_obj_framebuf_t *source = MP_OBJ_TO_PTR(args[1]);
421-
mp_int_t x = mp_obj_get_int(args[2]);
422-
mp_int_t y = mp_obj_get_int(args[3]);
423-
mp_int_t key = -1;
424-
if (n_args > 4) {
425-
key = mp_obj_get_int(args[4]);
418+
STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
419+
enum { ARG_self, ARG_source, ARG_x, ARG_y, ARG_key, ARG_remap };
420+
static const mp_arg_t allowed_args[] = {
421+
{ MP_QSTR_self, MP_ARG_REQUIRED | MP_ARG_OBJ },
422+
{ MP_QSTR_source, MP_ARG_REQUIRED | MP_ARG_OBJ },
423+
{ MP_QSTR_x, MP_ARG_INT | MP_ARG_REQUIRED },
424+
{ MP_QSTR_y, MP_ARG_INT | MP_ARG_REQUIRED },
425+
{ MP_QSTR_key, MP_ARG_INT, {.u_int = -1} },
426+
{ MP_QSTR_remap, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
427+
};
428+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
429+
mp_arg_parse_all(n_args, pos_args, kw_args,
430+
MP_ARRAY_SIZE(allowed_args), allowed_args, args);
431+
432+
mp_obj_framebuf_t *self = args[ARG_self].u_obj;
433+
mp_obj_framebuf_t *source = args[ARG_source].u_obj;
434+
mp_int_t x = args[ARG_x].u_int;
435+
mp_int_t y = args[ARG_y].u_int;
436+
mp_int_t key = args[ARG_key].u_int;
437+
438+
size_t remap_size = 0;
439+
mp_obj_t *remap;
440+
if (args[ARG_remap].u_obj != MP_OBJ_NULL) {
441+
mp_obj_get_array(args[ARG_remap].u_obj, &remap_size, &remap);
426442
}
427443

428444
if (
@@ -449,6 +465,9 @@ STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args) {
449465
for (int cx0 = x0; cx0 < x0end; ++cx0) {
450466
color = getpixel(source, cx1, y1);
451467
if (color != (uint32_t)key) {
468+
if (color < remap_size) {
469+
color = mp_obj_get_int(remap[color]);
470+
}
452471
setpixel(self, cx0, y0, color);
453472
}
454473
++cx1;
@@ -457,7 +476,7 @@ STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args) {
457476
}
458477
return mp_const_none;
459478
}
460-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_blit_obj, 4, 5, framebuf_blit);
479+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(framebuf_blit_obj, 4, framebuf_blit);
461480

462481
STATIC mp_obj_t framebuf_scroll(mp_obj_t self_in, mp_obj_t xstep_in, mp_obj_t ystep_in) {
463482
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);

0 commit comments

Comments
 (0)
0