Skip to content

Commit 824f47d

Browse files
Margaret Matochakmatch98
authored andcommitted
Added bitmap.blit function for bitmap slice copy
1 parent 4ba9ff8 commit 824f47d

File tree

3 files changed

+84
-35
lines changed

3 files changed

+84
-35
lines changed

shared-bindings/displayio/Bitmap.c

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -172,42 +172,82 @@ STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t val
172172
return mp_const_none;
173173
}
174174

175-
//| def blit(self, x: int, y: int, source_bitmap: bitmap, x1: int, y1: int, x2: int, y2:int) -> Any:
175+
//| def blit(self, x: int, y: int, source_bitmap: bitmap, x1: int, y1: int, x2: int, y2: int, skip_index: int) -> Any:
176176
//| """Inserts the source_bitmap region defined by rectangular boundaries
177177
//| (x1,y1) and (x2,y2) into the bitmap at the specified (x,y) location.
178178
//| :param int x: Horizontal pixel location in bitmap where source_bitmap upper-left
179179
//| corner will be placed
180180
//| :param int y: Vertical pixel location in bitmap where source_bitmap upper-left
181181
//| corner will be placed
182182
//| :param bitmap source_bitmap: Source bitmap that contains the graphical region to be copied
183-
//| : param x1: Minimum x-value for rectangular bounding box to be copied from the source bitmap
184-
//| : param y1: Minimum y-value for rectangular bounding box to be copied from the source bitmap
185-
//| : param x2: Maximum x-value for rectangular bounding box to be copied from the source bitmap
186-
//| : param y2: Maximum y-value for rectangular bounding box to be copied from the source bitmap
187-
//|
183+
//| : param int x1: Minimum x-value for rectangular bounding box to be copied from the source bitmap
184+
//| : param int y1: Minimum y-value for rectangular bounding box to be copied from the source bitmap
185+
//| : param int x2: Maximum x-value for rectangular bounding box to be copied from the source bitmap
186+
//| : param int y2: Maximum y-value for rectangular bounding box to be copied from the source bitmap
187+
//| : param int skip_index: bitmap palette index in the source that will not be copied, set `None` to copy all pixels
188188
//| ...
189189
//|
190190

