Grayscale quantization and fixing some defaults to support rgb565

This commit is contained in:
2026-03-15 22:01:45 -04:00
parent 7f296f9857
commit 2c79be36ef
5 changed files with 368 additions and 288 deletions

View File

@@ -91,7 +91,8 @@ internal esp_err_t api_devices_screen_image_handler(httpd_req_t *req)
// 1. Prepare the XML payload
const char *xml_to_register = NULL;
static char xml_buffer[DEVICE_XML_MAX + 100]; // static buffer to avoid stack overflow
static char
xml_buffer[DEVICE_XML_MAX + 100]; // static buffer to avoid stack overflow
if (dev->xml_layout[0] == '\0')
{
@@ -106,16 +107,6 @@ internal esp_err_t api_devices_screen_image_handler(httpd_req_t *req)
ESP_LOGI(kTagDeviceScreenImage,
"XML already contains <screen>, passing directly to parser.");
}
else
{
// Backwards compatibility for early setups - wrap it in screen and view
snprintf(xml_buffer, sizeof(xml_buffer),
"<screen>\n<view name=\"current_device\" width=\"100%%\" height=\"100%%\">\n%s\n</view>\n</screen>",
dev->xml_layout);
xml_to_register = xml_buffer;
ESP_LOGI(kTagDeviceScreenImage,
"Legacy XML without <screen> detected. Wrapped automatically.");
}
// 2. Register the XML payload as a component
lv_result_t res =
@@ -131,7 +122,8 @@ internal esp_err_t api_devices_screen_image_handler(httpd_req_t *req)
if (new_scr)
{
// We must load this newly created screen to make it active before rendering
// We must load this newly created screen to make it active before
// rendering
lv_screen_load(new_scr);
scr = new_scr; // Update local pointer since active screen changed
render_success = true;
@@ -175,14 +167,13 @@ internal esp_err_t api_devices_screen_image_handler(httpd_req_t *req)
uint32_t width = CONFIG_CALENDINK_DISPLAY_WIDTH;
uint32_t height = CONFIG_CALENDINK_DISPLAY_HEIGHT;
// Handle stride != width
uint8_t *packed_data = (uint8_t *)draw_buf->data;
bool needs_free = false;
if (draw_buf->header.stride != width)
// Allocate bounding memory for quantizing RGB565 buffer into tightly packed
// 8-bit PNG data.
uint8_t *packed_data =
(uint8_t *)heap_caps_malloc(width * height, MALLOC_CAP_SPIRAM);
if (!packed_data)
{
packed_data =
(uint8_t *)heap_caps_malloc(width * height, MALLOC_CAP_SPIRAM);
packed_data = (uint8_t *)malloc(width * height);
if (!packed_data)
{
xSemaphoreGive(g_LvglMutex);
@@ -191,11 +182,33 @@ internal esp_err_t api_devices_screen_image_handler(httpd_req_t *req)
"Out of memory");
return ESP_FAIL;
}
needs_free = true;
for (uint32_t y = 0; y < height; ++y)
}
// LVGL renders into RGB565 (2 bytes per pixel).
// Parse pixels, extract luminance, and quantize to 4 levels (0, 85, 170, 255).
for (uint32_t y = 0; y < height; ++y)
{
const uint16_t *src_row = (const uint16_t *)((const uint8_t *)draw_buf->data + (y * draw_buf->header.stride));
uint8_t *dst_row = packed_data + (y * width);
for (uint32_t x = 0; x < width; ++x)
{
memcpy(packed_data + (y * width),
(uint8_t *)draw_buf->data + (y * draw_buf->header.stride), width);
uint16_t c = src_row[x];
// Expand 5/6/5 components
uint8_t r_5 = (c >> 11) & 0x1F;
uint8_t g_6 = (c >> 5) & 0x3F;
uint8_t b_5 = c & 0x1F;
// Unpack to 8-bit true values
uint8_t r = (r_5 << 3) | (r_5 >> 2);
uint8_t g = (g_6 << 2) | (g_6 >> 4);
uint8_t b = (b_5 << 3) | (b_5 >> 2);
// Simple luminance
uint8_t lum = (r * 77 + g * 150 + b * 29) >> 8;
// 4-level linear quantization (0, 85, 170, 255)
dst_row[x] = (lum >> 6) * 85;
}
}
@@ -210,10 +223,7 @@ internal esp_err_t api_devices_screen_image_handler(httpd_req_t *req)
unsigned error = lodepng_encode_memory(&png, &pngsize, packed_data, width,
height, LCT_GREY, 8);
if (needs_free)
{
free(packed_data);
}
free(packed_data);
xSemaphoreGive(g_LvglMutex);