Skip to content

Commit 623ece2

Browse files
authored
Merge pull request #4429 from jepler/displayio-bitmap-memoryview
displayio.Bitmap: Make memoryview()able
2 parents 2589515 + 720d242 commit 623ece2

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

shared-bindings/displayio/Bitmap.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@
3737
#include "supervisor/shared/translate.h"
3838

3939
//| class Bitmap:
40-
//| """Stores values of a certain size in a 2D array"""
40+
//| """Stores values of a certain size in a 2D array
41+
//|
42+
//| 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
43+
//| per row is a multiple of 4, then the resulting memoryview will correspond directly with the bitmap's contents. Otherwise,
44+
//| the bitmap data is packed into the memoryview with unspecified padding.
45+
//|
46+
//| A read-only buffer can be used e.g., with `ulab.frombuffer` to efficiently create an array with the same content as a Bitmap;
47+
//| to move data efficiently from ulab back into a Bitmap, use `bitmaptools.arrayblit`.
48+
//| """
4149
//|
4250
//| def __init__(self, width: int, height: int, value_count: int) -> None:
4351
//| """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[] = {
300308
};
301309
STATIC MP_DEFINE_CONST_DICT(displayio_bitmap_locals_dict, displayio_bitmap_locals_dict_table);
302310

311+
// (the get_buffer protocol returns 0 for success, 1 for failure)
312+
STATIC mp_int_t bitmap_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
313+
displayio_bitmap_t *self = MP_OBJ_TO_PTR(self_in);
314+
return common_hal_displayio_bitmap_get_buffer(self, bufinfo, flags);
315+
}
316+
303317
const mp_obj_type_t displayio_bitmap_type = {
304318
{ &mp_type_type },
305319
.name = MP_QSTR_Bitmap,
306320
.make_new = displayio_bitmap_make_new,
307321
.subscr = bitmap_subscr,
308322
.locals_dict = (mp_obj_dict_t *)&displayio_bitmap_locals_dict,
323+
.buffer_p = { .get_buffer = bitmap_get_buffer },
309324
};

shared-bindings/displayio/Bitmap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16
4545
uint32_t skip_index, bool skip_index_none);
4646
uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y);
4747
void common_hal_displayio_bitmap_fill(displayio_bitmap_t *bitmap, uint32_t value);
48+
int common_hal_displayio_bitmap_get_buffer(displayio_bitmap_t *self, mp_buffer_info_t *bufinfo, mp_uint_t flags);
4849

4950
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_BITMAP_H

shared-module/displayio/Bitmap.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,23 @@ void common_hal_displayio_bitmap_fill(displayio_bitmap_t *self, uint32_t value)
271271
self->data[i] = word;
272272
}
273273
}
274+
275+
int common_hal_displayio_bitmap_get_buffer(displayio_bitmap_t *self, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
276+
if (flags & MP_BUFFER_WRITE) {
277+
return 1;
278+
}
279+
bufinfo->len = self->stride * self->height * sizeof(size_t);
280+
bufinfo->buf = self->data;
281+
switch (self->bits_per_value) {
282+
case 32:
283+
bufinfo->typecode = 'I';
284+
break;
285+
case 16:
286+
bufinfo->typecode = 'H';
287+
break;
288+
default:
289+
bufinfo->typecode = 'B';
290+
break;
291+
}
292+
return 0;
293+
}

0 commit comments

Comments
 (0)