10000 Merge pull request #7782 from jposada202020/adding_circle_bitmaptools · megacoder/circuitpython@4a14e4b · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a14e4b

Browse files
authored
Merge pull request adafruit#7782 from jposada202020/adding_circle_bitmaptools
Adding circle bitmaptools
2 parents e8711ee + 2f3ea81 commit 4a14e4b

File tree

4 files changed

+155
-15
lines changed

4 files changed

+155
-15
lines changed

locale/circuitpython.pot

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ msgid "%q in %q must be of type %q, not %q"
122122
msgstr ""
123123

124124
#: ports/espressif/common-hal/espulp/ULP.c
125+
#: ports/mimxrt10xx/common-hal/audiobusio/__init__.c
125126
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
126127
#: shared-bindings/digitalio/DigitalInOut.c
127128
#: shared-bindings/microcontroller/Pin.c
@@ -1217,13 +1218,15 @@ msgstr ""
12171218
msgid "Interrupt error."
12181219
msgstr ""
12191220

1221+
#: ports/mimxrt10xx/common-hal/audiobusio/__init__.c
12201222
#: ports/mimxrt10xx/common-hal/pwmio/PWMOut.c py/argcheck.c
12211223
#: shared-bindings/digitalio/DigitalInOut.c
12221224
#: shared-bindings/displayio/EPaperDisplay.c
12231225
msgid "Invalid %q"
12241226
msgstr ""
12251227

12261228
#: ports/atmel-samd/common-hal/microcontroller/Pin.c
1229+
#: ports/mimxrt10xx/common-hal/microcontroller/Pin.c
12271230
#: shared-bindings/microcontroller/Pin.c
12281231
msgid "Invalid %q pin"
12291232
msgstr ""
@@ -3827,11 +3830,7 @@ msgstr ""
38273830
msgid "out must be a float dense array"
38283831
msgstr ""
38293832

3830-
#: shared-bindings/displayio/Bitmap.c
3831-
msgid "out of range of source"
3832-
msgstr ""
3833-
3834-
#: shared-bindings/bitmaptools/__init__.c shared-bindings/displayio/Bitmap.c
3833+
#: shared-bindings/bitmaptools/__init__.c
38353834
msgid "out of range of target"
38363835
msgstr ""
38373836

@@ -3856,14 +3855,10 @@ msgstr ""
38563855
msgid "parameters must be registers in sequence r0 to r3"
38573856
msgstr ""
38583857

3859-
#: shared-bindings/bitmaptools/__init__.c shared-bindings/displayio/Bitmap.c
3858+
#: shared-bindings/bitmaptools/__init__.c
38603859
msgid "pixel coordinates out of bounds"
38613860
msgstr ""
38623861

3863-
#: shared-bindings/displayio/Bitmap.c
3864-
msgid "pixel value requires too many bits"
3865-
msgstr ""
3866-
38673862
#: shared-bindings/displayio/TileGrid.c shared-bindings/vectorio/VectorShape.c
38683863
msgid "pixel_shader must be displayio.Palette or displayio.ColorConverter"
38693864
msgstr ""
@@ -4276,10 +4271,6 @@ msgstr ""
42764271
msgid "value out of range of target"
42774272
msgstr ""
42784273

4279-
#: shared-bindings/displayio/Bitmap.c
4280-
msgid "value_count must be > 0"
4281-
msgstr ""
4282-
42834274
#: ports/espressif/common-hal/watchdog/WatchDogTimer.c
42844275
msgid "watchdog not initialized"
42854276
msgstr ""

shared-bindings/bitmaptools/__init__.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,86 @@ STATIC mp_obj_t bitmaptools_dither(size_t n_args, const mp_obj_t *pos_args, mp_m
868868
return mp_const_none;
869869
}
870870
MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_dither_obj, 0, bitmaptools_dither);
871+
// requires all 5 arguments
871872

