diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2018-08-15 11:01:01 -0700 |
|---|---|---|
| committer | Scott Shawcroft <scott@tannewt.org> | 2018-08-31 12:31:52 -0700 |
| commit | 6697544cdf87d0df5b761d401ad6ffe3a8d752fc (patch) | |
| tree | 1526897a43894d045d9ba88c8cd6d6c314ce191f /shared-module/displayio/Bitmap.c | |
| parent | 9b98ad779468676c3d5f1efdc06b454aaed7c407 (diff) | |
Introduce displayio to render graphics to displays.
It's designed to minimize RAM footprint by using Sprites to
represent objects on the screen. The object model also facilitates
partial screen updating which reduces the bandwidth needed to display.
This is all handled in C. Python simply manipulates the objects with
the ability to synchronize to frame timing.
Diffstat (limited to 'shared-module/displayio/Bitmap.c')
| -rw-r--r-- | shared-module/displayio/Bitmap.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/shared-module/displayio/Bitmap.c b/shared-module/displayio/Bitmap.c new file mode 100644 index 000000000..7c6a2ee80 --- /dev/null +++ b/shared-module/displayio/Bitmap.c @@ -0,0 +1,89 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/displayio/Bitmap.h" + +#include <string.h> + +#include "py/runtime.h" + +void common_hal_displayio_bitmap_construct(displayio_bitmap_t *self, uint32_t width, + uint32_t height, uint32_t value_size) { + uint32_t row_width = width * value_size; + // word align + if (row_width % 32 != 0) { + self->stride = (row_width / 32 + 1); + } else { + self->stride = row_width / 32; + } + self->width = width; + self->height = height; + self->data = m_malloc(self->stride * height * sizeof(uint32_t), false); + + self->bits_per_value = value_size; + + self->x_shift = 0; + uint32_t power_of_two = 1; + while (power_of_two < 32 / value_size ) { + self->x_shift++; + power_of_two <<= 1; + } + self->x_mask = (1 << self->x_shift) - 1; // Used as a modulus on the x value + self->bitmask = (1 << value_size) - 1; +} + +void common_hal_displayio_bitmap_load_row(displayio_bitmap_t *self, uint16_t y, uint8_t* data, uint16_t len) { + if (len != self->stride * 4) { + mp_raise_ValueError(translate("row must be packed and word aligned")); + } + uint32_t* row_value = self->data + (y * self->stride); + // Do the memcpy ourselves since we may want to flip endianness. + for (uint32_t i = 0; i < self->stride; i++) { + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wcast-align" + uint32_t value = ((uint32_t *)data)[i]; + #pragma GCC diagnostic pop + if (self->bits_per_value < 16) { + value = ((value >> 24) & 0xff) | + ((value << 8) & 0xff0000) | + ((value >> 8) & 0xff00) | + ((value << 24) & 0xff000000); + } + *row_value = value; + row_value++; + } +} +uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *self, int16_t x, int16_t y) { + int32_t row_start = y * self->stride; + if (self->bits_per_value < 8) { + uint32_t word = self->data[row_start + (x >> self->x_shift)]; + + return (word >> (32 - ((x & self->x_mask) + 1) * self->bits_per_value)) & self->bitmask; + } else { + uint32_t bytes_per_value = self->bits_per_value / 8; + return self->data[row_start + x * bytes_per_value]; + } +} |
