From 8c65c68b734b3a0711e8d0a7282d6c7d9def5ddf Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Mar 2021 22:05:40 +0100 Subject: sixel: get rid of an ‘imul’ in sixel_add() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By storing the current row’s byte offset into the backing image in the terminal struct. This replaces the ‘imul’ with a load, which can potentially be slow. But, this data should already be in the cache. --- terminal.h | 1 + 1 file changed, 1 insertion(+) (limited to 'terminal.h') diff --git a/terminal.h b/terminal.h index e4e662fb..2a8bb6f9 100644 --- a/terminal.h +++ b/terminal.h @@ -521,6 +521,7 @@ struct terminal { } state; struct coord pos; /* Current sixel coordinate */ + size_t row_byte_ofs; /* Byte position into image, for current row */ int color_idx; /* Current palette index */ int max_col; /* Largest column index we've seen (aka the image width) */ uint32_t *private_palette; /* Private palette, used when private mode 1070 is enabled */ -- cgit v1.3 From 4b0e9a6bee9699b9edce6b9afc6a66734ce00a49 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 7 Mar 2021 11:49:15 +0100 Subject: sixel: remove ‘max_col’ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The image width *is* the maximum number of columns we’ve seen. --- sixel.c | 15 +++------------ terminal.h | 1 - 2 files changed, 3 insertions(+), 13 deletions(-) (limited to 'terminal.h') diff --git a/sixel.c b/sixel.c index 4807f2a8..507caeee 100644 --- a/sixel.c +++ b/sixel.c @@ -39,7 +39,6 @@ sixel_init(struct terminal *term) term->sixel.pos = (struct coord){0, 0}; term->sixel.row_byte_ofs = 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)); @@ -830,7 +829,6 @@ sixel_unhook(struct terminal *term) term->sixel.image.data = NULL; term->sixel.image.width = 0; term->sixel.image.height = 0; - term->sixel.max_col = 0; term->sixel.pos = (struct coord){0, 0}; free(term->sixel.private_palette); @@ -923,12 +921,9 @@ sixel_add(struct terminal *term, uint32_t color, uint8_t sixel) int width = term->sixel.image.width; - if (term->sixel.pos.col >= width) { - width = max( - term->sixel.image.width, - max(term->sixel.max_col, term->sixel.pos.col + 1)); - - if (!resize(term, width, term->sixel.image.height)) + if (unlikely(term->sixel.pos.col >= width)) { + width = term->sixel.pos.col + 1; + if (unlikely(!resize(term, width, term->sixel.image.height))) return; } @@ -971,14 +966,10 @@ decsixel(struct terminal *term, uint8_t c) break; case '$': - 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.pos.col > term->sixel.max_col) - term->sixel.max_col = term->sixel.pos.col; term->sixel.pos.row += 6; term->sixel.pos.col = 0; term->sixel.row_byte_ofs += term->sixel.image.width * 6; diff --git a/terminal.h b/terminal.h index 2a8bb6f9..81893e34 100644 --- a/terminal.h +++ b/terminal.h @@ -523,7 +523,6 @@ struct terminal { struct coord pos; /* Current sixel coordinate */ size_t row_byte_ofs; /* Byte position into image, for current row */ int color_idx; /* Current palette index */ - int max_col; /* Largest column index we've seen (aka the image width) */ uint32_t *private_palette; /* Private palette, used when private mode 1070 is enabled */ uint32_t *shared_palette; /* Shared palette, used when private mode 1070 is disabled */ uint32_t *palette; /* Points to either private_palette or shared_palette */ -- cgit v1.3 From c181eb2bf68e2e776cff96b36489de5d45e67fe9 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 7 Mar 2021 14:44:16 +0100 Subject: sixel: empty pixels in the last sixel row doesn’t contribute to the image height MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit All-empty pixels rows in the last sixel row should not be included in the final sixel image. This allows applications to emit sixels whose height is not a multiple of 6, and is how XTerm works. This is done by tracking the largest row number that contains non-empty pixels. In unhook, when emitting the image, the image height is adjusted based on this value. --- sixel.c | 18 +++++++++++++++++- terminal.h | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'terminal.h') diff --git a/sixel.c b/sixel.c index 55586df5..6216523a 100644 --- a/sixel.c +++ b/sixel.c @@ -37,6 +37,7 @@ sixel_init(struct terminal *term) term->sixel.state = SIXEL_DECSIXEL; term->sixel.pos = (struct coord){0, 0}; + term->sixel.max_non_empty_row_no = 0; term->sixel.row_byte_ofs = 0; term->sixel.color_idx = 0; term->sixel.param = 0; @@ -695,6 +696,13 @@ sixel_reflow(struct terminal *term) void sixel_unhook(struct terminal *term) { + if (term->sixel.image.height > term->sixel.max_non_empty_row_no + 1) { + LOG_DBG( + "last row only partially filled, reducing image height: %d -> %d", + term->sixel.image.height, term->sixel.max_non_empty_row_no + 1); + term->sixel.image.height = term->sixel.max_non_empty_row_no + 1; + } + int pixel_row_idx = 0; int pixel_rows_left = term->sixel.image.height; const int stride = term->sixel.image.width * sizeof(uint32_t); @@ -846,6 +854,7 @@ resize(struct terminal *term, int new_width, int new_height) if (new_width > term->sixel.max_width || new_height > term->sixel.max_height) { + LOG_WARN("maximum image dimensions reached"); return false; } @@ -921,13 +930,20 @@ sixel_add(struct terminal *term, uint32_t color, uint8_t sixel) size_t ofs = term->sixel.row_byte_ofs + term->sixel.pos.col; uint32_t *data = term->sixel.image.data; + int max_non_empty_row = 0; for (int i = 0; i < 6; i++, sixel >>= 1, ofs += width) { - if (sixel & 1) + if (sixel & 1) { data[ofs] = color; + max_non_empty_row = i; + } } xassert(sixel == 0); term->sixel.pos.col++; + + term->sixel.max_non_empty_row_no = max( + term->sixel.max_non_empty_row_no, + term->sixel.pos.row + max_non_empty_row); } static void diff --git a/terminal.h b/terminal.h index 81893e34..8be5dd74 100644 --- a/terminal.h +++ b/terminal.h @@ -521,6 +521,7 @@ struct terminal { } state; struct coord pos; /* Current sixel coordinate */ + int max_non_empty_row_no; size_t row_byte_ofs; /* Byte position into image, for current row */ int color_idx; /* Current palette index */ uint32_t *private_palette; /* Private palette, used when private mode 1070 is enabled */ -- cgit v1.3