Added bitmap.blit function for bitmap slice copy

This commit is contained in:
Margaret Matocha 2020-08-12 21:50:17 -05:00 committed by Kevin Matocha
parent 4ba9ff892c
commit 824f47d6e9
3 changed files with 83 additions and 34 deletions

View File

@ -172,7 +172,7 @@ STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t val
return mp_const_none; return mp_const_none;
} }
//| def blit(self, x: int, y: int, source_bitmap: bitmap, x1: int, y1: int, x2: int, y2:int) -> Any: //| def blit(self, x: int, y: int, source_bitmap: bitmap, x1: int, y1: int, x2: int, y2: int, skip_index: int) -> Any:
//| """Inserts the source_bitmap region defined by rectangular boundaries //| """Inserts the source_bitmap region defined by rectangular boundaries
//| (x1,y1) and (x2,y2) into the bitmap at the specified (x,y) location. //| (x1,y1) and (x2,y2) into the bitmap at the specified (x,y) location.
//| :param int x: Horizontal pixel location in bitmap where source_bitmap upper-left //| :param int x: Horizontal pixel location in bitmap where source_bitmap upper-left
@ -180,34 +180,74 @@ STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t val
//| :param int y: Vertical pixel location in bitmap where source_bitmap upper-left //| :param int y: Vertical pixel location in bitmap where source_bitmap upper-left
//| corner will be placed //| corner will be placed
//| :param bitmap source_bitmap: Source bitmap that contains the graphical region to be copied //| :param bitmap source_bitmap: Source bitmap that contains the graphical region to be copied
//| : param x1: Minimum x-value for rectangular bounding box to be copied from the source bitmap //| : param int x1: Minimum x-value for rectangular bounding box to be copied from the source bitmap
//| : param y1: Minimum y-value for rectangular bounding box to be copied from the source bitmap //| : param int y1: Minimum y-value for rectangular bounding box to be copied from the source bitmap
//| : param x2: Maximum x-value for rectangular bounding box to be copied from the source bitmap //| : param int x2: Maximum x-value for rectangular bounding box to be copied from the source bitmap
//| : param y2: Maximum y-value for rectangular bounding box to be copied from the source bitmap //| : param int y2: Maximum y-value for rectangular bounding box to be copied from the source bitmap
//| //| : param int skip_index: bitmap palette index in the source that will not be copied, set `None` to copy all pixels
//| ... //| ...
//| //|
STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args){ STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args){
enum {ARG_x, ARG_y, ARG_source, ARG_x1, ARG_y1, ARG_x2, ARG_y2, ARG_skip_index};
static const mp_arg_t allowed_args[] = {
{MP_QSTR_x, MP_ARG_REQUIRED | MP_ARG_INT},
{MP_QSTR_y, MP_ARG_REQUIRED | MP_ARG_INT},
{MP_QSTR_source, MP_ARG_REQUIRED | MP_ARG_OBJ},
{MP_QSTR_x1, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{MP_QSTR_y1, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{MP_QSTR_x2, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, // None convert to source->width
{MP_QSTR_y2, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, // None convert to source->height
{MP_QSTR_skip_index, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj=mp_const_none} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Consider improving the input checking. displayio_bitmap_t *self = MP_OBJ_TO_PTR(pos_args[0]); //*******
displayio_bitmap_t *self = MP_OBJ_TO_PTR(pos_args[0]); int16_t x = args[ARG_x].u_int;
int16_t x = mp_obj_get_int(pos_args[1]); int16_t y = args[ARG_y].u_int;
int16_t y = mp_obj_get_int(pos_args[2]);
displayio_bitmap_t *source = MP_OBJ_TO_PTR(pos_args[3]);
int16_t x1 = mp_obj_get_int(pos_args[4]);
int16_t y1 = mp_obj_get_int(pos_args[5]);
int16_t x2 = mp_obj_get_int(pos_args[6]);
int16_t y2 = mp_obj_get_int(pos_args[7]);
if ( (x<0) || (y<0) || (x > self-> width) || (y > self->height) ) { displayio_bitmap_t *source = MP_OBJ_TO_PTR(args[ARG_source].u_obj);
int16_t x1, y1;
// if x1 or y1 is None, then set as the zero-point of the bitmap
if ( args[ARG_x1].u_obj == mp_const_none ) {
x1 = 0;
} else {
x1 = mp_obj_get_int(args[ARG_x1].u_obj);
}
//int16_t y1;
if ( args[ARG_y1].u_obj == mp_const_none ) {
y1 = 0;
} else {
y1 = mp_obj_get_int(args[ARG_y1].u_obj);
}
int16_t x2, y2;
// if x2 or y2 is None, then set as the maximum size of the source bitmap
if ( args[ARG_x2].u_obj == mp_const_none ) {
x2 = source->width-1;
} else {
x2 = mp_obj_get_int(args[ARG_x2].u_obj);
}
//int16_t y2;
if ( args[ARG_y2].u_obj == mp_const_none ) {
y2 = source->height-1;
} else {
y2 = mp_obj_get_int(args[ARG_y2].u_obj);
}
// Check x,y are within self (target) bitmap boundary
if ( (x < 0) || (y < 0) || (x >= self->width) || (y >= self->height) ) {
mp_raise_ValueError(translate("(x,y): out of range of target bitmap")); mp_raise_ValueError(translate("(x,y): out of range of target bitmap"));
} }
if ( (x1 < 0) || (x1 > source->width) || // Check x1,y1,x2,y2 are within source bitmap boundary
(y1 < 0) || (y1 > source->height) || if ( (x1 < 0) || (x1 >= source->width) ||
(x2 < 0) || (x2 > source->width) || (y1 < 0) || (y1 >= source->height) ||
(y2 < 0) || (y2 > source->height) ) { (x2 < 0) || (x2 >= source->width) ||
(y2 < 0) || (y2 >= source->height) ) {
mp_raise_ValueError(translate("(x1,y1) or (x2,y2): out of range of source bitmap")); mp_raise_ValueError(translate("(x1,y1) or (x2,y2): out of range of source bitmap"));
} }
@ -223,13 +263,23 @@ STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_arg
y1=temp; y1=temp;
} }
common_hal_displayio_bitmap_blit(self, x, y, source, x1, y1, x2, y2); uint32_t skip_index;
bool skip_index_none; // flag whether skip_value was None
if (args[ARG_skip_index].u_obj == mp_const_none ) {
skip_index = 0;
skip_index_none = true;
} else {
skip_index = mp_obj_get_int(args[ARG_skip_index].u_obj);
skip_index_none = false;
}
common_hal_displayio_bitmap_blit(self, x, y, source, x1, y1, x2, y2, skip_index, skip_index_none);
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_bitmap_blit_obj, 4, displayio_bitmap_obj_blit);
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_bitmap_blit_obj, 8, displayio_bitmap_obj_blit); // `displayio_bitmap_obj_blit` requires at least 4 arguments
// requires 8 parameters
//| def fill(self, value: Any) -> Any: //| def fill(self, value: Any) -> Any:
//| """Fills the bitmap with the supplied palette index value.""" //| """Fills the bitmap with the supplied palette index value."""

View File

@ -41,7 +41,8 @@ uint16_t common_hal_displayio_bitmap_get_width(displayio_bitmap_t *self);
uint32_t common_hal_displayio_bitmap_get_bits_per_value(displayio_bitmap_t *self); uint32_t common_hal_displayio_bitmap_get_bits_per_value(displayio_bitmap_t *self);
void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y, uint32_t value); void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y, uint32_t value);
void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16_t y, displayio_bitmap_t *source, void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16_t y, displayio_bitmap_t *source,
int16_t x1, int16_t y1, int16_t x2, int16_t y2); int16_t x1, int16_t y1, int16_t x2, int16_t y2,
uint32_t skip_index, bool skip_index_none);
uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y); uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y);
void common_hal_displayio_bitmap_fill(displayio_bitmap_t *bitmap, uint32_t value); void common_hal_displayio_bitmap_fill(displayio_bitmap_t *bitmap, uint32_t value);

