diff --git a/src/omv/img/binary.c b/src/omv/img/binary.c index 94ecc07d5..e14830f9b 100644 --- a/src/omv/img/binary.c +++ b/src/omv/img/binary.c @@ -6,51 +6,6 @@ #include "imlib.h" #ifdef IMLIB_ENABLE_BINARY_OPS -void imlib_zero(image_t *img, image_t *mask, bool invert) -{ - switch(img->bpp) { - case IMAGE_BPP_BINARY: { - for (int y = 0, yy = img->h; y < yy; y++) { - uint32_t *row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(img, y); - uint32_t *row_ptr_2 = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(mask, y); - for (int x = 0, xx = img->w; x < xx; x++) { - if (IMAGE_GET_BINARY_PIXEL_FAST(row_ptr_2, x) ^ invert) { - IMAGE_PUT_BINARY_PIXEL_FAST(row_ptr, x, 0); - } - } - } - break; - } - case IMAGE_BPP_GRAYSCALE: { - for (int y = 0, yy = img->h; y < yy; y++) { - uint8_t *row_ptr = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, y); - uint32_t *row_ptr_2 = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(mask, y); - for (int x = 0, xx = img->w; x < xx; x++) { - if (IMAGE_GET_BINARY_PIXEL_FAST(row_ptr_2, x) ^ invert) { - IMAGE_PUT_GRAYSCALE_PIXEL_FAST(row_ptr, x, 0); - } - } - } - break; - } - case IMAGE_BPP_RGB565: { - for (int y = 0, yy = img->h; y < yy; y++) { - uint16_t *row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, y); - uint32_t *row_ptr_2 = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(mask, y); - for (int x = 0, xx = img->w; x < xx; x++) { - if (IMAGE_GET_BINARY_PIXEL_FAST(row_ptr_2, x) ^ invert) { - IMAGE_PUT_RGB565_PIXEL_FAST(row_ptr, x, 0); - } - } - } - break; - } - default: { - break; - } - } -} - void imlib_binary(image_t *out, image_t *img, list_t *thresholds, bool invert, bool zero, image_t *mask) { image_t bmp; @@ -62,7 +17,6 @@ void imlib_binary(image_t *out, image_t *img, list_t *thresholds, bool invert, b for (list_lnk_t *it = iterator_start_from_head(thresholds); it; it = iterator_next(it)) { color_thresholds_list_lnk_data_t lnk_data; iterator_get(thresholds, it, &lnk_data); - switch(img->bpp) { case IMAGE_BPP_BINARY: { for (int y = 0, yy = img->h; y < yy; y++) { diff --git a/src/omv/img/imlib.c b/src/omv/img/imlib.c index fbe1b975e..369153d57 100644 --- a/src/omv/img/imlib.c +++ b/src/omv/img/imlib.c @@ -755,6 +755,48 @@ void imlib_save_image(image_t *img, const char *path, rectangle_t *roi, int qual //////////////////////////////////////////////////////////////////////////////// +void imlib_zero(image_t *img, image_t *mask, bool invert) +{ + switch(img->bpp) { + case IMAGE_BPP_BINARY: { + for (int y = 0, yy = img->h; y < yy; y++) { + uint32_t *row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(img, y); + for (int x = 0, xx = img->w; x < xx; x++) { + if (image_get_mask_pixel(mask, x, y) ^ invert) { + IMAGE_PUT_BINARY_PIXEL_FAST(row_ptr, x, 0); + } + } + } + break; + } + case IMAGE_BPP_GRAYSCALE: { + for (int y = 0, yy = img->h; y < yy; y++) { + uint8_t *row_ptr = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, y); + for (int x = 0, xx = img->w; x < xx; x++) { + if (image_get_mask_pixel(mask, x, y) ^ invert) { + IMAGE_PUT_GRAYSCALE_PIXEL_FAST(row_ptr, x, 0); + } + } + } + break; + } + case IMAGE_BPP_RGB565: { + for (int y = 0, yy = img->h; y < yy; y++) { + uint16_t *row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, y); + for (int x = 0, xx = img->w; x < xx; x++) { + if (image_get_mask_pixel(mask, x, y) ^ invert) { + IMAGE_PUT_RGB565_PIXEL_FAST(row_ptr, x, 0); + } + } + } + break; + } + default: { + break; + } + } +} + // A simple algorithm for correcting lens distortion. // See http://www.tannerhelland.com/4743/simple-algorithm-correcting-lens-distortion/ void imlib_lens_corr(image_t *img, float strength, float zoom) diff --git a/src/omv/img/imlib.h b/src/omv/img/imlib.h index 6d592fce4..7dba9b61f 100644 --- a/src/omv/img/imlib.h +++ b/src/omv/img/imlib.h @@ -1251,6 +1251,7 @@ void imlib_edge_canny(image_t *src, rectangle_t *roi, int low_thresh, int high_t void imlib_find_hog(image_t *src, rectangle_t *roi, int cell_size); // Helper Functions +void imlib_zero(image_t *img, image_t *mask, bool invert); void imlib_flood_fill_int(image_t *out, image_t *img, int x, int y, int seed_threshold, int floating_threshold, flood_fill_call_back_t cb, void *data); @@ -1267,7 +1268,6 @@ void imlib_flood_fill(image_t *img, int x, int y, float seed_threshold, float floating_threshold, int c, bool invert, bool clear_background, image_t *mask); // Binary Functions -void imlib_zero(image_t *img, image_t *mask, bool invert); void imlib_binary(image_t *out, image_t *img, list_t *thresholds, bool invert, bool zero, image_t *mask); void imlib_invert(image_t *img); void imlib_b_and(image_t *img, const char *path, image_t *other, int scalar, image_t *mask); diff --git a/src/omv/py/py_image.c b/src/omv/py/py_image.c index e0a45a771..5c918d941 100644 --- a/src/omv/py/py_image.c +++ b/src/omv/py/py_image.c @@ -1578,13 +1578,20 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_save_obj, 2, py_image_save); // Drawing Methods ////////////////// -STATIC mp_obj_t py_image_clear(mp_obj_t img_obj) +STATIC mp_obj_t py_image_clear(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { - image_t *arg_img = py_helper_arg_to_image_mutable_bayer(img_obj); - memset(arg_img->data, 0, image_size(arg_img)); - return img_obj; + image_t *arg_img = py_helper_arg_to_image_mutable_bayer(args[0]); + image_t *arg_msk = py_helper_keyword_to_image_mutable_mask(n_args, args, 1, kw_args); + + if (!arg_msk) { + memset(arg_img->data, 0, image_size(arg_img)); + } else { + imlib_zero(arg_img, arg_msk, false); + } + + return args[0]; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_image_clear_obj, py_image_clear); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_clear_obj, 1, py_image_clear); STATIC mp_obj_t py_image_draw_line(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { diff --git a/src/omv/py/qstrdefsomv.h b/src/omv/py/qstrdefsomv.h index 1fec57a58..c88270b29 100644 --- a/src/omv/py/qstrdefsomv.h +++ b/src/omv/py/qstrdefsomv.h @@ -431,6 +431,7 @@ Q(save) // Clear Q(clear) +Q(mask) // Draw Line Q(draw_line) @@ -488,7 +489,7 @@ Q(draw_image) // duplicate Q(x_scale) // duplicate Q(y_scale) Q(alpha) -Q(mask) +// duplicate Q(mask) // Draw Keypoints Q(draw_keypoints)