From 9fdd8043650ad97fdd286719ba06cc392846f093 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 18 Apr 2022 20:31:47 -0500 Subject: [PATCH] enforce new bitmap same size as previous --- locale/circuitpython.pot | 6 ++- shared-bindings/displayio/TileGrid.c | 59 +++++++--------------------- 2 files changed, 19 insertions(+), 46 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 3eb9b8d80b..5a8248d509 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -1599,6 +1599,10 @@ msgstr "" msgid "Name too long" msgstr "" +#: shared-bindings/displayio/TileGrid.c +msgid "New bitmap must be same size as old bitmap" +msgstr "" + #: ports/espressif/common-hal/_bleio/__init__.c msgid "Nimble out of memory" msgstr "" @@ -3024,7 +3028,7 @@ msgstr "" msgid "complex values not supported" msgstr "" -#: extmod/moduzlib.c shared-module/zlib/DecompIO.c +#: extmod/moduzlib.c msgid "compression header" msgstr "" diff --git a/shared-bindings/displayio/TileGrid.c b/shared-bindings/displayio/TileGrid.c index 8e47a344db..32a8796a5a 100644 --- a/shared-bindings/displayio/TileGrid.c +++ b/shared-bindings/displayio/TileGrid.c @@ -439,78 +439,47 @@ STATIC mp_obj_t displayio_tilegrid_obj_set_bitmap(mp_obj_t self_in, mp_obj_t bit mp_raise_TypeError(translate("bitmap must be displayio.Bitmap, displayio.Shape, or displayio.OnDiskBitmap")); } - /*if (bitmap->width % self->tile_width != 0) { - mp_raise_ValueError(translate("Tile width must exactly divide bitmap width")); - } - if (bitmap->height % self->tile_height != 0) { - mp_raise_ValueError(translate("Tile height must exactly divide bitmap height")); - }*/ - // enforce_bitmap_size(self_in, bitmap); - - uint16_t bitmap_width; - uint16_t bitmap_height; + uint16_t new_bitmap_width; + uint16_t new_bitmap_height; mp_obj_t native = mp_obj_cast_to_native_base(bitmap, &displayio_shape_type); if (native != MP_OBJ_NULL) { displayio_shape_t *bmp = MP_OBJ_TO_PTR(native); - bitmap_width = bmp->width; - bitmap_height = bmp->height; + new_bitmap_width = bmp->width; + new_bitmap_height = bmp->height; } else if (mp_obj_is_type(bitmap, &displayio_bitmap_type)) { displayio_bitmap_t *bmp = MP_OBJ_TO_PTR(bitmap); native = bitmap; - bitmap_width = bmp->width; - bitmap_height = bmp->height; + new_bitmap_width = bmp->width; + new_bitmap_height = bmp->height; } else if (mp_obj_is_type(bitmap, &displayio_ondiskbitmap_type)) { displayio_ondiskbitmap_t *bmp = MP_OBJ_TO_PTR(bitmap); native = bitmap; - bitmap_width = bmp->width; - bitmap_height = bmp->height; + new_bitmap_width = bmp->width; + new_bitmap_height = bmp->height; } else { mp_raise_TypeError_varg(translate("unsupported %q type"), MP_QSTR_bitmap); } - uint16_t tile_width = self->tile_width; - uint16_t tile_height = self->tile_height; mp_obj_t old_native = mp_obj_cast_to_native_base(self->bitmap, &displayio_shape_type); if (old_native != MP_OBJ_NULL) { displayio_shape_t *old_bmp = MP_OBJ_TO_PTR(old_native); - if (tile_width == old_bmp->width) { - self->tile_width = bitmap_width; + if (old_bmp->width != new_bitmap_width || old_bmp->height != new_bitmap_height) { + mp_raise_ValueError(translate("New bitmap must be same size as old bitmap")); } - if (tile_height == old_bmp->width) { - self->tile_height = bitmap_height; - } - } else if (mp_obj_is_type(self->bitmap, &displayio_bitmap_type)) { displayio_bitmap_t *old_bmp = MP_OBJ_TO_PTR(self->bitmap); old_native = self->bitmap; - if (tile_width == old_bmp->width) { - self->tile_width = bitmap_width; + if (old_bmp->width != new_bitmap_width || old_bmp->height != new_bitmap_height) { + mp_raise_ValueError(translate("New bitmap must be same size as old bitmap")); } - if (tile_height == old_bmp->width) { - self->tile_height = bitmap_height; - } - } else if (mp_obj_is_type(self->bitmap, &displayio_ondiskbitmap_type)) { displayio_ondiskbitmap_t *old_bmp = MP_OBJ_TO_PTR(self->bitmap); old_native = self->bitmap; - if (tile_width == old_bmp->width) { - self->tile_width = bitmap_width; - } - if (tile_height == old_bmp->width) { - self->tile_height = bitmap_height; + if (old_bmp->width != new_bitmap_width || old_bmp->height != new_bitmap_height) { + mp_raise_ValueError(translate("New bitmap must be same size as old bitmap")); } } - - - if (bitmap_width % tile_width != 0) { - mp_raise_ValueError(translate("Tile width must exactly divide bitmap width")); - } - if (bitmap_height % tile_height != 0) { - mp_raise_ValueError(translate("Tile height must exactly divide bitmap height")); - } - - common_hal_displayio_tilegrid_set_bitmap(self, bitmap); return mp_const_none;