diff options
| author | Margaret Matocha <ksmatocha@gmail.com> | 2020-08-10 11:04:56 -0500 |
|---|---|---|
| committer | Kevin Matocha <kmatocha@icloud.com> | 2020-08-14 12:59:48 -0500 |
| commit | 4ba9ff892c2176d4aeefe1a8287cdb899acf5da1 (patch) | |
| tree | 9c76a37002e58cc638950decde0d5309d16575cd | |
| parent | 98469322b7d35748f0f2cb03cc5bd4d470ea6339 (diff) | |
Added bitmap.blit function for copying slices of bitmaps
| -rw-r--r-- | shared-bindings/displayio/Bitmap.c | 62 | ||||
| -rw-r--r-- | shared-bindings/displayio/Bitmap.h | 2 | ||||
| -rw-r--r-- | shared-module/displayio/Bitmap.c | 31 |
3 files changed, 94 insertions, 1 deletions
diff --git a/shared-bindings/displayio/Bitmap.c b/shared-bindings/displayio/Bitmap.c index 0a45bbdfd..56c6007f0 100644 --- a/shared-bindings/displayio/Bitmap.c +++ b/shared-bindings/displayio/Bitmap.c @@ -172,7 +172,66 @@ STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t val return mp_const_none; } -//| def fill(self, value: int) -> None: +//| def blit(self, x: int, y: int, source_bitmap: bitmap, x1: int, y1: int, x2: int, y2:int) -> Any: +//| """Inserts the source_bitmap region defined by rectangular boundaries +//| (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 +//| corner will be placed +//| :param int y: Vertical pixel location in bitmap where source_bitmap upper-left +//| corner will be placed +//| :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 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 y2: Maximum y-value for rectangular bounding box to be copied from the source bitmap +//| +//| ... +//| + +STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args){ + + // Consider improving the input checking. + + displayio_bitmap_t *self = MP_OBJ_TO_PTR(pos_args[0]); + int16_t x = mp_obj_get_int(pos_args[1]); + 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) ) { + mp_raise_ValueError(translate("(x,y): out of range of target bitmap")); + } + if ( (x1 < 0) || (x1 > source->width) || + (y1 < 0) || (y1 > 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")); + } + + // Ensure x1 < x2 and y1 < y2 + if (x1 > x2) { + int16_t temp=x2; + x2=x1; + x1=temp; + } + if (y1 > y2) { + int16_t temp=y2; + y2=y1; + y1=temp; + } + + common_hal_displayio_bitmap_blit(self, x, y, source, x1, y1, x2, y2); + + return mp_const_none; +} + +MP_DEFINE_CONST_FUN_OBJ_KW(displayio_bitmap_blit_obj, 8, displayio_bitmap_obj_blit); +// requires 8 parameters + +//| def fill(self, value: Any) -> Any: //| """Fills the bitmap with the supplied palette index value.""" //| ... //| @@ -192,6 +251,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(displayio_bitmap_fill_obj, displayio_bitmap_obj_fill); STATIC const mp_rom_map_elem_t displayio_bitmap_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_bitmap_height_obj) }, { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_bitmap_width_obj) }, + { MP_ROM_QSTR(MP_QSTR_blit), MP_ROM_PTR(&displayio_bitmap_blit_obj) }, { MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&displayio_bitmap_fill_obj) }, }; diff --git a/shared-bindings/displayio/Bitmap.h b/shared-bindings/displayio/Bitmap.h index 46c337329..8225af6c6 100644 --- a/shared-bindings/displayio/Bitmap.h +++ b/shared-bindings/displayio/Bitmap.h @@ -40,6 +40,8 @@ uint16_t common_hal_displayio_bitmap_get_height(displayio_bitmap_t *self); 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); 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, + int16_t x1, int16_t y1, int16_t x2, int16_t y2); 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); diff --git a/shared-module/displayio/Bitmap.c b/shared-module/displayio/Bitmap.c index 8bcda6086..dde346ad3 100644 --- a/shared-module/displayio/Bitmap.c +++ b/shared-module/displayio/Bitmap.c @@ -105,6 +105,37 @@ uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *self, int16_t return 0; } +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) { + // 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 + // Default is copy all values, but for text glyphs this should copy only non-zero values + + if (self->read_only) { + 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 + for (uint16_t i=0; i<= (x2-x1) ; i++) { + if ( (x+i >= 0) && (x+i < self->width) ) { + for (uint16_t j=0; j<= (y2-y1) ; j++){ + if ((y+j >= 0) && (y+j < self->height) ) { + uint32_t value = common_hal_displayio_bitmap_get_pixel(source, x1+i, y1+j); + if ( (value != skip_value) ) { + common_hal_displayio_bitmap_set_pixel(self, x+i, y+j, value); + } + } + } + } + } + +} + + void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value) { if (self->read_only) { mp_raise_RuntimeError(translate("Read-only object")); |
