From c0ab9b7b3fe778ccfbeb69001afe25797b9d3f96 Mon Sep 17 00:00:00 2001 From: Michael Grant Date: Wed, 5 Aug 2026 21:38:28 +0100 Subject: [PATCH] =?UTF-8?q?Revert=20"Removed=20unconditional=20Floyd?= =?UTF-8?q?=E2=80=93Steinberg=20dithering=20from=20image-sixel.c:1077.=20K?= =?UTF-8?q?itty=20images=20now=20use=20the=20adaptive=20256-colour=20media?= =?UTF-8?q?n-cut=20palette=20with=20direct=20nearest-colour=20mapping."?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit ab591ae4302f52f6a9775dce4965419f16dd7ea6. --- image-sixel.c | 49 ++++++++++++++++++++++++++++++++++++++-- regress/image-support.sh | 4 ++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/image-sixel.c b/image-sixel.c index e81ff5554..01dc57fb0 100644 --- a/image-sixel.c +++ b/image-sixel.c @@ -978,6 +978,16 @@ sixel_nearest_colour(struct sixel_rgb *palette, u_int ncolours, return (best); } +static u_int +sixel_clamp_colour(int colour) +{ + if (colour < 0) + return (0); + if (colour > 255) + return (255); + return (colour); +} + static const u_char * sixel_from_image_pixel(struct image *im, u_int sourcex0, u_int sourcey0, u_int sourcewidth, u_int sourceheight, u_int sx, u_int sy, u_int x, @@ -1003,7 +1013,9 @@ sixel_from_image(struct image *im, u_int ox, u_int oy, u_int cells_x, struct sixel_rgb palette[SIXEL_PALETTE_SIZE]; const u_char *pixel; uint16_t *cache; - u_int x, y, sx, sy, index; + int *current, *next, *tmp; + int red_error, green_error, blue_error; + u_int x, y, sx, sy, index, error_index; u_int sourcex0, sourcey0, sourcewidth, sourceheight; u_int red, green, blue, colour, i, ncolours; uint64_t destination_width, destination_height; @@ -1076,22 +1088,55 @@ sixel_from_image(struct image *im, u_int ox, u_int oy, u_int cells_x, cache = xmalloc(SIXEL_HISTOGRAM_SIZE * sizeof *cache); memset(cache, 0xff, SIXEL_HISTOGRAM_SIZE * sizeof *cache); + current = xcalloc(((size_t)sx + 2) * 3, sizeof *current); + next = xcalloc(((size_t)sx + 2) * 3, sizeof *next); for (y = 0; y < sy; y++) { for (x = 0; x < sx; x++) { pixel = sixel_from_image_pixel(im, sourcex0, sourcey0, sourcewidth, sourceheight, sx, sy, x, y); if (pixel[3] < 128) continue; + error_index = (x + 1) * 3; + red = sixel_clamp_colour((int)pixel[0] + + current[error_index] / 16); + green = sixel_clamp_colour((int)pixel[1] + + current[error_index + 1] / 16); + blue = sixel_clamp_colour((int)pixel[2] + + current[error_index + 2] / 16); colour = sixel_nearest_colour(palette, ncolours, cache, - pixel[0], pixel[1], pixel[2]); + red, green, blue); if (sixel_set_pixel(si, x, y, colour + 1) != 0) goto fail; + + red_error = (int)red - palette[colour].red; + green_error = (int)green - palette[colour].green; + blue_error = (int)blue - palette[colour].blue; + current[error_index + 3] += red_error * 7; + current[error_index + 4] += green_error * 7; + current[error_index + 5] += blue_error * 7; + next[error_index - 3] += red_error * 3; + next[error_index - 2] += green_error * 3; + next[error_index - 1] += blue_error * 3; + next[error_index] += red_error * 5; + next[error_index + 1] += green_error * 5; + next[error_index + 2] += blue_error * 5; + next[error_index + 3] += red_error; + next[error_index + 4] += green_error; + next[error_index + 5] += blue_error; } + tmp = current; + current = next; + next = tmp; + memset(next, 0, ((size_t)sx + 2) * 3 * sizeof *next); } + free(current); + free(next); free(cache); return (si); fail: + free(current); + free(next); free(cache); sixel_free(si); return (NULL); diff --git a/regress/image-support.sh b/regress/image-support.sh index 39e1ad3d7..8265d7933 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