From 8b44703b41d88ea626b91c3c42e065ae207be08f Mon Sep 17 00:00:00 2001 From: Michael Grant Date: Wed, 5 Aug 2026 21:44:25 +0100 Subject: [PATCH] Fix SIXEL median-cut palette generation --- image-sixel.c | 3 +++ regress/image-support.sh | 22 ++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/image-sixel.c b/image-sixel.c index 01dc57fb0..dc1c4e93a 100644 --- a/image-sixel.c +++ b/image-sixel.c @@ -877,6 +877,9 @@ sixel_box_split(struct sixel_box *box, struct sixel_box *new, if (count >= box->count / 2) break; } + /* Keep the maximum occupied level in the new box. */ + if (level == last) + level--; memcpy(new, box, sizeof *new); if (channel == 0) { box->red_max = level; diff --git a/regress/image-support.sh b/regress/image-support.sh index 8265d7933..8f3c6ca46 100755 --- a/regress/image-support.sh +++ b/regress/image-support.sh @@ -125,7 +125,7 @@ $TMUX2 select-window -t:1 || exit 1 sleep 1 [ "$($TMUX2 display-message -p '#{cursor_x},#{cursor_y}')" = "2,1" ] || exit 1 $TMUX capture-pane -pS0 -E3 >$TMP || exit 1 -[ "$(sed -n 3p $TMP)" = " :" ] || exit 1 +[ "$(sed -n 3p $TMP)" = " -" ] || exit 1 [ "$(sed -n 4p $TMP)" = " *" ] || exit 1 # With normal cursor movement, scrolling is calculated from the full image @@ -139,7 +139,7 @@ $TMUX2 select-window -t:2 || exit 1 sleep 1 [ "$($TMUX2 display-message -p '#{cursor_y}')" = 3 ] || exit 1 $TMUX capture-pane -pS0 -E3 >$TMP || exit 1 -[ "$(sed -n 1p $TMP)" = ":" ] || exit 1 +[ "$(sed -n 1p $TMP)" = "-" ] || exit 1 [ "$(sed -n 2p $TMP)" = "*" ] || exit 1 [ "$(sed -n 3p $TMP)" = "@" ] || exit 1 @@ -181,4 +181,22 @@ $TMUX capture-pane -pS0 -E3 >$TMP || exit 1 [ "$(sed -n 1p $TMP | wc -c)" = 61 ] || exit 1 [ -z "$(sed -n 2p $TMP)" ] || exit 1 +# A weighted median at the maximum channel level must still leave colours on +# both sides of the split. This skewed black, grey and white image used to stop +# palette generation after one colour instead of producing three. +$TMUX kill-server 2>/dev/null +$TMUX2 kill-server 2>/dev/null +$TMUX2 new-session -d -x 20 -y 4 " + printf '\033_Ga=T,q=2,C=1,f=24,s=100,v=1,c=10,r=1;AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\033\\' + sleep 10" || exit 1 +$TMUX2 set -g status off || exit 1 +$TMUX2 set -as terminal-features ',*:sixel' || exit 1 +$TMUX new-session -d -x 20 -y 4 || exit 1 +$TMUX set -g status off || exit 1 +$TMUX pipe-pane -O "cat >$TMP" || exit 1 +$TMUX send-keys -l "$TMUX2 attach-session" || exit 1 +$TMUX send-keys Enter || exit 1 +sleep 1 +grep -a '#2;2;' $TMP >/dev/null || exit 1 + exit 0