From d83ffb2f6361199faa63ecffed7fd989f7dc6d6e Mon Sep 17 00:00:00 2001 From: Michael Grant Date: Thu, 6 Aug 2026 12:27:48 +0100 Subject: [PATCH] Make image types opaque --- image-ascii.c | 5 +- image-sixel.c | 119 +++++++++++++++++++++++++---------------- image.c | 141 +++++++++++++++++++++++++++++++++++++++++++++++-- screen-write.c | 2 +- tmux.h | 70 ++++++------------------ 5 files changed, 229 insertions(+), 108 deletions(-) diff --git a/image-ascii.c b/image-ascii.c index d1598a26f..ea5ad4fec 100644 --- a/image-ascii.c +++ b/image-ascii.c @@ -28,13 +28,10 @@ image_get_text_cell(__unused struct tty *tty, struct image *im, u_int x, __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; + level = image_get_brightness(im, x, y) * (sizeof ramp - 2) / 255; utf8_set(&out->data, ramp[level]); out->flags &= ~GRID_FLAG_IMAGE; } diff --git a/image-sixel.c b/image-sixel.c index dc1c4e93a..5d872e1e8 100644 --- a/image-sixel.c +++ b/image-sixel.c @@ -88,6 +88,40 @@ struct sixel_output { uint64_t age; }; +struct sixel_histogram { + u_int count; + uint64_t red; + uint64_t green; + uint64_t blue; +}; + +struct sixel_box { + u_int red_min; + u_int red_max; + u_int green_min; + u_int green_max; + u_int blue_min; + u_int blue_max; + u_int count; +}; + +struct sixel_rgb { + u_char red; + u_char green; + u_char blue; +}; + +struct sixel_source { + const u_char *pixels; + size_t stride; + u_int width; + u_int height; + u_int canvas_width; + u_int canvas_height; + u_int sx; + u_int sy; +}; + static int sixel_parse_expand_lines(struct sixel_image *si, u_int y) { @@ -516,7 +550,7 @@ sixel_to_image(struct sixel_image *si) if (im == NULL) free(pixels); else - im->sixel = si; + image_set_sixel(im, si); return (im); } #endif @@ -763,26 +797,6 @@ sixel_print(struct sixel_image *si, struct sixel_image *map, size_t *size) return (buf); } -struct sixel_histogram { - u_int count; - uint64_t red; - uint64_t green; - uint64_t blue; -}; - -struct sixel_box { - u_int red_min, red_max; - u_int green_min, green_max; - u_int blue_min, blue_max; - u_int count; -}; - -struct sixel_rgb { - u_char red; - u_char green; - u_char blue; -}; - /* Split a 5-bit RGB histogram into an adaptive palette using median cut. */ static void sixel_box_update(struct sixel_box *box, struct sixel_histogram *histogram) @@ -992,7 +1006,8 @@ sixel_clamp_colour(int colour) } static const u_char * -sixel_from_image_pixel(struct image *im, u_int sourcex0, u_int sourcey0, +sixel_from_image_pixel(const struct sixel_source *source, u_int sourcex0, + u_int sourcey0, u_int sourcewidth, u_int sourceheight, u_int sx, u_int sy, u_int x, u_int y) { @@ -1000,11 +1015,11 @@ sixel_from_image_pixel(struct image *im, u_int sourcex0, u_int sourcey0, sourcex = sourcex0 + (uint64_t)x * sourcewidth / sx; sourcey = sourcey0 + (uint64_t)y * sourceheight / sy; - if (sourcex >= im->width) - sourcex = im->width - 1; - if (sourcey >= im->height) - sourcey = im->height - 1; - return (im->pixels + sourcey * im->stride + sourcex * 4); + if (sourcex >= source->width) + sourcex = source->width - 1; + if (sourcey >= source->height) + sourcey = source->height - 1; + return (source->pixels + sourcey * source->stride + sourcex * 4); } static struct sixel_image * @@ -1012,6 +1027,7 @@ sixel_from_image(struct image *im, u_int ox, u_int oy, u_int cells_x, u_int cells_y, u_int xpixel, u_int ypixel) { struct sixel_image *si; + struct sixel_source source; struct sixel_histogram *histogram, *entry; struct sixel_rgb palette[SIXEL_PALETTE_SIZE]; const u_char *pixel; @@ -1024,14 +1040,19 @@ sixel_from_image(struct image *im, u_int ox, u_int oy, u_int cells_x, uint64_t destination_width, destination_height; uint64_t content_width, content_height, x0, x1, y0, y1; - destination_width = (uint64_t)im->sx * xpixel; - destination_height = (uint64_t)im->sy * ypixel; + source.pixels = image_get_pixels(im, &source.stride, NULL); + image_get_dimensions(im, &source.width, &source.height); + image_get_canvas_dimensions(im, &source.canvas_width, + &source.canvas_height); + image_get_cell_dimensions(im, &source.sx, &source.sy); + destination_width = (uint64_t)source.sx * xpixel; + destination_height = (uint64_t)source.sy * ypixel; if (destination_width > UINT_MAX || destination_height > UINT_MAX) return (NULL); - content_width = ((uint64_t)im->width * destination_width + - im->canvas_width - 1) / im->canvas_width; - content_height = ((uint64_t)im->height * destination_height + - im->canvas_height - 1) / im->canvas_height; + content_width = ((uint64_t)source.width * destination_width + + source.canvas_width - 1) / source.canvas_width; + content_height = ((uint64_t)source.height * destination_height + + source.canvas_height - 1) / source.canvas_height; x0 = (uint64_t)ox * xpixel; y0 = (uint64_t)oy * ypixel; x1 = ((uint64_t)ox + cells_x) * xpixel; @@ -1055,7 +1076,7 @@ sixel_from_image(struct image *im, u_int ox, u_int oy, u_int cells_x, histogram = xcalloc(SIXEL_HISTOGRAM_SIZE, sizeof *histogram); for (y = 0; y < sy; y++) { for (x = 0; x < sx; x++) { - pixel = sixel_from_image_pixel(im, sourcex0, sourcey0, + pixel = sixel_from_image_pixel(&source, sourcex0, sourcey0, sourcewidth, sourceheight, sx, sy, x, y); if (pixel[3] < 128) continue; @@ -1095,7 +1116,7 @@ sixel_from_image(struct image *im, u_int ox, u_int oy, u_int cells_x, 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, + pixel = sixel_from_image_pixel(&source, sourcex0, sourcey0, sourcewidth, sourceheight, sx, sy, x, y); if (pixel[3] < 128) continue; @@ -1223,11 +1244,15 @@ sixel_geometry_changed(struct tty *tty) static struct sixel_image * sixel_render_image(struct image *im, u_int xpixel, u_int ypixel) { + struct sixel_image *original; + u_int sx, sy; + + image_get_cell_dimensions(im, &sx, &sy); /* Preserve SIXEL's original palette and indexed pixels when possible. */ - if (im->sixel != NULL) - return (sixel_scale(im->sixel, xpixel, ypixel, 0, 0, - im->sx, im->sy, 1)); - return (sixel_from_image(im, 0, 0, im->sx, im->sy, xpixel, ypixel)); + original = image_get_sixel(im); + if (original != NULL) + return (sixel_scale(original, xpixel, ypixel, 0, 0, sx, sy, 1)); + return (sixel_from_image(im, 0, 0, sx, sy, xpixel, ypixel)); } static struct sixel_image * @@ -1240,7 +1265,8 @@ sixel_get_image(struct tty *tty, struct image *im) sixel_collect_images(so); for (cache = so->images; cache != NULL; cache = cache->next) { - if (cache->server_id != im->id || cache->xpixel != tty->xpixel || + if (cache->server_id != image_get_id(im) || + cache->xpixel != tty->xpixel || cache->ypixel != tty->ypixel) continue; cache->age = ++so->age; @@ -1267,7 +1293,7 @@ sixel_get_image(struct tty *tty, struct image *im) sixel_remove_cache(so, oldest); } cache = xcalloc(1, sizeof *cache); - cache->server_id = im->id; + cache->server_id = image_get_id(im); cache->xpixel = tty->xpixel; cache->ypixel = tty->ypixel; cache->size = size; @@ -1301,13 +1327,16 @@ sixel_draw_rectangle(struct tty *tty, const struct image_rectangle *rectangle, struct sixel_image *si, *crop; char *data; size_t size; + u_int source_x, source_y, width, height; + u_int destination_x, destination_y; - si = sixel_get_image(tty, rectangle->image); + si = sixel_get_image(tty, image_rectangle_get_image(rectangle)); if (si == NULL) return; + image_rectangle_get_coordinates(rectangle, &source_x, &source_y, &width, + &height, &destination_x, &destination_y); crop = sixel_scale(si, tty->xpixel, tty->ypixel, - rectangle->source_x, rectangle->source_y, rectangle->width, - rectangle->height, 1); + source_x, source_y, width, height, 1); if (!sixel_image_is_cached(tty, si)) sixel_free(si); if (crop == NULL) @@ -1318,7 +1347,7 @@ sixel_draw_rectangle(struct tty *tty, const struct image_rectangle *rectangle, return; tty_region_off(tty); tty_margin_off(tty); - tty_cursor(tty, rectangle->destination_x, rectangle->destination_y); + tty_cursor(tty, destination_x, destination_y); tty->flags |= TTY_NOBLOCK; tty_putn(tty, data, size, 0); tty_invalidate(tty); diff --git a/image.c b/image.c index 375dd370b..d21d7504d 100644 --- a/image.c +++ b/image.c @@ -25,6 +25,56 @@ #include "tmux.h" +/* A protocol-neutral average of part of an image cell. RGB is premultiplied. */ +struct image_sample { + u_char red; + u_char green; + u_char blue; + u_char alpha; + u_char brightness; +}; + +/* Half blocks, quadrants and sextants all divide evenly into a 2 by 6 grid. */ +#define IMAGE_SAMPLE_COLUMNS 2 +#define IMAGE_SAMPLE_ROWS 6 +struct image_cell { + struct image_sample whole; + struct image_sample samples[IMAGE_SAMPLE_ROWS][IMAGE_SAMPLE_COLUMNS]; +}; + +/* Immutable protocol-neutral image placement. */ +struct image { + u_int id; + u_int references; + u_int width; + u_int height; + u_int canvas_width; + u_int canvas_height; + u_int sx; + u_int sy; + size_t stride; + size_t size; + u_char *pixels; + /* Original indexed SIXEL data, if this image arrived as SIXEL. */ + struct sixel_image *sixel; + struct image_cell *cells; /* lazily generated text samples */ + + RB_ENTRY(image) entry; +}; +RB_HEAD(images, image); + +/* A cell-aligned part of an image to draw at a terminal position. */ +struct image_rectangle { + struct image *image; + struct grid_cell cell; + u_int source_x; + u_int source_y; + u_int width; + u_int height; + u_int destination_x; + u_int destination_y; +}; + static struct images images = RB_INITIALIZER(&images); static u_int image_next_id; @@ -202,6 +252,81 @@ image_find(u_int id) return (RB_FIND(images, &images, &find)); } +u_int +image_get_id(const struct image *im) +{ + return (im->id); +} + +void +image_get_dimensions(const struct image *im, u_int *width, u_int *height) +{ + if (width != NULL) + *width = im->width; + if (height != NULL) + *height = im->height; +} + +void +image_get_canvas_dimensions(const struct image *im, u_int *width, + u_int *height) +{ + if (width != NULL) + *width = im->canvas_width; + if (height != NULL) + *height = im->canvas_height; +} + +void +image_get_cell_dimensions(const struct image *im, u_int *sx, u_int *sy) +{ + if (sx != NULL) + *sx = im->sx; + if (sy != NULL) + *sy = im->sy; +} + +const u_char * +image_get_pixels(const struct image *im, size_t *stride, size_t *size) +{ + if (stride != NULL) + *stride = im->stride; + if (size != NULL) + *size = im->size; + return (im->pixels); +} + +struct sixel_image * +image_get_sixel(const struct image *im) +{ + return (im->sixel); +} + +void +image_set_sixel(struct image *im, struct sixel_image *si) +{ + im->sixel = si; +} + +struct image * +image_rectangle_get_image(const struct image_rectangle *rectangle) +{ + return (rectangle->image); +} + +void +image_rectangle_get_coordinates(const struct image_rectangle *rectangle, + u_int *source_x, u_int *source_y, u_int *width, u_int *height, + u_int *destination_x, u_int *destination_y) +{ + *source_x = rectangle->source_x; + *source_y = rectangle->source_y; + *width = rectangle->width; + *height = rectangle->height; + *destination_x = rectangle->destination_x; + *destination_y = rectangle->destination_y; +} + struct image * image_create(u_int width, u_int height, u_int canvas_width, u_int canvas_height, u_int sx, u_int sy, u_char *pixels) @@ -272,7 +397,7 @@ image_free(u_int id) free(im); } -const struct image_cell * +static const struct image_cell * image_get_cell(struct image *im, u_int x, u_int y) { if (im == NULL || x >= im->sx || y >= im->sy) @@ -282,6 +407,17 @@ image_get_cell(struct image *im, u_int x, u_int y) return (&im->cells[(size_t)y * im->sx + x]); } +u_char +image_get_brightness(struct image *im, u_int x, u_int y) +{ + const struct image_cell *cell; + + cell = image_get_cell(im, x, y); + if (cell == NULL) + return (0); + return (cell->whole.brightness); +} + void image_set_cell(struct grid_cell *gc, struct image *im, u_int x, u_int y) { @@ -451,10 +587,9 @@ image_write(struct screen_write_ctx *ctx, struct image *im, u_int bg) u_int cx = s->cx, cy = s->cy; u_int x, y, sx, sy, lines; - sx = im->sx; + image_get_cell_dimensions(im, &sx, &sy); if (sx > screen_size_x(s) - cx) sx = screen_size_x(s) - cx; - sy = im->sy; if (sy > screen_size_y(s) - 1) sy = screen_size_y(s) - 1; if (sx == 0 || sy == 0) diff --git a/screen-write.c b/screen-write.c index c11bf94ad..17d3a4752 100644 --- a/screen-write.c +++ b/screen-write.c @@ -3034,7 +3034,7 @@ screen_write_sixelimage(struct screen_write_ctx *ctx, struct sixel_image *si, return; } image_write(ctx, im, bg); - image_free(im->id); + image_free(image_get_id(im)); } #endif diff --git a/tmux.h b/tmux.h index e995b267b..7f90ac158 100644 --- a/tmux.h +++ b/tmux.h @@ -1038,56 +1038,6 @@ struct style { }; #ifdef ENABLE_IMAGES -/* A protocol-neutral average of part of an image cell. RGB is premultiplied. */ -struct image_sample { - u_char red; - u_char green; - u_char blue; - u_char alpha; - u_char brightness; -}; - -/* Half blocks, quadrants and sextants all divide evenly into a 2 by 6 grid. */ -#define IMAGE_SAMPLE_COLUMNS 2 -#define IMAGE_SAMPLE_ROWS 6 -struct image_cell { - struct image_sample whole; - struct image_sample samples[IMAGE_SAMPLE_ROWS][IMAGE_SAMPLE_COLUMNS]; -}; - -/* Immutable protocol-neutral image placement. */ -struct image { - u_int id; - u_int references; - u_int width; - u_int height; - u_int canvas_width; - u_int canvas_height; - u_int sx; - u_int sy; - size_t stride; - size_t size; - u_char *pixels; - /* Original indexed SIXEL data, if this image arrived as SIXEL. */ - struct sixel_image *sixel; - struct image_cell *cells; /* lazily generated text samples */ - - RB_ENTRY(image) entry; -}; -RB_HEAD(images, image); - -/* A cell-aligned part of an image to draw at a terminal position. */ -struct image_rectangle { - struct image *image; - struct grid_cell cell; - u_int source_x; - u_int source_y; - u_int width; - u_int height; - u_int destination_x; - u_int destination_y; -}; - #define IMAGE_SIZE_LIMIT (64 * 1024 * 1024) #endif @@ -4261,6 +4211,13 @@ char *regsub(const char *, const char *, const char *, int); struct image *image_create(u_int, u_int, u_int, u_int, u_int, u_int, u_char *); struct image *image_find(u_int); +u_int image_get_id(const struct image *); +void image_get_dimensions(const struct image *, u_int *, u_int *); +void image_get_canvas_dimensions(const struct image *, u_int *, u_int *); +void image_get_cell_dimensions(const struct image *, u_int *, u_int *); +const u_char *image_get_pixels(const struct image *, size_t *, size_t *); +struct sixel_image *image_get_sixel(const struct image *); +void image_set_sixel(struct image *, struct sixel_image *); void image_ref(u_int); void image_free(u_int); void image_set_cell(struct grid_cell *, struct image *, u_int, @@ -4281,19 +4238,22 @@ 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, const struct tty_style_ctx *); -const struct image_cell *image_get_cell(struct image *, u_int, u_int); +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 *); -void sixel_draw_rectangle(struct tty *, - const struct image_rectangle *, const struct tty_style_ctx *); -void sixel_free_output(struct tty *, int); -void sixel_geometry_changed(struct tty *); +struct image *image_rectangle_get_image(const struct image_rectangle *); +void image_rectangle_get_coordinates(const struct image_rectangle *, + u_int *, u_int *, u_int *, u_int *, u_int *, u_int *); #endif #ifdef ENABLE_SIXEL /* image-sixel.c */ #define SIXEL_COLOUR_REGISTERS 1024 +void sixel_draw_rectangle(struct tty *, + const struct image_rectangle *, const struct tty_style_ctx *); +void sixel_free_output(struct tty *, int); +void sixel_geometry_changed(struct tty *); struct sixel_image *sixel_parse(const char *, size_t, u_int, u_int, u_int); void sixel_free(struct sixel_image *); void sixel_log(struct sixel_image *);