From 0b3099a9ff935bc6d59c3db41316c262be254712 Mon Sep 17 00:00:00 2001 From: jposada202020 Date: Wed, 22 Mar 2023 18:44:45 -0400 Subject: [PATCH 01/12] adding bitmaptools circle --- shared-bindings/bitmaptools/__init__.c | 49 +++++++++++++++++++++ shared-bindings/bitmaptools/__init__.h | 5 +++ shared-module/bitmaptools/__init__.c | 61 +++++++++++++++++++++++++- 3 files changed, 114 insertions(+), 1 deletion(-) diff --git a/shared-bindings/bitmaptools/__init__.c b/shared-bindings/bitmaptools/__init__.c index 1195e1811d366..19ea952999d4a 100644 --- a/shared-bindings/bitmaptools/__init__.c +++ b/shared-bindings/bitmaptools/__init__.c @@ -868,7 +868,55 @@ STATIC mp_obj_t bitmaptools_dither(size_t n_args, const mp_obj_t *pos_args, mp_m return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_dither_obj, 0, bitmaptools_dither); +// requires all 5 arguments +//| def draw_circle( +//| dest_bitmap: displayio.Bitmap, x0: int, y0: int, radius: int, value: int +//| ) -> None: +//| """Draws a circle into a bitmap specified using a center (x0,y0) and radius r. +//| +//| :param bitmap dest_bitmap: Destination bitmap that will be written into +//| :param int x0: x-pixel position of the circle's center +//| :param int y0: y-pixel position of the circle's center +//| :param int radius: circle's radius +//| :param int value: Bitmap palette index that will be written into the +//| circle in the destination bitmap""" +//| ... +//| +STATIC mp_obj_t bitmaptools_obj_draw_circle(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum {ARG_dest_bitmap, ARG_x0, ARG_y0, ARG_radius, ARG_value}; + + static const mp_arg_t allowed_args[] = { + {MP_QSTR_dest_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_x0, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_y0, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_radius, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_value, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + }; + 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); + + displayio_bitmap_t *destination = MP_OBJ_TO_PTR(args[ARG_dest_bitmap].u_obj); // the destination bitmap + + uint32_t value, color_depth; + value = args[ARG_value].u_int; + color_depth = (1 << destination->bits_per_value); + if (color_depth <= value) { + mp_raise_ValueError(translate("out of range of target")); + } + + + int16_t x0 = args[ARG_x0].u_int; + int16_t y0 = args[ARG_y0].u_int; + int16_t radius = args[ARG_radius].u_int; + + + common_hal_bitmaptools_draw_circle(destination, x0, y0, radius, value); + + return mp_const_none; +} + +MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_draw_circle_obj, 0, bitmaptools_obj_draw_circle); STATIC const mp_rom_map_elem_t bitmaptools_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bitmaptools) }, @@ -880,6 +928,7 @@ STATIC const mp_rom_map_elem_t bitmaptools_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_boundary_fill), MP_ROM_PTR(&bitmaptools_boundary_fill_obj) }, { MP_ROM_QSTR(MP_QSTR_draw_line), MP_ROM_PTR(&bitmaptools_draw_line_obj) }, { MP_ROM_QSTR(MP_QSTR_draw_polygon), MP_ROM_PTR(&bitmaptools_draw_polygon_obj) }, + { MP_ROM_QSTR(MP_QSTR_draw_circle), MP_ROM_PTR(&bitmaptools_draw_circle_obj) }, { MP_ROM_QSTR(MP_QSTR_dither), MP_ROM_PTR(&bitmaptools_dither_obj) }, { MP_ROM_QSTR(MP_QSTR_DitherAlgorithm), MP_ROM_PTR(&bitmaptools_dither_algorithm_type) }, }; diff --git a/shared-bindings/bitmaptools/__init__.h b/shared-bindings/bitmaptools/__init__.h index db72410cf6652..3c356e761e5ae 100644 --- a/shared-bindings/bitmaptools/__init__.h +++ b/shared-bindings/bitmaptools/__init__.h @@ -64,6 +64,11 @@ void common_hal_bitmaptools_draw_line(displayio_bitmap_t *destination, int16_t x1, int16_t y1, uint32_t value); +void common_hal_bitmaptools_draw_circle(displayio_bitmap_t *destination, + int16_t x0, int16_t y0, + int16_t radius, + uint32_t value); + void common_hal_bitmaptools_draw_polygon(displayio_bitmap_t *destination, void *xs, void *ys, size_t points_len, int point_size, uint32_t value, bool close); void common_hal_bitmaptools_readinto(displayio_bitmap_t *self, mp_obj_t *file, int element_size, int bits_per_pixel, bool reverse_pixels_in_word, bool swap_bytes, bool reverse_rows); 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); diff --git a/shared-module/bitmaptools/__init__.c b/shared-module/bitmaptools/__init__.c index 8068bbe8b6e3e..699516f37cc53 100644 --- a/shared-module/bitmaptools/__init__.c +++ b/shared-module/bitmaptools/__init__.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2021 Kevin Matocha + * Copyright (c) 2021 Kevin Matocha, Jose David Montoya * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -40,6 +40,9 @@ #include #include +#define BITMAP_DEBUG(...) (void)0 +// #define BITMAP_DEBUG(...) mp_printf(&mp_plat_print, __VA_ARGS__) + void common_hal_bitmaptools_rotozoom(displayio_bitmap_t *self, int16_t ox, int16_t oy, int16_t dest_clip0_x, int16_t dest_clip0_y, int16_t dest_clip1_x, int16_t dest_clip1_y, @@ -918,3 +921,59 @@ void common_hal_bitmaptools_alphablend(displayio_bitmap_t *dest, displayio_bitma } } } + +STATIC void draw_circle(displayio_bitmap_t *destination, + int16_t x0, int16_t y0, + int16_t radius, uint32_t value) { + + int16_t d, y; + + mp_arg_validate_int_range(x0, SHRT_MIN, SHRT_MAX, MP_QSTR_x0); + mp_arg_validate_int_range(y0, SHRT_MIN, SHRT_MAX, MP_QSTR_y0); + + BITMAP_DEBUG("x, y, radius (%4d, %4d, %4d)\n", x0, y0, radius); + + y = radius; + d = 3 - 2 * radius; + + // Bresenham's circle algorithm + for (int x = 0; x <= y; x++) { + displayio_bitmap_write_pixel(destination, x + x0, y + y0, value); + displayio_bitmap_write_pixel(destination, -x + x0, -y + y0, value); + displayio_bitmap_write_pixel(destination, -x + x0, y + y0, value); + displayio_bitmap_write_pixel(destination, x + x0, -y + y0, value); + displayio_bitmap_write_pixel(destination, y + x0, x + y0, value); + displayio_bitmap_write_pixel(destination, -y + x0, x + y0, value); + displayio_bitmap_write_pixel(destination, -y + x0, -x + y0, value); + displayio_bitmap_write_pixel(destination, y + x0, -x + y0, value); + if (d <= 0) { + d = d + (4 * x) + 6; + } else { + d = d + 4 * (x - y) + 10; + y = y - 1; + } + } +} + +void common_hal_bitmaptools_draw_circle(displayio_bitmap_t *destination, + int16_t x0, int16_t y0, + int16_t radius, + uint32_t value) { + + + // update the dirty area + int16_t xbb0, xbb1, ybb0, ybb1; + + xbb0 = x0 - radius; + xbb1 = x0 + radius; + ybb0 = y0 - radius; + ybb1 = y0 + radius; + + displayio_area_t area = { xbb0, ybb0, xbb1, ybb1, NULL }; + displayio_area_t bitmap_area = { 0, 0, destination->width, destination->height, NULL }; + displayio_area_compute_overlap(&area, &bitmap_area, &area); + + displayio_bitmap_set_dirty_area(destination, &area); + + draw_circle(destination, x0, y0, radius, value); +} \ No newline at end of file From 29613c73e158cfd21732f076f12d7f0223d26b97 Mon Sep 17 00:00:00 2001 From: jposada202020 Date: Wed, 22 Mar 2023 19:05:18 -0400 Subject: [PATCH 02/12] pre-commit --- shared-bindings/bitmaptools/__init__.c | 2 +- shared-module/bitmaptools/__init__.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shared-bindings/bitmaptools/__init__.c b/shared-bindings/bitmaptools/__init__.c index 19ea952999d4a..e377d40031c64 100644 --- a/shared-bindings/bitmaptools/__init__.c +++ b/shared-bindings/bitmaptools/__init__.c @@ -928,7 +928,7 @@ STATIC const mp_rom_map_elem_t bitmaptools_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_boundary_fill), MP_ROM_PTR(&bitmaptools_boundary_fill_obj) }, { MP_ROM_QSTR(MP_QSTR_draw_line), MP_ROM_PTR(&bitmaptools_draw_line_obj) }, { MP_ROM_QSTR(MP_QSTR_draw_polygon), MP_ROM_PTR(&bitmaptools_draw_polygon_obj) }, - { MP_ROM_QSTR(MP_QSTR_draw_circle), MP_ROM_PTR(&bitmaptools_draw_circle_obj) }, + { MP_ROM_QSTR(MP_QSTR_draw_circle), MP_ROM_PTR(&bitmaptools_draw_circle_obj) }, { MP_ROM_QSTR(MP_QSTR_dither), MP_ROM_PTR(&bitmaptools_dither_obj) }, { MP_ROM_QSTR(MP_QSTR_DitherAlgorithm), MP_ROM_PTR(&bitmaptools_dither_algorithm_type) }, }; diff --git a/shared-module/bitmaptools/__init__.c b/shared-module/bitmaptools/__init__.c index 699516f37cc53..778d0629a7b4e 100644 --- a/shared-module/bitmaptools/__init__.c +++ b/shared-module/bitmaptools/__init__.c @@ -976,4 +976,4 @@ void common_hal_bitmaptools_draw_circle(displayio_bitmap_t *destination, displayio_bitmap_set_dirty_area(destination, &area); draw_circle(destination, x0, y0, radius, value); -} \ No newline at end of file +} From 6ad053e40375169e907dc5f4949775bef3c6b332 Mon Sep 17 00:00:00 2001 From: jposada202020 Date: Thu, 30 Mar 2023 08:51:52 -0400 Subject: [PATCH 03/12] correcting feedback --- locale/circuitpython.pot | 23 +++++------ shared-bindings/bitmaptools/__init__.c | 53 ++++++++++++++++++++++---- shared-module/bitmaptools/__init__.c | 5 +++ 3 files changed, 59 insertions(+), 22 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index b5026f78292be..21196702f2dc6 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -122,6 +122,7 @@ msgid "%q in %q must be of type %q, not %q" msgstr "" #: ports/espressif/common-hal/espulp/ULP.c +#: ports/mimxrt10xx/common-hal/audiobusio/__init__.c #: ports/raspberrypi/common-hal/rp2pio/StateMachine.c #: shared-bindings/digitalio/DigitalInOut.c #: shared-bindings/microcontroller/Pin.c @@ -1217,6 +1218,7 @@ msgstr "" msgid "Interrupt error." msgstr "" +#: ports/mimxrt10xx/common-hal/audiobusio/__init__.c #: ports/mimxrt10xx/common-hal/pwmio/PWMOut.c py/argcheck.c #: shared-bindings/digitalio/DigitalInOut.c #: shared-bindings/displayio/EPaperDisplay.c @@ -1224,6 +1226,7 @@ msgid "Invalid %q" msgstr "" #: ports/atmel-samd/common-hal/microcontroller/Pin.c +#: ports/mimxrt10xx/common-hal/microcontroller/Pin.c #: shared-bindings/microcontroller/Pin.c msgid "Invalid %q pin" msgstr "" @@ -3827,11 +3830,7 @@ msgstr "" msgid "out must be a float dense array" msgstr "" -#: shared-bindings/displayio/Bitmap.c -msgid "out of range of source" -msgstr "" - -#: shared-bindings/bitmaptools/__init__.c shared-bindings/displayio/Bitmap.c +#: shared-bindings/bitmaptools/__init__.c msgid "out of range of target" msgstr "" @@ -3856,14 +3855,10 @@ msgstr "" msgid "parameters must be registers in sequence r0 to r3" msgstr "" -#: shared-bindings/bitmaptools/__init__.c shared-bindings/displayio/Bitmap.c +#: shared-bindings/bitmaptools/__init__.c msgid "pixel coordinates out of bounds" msgstr "" -#: shared-bindings/displayio/Bitmap.c -msgid "pixel value requires too many bits" -msgstr "" - #: shared-bindings/displayio/TileGrid.c shared-bindings/vectorio/VectorShape.c msgid "pixel_shader must be displayio.Palette or displayio.ColorConverter" msgstr "" @@ -3905,6 +3900,10 @@ msgstr "" msgid "queue overflow" msgstr "" +#: shared-bindings/bitmaptools/__init__.c +msgid "radius must be greater than zero" +msgstr "" + #: py/parse.c msgid "raw f-strings are not supported" msgstr "" @@ -4276,10 +4275,6 @@ msgstr "" msgid "value out of range of target" msgstr "" -#: shared-bindings/displayio/Bitmap.c -msgid "value_count must be > 0" -msgstr "" - #: ports/espressif/common-hal/watchdog/WatchDogTimer.c msgid "watchdog not initialized" msgstr "" diff --git a/shared-bindings/bitmaptools/__init__.c b/shared-bindings/bitmaptools/__init__.c index 2a23a2c05d4c0..9aa7362269087 100644 --- a/shared-bindings/bitmaptools/__init__.c +++ b/shared-bindings/bitmaptools/__init__.c @@ -871,20 +871,48 @@ MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_dither_obj, 0, bitmaptools_dither); // requires all 5 arguments //| def draw_circle( -//| dest_bitmap: displayio.Bitmap, x0: int, y0: int, radius: int, value: int +//| dest_bitmap: displayio.Bitmap, x: int, y: int, radius: int, value: int //| ) -> None: //| """Draws a circle into a bitmap specified using a center (x0,y0) and radius r. //| //| :param bitmap dest_bitmap: Destination bitmap that will be written into -//| :param int x0: x-pixel position of the circle's center -//| :param int y0: y-pixel position of the circle's center +//| :param int x: x-pixel position of the circle's center +//| :param int y: y-pixel position of the circle's center //| :param int radius: circle's radius //| :param int value: Bitmap palette index that will be written into the -//| circle in the destination bitmap""" +//| circle in the destination bitmap +//| +//| .. code-block:: Python +//| +//| import board +//| import displayio +//| import bitmaptools +//| +//| display = board.DISPLAY +//| main_group = displayio.Group() +//| display.root_group = main_group +//| +//| palette = displayio.Palette(2) +//| palette[0] = 0xffffff +//| palette[1] = 0x440044 +//| +//| bmp = displayio.Bitmap(128,128, 2) +//| bmp.fill(0) +//| +//| bitmaptools.circle(64,64, 32, 1) +//| +//| tilegrid = displayio.TileGrid(bitmap=bmp, pixel_shader=palette) +//| main_group.append(tilegrid) +//| +//| while True: +//| pass +//| +//| """ +//| //| ... //| STATIC mp_obj_t bitmaptools_obj_draw_circle(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum {ARG_dest_bitmap, ARG_x0, ARG_y0, ARG_radius, ARG_value}; + enum {ARG_dest_bitmap, ARG_x, ARG_y, ARG_radius, ARG_value}; static const mp_arg_t allowed_args[] = { {MP_QSTR_dest_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, @@ -906,12 +934,21 @@ STATIC mp_obj_t bitmaptools_obj_draw_circle(size_t n_args, const mp_obj_t *pos_a } - int16_t x0 = args[ARG_x0].u_int; - int16_t y0 = args[ARG_y0].u_int; + int16_t x = args[ARG_x].u_int; + int16_t y = args[ARG_y].u_int; int16_t radius = args[ARG_radius].u_int; + if (x < 0 || x >= destination->width) { + mp_raise_ValueError(translate("out of range of target")); + } + if (y < 0 || y >= destination->height) { + mp_raise_ValueError(translate("out of range of target")); + } + if (radius < 0) { + mp_raise_ValueError(translate("radius must be greater than zero")); + } - common_hal_bitmaptools_draw_circle(destination, x0, y0, radius, value); + common_hal_bitmaptools_draw_circle(destination, x, y, radius, value); return mp_const_none; } diff --git a/shared-module/bitmaptools/__init__.c b/shared-module/bitmaptools/__init__.c index 449815533d0e1..7809e5fc8734c 100644 --- a/shared-module/bitmaptools/__init__.c +++ b/shared-module/bitmaptools/__init__.c @@ -931,6 +931,11 @@ STATIC void draw_circle(displayio_bitmap_t *destination, mp_arg_validate_int_range(x0, SHRT_MIN, SHRT_MAX, MP_QSTR_x0); mp_arg_validate_int_range(y0, SHRT_MIN, SHRT_MAX, MP_QSTR_y0); + x0 = MIN(x0, destination->width); + x0 = MAX(0, x0); + y0 = MIN(y0, destination->height); + y0 = MIN(0, y0); + BITMAP_DEBUG("x, y, radius (%4d, %4d, %4d)\n", x0, y0, radius); y = radius; From 844ea2f60b1b4f8c6155a2cd8cea8c0d15b9e4af Mon Sep 17 00:00:00 2001 From: jposada202020 <34255413+jposada202020@users.noreply.github.com> Date: Thu, 30 Mar 2023 15:28:15 -0400 Subject: [PATCH 04/12] Update shared-bindings/bitmaptools/__init__.c Co-authored-by: Dan Halbert --- shared-bindings/bitmaptools/__init__.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/shared-bindings/bitmaptools/__init__.c b/shared-bindings/bitmaptools/__init__.c index 9aa7362269087..7754bf57eb27b 100644 --- a/shared-bindings/bitmaptools/__init__.c +++ b/shared-bindings/bitmaptools/__init__.c @@ -916,10 +916,10 @@ STATIC mp_obj_t bitmaptools_obj_draw_circle(size_t n_args, const mp_obj_t *pos_a static const mp_arg_t allowed_args[] = { {MP_QSTR_dest_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, - {MP_QSTR_x0, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, - {MP_QSTR_y0, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, - {MP_QSTR_radius, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, - {MP_QSTR_value, MP_ARG_REQUIRED | MP_ARG_INT, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_x, MP_ARG_REQUIRED | MP_ARG_INT}, + {MP_QSTR_y, MP_ARG_REQUIRED | MP_ARG_INT}, + {MP_QSTR_radius, MP_ARG_REQUIRED | MP_ARG_INT}, + {MP_QSTR_value, MP_ARG_REQUIRED | MP_ARG_INT}, }; 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); From 4e332fb1c493d8498b7f71347eefcf2b9caacc95 Mon Sep 17 00:00:00 2001 From: jposada202020 <34255413+jposada202020@users.noreply.github.com> Date: Thu, 30 Mar 2023 15:28:42 -0400 Subject: [PATCH 05/12] Update shared-bindings/bitmaptools/__init__.h Co-authored-by: Dan Halbert --- shared-bindings/bitmaptools/__init__.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-bindings/bitmaptools/__init__.h b/shared-bindings/bitmaptools/__init__.h index 3c356e761e5ae..21fb1b50ab511 100644 --- a/shared-bindings/bitmaptools/__init__.h +++ b/shared-bindings/bitmaptools/__init__.h @@ -65,7 +65,7 @@ void common_hal_bitmaptools_draw_line(displayio_bitmap_t *destination, uint32_t value); void common_hal_bitmaptools_draw_circle(displayio_bitmap_t *destination, - int16_t x0, int16_t y0, + int16_t x, int16_t y, int16_t radius, uint32_t value); From 533f532ff24f70538b9891aef9c0c45ce46dbd96 Mon Sep 17 00:00:00 2001 From: jposada202020 <34255413+jposada202020@users.noreply.github.com> Date: Thu, 30 Mar 2023 15:29:18 -0400 Subject: [PATCH 06/12] Update shared-module/bitmaptools/__init__.c Co-authored-by: Dan Halbert --- shared-module/bitmaptools/__init__.c | 44 ++++++++++++++-------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/shared-module/bitmaptools/__init__.c b/shared-module/bitmaptools/__init__.c index 7809e5fc8734c..1ab9c72de93e8 100644 --- a/shared-module/bitmaptools/__init__.c +++ b/shared-module/bitmaptools/__init__.c @@ -923,39 +923,39 @@ void common_hal_bitmaptools_alphablend(displayio_bitmap_t *dest, displayio_bitma } STATIC void draw_circle(displayio_bitmap_t *destination, - int16_t x0, int16_t y0, + int16_t x, int16_t y, int16_t radius, uint32_t value) { - int16_t d, y; + int16_t d, yb; - mp_arg_validate_int_range(x0, SHRT_MIN, SHRT_MAX, MP_QSTR_x0); - mp_arg_validate_int_range(y0, SHRT_MIN, SHRT_MAX, MP_QSTR_y0); + mp_arg_validate_int_range(x0, SHRT_MIN, SHRT_MAX, MP_QSTR_x); + mp_arg_validate_int_range(y0, SHRT_MIN, SHRT_MAX, MP_QSTR_y); - x0 = MIN(x0, destination->width); - x0 = MAX(0, x0); - y0 = MIN(y0, destination->height); - y0 = MIN(0, y0); + x = MIN(x, destination->width); + x = MAX(0, x); + y = MIN(y, destination->height); + y = MIN(0, y); - BITMAP_DEBUG("x, y, radius (%4d, %4d, %4d)\n", x0, y0, radius); + BITMAP_DEBUG("x, y, radius (%4d, %4d, %4d)\n", x, y, radius); - y = radius; + yb = radius; d = 3 - 2 * radius; // Bresenham's circle algorithm - for (int x = 0; x <= y; x++) { - displayio_bitmap_write_pixel(destination, x + x0, y + y0, value); - displayio_bitmap_write_pixel(destination, -x + x0, -y + y0, value); - displayio_bitmap_write_pixel(destination, -x + x0, y + y0, value); - displayio_bitmap_write_pixel(destination, x + x0, -y + y0, value); - displayio_bitmap_write_pixel(destination, y + x0, x + y0, value); - displayio_bitmap_write_pixel(destination, -y + x0, x + y0, value); - displayio_bitmap_write_pixel(destination, -y + x0, -x + y0, value); - displayio_bitmap_write_pixel(destination, y + x0, -x + y0, value); + for (int xb = 0; x <= yb; xb++) { + displayio_bitmap_write_pixel(destination, xb + x, yb + y, value); + displayio_bitmap_write_pixel(destination, -xb + x, -yb + y, value); + displayio_bitmap_write_pixel(destination, -xb + x, yb + y, value); + displayio_bitmap_write_pixel(destination, xb + x, -yb + y, value); + displayio_bitmap_write_pixel(destination, yb + x, xb + y, value); + displayio_bitmap_write_pixel(destination, -yb + x, xb + y, value); + displayio_bitmap_write_pixel(destination, -yb + x, -xb + y, value); + displayio_bitmap_write_pixel(destination, yb + x, -xb + y, value); if (d <= 0) { - d = d + (4 * x) + 6; + d = d + (4 * xb) + 6; } else { - d = d + 4 * (x - y) + 10; - y = y - 1; + d = d + 4 * (xb - yb) + 10; + yb = yb - 1; } } } From f6a0fb20f2d41d8378abb467f7822d057fe1c74f Mon Sep 17 00:00:00 2001 From: jposada202020 <34255413+jposada202020@users.noreply.github.com> Date: Thu, 30 Mar 2023 15:29:39 -0400 Subject: [PATCH 07/12] Update shared-module/bitmaptools/__init__.c Co-authored-by: Dan Halbert --- shared-module/bitmaptools/__init__.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/shared-module/bitmaptools/__init__.c b/shared-module/bitmaptools/__init__.c index 1ab9c72de93e8..105a7c1e772bf 100644 --- a/shared-module/bitmaptools/__init__.c +++ b/shared-module/bitmaptools/__init__.c @@ -961,7 +961,7 @@ STATIC void draw_circle(displayio_bitmap_t *destination, } void common_hal_bitmaptools_draw_circle(displayio_bitmap_t *destination, - int16_t x0, int16_t y0, + int16_t x, int16_t y, int16_t radius, uint32_t value) { @@ -969,10 +969,10 @@ void common_hal_bitmaptools_draw_circle(displayio_bitmap_t *destination, // update the dirty area int16_t xbb0, xbb1, ybb0, ybb1; - xbb0 = x0 - radius; - xbb1 = x0 + radius; - ybb0 = y0 - radius; - ybb1 = y0 + radius; + xbb0 = x - radius; + xbb1 = x + radius; + ybb0 = y - radius; + ybb1 = y + radius; displayio_area_t area = { xbb0, ybb0, xbb1, ybb1, NULL }; displayio_area_t bitmap_area = { 0, 0, destination->width, destination->height, NULL }; @@ -980,5 +980,5 @@ void common_hal_bitmaptools_draw_circle(displayio_bitmap_t *destination, displayio_bitmap_set_dirty_area(destination, &area); - draw_circle(destination, x0, y0, radius, value); + draw_circle(destination, x, y, radius, value); } From 9a77199f2e92512ab3c0dcc20ec2279040a1813c Mon Sep 17 00:00:00 2001 From: jposada202020 <34255413+jposada202020@users.noreply.github.com> Date: Thu, 30 Mar 2023 15:43:48 -0400 Subject: [PATCH 08/12] Update shared-bindings/bitmaptools/__init__.c Co-authored-by: Dan Halbert --- shared-bindings/bitmaptools/__init__.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-bindings/bitmaptools/__init__.c b/shared-bindings/bitmaptools/__init__.c index 7754bf57eb27b..5ae5182c59d27 100644 --- a/shared-bindings/bitmaptools/__init__.c +++ b/shared-bindings/bitmaptools/__init__.c @@ -915,7 +915,7 @@ STATIC mp_obj_t bitmaptools_obj_draw_circle(size_t n_args, const mp_obj_t *pos_a enum {ARG_dest_bitmap, ARG_x, ARG_y, ARG_radius, ARG_value}; static const mp_arg_t allowed_args[] = { - {MP_QSTR_dest_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_dest_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ}, {MP_QSTR_x, MP_ARG_REQUIRED | MP_ARG_INT}, {MP_QSTR_y, MP_ARG_REQUIRED | MP_ARG_INT}, {MP_QSTR_radius, MP_ARG_REQUIRED | MP_ARG_INT}, From 4986ad6d6b0cdcbfd24031492986cd178522d6a0 Mon Sep 17 00:00:00 2001 From: jposada202020 <34255413+jposada202020@users.noreply.github.com> Date: Thu, 30 Mar 2023 15:43:57 -0400 Subject: [PATCH 09/12] Update shared-module/bitmaptools/__init__.c Co-authored-by: Dan Halbert --- shared-module/bitmaptools/__init__.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared-module/bitmaptools/__init__.c b/shared-module/bitmaptools/__init__.c index 105a7c1e772bf..0c9d2a1f6a99b 100644 --- a/shared-module/bitmaptools/__init__.c +++ b/shared-module/bitmaptools/__init__.c @@ -928,8 +928,8 @@ STATIC void draw_circle(displayio_bitmap_t *destination, int16_t d, yb; - mp_arg_validate_int_range(x0, SHRT_MIN, SHRT_MAX, MP_QSTR_x); - mp_arg_validate_int_range(y0, SHRT_MIN, SHRT_MAX, MP_QSTR_y); + mp_arg_validate_int_range(x, SHRT_MIN, SHRT_MAX, MP_QSTR_x); + mp_arg_validate_int_range(y, SHRT_MIN, SHRT_MAX, MP_QSTR_y); x = MIN(x, destination->width); x = MAX(0, x); From 1931b6c0424f8b8ed636321d31e87db96de53018 Mon Sep 17 00:00:00 2001 From: jposada202020 Date: Thu, 30 Mar 2023 18:08:17 -0400 Subject: [PATCH 10/12] fixing algorithm and MAX --- shared-module/bitmaptools/__init__.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared-module/bitmaptools/__init__.c b/shared-module/bitmaptools/__init__.c index 0c9d2a1f6a99b..0ded6d6abbb18 100644 --- a/shared-module/bitmaptools/__init__.c +++ b/shared-module/bitmaptools/__init__.c @@ -934,7 +934,7 @@ STATIC void draw_circle(displayio_bitmap_t *destination, x = MIN(x, destination->width); x = MAX(0, x); y = MIN(y, destination->height); - y = MIN(0, y); + y = MAX(0, y); BITMAP_DEBUG("x, y, radius (%4d, %4d, %4d)\n", x, y, radius); @@ -942,7 +942,7 @@ STATIC void draw_circle(displayio_bitmap_t *destination, d = 3 - 2 * radius; // Bresenham's circle algorithm - for (int xb = 0; x <= yb; xb++) { + for (int xb = 0; xb <= yb; xb++) { displayio_bitmap_write_pixel(destination, xb + x, yb + y, value); displayio_bitmap_write_pixel(destination, -xb + x, -yb + y, value); displayio_bitmap_write_pixel(destination, -xb + x, yb + y, value); From df46636c5e699fd2e98f4897285fd221ee129488 Mon Sep 17 00:00:00 2001 From: jposada202020 Date: Fri, 31 Mar 2023 20:15:07 -0400 Subject: [PATCH 11/12] improving range validation --- locale/circuitpython.pot | 4 ---- shared-bindings/bitmaptools/__init__.c | 12 +++--------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 21196702f2dc6..77d5aab1d32ea 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -3900,10 +3900,6 @@ msgstr "" msgid "queue overflow" msgstr "" -#: shared-bindings/bitmaptools/__init__.c -msgid "radius must be greater than zero" -msgstr "" - #: py/parse.c msgid "raw f-strings are not supported" msgstr "" diff --git a/shared-bindings/bitmaptools/__init__.c b/shared-bindings/bitmaptools/__init__.c index 5ae5182c59d27..629d602846810 100644 --- a/shared-bindings/bitmaptools/__init__.c +++ b/shared-bindings/bitmaptools/__init__.c @@ -938,15 +938,9 @@ STATIC mp_obj_t bitmaptools_obj_draw_circle(size_t n_args, const mp_obj_t *pos_a int16_t y = args[ARG_y].u_int; int16_t radius = args[ARG_radius].u_int; - if (x < 0 || x >= destination->width) { - mp_raise_ValueError(translate("out of range of target")); - } - if (y < 0 || y >= destination->height) { - mp_raise_ValueError(translate("out of range of target")); - } - if (radius < 0) { - mp_raise_ValueError(translate("radius must be greater than zero")); - } + mp_arg_validate_int_range(x, 0, destination->width, MP_QSTR_x) + mp_arg_validate_int_range(y, 0, destination->height, MP_QSTR_y) + mp_arg_validate_int_min(radius, 0, MP_QSTR_radius) common_hal_bitmaptools_draw_circle(destination, x, y, radius, value); From 2f3ea812776e6450a07710c32edeb78e72c525b3 Mon Sep 17 00:00:00 2001 From: jposada202020 Date: Fri, 31 Mar 2023 20:29:29 -0400 Subject: [PATCH 12/12] correcting --- shared-bindings/bitmaptools/__init__.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/shared-bindings/bitmaptools/__init__.c b/shared-bindings/bitmaptools/__init__.c index 629d602846810..d0aea33ef95bd 100644 --- a/shared-bindings/bitmaptools/__init__.c +++ b/shared-bindings/bitmaptools/__init__.c @@ -938,9 +938,9 @@ STATIC mp_obj_t bitmaptools_obj_draw_circle(size_t n_args, const mp_obj_t *pos_a int16_t y = args[ARG_y].u_int; int16_t radius = args[ARG_radius].u_int; - mp_arg_validate_int_range(x, 0, destination->width, MP_QSTR_x) - mp_arg_validate_int_range(y, 0, destination->height, MP_QSTR_y) - mp_arg_validate_int_min(radius, 0, MP_QSTR_radius) + mp_arg_validate_int_range(x, 0, destination->width, MP_QSTR_x); + mp_arg_validate_int_range(y, 0, destination->height, MP_QSTR_y); + mp_arg_validate_int_min(radius, 0, MP_QSTR_radius); common_hal_bitmaptools_draw_circle(destination, x, y, radius, value);