From fddac54f86b99a1bc3a0b72009f1d9f9ca02bc76 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Thu, 11 Aug 2022 11:05:15 +0100 Subject: [PATCH] JPEGDEC: Add option to disable dither on RGB332. --- micropython/modules/jpegdec/jpegdec.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/micropython/modules/jpegdec/jpegdec.cpp b/micropython/modules/jpegdec/jpegdec.cpp index 853e7b291..f7b2b4c61 100644 --- a/micropython/modules/jpegdec/jpegdec.cpp +++ b/micropython/modules/jpegdec/jpegdec.cpp @@ -30,6 +30,11 @@ typedef struct _JPEG_obj_t { PicoGraphics *current_graphics = nullptr; +uint8_t current_flags = 0; + +enum FLAGS : uint8_t { + FLAG_NO_DITHER = 1u +}; void *jpegdec_open_callback(const char *filename, int32_t *size) { @@ -124,10 +129,14 @@ MICROPY_EVENT_POLL_HOOK for(int x = 0; x < pDraw->iWidth; x++) { int i = y * pDraw->iWidth + x; if (current_graphics->pen_type == PicoGraphics::PEN_RGB332) { - //current_graphics->set_pen(RGB((RGB565)pDraw->pPixels[i]).to_rgb332()); - //current_graphics->pixel({pDraw->x + x, pDraw->y + y}); - // TODO make dither optional - current_graphics->set_pixel_dither({pDraw->x + x, pDraw->y + y}, (RGB565)(pDraw->pPixels[i])); + if (current_flags & FLAG_NO_DITHER) { + // Posterized output to RGB332 + current_graphics->set_pen(RGB((RGB565)pDraw->pPixels[i]).to_rgb332()); + current_graphics->pixel({pDraw->x + x, pDraw->y + y}); + } else { + // Dithered output to RGB332 + current_graphics->set_pixel_dither({pDraw->x + x, pDraw->y + y}, (RGB565)(pDraw->pPixels[i])); + } } else if (current_graphics->pen_type == PicoGraphics::PEN_P8 || current_graphics->pen_type == PicoGraphics::PEN_P4 || current_graphics->pen_type == PicoGraphics::PEN_3BIT) { current_graphics->set_pixel_dither({pDraw->x + x, pDraw->y + y}, RGB((RGB565)pDraw->pPixels[i])); } else { @@ -190,12 +199,13 @@ mp_obj_t _JPEG_openRAM(mp_obj_t self_in, mp_obj_t buffer) { // 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 }; + enum { ARG_self, ARG_x, ARG_y, ARG_scale, ARG_dither }; static const mp_arg_t allowed_args[] = { { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_x, MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_y, MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_scale, MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_dither, MP_ARG_OBJ, {.u_obj = mp_const_true} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; @@ -207,6 +217,8 @@ mp_obj_t _JPEG_decode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args int y = args[ARG_y].u_int; int f = args[ARG_scale].u_int; + 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 @@ -258,6 +270,7 @@ mp_obj_t _JPEG_decode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args result = self->jpeg->decode(x, y, f); current_graphics = nullptr; + current_flags = 0; // Close the file since we've opened it on-demand self->jpeg->close();