Merge pull request #168 from bptato/inlineimages

Improved iTerm2 image display + initial kitty image support
This commit is contained in:
Tatsuya Kinoshita
2021-02-21 12:02:26 +09:00
committed by GitHub
5 changed files with 191 additions and 14 deletions
+8 -2
View File
@@ -2013,9 +2013,13 @@ 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;
if (!len)
return NULL;
k = len;
if (k % 3)
k += 3 - (k % 3);
@@ -2024,7 +2028,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;
+1
View File
@@ -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.
+4
View File
@@ -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, i->width, i->height, i->sx, i->sy, sw * pixel_per_char, sh * pixel_per_line_i, sw, sh);
}
continue ;
+1
View File
@@ -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 (ImageMagick)")},
{0, NULL, NULL}
};
#endif /* USE_IMAGE */
+177 -12
View File
@@ -6,6 +6,7 @@
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
@@ -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;
if (stat(url, &st))
return;
fp = fopen(url, "r");
if (!fp)
return;
filecontent = Strfgetall(fp);
base64 = base64_encode(filecontent->ptr, filecontent->length);
if (!base64)
return;
MOVE(y,x);
buf = Sprintf("\x1b]1337;"
"File="
"name=%s;"
@@ -511,11 +511,179 @@ 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;
++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);
}
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, *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;
fp = fopen(url, "r");
if (!fp)
return;
MOVE(y, x);
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, sx, sy, sw, sh, cols, rows);
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;
if (c == EOF)
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, sx, sy, sw, sh, cols, rows);
writestr(buf->ptr);
buf = Sprintf("%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);
}
static void
save_gif(const char *path, u_char *header, size_t header_size, u_char *body, size_t body_size)
{
@@ -606,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)
{