Convert images to PNG for kitty with ImageMagick

This commit is contained in:
bptato
2021-02-19 23:46:07 +01:00
parent 728fb34e28
commit 081f42e35c
3 changed files with 67 additions and 18 deletions

View File

@@ -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 ;

2
rc.c
View File

@@ -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 */

81
terms.c
View File

@@ -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)
{