ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
lib/video/ascii/render.c
Go to the documentation of this file.
1
4
5/* Storage capacities in elements; text buffers include the terminating NUL. */
6#define VIDEO_ASCII_RENDER_SEQUENCE_BUFFER_SIZE 16
7#define ASCII_TEMPORAL_MOTION_LIMIT 8
8#define ASCII_TEMPORAL_MAX_INTERVAL_NS 250000000ULL
9#define ASCII_TEMPORAL_COLOR_TIME_NS 50000000ULL
10
11#include <ascii-chat/otel.h>
12#include <ascii-chat/common.h>
14#include <libplatform/util/string.h>
20#include <string.h>
21#include <limits.h>
23
24/* Keep the compatibility matcher self-contained while the production
25 * renderer uses the split common/stabilizer implementation. */
26#define ascii_render_set_output_target ascii_legacy_render_set_output_target
27#define ascii_stabilizer_set_target ascii_legacy_stabilizer_set_target
28#define ascii_stabilizer_reset ascii_legacy_stabilizer_reset
29#define ascii_stabilizer_prepare ascii_legacy_stabilizer_prepare
30#define ascii_stabilizer_choose ascii_legacy_stabilizer_choose
31#define ascii_frame_init ascii_legacy_frame_init
32#define ascii_frame_emit_row ascii_legacy_frame_emit_row
33#define ascii_frame_finish ascii_legacy_frame_finish
34#define ascii_half_frame_init ascii_legacy_half_frame_init
35#define ascii_frame_emit_halfrow ascii_legacy_frame_emit_halfrow
36#define ascii_render ascii_legacy_render
37#define ascii_render_halfblocks ascii_legacy_render_halfblocks
38
43uint32_t ascii_legacy_stabilizer_choose(ascii_temporal_cell_t *cell, const uint8_t samples[16], uint32_t candidate, uint64_t candidate_error, uint64_t previous_error, uint32_t color,
44 uint64_t elapsed_ns, uint32_t *stabilized_color);
45asciichat_error_t ascii_legacy_frame_init(ascii_frame_t *frame, const image_t *image, const char *palette);
46void ascii_legacy_frame_emit_row(ascii_frame_t *frame, ascii_color_t mode, bool background, bool more_rows, const simd_cells_t *ops);
49void ascii_legacy_frame_emit_halfrow(ascii_frame_t *frame, ascii_color_t mode, bool more_rows, const simd_cells_t *ops);
50char *ascii_legacy_render(const image_t *image, const char *palette, ascii_color_t mode, bool background, bool dither, const simd_cells_t *ops);
51char *ascii_legacy_render_halfblocks(const uint8_t *rgb, int width, int height, int stride, ascii_color_t mode, const simd_cells_t *ops);
52
53static _Thread_local outbuf_t *g_output_target;
54#define ASCII_RENDER_WORKSPACE_POOL_SIZE 4
55static _Thread_local uint32_t g_scratch_workspace[ASCII_RENDER_WORKSPACE_POOL_SIZE][IMAGE_MAX_WIDTH * ASCII_SCRATCH_PLANES];
56static _Thread_local bool g_scratch_workspace_busy[ASCII_RENDER_WORKSPACE_POOL_SIZE];
57
58static uint32_t *acquire_scratch(uint32_t *inline_scratch, size_t bytes) {
60 return inline_scratch;
61 if (bytes > sizeof(g_scratch_workspace[0]))
62 return NULL;
63 for (size_t i = 0; i < ASCII_RENDER_WORKSPACE_POOL_SIZE; ++i) {
64 if (!g_scratch_workspace_busy[i]) {
65 g_scratch_workspace_busy[i] = true;
66 return g_scratch_workspace[i];
67 }
68 }
69 return NULL;
70}
71
72static void release_scratch(uint32_t *scratch) {
73 for (size_t i = 0; i < ASCII_RENDER_WORKSPACE_POOL_SIZE; ++i) {
74 if (scratch == g_scratch_workspace[i]) {
75 g_scratch_workspace_busy[i] = false;
76 return;
77 }
78 }
79}
80static _Thread_local ascii_stabilizer_t *g_stabilizer_target;
81
83 ascii_stabilizer_t *previous = g_stabilizer_target;
84 g_stabilizer_target = state;
85 return previous;
86}
87
89 if (!state)
90 return;
91 SAFE_FREE(state->cells);
92 memset(state, 0, sizeof(*state));
93}
94
96 if (!state || !width || !height || width > SIZE_MAX / height || width * height > SIZE_MAX / sizeof(ascii_temporal_cell_t))
97 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid stabilization viewport");
98 if (state->cells && state->width == width && state->height == height && state->configuration == configuration)
99 return ASCIICHAT_OK;
101 if (!cells)
102 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate stabilization history");
103 SAFE_FREE(state->cells);
104 state->cells = cells;
105 state->width = width;
106 state->height = height;
107 state->configuration = configuration;
108 state->last_timestamp_ns = 0;
109 state->elapsed_ns = UINT64_MAX;
110 return ASCIICHAT_OK;
111}
112
113static uint32_t temporal_blend_color(uint32_t previous, uint32_t current, unsigned weight) {
114 uint32_t result = 0;
115 for (unsigned shift = 0; shift <= 16; shift += 8) {
116 unsigned old_channel = (previous >> shift) & 255;
117 unsigned new_channel = (current >> shift) & 255;
118 result |= (uint32_t)((old_channel * (256 - weight) + new_channel * weight + 128) / 256) << shift;
119 }
120 return result;
121}
122
123uint32_t ascii_stabilizer_choose(ascii_temporal_cell_t *cell, const uint8_t samples[16], uint32_t candidate, uint64_t candidate_error, uint64_t previous_error, uint32_t color, uint64_t elapsed_ns,
124 uint32_t *stabilized_color) {
125 unsigned motion = 0;
126 if (cell->valid) {
127 for (size_t i = 0; i < 16; ++i)
128 motion += (unsigned)abs((int)samples[i] - (int)cell->samples[i]);
129 motion /= 16;
130 }
131 uint32_t result = candidate;
132 uint32_t output_color = color;
133 if (cell->valid && motion <= ASCII_TEMPORAL_MOTION_LIMIT && elapsed_ns <= ASCII_TEMPORAL_MAX_INTERVAL_NS) {
134 uint64_t margin = candidate_error / 8 + 128;
135 if (previous_error != UINT64_MAX && (previous_error <= candidate_error || previous_error - candidate_error <= margin))
136 result = cell->glyph;
137 // Weight depends on elapsed time, avoiding a different response at each FPS.
138 unsigned weight = (unsigned)(elapsed_ns * 256 / (elapsed_ns + ASCII_TEMPORAL_COLOR_TIME_NS));
139 output_color = temporal_blend_color(cell->color, color, weight);
140 }
141 memcpy(cell->samples, samples, sizeof(cell->samples));
142 cell->glyph = result;
143 cell->color = output_color;
144 cell->valid = true;
145 if (stabilized_color)
146 *stabilized_color = output_color;
147 return result;
148}
149
151 outbuf_t *previous = g_output_target;
152 g_output_target = target;
153 return previous;
154}
155
156static void prepare_output(ascii_frame_t *frame, size_t capacity, outbuf_t *target) {
157 frame->output_target = target;
158 if (target) {
159 if (target->cap < capacity) {
160 target->buf = SAFE_REALLOC(target->buf, capacity, char *);
161 target->cap = capacity;
162 }
163 frame->output = *target;
164 frame->output.len = 0;
165 } else {
166 frame->output.cap = capacity;
167 frame->output.buf = SAFE_MALLOC(capacity, char *);
168 }
169}
170
171static void emit_color(outbuf_t *out, ascii_color_t mode, bool background, int value) {
172 if (mode == ASCII_COLOR_TRUE) {
173 if (background)
174 emit_set_truecolor_bg(out, (uint8_t)(value >> 16), (uint8_t)(value >> 8), (uint8_t)value);
175 else
176 emit_set_truecolor_fg(out, (uint8_t)(value >> 16), (uint8_t)(value >> 8), (uint8_t)value);
177 } else if (mode == ASCII_COLOR_256) {
178 if (background)
180 else
182 } else if (mode == ASCII_COLOR_16) {
184 char *end = background ? append_16color_bg(sequence, (uint8_t)value) : append_16color_fg(sequence, (uint8_t)value);
185 ob_write(out, sequence, (size_t)(end - sequence));
186 }
187}
188
189static size_t glyph_bytes(uint32_t key) {
190 uint8_t first;
191 memcpy(&first, &key, 1);
192 return first < 128 ? 1 : (first & 0xe0) == 0xc0 ? 2 : (first & 0xf0) == 0xe0 ? 3 : 4;
193}
194
195static void 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) {
196 if (mode == ASCII_COLOR_NONE && ops->emit_mono) {
197 ops->emit_mono(out, glyph, width);
198 return;
199 }
200 uint32_t current_fg = UINT32_MAX, current_bg = UINT32_MAX;
201 for (size_t x = 0; x < width;) {
202 size_t run = ops->run(glyph + x, fg + x, bg + x, width - x);
203 if (fg[x] == UINT32_MAX && bg[x] == UINT32_MAX && (current_fg != UINT32_MAX || current_bg != UINT32_MAX)) {
205 current_fg = current_bg = UINT32_MAX;
206 }
207 if (fg[x] != current_fg) {
208 emit_color(out, mode, false, (int)fg[x]);
209 current_fg = fg[x];
210 }
211 if (bg[x] != current_bg) {
212 emit_color(out, mode, true, (int)bg[x]);
213 current_bg = bg[x];
214 }
215 emit_glyph_run(out, &glyph[x], glyph_bytes(glyph[x]), (uint32_t)run);
216 x += run;
217 }
218}
219
220char *ascii_render(const image_t *image, const char *palette, ascii_color_t mode, bool background, bool dither, const simd_cells_t *ops) {
221 if (!ops || mode < ASCII_COLOR_NONE || mode > ASCII_COLOR_TRUE) {
222 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid rendering backend or color mode");
223 return NULL;
224 }
225 if (!dither) {
226#if SIMD_SUPPORT_SSE2
227 if (ops == &simd_cells_sse2)
228 return render_ascii_cells_sse2(image, palette, mode, background);
229#endif
230#if SIMD_SUPPORT_SSSE3
231 if (ops == &simd_cells_ssse3)
232 return render_ascii_cells_ssse3(image, palette, mode, background);
233#endif
234#if SIMD_SUPPORT_AVX2
235 if (ops == &simd_cells_avx2)
236 return render_ascii_cells_avx2(image, palette, mode, background);
237#endif
238#if SIMD_SUPPORT_NEON
239 if (ops == &simd_cells_neon)
240 return render_ascii_cells_neon(image, palette, mode, background);
241#endif
242#if SIMD_SUPPORT_SVE
243 if (ops == &simd_cells_sve)
244 return render_ascii_cells_sve(image, palette, mode, background);
245#endif
246 }
247 ascii_frame_t frame;
248 if (ascii_frame_init(&frame, image, palette) != ASCIICHAT_OK)
249 return NULL;
250 rgb_error_t *errors = NULL;
251 if (mode == ASCII_COLOR_16 && dither)
252 errors = SAFE_CALLOC((size_t)image->w * (size_t)image->h, sizeof(rgb_error_t), rgb_error_t *);
253 frame.dithered = errors != NULL;
254 for (int y = 0; y < image->h; ++y) {
255 const uint8_t *source = (const uint8_t *)(image->pixels + (size_t)y * frame.width);
256 ops->luminance(source, frame.luma, frame.width);
257 ops->pack(source, frame.packed, frame.width);
258 ops->lookup(frame.luma, frame.table, frame.glyph, frame.width);
259 if (mode == ASCII_COLOR_16 || mode == ASCII_COLOR_256) {
260 if (errors) {
261 // Floyd-Steinberg error propagation is ordered; the other stages remain SIMD.
262 for (int x = 0; x < image->w; ++x) {
263 uint32_t pixel = frame.packed[x];
264 frame.index[x] = rgb_to_16color_dithered((int)(pixel >> 16), (int)((pixel >> 8) & 255), (int)(pixel & 255), x, y, image->w, image->h, errors);
265 }
266 } else
267 ops->quantize(frame.packed, frame.index, frame.width, mode == ASCII_COLOR_16 ? 16 : 256);
268 }
269 ascii_frame_emit_row(&frame, mode, background, y + 1 < image->h, ops);
270 }
271 SAFE_FREE(errors);
272 return ascii_frame_finish(&frame);
273}
274
275char *ascii_render_halfblocks(const uint8_t *rgb, int width, int height, int stride, ascii_color_t mode, const simd_cells_t *ops) {
276#if SIMD_SUPPORT_SSE2
277 if (ops == &simd_cells_sse2)
278 return render_halfblocks_sse2(rgb, width, height, stride, mode);
279#endif
280#if SIMD_SUPPORT_SSSE3
281 if (ops == &simd_cells_ssse3)
282 return render_halfblocks_ssse3(rgb, width, height, stride, mode);
283#endif
284#if SIMD_SUPPORT_AVX2
285 if (ops == &simd_cells_avx2)
286 return render_halfblocks_avx2(rgb, width, height, stride, mode);
287#endif
288#if SIMD_SUPPORT_NEON
289 if (ops == &simd_cells_neon)
290 return render_halfblocks_neon(rgb, width, height, stride, mode);
291#endif
292#if SIMD_SUPPORT_SVE
293 if (ops == &simd_cells_sve)
294 return render_halfblocks_sve(rgb, width, height, stride, mode);
295#endif
296
297 if (width <= 0 || height <= 0)
298 return platform_strdup("");
299 if (!ops) {
300 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid half-block backend");
301 return NULL;
302 }
303 ascii_frame_t frame;
304 if (ascii_half_frame_init(&frame, rgb, width, height, &stride, mode) != ASCIICHAT_OK)
305 return NULL;
306 for (size_t y = 0; y < (size_t)height; y += 2) {
307 const uint8_t *top = rgb + y * (size_t)stride;
308 const uint8_t *bottom = y + 1 < (size_t)height ? top + stride : top;
309 ops->pack(top, frame.packed, frame.width);
310 ops->pack(bottom, frame.index, frame.width);
311 if (mode == ASCII_COLOR_TRUE) {
312 memcpy(frame.fg, frame.packed, frame.width * sizeof(uint32_t));
313 memcpy(frame.bg, frame.index, frame.width * sizeof(uint32_t));
314 } else if (mode != ASCII_COLOR_NONE) {
315 ops->quantize(frame.packed, frame.fg, frame.width, mode == ASCII_COLOR_16 ? 16 : 256);
316 ops->quantize(frame.index, frame.bg, frame.width, mode == ASCII_COLOR_16 ? 16 : 256);
317 }
318 ops->halfblock(frame.packed, frame.index, frame.glyph, frame.fg, frame.bg, frame.width, (unsigned)mode, frame.table);
319 ascii_frame_emit_halfrow(&frame, mode, y + 2 < (size_t)height, ops);
320 }
321 return ascii_frame_finish(&frame);
322}
323
324asciichat_error_t ascii_frame_init(ascii_frame_t *frame, const image_t *image, const char *palette) {
325 ASCII_OTEL_SCOPE(ASCII_FRAME_INIT);
326 outbuf_t *target = g_output_target;
327 memset(frame, 0, sizeof(*frame));
328 if (!image || !image->pixels || image->w <= 0 || image->h <= 0 || !palette || !palette[0])
329 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid image or palette");
330 size_t bytes, pixels, capacity, rows;
331 if (checked_size_mul((size_t)image->w, (size_t)image->h, &pixels) != ASCIICHAT_OK || checked_size_mul(pixels, 48u, &capacity) != ASCIICHAT_OK ||
332 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 ||
333 checked_size_mul((size_t)image->w, 6 * sizeof(uint32_t), &bytes) != ASCIICHAT_OK)
334 return ERROR_INVALID_PARAM;
336 if (!cache)
337 return ERROR_INVALID_PARAM;
338 prepare_output(frame, capacity, target);
339 frame->width = (size_t)image->w;
340 ascii_stabilizer_t *stabilizer_target = ascii_stabilizer_target();
341 if (stabilizer_target && stabilizer_target->cells && stabilizer_target->width == (size_t)image->w && stabilizer_target->height == (size_t)image->h)
342 frame->stabilizer = stabilizer_target;
343 for (size_t i = 0; i < 64; ++i)
344 memcpy(frame->table + i, cache->cache64[i].utf8_bytes, cache->cache64[i].byte_len);
345 frame->scratch = acquire_scratch(frame->inline_scratch, bytes);
346 if (!frame->scratch)
347 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate ASCII scratch workspace");
348 frame->packed = frame->scratch;
349 frame->index = frame->packed + frame->width;
350 frame->glyph = frame->index + frame->width;
351 frame->fg = frame->glyph + frame->width;
352 frame->bg = frame->fg + frame->width;
353 frame->luma = (uint8_t *)(frame->bg + frame->width);
354 return ASCIICHAT_OK;
355}
356void ascii_frame_emit_row(ascii_frame_t *frame, ascii_color_t mode, bool background, bool more_rows, const simd_cells_t *ops) {
357 if (frame->stabilizer && !frame->temporal_preselected) {
358 const uint64_t elapsed = frame->stabilizer->elapsed_ns;
359 const bool recent = elapsed <= ASCII_TEMPORAL_MAX_INTERVAL_NS;
360 const unsigned weight = recent ? (unsigned)(elapsed * 256 / (elapsed + ASCII_TEMPORAL_COLOR_TIME_NS)) : 256;
361 for (size_t x = 0; x < frame->width; ++x) {
362 ascii_temporal_cell_t *cell = &frame->stabilizer->cells[frame->row * frame->width + x];
363 uint8_t luma = frame->luma[x];
364 uint32_t glyph = frame->glyph[x];
365 uint32_t color = frame->packed[x];
366 if (cell->valid && recent && abs((int)luma - (int)cell->samples[0]) <= ASCII_TEMPORAL_MOTION_LIMIT) {
367 if (cell->glyph != glyph) {
368 // Only nearby brightness bins can win the hysteresis test. Searching
369 // outward avoids scanning the entire 64-entry palette for every cell.
370 unsigned bin = luma >> 2;
371 int best_delta = (int)luma - ((int)bin * 4 + 2);
372 uint64_t best_error = (uint64_t)(best_delta * best_delta);
373 uint64_t margin = best_error / 8 + 128;
374 for (unsigned distance = 1; distance < 64; ++distance) {
375 bool found = false;
376 for (int direction = -1; direction <= 1; direction += 2) {
377 int index = (int)bin + direction * (int)distance;
378 if (index < 0 || index >= 64 || frame->table[index] != cell->glyph)
379 continue;
380 int delta = (int)luma - (index * 4 + 2);
381 uint64_t error = (uint64_t)(delta * delta);
382 if (error <= best_error || error - best_error <= margin)
383 glyph = cell->glyph;
384 found = true;
385 }
386 // Bins farther than this cannot fall within the error margin.
387 int nearest_delta = (int)distance * 4 - 3;
388 if (found || (uint64_t)(nearest_delta * nearest_delta) > best_error + margin)
389 break;
390 }
391 }
392 color = temporal_blend_color(cell->color, color, weight);
393 }
394 cell->samples[0] = luma;
395 cell->glyph = frame->glyph[x] = glyph;
396 cell->color = frame->packed[x] = color;
397 cell->valid = true;
398 if (background)
399 frame->luma[x] = (uint8_t)((77u * (color >> 16) + 150u * ((color >> 8) & 255) + 29u * (color & 255) + 128u) >> 8);
400 }
401 if (!frame->dithered && (mode == ASCII_COLOR_16 || mode == ASCII_COLOR_256))
402 ops->quantize(frame->packed, frame->index, frame->width, mode == ASCII_COLOR_16 ? 16 : 256);
403 }
404 for (size_t x = 0; x < frame->width; ++x) {
405 frame->fg[x] = frame->bg[x] = UINT32_MAX;
406 if (mode == ASCII_COLOR_NONE)
407 continue;
408 uint32_t value = mode == ASCII_COLOR_TRUE ? frame->packed[x] : frame->index[x];
409 uint32_t contrast = frame->luma[x] < 128 ? (mode == ASCII_COLOR_TRUE ? 0xffffffu : 15u) : 0;
410 if (mode == ASCII_COLOR_16) {
411 uint8_t r, g, b;
412 get_16color_rgb((uint8_t)value, &r, &g, &b);
413 contrast = (77 * r + 150 * g + 29 * b) / 256 < 127 ? 15u : 0;
414 }
415 frame->fg[x] = background ? contrast : value;
416 frame->bg[x] = background ? value : UINT32_MAX;
417 }
418 emit_prepared_row(&frame->output, mode, ops, frame->glyph, frame->fg, frame->bg, frame->width);
419 emit_row_end(&frame->output, mode != ASCII_COLOR_NONE, more_rows);
420 ++frame->row;
421}
423 if (frame->scratch != frame->inline_scratch)
424 release_scratch(frame->scratch);
425 ob_term(&frame->output);
426 if (frame->output_target)
427 *frame->output_target = frame->output;
428 return frame->output.buf;
429}
430
432 ASCII_OTEL_SCOPE(ASCII_HALF_FRAME_INIT);
433 outbuf_t *target = g_output_target;
434 memset(frame, 0, sizeof(*frame));
435 size_t row_bytes, extent, bytes, capacity;
436 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)
437 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid half-block input");
438 if (*stride <= 0)
439 *stride = (int)row_bytes;
440 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 ||
441 checked_size_mul((size_t)width, ((size_t)height + 1) / 2, &capacity) != ASCIICHAT_OK || checked_size_mul(capacity, 48u, &capacity) != ASCIICHAT_OK ||
442 checked_size_add(capacity, 1u, &capacity) != ASCIICHAT_OK)
443 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid half-block stride or size");
444 prepare_output(frame, capacity, target);
445 frame->width = (size_t)width;
446 frame->source_height = (size_t)height;
447 if (g_stabilizer_target && g_stabilizer_target->cells && g_stabilizer_target->width == (size_t)width && g_stabilizer_target->height == (size_t)height)
448 frame->stabilizer = g_stabilizer_target;
449 frame->scratch = acquire_scratch(frame->inline_scratch, bytes);
450 if (!frame->scratch)
451 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate ASCII scratch workspace");
452 frame->packed = frame->scratch;
453 frame->index = frame->packed + width;
454 frame->glyph = frame->index + width;
455 frame->fg = frame->glyph + width;
456 frame->bg = frame->fg + width;
457 const char blocks[5][4] = {"\xe2\x96\x91", "\xe2\x96\x92", "\xe2\x96\x93", "\xe2\x96\x88", "\xe2\x96\x80"};
458 for (size_t i = 0; i < 5; ++i)
459 memcpy(frame->table + i, blocks[i], 3);
460 return ASCIICHAT_OK;
461}
462void ascii_frame_emit_halfrow(ascii_frame_t *frame, ascii_color_t mode, bool more_rows, const simd_cells_t *ops) {
463 if (frame->stabilizer) {
464 size_t top_row = frame->row * 2;
465 bool has_bottom = top_row + 1 < frame->source_height;
466 for (size_t x = 0; x < frame->width; ++x) {
467 uint32_t *pixels[] = {&frame->packed[x], &frame->index[x]};
468 for (size_t half = 0; half < (has_bottom ? 2u : 1u); ++half) {
469 uint32_t color = *pixels[half];
470 uint8_t luma = (uint8_t)((76u * (color >> 16) + 150u * ((color >> 8) & 255) + 29u * (color & 255)) >> 8);
471 uint8_t samples[16];
472 memset(samples, luma, sizeof(samples));
473 ascii_temporal_cell_t *cell = &frame->stabilizer->cells[(top_row + half) * frame->width + x];
474 ascii_stabilizer_choose(cell, samples, 0, 0, UINT64_MAX, color, frame->stabilizer->elapsed_ns, pixels[half]);
475 }
476 if (!has_bottom)
477 frame->index[x] = frame->packed[x];
478 }
479 if (mode == ASCII_COLOR_TRUE) {
480 memcpy(frame->fg, frame->packed, frame->width * sizeof(uint32_t));
481 memcpy(frame->bg, frame->index, frame->width * sizeof(uint32_t));
482 } else if (mode != ASCII_COLOR_NONE) {
483 ops->quantize(frame->packed, frame->fg, frame->width, mode == ASCII_COLOR_16 ? 16 : 256);
484 ops->quantize(frame->index, frame->bg, frame->width, mode == ASCII_COLOR_16 ? 16 : 256);
485 }
486 ops->halfblock(frame->packed, frame->index, frame->glyph, frame->fg, frame->bg, frame->width, (unsigned)mode, frame->table);
487 }
488 emit_prepared_row(&frame->output, mode, ops, frame->glyph, frame->fg, frame->bg, frame->width);
489 emit_row_end(&frame->output, mode != ASCII_COLOR_NONE, more_rows);
490 ++frame->row;
491}
492
493/* Compare a small spatial image patch against glyph coverage masks.
494 * The source is fitted at 8x8 samples per output cell, then averaged to 4x4.
495 * ASCII coverage comes from DejaVu Sans Mono; real terminal fonts may differ. */
496#include "glyph_masks.h"
497
498#define GLYPH_ASCII_COUNT 95
499#define GLYPH_BLOCK_COUNT 20
500#define GLYPH_COUNT (GLYPH_ASCII_COUNT + GLYPH_BLOCK_COUNT)
501
502static int glyph_palette_index(const uint8_t *p, size_t *length) {
503 if (*p >= 32 && *p <= 126) {
504 *length = 1;
505 return (int)*p - 32;
506 }
507 if (*p == 0xe2 && p[1] == 0x96 && p[2] >= 0x80 && p[2] <= 0x93) {
508 *length = 3;
509 return GLYPH_ASCII_COUNT + (int)(p[2] - 0x80);
510 }
511 return -1;
512}
513
514// Block elements have font-independent geometry. Coverage is the area of
515// each quarter-cell covered by the block, including eighth-cell boundaries.
516static void glyph_block_mask(int block, uint8_t mask[16]) {
517 for (int y = 0; y < 4; ++y) {
518 for (int x = 0; x < 4; ++x) {
519 int coverage = 0;
520 for (int sy = 0; sy < 2; ++sy) {
521 for (int sx = 0; sx < 2; ++sx) {
522 int px = 2 * x + sx, py = 2 * y + sy;
523 bool set = block == 0 ? py < 4 : block <= 7 ? py >= 8 - block : block == 8 ? true : block <= 15 ? px < 16 - block : block == 16 ? px >= 4 : false;
524 coverage += set ? 1 : 0;
525 }
526 }
527 mask[y * 4 + x] = block >= 17 ? (uint8_t)((block - 16) * 255 / 4) : (uint8_t)(coverage * 255 / 4);
528 }
529 }
530}
531
532static uint32_t glyph_key(int index) {
534 return (uint32_t)(index + 32);
535 uint32_t cp = 0x2580u + (uint32_t)(index - GLYPH_ASCII_COUNT);
536 const uint8_t bytes[4] = {(uint8_t)(0xe0u | (cp >> 12)), (uint8_t)(0x80u | ((cp >> 6) & 63)), (uint8_t)(0x80u | (cp & 63)), 0};
537 uint32_t key = 0;
538 memcpy(&key, bytes, sizeof(key));
539 return key;
540}
541
542bool ascii_glyph_palette_supported(const char *palette) {
543 if (!palette || !*palette)
544 return false;
545 for (const uint8_t *p = (const uint8_t *)palette; *p;) {
546 size_t length;
547 if (glyph_palette_index(p, &length) < 0)
548 return false;
549 p += length;
550 }
551 return true;
552}
553
554typedef struct {
555 int16_t mask[16];
556 int16_t centered[16];
565
566// Preserve hue while keeping visible ink against the dark canvas. Color is
567// weighted by glyph coverage, so a small feature is not colored by its corner.
568static uint32_t glyph_foreground(const glyph_candidate_t *glyph, const int16_t samples[3][16], const int sums[3]) {
569 unsigned channels[3], peak = 0;
570 for (int c = 0; c < 3; ++c) {
571 int weighted = 0;
572 for (int i = 0; i < 16; ++i)
573 weighted += samples[c][i] * glyph->mask[i];
574 channels[c] = glyph->sum ? (unsigned)((weighted + glyph->sum / 2) / glyph->sum) : (unsigned)((sums[c] + 8) / 16);
575 if (channels[c] > peak)
576 peak = channels[c];
577 }
578 if (peak) {
579 unsigned target = 128 + peak / 2;
580 for (int c = 0; c < 3; ++c)
581 channels[c] = (channels[c] * target + peak / 2) / peak;
582 }
583 return (channels[0] << 16) | (channels[1] << 8) | channels[2];
584}
585
586char *ascii_render_glyph_match(const image_t *image, ascii_color_t mode, const simd_cells_t *ops, const char *palette) {
587 return ascii_render_glyph_match_mode(image, mode, ops, palette, false);
588}
589
590char *ascii_render_glyph_match_mode(const image_t *image, ascii_color_t mode, const simd_cells_t *ops, const char *palette, bool background) {
591 if (!image || !image->pixels || !ops || image->w <= 0 || image->h <= 0 || image->w % 8 || image->h % 8 || mode < ASCII_COLOR_NONE || mode > ASCII_COLOR_TRUE) {
592 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid glyph matching input");
593 return NULL;
594 }
595 bool allowed[GLYPH_COUNT] = {false};
596 bool use_chroma_shape = false;
597 if (mode == ASCII_COLOR_TRUE && palette) {
598 for (const uint8_t *p = (const uint8_t *)palette; *p; ++p) {
599 if (*p >= 0x80) {
600 use_chroma_shape = true;
601 break;
602 }
603 }
604 }
605 int candidates[GLYPH_COUNT];
606 // Restrict the atlas to unique palette entries before scoring any cells.
607 size_t candidate_count = 0;
608 int (*shape_correlation)(const int16_t[16], const int16_t[16]) = ops->glyph_shape_correlation ? ops->glyph_shape_correlation : glyph_shape_correlation_scalar;
609 if (palette) {
610 if (!ascii_glyph_palette_supported(palette)) {
611 SET_ERRNO(ERROR_INVALID_PARAM, "Glyph palette contains an unsupported character");
612 return NULL;
613 }
614 for (const uint8_t *p = (const uint8_t *)palette; *p;) {
615 size_t length = 1;
616 int index = glyph_palette_index(p, &length);
617 if (!allowed[index]) {
618 allowed[index] = true;
619 candidates[candidate_count++] = index;
620 }
621 p += length;
622 }
623 } else {
624 for (int i = 0; i < GLYPH_ASCII_COUNT; ++i) {
625 allowed[i] = true;
626 candidates[candidate_count++] = i;
627 }
628 }
629 // Prepare coverage and its sufficient statistics once per frame.
631 // Precompute each mask's brightness and centered shape for correlation.
632 for (size_t c = 0; c < candidate_count; ++c) {
633 int g = candidates[c];
634 glyph_candidate_t *glyph = &glyphs[c];
635 glyph->key = glyph_key(g);
636 uint8_t block_mask[16];
637 const uint8_t *mask = g < GLYPH_ASCII_COUNT ? glyph_masks[g] : block_mask;
638 if (g >= GLYPH_ASCII_COUNT)
639 glyph_block_mask(g - GLYPH_ASCII_COUNT, block_mask);
640 glyph->sum = glyph->energy = 0;
641 for (int i = 0; i < 16; ++i) {
642 glyph->mask[i] = mask[i];
643 glyph->sum += mask[i];
644 }
645 int mean = glyph->sum / 16;
646 glyph->tone = glyph->sum * 255 / (g < GLYPH_ASCII_COUNT ? GLYPH_MASK_MAX_SUM : 16 * 255);
647 for (int i = 0; i < 16; ++i) {
648 glyph->centered[i] = (int16_t)((int)mask[i] - mean);
649 glyph->energy += glyph->centered[i] * glyph->centered[i];
650 }
651 }
653 cells.w /= 8;
654 cells.h /= 8;
655 ascii_frame_t frame;
656 if (ascii_frame_init(&frame, &cells, " .") != ASCIICHAT_OK)
657 return NULL;
658 frame.temporal_preselected = true;
659 for (int y = 0; y < cells.h; ++y) {
660 for (int x = 0; x < cells.w; ++x) {
661 int16_t samples[3][16] = {{0}};
662 int sums[3] = {0};
663 uint8_t patch[16];
664 int sample_mean = 0;
665 for (int sy = 0; sy < 4; ++sy) {
666 const rgb_pixel_t *top = image->pixels + (size_t)(y * 8 + sy * 2) * (size_t)image->w + (size_t)(x * 8);
667 const rgb_pixel_t *bottom = top + image->w;
668 for (int sx = 0; sx < 4; ++sx) {
669 const rgb_pixel_t a = top[sx * 2], b = top[sx * 2 + 1];
670 const rgb_pixel_t c = bottom[sx * 2], d = bottom[sx * 2 + 1];
671 int i = sy * 4 + sx;
672 int red = (a.r + b.r + c.r + d.r) / 4;
673 int green = (a.g + b.g + c.g + d.g) / 4;
674 int blue = (a.b + b.b + c.b + d.b) / 4;
675 uint8_t luma = (uint8_t)((77 * red + 150 * green + 29 * blue) >> 8);
676 patch[i] = (uint8_t)(luma + (luma * (255 - luma) + 256) / 512);
677 samples[0][i] = (int16_t)red;
678 samples[1][i] = (int16_t)green;
679 samples[2][i] = (int16_t)blue;
680 sample_mean += patch[i];
681 for (int channel = 0; channel < 3; ++channel)
682 sums[channel] += samples[channel][i];
683 }
684 }
685 // Compare local shape independently of the patch's average brightness.
686 sample_mean /= 16;
687 int16_t centered_samples[16];
688 int sample_energy = 0;
689 for (int i = 0; i < 16; ++i) {
690 int centered = (int)patch[i] - sample_mean;
691 centered_samples[i] = (int16_t)centered;
692 sample_energy += centered * centered;
693 }
694 size_t best = 0, previous_index = 0;
695 uint64_t best_error = UINT64_MAX;
696 uint64_t previous_error = UINT64_MAX;
697 ascii_temporal_cell_t *previous = frame.stabilizer ? &frame.stabilizer->cells[(size_t)y * frame.width + x] : NULL;
698 for (size_t c = 0; c < candidate_count; ++c) {
699 bool is_previous = previous && previous->valid && previous->glyph == glyphs[c].key;
700 int tone_delta = sample_mean - glyphs[c].tone;
701 int tone_error = 16 * tone_delta * tone_delta;
702 // Shape error cannot lower the score below the brightness error.
703 if (!is_previous && (uint64_t)tone_error > best_error)
704 continue;
705 int dot = shape_correlation(centered_samples, glyphs[c].centered);
706 uint64_t error = (uint64_t)(tone_error + sample_energy + glyphs[c].energy - 2 * dot);
707 if (use_chroma_shape) {
708 // Luminance alone cannot distinguish equal-luminance chromatic
709 // regions. Use the strongest color-channel shape correlation as a
710 // secondary signal so vertical and horizontal boundaries retain
711 // their orientation in true-colour output.
712 int chroma_dot = 0;
713 for (int channel = 0; channel < 3; ++channel) {
714 int channel_mean = sums[channel] / 16;
715 int channel_dot = 0;
716 for (int i = 0; i < 16; ++i)
717 channel_dot += (samples[channel][i] - channel_mean) * glyphs[c].centered[i];
718 if (abs(channel_dot) > abs(chroma_dot))
719 chroma_dot = channel_dot;
720 }
721 uint64_t chroma_bonus = (uint64_t)(abs(chroma_dot) / 8);
722 error = chroma_bonus < error ? error - chroma_bonus : 0;
723 }
724 if (is_previous) {
725 previous_error = error;
726 previous_index = c;
727 }
728 if (error < best_error) {
729 best_error = error;
730 best = c;
731 }
732 }
733 uint32_t rgb = glyph_foreground(&glyphs[best], samples, sums);
734 uint32_t bg_rgb = ((uint32_t)(sums[0] / 128) << 16) | ((uint32_t)(sums[1] / 128) << 8) | (uint32_t)(sums[2] / 128);
735 uint32_t selected = glyphs[best].key;
736 if (previous) {
737 ascii_temporal_cell_t old = *previous;
738 unsigned motion = 0;
739 unsigned color_motion = 0;
740 for (int i = 0; i < 16; ++i)
741 motion += (unsigned)abs((int)patch[i] - (int)old.samples[i]);
742 if (old.valid && mode != ASCII_COLOR_NONE) {
743 for (int channel = 0; channel < 3; ++channel)
744 for (int i = 0; i < 16; ++i)
745 color_motion += (unsigned)abs(samples[channel][i] - (int)old.rgb_samples[channel][i]);
746 color_motion /= 48;
747 }
748 uint64_t elapsed = frame.stabilizer->elapsed_ns;
749 uint64_t effective_elapsed = color_motion > ASCII_TEMPORAL_MOTION_LIMIT ? UINT64_MAX : elapsed;
750 selected = ascii_stabilizer_choose(previous, patch, selected, best_error, previous_error, rgb, effective_elapsed, &rgb);
751 if (selected != glyphs[best].key) {
752 *previous = old;
753 selected = ascii_stabilizer_choose(previous, patch, glyphs[best].key, best_error, previous_error, glyph_foreground(&glyphs[previous_index], samples, sums), effective_elapsed, &rgb);
754 }
755 if (old.valid && motion / 16 <= ASCII_TEMPORAL_MOTION_LIMIT && color_motion <= ASCII_TEMPORAL_MOTION_LIMIT && elapsed <= ASCII_TEMPORAL_MAX_INTERVAL_NS) {
756 unsigned weight = (unsigned)(elapsed * 256 / (elapsed + ASCII_TEMPORAL_COLOR_TIME_NS));
757 bg_rgb = temporal_blend_color(old.background, bg_rgb, weight);
758 }
759 for (int channel = 0; channel < 3; ++channel)
760 for (int i = 0; i < 16; ++i)
761 previous->rgb_samples[channel][i] = (uint8_t)samples[channel][i];
762 previous->background = bg_rgb;
763 }
764 frame.glyph[x] = selected;
765 frame.packed[x] = rgb;
766 frame.index[x] = bg_rgb;
767 frame.luma[x] = (uint8_t)((77 * ((rgb >> 16) & 255) + 150 * ((rgb >> 8) & 255) + 29 * (rgb & 255)) >> 8);
768 }
769 if (background && mode != ASCII_COLOR_NONE) {
770 if (mode == ASCII_COLOR_TRUE) {
771 memcpy(frame.fg, frame.packed, frame.width * sizeof(uint32_t));
772 memcpy(frame.bg, frame.index, frame.width * sizeof(uint32_t));
773 } else {
774 ops->quantize(frame.packed, frame.fg, frame.width, mode == ASCII_COLOR_16 ? 16 : 256);
775 ops->quantize(frame.index, frame.bg, frame.width, mode == ASCII_COLOR_16 ? 16 : 256);
776 }
777 ascii_emit_prepared_row(&frame.output, mode, ops, frame.glyph, frame.fg, frame.bg, frame.width);
778 emit_row_end(&frame.output, true, y + 1 < cells.h);
779 ++frame.row;
780 } else {
781 if (mode == ASCII_COLOR_16 || mode == ASCII_COLOR_256)
782 ops->quantize(frame.packed, frame.index, frame.width, mode == ASCII_COLOR_16 ? 16 : 256);
783 ascii_frame_emit_row(&frame, mode, false, y + 1 < cells.h, ops);
784 }
785 }
786 return ascii_frame_finish(&frame);
787}
ANSI escape sequence utilities and fast color code generation.
#define SET_ERRNO(code,...)
int glyph_shape_correlation_scalar(const int16_t samples[16], const int16_t mask[16])
Definition cells.c:4
const simd_cells_t simd_cells_sve
const simd_cells_t simd_cells_sse2
const simd_cells_t simd_cells_avx2
const simd_cells_t simd_cells_ssse3
const simd_cells_t simd_cells_neon
char cells[(512 *256 *2)]
Definition fft.c:22
audio_visualization_source_t source
Definition fft.c:25
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
#define GLYPH_MASK_MAX_SUM
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
uint8_t rgb_to_16color_dithered(int r, int g, int b, int x, int y, int width, int height, rgb_error_t *error_buffer)
Definition ansi.c:427
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)
#define SAFE_CALLOC(count, size, cast)
unsigned long long uint64_t
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_MEMORY
Definition error_codes.h:55
@ 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.
#define IMAGE_MAX_WIDTH
An owned RGB image with dimensions and row-major pixel storage.
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.
char * render_halfblocks_sse2(const uint8_t *, int, int, int, ascii_color_t)
char * render_halfblocks_ssse3(const uint8_t *, int, int, int, ascii_color_t)
char * render_halfblocks_avx2(const uint8_t *, int, int, int, ascii_color_t)
char * render_ascii_cells_sve(const image_t *, const char *, ascii_color_t, bool)
char * render_ascii_cells_sse2(const image_t *, const char *, ascii_color_t, bool)
char * render_halfblocks_sve(const uint8_t *, int, int, int, ascii_color_t)
#define ASCII_INLINE_SCRATCH_COLUMNS
char * render_halfblocks_neon(const uint8_t *, int, int, int, ascii_color_t)
char * render_ascii_cells_ssse3(const image_t *, const char *, ascii_color_t, bool)
char * render_ascii_cells_neon(const image_t *, const char *, ascii_color_t, bool)
char * render_ascii_cells_avx2(const image_t *, const char *, ascii_color_t, bool)
#define ASCII_RENDER_WORKSPACE_POOL_SIZE
asciichat_error_t ascii_legacy_stabilizer_prepare(ascii_stabilizer_t *state, size_t width, size_t height, uint64_t configuration)
#define ascii_frame_emit_halfrow
void ascii_legacy_frame_emit_halfrow(ascii_frame_t *frame, ascii_color_t mode, bool more_rows, const simd_cells_t *ops)
#define ascii_render_set_output_target
#define ascii_stabilizer_prepare
#define VIDEO_ASCII_RENDER_SEQUENCE_BUFFER_SIZE
#define ascii_stabilizer_reset
#define ascii_frame_init
#define ASCII_TEMPORAL_COLOR_TIME_NS
void ascii_legacy_stabilizer_reset(ascii_stabilizer_t *state)
ascii_stabilizer_t * ascii_legacy_stabilizer_set_target(ascii_stabilizer_t *state)
char * ascii_render_glyph_match(const image_t *image, ascii_color_t mode, const simd_cells_t *ops, const char *palette)
#define ascii_stabilizer_choose
char * ascii_legacy_render_halfblocks(const uint8_t *rgb, int width, int height, int stride, ascii_color_t mode, const simd_cells_t *ops)
#define GLYPH_COUNT
#define GLYPH_ASCII_COUNT
#define ascii_half_frame_init
#define ascii_stabilizer_set_target
outbuf_t * ascii_legacy_render_set_output_target(outbuf_t *target)
#define ASCII_TEMPORAL_MAX_INTERVAL_NS
bool ascii_glyph_palette_supported(const char *palette)
#define ascii_frame_emit_row
#define ascii_render
uint32_t ascii_legacy_stabilizer_choose(ascii_temporal_cell_t *cell, const uint8_t samples[16], uint32_t candidate, uint64_t candidate_error, uint64_t previous_error, uint32_t color, uint64_t elapsed_ns, uint32_t *stabilized_color)
#define ascii_render_halfblocks
#define ASCII_TEMPORAL_MOTION_LIMIT
char * ascii_legacy_frame_finish(ascii_frame_t *frame)
asciichat_error_t ascii_legacy_half_frame_init(ascii_frame_t *frame, const uint8_t *rgb, int width, int height, int *stride, ascii_color_t mode)
#define ascii_frame_finish
char * ascii_render_glyph_match_mode(const image_t *image, ascii_color_t mode, const simd_cells_t *ops, const char *palette, bool background)
void ascii_legacy_frame_emit_row(ascii_frame_t *frame, ascii_color_t mode, bool background, bool more_rows, const simd_cells_t *ops)
char * ascii_legacy_render(const image_t *image, const char *palette, ascii_color_t mode, bool background, bool dither, const simd_cells_t *ops)
asciichat_error_t ascii_legacy_frame_init(ascii_frame_t *frame, const image_t *image, const char *palette)
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:102
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 index
Definition queries.h:10
static bool size_t gotel_query_t * out
Definition queries.h:10
return true
Definition queries.h:15
char * platform_strdup(const char *s)
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...
uint64_t last_timestamp_ns
Definition stabilize.h:29
ascii_temporal_cell_t * cells
Definition stabilize.h:25
uint64_t elapsed_ns
Definition stabilize.h:28
uint64_t configuration
Definition stabilize.h:27
uint8_t samples[16]
Definition stabilize.h:14
uint8_t rgb_samples[3][16]
Definition stabilize.h:15
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).
uint8_t b
Blue color component (0-255).
uint8_t g
Green color component (0-255).
uint8_t r
Red color component (0-255).
The useful part of simd cells is in luminance: together they describe what is pending,...
Definition cells.h:14
void(* pack)(const uint8_t *rgb, uint32_t *out, size_t count)
Definition cells.h:16
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
void(* halfblock)(const uint32_t *top, const uint32_t *bottom, uint32_t *glyph, uint32_t *fg, uint32_t *bg, size_t count, unsigned mode, const uint32_t keys[5])
Definition cells.h:19
void(* quantize)(const uint32_t *rgb, uint32_t *out, size_t count, unsigned colors)
Definition cells.h:18
simd_luminance_fn luminance
Definition cells.h:15
void(* lookup)(const uint8_t *luma, const uint32_t table[64], uint32_t *out, size_t count)
Definition cells.h:17
int(* glyph_shape_correlation)(const int16_t samples[16], const int16_t mask[16])
Definition cells.h:23
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)