Skip to content

Commit dc6bf38

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 77cb2a1 + 05b9ba8 commit dc6bf38

File tree

8 files changed

+330
-6
lines changed

8 files changed

+330
-6
lines changed

locale/circuitpython.pot

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2627,6 +2627,10 @@ msgstr ""
26272627
msgid "binary op %q not implemented"
26282628
msgstr ""
26292629

2630+
#: shared-bindings/bitmaptools/__init__.c
2631+
msgid "bitmap sizes must match"
2632+
msgstr ""
2633+
26302634
#: extmod/modurandom.c
26312635
msgid "bits must be 32 or less"
26322636
msgstr ""
@@ -4118,6 +4122,18 @@ msgstr ""
41184122
msgid "source palette too large"
41194123
msgstr ""
41204124

4125+
#: shared-bindings/bitmaptools/__init__.c
4126+
msgid "source_bitmap must have value_count of 2 or 65536"
4127+
msgstr ""
4128+
4129+
#: shared-bindings/bitmaptools/__init__.c
4130+
msgid "source_bitmap must have value_count of 65536"
4131+
msgstr ""
4132+
4133+
#: shared-bindings/bitmaptools/__init__.c
4134+
msgid "source_bitmap must have value_count of 8"
4135+
msgstr ""
4136+
41214137
#: py/objstr.c
41224138
msgid "start/end indices"
41234139
msgstr ""
@@ -4366,6 +4382,10 @@ msgstr ""
43664382
msgid "unsupported colorspace for GifWriter"
43674383
msgstr ""
43684384

4385+
#: shared-bindings/bitmaptools/__init__.c
4386+
msgid "unsupported colorspace for dither"
4387+
msgstr ""
4388+
43694389
#: py/objstr.c
43704390
#, c-format
43714391
msgid "unsupported format character '%c' (0x%x) at index %d"

