Skip to content

Commit abf1512

Browse files
committed
adding skip_self_index argument to bitmap.blit()
1 parent ce3df82 commit abf1512

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

shared-bindings/displayio/Bitmap.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t val
196196
//| y1: int,
197197
//| x2: int,
198198
//| y2: int,
199-
//| skip_index: int
199+
//| skip_index: int,
200+
//| skip_self_index: int
200201
//| ) -> None:
201202
//| """Inserts the source_bitmap region defined by rectangular boundaries
202203
//| (x1,y1) and (x2,y2) into the bitmap at the specified (x,y) location.
@@ -211,10 +212,12 @@ STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t val
211212
//| :param int x2: Maximum x-value (exclusive) for rectangular bounding box to be copied from the source bitmap
212213
//| :param int y2: Maximum y-value (exclusive) for rectangular bounding box to be copied from the source bitmap
213214
//| :param int skip_index: bitmap palette index in the source that will not be copied,
214-
//| set to None to copy all pixels"""
215+
//| set to None to copy all pixels
216+
//| :param int skip_self_index: bitmap palette index in the self bitmap that will not get overwritten
217+
//| by the pixels from the source"""
215218
//| ...
216219
STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
217-
enum {ARG_x, ARG_y, ARG_source, ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_skip_index};
220+
enum {ARG_x, ARG_y, ARG_source, ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_skip_index, ARG_skip_self_index};
218221
static const mp_arg_t allowed_args[] = {
219222
{MP_QSTR_x, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL} },
220223
{MP_QSTR_y, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL} },
@@ -224,6 +227,7 @@ STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_arg
224227
{MP_QSTR_x2, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, // None convert to source->width
225228
{MP_QSTR_y2, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, // None convert to source->height
226229
{MP_QSTR_skip_index, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
230+
{MP_QSTR_skip_self_index, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
227231
};
228232
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
229233
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -283,7 +287,19 @@ STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_arg
283287
skip_index_none = false;
284288
}
285289

286-
common_hal_displayio_bitmap_blit(self, x, y, source, x1, y1, x2, y2, skip_index, skip_index_none);
290+
uint32_t skip_self_index;
291+
bool skip_self_index_none; // flag whether skip_self_value was None
292+
293+
if (args[ARG_skip_self_index].u_obj == mp_const_none) {
294+
skip_self_index = 0;
295+
skip_self_index_none = true;
296+
} else {
297+
skip_self_index = mp_obj_get_int(args[ARG_skip_self_index].u_obj);
298+
skip_self_index_none = false;
299+
}
300+
301+
common_hal_displayio_bitmap_blit(self, x, y, source, x1, y1, x2, y2, skip_index, skip_index_none, skip_self_index,
302+
skip_self_index_none);
287303

288304
return mp_const_none;
289305
}

shared-bindings/displayio/Bitmap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ uint32_t common_hal_displayio_bitmap_get_bits_per_value(displayio_bitmap_t *self
4444
void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y, uint32_t value);
4545
void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16_t y, displayio_bitmap_t *source,
4646
int16_t x1, int16_t y1, int16_t x2, int16_t y2,
47-
uint32_t skip_index, bool skip_index_none);
47+
uint32_t skip_index, bool skip_index_none, uint32_t skip_self_index, bool skip_self_index_none);
4848
uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y);
4949
void common_hal_displayio_bitmap_fill(displayio_bitmap_t *bitmap, uint32_t value);
5050
int common_hal_displayio_bitmap_get_buffer(displayio_bitmap_t *self, mp_buffer_info_t *bufinfo, mp_uint_t flags);

shared-module/displayio/Bitmap.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ void displayio_bitmap_write_pixel(displayio_bitmap_t *self, int16_t x, int16_t y
175175
}
176176

177177
void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16_t y, displayio_bitmap_t *source,
178-
int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint32_t skip_index, bool skip_index_none) {
178+
int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint32_t skip_index, bool skip_index_none, uint32_t skip_self_index,
179+
bool skip_self_index_none) {
179180
if (self->read_only) {
180181
mp_raise_RuntimeError(translate("Read-only"));
181182
}
@@ -222,8 +223,17 @@ void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16
222223

223224
if ((yd_index >= 0) && (yd_index < self->height)) {
224225
uint32_t value = common_hal_displayio_bitmap_get_pixel(source, xs_index, ys_index);
225-
if ((skip_index_none) || (value != skip_index)) { // write if skip_value_none is True
226-
displayio_bitmap_write_pixel(self, xd_index, yd_index, value);
226+
if (skip_self_index_none) { // if skip_self_index is none, then only check source skip
227+
if ((skip_index_none) || (value != skip_index)) { // write if skip_value_none is True
228+
displayio_bitmap_write_pixel(self, xd_index, yd_index, value);
229+
}
230+
} else { // check dest_value index against skip_self_index and skip if they match
231+
uint32_t dest_value = common_hal_displayio_bitmap_get_pixel(self, xd_index, yd_index);
232+
if (dest_value != skip_self_index) {
233+
if ((skip_index_none) || (value != skip_index)) { // write if skip_value_none is True
234+
displayio_bitmap_write_pixel(self, xd_index, yd_index, value);
235+
}
236+
}
227237
}
228238
}
229239
}

0 commit comments

Comments
 (0)