-
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 13 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,6 +24,7 @@ | |
* THE SOFTWARE. | ||
*/ | ||
|
||
#include <string.h> | ||
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. Was this needed or a debug artifact no longer required? I didn't see anything called that required string.h. 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. Good catch thank you. I've removed this in the latest version but will wait to make a new commit until I address the below feedback as well. |
||
|
||
#include "shared-bindings/bitmaptools/__init__.h" | ||
#include "shared-bindings/displayio/Bitmap.h" | ||
|
@@ -252,6 +253,123 @@ 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) | ||
); | ||
|
||
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; | ||
|
||
// 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; | ||
} | ||
|
||
// 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 = { 0, 0, destination->width, destination->height }; | ||
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. Were you setting the entire display dirty on purpose? Not saying you need to for this but could track the min/max x and y values to mark only the changed area dirty. 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. Yep I think this is correct it should be efficient to track min/max values as is works and then only make dirty the area that actually did get changed. I'll work on that and then make a new commit once it's complete. Thanks again for taking a look! |
||
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.