Skip to content

Commit d87bfaf

Browse files
Add random dithering to ColorConverter
1 parent b954b2f commit d87bfaf

File tree

9 files changed

+164
-39
lines changed

9 files changed

+164
-39
lines changed

shared-bindings/displayio/Display.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,33 @@ const mp_obj_property_t displayio_display_auto_brightness_obj = {
346346
(mp_obj_t)&mp_const_none_obj},
347347
};
348348

349+
//| .. attribute:: dither
350+
//|
351+
//| True when the display is dithered
352+
//|
353+
STATIC mp_obj_t displayio_display_obj_get_dither(mp_obj_t self_in) {
354+
displayio_display_obj_t *self = native_display(self_in);
355+
return mp_obj_new_bool(common_hal_displayio_display_get_dither(self));
356+
}
357+
MP_DEFINE_CONST_FUN_OBJ_1(displayio_display_get_dither_obj, displayio_display_obj_get_dither);
358+
359+
STATIC mp_obj_t displayio_display_obj_set_dither(mp_obj_t self_in, mp_obj_t dither) {
360+
displayio_display_obj_t *self = native_display(self_in);
361+
362+
common_hal_displayio_display_set_dither(self, mp_obj_is_true(dither));
363+
364+
return mp_const_none;
365+
}
366+
MP_DEFINE_CONST_FUN_OBJ_2(displayio_display_set_dither_obj, displayio_display_obj_set_dither);
367+
368+
const mp_obj_property_t displayio_display_dither_obj = {
369+
.base.type = &mp_type_property,
370+
.proxy = {(mp_obj_t)&displayio_display_get_dither_obj,
371+
(mp_obj_t)&displayio_display_set_dither_obj,
372+
(mp_obj_t)&mp_const_none_obj},
373+
};
374+
375+
349376
//| .. attribute:: width
350377
//|
351378
//| Gets the width of the board
@@ -487,6 +514,7 @@ STATIC const mp_rom_map_elem_t displayio_display_locals_dict_table[] = {
487514

488515
{ MP_ROM_QSTR(MP_QSTR_brightness), MP_ROM_PTR(&displayio_display_brightness_obj) },
489516
{ MP_ROM_QSTR(MP_QSTR_auto_brightness), MP_ROM_PTR(&displayio_display_auto_brightness_obj) },
517+
{ MP_ROM_QSTR(MP_QSTR_dither), MP_ROM_PTR(&displayio_display_dither_obj) },
490518

491519
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_display_width_obj) },
492520
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_display_height_obj) },

shared-bindings/displayio/Display.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ uint16_t common_hal_displayio_display_get_rotation(displayio_display_obj_t* self
6262
bool common_hal_displayio_display_get_auto_brightness(displayio_display_obj_t* self);
6363
void common_hal_displayio_display_set_auto_brightness(displayio_display_obj_t* self, bool auto_brightness);
6464

65+
bool common_hal_displayio_display_get_dither(displayio_display_obj_t* self);
66+
void common_hal_displayio_display_set_dither(displayio_display_obj_t* self, bool dither);
67+
6568
mp_float_t common_hal_displayio_display_get_brightness(displayio_display_obj_t* self);
6669
bool common_hal_displayio_display_set_brightness(displayio_display_obj_t* self, mp_float_t brightness);
6770

shared-module/displayio/ColorConverter.c

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@
2828

2929
#include "py/misc.h"
3030

31+
uint32_t displayio_colorconverter_dither_noise_1 (uint32_t n)
32+
{
33+
n = (n >> 13) ^ n;
34+
int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
35+
return (uint32_t) (((float)nn / (1073741824.0f*2)) * 255);
36+
}
37+
38+
uint32_t displayio_colorconverter_dither_noise_2(uint32_t x, uint32_t y) {
39+
return displayio_colorconverter_dither_noise_1(x + y * 0xFFFF);
40+
}
41+
3142
void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self) {
3243
}
3344

@@ -96,34 +107,72 @@ void displayio_colorconverter_compute_tricolor(const _displayio_colorspace_t* co
96107
}
97108
}
98109

