diff --git a/libraries/jpegdec/JPEGDEC.cpp b/libraries/jpegdec/JPEGDEC.cpp index 39e33a4b..5ac0d1d4 100644 --- a/libraries/jpegdec/JPEGDEC.cpp +++ b/libraries/jpegdec/JPEGDEC.cpp @@ -188,11 +188,14 @@ int JPEGDEC::decode(int x, int y, int iOptions) _jpeg.iXOffset = x; _jpeg.iYOffset = y; _jpeg.iOptions = iOptions; + _jpeg.pDitherBuffer = nullptr; return DecodeJPEG(&_jpeg); } /* decode() */ -int JPEGDEC::decodeDither(uint8_t *pDither, int iOptions) +int JPEGDEC::decodeDither(int x, int y, uint8_t *pDither, int iOptions) { + _jpeg.iXOffset = x; + _jpeg.iYOffset = y; _jpeg.iOptions = iOptions; _jpeg.pDitherBuffer = pDither; return DecodeJPEG(&_jpeg); diff --git a/libraries/jpegdec/JPEGDEC.h b/libraries/jpegdec/JPEGDEC.h index 38c92a3a..8f88ff33 100644 --- a/libraries/jpegdec/JPEGDEC.h +++ b/libraries/jpegdec/JPEGDEC.h @@ -213,7 +213,7 @@ class JPEGDEC #endif void close(); int decode(int x, int y, int iOptions); - int decodeDither(uint8_t *pDither, int iOptions); + int decodeDither(int x, int y, uint8_t *pDither, int iOptions); int getOrientation(); int getWidth(); int getHeight(); diff --git a/micropython/modules/jpegdec/jpegdec.cpp b/micropython/modules/jpegdec/jpegdec.cpp index baa9d561..6a124bd3 100644 --- a/micropython/modules/jpegdec/jpegdec.cpp +++ b/micropython/modules/jpegdec/jpegdec.cpp @@ -38,7 +38,7 @@ int JPEGDraw(JPEGDRAW *pDraw) { for(int x = 0; x < pDraw->iWidth; x++) { int i = y * pDraw->iWidth + x; uint8_t p = pixels[i / 2]; - p >>= (i & 0b1) * 4; + p >>= (i & 0b1) ? 0 : 4; p &= 0xf; current_graphics->set_pen(p); current_graphics->pixel({pDraw->x + x, pDraw->y + y}); @@ -108,7 +108,7 @@ static int _open(_JPEG_obj_t *self, void *buf, size_t len) { break; // TODO currently uses EIGHT_BIT_GREYSCALE and shifts down should this use a 4-bit dither? case PicoGraphics::PEN_P4: - self->jpeg->setPixelType(EIGHT_BIT_GRAYSCALE); + self->jpeg->setPixelType(FOUR_BIT_DITHERED); break; // TODO 2-bit is currently unsupported case PicoGraphics::PEN_P2: @@ -174,7 +174,15 @@ 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 // since the JPEGDRAW struct has no userdata current_graphics = self->graphics->graphics; - int result = self->jpeg->decode(x, y, f); + int result = -1; + + if(self->graphics->graphics->pen_type == PicoGraphics::PEN_P4) { + uint8_t *buf = new uint8_t[1024]; + result = self->jpeg->decodeDither(x, y, buf, f); + delete[] buf; + } else { + result = self->jpeg->decode(x, y, f); + } current_graphics = nullptr; return result == 1 ? mp_const_true : mp_const_false; }