0
0
mirror of https://github.com/rkd77/elinks.git synced 2025-06-30 22:19:29 -04:00

[kitty] Try to compress images

This commit is contained in:
Witold Filipczyk 2025-06-09 19:37:34 +02:00
parent 8f02de86e8
commit cfdc86fe2e
4 changed files with 34 additions and 5 deletions

1
src/cache/cache.h vendored
View File

@ -82,6 +82,7 @@ struct cache_entry {
unsigned int sixel:1; /* Is in sixel format? */
unsigned int kitty:1; /* Is in kitty base64? */
unsigned int compressed:1; /* Is kitty zlib? */
cache_mode_T cache_mode; /* Reload condition */
};

View File

@ -254,6 +254,7 @@ html_img_kitty(struct html_context *html_context, char *a,
int width = 0;
int height = 0;
int im_number = 0;
int compressed = 0;
char *url = get_attr_val(a, "src", html_context->doc_cp);
if (!url) {
@ -277,8 +278,9 @@ html_img_kitty(struct html_context *html_context, char *a,
width = cached->width;
height = cached->height;
im_number = cached->number;
compressed = cached->compressed;
} else {
data = el_kitty_get_image(fragment->data, fragment->length, &datalen, &width, &height);
data = el_kitty_get_image(fragment->data, fragment->length, &datalen, &width, &height, &compressed);
if (data) {
(void)add_fragment(cached, 0, (const char *)data, datalen);
@ -287,6 +289,7 @@ html_img_kitty(struct html_context *html_context, char *a,
cached->width = width;
cached->height = height;
cached->number = im_number = ++kitty_image_number;
cached->compressed = compressed;
}
}
}
@ -322,6 +325,7 @@ html_img_kitty(struct html_context *html_context, char *a,
int y;
im->number = html_top->name - document->text.source;
im->ID = im_number;
im->compressed = compressed;
for (y = 0; y < how_many; y++) {
int x;

View File

@ -10,6 +10,10 @@
#include <fcntl.h>
#include <sys/stat.h>
#ifdef CONFIG_GZIP
#include <zlib.h>
#endif
#include "elinks.h"
#include "document/document.h"
@ -20,13 +24,13 @@
#include "terminal/screen.h"
#include "terminal/terminal.h"
#include "util/base64.h"
#include "util/memcount.h"
#include "util/memory.h"
#define STB_IMAGE_IMPLEMENTATION
#include "terminal/stb_image.h"
unsigned char *
el_kitty_get_image(char *data, int length, int *outlen, int *width, int *height)
el_kitty_get_image(char *data, int length, int *outlen, int *width, int *height, int *compressed)
{
ELOG
int comp;
@ -37,7 +41,25 @@ el_kitty_get_image(char *data, int length, int *outlen, int *width, int *height)
return NULL;
}
int size = *width * *height * 4;
*compressed = 0;
#ifdef CONFIG_GZIP
unsigned char *complace = (unsigned char *)mem_alloc(size);
if (complace) {
unsigned long compsize = size;
int res = compress(complace, &compsize, pixels, size);
if (res == Z_OK) {
*compressed = 1;
b64 = base64_encode_bin(complace, compsize, outlen);
stbi_image_free(pixels);
mem_free(complace);
return b64;
}
mem_free(complace);
}
#endif
b64 = base64_encode_bin(pixels, size, outlen);
stbi_image_free(pixels);
@ -127,7 +149,7 @@ try_to_draw_k_images(struct terminal *term)
int sent = 0;
while (1) {
m = left >= 4000;
add_format_to_string(&text, "\033_Gf=32,I=%d,s=%d,v=%d,m=%d,t=d,a=T;", im->ID, im->width, im->height, m);
add_format_to_string(&text, "\033_Gf=32,I=%d,s=%d,v=%d,m=%d,t=d,a=T%s;", im->ID, im->width, im->height, m, (im->compressed ? ",o=z": ""));
add_bytes_to_string(&text, im->pixels.source + sent, m ? 4000 : left);
add_to_string(&text, "\033\\");
if (!m) {
@ -200,6 +222,7 @@ copy_k_frame(struct k_image *src, struct el_box *box, int cell_width, int cell_h
dest->ID = src->ID;
dest->number = src->number;
dest->sent = src->sent;
dest->compressed = src->compressed;
return dest;
}

View File

@ -28,6 +28,7 @@ struct k_image {
int w;
int h;
unsigned int sent:1;
unsigned int compressed:1;
};
void delete_k_image(struct k_image *im);
@ -39,7 +40,7 @@ int add_kitty_image_to_document(struct document *doc, const char *data, int data
struct k_image *copy_k_frame(struct k_image *src, struct el_box *box, int cell_width, int cell_height, int dx, int dy);
unsigned char *el_kitty_get_image(char *data, int len, int *outlen, int *width, int *height);
unsigned char *el_kitty_get_image(char *data, int len, int *outlen, int *width, int *height, int *compressed);
#endif