Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MP jpegdec: fix width/height functions by opening file/buffer earlier #895

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 40 additions & 27 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,
self->decode_callback);

// 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 MICROPY_EVENT_POLL_HOOK
MICROPY_EVENT_POLL_HOOK
Expand Down Expand Up @@ -208,6 +234,11 @@ mp_obj_t _JPEG_openFILE(mp_obj_t self_in, mp_obj_t filename) {

self->file = filename;

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

return mp_const_true;
}

Expand All @@ -219,6 +250,11 @@ mp_obj_t _JPEG_openRAM(mp_obj_t self_in, mp_obj_t buffer) {

self->file = buffer;

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

return mp_const_true;
}

Expand All @@ -244,30 +280,7 @@ 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.");
jpegdec_open_helper(self);

// Force a specific data output type to best match our PicoGraphics buffer
switch(self->graphics->graphics->pen_type) {
Expand All @@ -293,7 +306,7 @@ 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;

Expand All @@ -306,13 +319,13 @@ mp_obj_t _JPEG_decode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args
// 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);
}

}
Loading