Skip to content

Commit bff2e79

Browse files
committed
Inky 5.7: Dithered drawing ported from 7.3.
1 parent a5b0633 commit bff2e79

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

libraries/pico_graphics/pico_graphics.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ namespace pimoroni {
334334
class PicoGraphics_Pen3Bit : public PicoGraphics {
335335
public:
336336
static const uint16_t palette_size = 8;
337-
uint8_t color;
337+
uint color;
338338
RGB palette[8] = {
339339
/*
340340
{0x2b, 0x2a, 0x37},
@@ -365,10 +365,13 @@ namespace pimoroni {
365365
void set_pen(uint c) override;
366366
void set_pen(uint8_t r, uint8_t g, uint8_t b) override;
367367
void set_thickness(uint t) override {};
368+
int create_pen(uint8_t r, uint8_t g, uint8_t b) override;
369+
int create_pen_hsv(float h, float s, float v) override;
368370

369371
int get_palette_size() override {return palette_size;};
370372
RGB* get_palette() override {return palette;};
371373

374+
void _set_pixel(const Point &p, uint col);
372375
void set_pixel(const Point &p) override;
373376
void set_pixel_span(const Point &p, uint l) override;
374377
void get_dither_candidates(const RGB &col, const RGB *palette, size_t len, std::array<uint8_t, 16> &candidates);

libraries/pico_graphics/pico_graphics_pen_3bit.cpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ namespace pimoroni {
1010
}
1111
cache_built = false;
1212
}
13-
void PicoGraphics_Pen3Bit::set_pen(uint c) {
14-
color = c & 0xf;
15-
}
16-
void PicoGraphics_Pen3Bit::set_pen(uint8_t r, uint8_t g, uint8_t b) {
17-
}
18-
void PicoGraphics_Pen3Bit::set_pixel(const Point &p) {
13+
void PicoGraphics_Pen3Bit::_set_pixel(const Point &p, uint col) {
1914
uint offset = (bounds.w * bounds.h) / 8;
2015
uint8_t *buf = (uint8_t *)frame_buffer;
2116

@@ -25,27 +20,48 @@ namespace pimoroni {
2520
uint8_t *bufB = bufA + offset;
2621
uint8_t *bufC = bufA + offset + offset;
2722

28-
uint8_t cA = (color & 0b100) >> 2;
23+
uint8_t cA = (col & 0b100) >> 2;
2924
*bufA &= ~(1U << bo);
3025
*bufA |= (cA << bo);
3126

32-
uint8_t cB = (color & 0b010) >> 1;
27+
uint8_t cB = (col & 0b010) >> 1;
3328
*bufB &= ~(1U << bo);
3429
*bufB |= (cB << bo);
3530

36-
uint8_t cC = (color & 0b001);
31+
uint8_t cC = (col & 0b001);
3732
*bufC &= ~(1U << bo);
3833
*bufC |= (cC << bo);
3934
}
40-
35+
void PicoGraphics_Pen3Bit::set_pen(uint c) {
36+
color = c;
37+
}
38+
void PicoGraphics_Pen3Bit::set_pen(uint8_t r, uint8_t g, uint8_t b) {
39+
color = RGB(r, g, b).to_rgb888() | 0x7f000000;
40+
}
41+
int PicoGraphics_Pen3Bit::create_pen(uint8_t r, uint8_t g, uint8_t b) {
42+
return RGB(r, g, b).to_rgb888() | 0x7f000000;
43+
}
44+
int PicoGraphics_Pen3Bit::create_pen_hsv(float h, float s, float v) {
45+
return RGB::from_hsv(h, s, v).to_rgb888() | 0x7f000000;
46+
}
47+
void PicoGraphics_Pen3Bit::set_pixel(const Point &p) {
48+
if ((color & 0x7f000000) == 0x7f000000) {
49+
set_pixel_dither(p, RGB(color));
50+
} else {
51+
_set_pixel(p, color);
52+
}
53+
}
4154
void PicoGraphics_Pen3Bit::set_pixel_span(const Point &p, uint l) {
4255
Point lp = p;
4356
while(l--) {
44-
set_pixel(lp);
57+
if ((color & 0x7f000000) == 0x7f000000) {
58+
set_pixel_dither(lp, RGB(color));
59+
} else {
60+
_set_pixel(lp, color);
61+
}
4562
lp.x++;
4663
}
4764
}
48-
4965
void PicoGraphics_Pen3Bit::get_dither_candidates(const RGB &col, const RGB *palette, size_t len, std::array<uint8_t, 16> &candidates) {
5066
RGB error;
5167
for(size_t i = 0; i < candidates.size(); i++) {
@@ -87,8 +103,7 @@ namespace pimoroni {
87103

88104
// set the pixel
89105
//color = candidates[pattern[pattern_index]];
90-
color = candidate_cache[cache_key][dither16_pattern[pattern_index]];
91-
set_pixel(p);
106+
_set_pixel(p, candidate_cache[cache_key][dither16_pattern[pattern_index]]);
92107
}
93108
void PicoGraphics_Pen3Bit::frame_convert(PenType type, conversion_callback_func callback) {
94109
if(type == PEN_P4) {

0 commit comments

Comments
 (0)