873+
//| def draw_circle(
874+
//| dest_bitmap: displayio.Bitmap, x: int, y: int, radius: int, value: int
875+
//| ) -> None:
876+
//| """Draws a circle into a bitmap specified using a center (x0,y0) and radius r.
877+
//|
878+
//| :param bitmap dest_bitmap: Destination bitmap that will be written into
879+
//| :param int x: x-pixel position of the circle's center
880+
//| :param int y: y-pixel position of the circle's center
881+
//| :param int radius: circle's radius
882+
//| :param int value: Bitmap palette index that will be written into the
883+
//| circle in the destination bitmap
884+
//|
885+
//| .. code-block:: Python
886+
//|
887+
//| import board
888+
//| import displayio
889+
//| import bitmaptools
890+
//|
891+
//| display = board.DISPLAY
892+
//| main_group = displayio.Group()
893+
//| display.root_group = main_group
894+
//|
895+
//| palette = displayio.Palette(2)
896+
//| palette[0] = 0xffffff
897+
//| palette[1] = 0x440044
898+
//|
899+
//| bmp = displayio.Bitmap(128,128, 2)
900+
//| bmp.fill(0)
901+
//|
902+
//| bitmaptools.circle(64,64, 32, 1)
903+
//|
904+
//| tilegrid = displayio.TileGrid(bitmap=bmp, pixel_shader=palette)
905+
//| main_group.append(tilegrid)
906+
//|
907+
//| while True:
908+
//| pass
909+
//|
910+
//| """
911+
//|
912+
//| ...
913+
//|
914+
STATIC mp_obj_t bitmaptools_obj_draw_circle(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
915+
enum {ARG_dest_bitmap, ARG_x, ARG_y, ARG_radius, ARG_value};
916+
917+
static const mp_arg_t allowed_args[] = {
918+
{MP_QSTR_dest_bitmap, MP_ARG_REQUIRED | MP_ARG_OBJ},
919+
{MP_QSTR_x, MP_ARG_REQUIRED | MP_ARG_INT},
920+
{MP_QSTR_y, MP_ARG_REQUIRED | MP_ARG_INT},
921+
{MP_QSTR_radius, MP_ARG_REQUIRED | MP_ARG_INT},
922+
{MP_QSTR_value, MP_ARG_REQUIRED | MP_ARG_INT},
923+
};
924+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
925+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
926+
927+
displayio_bitmap_t *destination = MP_OBJ_TO_PTR(args[ARG_dest_bitmap].u_obj); // the destination bitmap
928+
929+
uint32_t value, color_depth;
930+
value = args[ARG_value].u_int;
931+
color_depth = (1 << destination->bits_per_value);
932+
if (color_depth <= value) {
933+
mp_raise_ValueError(translate("out of range of target"));
934+
}
935+
936+
937+
int16_t x = args[ARG_x].u_int;
938+
int16_t y = args[ARG_y].u_int;
939+
int16_t radius = args[ARG_radius].u_int;
940+
941+
mp_arg_validate_int_range(x, 0, destination->width, MP_QSTR_x);
942+
mp_arg_validate_int_range(y, 0, destination->height, MP_QSTR_y);
943+
mp_arg_validate_int_min(radius, 0, MP_QSTR_radius);
944+
945+
common_hal_bitmaptools_draw_circle(destination, x, y, radius, value);
946+
947+
return mp_const_none;
948+
}
949+
950+
MP_DEFINE_CONST_FUN_OBJ_KW(bitmaptools_draw_circle_obj, 0, bitmaptools_obj_draw_circle);
872951

873952
STATIC const mp_rom_map_elem_t bitmaptools_module_globals_table[] = {
874953
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bitmaptools) },
@@ -880,6 +959,7 @@ STATIC const mp_rom_map_elem_t bitmaptools_module_globals_table[] = {
880959
{ MP_ROM_QSTR(MP_QSTR_boundary_fill), MP_ROM_PTR(&bitmaptools_boundary_fill_obj) },
881960
{ MP_ROM_QSTR(MP_QSTR_draw_line), MP_ROM_PTR(&bitmaptools_draw_line_obj) },
882961
{ MP_ROM_QSTR(MP_QSTR_draw_polygon), MP_ROM_PTR(&bitmaptools_draw_polygon_obj) },
962+
{ MP_ROM_QSTR(MP_QSTR_draw_circle), MP_ROM_PTR(&bitmaptools_draw_circle_obj) },
883963
{ MP_ROM_QSTR(MP_QSTR_dither), MP_ROM_PTR(&bitmaptools_dither_obj) },
884964
{ MP_ROM_QSTR(MP_QSTR_DitherAlgorithm), MP_ROM_PTR(&bitmaptools_dither_algorithm_type) },
885965
};

