Function to print ARGB32 data

This commit is contained in:
Kovid Goyal 2026-02-25 10:48:22 +05:30
parent a823f72e0e
commit 1553958eab
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C

View file

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