From 1553958eab24860838c1ecdd96abcc1ac16f5556 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 25 Feb 2026 10:48:22 +0530 Subject: [PATCH] Function to print ARGB32 data --- kitty/print-graphics.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/kitty/print-graphics.h b/kitty/print-graphics.h index 06e6a9ec3..2232e1204 100644 --- a/kitty/print-graphics.h +++ b/kitty/print-graphics.h @@ -25,7 +25,33 @@ fprint_rgba32(FILE *fp, const uint32_t *rgba, uint32_t width, uint32_t height) { base64_encode8(data + offset, chunk, b64_buf, &b64_len, false); offset += chunk; const bool last = (offset >= total_bytes); + if (first) { + fprintf(fp, "\x1b_Ga=T,f=32,s=%u,v=%u,m=%d;", width, height, last ? 0 : 1); + first = false; + } else fprintf(fp, "\x1b_Gm=%d;", last ? 0 : 1); + fwrite(b64_buf, 1, b64_len, fp); + fputs("\x1b\\", fp); + } +} +static inline void +fprint_argb32(FILE *fp, const uint32_t *rgba, uint32_t width, uint32_t height) { + const uint8_t *data = (const uint8_t *)rgba; + const size_t total_bytes = (size_t)width * height * sizeof(uint32_t); + uint8_t b64_buf[4096], rgba_buf[3072]; + size_t offset = 0; + bool first = true; + while (offset < total_bytes) { + size_t chunk = total_bytes - offset; + if (chunk > sizeof(rgba_buf)) chunk = sizeof(rgba_buf); + size_t b64_len = sizeof(b64_buf); + for (size_t i = 0; i < chunk; i++) { + uint32_t argb = data[offset + i]; + rgba_buf[i] = ((argb & 0x00FFFFFF) << 8) | ((argb & 0xFF000000) >> 24); + } + base64_encode8(rgba_buf, chunk, b64_buf, &b64_len, false); + offset += chunk; + const bool last = (offset >= total_bytes); if (first) { fprintf(fp, "\x1b_Ga=T,f=32,s=%u,v=%u,m=%d;", width, height, last ? 0 : 1); first = false; @@ -37,3 +63,5 @@ fprint_rgba32(FILE *fp, const uint32_t *rgba, uint32_t width, uint32_t height) { static inline void print_rgba32(const uint32_t *rgba, uint32_t width, uint32_t height) { fprint_rgba32(stdout, rgba, width, height); } +static inline void +print_argb32(const uint32_t *rgba, uint32_t width, uint32_t height) { fprint_argb32(stdout, rgba, width, height); }