From de26f6156c2511df39981e2269672294e585ef8a Mon Sep 17 00:00:00 2001 From: bptato Date: Thu, 18 Feb 2021 17:23:42 +0100 Subject: Handle iTerm2 images more efficiently --- etc.c | 6 ++++-- terms.c | 49 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/etc.c b/etc.c index 4e662e4..2870caf 100644 --- a/etc.c +++ b/etc.c @@ -2013,7 +2013,7 @@ base64_encode(const unsigned char *src, size_t len) { unsigned char *w, *at; const unsigned char *in, *endw; - int j; + unsigned long j; size_t k; k = len; @@ -2024,7 +2024,9 @@ base64_encode(const unsigned char *src, size_t len) if (k + 1 < len) return NULL; - w = GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(k + 1); + w = GC_MALLOC_ATOMIC(k + 1); + if (!w) + return NULL; w[k] = 0; at = w; diff --git a/terms.c b/terms.c index 966006a..f7c300e 100644 --- a/terms.c +++ b/terms.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -489,20 +490,19 @@ put_image_osc5379(char *url, int x, int y, int w, int h, int sx, int sy, int sw, void put_image_iterm2(char *url, int x, int y, int w, int h) { - Str buf, filecontent; - const char *base64; + Str buf; + char *base64, *cbuf; FILE *fp; + int c, i; + struct stat st; - fp = fopen(url, "r"); - if (!fp) + if (stat(url, &st)) return; - filecontent = Strfgetall(fp); - base64 = base64_encode(filecontent->ptr, filecontent->length); - if (!base64) + fp = fopen(url, "r"); + if (!fp) return; - MOVE(y,x); buf = Sprintf("\x1b]1337;" "File=" "name=%s;" @@ -511,8 +511,39 @@ put_image_iterm2(char *url, int x, int y, int w, int h) "height=%d;" "preserveAspectRatio=0;" "inline=1" - ":%s\a", url, filecontent->length, w, h, base64); + ":", url, st.st_size, w, h); + + MOVE(y,x); + writestr(buf->ptr); + + cbuf = GC_MALLOC_ATOMIC(3072); + i = 0; + while ((c = fgetc(fp)) != EOF) { + cbuf[i] = c; + Strcat_char(buf, c); + ++i; + if (i == 3072) { + base64 = base64_encode(cbuf, i); + if (!base64) { + writestr("\a"); + return; + } + writestr(base64); + i = 0; + } + } + + if (i) { + base64 = base64_encode(cbuf, i); + if (!base64) { + writestr("\a"); + return; + } + writestr(base64); + } + + writestr("\a"); MOVE(Currentbuf->cursorY,Currentbuf->cursorX); } -- cgit v1.2.3 From 3304675affd22e9ede2c92b5c0085961a797485e Mon Sep 17 00:00:00 2001 From: bptato Date: Thu, 18 Feb 2021 19:18:06 +0100 Subject: Support kitty image protocol --- etc.c | 4 ++++ fm.h | 1 + image.c | 4 ++++ rc.c | 1 + terms.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 89 insertions(+), 1 deletion(-) diff --git a/etc.c b/etc.c index 2870caf..bc72005 100644 --- a/etc.c +++ b/etc.c @@ -2016,6 +2016,10 @@ base64_encode(const unsigned char *src, size_t len) unsigned long j; size_t k; + + if (!len) + return NULL; + k = len; if (k % 3) k += 3 - (k % 3); diff --git a/fm.h b/fm.h index c4a951f..997574a 100644 --- a/fm.h +++ b/fm.h @@ -316,6 +316,7 @@ extern int REV_LB[]; #define INLINE_IMG_OSC5379 1 #define INLINE_IMG_SIXEL 2 #define INLINE_IMG_ITERM2 3 +#define INLINE_IMG_KITTY 4 /* * Types. diff --git a/image.c b/image.c index 6e4e9b4..f25c895 100644 --- a/image.c +++ b/image.c @@ -198,6 +198,8 @@ syncImage(void) void put_image_osc5379(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh); void put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh, int n_terminal_image); void put_image_iterm2(char *url, int x, int y, int w, int h); +void put_image_kitty(char *url, int x, int y, int w, int h, int sx, int sy, int + sw, int sh, int c, int r); void drawImage() @@ -256,6 +258,8 @@ drawImage() put_image_osc5379(url, x, y, w, h, sx, sy, sw, sh); } else if (enable_inline_image == INLINE_IMG_ITERM2) { put_image_iterm2(url, x, y, sw, sh); + } else if (enable_inline_image == INLINE_IMG_KITTY) { + put_image_kitty(url, x, y, w, h, i->sx, i->sy, sw * pixel_per_char, sh * pixel_per_line_i, sw, sh); } continue ; diff --git a/rc.c b/rc.c index 3c72ffb..18693a5 100644 --- a/rc.c +++ b/rc.c @@ -374,6 +374,7 @@ static struct sel_c inlineimgstr[] = { {N_S(INLINE_IMG_OSC5379), N_("OSC 5379 (mlterm)")}, {N_S(INLINE_IMG_SIXEL), N_("sixel (img2sixel)")}, {N_S(INLINE_IMG_ITERM2), N_("OSC 1337 (iTerm2)")}, + {N_S(INLINE_IMG_KITTY), N_("kitty (PNG only)")}, {0, NULL, NULL} }; #endif /* USE_IMAGE */ diff --git a/terms.c b/terms.c index f7c300e..45c5f86 100644 --- a/terms.c +++ b/terms.c @@ -521,7 +521,6 @@ put_image_iterm2(char *url, int x, int y, int w, int h) i = 0; while ((c = fgetc(fp)) != EOF) { cbuf[i] = c; - Strcat_char(buf, c); ++i; if (i == 3072) { base64 = base64_encode(cbuf, i); @@ -547,6 +546,85 @@ put_image_iterm2(char *url, int x, int y, int w, int h) MOVE(Currentbuf->cursorY,Currentbuf->cursorX); } +void +put_image_kitty(char *url, int x, int y, int w, int h, int sx, int sy, int sw, + int sh, int cols, int rows) +{ + Str buf; + char *base64, *cbuf, *type; + FILE *fp; + int c, i, j, k, t; + struct stat st; + + if (stat(url, &st)) + return; + + fp = fopen(url, "r"); + if (!fp) + return; + + type = guessContentType(url); + if(!strcasecmp(type, "image/png")) { + t = 100; + } else { + /* TODO: kitty +kitten icat or link imlib/gdkpixbuf? */ + return; + } + + MOVE(y, x); + buf = Sprintf("\x1b_Gf=100,s=%d,v=%d,a=T,m=1,X=%d,Y=%d,x=%d,y=%d," + "w=%d,h=%d,c=%d,r=%d;", + w, h, x, y, sx, sy, sw, sh, cols, rows); + + writestr(buf->ptr); + + cbuf = GC_MALLOC_ATOMIC(3072); + i = 0; + j = buf->length; + + while (buf->length + i / 3 * 4 < 4096 && (c = fgetc(fp)) != EOF) { + cbuf[i] = c; + ++i; + } + + base64 = base64_encode(cbuf, i); + if (!base64) + return; + + buf = Sprintf("%s\x1b\\", base64); + writestr(buf->ptr); + i = 0; + base64 = NULL; + + while ((c = fgetc(fp)) != EOF) { + if (!i && base64) { + buf = Sprintf("\x1b_Gm=1;%s\x1b\\", base64); + writestr(buf->ptr); + } + cbuf[i] = c; + ++i; + if (i == 3072) { + base64 = base64_encode(cbuf, i); + if (!base64) + return; + + i = 0; + } + } + + if (i) { + base64 = base64_encode(cbuf, i); + if (!base64) + return; + } + + if (base64) { + buf = Sprintf("\x1b_Gm=0;%s\x1b\\", base64); + writestr(buf->ptr); + } + MOVE(Currentbuf->cursorY, Currentbuf->cursorX); +} + static void save_gif(const char *path, u_char *header, size_t header_size, u_char *body, size_t body_size) { -- cgit v1.2.3 From da628ebd885da1ae921ab1b254cd3253a87ea396 Mon Sep 17 00:00:00 2001 From: bptato Date: Thu, 18 Feb 2021 20:27:48 +0100 Subject: Fix small images on kitty --- terms.c | 63 +++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/terms.c b/terms.c index 45c5f86..3c42275 100644 --- a/terms.c +++ b/terms.c @@ -576,7 +576,6 @@ put_image_kitty(char *url, int x, int y, int w, int h, int sx, int sy, int sw, "w=%d,h=%d,c=%d,r=%d;", w, h, x, y, sx, sy, sw, sh, cols, rows); - writestr(buf->ptr); cbuf = GC_MALLOC_ATOMIC(3072); i = 0; @@ -591,36 +590,44 @@ put_image_kitty(char *url, int x, int y, int w, int h, int sx, int sy, int sw, if (!base64) return; - buf = Sprintf("%s\x1b\\", base64); + if (c == EOF) + buf = Sprintf("\x1b_Gf=100,s=%d,v=%d,a=T,m=0,X=%d,Y=%d,x=%d,y=%d," + "w=%d,h=%d,c=%d,r=%d;", + w, h, x, y, sx, sy, sw, sh, cols, rows); writestr(buf->ptr); - i = 0; - base64 = NULL; - while ((c = fgetc(fp)) != EOF) { - if (!i && base64) { - buf = Sprintf("\x1b_Gm=1;%s\x1b\\", base64); - writestr(buf->ptr); - } - cbuf[i] = c; - ++i; - if (i == 3072) { - base64 = base64_encode(cbuf, i); - if (!base64) - return; - - i = 0; - } - } - - if (i) { - base64 = base64_encode(cbuf, i); - if (!base64) - return; - } + buf = Sprintf("%s\x1b\\", base64); + writestr(buf->ptr); - if (base64) { - buf = Sprintf("\x1b_Gm=0;%s\x1b\\", base64); - writestr(buf->ptr); + if (c != EOF) { + i = 0; + base64 = NULL; + while ((c = fgetc(fp)) != EOF) { + if (!i && base64) { + buf = Sprintf("\x1b_Gm=1;%s\x1b\\", base64); + writestr(buf->ptr); + } + cbuf[i] = c; + ++i; + if (i == 3072) { + base64 = base64_encode(cbuf, i); + if (!base64) + return; + + i = 0; + } + } + + if (i) { + base64 = base64_encode(cbuf, i); + if (!base64) + return; + } + + if (base64) { + buf = Sprintf("\x1b_Gm=0;%s\x1b\\", base64); + writestr(buf->ptr); + } } MOVE(Currentbuf->cursorY, Currentbuf->cursorX); } -- cgit v1.2.3 From 728fb34e287533b406885434b0e2799dfc1d6d6a Mon Sep 17 00:00:00 2001 From: bptato Date: Thu, 18 Feb 2021 20:37:06 +0100 Subject: Fix potential segfault --- terms.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terms.c b/terms.c index 3c42275..04a1bcb 100644 --- a/terms.c +++ b/terms.c @@ -564,7 +564,7 @@ put_image_kitty(char *url, int x, int y, int w, int h, int sx, int sy, int sw, return; type = guessContentType(url); - if(!strcasecmp(type, "image/png")) { + if(type && !strcasecmp(type, "image/png")) { t = 100; } else { /* TODO: kitty +kitten icat or link imlib/gdkpixbuf? */ -- cgit v1.2.3 From 081f42e35c793bc79e95628e15dbe9147637c45e Mon Sep 17 00:00:00 2001 From: bptato Date: Fri, 19 Feb 2021 23:46:07 +0100 Subject: Convert images to PNG for kitty with ImageMagick --- image.c | 2 +- rc.c | 2 +- terms.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 67 insertions(+), 18 deletions(-) diff --git a/image.c b/image.c index f25c895..a84d974 100644 --- a/image.c +++ b/image.c @@ -259,7 +259,7 @@ drawImage() } else if (enable_inline_image == INLINE_IMG_ITERM2) { put_image_iterm2(url, x, y, sw, sh); } else if (enable_inline_image == INLINE_IMG_KITTY) { - put_image_kitty(url, x, y, w, h, i->sx, i->sy, sw * pixel_per_char, sh * pixel_per_line_i, sw, sh); + put_image_kitty(url, x, y, i->width, i->height, i->sx, i->sy, sw * pixel_per_char, sh * pixel_per_line_i, sw, sh); } continue ; diff --git a/rc.c b/rc.c index 18693a5..308bd55 100644 --- a/rc.c +++ b/rc.c @@ -374,7 +374,7 @@ static struct sel_c inlineimgstr[] = { {N_S(INLINE_IMG_OSC5379), N_("OSC 5379 (mlterm)")}, {N_S(INLINE_IMG_SIXEL), N_("sixel (img2sixel)")}, {N_S(INLINE_IMG_ITERM2), N_("OSC 1337 (iTerm2)")}, - {N_S(INLINE_IMG_KITTY), N_("kitty (PNG only)")}, + {N_S(INLINE_IMG_KITTY), N_("kitty (ImageMagick)")}, {0, NULL, NULL} }; #endif /* USE_IMAGE */ diff --git a/terms.c b/terms.c index 04a1bcb..37d23bb 100644 --- a/terms.c +++ b/terms.c @@ -546,15 +546,75 @@ put_image_iterm2(char *url, int x, int y, int w, int h) MOVE(Currentbuf->cursorY,Currentbuf->cursorX); } +void ttymode_set(int mode, int imode); +void ttymode_reset(int mode, int imode); + void put_image_kitty(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh, int cols, int rows) { Str buf; - char *base64, *cbuf, *type; + char *base64, *cbuf, *type, *tmpf; + char *argv[4]; FILE *fp; int c, i, j, k, t; struct stat st; + pid_t pid; + MySignalHandler(*volatile previntr) (SIGNAL_ARG); + MySignalHandler(*volatile prevquit) (SIGNAL_ARG); + MySignalHandler(*volatile prevstop) (SIGNAL_ARG); + + if (!url) + return; + + type = guessContentType(url); + t = 100; /* always convert to png for now. */ + + if(!(type && !strcasecmp(type, "image/png"))) { + buf = Strnew(); + tmpf = Sprintf("%s/%s.png", tmp_dir, mybasename(url))->ptr; + + /* convert only if png doesn't exist yet. */ + + if (stat(tmpf, &st)) { + if (stat(url, &st)) + return; + + flush_tty(); + + previntr = mySignal(SIGINT, SIG_IGN); + prevquit = mySignal(SIGQUIT, SIG_IGN); + prevstop = mySignal(SIGTSTP, SIG_IGN); + + if ((pid = fork()) == 0) { + i = 0; + + close(STDERR_FILENO); /* Don't output error message. */ + ttymode_set(ISIG, 0); + + if ((cbuf = getenv("W3M_KITTY_TO_PNG"))) + argv[i++] = cbuf; + else + argv[i++] = "convert"; + + argv[i++] = url; + argv[i++] = tmpf; + argv[i++] = NULL; + execvp(argv[0],argv); + exit(0); + } + else if (pid > 0) { + waitpid(pid, &i, 0); + ttymode_reset(ISIG, 0); + mySignal(SIGINT, previntr); + mySignal(SIGQUIT, prevquit); + mySignal(SIGTSTP, prevstop); + } + + pushText(fileToDelete, tmpf); + } + url = tmpf; + } if (stat(url, &st)) return; @@ -563,18 +623,10 @@ put_image_kitty(char *url, int x, int y, int w, int h, int sx, int sy, int sw, if (!fp) return; - type = guessContentType(url); - if(type && !strcasecmp(type, "image/png")) { - t = 100; - } else { - /* TODO: kitty +kitten icat or link imlib/gdkpixbuf? */ - return; - } - MOVE(y, x); - buf = Sprintf("\x1b_Gf=100,s=%d,v=%d,a=T,m=1,X=%d,Y=%d,x=%d,y=%d," + buf = Sprintf("\x1b_Gf=100,s=%d,v=%d,a=T,m=1,x=%d,y=%d," "w=%d,h=%d,c=%d,r=%d;", - w, h, x, y, sx, sy, sw, sh, cols, rows); + w, h, sx, sy, sw, sh, cols, rows); cbuf = GC_MALLOC_ATOMIC(3072); @@ -591,9 +643,9 @@ put_image_kitty(char *url, int x, int y, int w, int h, int sx, int sy, int sw, return; if (c == EOF) - buf = Sprintf("\x1b_Gf=100,s=%d,v=%d,a=T,m=0,X=%d,Y=%d,x=%d,y=%d," + buf = Sprintf("\x1b_Gf=100,s=%d,v=%d,a=T,m=0,x=%d,y=%d," "w=%d,h=%d,c=%d,r=%d;", - w, h, x, y, sx, sy, sw, sh, cols, rows); + w, h, sx, sy, sw, sh, cols, rows); writestr(buf->ptr); buf = Sprintf("%s\x1b\\", base64); @@ -722,9 +774,6 @@ save_first_animation_frame(const char *path) return NULL; } -void ttymode_set(int mode, int imode); -void ttymode_reset(int mode, int imode); - void put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh, int n_terminal_image) { -- cgit v1.2.3