diff --git a/src/omv/py/py_image.c b/src/omv/py/py_image.c index 0b5502032..6ab93e36e 100644 --- a/src/omv/py/py_image.c +++ b/src/omv/py/py_image.c @@ -156,6 +156,37 @@ static mp_obj_t py_image_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t va } } +// Start Helper Functions ///////////////////////////////////////////////////// + +static int get_int_kw(mp_map_t *kw_args, mp_obj_t kw, int default_val) +{ + mp_map_elem_t *kw_arg = mp_map_lookup(kw_args, kw, MP_MAP_LOOKUP); + if (kw_arg != NULL) { + default_val = mp_obj_get_int(kw_arg->value); + } + return default_val; +} + +static int get_color_kw(mp_map_t *kw_args, int default_color) +{ + mp_map_elem_t *kw_color = mp_map_lookup(kw_args, + MP_OBJ_NEW_QSTR(MP_QSTR_color), MP_MAP_LOOKUP); + if (kw_color != NULL) { + if (mp_obj_is_integer(kw_color->value)) { + default_color = mp_obj_get_int(kw_color->value); + } else { + mp_obj_t *arg_color; + mp_obj_get_array_fixed_n(kw_color->value, 3, &arg_color); + default_color = IM_RGB565(IM_R825(mp_obj_get_int(arg_color[0])), + IM_G826(mp_obj_get_int(arg_color[1])), + IM_B825(mp_obj_get_int(arg_color[2]))); + } + } + return default_color; +} + +// End Helper Functions /////////////////////////////////////////////////////// + static mp_obj_t py_image_width(mp_obj_t img_obj) { image_t *arg_img = py_image_cobj(img_obj); @@ -194,7 +225,7 @@ static mp_obj_t py_image_get_pixel(mp_obj_t img_obj, mp_obj_t x_obj, mp_obj_t y_ { image_t *arg_img = py_image_cobj(img_obj); PY_ASSERT_FALSE_MSG(IM_IS_JPEG(arg_img), - "Operation not supported on JPEG"); + "Operation not supported on JPEG"); int arg_x = mp_obj_get_int(x_obj); int arg_y = mp_obj_get_int(y_obj); @@ -218,7 +249,7 @@ static mp_obj_t py_image_set_pixel(uint n_args, const mp_obj_t *args) { image_t *arg_img = py_image_cobj(args[0]); PY_ASSERT_FALSE_MSG(IM_IS_JPEG(arg_img), - "Operation not supported on JPEG"); + "Operation not supported on JPEG"); int arg_x = mp_obj_get_int(args[1]); int arg_y = mp_obj_get_int(args[2]); @@ -238,36 +269,6 @@ static mp_obj_t py_image_set_pixel(uint n_args, const mp_obj_t *args) return mp_const_none; } -static int kwargs_helper_lookup_int(mp_map_t *kw_args, mp_obj_t kw, int default_val) -{ - int val = default_val; - mp_map_elem_t *kw_arg = mp_map_lookup(kw_args, kw, MP_MAP_LOOKUP); - - if (kw_arg != NULL) { - val = mp_obj_get_int(kw_arg->value); - } - return val; -} - -static int kwargs_helper_lookup_color(mp_map_t *kw_args, int default_col) -{ - int arg_c = default_col; // white - mp_map_elem_t *kw_color = mp_map_lookup(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_color), MP_MAP_LOOKUP); - - if (kw_color != NULL) { - if (MP_OBJ_IS_INT(kw_color->value)) { - arg_c = mp_obj_get_int(kw_color->value); - } else if(MP_OBJ_IS_OBJ(kw_color->value)) { - mp_obj_t *arg_color; - mp_obj_get_array_fixed_n(kw_color->value, 3, &arg_color); - arg_c = IM_RGB565(IM_R825(mp_obj_get_int(arg_color[0])), - IM_G826(mp_obj_get_int(arg_color[1])), - IM_G826(mp_obj_get_int(arg_color[2]))); - } - } - return arg_c; -} - static mp_obj_t py_image_draw_line(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { image_t *arg_img = py_image_cobj(args[0]); @@ -281,7 +282,7 @@ static mp_obj_t py_image_draw_line(uint n_args, const mp_obj_t *args, mp_map_t * int arg_y0 = mp_obj_get_int(arg_vec[1]); int arg_x1 = mp_obj_get_int(arg_vec[2]); int arg_y1 = mp_obj_get_int(arg_vec[3]); - int arg_c = kwargs_helper_lookup_color(kw_args, -1); + int arg_c = get_color_kw(kw_args, -1); // white imlib_draw_line(arg_img, arg_x0, arg_y0, arg_x1, arg_y1, arg_c); return mp_const_none; @@ -291,7 +292,7 @@ static mp_obj_t py_image_draw_rectangle(uint n_args, const mp_obj_t *args, mp_ma { image_t *arg_img = py_image_cobj(args[0]); PY_ASSERT_FALSE_MSG(IM_IS_JPEG(arg_img), - "Operation not supported on JPEG"); + "Operation not supported on JPEG"); mp_obj_t *arg_vec; mp_obj_get_array_fixed_n(args[1], 4, &arg_vec); @@ -300,7 +301,7 @@ static mp_obj_t py_image_draw_rectangle(uint n_args, const mp_obj_t *args, mp_ma int arg_ry = mp_obj_get_int(arg_vec[1]); int arg_rw = mp_obj_get_int(arg_vec[2]); int arg_rh = mp_obj_get_int(arg_vec[3]); - int arg_c = kwargs_helper_lookup_color(kw_args, -1); + int arg_c = get_color_kw(kw_args, -1); // white imlib_draw_rectangle(arg_img, arg_rx, arg_ry, arg_rw, arg_rh, arg_c); return mp_const_none; @@ -310,12 +311,12 @@ static mp_obj_t py_image_draw_circle(uint n_args, const mp_obj_t *args, mp_map_t { image_t *arg_img = py_image_cobj(args[0]); PY_ASSERT_FALSE_MSG(IM_IS_JPEG(arg_img), - "Operation not supported on JPEG"); + "Operation not supported on JPEG"); int arg_cx = mp_obj_get_int(args[1]); int arg_cy = mp_obj_get_int(args[2]); int arg_r = mp_obj_get_int(args[3]); - int arg_c = kwargs_helper_lookup_color(kw_args, -1); + int arg_c = get_color_kw(kw_args, -1); // white imlib_draw_circle(arg_img, arg_cx, arg_cy, arg_r, arg_c); return mp_const_none; @@ -327,10 +328,10 @@ static mp_obj_t py_image_draw_string(uint n_args, const mp_obj_t *args, mp_map_t PY_ASSERT_FALSE_MSG(IM_IS_JPEG(arg_img), "Operation not supported on JPEG"); - int arg_x_off = mp_obj_get_int(args[1]); - int arg_y_off = mp_obj_get_int(args[2]); + int arg_x_off = mp_obj_get_int(args[1]); + int arg_y_off = mp_obj_get_int(args[2]); const char *arg_str = mp_obj_str_get_str(args[3]); - int arg_c = kwargs_helper_lookup_color(kw_args, -1); + int arg_c = get_color_kw(kw_args, -1); // white imlib_draw_string(arg_img, arg_x_off, arg_y_off, arg_str, arg_c); return mp_const_none; @@ -344,8 +345,8 @@ static mp_obj_t py_image_draw_cross(uint n_args, const mp_obj_t *args, mp_map_t int arg_x = mp_obj_get_int(args[1]); int arg_y = mp_obj_get_int(args[2]); - int arg_c = kwargs_helper_lookup_color(kw_args, -1); - int arg_s = kwargs_helper_lookup_int(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_size), 5); + int arg_c = get_color_kw(kw_args, -1); // white + int arg_s = get_int_kw(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_size), 5); imlib_draw_line(arg_img, arg_x-arg_s, arg_y , arg_x+arg_s, arg_y , arg_c); imlib_draw_line(arg_img, arg_x , arg_y-arg_s, arg_x , arg_y+arg_s, arg_c); @@ -360,9 +361,8 @@ static mp_obj_t py_image_draw_keypoints(uint n_args, const mp_obj_t *args, mp_ma mp_obj_t kpts_obj = args[1]; PY_ASSERT_TYPE(kpts_obj, &py_kp_type); - - int arg_c = kwargs_helper_lookup_color(kw_args, -1); - int arg_s = kwargs_helper_lookup_int(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_size), 10); + int arg_c = get_color_kw(kw_args, -1); // white + int arg_s = get_int_kw(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_size), 10); for (int i=0; i<((py_kp_obj_t*)kpts_obj)->size; i++) { kp_t *kp = &((py_kp_obj_t*)kpts_obj)->kpts[i]; diff --git a/usr/examples/drawing.py b/usr/examples/drawing.py index 6407910f8..463bf5f3f 100644 --- a/usr/examples/drawing.py +++ b/usr/examples/drawing.py @@ -21,7 +21,7 @@ y = int(240/2) image.draw_circle(x, y, 50) image.draw_line((x, y, int(50*sin(45))+x, int(50*cos(45))+y)) image.draw_rectangle((x-60, y-60, 120, 120)) -image.draw_string(10, 10, "OpenMV", 0xFF) -image.draw_string(10, 25, "Hello World", 0xFF) +image.draw_string(10, 10, "OpenMV", color = 0xFF) +image.draw_string(10, 25, "Hello World", color = 0xFF) # Flush buffer... sensor.snapshot() diff --git a/usr/examples/mlx.py b/usr/examples/mlx.py index d3e04962f..1c1f98c18 100644 --- a/usr/examples/mlx.py +++ b/usr/examples/mlx.py @@ -43,9 +43,9 @@ while (True): image.blend(ir, (0, int(120/2-32/2), 0.6)) # Draw ambient, min and max temperatures. - image.draw_string(0, 0, "Ta: %0.2f"%ta, (0xFF, 0x00, 0x00)) - image.draw_string(0, 5, "To min: %0.2f"%(to_min+ta), (0xFF, 0x00, 0x00)) - image.draw_string(0, 10, "To max: %0.2f"%(to_max+ta), (0xFF, 0x00, 0x00)) + image.draw_string(0, 0, "Ta: %0.2f"%ta, color = (0xFF, 0x00, 0x00)) + image.draw_string(0, 8, "To min: %0.2f"%(to_min+ta), color = (0xFF, 0x00, 0x00)) + image.draw_string(0, 16, "To max: %0.2f"%(to_max+ta), color = (0xFF, 0x00, 0x00)) # Print FPS. print(clock.fps()) diff --git a/usr/examples/mlx_lcd.py b/usr/examples/mlx_lcd.py index b3e5b660c..749e562d6 100644 --- a/usr/examples/mlx_lcd.py +++ b/usr/examples/mlx_lcd.py @@ -42,9 +42,9 @@ while (True): image = sensor.snapshot() # Draw ambient, min and max temperatures. - image.draw_string(0, 0, "Ta: %0.2f"%ta, (0xFF, 0x00, 0x00)) - image.draw_string(0, 5, "To min: %0.2f"%(to_min+ta), (0xFF, 0x00, 0x00)) - image.draw_string(0, 10, "To max: %0.2f"%(to_max+ta), (0xFF, 0x00, 0x00)) + image.draw_string(0, 0, "Ta: %0.2f"%ta, color = (0xFF, 0x00, 0x00)) + image.draw_string(0, 8, "To min: %0.2f"%(to_min+ta), color = (0xFF, 0x00, 0x00)) + image.draw_string(0, 16, "To max: %0.2f"%(to_max+ta), color = (0xFF, 0x00, 0x00)) # Capture an FIR image ta, to_min, to_max, ir = mlx.read_ir(mlx.RAINBOW, 80, 0.90) diff --git a/usr/tests/test_drawing_2.py b/usr/tests/test_drawing_2.py index f4c2e8f26..34521b051 100644 --- a/usr/tests/test_drawing_2.py +++ b/usr/tests/test_drawing_2.py @@ -10,11 +10,6 @@ while(True): img = sensor.snapshot() for i in range(img.width()): c = ((i * 255) + (img.width()/2)) / img.width() - img.draw_line([i, 0, i, img.height()-1], int(c)) - sensor.snapshot() - time.sleep(1000) - for i in range(img.width()): - c = (((img.width() - i) * 255) + (img.width()/2)) / img.width() img.draw_line([i, 0, i, img.height()-1], color = int(c)) sensor.snapshot() time.sleep(1000) @@ -24,11 +19,6 @@ while(True): img = sensor.snapshot() for i in range(img.width()): c = ((i * 255) + (img.width()/2)) / img.width() - img.draw_line([i, 0, i, img.height()-1], [int(c), 0, 0]) - sensor.snapshot() - time.sleep(1000) - for i in range(img.width()): - c = (((img.width() - i) * 255) + (img.width()/2)) / img.width() img.draw_line([i, 0, i, img.height()-1], color = [int(c), 0, 0]) sensor.snapshot() time.sleep(1000) @@ -38,11 +28,6 @@ while(True): img = sensor.snapshot() for i in range(img.width()): c = ((i * 255) + (img.width()/2)) / img.width() - img.draw_line([i, 0, i, img.height()-1], [0, int(c), 0]) - sensor.snapshot() - time.sleep(1000) - for i in range(img.width()): - c = (((img.width() - i) * 255) + (img.width()/2)) / img.width() img.draw_line([i, 0, i, img.height()-1], color = [0, int(c), 0]) sensor.snapshot() time.sleep(1000) @@ -52,11 +37,6 @@ while(True): img = sensor.snapshot() for i in range(img.width()): c = ((i * 255) + (img.width()/2)) / img.width() - img.draw_line([i, 0, i, img.height()-1], [0, 0, int(c)]) - sensor.snapshot() - time.sleep(1000) - for i in range(img.width()): - c = (((img.width() - i) * 255) + (img.width()/2)) / img.width() img.draw_line([i, 0, i, img.height()-1], color = [0, 0, int(c)]) sensor.snapshot() time.sleep(1000)