Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jepler committed Oct 29, 2021
1 parent 3fd396f commit 9f25ff3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
8 changes: 6 additions & 2 deletions shared-module/bitmaptools/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ void common_hal_bitmaptools_dither(displayio_bitmap_t *dest_bitmap, displayio_bi
int32_t row2[width];
int32_t row3[width];
int32_t *rows[3] = {row1, row2, row3};
uint32_t out[width];

fill_row(source_bitmap, pixel_shader, rows[0], 0);
fill_row(source_bitmap, pixel_shader, rows[1], 1);
Expand All @@ -684,7 +685,8 @@ void common_hal_bitmaptools_dither(displayio_bitmap_t *dest_bitmap, displayio_bi
for (int x = 0; x < width; x++) {
int32_t pixel_in = rows[0][x];
bool pixel_out = pixel_in >= 128;
displayio_bitmap_write_pixel(dest_bitmap, x, y, pixel_out ? max_pixel : 0);
out[x] = pixel_out ? max_pixel : 0;

int err = pixel_in - (pixel_out ? 255 : 0);

for (int i = 0; i < info->count; i++) {
Expand All @@ -697,6 +699,7 @@ void common_hal_bitmaptools_dither(displayio_bitmap_t *dest_bitmap, displayio_bi
rows[dy][x1] = ((info->terms[i].dl * err) >> 8) + rows[dy][x1];
}
}
displayio_bitmap_write_pixels(dest_bitmap, out, 0, y, width);

int32_t *tmp = rows[0];
rows[0] = rows[1];
Expand All @@ -713,7 +716,7 @@ void common_hal_bitmaptools_dither(displayio_bitmap_t *dest_bitmap, displayio_bi
for (int x = width; x--;) {
int32_t pixel_in = rows[0][x];
bool pixel_out = pixel_in >= 128;
displayio_bitmap_write_pixel(dest_bitmap, x, y, pixel_out ? max_pixel : 0);
out[x] = pixel_out ? max_pixel : 0;
int err = pixel_in - (pixel_out ? 255 : 0);

for (int i = 0; i < info->count; i++) {
Expand All @@ -726,6 +729,7 @@ void common_hal_bitmaptools_dither(displayio_bitmap_t *dest_bitmap, displayio_bi
rows[dy][x1] = ((info->terms[i].dl * err) >> 8) + rows[dy][x1];
}
}
displayio_bitmap_write_pixels(dest_bitmap, out, 0, y, width);

tmp = rows[0];
rows[0] = rows[1];
Expand Down
65 changes: 65 additions & 0 deletions shared-module/displayio/Bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,71 @@ void common_hal_displayio_bitmap_get_pixels(displayio_bitmap_t *self, uint32_t *
#undef BIG_PIXEL_LOOP
}

void displayio_bitmap_write_pixels(displayio_bitmap_t *self, const uint32_t *pixels, int16_t x, int16_t y, size_t n) {
// Writes the pixels directly to the bitmap
// Must update the dirty area separately
switch (self->bits_per_value) {

#define SMALL_PIXEL_LOOP(sz) do { \
size_t *data = &self->data[y * self->stride + x / SIZE_BIT / sz]; \
/* First, put some initial pixels from an incompletely-used bytes */ \
while (x % SIZE_BIT / sz) { \
displayio_bitmap_write_pixel(self, x++, y, *pixels++); \
n--; \
} \
while (n > SIZE_BIT / sz) { \
size_t d = 0; \
for (size_t i = 0; i < SIZE_BIT; i++) { \
d <<= sz; \
d |= (*pixels++ & ((1 << sz) - 1)); \
} \
*data++ = d; \
} \
/* Put any straggling pixels */ \
while (n--) { \
displayio_bitmap_write_pixel(self, x++, y, *pixels++); \
} \
} while (0)
case 1:
SMALL_PIXEL_LOOP(1);
break;

case 2:
SMALL_PIXEL_LOOP(2);
break;

case 4:
SMALL_PIXEL_LOOP(4);
break;

#undef SMALL_PIXEL_LOOP

#define BIG_PIXEL_LOOP(type) do { \
type *data = ((type *)&self->data[y * self->stride]) + x; \
while (n--) { \
*data++ = *pixels++; \
} \
} while (0)

case 8:
BIG_PIXEL_LOOP(uint8_t);
break;

case 16:
BIG_PIXEL_LOOP(uint16_t);
break;

case 32:
BIG_PIXEL_LOOP(uint32_t);
break;

default:
abort();
}

#undef BIG_PIXEL_LOOP
}

void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, const displayio_area_t *dirty_area) {
if (self->read_only) {
mp_raise_RuntimeError(translate("Read-only object"));
Expand Down
1 change: 1 addition & 0 deletions shared-module/displayio/Bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ void displayio_bitmap_finish_refresh(displayio_bitmap_t *self);
displayio_area_t *displayio_bitmap_get_refresh_areas(displayio_bitmap_t *self, displayio_area_t *tail);
void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, const displayio_area_t *area);
void displayio_bitmap_write_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value);
void displayio_bitmap_write_pixels(displayio_bitmap_t *self, const uint32_t *pixels, int16_t x, int16_t y, size_t n);

#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_BITMAP_H

0 comments on commit 9f25ff3

Please sign in to comment.