Skip to content

Commit

Permalink
MP jpegdec: fix width/height functions by opening file/buffer earlier
Browse files Browse the repository at this point in the history
Open file/buffer in open function rather than on demand.
Also adds a close function for the user.
  • Loading branch information
Skyler84 authored and Gadgetoid committed Jan 23, 2024
1 parent 6b23c15 commit 54e5df8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
2 changes: 2 additions & 0 deletions micropython/modules/jpegdec/jpegdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) },
Expand Down
57 changes: 27 additions & 30 deletions micropython/modules/jpegdec/jpegdec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 };
Expand All @@ -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:
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions micropython/modules/jpegdec/jpegdec.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 54e5df8

Please sign in to comment.