shared-bindings/bitmaptools/__init__.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ void common_hal_bitmaptools_draw_line(displayio_bitmap_t *destination,
6464
int16_t x1, int16_t y1,
6565
uint32_t value);
6666

67+
void common_hal_bitmaptools_draw_circle(displayio_bitmap_t *destination,
68+
int16_t x, int16_t y,
69+
int16_t radius,
70+
uint32_t value);
71+
6772
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);
6873
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);
6974
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);

shared-module/bitmaptools/__init__.c

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2021 Kevin Matocha
6+
* Copyright (c) 2021 Kevin Matocha, Jose David Montoya
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -40,6 +40,9 @@
4040
#include <stdio.h>
4141
#include <string.h>
4242

43+
#define BITMAP_DEBUG(...) (void)0
44+
// #define BITMAP_DEBUG(...) mp_printf(&mp_plat_print, __VA_ARGS__)
45+
4346
void common_hal_bitmaptools_rotozoom(displayio_bitmap_t *self, int16_t ox, int16_t oy,
4447
int16_t dest_clip0_x, int16_t dest_clip0_y,
4548
int16_t dest_clip1_x, int16_t dest_clip1_y,
@@ -918,3 +921,64 @@ void common_hal_bitmaptools_alphablend(displayio_bitmap_t *dest, displayio_bitma
918921
}
919922
}
920923
}
924+
925+
STATIC void draw_circle(displayio_bitmap_t *destination,
926+
int16_t x, int16_t y,
927+
int16_t radius, uint32_t value) {
928+
929+
int16_t d, yb;
930+
931+
mp_arg_validate_int_range(x, SHRT_MIN, SHRT_MAX, MP_QSTR_x);
932+
mp_arg_validate_int_range(y, SHRT_MIN, SHRT_MAX, MP_QSTR_y);
933+
934+
x = MIN(x, destination->width);
935+
x = MAX(0, x);
936+
y = MIN(y, destination->height);
937+
y = MAX(0, y);
938+
939+
BITMAP_DEBUG("x, y, radius (%4d, %4d, %4d)\n", x, y, radius);
940+
941+
yb = radius;
942+
d = 3 - 2 * radius;
943+
944+
// Bresenham's circle algorithm
945+
for (int xb = 0; xb <= yb; xb++) {
946+
displayio_bitmap_write_pixel(destination, xb + x, yb + y, value);
947+
displayio_bitmap_write_pixel(destination, -xb + x, -yb + y, value);
948+
displayio_bitmap_write_pixel(destination, -xb + x, yb + y, value);
949+
displayio_bitmap_write_pixel(destination, xb + x, -yb + y, value);
950+
displayio_bitmap_write_pixel(destination, yb + x, xb + y, value);
951+
displayio_bitmap_write_pixel(destination, -yb + x, xb + y, value);
952+
displayio_bitmap_write_pixel(destination, -yb + x, -xb + y, value);
953+
displayio_bitmap_write_pixel(destination, yb + x, -xb + y, value);
954+
if (d <= 0) {
955+
d = d + (4 * xb) + 6;
956+
} else {
957+
d = d + 4 * (xb - yb) + 10;
958+
yb = yb - 1;
959+
}
960+
}
961+
}
962+
963+
void common_hal_bitmaptools_draw_circle(displayio_bitmap_t *destination,
964+
int16_t x, int16_t y,
965+
int16_t radius,
966+
uint32_t value) {
967+
968+
969+
// update the dirty area
970+
int16_t xbb0, xbb1, ybb0, ybb1;
971+
972+
xbb0 = x - radius;
973+
xbb1 = x + radius;
974+
ybb0 = y - radius;
975+
ybb1 = y + radius;
976+
977+
displayio_area_t area = { xbb0, ybb0, xbb1, ybb1, NULL };
978+
displayio_area_t bitmap_area = { 0, 0, destination->width, destination->height, NULL };
979+
displayio_area_compute_overlap(&area, &bitmap_area, &area);
980+
981+
displayio_bitmap_set_dirty_area(destination, &area);
982+
983+
draw_circle(destination, x, y, radius, value);
984+
}

0 commit comments

Comments
 (0)
0