PicoGraphics: Palettes as RGB, Scanline convert with callback.

driver/sh1107
Phil Howard 2022-06-09 20:21:46 +01:00
rodzic 8917505175
commit 62818a5f49
5 zmienionych plików z 182 dodań i 117 usunięć

Wyświetl plik

@ -172,12 +172,10 @@ namespace pimoroni {
gpio_put(dc, 1); // data mode
gpio_put(cs, 0);
uint16_t row_buf[width];
for(auto y = 0u; y < height; y++) {
graphics->get_row_rgb565(&row_buf, width * y, width);
// TODO: Add DMA->SPI / PIO while we prep the next row
spi_write_blocking(spi, (const uint8_t*)row_buf, width * sizeof(uint16_t));
}
graphics->scanline_convert(PicoGraphics::PEN_RGB565, [this](void *data, size_t length) {
spi_write_blocking(spi, (const uint8_t*)data, length);
});
gpio_put(cs, 1);
}
}

Wyświetl plik

@ -13,7 +13,7 @@ namespace pimoroni {
COL_ORDER = 0b01000000,
SWAP_XY = 0b00100000, // AKA "MV"
SCAN_ORDER = 0b00010000,
RGB = 0b00001000,
RGB_BGR = 0b00001000,
HORIZ_ORDER = 0b00000100
};
@ -255,13 +255,10 @@ namespace pimoroni {
spi_write_blocking(spi, &cmd, 1);
gpio_put(dc, 1); // data mode
uint16_t row_buf[width];
for(auto y = 0u; y < height; y++) {
graphics->get_row_rgb565(&row_buf, width * y, width);
// TODO: Add DMA->SPI / PIO while we prep the next row
spi_write_blocking(spi, (const uint8_t*)row_buf, width * sizeof(uint16_t));
}
graphics->scanline_convert(PicoGraphics::PEN_RGB565, [this](void *data, size_t length) {
spi_write_blocking(spi, (const uint8_t*)data, length);
});
gpio_put(cs, 1);
} else { // Parallel Bus
gpio_put(dc, 0); // command mode
@ -269,12 +266,9 @@ namespace pimoroni {
write_blocking_parallel(&cmd, 1);
gpio_put(dc, 1); // data mode
uint16_t row_buf[width];
for(auto y = 0u; y < height; y++) {
graphics->get_row_rgb565(&row_buf, width * y, width);
// TODO: Add DMA->SPI / PIO while we prep the next row
write_blocking_parallel((const uint8_t*)row_buf, width * sizeof(uint16_t));
}
graphics->scanline_convert(PicoGraphics::PEN_RGB565, [this](void *data, size_t length) {
write_blocking_parallel((const uint8_t*)data, length);
});
gpio_put(cs, 1);
}

Wyświetl plik

@ -8,7 +8,7 @@ namespace pimoroni {
int PicoGraphics::reset_pen(uint8_t i) {return -1;};
int PicoGraphics::create_pen(uint8_t r, uint8_t g, uint8_t b) {return -1;};
void PicoGraphics::set_pixel(const Point &p) {};
void PicoGraphics::get_row_rgb565(void *result, uint offset, uint length) {};
void PicoGraphics::scanline_convert(PenType type, conversion_callback_func callback) {};
void PicoGraphics::set_dimensions(int width, int height) {
bounds = clip = {0, 0, width, height};

Wyświetl plik

@ -4,6 +4,7 @@
#include <cstdint>
#include <algorithm>
#include <vector>
#include <functional>
#include "libraries/hershey_fonts/hershey_fonts.hpp"
#include "libraries/bitmap_fonts/bitmap_fonts.hpp"
@ -22,6 +23,60 @@
namespace pimoroni {
typedef uint8_t RGB332;
typedef uint16_t RGB565;
struct RGB {
uint8_t r, g, b;
constexpr RGB() : r(0), g(0), b(0) {}
constexpr RGB(RGB332 c) :
r((c & 0b11100000) >> 0),
g((c & 0b00011100) << 3),
b((c & 0b00000011) << 6) {}
constexpr RGB(RGB565 c) :
r((__builtin_bswap16(c) & 0b1111100000000000) >> 8),
g((__builtin_bswap16(c) & 0b0000011111100000) >> 3),
b((__builtin_bswap16(c) & 0b0000000000011111) << 3) {}
constexpr RGB(uint8_t r, uint8_t g, uint8_t b) : r(r), g(g), b(b) {}
// a rough approximation of how bright a colour is used to compare the
// relative brightness of two colours
int luminance() {
// weights based on https://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/
return r * 21 + g * 72 * b * 7;
}
// a relatively low cost approximation of how "different" two colours are
// perceived which avoids expensive colour space conversions.
// described in detail at https://www.compuphase.com/cmetric.htm
int distance(RGB c) {
int rmean = ((int)r + c.r) / 2;
int rx = (int)r - c.r, gx = (int)g - c.g, bx = (int)b - c.b;
return abs((int)(
(((512 + rmean) * rx * rx) >> 8) + 4 * gx * gx + (((767 - rmean) * bx * bx) >> 8)
));
}
int closest(const RGB *palette, size_t len) {
int d = INT_MAX, m = -1;
for(size_t i = 0; i < len; i++) {
int dc = distance(palette[i]);
if(dc < d) {m = i; d = dc;}
}
return m;
}
constexpr RGB565 to_rgb565() {
uint16_t p = ((r & 0b11111000) << 8) |
((g & 0b11111100) << 3) |
((b & 0b11111000) >> 3);
return __builtin_bswap16(p);
}
constexpr RGB565 to_rgb332() {
return (r & 0b11100000) | ((g & 0b11100000) >> 3) | ((b & 0b11000000) >> 6);
}
};
typedef int Pen;
struct Rect;
@ -57,11 +112,6 @@ namespace pimoroni {
class PicoGraphics {
public:
struct PaletteEntry {
RGB565 color;
bool used;
};
enum PenType {
PEN_P2 = 0,
PEN_P4,
@ -76,11 +126,13 @@ namespace pimoroni {
Rect bounds;
Rect clip;
typedef std::function<void(void *data, size_t length)> conversion_callback_func;
const bitmap::font_t *bitmap_font;
const hershey::font_t *hershey_font;
static constexpr RGB332 rgb_to_rgb332(uint8_t r, uint8_t g, uint8_t b) {
return (r & 0b11100000) | ((g & 0b11100000) >> 3) | ((b & 0b11000000) >> 6);
return RGB(r, g, b).to_rgb332();
}
static constexpr RGB565 rgb332_to_rgb565(RGB332 c) {
@ -98,24 +150,15 @@ namespace pimoroni {
}
static constexpr RGB565 rgb_to_rgb565(uint8_t r, uint8_t g, uint8_t b) {
uint16_t p = ((r & 0b11111000) << 8) |
((g & 0b11111100) << 3) |
((b & 0b11111000) >> 3);
return __builtin_bswap16(p);
return RGB(r, g, b).to_rgb565();
}
static constexpr void rgb332_to_rgb(RGB332 c, uint8_t &r, uint8_t &g, uint8_t &b) {
r = (c & 0b11100000) >> 0;
g = (c & 0b00011100) << 3;
b = (c & 0b00000011) << 6;
static constexpr RGB rgb332_to_rgb(RGB332 c) {
return RGB((RGB332)c);
};
static constexpr void rgb565_to_rgb(RGB565 c, uint8_t &r, uint8_t &g, uint8_t &b) {
c = __builtin_bswap16(c);
r = (c & 0b1111100000000000) >> 8;
g = (c & 0b0000011111100000) >> 3;
b = (c & 0b0000000000011111) << 3;
static constexpr RGB rgb565_to_rgb(RGB565 c) {
return RGB((RGB565)c);
};
PicoGraphics(uint16_t width, uint16_t height, void *frame_buffer)
@ -129,7 +172,7 @@ namespace pimoroni {
virtual int reset_pen(uint8_t i);
virtual int create_pen(uint8_t r, uint8_t g, uint8_t b);
virtual void set_pixel(const Point &p);
virtual void get_row_rgb565(void *result, uint offset, uint length);
virtual void scanline_convert(PenType type, conversion_callback_func callback);
void set_font(const bitmap::font_t *font);
void set_font(const hershey::font_t *font);
@ -161,17 +204,8 @@ namespace pimoroni {
class PicoGraphics_PenP4 : public PicoGraphics {
public:
uint8_t color;
PaletteEntry palette[16];
const RGB565 default_palette[16] = {
rgb_to_rgb565(57, 48, 57), // Black
rgb_to_rgb565(255, 255, 255), // White
rgb_to_rgb565(58, 91, 70), // Green
rgb_to_rgb565(61, 59, 94), // Blue
rgb_to_rgb565(156, 72, 75), // Red
rgb_to_rgb565(208, 190, 71), // Yellow
rgb_to_rgb565(177, 106, 73), // Orange
rgb_to_rgb565(255, 255, 255) // Clear
};
RGB palette[16];
PicoGraphics_PenP4(uint16_t width, uint16_t height, void *frame_buffer)
: PicoGraphics(width, height, frame_buffer) {
this->pen_type = PEN_P4;
@ -179,28 +213,23 @@ namespace pimoroni {
this->frame_buffer = (void *)(new uint8_t[buffer_size(width, height)]);
}
for(auto i = 0u; i < 16; i++) {
palette[i].used = false;
}
for(auto i = 0u; i < 8; i++) {
palette[i].color = default_palette[i];
palette[i].used = true;
palette[i] = {
uint8_t(i << 4),
uint8_t(i << 4),
uint8_t(i << 4)
};
}
}
void set_pen(uint c) {
color = c & 0xf;
}
void set_pen(uint8_t r, uint8_t g, uint8_t b) override {
// TODO look up closest palette colour, or just NOOP?
int pen = RGB(r, g, b).closest(palette, 16);
if(pen != -1) color = pen;
}
int update_pen(uint8_t i, uint8_t r, uint8_t g, uint8_t b) override {
i &= 0xf;
palette[i].color = rgb_to_rgb565(r, g, b);
palette[i].used = true;
return i;
}
int reset_pen(uint8_t i) override {
i &= 0xf;
palette[i].color = default_palette[i];
palette[i] = {r, g, b};
return i;
}
void set_pixel(const Point &p) override {
@ -215,14 +244,29 @@ namespace pimoroni {
*f &= m; // clear bits
*f |= b; // set value
}
void get_row_rgb565(void *result, uint offset, uint length) override {
uint8_t *src = (uint8_t *)frame_buffer;
uint16_t *dst = (uint16_t *)result;
for(auto x = 0u; x < length; x++) {
uint8_t c = src[(offset / 2) + (x / 2)];
uint8_t o = (~x & 0b1) * 4; // bit offset within byte
uint8_t b = (c >> o) & 0xf; // bit value shifted to position
dst[x] = palette[b].color;
void scanline_convert(PenType type, conversion_callback_func callback) override {
if(type == PEN_RGB565) {
// Cache the RGB888 palette as RGB565
RGB565 cache[16];
for(auto i = 0u; i < 16; i++) {
cache[i] = palette[i].to_rgb565();
}
// Treat our void* frame_buffer as uint8_t
uint8_t *src = (uint8_t *)frame_buffer;
// Allocate a per-row temporary buffer
uint16_t row_buf[bounds.w];
for(auto y = 0; y < bounds.h; y++) {
for(auto x = 0; x < bounds.w; x++) {
uint8_t c = src[(bounds.w * y / 2) + (x / 2)];
uint8_t o = (~x & 0b1) * 4; // bit offset within byte
uint8_t b = (c >> o) & 0xf; // bit value shifted to position
row_buf[x] = cache[b];
}
// Callback to the driver with the row data
callback(row_buf, bounds.w * sizeof(RGB565));
}
}
}
static size_t buffer_size(uint w, uint h) {
@ -233,7 +277,8 @@ namespace pimoroni {
class PicoGraphics_PenP8 : public PicoGraphics {
public:
uint8_t color;
PaletteEntry palette[256];
RGB palette[256];
bool used[256];
PicoGraphics_PenP8(uint16_t width, uint16_t height, void *frame_buffer)
: PicoGraphics(width, height, frame_buffer) {
this->pen_type = PEN_P8;
@ -241,48 +286,62 @@ namespace pimoroni {
this->frame_buffer = (void *)(new uint8_t[buffer_size(width, height)]);
}
for(auto i = 0u; i < 256; i++) {
reset_pen(i);
palette[i] = {0, 0, 0};
used[i] = false;
}
}
void set_pen(uint c) override {
color = c;
}
void set_pen(uint8_t r, uint8_t g, uint8_t b) override {
// TODO look up closest palette colour, or just NOOP?
int pen = RGB(r, g, b).closest(palette, 16);
if(pen != -1) color = pen;
}
int update_pen(uint8_t i, uint8_t r, uint8_t g, uint8_t b) override {
i &= 0xff;
palette[i].color = rgb_to_rgb565(r, g, b);
palette[i].used = true;
return i;
}
int reset_pen(uint8_t i) override {
i &= 0xff;
palette[i].color = 0;
palette[i].used = false;
palette[i] = {r, g, b};
return i;
}
int create_pen(uint8_t r, uint8_t g, uint8_t b) override {
// Create a colour and place it in the palette if there's space
RGB565 c = rgb_to_rgb565(r, g, b);
for(auto i = 0u; i < 256u; i++) {
if(!palette[i].used) {
palette[i].color = c;
palette[i].used = true;
if(!used[i]) {
palette[i] = {r, g, b};
used[i] = true;
return i;
}
}
return -1;
}
int reset_pen(uint8_t i) {
palette[i] = {0, 0, 0};
used[i] = false;
return i;
}
void set_pixel(const Point &p) override {
uint8_t *buf = (uint8_t *)frame_buffer;
buf[p.y * bounds.w + p.x] = color;
}
void get_row_rgb565(void *result, uint offset, uint length) override {
uint8_t *src = (uint8_t *)frame_buffer;
uint16_t *dst = (uint16_t *)result;
for(auto x = 0u; x < length; x++) {
dst[x] = palette[src[offset + x]].color;
void scanline_convert(PenType type, conversion_callback_func callback) override {
if(type == PEN_RGB565) {
// Cache the RGB888 palette as RGB565
RGB565 cache[256];
for(auto i = 0u; i < 256; i++) {
cache[i] = palette[i].to_rgb565();
}
// Treat our void* frame_buffer as uint8_t
uint8_t *src = (uint8_t *)frame_buffer;
// Allocate a per-row temporary buffer
uint16_t row_buf[bounds.w];
for(auto y = 0; y < bounds.h; y++) {
for(auto x = 0; x < bounds.w; x++) {
row_buf[x] = cache[src[bounds.w * y + x]];
}
// Callback to the driver with the row data
callback(row_buf, bounds.w * sizeof(RGB565));
}
}
}
static size_t buffer_size(uint w, uint h) {
@ -293,7 +352,7 @@ namespace pimoroni {
class PicoGraphics_PenRGB332 : public PicoGraphics {
public:
RGB332 color;
PaletteEntry palette[256];
RGB palette[256];
PicoGraphics_PenRGB332(uint16_t width, uint16_t height, void *frame_buffer)
: PicoGraphics(width, height, frame_buffer) {
this->pen_type = PEN_RGB332;
@ -301,8 +360,7 @@ namespace pimoroni {
this->frame_buffer = (void *)(new uint8_t[buffer_size(width, height)]);
}
for(auto i = 0u; i < 256; i++) {
palette[i].color = rgb332_to_rgb565(i);
palette[i].used = true;
palette[i] = RGB((RGB332)i);
}
}
void set_pen(uint c) override {
@ -318,11 +376,26 @@ namespace pimoroni {
uint8_t *buf = (uint8_t *)frame_buffer;
buf[p.y * bounds.w + p.x] = color;
}
void get_row_rgb565(void *result, uint offset, uint length) override {
uint8_t *src = (uint8_t *)frame_buffer;
uint16_t *dst = (uint16_t *)result;
for(auto x = 0u; x < length; x++) {
dst[x] = palette[src[offset + x]].color;
void scanline_convert(PenType type, conversion_callback_func callback) override {
if(type == PEN_RGB565) {
// Cache the RGB888 palette as RGB565
RGB565 cache[256];
for(auto i = 0u; i < 256; i++) {
cache[i] = palette[i].to_rgb565();
}
// Treat our void* frame_buffer as uint8_t
uint8_t *src = (uint8_t *)frame_buffer;
// Allocate a per-row temporary buffer
uint16_t row_buf[bounds.w];
for(auto y = 0; y < bounds.h; y++) {
for(auto x = 0; x < bounds.w; x++) {
row_buf[x] = cache[src[bounds.w * y + x]];
}
// Callback to the driver with the row data
callback(row_buf, bounds.w * sizeof(RGB565));
}
}
}
static size_t buffer_size(uint w, uint h) {
@ -332,6 +405,7 @@ namespace pimoroni {
class PicoGraphics_PenRGB565 : public PicoGraphics {
public:
RGB src_color;
RGB565 color;
PicoGraphics_PenRGB565(uint16_t width, uint16_t height, void *frame_buffer)
: PicoGraphics(width, height, frame_buffer) {
@ -344,7 +418,8 @@ namespace pimoroni {
color = c;
}
void set_pen(uint8_t r, uint8_t g, uint8_t b) override {
color = rgb_to_rgb565(r, g, b);
src_color = {r, g, b};
color = src_color.to_rgb565();
}
int create_pen(uint8_t r, uint8_t g, uint8_t b) override {
return rgb_to_rgb565(r, g, b);

Wyświetl plik

@ -255,41 +255,39 @@ mp_obj_t ModPicoGraphics_set_backlight(mp_obj_t self_in, mp_obj_t brightness) {
}
mp_obj_t ModPicoGraphics_module_RGB332_to_RGB(mp_obj_t rgb332) {
uint8_t r, g, b;
PicoGraphics::rgb332_to_rgb(mp_obj_get_int(rgb332), r, g, b);
RGB c((RGB332)mp_obj_get_int(rgb332));
mp_obj_t t[] = {
mp_obj_new_int(r),
mp_obj_new_int(g),
mp_obj_new_int(b),
mp_obj_new_int(c.r),
mp_obj_new_int(c.g),
mp_obj_new_int(c.b),
};
return mp_obj_new_tuple(3, t);
}
mp_obj_t ModPicoGraphics_module_RGB565_to_RGB(mp_obj_t rgb565) {
uint8_t r, g, b;
PicoGraphics::rgb565_to_rgb(mp_obj_get_int(rgb565), r, g, b);
RGB c((RGB565)mp_obj_get_int(rgb565));
mp_obj_t t[] = {
mp_obj_new_int(r),
mp_obj_new_int(g),
mp_obj_new_int(b),
mp_obj_new_int(c.r),
mp_obj_new_int(c.g),
mp_obj_new_int(c.b),
};
return mp_obj_new_tuple(3, t);
}
mp_obj_t ModPicoGraphics_module_RGB_to_RGB332(mp_obj_t r, mp_obj_t g, mp_obj_t b) {
return mp_obj_new_int(PicoGraphics::rgb_to_rgb332(
return mp_obj_new_int(RGB(
mp_obj_get_int(r),
mp_obj_get_int(g),
mp_obj_get_int(b)
));
).to_rgb332());
}
mp_obj_t ModPicoGraphics_module_RGB_to_RGB565(mp_obj_t r, mp_obj_t g, mp_obj_t b) {
return mp_obj_new_int(PicoGraphics::rgb_to_rgb565(
return mp_obj_new_int(RGB(
mp_obj_get_int(r),
mp_obj_get_int(g),
mp_obj_get_int(b)
));
).to_rgb565());
}
mp_obj_t ModPicoGraphics_set_pen(mp_obj_t self_in, mp_obj_t pen) {