Skip to content

Commit cea55cd

Browse files
committed
enforce bitmap and tile_size in set_bitmap
1 parent 948040f commit cea55cd

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

shared-bindings/displayio/TileGrid.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,52 @@ STATIC mp_obj_t displayio_tilegrid_make_new(const mp_obj_type_t *type, size_t n_
137137
return MP_OBJ_FROM_PTR(self);
138138
}
139139

140+
140141
// Helper to ensure we have the native super class instead of a subclass.
141142
static displayio_tilegrid_t *native_tilegrid(mp_obj_t tilegrid_obj) {
142143
mp_obj_t native_tilegrid = mp_obj_cast_to_native_base(tilegrid_obj, &displayio_tilegrid_type);
143144
mp_obj_assert_native_inited(native_tilegrid);
144145
return MP_OBJ_TO_PTR(native_tilegrid);
145146
}
147+
148+
static void enforce_bitmap_size(mp_obj_t self_in, mp_obj_t bitmap) {
149+
displayio_tilegrid_t *self = native_tilegrid(self_in);
150+
uint16_t bitmap_width;
151+
uint16_t bitmap_height;
152+
mp_obj_t native = mp_obj_cast_to_native_base(bitmap, &displayio_shape_type);
153+
if (native != MP_OBJ_NULL) {
154+
displayio_shape_t *bmp = MP_OBJ_TO_PTR(native);
155+
bitmap_width = bmp->width;
156+
bitmap_height = bmp->height;
157+
} else if (mp_obj_is_type(bitmap, &displayio_bitmap_type)) {
158+
displayio_bitmap_t *bmp = MP_OBJ_TO_PTR(bitmap);
159+
native = bitmap;
160+
bitmap_width = bmp->width;
161+
bitmap_height = bmp->height;
162+
} else if (mp_obj_is_type(bitmap, &displayio_ondiskbitmap_type)) {
163+
displayio_ondiskbitmap_t *bmp = MP_OBJ_TO_PTR(bitmap);
164+
native = bitmap;
165+
bitmap_width = bmp->width;
166+
bitmap_height = bmp->height;
167+
} else {
168+
mp_raise_TypeError_varg(translate("unsupported %q type"), MP_QSTR_bitmap);
169+
}
170+
uint16_t tile_width = self->tile_width;
171+
if (tile_width == 0) {
172+
tile_width = bitmap_width;
173+
}
174+
uint16_t tile_height = self->tile_height;
175+
if (tile_height == 0) {
176+
tile_height = bitmap_height;
177+
}
178+
if (bitmap_width % tile_width != 0) {
179+
mp_raise_ValueError(translate("Tile width must exactly divide bitmap width"));
180+
}
181+
if (bitmap_height % tile_height != 0) {
182+
mp_raise_ValueError(translate("Tile height must exactly divide bitmap height"));
183+
}
184+
}
185+
146186
//| hidden: bool
147187
//| """True when the TileGrid is hidden. This may be False even when a part of a hidden Group."""
148188
//|
@@ -397,6 +437,14 @@ STATIC mp_obj_t displayio_tilegrid_obj_set_bitmap(mp_obj_t self_in, mp_obj_t bit
397437
mp_raise_TypeError(translate("bitmap must be displayio.Bitmap, displayio.Shape, or displayio.OnDiskBitmap"));
398438
}
399439

440+
/*if (bitmap->width % self->tile_width != 0) {
441+
mp_raise_ValueError(translate("Tile width must exactly divide bitmap width"));
442+
}
443+
if (bitmap->height % self->tile_height != 0) {
444+
mp_raise_ValueError(translate("Tile height must exactly divide bitmap height"));
445+
}*/
446+
enforce_bitmap_size(self_in, bitmap);
447+
400448
common_hal_displayio_tilegrid_set_bitmap(self, bitmap);
401449

402450
return mp_const_none;

0 commit comments

Comments
 (0)