Merge pull request #2008 from kwagyeman/kwabena/add_file_path_image_loading

modules: Allow arg_to_image to load images from paths.
This commit is contained in:
Ibrahim Abdelkader 2023-11-01 21:42:52 +02:00 committed by GitHub
commit 5f4c6763f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 9 deletions

View File

@ -145,7 +145,8 @@ STATIC mp_obj_t py_display_write(uint n_args, const mp_obj_t *pos_args, mp_map_t
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
image_t *image = py_helper_arg_to_image(args[ARG_image].u_obj, 0); fb_alloc_mark();
image_t *image = py_helper_arg_to_image(args[ARG_image].u_obj, ARG_IMAGE_ANY | ARG_IMAGE_ALLOC);
rectangle_t roi = py_helper_arg_to_roi(args[ARG_roi].u_obj, image); rectangle_t roi = py_helper_arg_to_roi(args[ARG_roi].u_obj, image);
if (args[ARG_channel].u_int < -1 || args[ARG_channel].u_int > 2) { if (args[ARG_channel].u_int < -1 || args[ARG_channel].u_int > 2) {
@ -167,7 +168,6 @@ STATIC mp_obj_t py_display_write(uint n_args, const mp_obj_t *pos_args, mp_map_t
const uint16_t *color_palette = py_helper_arg_to_palette(args[ARG_color_palette].u_obj, PIXFORMAT_RGB565); const uint16_t *color_palette = py_helper_arg_to_palette(args[ARG_color_palette].u_obj, PIXFORMAT_RGB565);
const uint8_t *alpha_palette = py_helper_arg_to_palette(args[ARG_alpha_palette].u_obj, PIXFORMAT_GRAYSCALE); const uint8_t *alpha_palette = py_helper_arg_to_palette(args[ARG_alpha_palette].u_obj, PIXFORMAT_GRAYSCALE);
fb_alloc_mark();
py_display_p_t *display_p = (py_display_p_t *) MP_OBJ_TYPE_GET_SLOT(self->base.type, protocol); py_display_p_t *display_p = (py_display_p_t *) MP_OBJ_TYPE_GET_SLOT(self->base.type, protocol);
display_p->write(self, image, args[ARG_x].u_int, args[ARG_y].u_int, x_scale, y_scale, &roi, display_p->write(self, image, args[ARG_x].u_int, args[ARG_y].u_int, x_scale, y_scale, &roi,
args[ARG_channel].u_int, args[ARG_alpha].u_int, color_palette, alpha_palette, args[ARG_hint].u_int); args[ARG_channel].u_int, args[ARG_alpha].u_int, color_palette, alpha_palette, args[ARG_hint].u_int);

View File

@ -24,17 +24,33 @@ mp_obj_t py_func_unavailable(uint n_args, const mp_obj_t *args, mp_map_t *kw_arg
MP_DEFINE_CONST_FUN_OBJ_KW(py_func_unavailable_obj, 0, py_func_unavailable); MP_DEFINE_CONST_FUN_OBJ_KW(py_func_unavailable_obj, 0, py_func_unavailable);
image_t *py_helper_arg_to_image(const mp_obj_t arg, uint32_t flags) { image_t *py_helper_arg_to_image(const mp_obj_t arg, uint32_t flags) {
image_t *arg_img = py_image_cobj(arg); image_t *image = NULL;
if ((flags & ARG_IMAGE_ALLOC) && MP_OBJ_IS_STR(arg)) {
#if defined(IMLIB_ENABLE_IMAGE_FILE_IO)
const char *path = mp_obj_str_get_str(arg);
FIL fp;
image = xalloc(sizeof(image_t));
img_read_settings_t rs;
imlib_read_geometry(&fp, image, path, &rs);
file_close(&fp);
image->data = fb_alloc(image_size(image), FB_ALLOC_CACHE_ALIGN);
imlib_load_image(image, path);
#else
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Image I/O is not supported"));
#endif // IMLIB_ENABLE_IMAGE_FILE_IO
} else {
image = py_image_cobj(arg);
}
if (flags) { if (flags) {
if ((flags & ARG_IMAGE_MUTABLE) && !arg_img->is_mutable) { if ((flags & ARG_IMAGE_MUTABLE) && !image->is_mutable) {
mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("Expected a mutable image")); mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("Expected a mutable image"));
} else if ((flags & ARG_IMAGE_UNCOMPRESSED) && arg_img->is_compressed) { } else if ((flags & ARG_IMAGE_UNCOMPRESSED) && image->is_compressed) {
mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("Expected an uncompressed image")); mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("Expected an uncompressed image"));
} else if ((flags & ARG_IMAGE_GRAYSCALE) && arg_img->pixfmt != PIXFORMAT_GRAYSCALE) { } else if ((flags & ARG_IMAGE_GRAYSCALE) && image->pixfmt != PIXFORMAT_GRAYSCALE) {
mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("Expected an uncompressed image")); mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("Expected an uncompressed image"));
} }
} }
return arg_img; return image;
} }
const void *py_helper_arg_to_palette(const mp_obj_t arg, uint32_t pixfmt) { const void *py_helper_arg_to_palette(const mp_obj_t arg, uint32_t pixfmt) {

View File

@ -17,6 +17,7 @@ typedef enum py_helper_arg_image_flags {
ARG_IMAGE_MUTABLE = (1 << 0), ARG_IMAGE_MUTABLE = (1 << 0),
ARG_IMAGE_UNCOMPRESSED = (1 << 1), ARG_IMAGE_UNCOMPRESSED = (1 << 1),
ARG_IMAGE_GRAYSCALE = (1 << 2), ARG_IMAGE_GRAYSCALE = (1 << 2),
ARG_IMAGE_ALLOC = (1 << 3)
} py_helper_arg_image_flags_t; } py_helper_arg_image_flags_t;
extern const mp_obj_fun_builtin_var_t py_func_unavailable_obj; extern const mp_obj_fun_builtin_var_t py_func_unavailable_obj;

View File

@ -1555,8 +1555,9 @@ STATIC mp_obj_t py_image_draw_edges(uint n_args, const mp_obj_t *args, mp_map_t
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_draw_edges_obj, 2, py_image_draw_edges); STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_draw_edges_obj, 2, py_image_draw_edges);
STATIC mp_obj_t py_image_draw_image(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { STATIC mp_obj_t py_image_draw_image(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
fb_alloc_mark();
image_t *arg_img = py_helper_arg_to_image(args[0], ARG_IMAGE_MUTABLE); image_t *arg_img = py_helper_arg_to_image(args[0], ARG_IMAGE_MUTABLE);
image_t *arg_other = py_helper_arg_to_image(args[1], ARG_IMAGE_UNCOMPRESSED); image_t *arg_other = py_helper_arg_to_image(args[1], ARG_IMAGE_ANY | ARG_IMAGE_ALLOC);
const mp_obj_t *arg_vec; const mp_obj_t *arg_vec;
uint offset = py_helper_consume_array(n_args, args, 2, 2, &arg_vec); uint offset = py_helper_consume_array(n_args, args, 2, 2, &arg_vec);
@ -1634,7 +1635,6 @@ STATIC mp_obj_t py_image_draw_image(uint n_args, const mp_obj_t *args, mp_map_t
arg_y_scale = arg_x_scale; arg_y_scale = arg_x_scale;
} }
fb_alloc_mark();
imlib_draw_image(arg_img, arg_other, arg_x_off, arg_y_off, arg_x_scale, arg_y_scale, &arg_roi, imlib_draw_image(arg_img, arg_other, arg_x_off, arg_y_off, arg_x_scale, arg_y_scale, &arg_roi,
arg_rgb_channel, arg_alpha, color_palette, alpha_palette, hint, NULL, NULL, NULL); arg_rgb_channel, arg_alpha, color_palette, alpha_palette, hint, NULL, NULL, NULL);
fb_alloc_free_till_mark(); fb_alloc_free_till_mark();