image: move basic fallback into image.c

This commit is contained in:
Michael Grant
2026-08-18 23:59:32 +01:00
parent 9c70c3351b
commit f53976a266
6 changed files with 20 additions and 49 deletions

View File

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

View File

@@ -1,38 +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"
/* Fill a grid cell with an ASCII brightness glyph. */
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[] = " .:-=+*#%@";
u_int level = 0;
memcpy(out, gc, sizeof *out);
level = image_get_brightness(im, x, y) * (sizeof ramp - 2) / 255;
utf8_set(&out->data, ramp[level]);
out->flags &= ~GRID_FLAG_IMAGE;
}

19
image.c
View File

@@ -469,10 +469,24 @@ image_get_brightness(struct image *im, u_int x, u_int y)
return (cell->whole.brightness);
}
/* Fill a grid cell with an ASCII brightness glyph. */
static void
image_get_text_cell(struct image *im, u_int x, u_int y,
const struct grid_cell *gc, struct grid_cell *out)
{
static const char ramp[] = " .:-=+*#%@";
u_int level;
memcpy(out, gc, sizeof *out);
level = image_get_brightness(im, x, y) * (sizeof ramp - 2) / 255;
utf8_set(&out->data, ramp[level]);
out->flags &= ~GRID_FLAG_IMAGE;
}
/* Get the terminal cell used to draw an image marker. */
int
image_get_draw_cell(struct tty *tty, const struct grid_cell *gc,
struct grid_cell *out, const struct tty_style_ctx *style_ctx)
struct grid_cell *out)
{
struct image *im = image_find(gc->image_id);
@@ -487,8 +501,7 @@ image_get_draw_cell(struct tty *tty, const struct grid_cell *gc,
out->flags &= ~(GRID_FLAG_IMAGE|GRID_FLAG_IMAGE_DAMAGED);
return (0);
}
image_get_text_cell(tty, im, gc->image_x, gc->image_y, gc, out,
style_ctx);
image_get_text_cell(im, gc->image_x, gc->image_y, gc, out);
return (0);
}

5
tmux.h
View File

@@ -4240,16 +4240,13 @@ void image_redraw_all(struct screen_write_ctx *);
void image_redraw_scroll(struct screen_write_ctx *, u_int);
int image_backend_flags(struct tty *);
int image_get_draw_cell(struct tty *, const struct grid_cell *,
struct grid_cell *, const struct tty_style_ctx *);
struct grid_cell *);
void image_tty_update(struct tty *);
void image_tty_geometry_changed(struct tty *);
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, int, const struct tty_style_ctx *);
u_char image_get_brightness(struct image *, u_int, u_int);
void image_get_text_cell(struct tty *, struct image *, u_int,
u_int, const struct grid_cell *, struct grid_cell *,
const struct tty_style_ctx *);
struct image *image_rect_get_image(const struct image_rect *);
void image_rect_get_coords(const struct image_rect *,
u_int *, u_int *, u_int *, u_int *, u_int *, u_int *);

View File

@@ -258,8 +258,7 @@ tty_draw_line(struct tty *tty, struct screen *s, u_int px, u_int py, u_int nx,
#ifdef ENABLE_IMAGES
if (gc.flags & GRID_FLAG_IMAGE) {
(void)image_get_draw_cell(tty, &gc, &image_gc,
style_ctx);
(void)image_get_draw_cell(tty, &gc, &image_gc);
gcp = &image_gc;
} else
gcp = &gc;

2
tty.c
View File

@@ -2149,7 +2149,7 @@ tty_cell(struct tty *tty, const struct grid_cell *gc,
#ifdef ENABLE_IMAGES
if (gc->flags & GRID_FLAG_IMAGE) {
if (image_get_draw_cell(tty, gc, &image_gc, style_ctx))
if (image_get_draw_cell(tty, gc, &image_gc))
return;
gc = &image_gc;
}