Skip to content

displayio: Pass transparent_color to ColorConverter #3529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-10-16 13:56-0500\n"
"POT-Creation-Date: 2020-10-16 19:50-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -1404,6 +1404,10 @@ msgid ""
"%d bpp given"
msgstr ""

#: shared-module/displayio/ColorConverter.c
msgid "Only one color can be transparent at a time"
msgstr ""

#: shared-bindings/ipaddress/__init__.c
msgid "Only raw int supported for ip"
msgstr ""
Expand Down
2 changes: 1 addition & 1 deletion locale/zh_Latn_pinyin.po
Original file line number Diff line number Diff line change
Expand Up @@ -3277,7 +3277,7 @@ msgstr "yòubiān bìxū shì ndarray huò biāoliàng"

#: py/objstr.c
msgid "rsplit(None,n)"
msgstr "Rchāifēn(wú ,N)"
msgstr "Rchāifēn(wú,N)"

#: shared-bindings/audiocore/RawSample.c
msgid ""
Expand Down
4 changes: 2 additions & 2 deletions ports/atmel-samd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ $(echo PERIPHERALS_CHIP_FAMILY=$(PERIPHERALS_CHIP_FAMILY))
ifeq ($(DEBUG), 1)
CFLAGS += -ggdb3 -Og
# You may want to disable -flto if it interferes with debugging.
CFLAGS += -flto
CFLAGS += -flto -flto-partition=none
# You may want to enable these flags to make setting breakpoints easier.
# CFLAGS += -fno-inline -fno-ipa-sra
ifeq ($(CHIP_FAMILY), samd21)
Expand All @@ -144,7 +144,7 @@ else
CFLAGS += -finline-limit=$(CFLAGS_INLINE_LIMIT)
endif

CFLAGS += -flto
CFLAGS += -flto -flto-partition=none

ifeq ($(CIRCUITPY_FULL_BUILD),0)
CFLAGS += --param inline-unit-growth=15 --param max-inline-insns-auto=20
Expand Down
28 changes: 27 additions & 1 deletion shared-bindings/displayio/ColorConverter.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ STATIC mp_obj_t displayio_colorconverter_make_new(const mp_obj_type_t *type, siz
enum { ARG_dither};

static const mp_arg_t allowed_args[] = {
{ MP_QSTR_dither, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_dither, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
Expand Down Expand Up @@ -110,9 +110,35 @@ const mp_obj_property_t displayio_colorconverter_dither_obj = {
(mp_obj_t)&mp_const_none_obj},
};

//| def make_transparent(self, pixel: int) -> None:
//| """Sets a pixel to not opaque."""
//|
STATIC mp_obj_t displayio_colorconverter_make_transparent(mp_obj_t self_in, mp_obj_t transparent_color_obj) {
displayio_colorconverter_t *self = MP_OBJ_TO_PTR(self_in);

mp_int_t transparent_color = mp_obj_get_int(&transparent_color);
common_hal_displayio_colorconverter_make_transparent(self, transparent_color);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(displayio_colorconverter_make_transparent_obj, displayio_colorconverter_make_transparent);

//| def make_opaque(self, pixel: int) -> None:
//| """Sets a pixel to opaque."""
//|
STATIC mp_obj_t displayio_colorconverter_make_opaque(mp_obj_t self_in, mp_obj_t transparent_color_obj) {
displayio_colorconverter_t *self = MP_OBJ_TO_PTR(self_in);

mp_int_t transparent_color = mp_obj_get_int(&transparent_color);
common_hal_displayio_colorconverter_make_opaque(self, transparent_color);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(displayio_colorconverter_make_opaque_obj, displayio_colorconverter_make_opaque);

STATIC const mp_rom_map_elem_t displayio_colorconverter_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_convert), MP_ROM_PTR(&displayio_colorconverter_convert_obj) },
{ MP_ROM_QSTR(MP_QSTR_dither), MP_ROM_PTR(&displayio_colorconverter_dither_obj) },
{ MP_ROM_QSTR(MP_QSTR_make_transparent), MP_ROM_PTR(&displayio_colorconverter_make_transparent_obj) },
{ MP_ROM_QSTR(MP_QSTR_make_opaque), MP_ROM_PTR(&displayio_colorconverter_make_opaque_obj) },
};
STATIC MP_DEFINE_CONST_DICT(displayio_colorconverter_locals_dict, displayio_colorconverter_locals_dict_table);

Expand Down
3 changes: 3 additions & 0 deletions shared-bindings/displayio/ColorConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ void common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *col
void common_hal_displayio_colorconverter_set_dither(displayio_colorconverter_t* self, bool dither);
bool common_hal_displayio_colorconverter_get_dither(displayio_colorconverter_t* self);

void common_hal_displayio_colorconverter_make_transparent(displayio_colorconverter_t* self, uint32_t transparent_color);
void common_hal_displayio_colorconverter_make_opaque(displayio_colorconverter_t* self, uint32_t transparent_color);

#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_COLORCONVERTER_H
19 changes: 19 additions & 0 deletions shared-module/displayio/ColorConverter.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "shared-bindings/displayio/ColorConverter.h"

#include "py/misc.h"
#include "py/runtime.h"

uint32_t displayio_colorconverter_dither_noise_1 (uint32_t n)
{
Expand Down Expand Up @@ -128,9 +129,27 @@ bool common_hal_displayio_colorconverter_get_dither(displayio_colorconverter_t*
return self->dither;
}

void common_hal_displayio_colorconverter_make_transparent(displayio_colorconverter_t* self, uint32_t transparent_color) {
if (self->transparent_color >= 0x1000000) {
mp_raise_RuntimeError(translate("Only one color can be transparent at a time"));
}
self->transparent_color = transparent_color;
}

void common_hal_displayio_colorconverter_make_opaque(displayio_colorconverter_t* self, uint32_t transparent_color) {
(void) transparent_color;
// 0x1000000 will never equal a valid color
self->transparent_color = 0x1000000;
}

void 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) {
uint32_t pixel = input_pixel->pixel;

if (self->transparent_color == pixel) {
output_color->opaque = false;
return;
}

if (self->dither){
uint8_t randr = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x,input_pixel->tile_y));
uint8_t randg = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x+33,input_pixel->tile_y));
Expand Down
1 change: 1 addition & 0 deletions shared-module/displayio/ColorConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
typedef struct {
mp_obj_base_t base;
bool dither;
uint32_t transparent_color;
} displayio_colorconverter_t;

bool displayio_colorconverter_needs_refresh(displayio_colorconverter_t *self);
Expand Down