aboutsummaryrefslogtreecommitdiff
path: root/sixel.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2025-02-21 10:01:29 +0000
committerDaniel Eklöf <daniel@ekloef.se>2025-03-05 17:45:01 +0000
commitccf625b9914917e131beb83379c526bab1927edc (patch)
treec996479765fcb3aba2547f211ae7e6f848a4b264 /sixel.c
parentconfig: add search-bindings.delete-to-{start,end} key bindings (diff)
render: gamma-correct blending
This implements gamma-correct blending, which mainly affects font rendering. The implementation requires compile-time availability of the new color-management protocol (available in wayland-protocols >= 1.41), and run-time support for the same in the compositor (specifically, the EXT_LINEAR TF function and sRGB primaries). How it works: all colors are decoded from sRGB to linear (using a lookup table, generated in the exact same way pixman generates it's internal conversion tables) before being used by pixman. The resulting image buffer is thus in decoded/linear format. We use the color-management protocol to inform the compositor of this, by tagging the wayland surfaces with the 'ext_linear' image attribute. Sixes: all colors are sRGB internally, and decoded to linear before being used in any sixels. Thus, the image buffers will contain linear colors. This is important, since otherwise there would be a decode/encode penalty every time a sixel is blended to the grid. Emojis: we require fcft >= 3.2, which adds support for sRGB decoding color glyphs. Meaning, the emoji pixman surfaces can be blended directly to the grid, just like sixels. Gamma-correct blending is enabled by default *when the compositor supports it*. There's a new option to explicitly enable/disable it: gamma-correct-blending=no|yes. If set to 'yes', and the compositor does not implement the required color-management features, warning logs are emitted. There's a loss of precision when storing linear pixels in 8-bit channels. For this reason, this patch also adds supports for 10-bit surfaces. For now, this is disabled by default since such surfaces only have 2 bits for alpha. It can be enabled with tweak.surface-bit-depth=10-bit. Perhaps, in the future, we can enable it by default if: * gamma-correct blending is enabled * the user has not enabled a transparent background
Diffstat (limited to 'sixel.c')
-rw-r--r--sixel.c97
1 files changed, 81 insertions, 16 deletions
diff --git a/sixel.c b/sixel.c
index 44a5995b..dd933d7a 100644
--- a/sixel.c
+++ b/sixel.c
@@ -10,6 +10,7 @@
#include "grid.h"
#include "hsl.h"
#include "render.h"
+#include "srgb.h"
#include "util.h"
#include "xmalloc.h"
#include "xsnprintf.h"
@@ -19,6 +20,40 @@ static size_t count;
static void sixel_put_generic(struct terminal *term, uint8_t c);
static void sixel_put_ar_11(struct terminal *term, uint8_t c);
+static uint32_t
+color_decode_srgb(const struct terminal *term, uint16_t r, uint16_t g, uint16_t b)
+{
+ if (term->sixel.linear_blending) {
+ if (term->sixel.use_10bit) {
+ r = srgb_decode_8_to_16(r) >> 6;
+ g = srgb_decode_8_to_16(g) >> 6;
+ b = srgb_decode_8_to_16(b) >> 6;
+ } else {
+ r = srgb_decode_8_to_8(r);
+ g = srgb_decode_8_to_8(g);
+ b = srgb_decode_8_to_8(b);
+ }
+ } else {
+ if (term->sixel.use_10bit) {
+ r <<= 2;
+ g <<= 2;
+ b <<= 2;
+ }
+ }
+
+ uint32_t color;
+
+ if (term->sixel.use_10bit) {
+ if (PIXMAN_FORMAT_TYPE(term->sixel.pixman_fmt) == PIXMAN_TYPE_ARGB)
+ color = 0x3u << 30 | r << 20 | g << 10 | b;
+ else
+ color = 0x3u << 30 | b << 20 | g << 10 | r;
+ } else
+ color = 0xffu << 24 | r << 16 | g << 8 | b;
+
+ return color;
+}
+
void
sixel_fini(struct terminal *term)
{
@@ -75,6 +110,23 @@ sixel_init(struct terminal *term, int p1, int p2, int p3)
term->sixel.image.height = 0;
term->sixel.image.alloc_height = 0;
term->sixel.image.bottom_pixel = 0;
+ term->sixel.linear_blending = render_do_linear_blending(term);
+ term->sixel.pixman_fmt = PIXMAN_a8r8g8b8;
+
+ if (term->conf->tweak.surface_bit_depth == SHM_10_BIT) {
+ if (term->wl->shm_have_argb2101010 && term->wl->shm_have_xrgb2101010) {
+ term->sixel.use_10bit = true;
+ term->sixel.pixman_fmt = PIXMAN_a2r10g10b10;
+ }
+
+ else if (term->wl->shm_have_abgr2101010 && term->wl->shm_have_xbgr2101010) {
+ term->sixel.use_10bit = true;
+ term->sixel.pixman_fmt = PIXMAN_a2b10g10r10;
+ }
+ }
+
+ const size_t active_palette_entries = min(
+ ALEN(term->conf->colors.sixel), term->sixel.palette_size);
if (term->sixel.use_private_palette) {
xassert(term->sixel.private_palette == NULL);
@@ -83,11 +135,18 @@ sixel_init(struct terminal *term, int p1, int p2, int p3)
memcpy(
term->sixel.private_palette, term->conf->colors.sixel,
- min(sizeof(term->conf->colors.sixel),
- term->sixel.palette_size * sizeof(term->sixel.private_palette[0])));
+ active_palette_entries * sizeof(term->sixel.private_palette[0]));
- term->sixel.palette = term->sixel.private_palette;
+ if (term->sixel.linear_blending || term->sixel.use_10bit) {
+ for (size_t i = 0; i < active_palette_entries; i++) {
+ uint8_t r = (term->sixel.private_palette[i] >> 16) & 0xff;
+ uint8_t g = (term->sixel.private_palette[i] >> 8) & 0xff;
+ uint8_t b = (term->sixel.private_palette[i] >> 0) & 0xff;
+ term->sixel.private_palette[i] = color_decode_srgb(term, r, g, b);
+ }
+ }
+ term->sixel.palette = term->sixel.private_palette;
} else {
if (term->sixel.shared_palette == NULL) {
term->sixel.shared_palette = xcalloc(
@@ -95,8 +154,16 @@ sixel_init(struct terminal *term, int p1, int p2, int p3)
memcpy(
term->sixel.shared_palette, term->conf->colors.sixel,
- min(sizeof(term->conf->colors.sixel),
- term->sixel.palette_size * sizeof(term->sixel.shared_palette[0])));
+ active_palette_entries * sizeof(term->sixel.shared_palette[0]));
+
+ if (term->sixel.linear_blending || term->sixel.use_10bit) {
+ for (size_t i = 0; i < active_palette_entries; i++) {
+ uint8_t r = (term->sixel.private_palette[i] >> 16) & 0xff;
+ uint8_t g = (term->sixel.private_palette[i] >> 8) & 0xff;
+ uint8_t b = (term->sixel.private_palette[i] >> 0) & 0xff;
+ term->sixel.private_palette[i] = color_decode_srgb(term, r, g, b);
+ }
+ }
} else {
/* Shared palette - do *not* reset palette for new sixels */
}
@@ -488,7 +555,7 @@ blend_new_image_over_old(const struct terminal *term,
int stride = new_width * sizeof(uint32_t);
uint32_t *new_data = xmalloc(stride * new_height);
pixman_image_t *pix2 = pixman_image_create_bits_no_clear(
- PIXMAN_a8r8g8b8, new_width, new_height, new_data, stride);
+ term->sixel.pixman_fmt, new_width, new_height, new_data, stride);
#if defined(_DEBUG)
/* Fill new image with an easy-to-recognize color (green) */
@@ -651,8 +718,7 @@ sixel_overwrite(struct terminal *term, struct sixel *six,
}
pixman_image_t *new_pix = pixman_image_create_bits_no_clear(
- PIXMAN_a8r8g8b8,
- new_width, new_height, new_data, new_width * sizeof(uint32_t));
+ term->sixel.pixman_fmt, new_width, new_height, new_data, new_width * sizeof(uint32_t));
struct sixel new_six = {
.pix = NULL,
@@ -948,7 +1014,7 @@ sixel_sync_cache(const struct terminal *term, struct sixel *six)
uint8_t *scaled_data = xmalloc(scaled_height * scaled_stride);
pixman_image_t *scaled_pix = pixman_image_create_bits_no_clear(
- PIXMAN_a8r8g8b8, scaled_width, scaled_height,
+ term->sixel.pixman_fmt, scaled_width, scaled_height,
(uint32_t *)scaled_data, scaled_stride);
pixman_image_composite32(
@@ -1232,7 +1298,7 @@ sixel_unhook(struct terminal *term)
image.pos.row, image.pos.row + image.rows);
image.original.pix = pixman_image_create_bits_no_clear(
- PIXMAN_a8r8g8b8, image.original.width, image.original.height,
+ term->sixel.pixman_fmt, image.original.width, image.original.height,
img_data, stride);
pixel_row_idx += height;
@@ -2006,15 +2072,14 @@ decgci(struct terminal *term, uint8_t c)
}
case 2: { /* RGB */
- uint8_t r = 255 * min(c1, 100) / 100;
- uint8_t g = 255 * min(c2, 100) / 100;
- uint8_t b = 255 * min(c3, 100) / 100;
+ uint16_t r = 255 * min(c1, 100) / 100;
+ uint16_t g = 255 * min(c2, 100) / 100;
+ uint16_t b = 255 * min(c3, 100) / 100;
- LOG_DBG("setting palette #%d = RGB %hhu/%hhu/%hhu",
+ LOG_DBG("setting palette #%d = RGB %hu/%hu/%hu",
term->sixel.color_idx, r, g, b);
- term->sixel.palette[term->sixel.color_idx] =
- 0xffu << 24 | r << 16 | g << 8 | b;
+ term->sixel.palette[term->sixel.color_idx] = color_decode_srgb(term, r, g, b);
break;
}
}