99-
bool displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color) {
110+
void common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color) {
111+
displayio_input_pixel_t input_pixel;
112+
input_pixel.pixel = input_color;
113+
input_pixel.x = input_pixel.y = input_pixel.tile = input_pixel.tile_x = input_pixel.tile_y = 0;
114+
115+
displayio_output_pixel_t output_pixel;
116+
output_pixel.pixel = 0;
117+
output_pixel.opaque = false;
118+
119+
displayio_colorconverter_convert(self, colorspace, &input_pixel, &output_pixel);
120+
121+
(*output_color) = output_pixel.pixel;
122+
}
123+
124+
bool displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, const displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color) {
125+
uint32_t pixel = input_pixel->pixel;
126+
127+
if (colorspace->dither){
128+
uint8_t randr = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x,input_pixel->tile_y));
129+
uint8_t randg = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x+33,input_pixel->tile_y));
130+
uint8_t randb = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x,input_pixel->tile_y+33));
131+
132+
uint32_t r8 = (pixel >> 16);
133+
uint32_t g8 = (pixel >> 8) & 0xff;
134+
uint32_t b8 = pixel & 0xff;
135+
136+
if (colorspace->depth == 16) {
137+
b8 = MIN(255,b8 + (randb&0x07));
138+
r8 = MIN(255,r8 + (randr&0x07));
139+
g8 = MIN(255,g8 + (randg&0x03));
140+
} else {
141+
int bitmask = 0xFF >> colorspace->depth;
142+
b8 = MIN(255,b8 + (randb&bitmask));
143+
r8 = MIN(255,r8 + (randr&bitmask));
144+
g8 = MIN(255,g8 + (randg&bitmask));
145+
}
146+
pixel = r8 << 16 | g8 << 8 | b8;
147+
}
148+
100149
if (colorspace->depth == 16) {
101-
*output_color = displayio_colorconverter_compute_rgb565(input_color);
150+
output_color->pixel = displayio_colorconverter_compute_rgb565(pixel);
151+
output_color->opaque = true;
102152
return true;
103153
} else if (colorspace->tricolor) {
104-
uint8_t luma = displayio_colorconverter_compute_luma(input_color);
105-
*output_color = luma >> (8 - colorspace->depth);
106-
if (displayio_colorconverter_compute_chroma(input_color) <= 16) {
154+
uint8_t luma = displayio_colorconverter_compute_luma(pixel);
155+
output_color->pixel = luma >> (8 - colorspace->depth);
156+
if (displayio_colorconverter_compute_chroma(pixel) <= 16) {
107157
if (!colorspace->grayscale) {
108-
*output_color = 0;
158+
output_color->pixel = 0;
109159
}
160+
output_color->opaque = true;
110161
return true;
111162
}
112-
uint8_t pixel_hue = displayio_colorconverter_compute_hue(input_color);
113-
displayio_colorconverter_compute_tricolor(colorspace, pixel_hue, luma, output_color);
163+
uint8_t pixel_hue = displayio_colorconverter_compute_hue(pixel);
164+
displayio_colorconverter_compute_tricolor(colorspace, pixel_hue, luma, &output_color->pixel);
114165
return true;
115-
} else if (colorspace->grayscale && colorspace->depth <= 8) {
116-
uint8_t luma = displayio_colorconverter_compute_luma(input_color);
117-
*output_color = luma >> (8 - colorspace->depth);
166+
} else if (colorspace->grayscale && colorspace->depth <= 8) {
167+
uint8_t luma = displayio_colorconverter_compute_luma(pixel);
168+
output_color->pixel = luma >> (8 - colorspace->depth);
169+
output_color->opaque = true;
118170
return true;
119171
}
172+
output_color->opaque = false;
120173
return false;
121174
}
122175

123-
void common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color) {
124-
displayio_colorconverter_convert(self, colorspace, input_color, output_color);
125-
}
126-
127176
// Currently no refresh logic is needed for a ColorConverter.
128177
bool displayio_colorconverter_needs_refresh(displayio_colorconverter_t *self) {
129178
return false;

shared-module/displayio/ColorConverter.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ typedef struct {
3939

4040
bool displayio_colorconverter_needs_refresh(displayio_colorconverter_t *self);
4141
void displayio_colorconverter_finish_refresh(displayio_colorconverter_t *self);
42-
bool displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color);
42+
bool displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, const displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color);
43+
44+
uint32_t displayio_colorconverter_dither_noise_1 (uint32_t n);
45+
uint32_t displayio_colorconverter_dither_noise_2(uint32_t x, uint32_t y);
46+
4347
uint16_t displayio_colorconverter_compute_rgb565(uint32_t color_rgb888);
4448
uint8_t displayio_colorconverter_compute_luma(uint32_t color_rgb888);
4549
uint8_t displayio_colorconverter_compute_chroma(uint32_t color_rgb888);

shared-module/displayio/Display.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ void common_hal_displayio_display_set_auto_brightness(displayio_display_obj_t* s
150150
self->auto_brightness = auto_brightness;
151151
}
152152

