2022-06-13 11:26:21 +01:00
|
|
|
#include "pico_graphics.hpp"
|
|
|
|
|
|
|
|
namespace pimoroni {
|
2022-06-17 08:08:47 +01:00
|
|
|
|
2022-06-13 11:26:21 +01:00
|
|
|
PicoGraphics_PenP4::PicoGraphics_PenP4(uint16_t width, uint16_t height, void *frame_buffer)
|
|
|
|
: PicoGraphics(width, height, frame_buffer) {
|
|
|
|
this->pen_type = PEN_P4;
|
|
|
|
if(this->frame_buffer == nullptr) {
|
|
|
|
this->frame_buffer = (void *)(new uint8_t[buffer_size(width, height)]);
|
|
|
|
}
|
2022-06-14 15:06:44 +01:00
|
|
|
for(auto i = 0u; i < palette_size; i++) {
|
2022-06-13 11:26:21 +01:00
|
|
|
palette[i] = {
|
|
|
|
uint8_t(i << 4),
|
|
|
|
uint8_t(i << 4),
|
|
|
|
uint8_t(i << 4)
|
|
|
|
};
|
2022-06-14 15:06:44 +01:00
|
|
|
used[i] = false;
|
2022-06-13 11:26:21 +01:00
|
|
|
}
|
2022-06-14 15:25:50 +01:00
|
|
|
cache_built = false;
|
2022-06-13 11:26:21 +01:00
|
|
|
}
|
|
|
|
void PicoGraphics_PenP4::set_pen(uint c) {
|
|
|
|
color = c & 0xf;
|
2022-06-17 08:08:47 +01:00
|
|
|
}
|
2022-06-13 11:26:21 +01:00
|
|
|
void PicoGraphics_PenP4::set_pen(uint8_t r, uint8_t g, uint8_t b) {
|
2022-06-14 15:06:44 +01:00
|
|
|
int pen = RGB(r, g, b).closest(palette, palette_size);
|
2022-06-13 11:26:21 +01:00
|
|
|
if(pen != -1) color = pen;
|
|
|
|
}
|
|
|
|
int PicoGraphics_PenP4::update_pen(uint8_t i, uint8_t r, uint8_t g, uint8_t b) {
|
|
|
|
i &= 0xf;
|
2022-06-14 15:25:50 +01:00
|
|
|
used[i] = true;
|
2022-06-13 11:26:21 +01:00
|
|
|
palette[i] = {r, g, b};
|
2022-06-14 15:25:50 +01:00
|
|
|
cache_built = false;
|
2022-06-13 11:26:21 +01:00
|
|
|
return i;
|
|
|
|
}
|
2022-06-14 15:06:44 +01:00
|
|
|
int PicoGraphics_PenP4::create_pen(uint8_t r, uint8_t g, uint8_t b) {
|
|
|
|
// Create a colour and place it in the palette if there's space
|
|
|
|
for(auto i = 0u; i < palette_size; i++) {
|
|
|
|
if(!used[i]) {
|
|
|
|
palette[i] = {r, g, b};
|
|
|
|
used[i] = true;
|
2022-06-14 15:25:50 +01:00
|
|
|
cache_built = false;
|
2022-06-14 15:06:44 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2023-02-13 10:38:50 +00:00
|
|
|
int PicoGraphics_PenP4::create_pen_hsv(float h, float s, float v) {
|
2023-02-23 12:43:49 +00:00
|
|
|
RGB p = RGB::from_hsv(h, s, v);
|
|
|
|
return create_pen(p.r, p.g, p.b);
|
2023-02-13 10:38:50 +00:00
|
|
|
}
|
2022-06-14 15:06:44 +01:00
|
|
|
int PicoGraphics_PenP4::reset_pen(uint8_t i) {
|
|
|
|
palette[i] = {0, 0, 0};
|
|
|
|
used[i] = false;
|
2022-06-14 15:25:50 +01:00
|
|
|
cache_built = false;
|
2022-06-14 15:06:44 +01:00
|
|
|
return i;
|
|
|
|
}
|
2022-06-13 11:26:21 +01:00
|
|
|
void PicoGraphics_PenP4::set_pixel(const Point &p) {
|
2022-09-21 11:26:56 +01:00
|
|
|
auto i = (p.x + p.y * bounds.w);
|
|
|
|
|
2022-06-13 11:26:21 +01:00
|
|
|
// pointer to byte in framebuffer that contains this pixel
|
|
|
|
uint8_t *buf = (uint8_t *)frame_buffer;
|
2022-09-21 11:26:56 +01:00
|
|
|
uint8_t *f = &buf[i / 2];
|
2022-06-13 11:26:21 +01:00
|
|
|
|
2022-09-21 11:26:56 +01:00
|
|
|
uint8_t o = (~i & 0b1) * 4; // bit offset within byte
|
2022-06-13 11:26:21 +01:00
|
|
|
uint8_t m = ~(0b1111 << o); // bit mask for byte
|
|
|
|
uint8_t b = color << o; // bit value shifted to position
|
|
|
|
|
|
|
|
*f &= m; // clear bits
|
|
|
|
*f |= b; // set value
|
|
|
|
}
|
2022-06-17 08:08:47 +01:00
|
|
|
|
|
|
|
void PicoGraphics_PenP4::set_pixel_span(const Point &p, uint l) {
|
2022-09-21 11:26:56 +01:00
|
|
|
auto i = (p.x + p.y * bounds.w);
|
|
|
|
|
2022-06-17 08:08:47 +01:00
|
|
|
// pointer to byte in framebuffer that contains this pixel
|
|
|
|
uint8_t *buf = (uint8_t *)frame_buffer;
|
2022-09-21 11:26:56 +01:00
|
|
|
uint8_t *f = &buf[i / 2];
|
2022-06-17 08:08:47 +01:00
|
|
|
|
|
|
|
// doubled up color value, so the color is stored in both nibbles
|
|
|
|
uint8_t cc = color | (color << 4);
|
|
|
|
|
|
|
|
// handle the first pixel if not byte aligned
|
2022-09-21 11:26:56 +01:00
|
|
|
if(i & 0b1) {*f &= 0b11110000; *f |= (cc & 0b00001111); f++; l--;}
|
2022-06-17 08:08:47 +01:00
|
|
|
|
|
|
|
// write any double nibble pixels
|
|
|
|
while(l > 1) {*f++ = cc; l -= 2;}
|
|
|
|
|
|
|
|
// handle the last pixel if not byte aligned
|
|
|
|
if(l) {*f &= 0b00001111; *f |= (cc & 0b11110000);}
|
|
|
|
}
|
|
|
|
|
2022-06-14 15:06:44 +01:00
|
|
|
void PicoGraphics_PenP4::get_dither_candidates(const RGB &col, const RGB *palette, size_t len, std::array<uint8_t, 16> &candidates) {
|
|
|
|
RGB error;
|
|
|
|
for(size_t i = 0; i < candidates.size(); i++) {
|
|
|
|
candidates[i] = (col + error).closest(palette, len);
|
|
|
|
error += (col - palette[candidates[i]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort by a rough approximation of luminance, this ensures that neighbouring
|
|
|
|
// pixels in the dither matrix are at extreme opposites of luminence
|
|
|
|
// giving a more balanced output
|
|
|
|
std::sort(candidates.begin(), candidates.end(), [palette](int a, int b) {
|
|
|
|
return palette[a].luminance() > palette[b].luminance();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-13 11:26:21 +01:00
|
|
|
void PicoGraphics_PenP4::set_pixel_dither(const Point &p, const RGB &c) {
|
|
|
|
if(!bounds.contains(p)) return;
|
|
|
|
|
2022-06-24 08:55:30 +01:00
|
|
|
uint used_palette_entries = 0;
|
|
|
|
for(auto i = 0u; i < palette_size; i++) {
|
|
|
|
if(!used[i]) break;
|
|
|
|
used_palette_entries++;
|
|
|
|
}
|
|
|
|
|
2022-06-14 15:06:44 +01:00
|
|
|
if(!cache_built) {
|
|
|
|
for(uint i = 0; i < 512; i++) {
|
|
|
|
RGB cache_col((i & 0x1C0) >> 1, (i & 0x38) << 2, (i & 0x7) << 5);
|
2022-06-24 08:55:30 +01:00
|
|
|
get_dither_candidates(cache_col, palette, used_palette_entries, candidate_cache[i]);
|
2022-06-14 15:06:44 +01:00
|
|
|
}
|
|
|
|
cache_built = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint cache_key = ((c.r & 0xE0) << 1) | ((c.g & 0xE0) >> 2) | ((c.b & 0xE0) >> 5);
|
|
|
|
//get_dither_candidates(c, palette, 256, candidates);
|
2022-06-13 11:26:21 +01:00
|
|
|
|
|
|
|
// find the pattern coordinate offset
|
|
|
|
uint pattern_index = (p.x & 0b11) | ((p.y & 0b11) << 2);
|
|
|
|
|
|
|
|
// set the pixel
|
2022-06-14 15:06:44 +01:00
|
|
|
//color = candidates[pattern[pattern_index]];
|
2022-06-30 15:24:58 +01:00
|
|
|
color = candidate_cache[cache_key][dither16_pattern[pattern_index]];
|
2022-06-13 11:26:21 +01:00
|
|
|
set_pixel(p);
|
|
|
|
}
|
2022-08-23 23:07:13 +01:00
|
|
|
void PicoGraphics_PenP4::frame_convert(PenType type, conversion_callback_func callback) {
|
2022-06-13 11:26:21 +01:00
|
|
|
if(type == PEN_RGB565) {
|
|
|
|
// Cache the RGB888 palette as RGB565
|
2022-06-14 15:06:44 +01:00
|
|
|
RGB565 cache[palette_size];
|
|
|
|
for(auto i = 0u; i < palette_size; i++) {
|
2022-06-13 11:26:21 +01:00
|
|
|
cache[i] = palette[i].to_rgb565();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Treat our void* frame_buffer as uint8_t
|
|
|
|
uint8_t *src = (uint8_t *)frame_buffer;
|
2022-08-23 23:07:13 +01:00
|
|
|
uint8_t o = 4;
|
|
|
|
|
|
|
|
frame_convert_rgb565(callback, [&]() {
|
|
|
|
uint8_t c = *src;
|
|
|
|
uint8_t b = (c >> o) & 0xf; // bit value shifted to position
|
|
|
|
|
|
|
|
// Increment to next 4-bit entry
|
|
|
|
o ^= 4;
|
|
|
|
if (o != 0) ++src;
|
|
|
|
|
|
|
|
return cache[b];
|
|
|
|
});
|
2022-06-13 11:26:21 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-02 11:49:16 +01:00
|
|
|
}
|