Rename functions in image-fallback.c to be consistent with other image back-ends.

This commit is contained in:
Michael Grant
2026-10-09 23:53:40 +02:00
parent 729b7e7a13
commit fb70168520
4 changed files with 53 additions and 55 deletions

View File

@@ -67,7 +67,7 @@ static const struct image_rgb image_ansi_colours[16] = {
/* Return the squared RGB distance between two colours. */ /* Return the squared RGB distance between two colours. */
static u_int static u_int
image_glyph_distance(struct image_rgb a, struct image_rgb b) fallback_distance(struct image_rgb a, struct image_rgb b)
{ {
int r = a.r - b.r, g = a.g - b.g, bl = a.b - b.b; int r = a.r - b.r, g = a.g - b.g, bl = a.b - b.b;
@@ -76,7 +76,7 @@ image_glyph_distance(struct image_rgb a, struct image_rgb b)
/* Return whether a colour is close enough to grey to use neutral colours. */ /* Return whether a colour is close enough to grey to use neutral colours. */
static int static int
image_glyph_low_saturation(struct image_rgb colour) fallback_low_saturation(struct image_rgb colour)
{ {
u_int minimum, maximum; u_int minimum, maximum;
@@ -94,17 +94,17 @@ image_glyph_low_saturation(struct image_rgb colour)
/* Find the closest ANSI colour, keeping greys on the neutral ramp. */ /* Find the closest ANSI colour, keeping greys on the neutral ramp. */
static u_int static u_int
image_glyph_nearest_ansi(struct image_rgb colour, u_int colours) fallback_nearest_ansi(struct image_rgb colour, u_int colours)
{ {
u_int i, distance, best_distance = UINT_MAX, best = 0; u_int i, distance, best_distance = UINT_MAX, best = 0;
int neutral = image_glyph_low_saturation(colour); int neutral = fallback_low_saturation(colour);
for (i = 0; i < colours; i++) { for (i = 0; i < colours; i++) {
if (neutral && i != 0 && i != 7 && if (neutral && i != 0 && i != 7 &&
(colours != 16 || (i != 8 && i != 15))) { (colours != 16 || (i != 8 && i != 15))) {
continue; continue;
} }
distance = image_glyph_distance(colour, image_ansi_colours[i]); distance = fallback_distance(colour, image_ansi_colours[i]);
if (distance < best_distance) { if (distance < best_distance) {
best_distance = distance; best_distance = distance;
best = i; best = i;
@@ -115,7 +115,7 @@ image_glyph_nearest_ansi(struct image_rgb colour, u_int colours)
/* Map an RGB colour to the terminal palette and its output colour code. */ /* Map an RGB colour to the terminal palette and its output colour code. */
static struct image_rgb static struct image_rgb
image_glyph_quantize(struct image_rgb colour, enum image_glyph_palette palette, fallback_quantize(struct image_rgb colour, enum image_glyph_palette palette,
int *output) int *output)
{ {
struct image_rgb result; struct image_rgb result;
@@ -135,7 +135,7 @@ image_glyph_quantize(struct image_rgb colour, enum image_glyph_palette palette,
ncolours = 16; ncolours = 16;
if (palette == IMAGE_GLYPH_PALETTE_8) if (palette == IMAGE_GLYPH_PALETTE_8)
ncolours = 8; ncolours = 8;
index = image_glyph_nearest_ansi(colour, ncolours); index = fallback_nearest_ansi(colour, ncolours);
result = image_ansi_colours[index]; result = image_ansi_colours[index];
if (index < 8) if (index < 8)
*output = index; *output = index;
@@ -146,7 +146,7 @@ image_glyph_quantize(struct image_rgb colour, enum image_glyph_palette palette,
/* Fit two colours to a block glyph's pixel samples. */ /* Fit two colours to a block glyph's pixel samples. */
static void static void
image_glyph_fit_colours(const struct image_rgb *samples, u_int count, fallback_fit_colours(const struct image_rgb *samples, u_int count,
struct image_rgb centres[2]) struct image_rgb centres[2])
{ {
u_int sum[2][3], counts[2], group, i, j, iteration, distance; u_int sum[2][3], counts[2], group, i, j, iteration, distance;
@@ -155,7 +155,7 @@ image_glyph_fit_colours(const struct image_rgb *samples, u_int count,
/* Start with the two most widely separated samples. */ /* Start with the two most widely separated samples. */
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
for (j = i + 1; j < count; j++) { for (j = i + 1; j < count; j++) {
distance = image_glyph_distance(samples[i], samples[j]); distance = fallback_distance(samples[i], samples[j]);
if (distance > maximum) { if (distance > maximum) {
maximum = distance; maximum = distance;
first = i; first = i;
@@ -171,8 +171,8 @@ image_glyph_fit_colours(const struct image_rgb *samples, u_int count,
memset(sum, 0, sizeof sum); memset(sum, 0, sizeof sum);
memset(counts, 0, sizeof counts); memset(counts, 0, sizeof counts);
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
group = image_glyph_distance(samples[i], centres[1]) < group = fallback_distance(samples[i], centres[1]) <
image_glyph_distance(samples[i], centres[0]); fallback_distance(samples[i], centres[0]);
sum[group][0] += samples[i].r; sum[group][0] += samples[i].r;
sum[group][1] += samples[i].g; sum[group][1] += samples[i].g;
sum[group][2] += samples[i].b; sum[group][2] += samples[i].b;
@@ -190,7 +190,7 @@ image_glyph_fit_colours(const struct image_rgb *samples, u_int count,
/* Copy the UTF-8 character for an ACS key into a fallback cell. */ /* Copy the UTF-8 character for an ACS key into a fallback cell. */
static int static int
image_glyph_set_acs(struct tty *tty, struct utf8_data *data, u_char key) fallback_set_acs(struct tty *tty, struct utf8_data *data, u_char key)
{ {
struct utf8_data *ud; struct utf8_data *ud;
const char *s = tty_acs_get(tty, key); const char *s = tty_acs_get(tty, key);
@@ -209,7 +209,7 @@ image_glyph_set_acs(struct tty *tty, struct utf8_data *data, u_char key)
/* Map a block's foreground mask to a half, quadrant or sextant glyph. */ /* Map a block's foreground mask to a half, quadrant or sextant glyph. */
static u_char static u_char
image_glyph_block_key(enum image_glyph_detail detail, u_int mask) fallback_block_key(enum image_glyph_detail detail, u_int mask)
{ {
static const u_char half[4] = { static const u_char half[4] = {
0, TTY_ACS_IMAGE_HALF_UPPER, TTY_ACS_IMAGE_HALF_LOWER, 0, TTY_ACS_IMAGE_HALF_UPPER, TTY_ACS_IMAGE_HALF_LOWER,
@@ -250,7 +250,7 @@ image_glyph_block_key(enum image_glyph_detail detail, u_int mask)
/* Build and cache dithered brightness levels for a shading palette. */ /* Build and cache dithered brightness levels for a shading palette. */
static u_char * static u_char *
image_glyph_make_shades(struct image *im, u_int levels) fallback_make_shades(struct image *im, u_int levels)
{ {
struct image_glyph_data *data = im->fallback_data; struct image_glyph_data *data = im->fallback_data;
const struct image_cell *cell; const struct image_cell *cell;
@@ -334,7 +334,7 @@ image_glyph_make_shades(struct image *im, u_int levels)
/* Choose the richest colour palette supported by this terminal. */ /* Choose the richest colour palette supported by this terminal. */
static enum image_glyph_palette static enum image_glyph_palette
image_glyph_get_palette(struct tty *tty) fallback_get_palette(struct tty *tty)
{ {
int colours; int colours;
@@ -352,7 +352,7 @@ image_glyph_get_palette(struct tty *tty)
/* Choose the fallback glyph detail supported by this terminal. */ /* Choose the fallback glyph detail supported by this terminal. */
static enum image_glyph_detail static enum image_glyph_detail
image_glyph_get_detail(struct tty *tty, enum image_glyph_palette palette) fallback_get_detail(struct tty *tty, enum image_glyph_palette palette)
{ {
if (tty_acs_needed(tty)) if (tty_acs_needed(tty))
return (IMAGE_GLYPH_ASCII); return (IMAGE_GLYPH_ASCII);
@@ -369,7 +369,7 @@ image_glyph_get_detail(struct tty *tty, enum image_glyph_palette palette)
/* Render one image cell using a two-colour block glyph. */ /* Render one image cell using a two-colour block glyph. */
static void static void
image_glyph_block(struct tty *tty, struct image *im, u_int x, u_int y, fallback_block(struct tty *tty, struct image *im, u_int x, u_int y,
enum image_glyph_detail detail, enum image_glyph_palette palette, enum image_glyph_detail detail, enum image_glyph_palette palette,
struct grid_cell *out) struct grid_cell *out)
{ {
@@ -408,22 +408,22 @@ image_glyph_block(struct tty *tty, struct image *im, u_int x, u_int y,
/* Fit the two colours and choose which subcells use the foreground. */ /* Fit the two colours and choose which subcells use the foreground. */
n = columns * rows; n = columns * rows;
image_glyph_fit_colours(samples, n, centres); fallback_fit_colours(samples, n, centres);
quantized[0] = image_glyph_quantize(centres[0], palette, &colours[0]); quantized[0] = fallback_quantize(centres[0], palette, &colours[0]);
quantized[1] = image_glyph_quantize(centres[1], palette, &colours[1]); quantized[1] = fallback_quantize(centres[1], palette, &colours[1]);
mask = 0; mask = 0;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
if (image_glyph_distance(samples[i], quantized[1]) < if (fallback_distance(samples[i], quantized[1]) <
image_glyph_distance(samples[i], quantized[0])) { fallback_distance(samples[i], quantized[0])) {
mask |= (1U << i); mask |= (1U << i);
} }
} }
key = image_glyph_block_key(detail, mask); key = fallback_block_key(detail, mask);
/* Use a blank cell if the terminal has no matching glyph. */ /* Use a blank cell if the terminal has no matching glyph. */
if (key == 0) if (key == 0)
utf8_set(&out->data, ' '); utf8_set(&out->data, ' ');
else if (!image_glyph_set_acs(tty, &out->data, key)) else if (!fallback_set_acs(tty, &out->data, key))
utf8_set(&out->data, ' '); utf8_set(&out->data, ' ');
out->fg = colours[1]; out->fg = colours[1];
out->bg = colours[0]; out->bg = colours[0];
@@ -431,7 +431,7 @@ image_glyph_block(struct tty *tty, struct image *im, u_int x, u_int y,
/* Render an image cell using the terminal's text and colour capabilities. */ /* Render an image cell using the terminal's text and colour capabilities. */
void void
image_get_fallback_cell(struct tty *tty, struct image *im, u_int x, u_int y, fallback_get_cell(struct tty *tty, struct image *im, u_int x, u_int y,
const struct grid_cell *gc, struct grid_cell *out) const struct grid_cell *gc, struct grid_cell *out)
{ {
static const char ascii[] = " .:-=+*#%@"; static const char ascii[] = " .:-=+*#%@";
@@ -457,8 +457,8 @@ image_get_fallback_cell(struct tty *tty, struct image *im, u_int x, u_int y,
} }
/* Select the palette and glyph detail for this terminal. */ /* Select the palette and glyph detail for this terminal. */
palette = image_glyph_get_palette(tty); palette = fallback_get_palette(tty);
detail = image_glyph_get_detail(tty, palette); detail = fallback_get_detail(tty, palette);
if (detail == IMAGE_GLYPH_ASCII) { if (detail == IMAGE_GLYPH_ASCII) {
level = cell->whole.brightness * (sizeof ascii - 2) / 255; level = cell->whole.brightness * (sizeof ascii - 2) / 255;
utf8_set(&out->data, ascii[level]); utf8_set(&out->data, ascii[level]);
@@ -468,13 +468,13 @@ image_get_fallback_cell(struct tty *tty, struct image *im, u_int x, u_int y,
nlevels = 8; nlevels = 8;
if (detail == IMAGE_GLYPH_SHADE5) if (detail == IMAGE_GLYPH_SHADE5)
nlevels = 5; nlevels = 5;
levels = image_glyph_make_shades(im, nlevels); levels = fallback_make_shades(im, nlevels);
level = levels[(size_t)y * im->sx + x]; level = levels[(size_t)y * im->sx + x];
key = (detail == IMAGE_GLYPH_SHADE5 ? shades[level] : key = (detail == IMAGE_GLYPH_SHADE5 ? shades[level] :
bold_shades[level]); bold_shades[level]);
if (key == 0) if (key == 0)
utf8_set(&out->data, ' '); utf8_set(&out->data, ' ');
else if (!image_glyph_set_acs(tty, &out->data, key)) else if (!fallback_set_acs(tty, &out->data, key))
utf8_set(&out->data, ' '); utf8_set(&out->data, ' ');
out->fg = 7; out->fg = 7;
out->bg = 0; out->bg = 0;
@@ -485,12 +485,12 @@ image_get_fallback_cell(struct tty *tty, struct image *im, u_int x, u_int y,
} }
return; return;
} }
image_glyph_block(tty, im, x, y, detail, palette, out); fallback_block(tty, im, x, y, detail, palette, out);
} }
/* Free the brightness levels cached for text image rendering. */ /* Free the brightness levels cached for text image rendering. */
void void
image_free_fallback(struct image *im) fallback_free(struct image *im)
{ {
struct image_glyph_data *data = im->fallback_data; struct image_glyph_data *data = im->fallback_data;

View File

@@ -494,6 +494,17 @@ sixel_free(struct sixel_image *si)
free(si); free(si);
} }
/* Return the cell dimensions occupied by a SIXEL image. */
static void
sixel_size_in_cells(struct sixel_image *si, u_int *x, u_int *y)
{
if (si->cell_w == 0)
si->cell_w = 8;
if (si->cell_h == 0)
si->cell_h = 16;
image_size_in_cells(si->sx, si->sy, si->cell_w, si->cell_h, x, y);
}
/* Write a SIXEL image to the debug log. */ /* Write a SIXEL image to the debug log. */
void void
sixel_log(struct sixel_image *si) sixel_log(struct sixel_image *si)
@@ -515,23 +526,12 @@ sixel_log(struct sixel_image *si)
s[x] = '0' + (sl->pixels[x] - 1) % 10; s[x] = '0' + (sl->pixels[x] - 1) % 10;
else else
s[x] = '.'; s[x] = '.';
} }
s[x] = '\0'; s[x] = '\0';
log_debug("%s: %4u: %s", __func__, y, s); log_debug("%s: %4u: %s", __func__, y, s);
} }
} }
/* Return the cell dimensions occupied by a SIXEL image. */
void
sixel_size_in_cells(struct sixel_image *si, u_int *x, u_int *y)
{
if (si->cell_w == 0)
si->cell_w = 8;
if (si->cell_h == 0)
si->cell_h = 16;
image_size_in_cells(si->sx, si->sy, si->cell_w, si->cell_h, x, y);
}
/* Convert one HLS component to RGB. */ /* Convert one HLS component to RGB. */
static double static double
sixel_hue(double p, double q, double t) sixel_hue(double p, double q, double t)
@@ -631,7 +631,7 @@ sixel_to_image(struct sixel_image *si)
} }
/* Scale or crop an indexed SIXEL image. */ /* Scale or crop an indexed SIXEL image. */
struct sixel_image * static struct sixel_image *
sixel_scale(struct sixel_image *si, u_int cell_w, u_int cell_h, u_int ox, sixel_scale(struct sixel_image *si, u_int cell_w, u_int cell_h, u_int ox,
u_int oy, u_int sx, u_int sy, int colours) u_int oy, u_int sx, u_int sy, int colours)
{ {
@@ -913,7 +913,7 @@ sixel_print_compress_colors(struct sixel_image *si, struct sixel_plane *planes,
} }
/* Encode an indexed SIXEL image for terminal output. */ /* Encode an indexed SIXEL image for terminal output. */
char * static char *
sixel_print(struct sixel_image *si, struct sixel_image *map, size_t *size) sixel_print(struct sixel_image *si, struct sixel_image *map, size_t *size)
{ {
char *buf, tmp[64]; char *buf, tmp[64];

View File

@@ -1132,7 +1132,7 @@ image_free(u_int id)
image_free(im->parent_id); image_free(im->parent_id);
if (im->sixel != NULL) if (im->sixel != NULL)
sixel_free(im->sixel); sixel_free(im->sixel);
image_free_fallback(im); fallback_free(im);
free(im->cells); free(im->cells);
free(im); free(im);
} }
@@ -1183,7 +1183,7 @@ image_get_fallback_at(struct tty *tty, struct screen *s, u_int x, u_int y,
return (-1); return (-1);
} }
} }
image_get_fallback_cell(tty, placement->image, fallback_get_cell(tty, placement->image,
found->source_x + x - found->x, found->source_y, gc, out); found->source_x + x - found->x, found->source_y, gc, out);
return (1); return (1);
} }

14
tmux.h
View File

@@ -4399,10 +4399,7 @@ void image_tty_geometry_changed(struct tty *);
void image_tty_free(struct tty *, int); void image_tty_free(struct tty *, int);
void image_draw_line(struct tty *, struct screen *, u_int, u_int, void image_draw_line(struct tty *, struct screen *, u_int, u_int,
u_int, u_int, u_int, int); u_int, u_int, u_int, int);
void image_get_fallback_cell(struct tty *, struct image *, u_int,
u_int, const struct grid_cell *, struct grid_cell *);
const struct image_cell *image_get_cell(struct image *, u_int, u_int); const struct image_cell *image_get_cell(struct image *, u_int, u_int);
void image_free_fallback(struct image *);
int image_get_fallback_at(struct tty *, struct screen *, u_int, int image_get_fallback_at(struct tty *, struct screen *, u_int,
u_int, const struct grid_cell *, struct grid_cell *); u_int, const struct grid_cell *, struct grid_cell *);
struct image *image_rect_get_image(const struct image_rect *); struct image *image_rect_get_image(const struct image_rect *);
@@ -4431,6 +4428,12 @@ int image_grid_get_placeholder(struct grid *, u_int, u_int,
struct kitty_placeholder *); struct kitty_placeholder *);
void image_place_cell_kitty(struct screen_write_ctx *, struct image *, void image_place_cell_kitty(struct screen_write_ctx *, struct image *,
u_int, u_int, u_int, u_int, u_int, u_int, int32_t); u_int, u_int, u_int, u_int, u_int, u_int, int32_t);
/* image-fallback.c */
void fallback_get_cell(struct tty *, struct image *, u_int,
u_int, const struct grid_cell *, struct grid_cell *);
void fallback_free(struct image *);
/* image-kitty.c */ /* image-kitty.c */
struct image *kitty_parse_image(void **, const u_char *, size_t, u_int, struct image *kitty_parse_image(void **, const u_char *, size_t, u_int,
u_int, struct kitty_parse_result *); u_int, struct kitty_parse_result *);
@@ -4458,11 +4461,6 @@ struct sixel_image *sixel_parse(const char *, size_t, u_int, u_int, u_int,
u_int); u_int);
void sixel_free(struct sixel_image *); void sixel_free(struct sixel_image *);
void sixel_log(struct sixel_image *); void sixel_log(struct sixel_image *);
void sixel_size_in_cells(struct sixel_image *, u_int *, u_int *);
struct sixel_image *sixel_scale(struct sixel_image *, u_int, u_int, u_int,
u_int, u_int, u_int, int);
char *sixel_print(struct sixel_image *, struct sixel_image *,
size_t *);
struct image *sixel_to_image(struct sixel_image *); struct image *sixel_to_image(struct sixel_image *);
#endif #endif