153+
void common_hal_displayio_display_set_dither(displayio_display_obj_t* self, bool dither) {
154+
displayio_display_core_set_dither(&self->core, dither);
155+
}
156+
157+
bool common_hal_displayio_display_get_dither(displayio_display_obj_t* self) {
158+
return displayio_display_core_get_dither(&self->core);
159+
}
160+
153161
mp_float_t common_hal_displayio_display_get_brightness(displayio_display_obj_t* self) {
154162
return self->current_brightness;
155163
}

shared-module/displayio/Palette.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ typedef struct {
4141
bool tricolor;
4242
bool pixels_in_byte_share_row;
4343
bool reverse_pixels_in_byte;
44+
bool dither;
4445
} _displayio_colorspace_t;
4546

4647
typedef struct {
@@ -52,6 +53,20 @@ typedef struct {
5253
bool transparent; // This may have additional bits added later for blending.
5354
} _displayio_color_t;
5455

56+
typedef struct {
57+
uint32_t pixel;
58+
uint16_t x;
59+
uint16_t y;
60+
uint8_t tile;
61+
uint16_t tile_x;
62+
uint16_t tile_y;
63+
} displayio_input_pixel_t;
64+
65+
typedef struct {
66+
uint32_t pixel;
67+
bool opaque;
68+
} displayio_output_pixel_t;
69+
5570
typedef struct {
5671
mp_obj_base_t base;
5772
_displayio_color_t* colors;

shared-module/displayio/TileGrid.c

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,16 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_c
377377
}
378378

379379
uint8_t pixels_per_byte = 8 / colorspace->depth;
380-
for (int16_t y = start_y; y < end_y; y++) {
381-
int16_t row_start = start + (y - start_y + y_shift) * y_stride; // in pixels
382-
int16_t local_y = y / self->absolute_transform->scale;
383-
for (int16_t x = start_x; x < end_x; x++) {
380+
381+
displayio_input_pixel_t input_pixel;
382+
displayio_output_pixel_t output_pixel;
383+
384+
for (input_pixel.y = start_y; input_pixel.y < end_y; ++input_pixel.y) {
385+
int16_t row_start = start + (input_pixel.y - start_y + y_shift) * y_stride; // in pixels
386+
int16_t local_y = input_pixel.y / self->absolute_transform->scale;
387+
for (input_pixel.x = start_x; input_pixel.x < end_x; ++input_pixel.x) {
384388
// Compute the destination pixel in the buffer and mask based on the transformations.
385-
int16_t offset = row_start + (x - start_x + x_shift) * x_stride; // in pixels
389+
int16_t offset = row_start + (input_pixel.x - start_x + x_shift) * x_stride; // in pixels
386390

387391
// This is super useful for debugging out of range accesses. Uncomment to use.
388392
// if (offset < 0 || offset >= (int32_t) displayio_area_size(area)) {
@@ -393,41 +397,43 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_c
393397
if ((mask[offset / 32] & (1 << (offset % 32))) != 0) {
394398
continue;
395399
}
396-
int16_t local_x = x / self->absolute_transform->scale;
400+
int16_t local_x = input_pixel.x / self->absolute_transform->scale;
397401
uint16_t tile_location = ((local_y / self->tile_height + self->top_left_y) % self->height_in_tiles) * self->width_in_tiles + (local_x / self->tile_width + self->top_left_x) % self->width_in_tiles;
398-
uint8_t tile = tiles[tile_location];
399-
uint16_t tile_x = (tile % self->bitmap_width_in_tiles) * self->tile_width + local_x % self->tile_width;
400-
uint16_t tile_y = (tile / self->bitmap_width_in_tiles) * self->tile_height + local_y % self->tile_height;
402+
input_pixel.tile = tiles[tile_location];
403+
input_pixel.tile_x = (input_pixel.tile % self->bitmap_width_in_tiles) * self->tile_width + local_x % self->tile_width;
404+
input_pixel.tile_y = (input_pixel.tile / self->bitmap_width_in_tiles) * self->tile_height + local_y % self->tile_height;
405+
406+
//uint32_t value = 0;
407+
output_pixel.pixel = 0;
408+
input_pixel.pixel = 0;
401409

402-
uint32_t value = 0;
403410
// We always want to read bitmap pixels by row first and then transpose into the destination
404411
// buffer because most bitmaps are row associated.
405412
if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_bitmap_type)) {
406-
value = common_hal_displayio_bitmap_get_pixel(self->bitmap, tile_x, tile_y);
413+
input_pixel.pixel = common_hal_displayio_bitmap_get_pixel(self->bitmap, input_pixel.tile_x, input_pixel.tile_y);
407414
} else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_shape_type)) {
408-
value = common_hal_displayio_shape_get_pixel(self->bitmap, tile_x, tile_y);
415+
input_pixel.pixel = common_hal_displayio_shape_get_pixel(self->bitmap, input_pixel.tile_x, input_pixel.tile_y);
409416
} else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_ondiskbitmap_type)) {
410-
value = common_hal_displayio_ondiskbitmap_get_pixel(self->bitmap, tile_x, tile_y);
417+
input_pixel.pixel = common_hal_displayio_ondiskbitmap_get_pixel(self->bitmap, input_pixel.tile_x, input_pixel.tile_y);
411418
}
412-
413-
uint32_t pixel;
414-
bool opaque = true;
419+
420+
output_pixel.opaque = true;
415421
if (self->pixel_shader == mp_const_none) {
416-
pixel = value;
422+
output_pixel.pixel = input_pixel.pixel;
417423
} else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type)) {
418-
opaque = displayio_palette_get_color(self->pixel_shader, colorspace, value, &pixel);
424+
output_pixel.opaque = displayio_palette_get_color(self->pixel_shader, colorspace, input_pixel.pixel, &output_pixel.pixel);
419425
} else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type)) {
420-
opaque = displayio_colorconverter_convert(self->pixel_shader, colorspace, value, &pixel);
426+
displayio_colorconverter_convert(self->pixel_shader, colorspace, &input_pixel, &output_pixel);
421427
}
422-
if (!opaque) {
428+
if (!output_pixel.opaque) {
423429
// A pixel is transparent so we haven't fully covered the area ourselves.
424430
full_coverage = false;
425431
} else {
426432
mask[offset / 32] |= 1 << (offset % 32);
427433
if (colorspace->depth == 16) {
428-
*(((uint16_t*) buffer) + offset) = pixel;
434+
*(((uint16_t*) buffer) + offset) = output_pixel.pixel;
429435
} else if (colorspace->depth == 8) {
430-
*(((uint8_t*) buffer) + offset) = pixel;
436+
*(((uint8_t*) buffer) + offset) = output_pixel.pixel;
431437
} else if (colorspace->depth < 8) {
432438
// Reorder the offsets to pack multiple rows into a byte (meaning they share a column).
433439
if (!colorspace->pixels_in_byte_share_row) {
@@ -446,7 +452,7 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_c
446452
// Reverse the shift by subtracting it from the leftmost shift.
447453
shift = (pixels_per_byte - 1) * colorspace->depth - shift;
448454
}
449-
((uint8_t*)buffer)[offset / pixels_per_byte] |= pixel << shift;
455+
((uint8_t*)buffer)[offset / pixels_per_byte] |= output_pixel.pixel << shift;
450456
}
451457
}
452458
}

