Skip to content

Commit

Permalink
JPEGDEC: Backport width/height changes from pngdec.
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyler84 authored and Gadgetoid committed Jan 23, 2024
1 parent 54e5df8 commit b4bedf4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
2 changes: 0 additions & 2 deletions micropython/modules/jpegdec/jpegdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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 @@ -13,7 +12,6 @@ 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
72 changes: 44 additions & 28 deletions micropython/modules/jpegdec/jpegdec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ typedef struct _JPEG_obj_t {
mp_obj_t file;
mp_buffer_info_t buf;
ModPicoGraphics_obj_t *graphics;
int width;
int height;
} _JPEG_obj_t;


Expand Down Expand Up @@ -83,6 +85,30 @@ int32_t jpegdec_seek_callback(JPEGFILE *jpeg, int32_t p) {
return seek_s.offset;
}

void jpegdec_open_helper(_JPEG_obj_t *self) {
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, self->decode_callback);
}

if(result != 0) mp_raise_msg(&mp_type_RuntimeError, "JPEG: could not read file/buffer.");
}

int JPEGDraw(JPEGDRAW *pDraw) {
#ifdef mp_event_handle_nowait
mp_event_handle_nowait();
Expand Down Expand Up @@ -205,20 +231,13 @@ 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;

GET_STR_DATA_LEN(filename, str, str_len);
self->file = filename;

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.");
jpegdec_open_helper(self);
self->width = self->jpeg->getWidth();
self->height = self->jpeg->getHeight();
self->jpeg->close();

return mp_const_true;
}
Expand All @@ -229,22 +248,14 @@ mp_obj_t _JPEG_openRAM(mp_obj_t self_in, mp_obj_t buffer) {

// TODO Check for valid 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->file = buffer;

jpegdec_open_helper(self);
self->width = self->jpeg->getWidth();
self->height = self->jpeg->getHeight();
self->jpeg->close();

return mp_const_none;
return mp_const_true;
}

// decode
Expand All @@ -269,6 +280,8 @@ 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;

jpegdec_open_helper(self);

// Force a specific data output type to best match our PicoGraphics buffer
switch(self->graphics->graphics->pen_type) {
case PicoGraphics::PEN_RGB332:
Expand All @@ -293,23 +306,26 @@ mp_obj_t _JPEG_decode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args
// We need to store a pointer to the PicoGraphics surface
self->jpeg->setUserPointer((void *)self->graphics->graphics);

result = self->jpeg->decode(x, y, f);
int result = self->jpeg->decode(x, y, f);

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;
}

// get_width
mp_obj_t _JPEG_getWidth(mp_obj_t self_in) {
_JPEG_obj_t *self = MP_OBJ_TO_PTR2(self_in, _JPEG_obj_t);
return mp_obj_new_int(self->jpeg->getWidth());
return mp_obj_new_int(self->width);
}

// get_height
mp_obj_t _JPEG_getHeight(mp_obj_t self_in) {
_JPEG_obj_t *self = MP_OBJ_TO_PTR2(self_in, _JPEG_obj_t);
return mp_obj_new_int(self->jpeg->getHeight());
return mp_obj_new_int(self->height);
}

}
1 change: 0 additions & 1 deletion micropython/modules/jpegdec/jpegdec.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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 b4bedf4

Please sign in to comment.