From a66ef32da2fee0077f7917071294fa54b689e2e9 Mon Sep 17 00:00:00 2001 From: Kevin Matocha Date: Thu, 13 Aug 2020 17:03:06 -0500 Subject: [PATCH] Added inclusive indexing for x2,y2, fixed default value handling for x1,y1, added bitmap palette comparison --- locale/circuitpython.pot | 40 ++++++++++++++++++++++++-- shared-bindings/displayio/Bitmap.c | 45 ++++++++++++------------------ shared-module/displayio/Bitmap.c | 4 +-- 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index a1b286e2ed..3eb5b1ced2 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-08-14 09:36-0400\n" +"POT-Creation-Date: 2020-08-14 13:10-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -82,6 +82,8 @@ msgstr "" msgid "%q list must be a list" msgstr "" +#: shared-bindings/memorymonitor/AllocationAlarm 3.c +#: shared-bindings/memorymonitor/AllocationAlarm 4.c #: shared-bindings/memorymonitor/AllocationAlarm.c msgid "%q must be >= 0" msgstr "" @@ -89,6 +91,8 @@ msgstr "" #: shared-bindings/_bleio/CharacteristicBuffer.c #: shared-bindings/_bleio/PacketBuffer.c shared-bindings/displayio/Group.c #: shared-bindings/displayio/Shape.c +#: shared-bindings/memorymonitor/AllocationAlarm 3.c +#: shared-bindings/memorymonitor/AllocationAlarm 4.c #: shared-bindings/memorymonitor/AllocationAlarm.c #: shared-bindings/vectorio/Circle.c shared-bindings/vectorio/Rectangle.c msgid "%q must be >= 1" @@ -252,6 +256,14 @@ msgstr "" msgid "'yield' outside function" msgstr "" +#: shared-bindings/displayio/Bitmap.c +msgid "(x,y): out of range of target bitmap" +msgstr "" + +#: shared-bindings/displayio/Bitmap.c +msgid "(x1,y1) or (x2,y2): out of range of source bitmap" +msgstr "" + #: py/compile.c msgid "*x must be assignment target" msgstr "" @@ -306,6 +318,7 @@ msgstr "" msgid "All timers for this pin are in use" msgstr "" +#: ports/atmel-samd/common-hal/_pew/PewPew 2.c #: ports/atmel-samd/common-hal/_pew/PewPew.c #: ports/atmel-samd/common-hal/audioio/AudioOut.c #: ports/atmel-samd/common-hal/frequencyio/FrequencyIn.c @@ -322,7 +335,11 @@ msgstr "" msgid "Already advertising." msgstr "" +#: shared-module/memorymonitor/AllocationAlarm 3.c +#: shared-module/memorymonitor/AllocationAlarm 4.c #: shared-module/memorymonitor/AllocationAlarm.c +#: shared-module/memorymonitor/AllocationSize 3.c +#: shared-module/memorymonitor/AllocationSize 4.c #: shared-module/memorymonitor/AllocationSize.c msgid "Already running" msgstr "" @@ -362,6 +379,8 @@ msgstr "" msgid "At most %d %q may be specified (not %d)" msgstr "" +#: shared-module/memorymonitor/AllocationAlarm 3.c +#: shared-module/memorymonitor/AllocationAlarm 4.c #: shared-module/memorymonitor/AllocationAlarm.c #, c-format msgid "Attempt to allocate %d blocks" @@ -491,6 +510,12 @@ msgid "Can't set CCCD on local Characteristic" msgstr "" #: shared-bindings/displayio/Bitmap.c +msgid "Cannot blit: source palette too large." +msgstr "" + +#: shared-bindings/displayio/Bitmap.c +#: shared-bindings/memorymonitor/AllocationSize 3.c +#: shared-bindings/memorymonitor/AllocationSize 4.c #: shared-bindings/memorymonitor/AllocationSize.c #: shared-bindings/pulseio/PulseIn.c msgid "Cannot delete values" @@ -1014,6 +1039,7 @@ msgstr "" #: ports/atmel-samd/common-hal/busio/I2C.c #: ports/atmel-samd/common-hal/busio/SPI.c #: ports/atmel-samd/common-hal/busio/UART.c +#: ports/atmel-samd/common-hal/i2cperipheral/I2CPeripheral 2.c #: ports/atmel-samd/common-hal/i2cperipheral/I2CPeripheral.c #: ports/cxd56/common-hal/busio/I2C.c ports/cxd56/common-hal/busio/SPI.c #: ports/cxd56/common-hal/busio/UART.c ports/cxd56/common-hal/sdioio/SDCard.c @@ -1370,6 +1396,8 @@ msgstr "" msgid "Random number generation error" msgstr "" +#: shared-bindings/memorymonitor/AllocationSize 3.c +#: shared-bindings/memorymonitor/AllocationSize 4.c #: shared-bindings/memorymonitor/AllocationSize.c #: shared-bindings/pulseio/PulseIn.c msgid "Read-only" @@ -1462,6 +1490,8 @@ msgstr "" #: shared-bindings/displayio/Bitmap.c shared-bindings/displayio/Group.c #: shared-bindings/displayio/TileGrid.c +#: shared-bindings/memorymonitor/AllocationSize 3.c +#: shared-bindings/memorymonitor/AllocationSize 4.c #: shared-bindings/memorymonitor/AllocationSize.c #: shared-bindings/pulseio/PulseIn.c msgid "Slices not supported" @@ -1791,10 +1821,14 @@ msgstr "" msgid "address %08x is not aligned to %d bytes" msgstr "" +#: shared-bindings/i2cperipheral/I2CPeripheral 3.c +#: shared-bindings/i2cperipheral/I2CPeripheral 4.c #: shared-bindings/i2cperipheral/I2CPeripheral.c msgid "address out of bounds" msgstr "" +#: shared-bindings/i2cperipheral/I2CPeripheral 3.c +#: shared-bindings/i2cperipheral/I2CPeripheral 4.c #: shared-bindings/i2cperipheral/I2CPeripheral.c msgid "addresses is empty" msgstr "" @@ -1957,7 +1991,9 @@ msgstr "" msgid "can't assign to expression" msgstr "" -#: py/obj.c py/objint.c shared-bindings/i2cperipheral/I2CPeripheral.c +#: py/obj.c py/objint.c shared-bindings/i2cperipheral/I2CPeripheral 3.c +#: shared-bindings/i2cperipheral/I2CPeripheral 4.c +#: shared-bindings/i2cperipheral/I2CPeripheral.c msgid "can't convert %q to %q" msgstr "" diff --git a/shared-bindings/displayio/Bitmap.c b/shared-bindings/displayio/Bitmap.c index af9f6c3c8b..5852f1a703 100644 --- a/shared-bindings/displayio/Bitmap.c +++ b/shared-bindings/displayio/Bitmap.c @@ -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; } -//| def blit(self, x: int, y: int, source_bitmap: bitmap, x1: int, y1: int, x2: int, y2: int, skip_index: 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 //| (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 @@ -193,9 +193,9 @@ STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_arg 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_source_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ}, + {MP_QSTR_x1, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = 0} }, + {MP_QSTR_y1, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = 0} }, {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} }, @@ -203,51 +203,42 @@ STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_arg 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); - 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 y = args[ARG_y].u_int; 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); + // ensure that the target bitmap (self) has at least as many `bits_per_value` as the source + if (self->bits_per_value < source->bits_per_value) { + mp_raise_ValueError(translate("Cannot blit: source palette too large.")); } - //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; + + int16_t x1, y1, 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; + x2 = source->width; } 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; + y2 = source->height; } 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) ) { + if ( (x < 0) || (y < 0) || (x > self->width) || (y > self->height) ) { mp_raise_ValueError(translate("(x,y): out of range of target bitmap")); } // Check x1,y1,x2,y2 are within source bitmap boundary - if ( (x1 < 0) || (x1 >= source->width) || - (y1 < 0) || (y1 >= source->height) || - (x2 < 0) || (x2 >= source->width) || - (y2 < 0) || (y2 >= source->height) ) { + 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")); } @@ -272,7 +263,7 @@ STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_arg } 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); diff --git a/shared-module/displayio/Bitmap.c b/shared-module/displayio/Bitmap.c index dbcba6b583..7de9e9755e 100644 --- a/shared-module/displayio/Bitmap.c +++ b/shared-module/displayio/Bitmap.c @@ -118,9 +118,9 @@ void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16 } // simplest version - use internal functions for get/set pixels - for (int16_t i=0; i<= (x2-x1) ; i++) { + for (int16_t i=0; i < (x2-x1) ; i++) { if ( (x+i >= 0) && (x+i < self->width) ) { - for (int16_t j=0; j<= (y2-y1) ; j++){ + for (int16_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 ( (skip_index_none) || (value != skip_index) ) { // write if skip_value_none is True