Merge pull request #899 from pimoroni/jpegdec/width_height_fix
JPEGDEC: Backport width/height changes from pngdec.
This commit is contained in:
commit
ab64fcaccc
|
@ -26,6 +26,8 @@ typedef struct _JPEG_obj_t {
|
||||||
mp_obj_t file;
|
mp_obj_t file;
|
||||||
mp_buffer_info_t buf;
|
mp_buffer_info_t buf;
|
||||||
ModPicoGraphics_obj_t *graphics;
|
ModPicoGraphics_obj_t *graphics;
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
} _JPEG_obj_t;
|
} _JPEG_obj_t;
|
||||||
|
|
||||||
|
|
||||||
|
@ -174,6 +176,30 @@ mp_event_handle_nowait();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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, JPEGDraw);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(result != 1) mp_raise_msg(&mp_type_RuntimeError, "JPEG: could not read file/buffer.");
|
||||||
|
}
|
||||||
|
|
||||||
mp_obj_t _JPEG_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
mp_obj_t _JPEG_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
||||||
enum {
|
enum {
|
||||||
ARG_picographics
|
ARG_picographics
|
||||||
|
@ -208,6 +234,11 @@ mp_obj_t _JPEG_openFILE(mp_obj_t self_in, mp_obj_t filename) {
|
||||||
|
|
||||||
self->file = 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;
|
return mp_const_true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,6 +250,11 @@ mp_obj_t _JPEG_openRAM(mp_obj_t self_in, mp_obj_t buffer) {
|
||||||
|
|
||||||
self->file = 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;
|
return mp_const_true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
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
|
jpegdec_open_helper(self);
|
||||||
|
|
||||||
// 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
|
// Force a specific data output type to best match our PicoGraphics buffer
|
||||||
switch(self->graphics->graphics->pen_type) {
|
switch(self->graphics->graphics->pen_type) {
|
||||||
|
@ -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
|
// We need to store a pointer to the PicoGraphics surface
|
||||||
self->jpeg->setUserPointer((void *)self->graphics->graphics);
|
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;
|
current_flags = 0;
|
||||||
|
|
||||||
|
@ -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
|
// get_width
|
||||||
mp_obj_t _JPEG_getWidth(mp_obj_t self_in) {
|
mp_obj_t _JPEG_getWidth(mp_obj_t self_in) {
|
||||||
_JPEG_obj_t *self = MP_OBJ_TO_PTR2(self_in, _JPEG_obj_t);
|
_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
|
// get_height
|
||||||
mp_obj_t _JPEG_getHeight(mp_obj_t self_in) {
|
mp_obj_t _JPEG_getHeight(mp_obj_t self_in) {
|
||||||
_JPEG_obj_t *self = MP_OBJ_TO_PTR2(self_in, _JPEG_obj_t);
|
_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…
Reference in New Issue