From 54e5df87d3b86e2db6498519d1d25b9a7645efdf Mon Sep 17 00:00:00 2001 From: Skyler Mansfield <90399509+Skyler84@users.noreply.github.com> Date: Fri, 12 Jan 2024 16:27:04 +0000 Subject: [PATCH] MP jpegdec: fix width/height functions by opening file/buffer earlier Open file/buffer in open function rather than on demand. Also adds a close function for the user. --- micropython/modules/jpegdec/jpegdec.c | 2 + micropython/modules/jpegdec/jpegdec.cpp | 57 ++++++++++++------------- micropython/modules/jpegdec/jpegdec.h | 1 + 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/micropython/modules/jpegdec/jpegdec.c b/micropython/modules/jpegdec/jpegdec.c index 85dfade93..3696fa4ac 100644 --- a/micropython/modules/jpegdec/jpegdec.c +++ b/micropython/modules/jpegdec/jpegdec.c @@ -3,6 +3,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(JPEG_del_obj, _JPEG_del); STATIC MP_DEFINE_CONST_FUN_OBJ_2(JPEG_openRAM_obj, _JPEG_openRAM); STATIC MP_DEFINE_CONST_FUN_OBJ_2(JPEG_openFILE_obj, _JPEG_openFILE); +STATIC MP_DEFINE_CONST_FUN_OBJ_2(JPEG_close_obj, _JPEG_close); STATIC MP_DEFINE_CONST_FUN_OBJ_KW(JPEG_decode_obj, 1, _JPEG_decode); STATIC MP_DEFINE_CONST_FUN_OBJ_1(JPEG_getWidth_obj, _JPEG_getWidth); STATIC MP_DEFINE_CONST_FUN_OBJ_1(JPEG_getHeight_obj, _JPEG_getHeight); @@ -12,6 +13,7 @@ STATIC const mp_rom_map_elem_t JPEG_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&JPEG_del_obj) }, { MP_ROM_QSTR(MP_QSTR_open_RAM), MP_ROM_PTR(&JPEG_openRAM_obj) }, { MP_ROM_QSTR(MP_QSTR_open_file), MP_ROM_PTR(&JPEG_openFILE_obj) }, + { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&JPEG_close_obj) }, { MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&JPEG_decode_obj) }, { MP_ROM_QSTR(MP_QSTR_get_width), MP_ROM_PTR(&JPEG_getWidth_obj) }, { MP_ROM_QSTR(MP_QSTR_get_height), MP_ROM_PTR(&JPEG_getHeight_obj) }, diff --git a/micropython/modules/jpegdec/jpegdec.cpp b/micropython/modules/jpegdec/jpegdec.cpp index 5c8e34058..f23e1a0ec 100644 --- a/micropython/modules/jpegdec/jpegdec.cpp +++ b/micropython/modules/jpegdec/jpegdec.cpp @@ -205,8 +205,20 @@ mp_obj_t _JPEG_openFILE(mp_obj_t self_in, mp_obj_t filename) { _JPEG_obj_t *self = MP_OBJ_TO_PTR2(self_in, _JPEG_obj_t); // TODO Check for valid filename, and maybe that file exists? + if (!mp_obj_is_str(self->file)) + return mp_const_false; - self->file = filename; + GET_STR_DATA_LEN(filename, str, str_len); + + result = self->jpeg->open( + (const char*)str, + jpegdec_open_callback, + jpegdec_close_callback, + jpegdec_read_callback, + jpegdec_seek_callback, + JPEGDraw); + + if(result != 1) mp_raise_msg(&mp_type_RuntimeError, "JPEG: could not read file."); return mp_const_true; } @@ -217,11 +229,24 @@ mp_obj_t _JPEG_openRAM(mp_obj_t self_in, mp_obj_t buffer) { // TODO Check for valid buffer - self->file = buffer; + mp_get_buffer_raise(buffer, &self->buf, MP_BUFFER_READ); + + result = self->jpeg->openRAM((uint8_t *)self->buf.buf, self->buf.len, JPEGDraw); + + if(result != 1) mp_raise_msg(&mp_type_RuntimeError, "JPEG: could not read buffer."); return mp_const_true; } +// close +mp_obj_t _JPEG_close(mp_obj_t self_in) { + _JPEG_obj_t *self = MP_OBJ_TO_PTR2(self_in, _JPEG_obj_t); + + self->jpeg->close(); + + return mp_const_none; +} + // decode mp_obj_t _JPEG_decode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_self, ARG_x, ARG_y, ARG_scale, ARG_dither }; @@ -244,31 +269,6 @@ mp_obj_t _JPEG_decode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args current_flags = args[ARG_dither].u_obj == mp_const_false ? FLAG_NO_DITHER : 0; - // Just-in-time open of the filename/buffer we stored in self->file via open_RAM or open_file - - // Source is a filename - int result = -1; - - if(mp_obj_is_str(self->file)){ - GET_STR_DATA_LEN(self->file, str, str_len); - - result = self->jpeg->open( - (const char*)str, - jpegdec_open_callback, - jpegdec_close_callback, - jpegdec_read_callback, - jpegdec_seek_callback, - JPEGDraw); - - // Source is a buffer - } else { - mp_get_buffer_raise(self->file, &self->buf, MP_BUFFER_READ); - - result = self->jpeg->openRAM((uint8_t *)self->buf.buf, self->buf.len, JPEGDraw); - } - - if(result != 1) mp_raise_msg(&mp_type_RuntimeError, "JPEG: could not read file/buffer."); - // Force a specific data output type to best match our PicoGraphics buffer switch(self->graphics->graphics->pen_type) { case PicoGraphics::PEN_RGB332: @@ -297,9 +297,6 @@ mp_obj_t _JPEG_decode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args current_flags = 0; - // Close the file since we've opened it on-demand - self->jpeg->close(); - return result == 1 ? mp_const_true : mp_const_false; } diff --git a/micropython/modules/jpegdec/jpegdec.h b/micropython/modules/jpegdec/jpegdec.h index fd1a59de4..bc6b13886 100644 --- a/micropython/modules/jpegdec/jpegdec.h +++ b/micropython/modules/jpegdec/jpegdec.h @@ -7,6 +7,7 @@ extern mp_obj_t _JPEG_make_new(const mp_obj_type_t *type, size_t n_args, size_t extern mp_obj_t _JPEG_del(mp_obj_t self_in); extern mp_obj_t _JPEG_openRAM(mp_obj_t self_in, mp_obj_t buffer); extern mp_obj_t _JPEG_openFILE(mp_obj_t self_in, mp_obj_t filename); +extern mp_obj_t _JPEG_close(mp_obj_t self_in); extern mp_obj_t _JPEG_decode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); extern mp_obj_t _JPEG_getWidth(mp_obj_t self_in); extern mp_obj_t _JPEG_getHeight(mp_obj_t self_in); \ No newline at end of file