-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Bitmaptools boundary_fill #5145
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
Changes from 16 commits
bcfec10
6d57f43
158048e
87358f8
fd372cf
0bbb0f1
c1e164e
ec8b31e
4d8494f
6f78306
029150a
6bd8a1d
aeeba39
0f478d5
707f2e2
4c95150
80c7a15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ | |
* THE SOFTWARE. | ||
*/ | ||
|
||
|
||
#include "shared-bindings/bitmaptools/__init__.h" | ||
#include "shared-bindings/displayio/Bitmap.h" | ||
#include "shared-module/displayio/Bitmap.h" | ||
|
@@ -252,6 +251,148 @@ void common_hal_bitmaptools_fill_region(displayio_bitmap_t *destination, | |
} | ||
} | ||
|
||
void common_hal_bitmaptools_boundary_fill(displayio_bitmap_t *destination, | ||
int16_t x, int16_t y, | ||
uint32_t fill_color_value, uint32_t replaced_color_value) { | ||
|
||
if (fill_color_value == replaced_color_value) { | ||
// There is nothing to do | ||
return; | ||
} | ||
|
||
|
||
|
||
|
||
uint32_t current_point_color_value; | ||
|
||
// the list of points that we'll check | ||
mp_obj_t fill_area = mp_obj_new_list(0, NULL); | ||
|
||
// first point is the one user passed in | ||
mp_obj_t point[] = { mp_obj_new_int(x), mp_obj_new_int(y) }; | ||
mp_obj_list_append( | ||
fill_area, | ||
mp_obj_new_tuple(2, point) | ||
); | ||
|
||
int16_t minx = x; | ||
int16_t miny = x; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't miny = y and maxx = x respectively? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whoops, yes each should be their own respective values. Thank you There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed this with latest commit |
||
int16_t maxx = y; | ||
int16_t maxy = y; | ||
|
||
if (replaced_color_value == INT_MAX) { | ||
current_point_color_value = common_hal_displayio_bitmap_get_pixel( | ||
destination, | ||
mp_obj_get_int(point[0]), | ||
mp_obj_get_int(point[1])); | ||
replaced_color_value = (uint32_t)current_point_color_value; | ||
} | ||
|
||
mp_obj_t *fill_points; | ||
size_t list_length = 0; | ||
mp_obj_list_get(fill_area, &list_length, &fill_points); | ||
|
||
mp_obj_t current_point; | ||
|
||
size_t tuple_len = 0; | ||
mp_obj_t *tuple_items; | ||
|
||
int cur_x, cur_y; | ||
|
||
// while there are still points to check | ||
while (list_length > 0) { | ||
mp_obj_list_get(fill_area, &list_length, &fill_points); | ||
current_point = mp_obj_list_pop(fill_area, 0); | ||
mp_obj_tuple_get(current_point, &tuple_len, &tuple_items); | ||
current_point_color_value = common_hal_displayio_bitmap_get_pixel( | ||
destination, | ||
mp_obj_get_int(tuple_items[0]), | ||
mp_obj_get_int(tuple_items[1])); | ||
|
||
// if the current point is not background color ignore it | ||
if (current_point_color_value != replaced_color_value) { | ||
mp_obj_list_get(fill_area, &list_length, &fill_points); | ||
continue; | ||
} | ||
|
||
cur_x = mp_obj_int_get_checked(tuple_items[0]); | ||
cur_y = mp_obj_int_get_checked(tuple_items[1]); | ||
|
||
if (cur_x < minx) { | ||
minx = (int16_t)cur_x; | ||
} | ||
if (cur_x > maxx) { | ||
maxx = (int16_t)cur_x; | ||
} | ||
if (cur_y < miny) { | ||
miny = (int16_t)cur_y; | ||
} | ||
if (cur_y > maxy) { | ||
maxy = (int16_t)cur_y; | ||
} | ||
|
||
// fill the current point with fill color | ||
displayio_bitmap_write_pixel( | ||
destination, | ||
mp_obj_get_int(tuple_items[0]), | ||
mp_obj_get_int(tuple_items[1]), | ||
fill_color_value); | ||
|
||
// add all 4 surrounding points to the list to check | ||
FoamyGuy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// ignore points outside of the bitmap | ||
if (mp_obj_int_get_checked(tuple_items[1]) - 1 >= 0) { | ||
mp_obj_t above_point[] = { | ||
tuple_items[0], | ||
MP_OBJ_NEW_SMALL_INT(mp_obj_int_get_checked(tuple_items[1]) - 1) | ||
}; | ||
mp_obj_list_append( | ||
fill_area, | ||
mp_obj_new_tuple(2, above_point)); | ||
} | ||
|
||
// ignore points outside of the bitmap | ||
if (mp_obj_int_get_checked(tuple_items[0]) - 1 >= 0) { | ||
mp_obj_t left_point[] = { | ||
MP_OBJ_NEW_SMALL_INT(mp_obj_int_get_checked(tuple_items[0]) - 1), | ||
tuple_items[1] | ||
}; | ||
mp_obj_list_append( | ||
fill_area, | ||
mp_obj_new_tuple(2, left_point)); | ||
} | ||
|
||
// ignore points outside of the bitmap | ||
if (mp_obj_int_get_checked(tuple_items[0]) + 1 < destination->width) { | ||
mp_obj_t right_point[] = { | ||
MP_OBJ_NEW_SMALL_INT(mp_obj_int_get_checked(tuple_items[0]) + 1), | ||
tuple_items[1] | ||
}; | ||
mp_obj_list_append( | ||
fill_area, | ||
mp_obj_new_tuple(2, right_point)); | ||
} | ||
|
||
// ignore points outside of the bitmap | ||
if (mp_obj_int_get_checked(tuple_items[1]) + 1 < destination->height) { | ||
mp_obj_t below_point[] = { | ||
tuple_items[0], | ||
MP_OBJ_NEW_SMALL_INT(mp_obj_int_get_checked(tuple_items[1]) + 1) | ||
}; | ||
mp_obj_list_append( | ||
fill_area, | ||
mp_obj_new_tuple(2, below_point)); | ||
} | ||
|
||
mp_obj_list_get(fill_area, &list_length, &fill_points); | ||
} | ||
|
||
// set dirty the area so displayio will draw | ||
displayio_area_t area = { minx, miny, maxx + 1, maxy + 1}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason the max x/y is +1 that maybe I'm missing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not 100% sure about the reason, it seems like I did try initially without the |
||
displayio_bitmap_set_dirty_area(destination, &area); | ||
|
||
} | ||
|
||
void common_hal_bitmaptools_draw_line(displayio_bitmap_t *destination, | ||
int16_t x0, int16_t y0, | ||
int16_t x1, int16_t y1, | ||
|
Uh oh!
There was an error while loading. Please reload this page.