ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
media/kitty/image.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define KITTY_BASE64_CHUNK_SIZE 4096
4#define KITTY_RAW_CHUNK_SIZE (KITTY_BASE64_CHUNK_SIZE / 4 * 3)
5#define KITTY_FRAMING_BUFFER_SIZE 128
6#define MEDIA_KITTY_IMAGE_PACKET_BUFFER_SIZE (KITTY_BASE64_CHUNK_SIZE + KITTY_FRAMING_BUFFER_SIZE)
7
11#include <limits.h>
12#include <stddef.h>
13
14asciichat_error_t kitty_image_write(FILE *stream, const uint8_t *rgba, int width, int height, int pitch) {
15 if (!stream || !rgba || width <= 0 || height <= 0 || width > INT_MAX / 4 || pitch < width * 4 || (size_t)height > SIZE_MAX / (size_t)pitch)
16 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid Kitty image dimensions or pixels");
17
18 // 3072 raw bytes produce the protocol maximum of 4096 base64 bytes.
19 static const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
20 const size_t row_bytes = (size_t)width * 4;
21 const size_t total = row_bytes * (size_t)height;
22 size_t offset = 0;
23 bool first = true;
24 while (offset < total) {
26 size_t count = total - offset;
27 if (count > KITTY_RAW_CHUNK_SIZE)
29 bool more = offset + count < total;
30 int prefix = first ? snprintf(packet, sizeof(packet), "\033_Ga=T,f=32,s=%d,v=%d,t=d,q=2,m=%d;", width, height, more) : snprintf(packet, sizeof(packet), "\033_Gm=%d;", more);
31 size_t used = (size_t)prefix;
32 for (size_t i = 0; i < count; i += 3) {
33 uint32_t triple = 0;
34 size_t available = count - i < 3 ? count - i : 3;
35 for (size_t j = 0; j < available; ++j) {
36 size_t index = offset + i + j;
37 triple |= (uint32_t)rgba[(index / row_bytes) * (size_t)pitch + index % row_bytes] << (16 - 8 * j);
38 }
39 packet[used++] = alphabet[(triple >> 18) & 63];
40 packet[used++] = alphabet[(triple >> 12) & 63];
41 packet[used++] = available > 1 ? alphabet[(triple >> 6) & 63] : '=';
42 packet[used++] = available > 2 ? alphabet[triple & 63] : '=';
43 }
44 packet[used++] = '\033';
45 packet[used++] = '\\';
47 asciichat_error_t err = stdout_writer_write(stream, packet, used);
49 if (err != ASCIICHAT_OK)
50 return err;
51 offset += count;
52 first = false;
53 }
54 return ASCIICHAT_OK;
55}
#define SET_ERRNO(code,...)
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
unsigned int uint32_t
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define KITTY_RAW_CHUNK_SIZE
asciichat_error_t kitty_image_write(FILE *stream, const uint8_t *rgba, int width, int height, int pitch)
#define MEDIA_KITTY_IMAGE_PACKET_BUFFER_SIZE
static bool size_t index
Definition queries.h:10
asciichat_error_t stdout_writer_write(FILE *stream, const void *data, size_t size)
void ui_controller_packet_begin(void)
void ui_controller_packet_end(void)