From 6ab7052be4932ae52df9aea550c2a8c5d1373f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 7 Mar 2021 11:51:24 +0100 Subject: [PATCH] =?UTF-8?q?sixel:=20set=20=E2=80=98col=E2=80=99=20outside?= =?UTF-8?q?=20image=20boundaries=20when=20we=E2=80=99ve=20reached=20max=20?= =?UTF-8?q?height?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is to optimize sixel_add(). It already checks ‘pos’, to see if it needs to resize the image, and if the resize fails (either due to allocation failures, or because we’ve reached the maximum width), we bail out. We need to do the same thing for height. But, we don’t want to add another check to sixel_add() since its’s performance critical. By setting ‘col’ outside the image boundaries when ‘row’ reaches the maximum image height, we can “re-use” the existing code in sixel_add(). --- sixel.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/sixel.c b/sixel.c index 507caeee..6003783c 100644 --- a/sixel.c +++ b/sixel.c @@ -966,7 +966,15 @@ decsixel(struct terminal *term, uint8_t c) break; case '$': - term->sixel.pos.col = 0; + if (likely(term->sixel.pos.col <= term->sixel.max_width)) { + /* + * We set, and keep, ‘col’ outside the image boundary when + * we’ve reached the maximum image height, to avoid also + * having to check the row vs image height in the common + * path in sixel_add(). + */ + term->sixel.pos.col = 0; + } break; case '-': @@ -974,8 +982,10 @@ decsixel(struct terminal *term, uint8_t c) term->sixel.pos.col = 0; term->sixel.row_byte_ofs += term->sixel.image.width * 6; - if (term->sixel.pos.row >= term->sixel.image.height) - resize(term, term->sixel.image.width, term->sixel.pos.row + 6); + if (term->sixel.pos.row >= term->sixel.image.height) { + if (!resize(term, term->sixel.image.width, term->sixel.pos.row + 6)) + term->sixel.pos.col = term->sixel.max_width + 1; + } break; case '?': case '@': case 'A': case 'B': case 'C': case 'D': case 'E':