From 0b89c8943f3660787661b13d02e79c492612a177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 6 Mar 2021 15:55:35 +0100 Subject: [PATCH] sixel: use Set Raster Attributes to configure image size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This lets the terminal allocate the image once, instead of dynamically resizing the image as the sixel data is emitted. Note that this isn’t limiting the sixel size - if the sixel ends up being larger than advertised here, the terminal is required to resize the image to fit the entire sixel (i.e. you cannot use this to crop the image to something that isn’t a multiple of 6). --- src/lib/sixel.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lib/sixel.c b/src/lib/sixel.c index 54c32e59b..d95906720 100644 --- a/src/lib/sixel.c +++ b/src/lib/sixel.c @@ -161,8 +161,12 @@ write_rle(FILE* fp, int seenrle, unsigned char crle){ // Emit the sprixel in its entirety, plus enable and disable pixel mode. static int -write_sixel_data(FILE* fp, int lenx, sixeltable* stab){ +write_sixel_data(FILE* fp, int leny, int lenx, sixeltable* stab){ fprintf(fp, "\e[?80h\ePq"); // FIXME pixelon + + // Set Raster Attributes - pan/pad=1 (pixel aspect ratio), Ph=lenx, Pv=leny + fprintf(fp, "\"1;1;%d;%d", lenx, leny); + for(int i = 0 ; i < stab->ctab->colors ; ++i){ const unsigned char* rgb = stab->ctab->table + i * 5; fprintf(fp, "#%d;2;%u;%u;%u", i, rgb[0], rgb[1], rgb[2]); @@ -212,7 +216,7 @@ write_sixel_data(FILE* fp, int lenx, sixeltable* stab){ // are programmed as a set of registers, which are then referenced by the // stacks. There is also a RLE component, handled in rasterization. // A pixel block is indicated by setting cell_pixels_p(). -int sixel_blit_inner(ncplane* nc, int placey, int placex, int lenx, +int sixel_blit_inner(ncplane* nc, int placey, int placex, int leny, int lenx, sixeltable* stab, unsigned cellpixx){ char* buf = NULL; size_t size = 0; @@ -220,7 +224,7 @@ int sixel_blit_inner(ncplane* nc, int placey, int placex, int lenx, if(fp == NULL){ return -1; } - if(write_sixel_data(fp, lenx, stab)){ + if(write_sixel_data(fp, leny, lenx, stab)){ fclose(fp); free(buf); return -1; @@ -257,7 +261,7 @@ int sixel_blit(ncplane* nc, int placey, int placex, int linesize, free(stable.data); return -1; } - int r = sixel_blit_inner(nc, placey, placex, lenx, &stable, cellpixx); + int r = sixel_blit_inner(nc, placey, placex, leny, lenx, &stable, cellpixx); free(stable.data); free(ctab); return r;