Skip to content

Commit

Permalink
Merge pull request #907 from pimoroni/patch-pngdec-1bit
Browse files Browse the repository at this point in the history
Fixes for PNGDEC on Badger 2040 / Badger 2040 W
  • Loading branch information
Gadgetoid authored Feb 27, 2024
2 parents ab64fca + c4f70df commit d831074
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion libraries/pico_graphics/pico_graphics_pen_1bitY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace pimoroni {
}

void PicoGraphics_Pen1BitY::set_pen(uint8_t r, uint8_t g, uint8_t b) {
color = std::max(r, std::max(g, b));
color = std::max(r, std::max(g, b)) >> 4;
}

void PicoGraphics_Pen1BitY::set_pixel(const Point &p) {
Expand Down
12 changes: 8 additions & 4 deletions micropython/modules/pngdec/pngdec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,23 @@ mp_event_handle_nowait();
} else if (pDraw->iPixelType == PNG_PIXEL_INDEXED) {
for(int x = 0; x < pDraw->iWidth; x++) {
uint8_t i = 0;
if(pDraw->iBpp == 8) {
if(pDraw->iBpp == 8) { // 8bpp
i = *pixel++;
} else if (pDraw->iBpp == 4) {
} else if (pDraw->iBpp == 4) { // 4bpp
i = *pixel;
i >>= (x & 0b1) ? 0 : 4;
i &= 0xf;
if (x & 1) pixel++;
} else {
} else if (pDraw->iBpp == 2) { // 2bpp
i = *pixel;
i >>= 6 - ((x & 0b11) << 1);
i &= 0x3;
if ((x & 0b11) == 0b11) pixel++;
} else { // 1bpp
i = *pixel;
i >>= 7 - (x & 0b111);
i &= 0b1;
if ((x & 0b111) == 0b111) pixel++;
}
if(x < target->source.x || x >= target->source.x + target->source.w) continue;
// grab the colour from the palette
Expand Down Expand Up @@ -243,7 +248,6 @@ mp_event_handle_nowait();
}
}
}

} else {
current_graphics->set_pen(r, g, b);
current_graphics->rectangle({current_position.x, current_position.y, scale.x, scale.y});
Expand Down

0 comments on commit d831074

Please sign in to comment.