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 --- terminal.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'terminal.h') 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 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(-) (limited to 'terminal.h') 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 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(-) (limited to 'terminal.h') 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 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(-) (limited to 'terminal.h') 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 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(-) (limited to 'terminal.h') 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(-) (limited to 'terminal.h') 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(-) (limited to 'terminal.h') 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 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(-) (limited to 'terminal.h') 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(-) (limited to 'terminal.h') 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