Inline basic image fallback renderer

This commit is contained in:
Michael Grant
2026-08-06 10:25:29 +01:00
parent 674cdd94f5
commit 7d2d187db4
6 changed files with 25 additions and 47 deletions

View File

@@ -249,7 +249,7 @@ endif
# Enable image support.
if ENABLE_IMAGES
dist_tmux_SOURCES += image.c image-sixel.c image-kitty.c image-ascii.c
dist_tmux_SOURCES += image.c image-sixel.c image-kitty.c
endif
# Add bits for fuzzing if enabled.

View File

@@ -1,40 +0,0 @@
/* $OpenBSD$ */
/*
* Copyright (c) 2026 Michael Grant <mgrant@grant.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <string.h>
#include "tmux.h"
void
image_get_text_cell(__unused struct tty *tty, struct image *im, u_int x,
u_int y, const struct grid_cell *gc, struct grid_cell *out,
__unused const struct tty_style_ctx *style_ctx)
{
static const char ramp[] = " .:-=+*#%@";
const struct image_cell *cell;
u_int level = 0;
memcpy(out, gc, sizeof *out);
cell = image_get_cell(im, x, y);
if (cell != NULL)
level = cell->whole.brightness * (sizeof ramp - 2) / 255;
utf8_set(&out->data, ramp[level]);
out->flags &= ~GRID_FLAG_IMAGE;
}

24
image.c
View File

@@ -42,8 +42,8 @@ struct image_backend {
void (*geometry_changed)(struct tty *);
};
static const struct image_backend image_backend_ascii = {
"ascii", IMAGE_BACKEND_SCROLLS, NULL, NULL, NULL
static const struct image_backend image_backend_fallback = {
"fallback", IMAGE_BACKEND_SCROLLS, NULL, NULL, NULL
};
static const struct image_backend image_backend_kitty = {
"kitty", IMAGE_BACKEND_GRAPHICAL|IMAGE_BACKEND_SCROLLS,
@@ -62,7 +62,7 @@ image_tty_find_backend(struct tty *tty)
if (tty->term != NULL && tty->term->flags & TERM_SIXEL &&
tty->xpixel != 0 && tty->ypixel != 0)
return (&image_backend_sixel);
return (&image_backend_ascii);
return (&image_backend_fallback);
}
void
@@ -328,6 +328,24 @@ image_get_cell(struct image *im, u_int x, u_int y)
return (&im->cells[(size_t)y * im->sx + x]);
}
/* Character-cell fallback for clients without a graphical image protocol. */
void
image_get_fallback_cell(__unused struct tty *tty, struct image *im, u_int x,
u_int y, const struct grid_cell *gc, struct grid_cell *out,
__unused const struct tty_style_ctx *style_ctx)
{
static const char ramp[] = " .:-=+*#%@";
const struct image_cell *cell;
u_int level = 0;
memcpy(out, gc, sizeof *out);
cell = image_get_cell(im, x, y);
if (cell != NULL)
level = cell->whole.brightness * (sizeof ramp - 2) / 255;
utf8_set(&out->data, ramp[level]);
out->flags &= ~GRID_FLAG_IMAGE;
}
void
image_set_cell(struct grid_cell *gc, struct image *im, u_int x, u_int y)
{

2
tmux.h
View File

@@ -4318,7 +4318,7 @@ void image_tty_free(struct tty *, int);
void image_draw_line(struct tty *, struct screen *, u_int, u_int,
u_int, u_int, u_int, const struct tty_style_ctx *);
const struct image_cell *image_get_cell(struct image *, u_int, u_int);
void image_get_text_cell(struct tty *, struct image *, u_int,
void image_get_fallback_cell(struct tty *, struct image *, u_int,
u_int, const struct grid_cell *, struct grid_cell *,
const struct tty_style_ctx *);
void image_clear(struct screen_write_ctx *, u_int);

View File

@@ -273,7 +273,7 @@ tty_draw_line(struct tty *tty, struct screen *s, u_int px, u_int py, u_int nx,
~GRID_FLAG_IMAGE;
skip = 1;
} else {
image_get_text_cell(tty, im,
image_get_fallback_cell(tty, im,
gc.image_x, gc.image_y, &gc,
&image_gc, style_ctx);
}

2
tty.c
View File

@@ -2161,7 +2161,7 @@ tty_cell(struct tty *tty, const struct grid_cell *gc,
if (image_tty_is_graphical(tty))
return;
im = image_find(gc->image_id);
image_get_text_cell(tty, im, gc->image_x, gc->image_y, gc,
image_get_fallback_cell(tty, im, gc->image_x, gc->image_y, gc,
&image_gc, style_ctx);
gc = &image_gc;
}