From 9e3bfb1eab2c7b1c7a1ec6021331fddc57e8a266 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 21 Feb 2020 21:53:23 +0100 Subject: sixel: initial support This implements basic parsing of sixel data. Lots of limitations and temporary solutions as this is still work-in-progress: * Maximum image size hardcoded to 800x800 * No HLS color format support * Image is always rendered at 0x0 in the terminal --- dcs.c | 23 ++++- meson.build | 1 + render.c | 21 +++++ sixel.c | 284 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ sixel.h | 7 ++ terminal.h | 17 ++++ 6 files changed, 350 insertions(+), 3 deletions(-) create mode 100644 sixel.c create mode 100644 sixel.h diff --git a/dcs.c b/dcs.c index 048cd62e..f9e3c10c 100644 --- a/dcs.c +++ b/dcs.c @@ -3,6 +3,7 @@ #define LOG_MODULE "dcs" #define LOG_ENABLE_DBG 0 #include "log.h" +#include "sixel.h" #include "vt.h" static void @@ -35,9 +36,20 @@ dcs_hook(struct terminal *term, uint8_t final) assert(term->vt.dcs.data == NULL); assert(term->vt.dcs.size == 0); + assert(term->vt.dcs.put_handler == NULL); assert(term->vt.dcs.unhook_handler == NULL); switch (term->vt.private[0]) { + case 0: + switch (final) { + case 'q': + sixel_init(term); + term->vt.dcs.put_handler = &sixel_put; + term->vt.dcs.unhook_handler = &sixel_unhook; + break; + } + break; + case '=': switch (final) { case 's': @@ -75,9 +87,13 @@ void dcs_put(struct terminal *term, uint8_t c) { LOG_DBG("PUT: %c", c); - if (!ensure_size(term, term->vt.dcs.idx + 1)) - return; - term->vt.dcs.data[term->vt.dcs.idx++] = c; + if (term->vt.dcs.put_handler != NULL) + term->vt.dcs.put_handler(term, c); + else { + if (!ensure_size(term, term->vt.dcs.idx + 1)) + return; + term->vt.dcs.data[term->vt.dcs.idx++] = c; + } } void @@ -87,6 +103,7 @@ dcs_unhook(struct terminal *term) term->vt.dcs.unhook_handler(term); term->vt.dcs.unhook_handler = NULL; + term->vt.dcs.put_handler = NULL; free(term->vt.dcs.data); term->vt.dcs.data = NULL; diff --git a/meson.build b/meson.build index 2da5c65f..16483f51 100644 --- a/meson.build +++ b/meson.build @@ -117,6 +117,7 @@ executable( 'selection.c', 'selection.h', 'server.c', 'server.h', 'shm.c', 'shm.h', + 'sixel.c', 'sixel.h', 'slave.c', 'slave.h', 'terminal.c', 'terminal.h', 'tokenize.c', 'tokenize.h', diff --git a/render.c b/render.c index bb229288..a88aba17 100644 --- a/render.c +++ b/render.c @@ -816,6 +816,27 @@ grid_render(struct terminal *term) cols_updated * term->cell_width, term->cell_height); } + if (term->sixel.pix != NULL) { + pixman_image_composite( + PIXMAN_OP_SRC, + term->sixel.pix, + NULL, + pix, + 0, 0, + 0, 0, + 0, 0, + term->sixel.max_col, + term->sixel.row * 6); + wl_surface_damage_buffer( + term->window->surface, + 0, 0, term->sixel.max_col, term->sixel.row * 6); + + pixman_image_unref(term->sixel.pix); + free(term->sixel.image); + term->sixel.pix = NULL; + term->sixel.image = NULL; + } + if (term->flash.active) { /* Note: alpha is pre-computed in each color component */ /* TODO: dim while searching */ diff --git a/sixel.c b/sixel.c new file mode 100644 index 00000000..2ca783e5 --- /dev/null +++ b/sixel.c @@ -0,0 +1,284 @@ +#include "sixel.h" + +#include + +#define LOG_MODULE "sixel" +#define LOG_ENABLE_DBG 0 +#include "log.h" +#include "render.h" + +#define max(x, y) ((x) > (y) ? (x) : (y)) + +static const size_t COLOR_COUNT = 1024; +static const size_t IMAGE_WIDTH = 800; +static const size_t IMAGE_HEIGHT = 800; + +static size_t count; + +void +sixel_init(struct terminal *term) +{ + if (term->sixel.pix != NULL) { + pixman_image_unref(term->sixel.pix); + free(term->sixel.image); + term->sixel.pix = NULL; + term->sixel.image = NULL; + } + + assert(term->sixel.palette == NULL); + assert(term->sixel.image == NULL); + + if (term->sixel.image != NULL) { + if (term->sixel.pix != NULL) { + pixman_image_unref(term->sixel.pix); + term->sixel.pix = NULL; + } + free(term->sixel.image); + } + + term->sixel.state = SIXEL_SIXEL; + term->sixel.row = 0; + term->sixel.col = 0; + term->sixel.color_idx = 0; + term->sixel.max_col = 0; + term->sixel.param = 0; + term->sixel.param_idx = 0; + memset(term->sixel.params, 0, sizeof(term->sixel.params)); + term->sixel.palette = calloc(COLOR_COUNT, sizeof(term->sixel.palette[0])); + term->sixel.image = calloc(IMAGE_WIDTH * IMAGE_HEIGHT, sizeof(term->sixel.image[0])); + + count = 0; + + /* TODO: default palette */ +} + +void +sixel_unhook(struct terminal *term) +{ + free(term->sixel.palette); + term->sixel.palette = NULL; + + if (term->sixel.pix != NULL) + pixman_image_unref(term->sixel.pix); + + LOG_DBG("generating %dx%d pixman image", term->sixel.row * 6, term->sixel.max_col); + + if (term->sixel.col > 0) { + if (term->sixel.col > term->sixel.max_col) + term->sixel.max_col = term->sixel.col; + term->sixel.row++; + term->sixel.col = 0; + } + + term->sixel.pix = pixman_image_create_bits_no_clear( + PIXMAN_a8r8g8b8, + term->sixel.max_col, + term->sixel.row * 6, + term->sixel.image, + IMAGE_WIDTH * sizeof(uint32_t)); + + size_t lines = max(1, term->sixel.row * 6 / term->cell_height); + for (size_t i = 0; i < lines; i++) + term_linefeed(term); + render_refresh(term); +} + +static void +sixel_add(struct terminal *term, uint32_t color, uint8_t sixel) +{ + LOG_DBG("adding sixel %02hhx using color 0x%06x", sixel, color); + if (term->sixel.col >= IMAGE_WIDTH) { + LOG_WARN("column outside image width"); + return; + } + if (term->sixel.row >= IMAGE_HEIGHT) { + LOG_WARN("row outside image height"); + return; + } + + for (int i = 0; i < 6; i++) { + int bit = sixel & 1; + sixel >>= 1; + if (bit) { + size_t idx = (term->sixel.row * 6 + i) * IMAGE_WIDTH + term->sixel.col; + term->sixel.image[idx] = 0x00 << 24 | color; + } + } + + assert(sixel == 0); + + term->sixel.col++; +} + +static void +sixel_sixel(struct terminal *term, uint8_t c) +{ + switch (c) { + case '"': + term->sixel.state = SIXEL_RASTER; + term->sixel.param = 0; + term->sixel.param_idx = 0; + break; + + case '#': + term->sixel.state = SIXEL_COLOR; + term->sixel.color_idx = 0; + term->sixel.param = 0; + term->sixel.param_idx = 0; + break; + + case '$': + if (term->sixel.col > term->sixel.max_col) + term->sixel.max_col = term->sixel.col; + term->sixel.col = 0; + break; + + case '-': + if (term->sixel.col > term->sixel.max_col) + term->sixel.max_col = term->sixel.col; + term->sixel.row++; + term->sixel.col = 0; + break; + + case '!': + term->sixel.state = SIXEL_REPEAT; + term->sixel.param = 0; + term->sixel.param_idx = 0; + break; + + default: + if (c < '?' || c > '~') { + LOG_ERR("invalid sixel charactwer: '%c' at idx=%zu", c, count); + return; + } + + sixel_add(term, term->sixel.palette[term->sixel.color_idx], c - 63); + break; + } +} + +static void +sixel_repeat(struct terminal *term, uint8_t c) +{ + if (c >= '0' && c <= '9') { + term->sixel.param *= 10; + term->sixel.param += c - '0'; + } else { + LOG_DBG("repeating '%c' %u times", c, term->sixel.param); + term->sixel.state = SIXEL_SIXEL; + for (unsigned i = 0; i < term->sixel.param; i++) + sixel_sixel(term, c); + } +} + +static void +sixel_raster(struct terminal *term, uint8_t c) +{ + if (c >= '0' && c <= '9') { + term->sixel.param *= 10; + term->sixel.param += c - '0'; + } else { + if (term->sixel.param_idx < sizeof(term->sixel.params) / sizeof(term->sixel.params[0])) + term->sixel.params[term->sixel.param_idx++] = term->sixel.param; + + term->sixel.param = 0; + + if (c != ';') { + unsigned pan __attribute__((unused)) = term->sixel.params[0]; + unsigned pad __attribute__((unused)) = term->sixel.params[1]; + unsigned ph __attribute__((unused)) = term->sixel.params[2]; + unsigned pv __attribute__((unused)) = term->sixel.params[3]; + + LOG_DBG("pan=%u, pad=%u (aspect ratio = %u), size=%ux%u", + pan, pad, pan / pad, ph, pv); + } + + switch (c) { + case '#': term->sixel.state = SIXEL_COLOR; break; + case ';': term->sixel.state = SIXEL_RASTER; break; + default: term->sixel.state = SIXEL_SIXEL; sixel_sixel(term, c); break; + } + } +} + +static void +sixel_color(struct terminal *term, uint8_t c) +{ + if (c >= '0' && c <= '9') { + term->sixel.param *= 10; + term->sixel.param += c - '0'; + } else { + if (term->sixel.param < COLOR_COUNT) + term->sixel.color_idx = term->sixel.param; + else + term->sixel.color_idx = 0; + + term->sixel.param_idx = 0; + term->sixel.param = 0; + + switch (c) { + case '#': term->sixel.state = SIXEL_COLOR; break; + case ';': term->sixel.state = SIXEL_COLOR_SPEC; break; + default: term->sixel.state = SIXEL_SIXEL; sixel_sixel(term, c); break; + } + } +} + +static void +sixel_color_spec(struct terminal *term, uint8_t c) +{ + if (c >= '0' && c <= '9') { + term->sixel.param *= 10; + term->sixel.param += c - '0'; + } else { + if (term->sixel.param_idx < sizeof(term->sixel.params) / sizeof(term->sixel.params[0])) + term->sixel.params[term->sixel.param_idx++] = term->sixel.param; + + term->sixel.param = 0; + + if (c != ';') { + unsigned format = term->sixel.params[0]; + unsigned c1 = term->sixel.params[1]; + unsigned c2 = term->sixel.params[2]; + unsigned c3 = term->sixel.params[3]; + + if (format == 1) { + assert(false && "HLS color format not implemented"); + } else { + uint8_t r = 255 * c1 / 100; + uint8_t g = 255 * c2 / 100; + uint8_t b = 255 * c3 / 100; + + LOG_DBG("setting palette #%d = RGB %hhu/%hhu/%hhu", + term->sixel.color_idx, r, g, b); + + term->sixel.palette[term->sixel.color_idx] = r << 16 | g << 8 | b; + } + } + + switch (c) { + case '#': term->sixel.state = SIXEL_COLOR; break; + case ';': term->sixel.state = SIXEL_COLOR_SPEC; break; + default: term->sixel.state = SIXEL_SIXEL; sixel_sixel(term, c); break; + } + } +} + +void +sixel_put(struct terminal *term, uint8_t c) +{ + count++; + switch (c) { + case ' ': return; + case '\n': return; + case '\r': return; + } + + switch (term->sixel.state) { + case SIXEL_SIXEL: sixel_sixel(term, c); break; + case SIXEL_REPEAT: sixel_repeat(term, c); break; + case SIXEL_RASTER: sixel_raster(term, c); break; + case SIXEL_COLOR: sixel_color(term, c); break; + case SIXEL_COLOR_SPEC: sixel_color_spec(term, c); break; + } +} diff --git a/sixel.h b/sixel.h new file mode 100644 index 00000000..0cb24bfe --- /dev/null +++ b/sixel.h @@ -0,0 +1,7 @@ +#pragma once + +#include "terminal.h" + +void sixel_init(struct terminal *term); +void sixel_put(struct terminal *term, uint8_t c); +void sixel_unhook(struct terminal *term); diff --git a/terminal.h b/terminal.h index d9727b32..756b0554 100644 --- a/terminal.h +++ b/terminal.h @@ -131,6 +131,7 @@ struct vt { uint8_t *data; size_t size; size_t idx; + void (*put_handler)(struct terminal *term, uint8_t c); void (*unhook_handler)(struct terminal *term); } dcs; struct attributes attrs; @@ -344,6 +345,22 @@ struct terminal { int upper_fd; } delayed_render_timer; + struct { + enum { SIXEL_SIXEL, SIXEL_REPEAT, SIXEL_RASTER, SIXEL_COLOR, SIXEL_COLOR_SPEC} state; + int row; + int col; + int color_idx; + int max_col; + unsigned params[4]; + uint32_t *palette; + uint32_t *image; + + unsigned int param; + unsigned param_idx; + + pixman_image_t *pix; + } sixel; + bool hold_at_exit; bool is_shutting_down; void (*shutdown_cb)(void *data, int exit_code); -- cgit v1.3 From a1b5862db21ab0ae52ba2e8314be46432eac5225 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 21 Feb 2020 23:35:43 +0100 Subject: scroll-up: ensure view is valid after adjusting an overshot scrollback When we scroll up, we need to ensure that we don't scroll too far, "past" the scrollback limit. I.e. we need to ensure we don't wrap around. The code did this. But, in certain scenarios, the resulting view points into uninitialized scrollback history. This happens when we haven't yet filled the entire scrollback, and scroll up enough lines to wrap around the scrollback. The old code would adjust the view for the wrap around, but doing so pointed the view at the not-yet utilized scrollback. --- commands.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/commands.c b/commands.c index 572dae68..53d0dfb3 100644 --- a/commands.c +++ b/commands.c @@ -55,6 +55,9 @@ cmd_scrollback_up(struct terminal *term, int rows) new_view = end + 1; } + while (term->grid->rows[new_view] == NULL) + new_view = (new_view + 1) % term->grid->num_rows; + #if defined(_DEBUG) for (int r = 0; r < term->rows; r++) assert(term->grid->rows[(new_view + r) % term->grid->num_rows] != NULL); -- cgit v1.3 From f0fc82f0987166b3fe4724e50da05ba611231c67 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 21 Feb 2020 23:40:35 +0100 Subject: sixel: wip: maintain a list of finished, and "active" sixel images In unhook, add the generated image to a list of finished sixel images, along with positioning and size information. When rendering, loop this list of images, and render the images (or parts of) that are visible. When scrolling, check if any part of the images cover the re-cycled lines, and if so, remove the *entire* image from the list. This means we have the following limitations: * The renderer always renders the whole (visible area of) the image(s). There are times when this isn't necessary - for example, when the image is scrolled inside the visible area. * It would be nice if we could crop the image when parts of it is scrolled out. --- render.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++------------ sixel.c | 44 +++++++++++++++++++----------------------- terminal.c | 39 +++++++++++++++++++++++++++++++++++++ terminal.h | 12 ++++++++++-- 4 files changed, 122 insertions(+), 38 deletions(-) diff --git a/render.c b/render.c index a88aba17..9cad3012 100644 --- a/render.c +++ b/render.c @@ -816,25 +816,66 @@ grid_render(struct terminal *term) cols_updated * term->cell_width, term->cell_height); } - if (term->sixel.pix != NULL) { + tll_foreach(term->sixel_images, it) { + int first_visible_row = -1; + + if (view_end >= term->grid->view) { + /* Not wrapped */ + for (size_t i = it->item.pos.row; + i < it->item.pos.row + (it->item.height + term->cell_height - 1) / term->cell_height; + i++) + { + int row = i & (term->grid->num_rows - 1); + if (row >= term->grid->view && row <= view_end) { + first_visible_row = i; + break; + } + } + } else { + /* Wrapped */ + for (size_t i = it->item.pos.row; + i < it->item.pos.row + (it->item.height + term->cell_height - 1) / term->cell_height; + i++) + { + int row = i & (term->grid->num_rows - 1); + if (row >= term->grid->view || row <= view_end) { + first_visible_row = i; + break; + } + } + } + + if (first_visible_row < 0) + continue; + + const int first_img_row = first_visible_row - it->item.pos.row; + const int row = first_visible_row & (term->grid->num_rows - 1); + const int view_aligned = + (row - term->grid->view + term->grid->num_rows) & (term->grid->num_rows - 1); + + const int x = term->x_margin + it->item.pos.col * term->cell_width; + const int y = max(term->y_margin, term->y_margin + view_aligned * term->cell_height); + + const int width = min(it->item.width, term->width - x - term->x_margin); + const int height = min( + it->item.height - first_img_row * term->cell_height, + term->height - y - term->y_margin); + + //LOG_INFO("x=%d, y=%d, width=%d, height=%d", x, y, width, height); + pixman_image_composite( PIXMAN_OP_SRC, - term->sixel.pix, + it->item.pix, NULL, pix, + 0, first_img_row * term->cell_height, 0, 0, - 0, 0, - 0, 0, - term->sixel.max_col, - term->sixel.row * 6); + x, y, + width, height); + wl_surface_damage_buffer( term->window->surface, - 0, 0, term->sixel.max_col, term->sixel.row * 6); - - pixman_image_unref(term->sixel.pix); - free(term->sixel.image); - term->sixel.pix = NULL; - term->sixel.image = NULL; + x, y, width, height); } if (term->flash.active) { diff --git a/sixel.c b/sixel.c index 2ca783e5..f9c4c4cf 100644 --- a/sixel.c +++ b/sixel.c @@ -18,24 +18,9 @@ static size_t count; void sixel_init(struct terminal *term) { - if (term->sixel.pix != NULL) { - pixman_image_unref(term->sixel.pix); - free(term->sixel.image); - term->sixel.pix = NULL; - term->sixel.image = NULL; - } - assert(term->sixel.palette == NULL); assert(term->sixel.image == NULL); - if (term->sixel.image != NULL) { - if (term->sixel.pix != NULL) { - pixman_image_unref(term->sixel.pix); - term->sixel.pix = NULL; - } - free(term->sixel.image); - } - term->sixel.state = SIXEL_SIXEL; term->sixel.row = 0; term->sixel.col = 0; @@ -58,28 +43,39 @@ sixel_unhook(struct terminal *term) free(term->sixel.palette); term->sixel.palette = NULL; - if (term->sixel.pix != NULL) - pixman_image_unref(term->sixel.pix); - LOG_DBG("generating %dx%d pixman image", term->sixel.row * 6, term->sixel.max_col); - if (term->sixel.col > 0) { + if (term->sixel.col >= 0) { if (term->sixel.col > term->sixel.max_col) term->sixel.max_col = term->sixel.col; term->sixel.row++; term->sixel.col = 0; } - term->sixel.pix = pixman_image_create_bits_no_clear( + struct sixel image = { + .data = term->sixel.image, + .width = term->sixel.max_col, + .height = term->sixel.row * 6, + .pos = (struct coord){term->cursor.point.col, term->grid->offset + term->cursor.point.row}, + }; + + image.pix = pixman_image_create_bits_no_clear( PIXMAN_a8r8g8b8, - term->sixel.max_col, - term->sixel.row * 6, + image.width, image.height, term->sixel.image, IMAGE_WIDTH * sizeof(uint32_t)); - size_t lines = max(1, term->sixel.row * 6 / term->cell_height); + tll_push_back(term->sixel_images, image); + + term->sixel.image = NULL; + term->sixel.max_col = 0; + term->sixel.col = 0; + term->sixel.row = 0; + + const size_t lines = (image.height + term->cell_height - 1) / term->cell_height; for (size_t i = 0; i < lines; i++) term_linefeed(term); + term_formfeed(term); render_refresh(term); } @@ -101,7 +97,7 @@ sixel_add(struct terminal *term, uint32_t color, uint8_t sixel) sixel >>= 1; if (bit) { size_t idx = (term->sixel.row * 6 + i) * IMAGE_WIDTH + term->sixel.col; - term->sixel.image[idx] = 0x00 << 24 | color; + term->sixel.image[idx] = term->colors.alpha / 256 << 24 | color; } } diff --git a/terminal.c b/terminal.c index ba19b552..b80b9898 100644 --- a/terminal.c +++ b/terminal.c @@ -740,6 +740,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, .lower_fd = delay_lower_fd, .upper_fd = delay_upper_fd, }, + .sixel_images = tll_init(), .hold_at_exit = conf->hold_at_exit, .shutdown_cb = shutdown_cb, .shutdown_data = shutdown_data, @@ -993,6 +994,12 @@ term_destroy(struct terminal *term) tll_free(term->ptmx_buffer); tll_free(term->tab_stops); + tll_foreach(term->sixel_images, it) { + pixman_image_unref(it->item.pix); + free(it->item.data); + } + tll_free(term->sixel_images); + free(term->foot_exe); free(term->cwd); @@ -1521,6 +1528,21 @@ term_scroll_partial(struct terminal *term, struct scroll_region region, int rows erase_line(term, grid_row_and_alloc(term->grid, r)); if (selection_on_row_in_view(term, r)) selection_cancel(term); + + + tll_foreach(term->sixel_images, it) { + /* Make it simple - remove the entire image if it starts + * getting scrolled out */ + + int img_top_row = it->item.pos.row & (term->grid->num_rows - 1); + int new_row = (term->grid->offset + r) & (term->grid->num_rows - 1); + + if (img_top_row == new_row) { + pixman_image_unref(it->item.pix); + free(it->item.data); + tll_remove(term->sixel_images, it); + } + } } term_damage_scroll(term, DAMAGE_SCROLL, region, rows); @@ -1572,6 +1594,23 @@ term_scroll_reverse_partial(struct terminal *term, erase_line(term, grid_row_and_alloc(term->grid, r)); if (selection_on_row_in_view(term, r)) selection_cancel(term); + + tll_foreach(term->sixel_images, it) { + /* Make it simple - remove the entire image if it starts + * getting scrolled out */ + + /* TODO: untested */ + + int img_rows = (it->item.height + term->cell_height - 1) / term->cell_height; + int img_bottom_row = (it->item.pos.row + img_rows) & (term->grid->num_rows - 1); + int new_row = (term->grid->offset + r) & (term->grid->num_rows - 1); + + if (img_bottom_row == new_row) { + pixman_image_unref(it->item.pix); + free(it->item.data); + tll_remove(term->sixel_images, it); + } + } } term_damage_scroll(term, DAMAGE_SCROLL_REVERSE, region, rows); diff --git a/terminal.h b/terminal.h index 756b0554..69b94624 100644 --- a/terminal.h +++ b/terminal.h @@ -175,6 +175,14 @@ struct ptmx_buffer { size_t idx; }; +struct sixel { + void *data; + pixman_image_t *pix; + int width; + int height; + struct coord pos; +}; + struct terminal { struct fdm *fdm; const struct config *conf; @@ -357,10 +365,10 @@ struct terminal { unsigned int param; unsigned param_idx; - - pixman_image_t *pix; } sixel; + tll(struct sixel) sixel_images; + bool hold_at_exit; bool is_shutting_down; void (*shutdown_cb)(void *data, int exit_code); -- cgit v1.3 From 564e8909f4c90320657a14812d9202dfeff53fdb Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 21 Feb 2020 23:48:45 +0100 Subject: render: break out sixel rendering code --- render.c | 134 ++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 73 insertions(+), 61 deletions(-) diff --git a/render.c b/render.c index 9cad3012..cd96ba24 100644 --- a/render.c +++ b/render.c @@ -519,6 +519,78 @@ grid_render_scroll_reverse(struct terminal *term, struct buffer *buf, } } +static void +render_sixel(struct terminal *term, pixman_image_t *pix, const struct sixel *sixel) +{ + int view_end = (term->grid->view + term->rows - 1) & (term->grid->num_rows - 1); + int first_visible_row = -1; + + if (view_end >= term->grid->view) { + /* Not wrapped */ + for (size_t i = sixel->pos.row; + i < sixel->pos.row + (sixel->height + term->cell_height - 1) / term->cell_height; + i++) + { + int row = i & (term->grid->num_rows - 1); + if (row >= term->grid->view && row <= view_end) { + first_visible_row = i; + break; + } + } + } else { + /* Wrapped */ + for (size_t i = sixel->pos.row; + i < sixel->pos.row + (sixel->height + term->cell_height - 1) / term->cell_height; + i++) + { + int row = i & (term->grid->num_rows - 1); + if (row >= term->grid->view || row <= view_end) { + first_visible_row = i; + break; + } + } + } + + if (first_visible_row < 0) + return; + + const int first_img_row = first_visible_row - sixel->pos.row; + const int row = first_visible_row & (term->grid->num_rows - 1); + const int view_aligned = + (row - term->grid->view + term->grid->num_rows) & (term->grid->num_rows - 1); + + const int x = term->x_margin + sixel->pos.col * term->cell_width; + const int y = max(term->y_margin, term->y_margin + view_aligned * term->cell_height); + + const int width = min(sixel->width, term->width - x - term->x_margin); + const int height = min( + sixel->height - first_img_row * term->cell_height, + term->height - y - term->y_margin); + + //LOG_INFO("x=%d, y=%d, width=%d, height=%d", x, y, width, height); + + pixman_image_composite( + PIXMAN_OP_SRC, + sixel->pix, + NULL, + pix, + 0, first_img_row * term->cell_height, + 0, 0, + x, y, + width, height); + + wl_surface_damage_buffer( + term->window->surface, + x, y, width, height); +} + +static void +render_sixel_images(struct terminal *term, pixman_image_t *pix) +{ + tll_foreach(term->sixel_images, it) + render_sixel(term, pix, &it->item); +} + static void render_row(struct terminal *term, pixman_image_t *pix, struct row *row, int row_no) { @@ -816,67 +888,7 @@ grid_render(struct terminal *term) cols_updated * term->cell_width, term->cell_height); } - tll_foreach(term->sixel_images, it) { - int first_visible_row = -1; - - if (view_end >= term->grid->view) { - /* Not wrapped */ - for (size_t i = it->item.pos.row; - i < it->item.pos.row + (it->item.height + term->cell_height - 1) / term->cell_height; - i++) - { - int row = i & (term->grid->num_rows - 1); - if (row >= term->grid->view && row <= view_end) { - first_visible_row = i; - break; - } - } - } else { - /* Wrapped */ - for (size_t i = it->item.pos.row; - i < it->item.pos.row + (it->item.height + term->cell_height - 1) / term->cell_height; - i++) - { - int row = i & (term->grid->num_rows - 1); - if (row >= term->grid->view || row <= view_end) { - first_visible_row = i; - break; - } - } - } - - if (first_visible_row < 0) - continue; - - const int first_img_row = first_visible_row - it->item.pos.row; - const int row = first_visible_row & (term->grid->num_rows - 1); - const int view_aligned = - (row - term->grid->view + term->grid->num_rows) & (term->grid->num_rows - 1); - - const int x = term->x_margin + it->item.pos.col * term->cell_width; - const int y = max(term->y_margin, term->y_margin + view_aligned * term->cell_height); - - const int width = min(it->item.width, term->width - x - term->x_margin); - const int height = min( - it->item.height - first_img_row * term->cell_height, - term->height - y - term->y_margin); - - //LOG_INFO("x=%d, y=%d, width=%d, height=%d", x, y, width, height); - - pixman_image_composite( - PIXMAN_OP_SRC, - it->item.pix, - NULL, - pix, - 0, first_img_row * term->cell_height, - 0, 0, - x, y, - width, height); - - wl_surface_damage_buffer( - term->window->surface, - x, y, width, height); - } + render_sixel_images(term, pix); if (term->flash.active) { /* Note: alpha is pre-computed in each color component */ -- cgit v1.3 From 63140a68f516c8b4616bf6c726d23a75b73e5713 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 00:05:25 +0100 Subject: sixel: calculate image height in (cell) rows --- render.c | 43 ++++++++++++++++++++++--------------------- sixel.c | 1 + terminal.c | 3 +-- terminal.h | 1 + 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/render.c b/render.c index cd96ba24..a4741b90 100644 --- a/render.c +++ b/render.c @@ -520,30 +520,23 @@ grid_render_scroll_reverse(struct terminal *term, struct buffer *buf, } static void -render_sixel(struct terminal *term, pixman_image_t *pix, const struct sixel *sixel) +render_sixel(struct terminal *term, pixman_image_t *pix, + const struct sixel *sixel) { int view_end = (term->grid->view + term->rows - 1) & (term->grid->num_rows - 1); int first_visible_row = -1; - if (view_end >= term->grid->view) { - /* Not wrapped */ - for (size_t i = sixel->pos.row; - i < sixel->pos.row + (sixel->height + term->cell_height - 1) / term->cell_height; - i++) - { - int row = i & (term->grid->num_rows - 1); + for (size_t i = sixel->pos.row; i < sixel->pos.row + sixel->rows; i++) { + int row = i & (term->grid->num_rows - 1); + + if (view_end >= term->grid->view) { + /* Not wrapped */ if (row >= term->grid->view && row <= view_end) { first_visible_row = i; break; } - } - } else { - /* Wrapped */ - for (size_t i = sixel->pos.row; - i < sixel->pos.row + (sixel->height + term->cell_height - 1) / term->cell_height; - i++) - { - int row = i & (term->grid->num_rows - 1); + } else { + /* Wrapped */ if (row >= term->grid->view || row <= view_end) { first_visible_row = i; break; @@ -554,20 +547,30 @@ render_sixel(struct terminal *term, pixman_image_t *pix, const struct sixel *six if (first_visible_row < 0) return; + /* First visible (0 based) row of the image */ const int first_img_row = first_visible_row - sixel->pos.row; + + /* Map first visible line to current grid view */ const int row = first_visible_row & (term->grid->num_rows - 1); const int view_aligned = (row - term->grid->view + term->grid->num_rows) & (term->grid->num_rows - 1); + /* Translate row/column to x/y pixel values */ const int x = term->x_margin + sixel->pos.col * term->cell_width; - const int y = max(term->y_margin, term->y_margin + view_aligned * term->cell_height); + const int y = max( + term->y_margin, term->y_margin + view_aligned * term->cell_height); + /* Width/height, in pixels - and don't touch the window margins */ const int width = min(sixel->width, term->width - x - term->x_margin); const int height = min( sixel->height - first_img_row * term->cell_height, term->height - y - term->y_margin); - //LOG_INFO("x=%d, y=%d, width=%d, height=%d", x, y, width, height); + /* Verify we're not stepping outside the grid */ + assert(x >= term->x_margin); + assert(y >= term->y_margin); + assert(x + width <= term->width - term->x_margin); + assert(y + height <= term->height - term->y_margin); pixman_image_composite( PIXMAN_OP_SRC, @@ -579,9 +582,7 @@ render_sixel(struct terminal *term, pixman_image_t *pix, const struct sixel *six x, y, width, height); - wl_surface_damage_buffer( - term->window->surface, - x, y, width, height); + wl_surface_damage_buffer(term->window->surface, x, y, width, height); } static void diff --git a/sixel.c b/sixel.c index f9c4c4cf..0cfb2a24 100644 --- a/sixel.c +++ b/sixel.c @@ -56,6 +56,7 @@ sixel_unhook(struct terminal *term) .data = term->sixel.image, .width = term->sixel.max_col, .height = term->sixel.row * 6, + .rows = (term->sixel.row * 6 + term->cell_height - 1) / term->cell_height, .pos = (struct coord){term->cursor.point.col, term->grid->offset + term->cursor.point.row}, }; diff --git a/terminal.c b/terminal.c index b80b9898..c6c349c3 100644 --- a/terminal.c +++ b/terminal.c @@ -1601,8 +1601,7 @@ term_scroll_reverse_partial(struct terminal *term, /* TODO: untested */ - int img_rows = (it->item.height + term->cell_height - 1) / term->cell_height; - int img_bottom_row = (it->item.pos.row + img_rows) & (term->grid->num_rows - 1); + int img_bottom_row = (it->item.pos.row + it->item.rows) & (term->grid->num_rows - 1); int new_row = (term->grid->offset + r) & (term->grid->num_rows - 1); if (img_bottom_row == new_row) { diff --git a/terminal.h b/terminal.h index 69b94624..bf113aa3 100644 --- a/terminal.h +++ b/terminal.h @@ -180,6 +180,7 @@ struct sixel { pixman_image_t *pix; int width; int height; + int rows; struct coord pos; }; -- cgit v1.3 From da76b8cbf85cdfd3caaf95e469db86a7bc1a2b64 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 00:10:42 +0100 Subject: sixel: ugly hack to remove over-written sixel images --- sixel.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sixel.c b/sixel.c index 0cfb2a24..240754f7 100644 --- a/sixel.c +++ b/sixel.c @@ -66,6 +66,14 @@ sixel_unhook(struct terminal *term) term->sixel.image, IMAGE_WIDTH * sizeof(uint32_t)); + tll_foreach(term->sixel_images, it) { + if (it->item.pos.row == image.pos.row) { + pixman_image_unref(it->item.pix); + free(it->item.data); + tll_remove(term->sixel_images, it); + } + } + tll_push_back(term->sixel_images, image); term->sixel.image = NULL; -- cgit v1.3 From 6f84b79810102708ec42a371e7a4a3446c741d03 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 00:19:38 +0100 Subject: sixel: always bump row on unhook --- sixel.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/sixel.c b/sixel.c index 240754f7..4bb7a28e 100644 --- a/sixel.c +++ b/sixel.c @@ -45,12 +45,10 @@ sixel_unhook(struct terminal *term) LOG_DBG("generating %dx%d pixman image", term->sixel.row * 6, term->sixel.max_col); - if (term->sixel.col >= 0) { - if (term->sixel.col > term->sixel.max_col) - term->sixel.max_col = term->sixel.col; - term->sixel.row++; - term->sixel.col = 0; - } + if (term->sixel.col > term->sixel.max_col) + term->sixel.max_col = term->sixel.col; + term->sixel.row++; + term->sixel.col = 0; struct sixel image = { .data = term->sixel.image, -- cgit v1.3 From bb82b9fabc00e1fadca6fe630e91f07dbe1168b3 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 00:23:19 +0100 Subject: sixel: add sixel_destroy() --- sixel.c | 17 +++++++++++++---- sixel.h | 2 ++ terminal.c | 17 +++++++---------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/sixel.c b/sixel.c index 4bb7a28e..fdad65a9 100644 --- a/sixel.c +++ b/sixel.c @@ -37,14 +37,22 @@ sixel_init(struct terminal *term) /* TODO: default palette */ } +void +sixel_destroy(struct sixel *sixel) +{ + pixman_image_unref(sixel->pix); + free(sixel->data); + + sixel->pix = NULL; + sixel->data = NULL; +} + void sixel_unhook(struct terminal *term) { free(term->sixel.palette); term->sixel.palette = NULL; - LOG_DBG("generating %dx%d pixman image", term->sixel.row * 6, term->sixel.max_col); - if (term->sixel.col > term->sixel.max_col) term->sixel.max_col = term->sixel.col; term->sixel.row++; @@ -58,6 +66,8 @@ sixel_unhook(struct terminal *term) .pos = (struct coord){term->cursor.point.col, term->grid->offset + term->cursor.point.row}, }; + LOG_DBG("generating %dx%d pixman image", image.width, image.height); + image.pix = pixman_image_create_bits_no_clear( PIXMAN_a8r8g8b8, image.width, image.height, @@ -66,8 +76,7 @@ sixel_unhook(struct terminal *term) tll_foreach(term->sixel_images, it) { if (it->item.pos.row == image.pos.row) { - pixman_image_unref(it->item.pix); - free(it->item.data); + sixel_destroy(&it->item); tll_remove(term->sixel_images, it); } } diff --git a/sixel.h b/sixel.h index 0cb24bfe..5933cf81 100644 --- a/sixel.h +++ b/sixel.h @@ -5,3 +5,5 @@ void sixel_init(struct terminal *term); void sixel_put(struct terminal *term, uint8_t c); void sixel_unhook(struct terminal *term); + +void sixel_destroy(struct sixel *sixel); diff --git a/terminal.c b/terminal.c index c6c349c3..cb7c08e9 100644 --- a/terminal.c +++ b/terminal.c @@ -19,12 +19,13 @@ #include "log.h" #include "async.h" +#include "config.h" #include "grid.h" #include "render.h" -#include "vt.h" #include "selection.h" -#include "config.h" +#include "sixel.h" #include "slave.h" +#include "vt.h" #define min(x, y) ((x) < (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y)) @@ -994,10 +995,8 @@ term_destroy(struct terminal *term) tll_free(term->ptmx_buffer); tll_free(term->tab_stops); - tll_foreach(term->sixel_images, it) { - pixman_image_unref(it->item.pix); - free(it->item.data); - } + tll_foreach(term->sixel_images, it) + sixel_destroy(&it->item); tll_free(term->sixel_images); free(term->foot_exe); @@ -1538,8 +1537,7 @@ term_scroll_partial(struct terminal *term, struct scroll_region region, int rows int new_row = (term->grid->offset + r) & (term->grid->num_rows - 1); if (img_top_row == new_row) { - pixman_image_unref(it->item.pix); - free(it->item.data); + sixel_destroy(&it->item); tll_remove(term->sixel_images, it); } } @@ -1605,8 +1603,7 @@ term_scroll_reverse_partial(struct terminal *term, int new_row = (term->grid->offset + r) & (term->grid->num_rows - 1); if (img_bottom_row == new_row) { - pixman_image_unref(it->item.pix); - free(it->item.data); + sixel_destroy(&it->item); tll_remove(term->sixel_images, it); } } -- cgit v1.3 From 00c6d4dde1346dc5f3bee394f147b8195c46a28c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 10:46:35 +0100 Subject: sixel: resize image buffer dynamically --- sixel.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++-------------- terminal.h | 7 ++++- 2 files changed, 86 insertions(+), 24 deletions(-) diff --git a/sixel.c b/sixel.c index fdad65a9..81dd980f 100644 --- a/sixel.c +++ b/sixel.c @@ -10,8 +10,6 @@ #define max(x, y) ((x) > (y) ? (x) : (y)) static const size_t COLOR_COUNT = 1024; -static const size_t IMAGE_WIDTH = 800; -static const size_t IMAGE_HEIGHT = 800; static size_t count; @@ -19,7 +17,7 @@ void sixel_init(struct terminal *term) { assert(term->sixel.palette == NULL); - assert(term->sixel.image == NULL); + assert(term->sixel.image.data == NULL); term->sixel.state = SIXEL_SIXEL; term->sixel.row = 0; @@ -30,7 +28,9 @@ sixel_init(struct terminal *term) term->sixel.param_idx = 0; memset(term->sixel.params, 0, sizeof(term->sixel.params)); term->sixel.palette = calloc(COLOR_COUNT, sizeof(term->sixel.palette[0])); - term->sixel.image = calloc(IMAGE_WIDTH * IMAGE_HEIGHT, sizeof(term->sixel.image[0])); + term->sixel.image.data = calloc(1 * 6, sizeof(term->sixel.image.data[0])); + term->sixel.image.width = 1; + term->sixel.image.height = 6; count = 0; @@ -59,7 +59,7 @@ sixel_unhook(struct terminal *term) term->sixel.col = 0; struct sixel image = { - .data = term->sixel.image, + .data = term->sixel.image.data, .width = term->sixel.max_col, .height = term->sixel.row * 6, .rows = (term->sixel.row * 6 + term->cell_height - 1) / term->cell_height, @@ -71,8 +71,8 @@ sixel_unhook(struct terminal *term) image.pix = pixman_image_create_bits_no_clear( PIXMAN_a8r8g8b8, image.width, image.height, - term->sixel.image, - IMAGE_WIDTH * sizeof(uint32_t)); + term->sixel.image.data, + term->sixel.image.width * sizeof(uint32_t)); tll_foreach(term->sixel_images, it) { if (it->item.pos.row == image.pos.row) { @@ -83,7 +83,9 @@ sixel_unhook(struct terminal *term) tll_push_back(term->sixel_images, image); - term->sixel.image = NULL; + term->sixel.image.data = NULL; + term->sixel.image.width = 0; + term->sixel.image.height = 0; term->sixel.max_col = 0; term->sixel.col = 0; term->sixel.row = 0; @@ -95,25 +97,79 @@ sixel_unhook(struct terminal *term) render_refresh(term); } +static bool +resize(struct terminal *term, int new_width, int new_height) +{ + LOG_DBG("resizing image: %dx%d -> %dx%d", + term->sixel.image.width, term->sixel.image.height, + new_width, new_height); + + uint32_t *old_data = term->sixel.image.data; + const int old_width = term->sixel.image.width; + const int old_height = term->sixel.image.height; + + assert(new_width >= old_width); + assert(new_height >= old_height); + + uint32_t *new_data = NULL; + + if (new_width == old_width) { + /* Width (and thus stride) is the same, so we can simply + * re-alloc the existing buffer */ + + new_data = realloc(old_data, new_width * new_height * sizeof(uint32_t)); + if (new_data == NULL) { + LOG_ERRNO("failed to reallocate sixel image buffer"); + return false; + } + + assert(new_height > old_height); + memset(&new_data[old_height * new_width], 0, (new_height - old_height) * new_width * sizeof(uint32_t)); + } else { + /* Width (and thus stride) change - need to allocate a new buffer */ + assert(new_width > old_width); + new_data = malloc(new_width * new_height * sizeof(uint32_t)); + + /* Copy old rows, and zero-initialize the tail of each row */ + for (int r = 0; r < old_height; r++) { + memcpy(&new_data[r * new_width], &old_data[r * old_width], old_width * sizeof(uint32_t)); + memset(&new_data[r * new_width + old_width], 0, (new_width - old_width) * sizeof(uint32_t)); + } + + /* Zero-initiailize new rows */ + for (int r = old_height; r < new_height; r++) + memset(&new_data[r * new_width], 0, new_width * sizeof(uint32_t)); + + free(old_data); + } + + assert(new_data != NULL); + term->sixel.image.data = new_data; + term->sixel.image.width = new_width; + term->sixel.image.height = new_height; + + return true; +} + static void sixel_add(struct terminal *term, uint32_t color, uint8_t sixel) { - LOG_DBG("adding sixel %02hhx using color 0x%06x", sixel, color); - if (term->sixel.col >= IMAGE_WIDTH) { - LOG_WARN("column outside image width"); - return; - } - if (term->sixel.row >= IMAGE_HEIGHT) { - LOG_WARN("row outside image height"); - return; + //LOG_DBG("adding sixel %02hhx using color 0x%06x", sixel, color); + + if (term->sixel.col >= term->sixel.image.width || + term->sixel.row * 6 >= term->sixel.image.height) + { + resize(term, + max(term->sixel.max_col, term->sixel.col + 1), + (term->sixel.row + 1) * 6); } - for (int i = 0; i < 6; i++) { - int bit = sixel & 1; - sixel >>= 1; - if (bit) { - size_t idx = (term->sixel.row * 6 + i) * IMAGE_WIDTH + term->sixel.col; - term->sixel.image[idx] = term->colors.alpha / 256 << 24 | color; + for (int i = 0; i < 6; i++, sixel >>= 1) { + if (sixel & 1) { + size_t pixel_row = term->sixel.row * 6 + i; + size_t stride = term->sixel.image.width; + size_t idx = pixel_row * stride + term->sixel.col; + term->sixel.image.data[idx] = term->colors.alpha / 256 << 24 | color; } } @@ -176,7 +232,8 @@ sixel_repeat(struct terminal *term, uint8_t c) term->sixel.param *= 10; term->sixel.param += c - '0'; } else { - LOG_DBG("repeating '%c' %u times", c, term->sixel.param); + //LOG_DBG("repeating '%c' %u times", c, term->sixel.param); + term->sixel.state = SIXEL_SIXEL; for (unsigned i = 0; i < term->sixel.param; i++) sixel_sixel(term, c); diff --git a/terminal.h b/terminal.h index bf113aa3..593e0a9f 100644 --- a/terminal.h +++ b/terminal.h @@ -362,7 +362,12 @@ struct terminal { int max_col; unsigned params[4]; uint32_t *palette; - uint32_t *image; + + struct { + uint32_t *data; + int width; + int height; + } image; unsigned int param; unsigned param_idx; -- cgit v1.3 From b03ffa3720b3a1d4e2b0b63f317287cf3afc58cb Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 10:46:54 +0100 Subject: sixel: resize image buffer after receiving a "raster" command --- sixel.c | 1 + 1 file changed, 1 insertion(+) diff --git a/sixel.c b/sixel.c index 81dd980f..b3bbdb65 100644 --- a/sixel.c +++ b/sixel.c @@ -260,6 +260,7 @@ sixel_raster(struct terminal *term, uint8_t c) LOG_DBG("pan=%u, pad=%u (aspect ratio = %u), size=%ux%u", pan, pad, pan / pad, ph, pv); + resize(term, ph, pv); } switch (c) { -- cgit v1.3 From ad5b2030f0094f9232af5170823ef66fdde7a625 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 10:47:16 +0100 Subject: term: free sixel images on reset --- terminal.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/terminal.c b/terminal.c index cb7c08e9..3350c065 100644 --- a/terminal.c +++ b/terminal.c @@ -1121,6 +1121,10 @@ term_reset(struct terminal *term, bool hard) term->meta.esc_prefix = true; term->meta.eight_bit = true; + tll_foreach(term->sixel_images, it) + sixel_destroy(&it->item); + tll_free(term->sixel_images); + if (!hard) return; -- cgit v1.3 From 87193ff339df0e9808ef438239bfa84b4dcf1b7c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 10:49:00 +0100 Subject: sixel: raster: only resize image buffer if necessary --- sixel.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sixel.c b/sixel.c index b3bbdb65..cfef67f3 100644 --- a/sixel.c +++ b/sixel.c @@ -260,7 +260,9 @@ sixel_raster(struct terminal *term, uint8_t c) LOG_DBG("pan=%u, pad=%u (aspect ratio = %u), size=%ux%u", pan, pad, pan / pad, ph, pv); - resize(term, ph, pv); + + if (ph >= term->sixel.image.height && pv >= term->sixel.image.width) + resize(term, ph, pv); } switch (c) { -- cgit v1.3 From 1b728dcac823193d96a490b7f7ee1acfa782da7f Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 10:50:05 +0100 Subject: sixel: rename state SIXEL_SIXEL -> SIXEL_GROUND --- sixel.c | 12 ++++++------ terminal.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/sixel.c b/sixel.c index cfef67f3..29be361b 100644 --- a/sixel.c +++ b/sixel.c @@ -19,7 +19,7 @@ sixel_init(struct terminal *term) assert(term->sixel.palette == NULL); assert(term->sixel.image.data == NULL); - term->sixel.state = SIXEL_SIXEL; + term->sixel.state = SIXEL_GROUND; term->sixel.row = 0; term->sixel.col = 0; term->sixel.color_idx = 0; @@ -234,7 +234,7 @@ sixel_repeat(struct terminal *term, uint8_t c) } else { //LOG_DBG("repeating '%c' %u times", c, term->sixel.param); - term->sixel.state = SIXEL_SIXEL; + term->sixel.state = SIXEL_GROUND; for (unsigned i = 0; i < term->sixel.param; i++) sixel_sixel(term, c); } @@ -268,7 +268,7 @@ sixel_raster(struct terminal *term, uint8_t c) switch (c) { case '#': term->sixel.state = SIXEL_COLOR; break; case ';': term->sixel.state = SIXEL_RASTER; break; - default: term->sixel.state = SIXEL_SIXEL; sixel_sixel(term, c); break; + default: term->sixel.state = SIXEL_GROUND; sixel_sixel(term, c); break; } } } @@ -291,7 +291,7 @@ sixel_color(struct terminal *term, uint8_t c) switch (c) { case '#': term->sixel.state = SIXEL_COLOR; break; case ';': term->sixel.state = SIXEL_COLOR_SPEC; break; - default: term->sixel.state = SIXEL_SIXEL; sixel_sixel(term, c); break; + default: term->sixel.state = SIXEL_GROUND; sixel_sixel(term, c); break; } } } @@ -331,7 +331,7 @@ sixel_color_spec(struct terminal *term, uint8_t c) switch (c) { case '#': term->sixel.state = SIXEL_COLOR; break; case ';': term->sixel.state = SIXEL_COLOR_SPEC; break; - default: term->sixel.state = SIXEL_SIXEL; sixel_sixel(term, c); break; + default: term->sixel.state = SIXEL_GROUND; sixel_sixel(term, c); break; } } } @@ -347,7 +347,7 @@ sixel_put(struct terminal *term, uint8_t c) } switch (term->sixel.state) { - case SIXEL_SIXEL: sixel_sixel(term, c); break; + case SIXEL_GROUND: sixel_sixel(term, c); break; case SIXEL_REPEAT: sixel_repeat(term, c); break; case SIXEL_RASTER: sixel_raster(term, c); break; case SIXEL_COLOR: sixel_color(term, c); break; diff --git a/terminal.h b/terminal.h index 593e0a9f..c160c902 100644 --- a/terminal.h +++ b/terminal.h @@ -355,7 +355,7 @@ struct terminal { } delayed_render_timer; struct { - enum { SIXEL_SIXEL, SIXEL_REPEAT, SIXEL_RASTER, SIXEL_COLOR, SIXEL_COLOR_SPEC} state; + enum { SIXEL_GROUND, SIXEL_REPEAT, SIXEL_RASTER, SIXEL_COLOR, SIXEL_COLOR_SPEC} state; int row; int col; int color_idx; -- cgit v1.3 From f1936959603c7f1530c6ce72cfda1f64edcb0432 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 10:54:52 +0100 Subject: sixel: use a struct coord for current sixel position --- sixel.c | 46 ++++++++++++++++++++++------------------------ terminal.h | 22 +++++++++++----------- 2 files changed, 33 insertions(+), 35 deletions(-) diff --git a/sixel.c b/sixel.c index 29be361b..8751ac2a 100644 --- a/sixel.c +++ b/sixel.c @@ -20,8 +20,7 @@ sixel_init(struct terminal *term) assert(term->sixel.image.data == NULL); term->sixel.state = SIXEL_GROUND; - term->sixel.row = 0; - term->sixel.col = 0; + term->sixel.pos = (struct coord){0, 0}; term->sixel.color_idx = 0; term->sixel.max_col = 0; term->sixel.param = 0; @@ -53,16 +52,16 @@ sixel_unhook(struct terminal *term) free(term->sixel.palette); term->sixel.palette = NULL; - if (term->sixel.col > term->sixel.max_col) - term->sixel.max_col = term->sixel.col; - term->sixel.row++; - term->sixel.col = 0; + if (term->sixel.pos.col > term->sixel.max_col) + term->sixel.max_col = term->sixel.pos.col; + term->sixel.pos.row++; + term->sixel.pos.col = 0; struct sixel image = { .data = term->sixel.image.data, .width = term->sixel.max_col, - .height = term->sixel.row * 6, - .rows = (term->sixel.row * 6 + term->cell_height - 1) / term->cell_height, + .height = term->sixel.pos.row * 6, + .rows = (term->sixel.pos.row * 6 + term->cell_height - 1) / term->cell_height, .pos = (struct coord){term->cursor.point.col, term->grid->offset + term->cursor.point.row}, }; @@ -87,8 +86,7 @@ sixel_unhook(struct terminal *term) term->sixel.image.width = 0; term->sixel.image.height = 0; term->sixel.max_col = 0; - term->sixel.col = 0; - term->sixel.row = 0; + term->sixel.pos = (struct coord){0, 0}; const size_t lines = (image.height + term->cell_height - 1) / term->cell_height; for (size_t i = 0; i < lines; i++) @@ -156,26 +154,26 @@ sixel_add(struct terminal *term, uint32_t color, uint8_t sixel) { //LOG_DBG("adding sixel %02hhx using color 0x%06x", sixel, color); - if (term->sixel.col >= term->sixel.image.width || - term->sixel.row * 6 >= term->sixel.image.height) + if (term->sixel.pos.col >= term->sixel.image.width || + term->sixel.pos.row * 6 >= term->sixel.image.height) { resize(term, - max(term->sixel.max_col, term->sixel.col + 1), - (term->sixel.row + 1) * 6); + max(term->sixel.max_col, term->sixel.pos.col + 1), + (term->sixel.pos.row + 1) * 6); } for (int i = 0; i < 6; i++, sixel >>= 1) { if (sixel & 1) { - size_t pixel_row = term->sixel.row * 6 + i; + size_t pixel_row = term->sixel.pos.row * 6 + i; size_t stride = term->sixel.image.width; - size_t idx = pixel_row * stride + term->sixel.col; + size_t idx = pixel_row * stride + term->sixel.pos.col; term->sixel.image.data[idx] = term->colors.alpha / 256 << 24 | color; } } assert(sixel == 0); - term->sixel.col++; + term->sixel.pos.col++; } static void @@ -196,16 +194,16 @@ sixel_sixel(struct terminal *term, uint8_t c) break; case '$': - if (term->sixel.col > term->sixel.max_col) - term->sixel.max_col = term->sixel.col; - term->sixel.col = 0; + if (term->sixel.pos.col > term->sixel.max_col) + term->sixel.max_col = term->sixel.pos.col; + term->sixel.pos.col = 0; break; case '-': - if (term->sixel.col > term->sixel.max_col) - term->sixel.max_col = term->sixel.col; - term->sixel.row++; - term->sixel.col = 0; + if (term->sixel.pos.col > term->sixel.max_col) + term->sixel.max_col = term->sixel.pos.col; + term->sixel.pos.row++; + term->sixel.pos.col = 0; break; case '!': diff --git a/terminal.h b/terminal.h index c160c902..05b9b00b 100644 --- a/terminal.h +++ b/terminal.h @@ -356,21 +356,21 @@ struct terminal { struct { enum { SIXEL_GROUND, SIXEL_REPEAT, SIXEL_RASTER, SIXEL_COLOR, SIXEL_COLOR_SPEC} state; - int row; - int col; - int color_idx; - int max_col; - unsigned params[4]; - uint32_t *palette; + + struct coord pos; /* Current sixel coordinate */ + int color_idx; /* Current palette index */ + int max_col; /* Largest column index we've seen (aka the image width) */ + uint32_t *palette; /* Color palette */ struct { - uint32_t *data; - int width; - int height; + uint32_t *data; /* Raw image data, in ARGB */ + int width; /* Image width, in pixels */ + int height; /* Image height, in pixels */ } image; - unsigned int param; - unsigned param_idx; + unsigned params[4]; /* Collected parmaeters, for RASTER, COLOR_SPEC */ + unsigned int param; /* Currently collecting parameter, for RASTER, COLOR_SPEC and REPEAT */ + unsigned param_idx; /* Parameters seen */ } sixel; tll(struct sixel) sixel_images; -- cgit v1.3 From 762526485113ea711dcf322063b1f3c6dc3e753e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 11:30:30 +0100 Subject: sixel: clean up state machine Use proper state names, and implement the state machine using switch statements. --- sixel.c | 188 +++++++++++++++++++++++++++++++------------------------------ terminal.h | 11 +++- 2 files changed, 105 insertions(+), 94 deletions(-) diff --git a/sixel.c b/sixel.c index 8751ac2a..0656e4c1 100644 --- a/sixel.c +++ b/sixel.c @@ -7,7 +7,9 @@ #include "log.h" #include "render.h" +#define ALEN(v) (sizeof(v) / sizeof(v[0])) #define max(x, y) ((x) > (y) ? (x) : (y)) +#define min(x, y) ((x) < (y) ? (x) : (y)) static const size_t COLOR_COUNT = 1024; @@ -19,7 +21,7 @@ sixel_init(struct terminal *term) assert(term->sixel.palette == NULL); assert(term->sixel.image.data == NULL); - term->sixel.state = SIXEL_GROUND; + term->sixel.state = SIXEL_DECSIXEL; term->sixel.pos = (struct coord){0, 0}; term->sixel.color_idx = 0; term->sixel.max_col = 0; @@ -177,17 +179,23 @@ sixel_add(struct terminal *term, uint32_t color, uint8_t sixel) } static void -sixel_sixel(struct terminal *term, uint8_t c) +decsixel(struct terminal *term, uint8_t c) { switch (c) { case '"': - term->sixel.state = SIXEL_RASTER; + term->sixel.state = SIXEL_DECGRA; + term->sixel.param = 0; + term->sixel.param_idx = 0; + break; + + case '!': + term->sixel.state = SIXEL_DECGRI; term->sixel.param = 0; term->sixel.param_idx = 0; break; case '#': - term->sixel.state = SIXEL_COLOR; + term->sixel.state = SIXEL_DECGCI; term->sixel.color_idx = 0; term->sixel.param = 0; term->sixel.param_idx = 0; @@ -206,115 +214,118 @@ sixel_sixel(struct terminal *term, uint8_t c) term->sixel.pos.col = 0; break; - case '!': - term->sixel.state = SIXEL_REPEAT; - term->sixel.param = 0; - term->sixel.param_idx = 0; + case '?'...'~': + sixel_add(term, term->sixel.palette[term->sixel.color_idx], c - 63); break; - default: - if (c < '?' || c > '~') { - LOG_ERR("invalid sixel charactwer: '%c' at idx=%zu", c, count); - return; - } + case ' ': + case '\n': + case '\r': + break; - sixel_add(term, term->sixel.palette[term->sixel.color_idx], c - 63); + default: + LOG_WARN("invalid sixel charactwer: '%c' at idx=%zu", c, count); break; } } static void -sixel_repeat(struct terminal *term, uint8_t c) +decgra(struct terminal *term, uint8_t c) { - if (c >= '0' && c <= '9') { + switch (c) { + case '0'...'9': term->sixel.param *= 10; term->sixel.param += c - '0'; - } else { - //LOG_DBG("repeating '%c' %u times", c, term->sixel.param); + break; - term->sixel.state = SIXEL_GROUND; - for (unsigned i = 0; i < term->sixel.param; i++) - sixel_sixel(term, c); - } -} + case ';': + if (term->sixel.param_idx < ALEN(term->sixel.params)) + term->sixel.params[term->sixel.param_idx++] = term->sixel.param; + term->sixel.param = 0; + break; -static void -sixel_raster(struct terminal *term, uint8_t c) -{ - if (c >= '0' && c <= '9') { - term->sixel.param *= 10; - term->sixel.param += c - '0'; - } else { - if (term->sixel.param_idx < sizeof(term->sixel.params) / sizeof(term->sixel.params[0])) + default: { + if (term->sixel.param_idx < ALEN(term->sixel.params)) term->sixel.params[term->sixel.param_idx++] = term->sixel.param; - term->sixel.param = 0; + int nparams = term->sixel.param_idx; + unsigned pan = nparams > 0 ? term->sixel.params[0] : 0; + unsigned pad = nparams > 1 ? term->sixel.params[1] : 0; + unsigned ph = nparams > 2 ? term->sixel.params[2] : 0; + unsigned pv = nparams > 3 ? term->sixel.params[3] : 0; - if (c != ';') { - unsigned pan __attribute__((unused)) = term->sixel.params[0]; - unsigned pad __attribute__((unused)) = term->sixel.params[1]; - unsigned ph __attribute__((unused)) = term->sixel.params[2]; - unsigned pv __attribute__((unused)) = term->sixel.params[3]; + pan = pan > 0 ? pan : 1; + pad = pad > 0 ? pad : 1; - LOG_DBG("pan=%u, pad=%u (aspect ratio = %u), size=%ux%u", - pan, pad, pan / pad, ph, pv); + LOG_DBG("pan=%u, pad=%u (aspect ratio = %u), size=%ux%u", + pan, pad, pan / pad, ph, pv); - if (ph >= term->sixel.image.height && pv >= term->sixel.image.width) - resize(term, ph, pv); - } + if (ph >= term->sixel.image.height && pv >= term->sixel.image.width) + resize(term, ph, pv); - switch (c) { - case '#': term->sixel.state = SIXEL_COLOR; break; - case ';': term->sixel.state = SIXEL_RASTER; break; - default: term->sixel.state = SIXEL_GROUND; sixel_sixel(term, c); break; - } + term->sixel.state = SIXEL_DECSIXEL; + sixel_put(term, c); + break; + } } } static void -sixel_color(struct terminal *term, uint8_t c) +decgri(struct terminal *term, uint8_t c) { - if (c >= '0' && c <= '9') { + switch (c) { + case '0'...'9': term->sixel.param *= 10; term->sixel.param += c - '0'; - } else { - if (term->sixel.param < COLOR_COUNT) - term->sixel.color_idx = term->sixel.param; - else - term->sixel.color_idx = 0; - - term->sixel.param_idx = 0; - term->sixel.param = 0; + break; - switch (c) { - case '#': term->sixel.state = SIXEL_COLOR; break; - case ';': term->sixel.state = SIXEL_COLOR_SPEC; break; - default: term->sixel.state = SIXEL_GROUND; sixel_sixel(term, c); break; - } + default: + //LOG_DBG("repeating '%c' %u times", c, term->sixel.param); + for (unsigned i = 0; i < term->sixel.param; i++) + decsixel(term, c); + term->sixel.state = SIXEL_DECSIXEL; + break; } } static void -sixel_color_spec(struct terminal *term, uint8_t c) +decgci(struct terminal *term, uint8_t c) { - if (c >= '0' && c <= '9') { + switch (c) { + case '0'...'9': term->sixel.param *= 10; term->sixel.param += c - '0'; - } else { - if (term->sixel.param_idx < sizeof(term->sixel.params) / sizeof(term->sixel.params[0])) - term->sixel.params[term->sixel.param_idx++] = term->sixel.param; + break; + case ';': + if (term->sixel.param_idx < ALEN(term->sixel.params)) + term->sixel.params[term->sixel.param_idx++] = term->sixel.param; term->sixel.param = 0; + break; - if (c != ';') { - unsigned format = term->sixel.params[0]; - unsigned c1 = term->sixel.params[1]; - unsigned c2 = term->sixel.params[2]; - unsigned c3 = term->sixel.params[3]; + default: { + if (term->sixel.param_idx < ALEN(term->sixel.params)) + term->sixel.params[term->sixel.param_idx++] = term->sixel.param; + + int nparams = term->sixel.param_idx; + + if (nparams > 0) + term->sixel.color_idx = min(1 + term->sixel.params[0], COLOR_COUNT - 1); + + if (nparams > 4) { + unsigned format = term->sixel.params[1]; + unsigned c1 = term->sixel.params[2]; + unsigned c2 = term->sixel.params[3]; + unsigned c3 = term->sixel.params[4]; - if (format == 1) { + switch (format) { + case 1: { /* HLS */ + LOG_ERR("HLS color format not implemented"); assert(false && "HLS color format not implemented"); - } else { + break; + } + + case 2: { /* RGB */ uint8_t r = 255 * c1 / 100; uint8_t g = 255 * c2 / 100; uint8_t b = 255 * c3 / 100; @@ -323,32 +334,27 @@ sixel_color_spec(struct terminal *term, uint8_t c) term->sixel.color_idx, r, g, b); term->sixel.palette[term->sixel.color_idx] = r << 16 | g << 8 | b; + break; + } } } - switch (c) { - case '#': term->sixel.state = SIXEL_COLOR; break; - case ';': term->sixel.state = SIXEL_COLOR_SPEC; break; - default: term->sixel.state = SIXEL_GROUND; sixel_sixel(term, c); break; - } + term->sixel.state = SIXEL_DECSIXEL; + sixel_put(term, c); + break; + } } } void sixel_put(struct terminal *term, uint8_t c) { - count++; - switch (c) { - case ' ': return; - case '\n': return; - case '\r': return; - } - switch (term->sixel.state) { - case SIXEL_GROUND: sixel_sixel(term, c); break; - case SIXEL_REPEAT: sixel_repeat(term, c); break; - case SIXEL_RASTER: sixel_raster(term, c); break; - case SIXEL_COLOR: sixel_color(term, c); break; - case SIXEL_COLOR_SPEC: sixel_color_spec(term, c); break; + case SIXEL_DECSIXEL: decsixel(term, c); break; + case SIXEL_DECGRA: decgra(term, c); break; + case SIXEL_DECGRI: decgri(term, c); break; + case SIXEL_DECGCI: decgci(term, c); break; } + + count++; } diff --git a/terminal.h b/terminal.h index 05b9b00b..882abfba 100644 --- a/terminal.h +++ b/terminal.h @@ -355,7 +355,12 @@ struct terminal { } delayed_render_timer; struct { - enum { SIXEL_GROUND, SIXEL_REPEAT, SIXEL_RASTER, SIXEL_COLOR, SIXEL_COLOR_SPEC} state; + enum { + SIXEL_DECSIXEL, /* DECSIXEL body part ", $, -, ? ... ~ */ + SIXEL_DECGRA, /* DECGRA Set Raster Attributes " Pan; Pad; Ph; Pv */ + SIXEL_DECGRI, /* DECGRI Graphics Repeat Introducer ! Pn Ch */ + SIXEL_DECGCI, /* DECGCI Graphics Color Introducer # Pc; Pu; Px; Py; Pz */ + } state; struct coord pos; /* Current sixel coordinate */ int color_idx; /* Current palette index */ @@ -368,8 +373,8 @@ struct terminal { int height; /* Image height, in pixels */ } image; - unsigned params[4]; /* Collected parmaeters, for RASTER, COLOR_SPEC */ - unsigned int param; /* Currently collecting parameter, for RASTER, COLOR_SPEC and REPEAT */ + unsigned params[5]; /* Collected parmaeters, for RASTER, COLOR_SPEC */ + unsigned param; /* Currently collecting parameter, for RASTER, COLOR_SPEC and REPEAT */ unsigned param_idx; /* Parameters seen */ } sixel; -- cgit v1.3 From a418521ced195562c00b49a7676d9396eaf8189f Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 11:37:25 +0100 Subject: sixel: mention why we add one --- sixel.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sixel.c b/sixel.c index 0656e4c1..7e547822 100644 --- a/sixel.c +++ b/sixel.c @@ -309,8 +309,10 @@ decgci(struct terminal *term, uint8_t c) int nparams = term->sixel.param_idx; - if (nparams > 0) + if (nparams > 0) { + /* Add one, as we use idx==0 for background color (TODO) */ term->sixel.color_idx = min(1 + term->sixel.params[0], COLOR_COUNT - 1); + } if (nparams > 4) { unsigned format = term->sixel.params[1]; -- cgit v1.3 From a8dc61d0ffbe9f95ab2fb5d39474d66d5e57de2e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 11:52:22 +0100 Subject: sixel: resize: initialize new rows/columns with the background color --- sixel.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/sixel.c b/sixel.c index 7e547822..f7966b80 100644 --- a/sixel.c +++ b/sixel.c @@ -29,10 +29,13 @@ sixel_init(struct terminal *term) term->sixel.param_idx = 0; memset(term->sixel.params, 0, sizeof(term->sixel.params)); term->sixel.palette = calloc(COLOR_COUNT, sizeof(term->sixel.palette[0])); - term->sixel.image.data = calloc(1 * 6, sizeof(term->sixel.image.data[0])); + term->sixel.image.data = malloc(1 * 6 * sizeof(term->sixel.image.data[0])); term->sixel.image.width = 1; term->sixel.image.height = 6; + for (size_t i = 0; i < 1 * 6; i++) + term->sixel.image.data[i] = term->colors.alpha / 256 << 24 | term->sixel.palette[0]; + count = 0; /* TODO: default palette */ @@ -124,25 +127,28 @@ resize(struct terminal *term, int new_width, int new_height) } assert(new_height > old_height); - memset(&new_data[old_height * new_width], 0, (new_height - old_height) * new_width * sizeof(uint32_t)); + } else { /* Width (and thus stride) change - need to allocate a new buffer */ assert(new_width > old_width); new_data = malloc(new_width * new_height * sizeof(uint32_t)); - /* Copy old rows, and zero-initialize the tail of each row */ + /* Copy old rows, and initialize new columns to background color */ for (int r = 0; r < old_height; r++) { memcpy(&new_data[r * new_width], &old_data[r * old_width], old_width * sizeof(uint32_t)); - memset(&new_data[r * new_width + old_width], 0, (new_width - old_width) * sizeof(uint32_t)); - } - - /* Zero-initiailize new rows */ - for (int r = old_height; r < new_height; r++) - memset(&new_data[r * new_width], 0, new_width * sizeof(uint32_t)); + for (int c = old_width; c < new_width; c++) + new_data[r * new_width + c] = term->colors.alpha / 256 << 24 | term->sixel.palette[0]; + } free(old_data); } + /* Initialize new rows to background color */ + for (int r = old_height; r < new_height; r++) { + for (int c = 0; c < new_width; c++) + new_data[r * new_width + c] = term->colors.alpha / 256 << 24 | term->sixel.palette[0]; + } + assert(new_data != NULL); term->sixel.image.data = new_data; term->sixel.image.width = new_width; -- cgit v1.3 From 23d39e6f90bf57a4690f18312f32ef99eb665774 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 11:52:48 +0100 Subject: sixel: finalize: use width/height from image, not row/col pos --- sixel.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/sixel.c b/sixel.c index f7966b80..7da45ec5 100644 --- a/sixel.c +++ b/sixel.c @@ -57,16 +57,11 @@ sixel_unhook(struct terminal *term) free(term->sixel.palette); term->sixel.palette = NULL; - if (term->sixel.pos.col > term->sixel.max_col) - term->sixel.max_col = term->sixel.pos.col; - term->sixel.pos.row++; - term->sixel.pos.col = 0; - struct sixel image = { .data = term->sixel.image.data, - .width = term->sixel.max_col, - .height = term->sixel.pos.row * 6, - .rows = (term->sixel.pos.row * 6 + term->cell_height - 1) / term->cell_height, + .width = term->sixel.image.width, + .height = term->sixel.image.height, + .rows = (term->sixel.image.height + term->cell_height - 1) / term->cell_height, .pos = (struct coord){term->cursor.point.col, term->grid->offset + term->cursor.point.row}, }; @@ -93,8 +88,7 @@ sixel_unhook(struct terminal *term) term->sixel.max_col = 0; term->sixel.pos = (struct coord){0, 0}; - const size_t lines = (image.height + term->cell_height - 1) / term->cell_height; - for (size_t i = 0; i < lines; i++) + for (size_t i = 0; i < image.rows; i++) term_linefeed(term); term_formfeed(term); render_refresh(term); -- cgit v1.3 From a9971f4c328a1cdeae59dc767b57f10c47224c9d Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 12:16:19 +0100 Subject: csi: DA: we now implement sixel --- csi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csi.c b/csi.c index 3fc64ee7..ed237592 100644 --- a/csi.c +++ b/csi.c @@ -372,7 +372,7 @@ csi_dispatch(struct terminal *term, uint8_t final) * - 28 Rectangular editing. * - 29 ANSI text locator (i.e., DEC Locator mode). */ - const char *reply = "\033[?62;6;15;17;22c"; + const char *reply = "\033[?62;4;6;15;17;22c"; term_to_slave(term, reply, strlen(reply)); break; } -- cgit v1.3 From 5e65db3b070375abf4f32e62b0f05cc464ec28a2 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 14:01:20 +0100 Subject: sixel: use terminal's background color, not palette[0] --- sixel.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sixel.c b/sixel.c index 7da45ec5..69025669 100644 --- a/sixel.c +++ b/sixel.c @@ -34,7 +34,7 @@ sixel_init(struct terminal *term) term->sixel.image.height = 6; for (size_t i = 0; i < 1 * 6; i++) - term->sixel.image.data[i] = term->colors.alpha / 256 << 24 | term->sixel.palette[0]; + term->sixel.image.data[i] = term->colors.alpha / 256 << 24 | term->colors.bg; count = 0; @@ -132,7 +132,7 @@ resize(struct terminal *term, int new_width, int new_height) memcpy(&new_data[r * new_width], &old_data[r * old_width], old_width * sizeof(uint32_t)); for (int c = old_width; c < new_width; c++) - new_data[r * new_width + c] = term->colors.alpha / 256 << 24 | term->sixel.palette[0]; + new_data[r * new_width + c] = term->colors.alpha / 256 << 24 | term->colors.bg; } free(old_data); } @@ -140,7 +140,7 @@ resize(struct terminal *term, int new_width, int new_height) /* Initialize new rows to background color */ for (int r = old_height; r < new_height; r++) { for (int c = 0; c < new_width; c++) - new_data[r * new_width + c] = term->colors.alpha / 256 << 24 | term->sixel.palette[0]; + new_data[r * new_width + c] = term->colors.alpha / 256 << 24 | term->colors.bg; } assert(new_data != NULL); -- cgit v1.3 From 8e37a18083e8cdba1cfd4ee042d8f8edf7cfad39 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 14:02:00 +0100 Subject: sixel: application configurable palette size (color count) This implements the CSI escapes for retrieving and (re)setting the palette size. --- csi.c | 25 +++++++++++++++++++++++++ sixel.c | 41 +++++++++++++++++++++++++++++++++++------ sixel.h | 7 +++++++ terminal.c | 3 +++ terminal.h | 3 +++ 5 files changed, 73 insertions(+), 6 deletions(-) diff --git a/csi.c b/csi.c index ed237592..d5c06905 100644 --- a/csi.c +++ b/csi.c @@ -15,6 +15,7 @@ #include "grid.h" #include "vt.h" #include "selection.h" +#include "sixel.h" #define min(x, y) ((x) < (y) ? (x) : (y)) @@ -1227,6 +1228,30 @@ csi_dispatch(struct terminal *term, uint8_t final) } break; + case 'S': { + unsigned target = vt_param_get(term, 0, 0); + unsigned operation = vt_param_get(term, 1, 0); + + switch (target) { + case 1: + switch (operation) { + case 1: sixel_colors_report_current(term); break; + case 2: sixel_colors_reset(term); break; + case 3: sixel_colors_set(term, vt_param_get(term, 2, 0)); break; + case 4: sixel_colors_report_max(term); + } + break; + + case 2: + break; + + case 3: + break; + + } + break; + } + default: UNHANDLED(); break; diff --git a/sixel.c b/sixel.c index 69025669..2536f467 100644 --- a/sixel.c +++ b/sixel.c @@ -11,7 +11,6 @@ #define max(x, y) ((x) > (y) ? (x) : (y)) #define min(x, y) ((x) < (y) ? (x) : (y)) -static const size_t COLOR_COUNT = 1024; static size_t count; @@ -20,6 +19,7 @@ sixel_init(struct terminal *term) { assert(term->sixel.palette == NULL); assert(term->sixel.image.data == NULL); + assert(term->sixel.palette_size <= SIXEL_MAX_COLORS); term->sixel.state = SIXEL_DECSIXEL; term->sixel.pos = (struct coord){0, 0}; @@ -28,7 +28,7 @@ sixel_init(struct terminal *term) term->sixel.param = 0; term->sixel.param_idx = 0; memset(term->sixel.params, 0, sizeof(term->sixel.params)); - term->sixel.palette = calloc(COLOR_COUNT, sizeof(term->sixel.palette[0])); + term->sixel.palette = calloc(term->sixel.palette_size, sizeof(term->sixel.palette[0])); term->sixel.image.data = malloc(1 * 6 * sizeof(term->sixel.image.data[0])); term->sixel.image.width = 1; term->sixel.image.height = 6; @@ -309,10 +309,8 @@ decgci(struct terminal *term, uint8_t c) int nparams = term->sixel.param_idx; - if (nparams > 0) { - /* Add one, as we use idx==0 for background color (TODO) */ - term->sixel.color_idx = min(1 + term->sixel.params[0], COLOR_COUNT - 1); - } + if (nparams > 0) + term->sixel.color_idx = min(term->sixel.params[0], term->sixel.palette_size - 1); if (nparams > 4) { unsigned format = term->sixel.params[1]; @@ -360,3 +358,34 @@ sixel_put(struct terminal *term, uint8_t c) count++; } + +void +sixel_colors_report_current(struct terminal *term) +{ + char reply[24]; + snprintf(reply, sizeof(reply), "\033[?1;0;%uS", term->sixel.palette_size); + term_to_slave(term, reply, strlen(reply)); +} + +void +sixel_colors_reset(struct terminal *term) +{ + term->sixel.palette_size = SIXEL_MAX_COLORS; + LOG_DBG("sixel palette size reset to %u", SIXEL_MAX_COLORS); +} + +void +sixel_colors_set(struct terminal *term, unsigned count) +{ + unsigned new_palette_size = min(max(2, count), SIXEL_MAX_COLORS); + term->sixel.palette_size = new_palette_size; + LOG_DBG("sixel palette size set to %u", new_palette_size); +} + +void +sixel_colors_report_max(struct terminal *term) +{ + char reply[24]; + snprintf(reply, sizeof(reply), "\033[?1;0;%uS", SIXEL_MAX_COLORS); + term_to_slave(term, reply, strlen(reply)); +} diff --git a/sixel.h b/sixel.h index 5933cf81..8e6d2949 100644 --- a/sixel.h +++ b/sixel.h @@ -2,8 +2,15 @@ #include "terminal.h" +#define SIXEL_MAX_COLORS 1024u + void sixel_init(struct terminal *term); void sixel_put(struct terminal *term, uint8_t c); void sixel_unhook(struct terminal *term); void sixel_destroy(struct sixel *sixel); + +void sixel_colors_report_current(struct terminal *term); +void sixel_colors_reset(struct terminal *term); +void sixel_colors_set(struct terminal *term, unsigned count); +void sixel_colors_report_max(struct terminal *term); diff --git a/terminal.c b/terminal.c index 3350c065..493a8ada 100644 --- a/terminal.c +++ b/terminal.c @@ -741,6 +741,9 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, .lower_fd = delay_lower_fd, .upper_fd = delay_upper_fd, }, + .sixel = { + .palette_size = SIXEL_MAX_COLORS, + }, .sixel_images = tll_init(), .hold_at_exit = conf->hold_at_exit, .shutdown_cb = shutdown_cb, diff --git a/terminal.h b/terminal.h index 882abfba..e0738057 100644 --- a/terminal.h +++ b/terminal.h @@ -376,6 +376,9 @@ struct terminal { unsigned params[5]; /* Collected parmaeters, for RASTER, COLOR_SPEC */ unsigned param; /* Currently collecting parameter, for RASTER, COLOR_SPEC and REPEAT */ unsigned param_idx; /* Parameters seen */ + + /* Application configurable */ + unsigned palette_size; } sixel; tll(struct sixel) sixel_images; -- cgit v1.3 From 25b09b909ca39cee6432ffd10e179336dbc178e1 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 21:03:24 +0100 Subject: sixel: application configurable image max width/height --- csi.c | 10 +++++---- sixel.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- sixel.h | 5 +++++ terminal.h | 4 +++- 4 files changed, 86 insertions(+), 8 deletions(-) diff --git a/csi.c b/csi.c index d5c06905..38d017f5 100644 --- a/csi.c +++ b/csi.c @@ -1243,11 +1243,13 @@ csi_dispatch(struct terminal *term, uint8_t final) break; case 2: + switch (operation) { + case 1: sixel_geometry_report_current(term); break; + case 2: sixel_geometry_reset(term); break; + case 3: sixel_geometry_set(term, vt_param_get(term, 2, 0), vt_param_get(term, 3, 0)); break; + case 4: sixel_geometry_report_max(term); + } break; - - case 3: - break; - } break; } diff --git a/sixel.c b/sixel.c index 2536f467..01c6987b 100644 --- a/sixel.c +++ b/sixel.c @@ -94,6 +94,22 @@ sixel_unhook(struct terminal *term) render_refresh(term); } +static unsigned +max_width(const struct terminal *term) +{ + return term->sixel.max_width == 0 + ? term->cols * term->cell_width + : term->sixel.max_width; +} + +static unsigned +max_height(const struct terminal *term) +{ + return term->sixel.max_height == 0 + ? term->rows * term->cell_height + : term->sixel.max_height; +} + static bool resize(struct terminal *term, int new_width, int new_height) { @@ -156,8 +172,14 @@ sixel_add(struct terminal *term, uint32_t color, uint8_t sixel) { //LOG_DBG("adding sixel %02hhx using color 0x%06x", sixel, color); + if (term->sixel.pos.col >= max_width(term) || + term->sixel.pos.row * 6 + 5 >= max_height(term)) + { + return; + } + if (term->sixel.pos.col >= term->sixel.image.width || - term->sixel.pos.row * 6 >= term->sixel.image.height) + term->sixel.pos.row * 6 + 5 >= term->sixel.image.height) { resize(term, max(term->sixel.max_col, term->sixel.pos.col + 1), @@ -174,7 +196,6 @@ sixel_add(struct terminal *term, uint32_t color, uint8_t sixel) } assert(sixel == 0); - term->sixel.pos.col++; } @@ -260,8 +281,11 @@ decgra(struct terminal *term, uint8_t c) LOG_DBG("pan=%u, pad=%u (aspect ratio = %u), size=%ux%u", pan, pad, pan / pad, ph, pv); - if (ph >= term->sixel.image.height && pv >= term->sixel.image.width) + if (ph >= term->sixel.image.height && pv >= term->sixel.image.width && + ph <= max_height(term) && pv <= max_width(term)) + { resize(term, ph, pv); + } term->sixel.state = SIXEL_DECSIXEL; sixel_put(term, c); @@ -365,6 +389,7 @@ sixel_colors_report_current(struct terminal *term) char reply[24]; snprintf(reply, sizeof(reply), "\033[?1;0;%uS", term->sixel.palette_size); term_to_slave(term, reply, strlen(reply)); + LOG_DBG("query response for current color count: %u", term->sixel.palette_size); } void @@ -388,4 +413,48 @@ sixel_colors_report_max(struct terminal *term) char reply[24]; snprintf(reply, sizeof(reply), "\033[?1;0;%uS", SIXEL_MAX_COLORS); term_to_slave(term, reply, strlen(reply)); + LOG_DBG("query response for max color count: %u", SIXEL_MAX_COLORS); +} + +void +sixel_geometry_report_current(struct terminal *term) +{ + char reply[64]; + snprintf(reply, sizeof(reply), "\033[?2;0;%u;%uS", + max_width(term), max_height(term)); + term_to_slave(term, reply, strlen(reply)); + + LOG_DBG("query response for current sixel geometry: %ux%u", + max_width(term), max_height(term)); +} + +void +sixel_geometry_reset(struct terminal *term) +{ + term->sixel.max_width = 0; + term->sixel.max_height = 0; + LOG_DBG("sixel geometry reset to %ux%u", max_width(term), max_height(term)); +} + +void +sixel_geometry_set(struct terminal *term, unsigned width, unsigned height) +{ + term->sixel.max_width = width; + term->sixel.max_height = height; + LOG_DBG("sixel geometry set to %ux%u", + term->sixel.max_width, term->sixel.max_height); +} + +void +sixel_geometry_report_max(struct terminal *term) +{ + unsigned max_width = term->cols * term->cell_width; + unsigned max_height = term->rows * term->cell_height; + + char reply[64]; + snprintf(reply, sizeof(reply), "\033[?2;0;%u;%uS", max_width, max_height); + term_to_slave(term, reply, strlen(reply)); + + LOG_DBG("query response for max sixel geometry: %ux%u", + max_width, max_height); } diff --git a/sixel.h b/sixel.h index 8e6d2949..dbce21c5 100644 --- a/sixel.h +++ b/sixel.h @@ -14,3 +14,8 @@ void sixel_colors_report_current(struct terminal *term); void sixel_colors_reset(struct terminal *term); void sixel_colors_set(struct terminal *term, unsigned count); void sixel_colors_report_max(struct terminal *term); + +void sixel_geometry_report_current(struct terminal *term); +void sixel_geometry_reset(struct terminal *term); +void sixel_geometry_set(struct terminal *term, unsigned width, unsigned height); +void sixel_geometry_report_max(struct terminal *term); diff --git a/terminal.h b/terminal.h index e0738057..5e491d29 100644 --- a/terminal.h +++ b/terminal.h @@ -378,7 +378,9 @@ struct terminal { unsigned param_idx; /* Parameters seen */ /* Application configurable */ - unsigned palette_size; + unsigned palette_size; /* Number of colors in palette */ + unsigned max_width; /* Maximum image width, in pixels */ + unsigned max_height; /* Maximum image height, in pixels */ } sixel; tll(struct sixel) sixel_images; -- cgit v1.3 From 7985cefd65d98f6ffd86c64c3bb866a0283e10d2 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 21:04:24 +0100 Subject: sixel: add comments to max_width()/max_height() --- sixel.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sixel.c b/sixel.c index 01c6987b..be21d263 100644 --- a/sixel.c +++ b/sixel.c @@ -97,6 +97,7 @@ sixel_unhook(struct terminal *term) static unsigned max_width(const struct terminal *term) { + /* foot extension - treat 0 to mean current terminal size */ return term->sixel.max_width == 0 ? term->cols * term->cell_width : term->sixel.max_width; @@ -105,6 +106,7 @@ max_width(const struct terminal *term) static unsigned max_height(const struct terminal *term) { + /* foot extension - treat 0 to mean current terminal size */ return term->sixel.max_height == 0 ? term->rows * term->cell_height : term->sixel.max_height; -- cgit v1.3 From f96eb5ff074dff8c9c9dce63886a94875604901c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 21:18:55 +0100 Subject: sixel: add HLS color format support --- meson.build | 1 + sixel-hls.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ sixel-hls.h | 9 +++++ sixel.c | 7 ++-- 4 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 sixel-hls.c create mode 100644 sixel-hls.h diff --git a/meson.build b/meson.build index 16483f51..21c23718 100644 --- a/meson.build +++ b/meson.build @@ -118,6 +118,7 @@ executable( 'server.c', 'server.h', 'shm.c', 'shm.h', 'sixel.c', 'sixel.h', + 'sixel-hls.c', 'sixel-hls.h', 'slave.c', 'slave.h', 'terminal.c', 'terminal.h', 'tokenize.c', 'tokenize.h', diff --git a/sixel-hls.c b/sixel-hls.c new file mode 100644 index 00000000..8b88b414 --- /dev/null +++ b/sixel-hls.c @@ -0,0 +1,117 @@ +// sixel.c (part of mintty) +// this function is derived from a part of graphics.c +// in Xterm pl#310 originally written by Ross Combs. +// +// Copyright 2013,2014 by Ross Combs +// +// All Rights Reserved +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name(s) of the above copyright +// holders shall not be used in advertising or otherwise to promote the +// sale, use or other dealings in this Software without prior written +// authorization. + +#include "sixel-hls.h" + +#define SIXEL_RGB(r, g, b) (((r) << 16) + ((g) << 8) + (b)) + +int +hls_to_rgb(int hue, int lum, int sat) +{ + double hs = (hue + 240) % 360; + double hv = hs / 360.0; + double lv = lum / 100.0; + double sv = sat / 100.0; + double c, x, m, c2; + double r1, g1, b1; + int r, g, b; + int hpi; + + if (sat == 0) { + r = g = b = lum * 255 / 100; + return SIXEL_RGB(r, g, b); + } + + if ((c2 = ((2.0 * lv) - 1.0)) < 0.0) { + c2 = -c2; + } + c = (1.0 - c2) * sv; + hpi = (int) (hv * 6.0); + x = (hpi & 1) ? c : 0.0; + m = lv - 0.5 * c; + + switch (hpi) { + case 0: + r1 = c; + g1 = x; + b1 = 0.0; + break; + case 1: + r1 = x; + g1 = c; + b1 = 0.0; + break; + case 2: + r1 = 0.0; + g1 = c; + b1 = x; + break; + case 3: + r1 = 0.0; + g1 = x; + b1 = c; + break; + case 4: + r1 = x; + g1 = 0.0; + b1 = c; + break; + case 5: + r1 = c; + g1 = 0.0; + b1 = x; + break; + default: + return SIXEL_RGB(255, 255, 255); + } + + r = (int) ((r1 + m) * 100.0 + 0.5); + g = (int) ((g1 + m) * 100.0 + 0.5); + b = (int) ((b1 + m) * 100.0 + 0.5); + + if (r < 0) { + r = 0; + } else if (r > 100) { + r = 100; + } + if (g < 0) { + g = 0; + } else if (g > 100) { + g = 100; + } + if (b < 0) { + b = 0; + } else if (b > 100) { + b = 100; + } + return SIXEL_RGB(r * 255 / 100, g * 255 / 100, b * 255 / 100); +} diff --git a/sixel-hls.h b/sixel-hls.h new file mode 100644 index 00000000..6d6b9f2f --- /dev/null +++ b/sixel-hls.h @@ -0,0 +1,9 @@ +#pragma once + +/* + * Primary color hues: + * blue: 0 degrees + * red: 120 degrees + * green: 240 degrees + */ +int hls_to_rgb(int hue, int lum, int sat); diff --git a/sixel.c b/sixel.c index be21d263..7b1c8646 100644 --- a/sixel.c +++ b/sixel.c @@ -6,6 +6,7 @@ #define LOG_ENABLE_DBG 0 #include "log.h" #include "render.h" +#include "sixel-hls.h" #define ALEN(v) (sizeof(v) / sizeof(v[0])) #define max(x, y) ((x) > (y) ? (x) : (y)) @@ -346,8 +347,10 @@ decgci(struct terminal *term, uint8_t c) switch (format) { case 1: { /* HLS */ - LOG_ERR("HLS color format not implemented"); - assert(false && "HLS color format not implemented"); + uint32_t rgb = hls_to_rgb(c1, c2, c3); + LOG_DBG("setting palette #%d = HLS %hhu/%hhu/%hhu (0x%06x)", + term->sixel.color_idx, c1, c2, c3, rgb); + term->sixel.palette[term->sixel.color_idx] = rgb; break; } -- cgit v1.3 From 05ff2a8a15d5be6b996a16e84ffed5b8b43b7d49 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 21:20:22 +0100 Subject: sixel: don't ever shrink the image --- sixel.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/sixel.c b/sixel.c index 7b1c8646..15aeca43 100644 --- a/sixel.c +++ b/sixel.c @@ -184,9 +184,15 @@ sixel_add(struct terminal *term, uint32_t color, uint8_t sixel) if (term->sixel.pos.col >= term->sixel.image.width || term->sixel.pos.row * 6 + 5 >= term->sixel.image.height) { - resize(term, - max(term->sixel.max_col, term->sixel.pos.col + 1), - (term->sixel.pos.row + 1) * 6); + int width = max( + term->sixel.image.width, + max(term->sixel.max_col, term->sixel.pos.col + 1)); + + int height = max( + term->sixel.image.height, + (term->sixel.pos.row + 1) * 6)) + + resize(term, width, height);; } for (int i = 0; i < 6; i++, sixel >>= 1) { -- cgit v1.3 From 6e8c4149d2db6b400d6f47881a49ecaf9fb5c1a9 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 21:21:14 +0100 Subject: README: add sixel support to feature list --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f67ac691..ce1f8d9f 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ * Scrollback search * Color emoji support * "Server" mode (one master process, many windows) +* [Sixel image support](https://en.wikipedia.org/wiki/Sixel) ## Non-features -- cgit v1.3 From 462229af7025a29f421d4acf657a71175bafb8c0 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 21:24:21 +0100 Subject: sixel: oops --- sixel.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sixel.c b/sixel.c index 15aeca43..7d31dea4 100644 --- a/sixel.c +++ b/sixel.c @@ -190,9 +190,9 @@ sixel_add(struct terminal *term, uint32_t color, uint8_t sixel) int height = max( term->sixel.image.height, - (term->sixel.pos.row + 1) * 6)) + (term->sixel.pos.row + 1) * 6); - resize(term, width, height);; + resize(term, width, height); } for (int i = 0; i < 6; i++, sixel >>= 1) { -- cgit v1.3 From 80361ca04e12b65396d27fa1d981f6739861e453 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 21:35:45 +0100 Subject: sixel: purge images at current cursor row When printing a character, or starting a new sixel image, purge all images that cover the cursor's current row. --- sixel.c | 25 ++++++++++++++++++------- sixel.h | 1 + terminal.c | 2 ++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/sixel.c b/sixel.c index 7d31dea4..db87844e 100644 --- a/sixel.c +++ b/sixel.c @@ -39,6 +39,8 @@ sixel_init(struct terminal *term) count = 0; + sixel_purge_at_cursor(term); + /* TODO: default palette */ } @@ -52,6 +54,22 @@ sixel_destroy(struct sixel *sixel) sixel->data = NULL; } +void +sixel_purge_at_cursor(struct terminal *term) +{ + const int row = term->grid->offset + term->cursor.point.row; + + tll_foreach(term->sixel_images, it) { + const int start = it->item.pos.row; + const int end = start + it->item.rows; + + if (row >= start && row < end) { + sixel_destroy(&it->item); + tll_remove(term->sixel_images, it); + } + } +} + void sixel_unhook(struct terminal *term) { @@ -74,13 +92,6 @@ sixel_unhook(struct terminal *term) term->sixel.image.data, term->sixel.image.width * sizeof(uint32_t)); - tll_foreach(term->sixel_images, it) { - if (it->item.pos.row == image.pos.row) { - sixel_destroy(&it->item); - tll_remove(term->sixel_images, it); - } - } - tll_push_back(term->sixel_images, image); term->sixel.image.data = NULL; diff --git a/sixel.h b/sixel.h index dbce21c5..c5c489af 100644 --- a/sixel.h +++ b/sixel.h @@ -9,6 +9,7 @@ void sixel_put(struct terminal *term, uint8_t c); void sixel_unhook(struct terminal *term); void sixel_destroy(struct sixel *sixel); +void sixel_purge_at_cursor(struct terminal *term); void sixel_colors_report_current(struct terminal *term); void sixel_colors_reset(struct terminal *term); diff --git a/terminal.c b/terminal.c index 493a8ada..5c7b94e5 100644 --- a/terminal.c +++ b/terminal.c @@ -2128,6 +2128,8 @@ term_print(struct terminal *term, wchar_t wc, int width) print_linewrap(term); print_insert(term, width); + sixel_purge_at_cursor(term); + /* *Must* get current cell *after* linewrap+insert */ struct row *row = term->grid->cur_row; struct cell *cell = &row->cells[term->cursor.point.col]; -- cgit v1.3 From 270b7cc6f4a16e67fb65cccb43bb16a3e47c64ea Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 21:55:10 +0100 Subject: sixel: purge images in unhook, rather than in init This fixes an issue where the cursor was seen blinking in animated GIFs. --- sixel.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sixel.c b/sixel.c index db87844e..19dd6069 100644 --- a/sixel.c +++ b/sixel.c @@ -39,8 +39,6 @@ sixel_init(struct terminal *term) count = 0; - sixel_purge_at_cursor(term); - /* TODO: default palette */ } @@ -76,6 +74,8 @@ sixel_unhook(struct terminal *term) free(term->sixel.palette); term->sixel.palette = NULL; + sixel_purge_at_cursor(term); + struct sixel image = { .data = term->sixel.image.data, .width = term->sixel.image.width, -- cgit v1.3 From 5c7fc3a0c7e231385c31ac52cd79946d0cb94feb Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 22 Feb 2020 22:04:28 +0100 Subject: README: add screenshot of sixel graphics --- README.md | 3 +++ doc/sixel-wow.png | Bin 0 -> 119970 bytes 2 files changed, 3 insertions(+) create mode 100644 doc/sixel-wow.png diff --git a/README.md b/README.md index ce1f8d9f..46cd07cc 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,9 @@ * "Server" mode (one master process, many windows) * [Sixel image support](https://en.wikipedia.org/wiki/Sixel) + ![wow](doc/sixel-wow.png "Sixel screenshot") + + ## Non-features diff --git a/doc/sixel-wow.png b/doc/sixel-wow.png new file mode 100644 index 00000000..da481ac8 Binary files /dev/null and b/doc/sixel-wow.png differ -- cgit v1.3