View File

@ -106,26 +106,24 @@ uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *self, int16_t
} }
void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16_t y, displayio_bitmap_t *source, void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16_t y, displayio_bitmap_t *source,
int16_t x1, int16_t y1, int16_t x2, int16_t y2) { int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint32_t skip_index, bool skip_index_none) {
// Copy complete "source" bitmap into "self" bitmap at location x,y in the "self" // Copy complete "source" bitmap into "self" bitmap at location x,y in the "self"
// Add a boolean to determine if all values are copied, or only if non-zero // Add a boolean to determine if all values are copied, or only if non-zero
// Default is copy all values, but for text glyphs this should copy only non-zero values //
// If skip_value is encountered in the source bitmap, it will not be copied.
// If skip_value is `None`, then all pixels are copied.
if (self->read_only) { if (self->read_only) {
mp_raise_RuntimeError(translate("Read-only object")); mp_raise_RuntimeError(translate("Read-only object"));
} }
// If this value is encountered in the source bitmap, it will not be copied (for text glyphs)
// This should be added as an optional parameter, and if it is `None`, then all pixels are copied
uint32_t skip_value=0;
// simplest version - use internal functions for get/set pixels // simplest version - use internal functions for get/set pixels
for (uint16_t i=0; i<= (x2-x1) ; i++) { for (int16_t i=0; i<= (x2-x1) ; i++) {
if ( (x+i >= 0) && (x+i < self->width) ) { if ( (x+i >= 0) && (x+i < self->width) ) {
for (uint16_t j=0; j<= (y2-y1) ; j++){ for (int16_t j=0; j<= (y2-y1) ; j++){
if ((y+j >= 0) && (y+j < self->height) ) { if ((y+j >= 0) && (y+j < self->height) ) {
uint32_t value = common_hal_displayio_bitmap_get_pixel(source, x1+i, y1+j); uint32_t value = common_hal_displayio_bitmap_get_pixel(source, x1+i, y1+j);
if ( (value != skip_value) ) { if ( (skip_index_none) || (value != skip_index) ) { // write if skip_value_none is True
common_hal_displayio_bitmap_set_pixel(self, x+i, y+j, value); common_hal_displayio_bitmap_set_pixel(self, x+i, y+j, value);
} }
} }