ports/espressif/supervisor/port.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ safe_mode_t port_init(void) {
127127
heap = NULL;
128128
never_reset_module_internal_pins();
129129

130-
#if defined(DEBUG)
130+
#ifndef DEBUG
131+
#define DEBUG (0)
132+
#endif
133+
134+
#if DEBUG
131135
// debug UART
132136
#ifdef CONFIG_IDF_TARGET_ESP32C3
133137
common_hal_never_reset_pin(&pin_GPIO20);
@@ -138,7 +142,11 @@ safe_mode_t port_init(void) {
138142
#endif
139143
#endif
140144

141-
#if defined(DEBUG) || defined(ENABLE_JTAG)
145+
#ifndef ENABLE_JTAG
146+
#define ENABLE_JTAG (defined(DEBUG) && DEBUG)
147+
#endif
148+
149+
#if ENABLE_JTAG
142150
// JTAG
143151
#ifdef CONFIG_IDF_TARGET_ESP32C3
144152
common_hal_never_reset_pin(&pin_GPIO4);

ports/stm/boards/meowbit_v121/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ LD_FILE = boards/STM32F401xe_boot.ld
2121
# LD_FILE = boards/STM32F401xe_fs.ld
2222

2323
CIRCUITPY_AESIO = 0
24+
CIRCUITPY_BITMAPTOOLS = 0
2425
CIRCUITPY_BLEIO_HCI = 0
2526
CIRCUITPY_GIFIO = 0
2627
CIRCUITPY_ULAB = 0

ports/stm/boards/thunderpack_v12/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ SPI_FLASH_FILESYSTEM = 1
1212
EXTERNAL_FLASH_DEVICES = GD25Q16C
1313

1414
CIRCUITPY_NVM = 1
15+
CIRCUITPY_BITMAPTOOLS = 0
1516
CIRCUITPY_BLEIO_HCI = 0
1617

1718
MCU_SERIES = F4

shared-bindings/bitmaptools/__init__.c

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
*/
2626

2727
#include "shared-bindings/displayio/Bitmap.h"
28+
#include "shared-bindings/displayio/Palette.h"
29+
#include "shared-bindings/displayio/ColorConverter.h"
2830
#include "shared-bindings/bitmaptools/__init__.h"
2931

3032
#include <stdint.h>
3133

3234
#include "py/binary.h"
35+
#include "py/enum.h"
3336
#include "py/obj.h"
3437
#include "py/runtime.h"
3538

@@ -642,9 +645,92 @@ STATIC mp_obj_t bitmaptools_readinto(size_t n_args, const mp_obj_t *pos_args, mp
642645

643646
return mp_const_none;
644647
}
645-
646648
MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_readinto_obj, 0, bitmaptools_readinto);
647649

650+
//| class DitherAlgorithm:
651+
//| """Identifies the algorith for dither to use"""
652+
//|
653+
//| Atkinson: "DitherAlgorithm"
654+
//| """The classic Atkinson dither, often associated with the Hypercard esthetic"""
655+
//|
656+
//| FloydStenberg: "DitherAlgorithm"
657+
//| """The Floyd-Stenberg dither"""
658+
//|
659+
MAKE_ENUM_VALUE(bitmaptools_dither_algorithm_type, dither_algorithm, Atkinson, DITHER_ALGORITHM_ATKINSON);
660+
MAKE_ENUM_VALUE(bitmaptools_dither_algorithm_type, dither_algorithm, FloydStenberg, DITHER_ALGORITHM_ATKINSON);
661+
662+
MAKE_ENUM_MAP(bitmaptools_dither_algorithm) {
663+
MAKE_ENUM_MAP_ENTRY(dither_algorithm, Atkinson),
664+
MAKE_ENUM_MAP_ENTRY(dither_algorithm, FloydStenberg),
665+
};
666+
STATIC MP_DEFINE_CONST_DICT(bitmaptools_dither_algorithm_locals_dict, bitmaptools_dither_algorithm_locals_table);
667+
668+
MAKE_PRINTER(bitmaptools, bitmaptools_dither_algorithm);
669+
670+
MAKE_ENUM_TYPE(bitmaptools, DitherAlgorithm, bitmaptools_dither_algorithm);
671+
672+
//| def dither(dest_bitmap: displayio.Bitmap, source_bitmapp: displayio.Bitmap, source_colorspace: displayio.Colorspace, algorithm: DitherAlgorithm=DitherAlgorithm.Atkinson) -> None:
673+
//| """Convert the input image into a 2-level output image using the given dither algorithm.
674+
//|
675+
//| :param bitmap dest_bitmap: Destination bitmap. It must have a value_count of 2 or 65536. The stored values are 0 and the maximum pixel value.
676+
//| :param bitmap source_bitmap: Source bitmap that contains the graphical region to be dithered. It must have a value_count of 65536.
677+
//| :param colorspace: The colorspace of the image. The supported colorspaces are ``RGB565``, ``BGR565``, ``RGB565_SWAPPED``, and ``BGR565_SWAPPED``
678+
//| :param algorithm: The dither algorithm to use, one of the `DitherAlgorithm` values.
679+
//| """
680+
//| ...
681+
//|
682+
STATIC mp_obj_t bitmaptools_dither(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
683+
enum { ARG_dest_bitmap, ARG_source_bitmap, ARG_source_colorspace, ARG_algorithm };
684+
static const mp_arg_t allowed_args[] = {
685+
{ MP_QSTR_dest_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ },
686+
{ MP_QSTR_source_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ },
687+
{ MP_QSTR_source_colorspace, MP_ARG_REQUIRED | MP_ARG_OBJ },
688+
{ MP_QSTR_algorithm, MP_ARG_OBJ, { .u_obj = MP_ROM_PTR((void *)&dither_algorithm_Atkinson_obj) } },
689+
};
690+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
691+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
692+
displayio_bitmap_t *source_bitmap = mp_arg_validate_type(args[ARG_source_bitmap].u_obj, &displayio_bitmap_type, MP_QSTR_source_bitmap);
693+
displayio_bitmap_t *dest_bitmap = mp_arg_validate_type(args[ARG_dest_bitmap].u_obj, &displayio_bitmap_type, MP_QSTR_dest_bitmap);
694+
bitmaptools_dither_algorithm_t algorithm = cp_enum_value(&bitmaptools_dither_algorithm_type, args[ARG_algorithm].u_obj);
695+
displayio_colorspace_t colorspace = cp_enum_value(&displayio_colorspace_type, args[ARG_source_colorspace].u_obj);
696+
697+
if (source_bitmap->width != dest_bitmap->width || source_bitmap->height != dest_bitmap->height) {
698+
mp_raise_TypeError(translate("bitmap sizes must match"));
699+
}
700+
701+
if (dest_bitmap->bits_per_value != 16 && dest_bitmap->bits_per_value != 1) {
702+
mp_raise_TypeError(translate("source_bitmap must have value_count of 2 or 65536"));
703+
}
704+
705+
706+
switch (colorspace) {
707+
case DISPLAYIO_COLORSPACE_RGB565:
708+
case DISPLAYIO_COLORSPACE_RGB565_SWAPPED:
709+
case DISPLAYIO_COLORSPACE_BGR565:
710+
case DISPLAYIO_COLORSPACE_BGR565_SWAPPED:
711+
if (source_bitmap->bits_per_value != 16) {
712+
mp_raise_TypeError(translate("source_bitmap must have value_count of 65536"));
713+
}
714+
break;
715+
716+
case DISPLAYIO_COLORSPACE_L8:
717+
if (source_bitmap->bits_per_value != 8) {
718+
mp_raise_TypeError(translate("source_bitmap must have value_count of 8"));
719+
}
720+
break;
721+
722+
default:
723+
mp_raise_TypeError(translate("unsupported colorspace for dither"));
724+
}
725+
726+
727+
common_hal_bitmaptools_dither(dest_bitmap, source_bitmap, colorspace, algorithm);
728+
729+
return mp_const_none;
730+
}
731+
MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_dither_obj, 0, bitmaptools_dither);
732+
733+
648734
STATIC const mp_rom_map_elem_t bitmaptools_module_globals_table[] = {
649735
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bitmaptools) },
650736
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&bitmaptools_readinto_obj) },
@@ -654,6 +740,8 @@ STATIC const mp_rom_map_elem_t bitmaptools_module_globals_table[] = {
654740
{ MP_ROM_QSTR(MP_QSTR_fill_region), MP_ROM_PTR(&bitmaptools_fill_region_obj) },
655741
{ MP_ROM_QSTR(MP_QSTR_boundary_fill), MP_ROM_PTR(&bitmaptools_boundary_fill_obj) },
656742
{ MP_ROM_QSTR(MP_QSTR_draw_line), MP_ROM_PTR(&bitmaptools_draw_line_obj) },
743+
{ MP_ROM_QSTR(MP_QSTR_dither), MP_ROM_PTR(&bitmaptools_dither_obj) },
744+
{ MP_ROM_QSTR(MP_QSTR_DitherAlgorithm), MP_ROM_PTR(&bitmaptools_dither_algorithm_type) },
657745
};
658746
STATIC MP_DEFINE_CONST_DICT(bitmaptools_module_globals, bitmaptools_module_globals_table);
659747