191191
STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args){
192+
enum {ARG_x, ARG_y, ARG_source, ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_skip_index};
193+
static const mp_arg_t allowed_args[] = {
194+
{MP_QSTR_x, MP_ARG_REQUIRED | MP_ARG_INT},
195+
{MP_QSTR_y, MP_ARG_REQUIRED | MP_ARG_INT},
196+
{MP_QSTR_source, MP_ARG_REQUIRED | MP_ARG_OBJ},
197+
{MP_QSTR_x1, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
198+
{MP_QSTR_y1, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
199+
{MP_QSTR_x2, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, // None convert to source->width
200+
{MP_QSTR_y2, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, // None convert to source->height
201+
{MP_QSTR_skip_index, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj=mp_const_none} },
202+
};
203+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
204+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
205+
206+
displayio_bitmap_t *self = MP_OBJ_TO_PTR(pos_args[0]); //*******
207+
208+
int16_t x = args[ARG_x].u_int;
209+
int16_t y = args[ARG_y].u_int;
210+
211+
displayio_bitmap_t *source = MP_OBJ_TO_PTR(args[ARG_source].u_obj);
212+
213+
int16_t x1, y1;
214+
// if x1 or y1 is None, then set as the zero-point of the bitmap
215+
if ( args[ARG_x1].u_obj == mp_const_none ) {
216+
x1 = 0;
217+
} else {
218+
x1 = mp_obj_get_int(args[ARG_x1].u_obj);
219+
}
220+
//int16_t y1;
221+
if ( args[ARG_y1].u_obj == mp_const_none ) {
222+
y1 = 0;
223+
} else {
224+
y1 = mp_obj_get_int(args[ARG_y1].u_obj);
225+
}
226+
227+
int16_t x2, y2;
228+
// if x2 or y2 is None, then set as the maximum size of the source bitmap
229+
if ( args[ARG_x2].u_obj == mp_const_none ) {
230+
x2 = source->width-1;
231+
} else {
232+
x2 = mp_obj_get_int(args[ARG_x2].u_obj);
233+
}
234+
//int16_t y2;
235+
if ( args[ARG_y2].u_obj == mp_const_none ) {
236+
y2 = source->height-1;
237+
} else {
238+
y2 = mp_obj_get_int(args[ARG_y2].u_obj);
239+
}
192240

193-
// Consider improving the input checking.
194-
195-
displayio_bitmap_t *self = MP_OBJ_TO_PTR(pos_args[0]);
196-
int16_t x = mp_obj_get_int(pos_args[1]);
197-
int16_t y = mp_obj_get_int(pos_args[2]);
198-
displayio_bitmap_t *source = MP_OBJ_TO_PTR(pos_args[3]);
199-
int16_t x1 = mp_obj_get_int(pos_args[4]);
200-
int16_t y1 = mp_obj_get_int(pos_args[5]);
201-
int16_t x2 = mp_obj_get_int(pos_args[6]);
202-
int16_t y2 = mp_obj_get_int(pos_args[7]);
203241

204-
if ( (x<0) || (y<0) || (x > self-> width) || (y > self->height) ) {
242+
// Check x,y are within self (target) bitmap boundary
243+
if ( (x < 0) || (y < 0) || (x >= self->width) || (y >= self->height) ) {
205244
mp_raise_ValueError(translate("(x,y): out of range of target bitmap"));
206245
}
207-
if ( (x1 < 0) || (x1 > source->width) ||
208-
(y1 < 0) || (y1 > source->height) ||
209-
(x2 < 0) || (x2 > source->width) ||
210-
(y2 < 0) || (y2 > source->height) ) {
246+
// Check x1,y1,x2,y2 are within source bitmap boundary
247+
if ( (x1 < 0) || (x1 >= source->width) ||
248+
(y1 < 0) || (y1 >= source->height) ||
249+
(x2 < 0) || (x2 >= source->width) ||
250+
(y2 < 0) || (y2 >= source->height) ) {
211251
mp_raise_ValueError(translate("(x1,y1) or (x2,y2): out of range of source bitmap"));
212252
}
213253

@@ -223,13 +263,23 @@ STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_arg
223263
y1=temp;
224264
}
225265

226-
common_hal_displayio_bitmap_blit(self, x, y, source, x1, y1, x2, y2);
266+
uint32_t skip_index;
267+
bool skip_index_none; // flag whether skip_value was None
268+
269+
if (args[ARG_skip_index].u_obj == mp_const_none ) {
270+
skip_index = 0;
271+
skip_index_none = true;
272+
} else {
273+
skip_index = mp_obj_get_int(args[ARG_skip_index].u_obj);
274+
skip_index_none = false;
275+
}
276+
277+
common_hal_displayio_bitmap_blit(self, x, y, source, x1, y1, x2, y2, skip_index, skip_index_none);
227278

228279
return mp_const_none;
229280
}
230-
231-
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_bitmap_blit_obj, 8, displayio_bitmap_obj_blit);
232-
// requires 8 parameters
281+
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_bitmap_blit_obj, 4, displayio_bitmap_obj_blit);
282+
// `displayio_bitmap_obj_blit` requires at least 4 arguments
233283

234284
//| def fill(self, value: Any) -> Any:
235285
//| """Fills the bitmap with the supplied palette index value."""

shared-bindings/displayio/Bitmap.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ uint16_t common_hal_displayio_bitmap_get_width(displayio_bitmap_t *self);
4141
uint32_t common_hal_displayio_bitmap_get_bits_per_value(displayio_bitmap_t *self);
4242
void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y, uint32_t value);
4343
void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16_t y, displayio_bitmap_t *source,
44-
int16_t x1, int16_t y1, int16_t x2, int16_t y2);
44+
int16_t x1, int16_t y1, int16_t x2, int16_t y2,
45+
uint32_t skip_index, bool skip_index_none);
4546
uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y);
4647
void common_hal_displayio_bitmap_fill(displayio_bitmap_t *bitmap, uint32_t value);
4748

shared-module/displayio/Bitmap.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,26 +106,24 @@ uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *self, int16_t
106106
}
107107

108108
void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16_t y, displayio_bitmap_t *source,
109-
int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
109+
int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint32_t skip_index, bool skip_index_none) {
110110
// Copy complete "source" bitmap into "self" bitmap at location x,y in the "self"
111111
// Add a boolean to determine if all values are copied, or only if non-zero
112-
// Default is copy all values, but for text glyphs this should copy only non-zero values
112+
//
113+
// If skip_value is encountered in the source bitmap, it will not be copied.
114+
// If skip_value is `None`, then all pixels are copied.
113115

114116
if (self->read_only) {
115117
mp_raise_RuntimeError(translate("Read-only object"));
116118
}
117119

118-
// If this value is encountered in the source bitmap, it will not be copied (for text glyphs)
119-
// This should be added as an optional parameter, and if it is `None`, then all pixels are copied
120-
uint32_t skip_value=0;
121-
122120
// simplest version - use internal functions for get/set pixels
123-
for (uint16_t i=0; i<= (x2-x1) ; i++) {
121+
for (int16_t i=0; i<= (x2-x1) ; i++) {
124122
if ( (x+i >= 0) && (x+i < self->width) ) {
125-
for (uint16_t j=0; j<= (y2-y1) ; j++){
123+
for (int16_t j=0; j<= (y2-y1) ; j++){
126124
if ((y+j >= 0) && (y+j < self->height) ) {
127125
uint32_t value = common_hal_displayio_bitmap_get_pixel(source, x1+i, y1+j);
128-
if ( (value != skip_value) ) {
126+
if ( (skip_index_none) || (value != skip_index) ) { // write if skip_value_none is True
129127
common_hal_displayio_bitmap_set_pixel(self, x+i, y+j, value);
130128
}
131129
}

0 commit comments

Comments
 (0)