diff options
| author | Jeff Epler <jepler@gmail.com> | 2021-03-17 16:11:04 -0500 |
|---|---|---|
| committer | Jeff Epler <jepler@gmail.com> | 2021-03-17 16:26:45 -0500 |
| commit | c157ada90c3dabbb005b431f98e5c81e57e8a4bc (patch) | |
| tree | 9ebfe37403d6988980e8b3aa7893102d2cfaa705 /shared-bindings | |
| parent | bfc8c89536e12dba0cc9adc0065819d730bb0983 (diff) | |
displayio.Bitmap: Make memoryview()able
Diffstat (limited to 'shared-bindings')
| -rw-r--r-- | shared-bindings/displayio/Bitmap.c | 17 | ||||
| -rw-r--r-- | shared-bindings/displayio/Bitmap.h | 1 |
2 files changed, 17 insertions, 1 deletions
diff --git a/shared-bindings/displayio/Bitmap.c b/shared-bindings/displayio/Bitmap.c index f906be2f9..491b4e6dd 100644 --- a/shared-bindings/displayio/Bitmap.c +++ b/shared-bindings/displayio/Bitmap.c @@ -37,7 +37,15 @@ #include "supervisor/shared/translate.h" //| class Bitmap: -//| """Stores values of a certain size in a 2D array""" +//| """Stores values of a certain size in a 2D array +//| +//| Bitmaps can be treated as read-only buffers. If the number of bits in a pixel is 8, 16, or 32; and the number of bytes +//| per row is a multiple of 4, then the resulting memoryview will correspond directly with the bitmap's contents. Otherwise, +//| the bitmap data is packed into the memoryview with unspecified padding. +//| +//| A read-only buffer can be used e.g., with `ulab.fromarray` to efficiently create an array with the same content as a Bitmap; +//| to move data efficiently from ulab back into a Bitmap, use `bitmaptools.arrayblit`. +//| """ //| //| def __init__(self, width: int, height: int, value_count: int) -> None: //| """Create a Bitmap object with the given fixed size. Each pixel stores a value that is used to @@ -300,10 +308,17 @@ STATIC const mp_rom_map_elem_t displayio_bitmap_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(displayio_bitmap_locals_dict, displayio_bitmap_locals_dict_table); +// (the get_buffer protocol returns 0 for success, 1 for failure) +STATIC mp_int_t bitmap_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) { + displayio_bitmap_t *self = MP_OBJ_TO_PTR(self_in); + return common_hal_displayio_bitmap_get_buffer(self, bufinfo, flags); +} + const mp_obj_type_t displayio_bitmap_type = { { &mp_type_type }, .name = MP_QSTR_Bitmap, .make_new = displayio_bitmap_make_new, .subscr = bitmap_subscr, .locals_dict = (mp_obj_dict_t *)&displayio_bitmap_locals_dict, + .buffer_p = { .get_buffer = bitmap_get_buffer }, }; diff --git a/shared-bindings/displayio/Bitmap.h b/shared-bindings/displayio/Bitmap.h index 1f74c31fc..458047510 100644 --- a/shared-bindings/displayio/Bitmap.h +++ b/shared-bindings/displayio/Bitmap.h @@ -45,5 +45,6 @@ void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16 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); void common_hal_displayio_bitmap_fill(displayio_bitmap_t *bitmap, uint32_t value); +int common_hal_displayio_bitmap_get_buffer(displayio_bitmap_t *self, mp_buffer_info_t *bufinfo, mp_uint_t flags); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_BITMAP_H |