shared-bindings/bitmaptools/__init__.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,18 @@
2828
#define MICROPY_INCLUDED_SHARED_BINDINGS_BITMAPTOOLS__INIT__H
2929

3030
#include "shared-module/displayio/Bitmap.h"
31+
#include "shared-bindings/displayio/ColorConverter.h"
3132
#include "shared-bindings/displayio/__init__.h"
33+
#include "shared-module/displayio/Palette.h"
3234
#include "py/obj.h"
3335
#include "extmod/vfs_fat.h"
3436

37+
typedef enum {
38+
DITHER_ALGORITHM_ATKINSON, DITHER_ALGORITHM_FLOYD_STENBERG,
39+
} bitmaptools_dither_algorithm_t;
40+
41+
extern const mp_obj_type_t bitmaptools_dither_algorithm_type;
42+
3543
void common_hal_bitmaptools_rotozoom(displayio_bitmap_t *self, int16_t ox, int16_t oy,
3644
int16_t dest_clip0_x, int16_t dest_clip0_y,
3745
int16_t dest_clip1_x, int16_t dest_clip1_y,
@@ -58,6 +66,7 @@ void common_hal_bitmaptools_draw_line(displayio_bitmap_t *destination,
5866

5967
void common_hal_bitmaptools_readinto(displayio_bitmap_t *self, pyb_file_obj_t *file, int element_size, int bits_per_pixel, bool reverse_pixels_in_word, bool swap_bytes, bool reverse_rows);
6068
void common_hal_bitmaptools_arrayblit(displayio_bitmap_t *self, void *data, int element_size, int x1, int y1, int x2, int y2, bool skip_specified, uint32_t skip_index);
69+
void common_hal_bitmaptools_dither(displayio_bitmap_t *dest_bitmap, displayio_bitmap_t *source_bitmap, displayio_colorspace_t colorspace, bitmaptools_dither_algorithm_t algorithm);
6170

6271
void common_hal_bitmaptools_alphablend(displayio_bitmap_t *destination, displayio_bitmap_t *source1, displayio_bitmap_t *source2, displayio_colorspace_t colorspace, float factor1, float factor2);
6372

shared-bindings/displayio/ColorConverter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
//| class ColorConverter:
4040
//| """Converts one color format to another."""
4141
//|
42-
//| def __init__(self, *, colorspace: Colorspace=Colorspace.RGB888, dither: bool = False) -> None:
42+
//| def __init__(self, *, input_colorspace: Colorspace=Colorspace.RGB888, dither: bool = False) -> None:
4343
//| """Create a ColorConverter object to convert color formats.
4444
//|
4545
//| :param Colorspace colorspace: The source colorspace, one of the Colorspace constants

0 commit comments

Comments
 (0)