ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
video/ascii/render/common.c
Go to the documentation of this file.
1#include <ascii-chat/otel.h>
2#include <ascii-chat/common.h>
8#include <string.h>
9#include <limits.h>
10
11#define VIDEO_ASCII_RENDER_SEQUENCE_BUFFER_SIZE 16
12
13static _Thread_local outbuf_t *g_output_target;
14
16 outbuf_t *previous = g_output_target;
17 g_output_target = target;
18 return previous;
19}
20
21static void prepare_output(ascii_frame_t *frame, size_t capacity, outbuf_t *target) {
22 frame->output_target = target;
23 if (target) {
24 if (target->cap < capacity) {
25 target->buf = SAFE_REALLOC(target->buf, capacity, char *);
26 target->cap = capacity;
27 }
28 frame->output = *target;
29 frame->output.len = 0;
30 } else {
31 frame->output.cap = capacity;
32 frame->output.buf = SAFE_MALLOC(capacity, char *);
33 }
34}
35
36static void emit_color(outbuf_t *out, ascii_color_t mode, bool background, int value) {
37 if (mode == ASCII_COLOR_TRUE) {
38 if (background)
39 emit_set_truecolor_bg(out, (uint8_t)(value >> 16), (uint8_t)(value >> 8), (uint8_t)value);
40 else
41 emit_set_truecolor_fg(out, (uint8_t)(value >> 16), (uint8_t)(value >> 8), (uint8_t)value);
42 } else if (mode == ASCII_COLOR_256) {
43 if (background)
45 else
47 } else if (mode == ASCII_COLOR_16) {
49 char *end = background ? append_16color_bg(sequence, (uint8_t)value) : append_16color_fg(sequence, (uint8_t)value);
50 ob_write(out, sequence, (size_t)(end - sequence));
51 }
52}
53
54static size_t glyph_bytes(uint32_t key) {
55 uint8_t first;
56 memcpy(&first, &key, sizeof(first));
57 return first < 128 ? 1 : (first & 0xe0) == 0xc0 ? 2 : (first & 0xf0) == 0xe0 ? 3 : 4;
58}
59
60void ascii_emit_prepared_row(outbuf_t *out, ascii_color_t mode, const simd_cells_t *ops, const uint32_t *glyph, const uint32_t *fg, const uint32_t *bg, size_t width) {
61 if (mode == ASCII_COLOR_NONE) {
62 if (ops->emit_mono) {
63 ops->emit_mono(out, glyph, width);
64 } else {
65 for (size_t x = 0; x < width;) {
66 size_t run = ops->run(glyph + x, fg + x, bg + x, width - x);
67 emit_glyph_run(out, &glyph[x], glyph_bytes(glyph[x]), (uint32_t)run);
68 x += run;
69 }
70 }
71 return;
72 }
73 uint32_t current_fg = UINT32_MAX, current_bg = UINT32_MAX;
74 for (size_t x = 0; x < width;) {
75 size_t run = ops->run(glyph + x, fg + x, bg + x, width - x);
76 if (fg[x] == UINT32_MAX && bg[x] == UINT32_MAX && (current_fg != UINT32_MAX || current_bg != UINT32_MAX)) {
78 current_fg = current_bg = UINT32_MAX;
79 }
80 if (fg[x] != current_fg) {
81 emit_color(out, mode, false, (int)fg[x]);
82 current_fg = fg[x];
83 }
84 if (bg[x] != current_bg) {
85 emit_color(out, mode, true, (int)bg[x]);
86 current_bg = bg[x];
87 }
88 emit_glyph_run(out, &glyph[x], glyph_bytes(glyph[x]), (uint32_t)run);
89 x += run;
90 }
91}
92
93asciichat_error_t ascii_frame_init(ascii_frame_t *frame, const image_t *image, const char *palette) {
94 ASCII_OTEL_SCOPE(ASCII_FRAME_INIT);
96 memset(frame, 0, sizeof(*frame));
97 if (!image || !image->pixels || image->w <= 0 || image->h <= 0 || !palette || !palette[0])
98 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid image or palette");
99 size_t bytes, pixels, capacity, rows;
100 if (checked_size_mul((size_t)image->w, (size_t)image->h, &pixels) != ASCIICHAT_OK || checked_size_mul(pixels, 48u, &capacity) != ASCIICHAT_OK ||
101 checked_size_mul((size_t)image->h, 5u, &rows) != ASCIICHAT_OK || checked_size_add(capacity, rows, &capacity) != ASCIICHAT_OK || checked_size_add(capacity, 1u, &capacity) != ASCIICHAT_OK ||
102 checked_size_mul((size_t)image->w, 6 * sizeof(uint32_t), &bytes) != ASCIICHAT_OK)
103 return ERROR_INVALID_PARAM;
105 if (!cache)
106 return ERROR_INVALID_PARAM;
107 prepare_output(frame, capacity, target);
108 frame->width = (size_t)image->w;
111 for (size_t i = 0; i < 64; ++i)
112 memcpy(frame->table + i, cache->cache64[i].utf8_bytes, (size_t)cache->cache64[i].byte_len);
113 frame->scratch = bytes <= sizeof(frame->inline_scratch) ? frame->inline_scratch : SAFE_MALLOC(bytes, uint32_t *);
114 frame->packed = frame->scratch;
115 frame->index = frame->packed + frame->width;
116 frame->glyph = frame->index + frame->width;
117 frame->fg = frame->glyph + frame->width;
118 frame->bg = frame->fg + frame->width;
119 frame->luma = (uint8_t *)(frame->bg + frame->width);
120 return ASCIICHAT_OK;
121}
123 if (mode == ASCII_COLOR_16) {
124 uint8_t r, g, b;
125 get_16color_rgb((uint8_t)color, &r, &g, &b);
126 return (77 * r + 150 * g + 29 * b) / 256 < 127 ? 15u : 0;
127 }
128 return luma < 128 ? (mode == ASCII_COLOR_TRUE ? 0xffffffu : 15u) : 0;
129}
130
131void ascii_frame_emit_row(ascii_frame_t *frame, ascii_color_t mode, bool background, bool more_rows, const simd_cells_t *ops) {
132 ascii_stabilizer_frame_row(frame, mode, background, ops);
133 for (size_t x = 0; x < frame->width; ++x) {
134 frame->fg[x] = frame->bg[x] = UINT32_MAX;
135 if (mode == ASCII_COLOR_NONE)
136 continue;
137 uint32_t value = mode == ASCII_COLOR_TRUE ? frame->packed[x] : frame->index[x];
138 uint32_t contrast = ascii_background_contrast(mode, value, frame->luma[x]);
139 frame->fg[x] = background ? contrast : value;
140 frame->bg[x] = background ? value : UINT32_MAX;
141 }
142 ascii_emit_prepared_row(&frame->output, mode, ops, frame->glyph, frame->fg, frame->bg, frame->width);
143 emit_row_end(&frame->output, mode != ASCII_COLOR_NONE, more_rows);
144 ++frame->row;
145}
147 if (frame->scratch != frame->inline_scratch)
148 SAFE_FREE(frame->scratch);
149 ob_term(&frame->output);
150 if (frame->output_target)
151 *frame->output_target = frame->output;
152 return frame->output.buf;
153}
154
156 ASCII_OTEL_SCOPE(ASCII_HALF_FRAME_INIT);
158 memset(frame, 0, sizeof(*frame));
159 size_t row_bytes, extent, bytes, capacity;
160 if (!rgb || width <= 0 || height <= 0 || mode < ASCII_COLOR_NONE || mode > ASCII_COLOR_TRUE || checked_size_mul((size_t)width, 3, &row_bytes) != ASCIICHAT_OK || row_bytes > INT_MAX)
161 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid half-block input");
162 if (*stride <= 0)
163 *stride = (int)row_bytes;
164 if ((size_t)*stride < row_bytes || checked_size_mul((size_t)*stride, (size_t)height, &extent) != ASCIICHAT_OK || checked_size_mul((size_t)width, 6 * sizeof(uint32_t), &bytes) != ASCIICHAT_OK ||
165 checked_size_mul((size_t)width, ((size_t)height + 1) / 2, &capacity) != ASCIICHAT_OK || checked_size_mul(capacity, 48u, &capacity) != ASCIICHAT_OK ||
166 checked_size_add(capacity, 1u, &capacity) != ASCIICHAT_OK)
167 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid half-block stride or size");
168 prepare_output(frame, capacity, target);
169 frame->width = (size_t)width;
170 frame->source_height = (size_t)height;
173 frame->scratch = bytes <= sizeof(frame->inline_scratch) ? frame->inline_scratch : SAFE_MALLOC(bytes, uint32_t *);
174 frame->packed = frame->scratch;
175 frame->index = frame->packed + width;
176 frame->glyph = frame->index + width;
177 frame->fg = frame->glyph + width;
178 frame->bg = frame->fg + width;
179 const char blocks[5][4] = {"\xe2\x96\x91", "\xe2\x96\x92", "\xe2\x96\x93", "\xe2\x96\x88", "\xe2\x96\x80"};
180 for (size_t i = 0; i < 5; ++i)
181 memcpy(frame->table + i, blocks[i], sizeof(blocks[i]) - 1);
182 return ASCIICHAT_OK;
183}
184void ascii_frame_emit_halfrow(ascii_frame_t *frame, ascii_color_t mode, bool more_rows, const simd_cells_t *ops) {
185 ascii_stabilizer_halfrow(frame, mode, ops);
186 ascii_emit_prepared_row(&frame->output, mode, ops, frame->glyph, frame->fg, frame->bg, frame->width);
187 emit_row_end(&frame->output, mode != ASCII_COLOR_NONE, more_rows);
188 ++frame->row;
189}
ANSI escape sequence utilities and fast color code generation.
#define SET_ERRNO(code,...)
char cells[(512 *256 *2)]
Definition fft.c:22
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
char * append_16color_fg(char *dst, uint8_t color_index)
Definition ansi.c:321
char * append_16color_bg(char *dst, uint8_t color_index)
Definition ansi.c:337
void get_16color_rgb(uint8_t color_index, uint8_t *r, uint8_t *g, uint8_t *b)
Definition ansi.c:396
#define SAFE_REALLOC(ptr, size, cast)
unsigned int uint32_t
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
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
void emit_set_256_color_bg(outbuf_t *ob, uint8_t color_idx)
Emit 256-color background ANSI sequence.
utf8_palette_cache_t * get_utf8_palette_cache(const char *ascii_chars)
Get UTF-8 palette cache for a character set.
void emit_set_256_color_fg(outbuf_t *ob, uint8_t color_idx)
Emit 256-color foreground ANSI sequence.
void ob_term(outbuf_t *ob)
Append null terminator to buffer.
void emit_set_truecolor_fg(outbuf_t *out, uint8_t r, uint8_t g, uint8_t b)
Emit truecolor foreground ANSI sequence.
struct utf8_palette_cache utf8_palette_cache_t
void emit_reset(outbuf_t *out)
Emit ANSI reset sequence.
void emit_set_truecolor_bg(outbuf_t *out, uint8_t r, uint8_t g, uint8_t b)
Emit truecolor background ANSI sequence.
void ob_write(outbuf_t *ob, const void *s, size_t n)
Append a string to buffer.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
SIMD-optimized ASCII conversion interface.
#define ascii_frame_emit_halfrow
#define ascii_render_set_output_target
#define VIDEO_ASCII_RENDER_SEQUENCE_BUFFER_SIZE
#define ascii_frame_init
#define ascii_half_frame_init
#define ascii_frame_emit_row
#define ascii_frame_finish
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
void emit_glyph_run(outbuf_t *out, const void *glyph, size_t bytes, uint32_t count)
void emit_row_end(outbuf_t *out, bool colored, bool newline)
✅ Safe Integer Arithmetic and Overflow Detection
static bool size_t gotel_query_t * out
Definition queries.h:8
void ascii_stabilizer_halfrow(ascii_frame_t *frame, ascii_color_t mode, const simd_cells_t *ops)
Definition stabilize.c:175
void ascii_stabilizer_frame_row(ascii_frame_t *frame, ascii_color_t mode, bool background, const simd_cells_t *ops)
Definition stabilize.c:123
ascii_stabilizer_t * ascii_stabilizer_target(void)
Definition stabilize.c:9
🔤 String Manipulation and Shell Escaping Utilities
When code needs to work with ascii frame, it keeps output, output_target, width, source_height here i...
SIMD-friendly storage for one RGB pixel. r, g, and b hold the color channels and padding keeps the la...
int h
Image height in pixels (must be > 0).
rgb_pixel_t * pixels
Pixel data array (width * height RGB pixels, row-major order).
int w
Image width in pixels (must be > 0).
Dynamic output buffer (auto-expanding).
size_t cap
Buffer capacity in bytes (maximum length before reallocation).
size_t len
Current length in bytes (excluding null terminator).
char * buf
Buffer pointer (allocated, owned by caller, must be freed).
The useful part of simd cells is in luminance: together they describe what is pending,...
Definition cells.h:14
size_t(* run)(const uint32_t *glyph, const uint32_t *fg, const uint32_t *bg, size_t count)
Definition cells.h:20
void(* emit_mono)(outbuf_t *out, const uint32_t *glyph, size_t count)
Definition cells.h:21
uint32_t ascii_background_contrast(ascii_color_t mode, uint32_t color, uint8_t luma)
void ascii_emit_prepared_row(outbuf_t *out, ascii_color_t mode, const simd_cells_t *ops, const uint32_t *glyph, const uint32_t *fg, const uint32_t *bg, size_t width)