Skip to content

Commit 64b1d00

Browse files
committed
Allow a Bitmap to be constructed from a buffer (in C anyway)
.. so that Camera.take() can return one without copying
1 parent 258f726 commit 64b1d00

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

shared-bindings/displayio/Bitmap.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ extern const mp_obj_type_t displayio_bitmap_type;
3333

3434
void common_hal_displayio_bitmap_construct(displayio_bitmap_t *self, uint32_t width,
3535
uint32_t height, uint32_t bits_per_value);
36+
void common_hal_displayio_bitmap_construct_from_buffer(displayio_bitmap_t *self, uint32_t width,
37+
uint32_t height, uint32_t bits_per_value, uint32_t *data, bool read_only);
3638

3739
void common_hal_displayio_bitmap_load_row(displayio_bitmap_t *self, uint16_t y, uint8_t *data,
3840
uint16_t len);

shared-module/displayio/Bitmap.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,29 @@
3030

3131
#include "py/runtime.h"
3232

33-
void common_hal_displayio_bitmap_construct(displayio_bitmap_t *self, uint32_t width,
34-
uint32_t height, uint32_t bits_per_value) {
33+
enum { align_bits = 8 * sizeof(uint32_t) };
34+
35+
static int stride(uint32_t width, uint32_t bits_per_value) {
3536
uint32_t row_width = width * bits_per_value;
3637
// align to uint32_t
37-
uint8_t align_bits = 8 * sizeof(uint32_t);
38-
if (row_width % align_bits != 0) {
39-
self->stride = (row_width / align_bits + 1);
40-
} else {
41-
self->stride = row_width / align_bits;
42-
}
38+
return (row_width + align_bits - 1) / align_bits;
39+
}
40+
41+
void common_hal_displayio_bitmap_construct(displayio_bitmap_t *self, uint32_t width,
42+
uint32_t height, uint32_t bits_per_value) {
43+
common_hal_displayio_bitmap_construct_from_buffer(self, width, height, bits_per_value, NULL, false);
44+
}
45+
46+
void common_hal_displayio_bitmap_construct_from_buffer(displayio_bitmap_t *self, uint32_t width,
47+
uint32_t height, uint32_t bits_per_value, uint32_t *data, bool read_only) {
4348
self->width = width;
4449
self->height = height;
45-
self->data = m_malloc(self->stride * height * sizeof(uint32_t), false);
46-
self->read_only = false;
50+
self->stride = stride(width, bits_per_value);
51+
if (!data) {
52+
data = m_malloc(self->stride * height * sizeof(uint32_t), false);
53+
}
54+
self->data = data;
55+
self->read_only = read_only;
4756
self->bits_per_value = bits_per_value;
4857

4958
if (bits_per_value > 8 && bits_per_value != 16 && bits_per_value != 32) {
@@ -70,6 +79,7 @@ void common_hal_displayio_bitmap_construct(displayio_bitmap_t *self, uint32_t wi
7079
self->dirty_area.y2 = height;
7180
}
7281

82+
7383
uint16_t common_hal_displayio_bitmap_get_height(displayio_bitmap_t *self) {
7484
return self->height;
7585
}

0 commit comments

Comments
 (0)