shared-module/displayio/display_core.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void displayio_display_core_construct(displayio_display_core_t* self,
4949
self->colorspace.pixels_in_byte_share_row = pixels_in_byte_share_row;
5050
self->colorspace.bytes_per_cell = bytes_per_cell;
5151
self->colorspace.reverse_pixels_in_byte = reverse_pixels_in_byte;
52+
self->colorspace.dither = false;
5253
self->current_group = NULL;
5354
self->colstart = colstart;
5455
self->rowstart = rowstart;
@@ -171,6 +172,14 @@ uint16_t displayio_display_core_get_height(displayio_display_core_t* self){
171172
return self->height;
172173
}
173174

175+
void displayio_display_core_set_dither(displayio_display_core_t* self, bool dither){
176+
self->colorspace.dither = dither;
177+
}
178+
179+
bool displayio_display_core_get_dither(displayio_display_core_t* self){
180+
return self->colorspace.dither;
181+
}
182+
174183
bool displayio_display_core_bus_free(displayio_display_core_t *self) {
175184
return self->bus_free(self->bus);
176185
}

shared-module/displayio/display_core.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ bool displayio_display_core_show(displayio_display_core_t* self, displayio_group
6565
uint16_t displayio_display_core_get_width(displayio_display_core_t* self);
6666
uint16_t displayio_display_core_get_height(displayio_display_core_t* self);
6767

68+
void displayio_display_core_set_dither(displayio_display_core_t* self, bool dither);
69+
bool displayio_display_core_get_dither(displayio_display_core_t* self);
70+
6871
bool displayio_display_core_bus_free(displayio_display_core_t *self);
6972
bool displayio_display_core_begin_transaction(displayio_display_core_t* self);
7073
void displayio_display_core_end_transaction(displayio_display_core_t* self);

0 commit comments

Comments
 (0)