ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
render.h File Reference

Go to the source code of this file.

Data Structures

struct  ascii_frame_t
 When code needs to work with ascii frame, it keeps output, output_target, width, source_height here instead of scattering it across globals. Initialization establishes the handles and sizes; teardown must release any allocation, lock, thread, or callback context recorded in those fields. More...

Macros

#define ASCII_INLINE_SCRATCH_COLUMNS   512
#define ASCII_SCRATCH_PLANES   6
#define ASCII_FRAME_DECLARED
#define DECLARE_ASCII_PROCESSOR(isa)
#define DECLARE_HALF_PROCESSOR(isa)

Functions

asciichat_error_t ascii_stabilizer_prepare (ascii_stabilizer_t *state, size_t width, size_t height, uint64_t configuration)
void ascii_stabilizer_reset (ascii_stabilizer_t *state)
ascii_stabilizer_t * ascii_stabilizer_set_target (ascii_stabilizer_t *state)
uint32_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, uint32_t *stabilized_color)
char * ascii_render (const image_t *image, const char *palette, ascii_color_t color, bool background, bool dither, const simd_cells_t *ops)
bool ascii_glyph_palette_supported (const char *palette)
char * ascii_render_glyph_match (const image_t *image, ascii_color_t color, const simd_cells_t *ops, const char *palette)
char * ascii_render_glyph_match_mode (const image_t *image, ascii_color_t color, const simd_cells_t *ops, const char *palette, bool background)
char * ascii_render_halfblocks (const uint8_t *rgb, int width, int height, int stride_bytes, ascii_color_t color, const simd_cells_t *ops)
asciichat_error_t ascii_frame_init (ascii_frame_t *frame, const image_t *image, const char *palette)
void ascii_frame_emit_row (ascii_frame_t *frame, ascii_color_t mode, bool background, bool more_rows, const simd_cells_t *ops)
char * ascii_frame_finish (ascii_frame_t *frame)
char * render_ascii_cells_sse2 (const image_t *, const char *, ascii_color_t, bool)
char * render_ascii_cells_ssse3 (const image_t *, const char *, ascii_color_t, bool)
char * render_ascii_cells_avx2 (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_sve (const image_t *, const char *, ascii_color_t, bool)
asciichat_error_t ascii_half_frame_init (ascii_frame_t *frame, const uint8_t *rgb, int width, int height, int *stride, ascii_color_t mode)
void ascii_frame_emit_halfrow (ascii_frame_t *frame, ascii_color_t mode, bool more_rows, const simd_cells_t *ops)
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_halfblocks_neon (const uint8_t *, int, int, int, ascii_color_t)
char * render_halfblocks_sve (const uint8_t *, int, int, int, ascii_color_t)
outbuf_t * ascii_render_set_output_target (outbuf_t *target)

Macro Definition Documentation

◆ ASCII_FRAME_DECLARED

#define ASCII_FRAME_DECLARED

Definition at line 34 of file include/ascii-chat/video/ascii/render.h.

◆ ASCII_INLINE_SCRATCH_COLUMNS

#define ASCII_INLINE_SCRATCH_COLUMNS   512

Definition at line 4 of file include/ascii-chat/video/ascii/render.h.

◆ ASCII_SCRATCH_PLANES

#define ASCII_SCRATCH_PLANES   6

Definition at line 5 of file include/ascii-chat/video/ascii/render.h.

◆ DECLARE_ASCII_PROCESSOR

#define DECLARE_ASCII_PROCESSOR ( isa)
Value:
char *render_ascii_cells_##isa(const image_t *, const char *, ascii_color_t, bool)

Definition at line 60 of file include/ascii-chat/video/ascii/render.h.

◆ DECLARE_HALF_PROCESSOR

#define DECLARE_HALF_PROCESSOR ( isa)
Value:
char *render_halfblocks_##isa(const uint8_t *, int, int, int, ascii_color_t)
unsigned char uint8_t

Definition at line 70 of file include/ascii-chat/video/ascii/render.h.

Function Documentation

◆ ascii_frame_emit_halfrow()

void ascii_frame_emit_halfrow ( ascii_frame_t * frame,
ascii_color_t mode,
bool more_rows,
const simd_cells_t * ops )

Definition at line 184 of file video/ascii/render/common.c.

184 {
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}
void emit_row_end(outbuf_t *out, bool colored, bool newline)
void ascii_stabilizer_halfrow(ascii_frame_t *frame, ascii_color_t mode, const simd_cells_t *ops)
Definition stabilize.c:175
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)

◆ ascii_frame_emit_row()

void ascii_frame_emit_row ( ascii_frame_t * frame,
ascii_color_t mode,
bool background,
bool more_rows,
const simd_cells_t * ops )

Definition at line 131 of file video/ascii/render/common.c.

131 {
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}
unsigned int uint32_t
void ascii_stabilizer_frame_row(ascii_frame_t *frame, ascii_color_t mode, bool background, const simd_cells_t *ops)
Definition stabilize.c:123
uint32_t ascii_background_contrast(ascii_color_t mode, uint32_t color, uint8_t luma)

◆ ascii_frame_finish()

char * ascii_frame_finish ( ascii_frame_t * frame)

Definition at line 146 of file video/ascii/render/common.c.

146 {
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}
#define SAFE_FREE(ptr)
void ob_term(outbuf_t *ob)
Append null terminator to buffer.
char * buf
Buffer pointer (allocated, owned by caller, must be freed).

◆ ascii_frame_init()

asciichat_error_t ascii_frame_init ( ascii_frame_t * frame,
const image_t * image,
const char * palette )

Definition at line 93 of file video/ascii/render/common.c.

93 {
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}
#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
#define SAFE_MALLOC(size, cast)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
utf8_palette_cache_t * get_utf8_palette_cache(const char *ascii_chars)
Get UTF-8 palette cache for a character set.
struct utf8_palette_cache utf8_palette_cache_t
#define ascii_render_set_output_target
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:102
ascii_stabilizer_t * ascii_stabilizer_target(void)
Definition stabilize.c:9
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).

◆ ascii_glyph_palette_supported()

bool ascii_glyph_palette_supported ( const char * palette)

Definition at line 542 of file lib/video/ascii/render.c.

542 {
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}

Referenced by ascii_render_glyph_match_mode().

◆ ascii_half_frame_init()

asciichat_error_t ascii_half_frame_init ( ascii_frame_t * frame,
const uint8_t * rgb,
int width,
int height,
int * stride,
ascii_color_t mode )

Definition at line 155 of file video/ascii/render/common.c.

155 {
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}

◆ ascii_render()

char * ascii_render ( const image_t * image,
const char * palette,
ascii_color_t color,
bool background,
bool dither,
const simd_cells_t * ops )

Definition at line 28 of file lib/video/ascii/render/render.c.

28 {
29 if (!ops) {
30 SET_ERRNO(ERROR_INVALID_PARAM, "Missing ASCII rendering backend");
31 return NULL;
32 }
33 const ascii_render_options_t options = {.kind = ASCII_RENDERER_LUMINANCE, .color = color, .palette = palette, .backend = ops, .background = background, .dither = dither};
34 return ascii_render_image(image, &options);
35}
char * ascii_render_image(const image_t *image, const ascii_render_options_t *options)
@ ASCII_RENDERER_LUMINANCE

◆ ascii_render_glyph_match()

char * ascii_render_glyph_match ( const image_t * image,
ascii_color_t color,
const simd_cells_t * ops,
const char * palette )

Definition at line 586 of file lib/video/ascii/render.c.

586 {
587 return ascii_render_glyph_match_mode(image, mode, ops, palette, false);
588}
char * ascii_render_glyph_match_mode(const image_t *image, ascii_color_t mode, const simd_cells_t *ops, const char *palette, bool background)

◆ ascii_render_glyph_match_mode()

char * ascii_render_glyph_match_mode ( const image_t * image,
ascii_color_t color,
const simd_cells_t * ops,
const char * palette,
bool background )

Definition at line 590 of file lib/video/ascii/render.c.

590 {
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}
int glyph_shape_correlation_scalar(const int16_t samples[16], const int16_t mask[16])
Definition cells.c:4
#define GLYPH_MASK_MAX_SUM
unsigned long long uint64_t
#define ascii_frame_init
#define ASCII_TEMPORAL_COLOR_TIME_NS
#define ascii_stabilizer_choose
#define GLYPH_COUNT
#define GLYPH_ASCII_COUNT
#define ASCII_TEMPORAL_MAX_INTERVAL_NS
bool ascii_glyph_palette_supported(const char *palette)
#define ascii_frame_emit_row
#define ASCII_TEMPORAL_MOTION_LIMIT
#define ascii_frame_finish
static bool size_t index
Definition queries.h:10
When code needs to work with ascii frame, it keeps output, output_target, width, source_height here i...
ascii_temporal_cell_t * cells
Definition stabilize.h:25
uint64_t elapsed_ns
Definition stabilize.h:28
uint8_t samples[16]
Definition stabilize.h:14
uint8_t rgb_samples[3][16]
Definition stabilize.h:15
uint8_t b
Blue color component (0-255).
uint8_t g
Green color component (0-255).
uint8_t r
Red color component (0-255).
void(* quantize)(const uint32_t *rgb, uint32_t *out, size_t count, unsigned colors)
Definition cells.h:18
int(* glyph_shape_correlation)(const int16_t samples[16], const int16_t mask[16])
Definition cells.h:23

◆ ascii_render_halfblocks()

char * ascii_render_halfblocks ( const uint8_t * rgb,
int width,
int height,
int stride_bytes,
ascii_color_t color,
const simd_cells_t * ops )

Two source rows per terminal row; the supplied backend processes both rows.

Definition at line 37 of file lib/video/ascii/render/render.c.

37 {
38 return ascii_luminance_halfblocks(rgb, width, height, stride, color, ops);
39}
char * ascii_luminance_halfblocks(const uint8_t *rgb, int width, int height, int stride, ascii_color_t mode, const simd_cells_t *ops)

◆ ascii_render_set_output_target()

outbuf_t * ascii_render_set_output_target ( outbuf_t * target)

Internal one-shot output target, consumed by the next frame initializer. Returns the prior target so capability conversion can restore its scope.

Definition at line 15 of file video/ascii/render/common.c.

15 {
16 outbuf_t *previous = g_output_target;
17 g_output_target = target;
18 return previous;
19}

◆ ascii_stabilizer_choose()

uint32_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,
uint32_t * stabilized_color )

Choose a glyph using current and previous candidate errors and patch motion.

Definition at line 61 of file stabilize.c.

62 {
63 unsigned motion = 0;
64 if (cell->valid) {
65 for (size_t i = 0; i < 16; ++i)
66 motion += (unsigned)abs((int)samples[i] - (int)cell->samples[i]);
67 motion /= 16;
68 }
69 uint32_t result = candidate;
70 uint32_t output_color = color;
71 if (cell->valid && motion <= ASCII_TEMPORAL_MOTION_LIMIT && elapsed_ns <= ASCII_TEMPORAL_MAX_INTERVAL_NS) {
72 uint64_t margin = candidate_error / 8 + 128;
73 if (previous_error != UINT64_MAX && (previous_error <= candidate_error || previous_error - candidate_error <= margin))
74 result = cell->glyph;
75 // Weight depends on elapsed time, avoiding a different response at each FPS.
76 unsigned weight = (unsigned)(elapsed_ns * 256 / (elapsed_ns + ASCII_TEMPORAL_COLOR_TIME_NS));
77 output_color = ascii_stabilizer_blend_color(cell->color, color, weight);
78 }
79 memcpy(cell->samples, samples, sizeof(cell->samples));
80 cell->glyph = result;
81 cell->color = output_color;
82 cell->valid = true;
83 if (stabilized_color)
84 *stabilized_color = output_color;
85 return result;
86}
uint32_t ascii_stabilizer_blend_color(uint32_t previous, uint32_t current, unsigned weight)
Definition stabilize.c:44

◆ ascii_stabilizer_prepare()

asciichat_error_t ascii_stabilizer_prepare ( ascii_stabilizer_t * state,
size_t width,
size_t height,
uint64_t configuration )

Prepare history for a viewport and rendering contract, resetting it on change.

Definition at line 26 of file stabilize.c.

26 {
27 if (!state || !width || !height || width > SIZE_MAX / height || width * height > SIZE_MAX / sizeof(ascii_temporal_cell_t))
28 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid stabilization viewport");
29 if (state->cells && state->width == width && state->height == height && state->configuration == configuration)
30 return ASCIICHAT_OK;
32 if (!cells)
33 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate stabilization history");
34 SAFE_FREE(state->cells);
35 state->cells = cells;
36 state->width = width;
37 state->height = height;
38 state->configuration = configuration;
39 state->last_timestamp_ns = 0;
40 state->elapsed_ns = UINT64_MAX;
41 return ASCIICHAT_OK;
42}
#define SAFE_CALLOC(count, size, cast)
@ ERROR_MEMORY
Definition error_codes.h:55
uint64_t last_timestamp_ns
Definition stabilize.h:29
uint64_t configuration
Definition stabilize.h:27

◆ ascii_stabilizer_reset()

void ascii_stabilizer_reset ( ascii_stabilizer_t * state)

Definition at line 19 of file stabilize.c.

19 {
20 if (!state)
21 return;
22 SAFE_FREE(state->cells);
23 memset(state, 0, sizeof(*state));
24}

◆ ascii_stabilizer_set_target()

ascii_stabilizer_t * ascii_stabilizer_set_target ( ascii_stabilizer_t * state)

Select history for the current thread until the previous target is restored.

Definition at line 13 of file stabilize.c.

13 {
14 ascii_stabilizer_t *previous = g_stabilizer_target;
15 g_stabilizer_target = state;
16 return previous;
17}

◆ render_ascii_cells_avx2()

char * render_ascii_cells_avx2 ( const image_t * ,
const char * ,
ascii_color_t ,
bool  )

◆ render_ascii_cells_neon()

char * render_ascii_cells_neon ( const image_t * ,
const char * ,
ascii_color_t ,
bool  )

◆ render_ascii_cells_sse2()

char * render_ascii_cells_sse2 ( const image_t * ,
const char * ,
ascii_color_t ,
bool  )

◆ render_ascii_cells_ssse3()

char * render_ascii_cells_ssse3 ( const image_t * ,
const char * ,
ascii_color_t ,
bool  )

◆ render_ascii_cells_sve()

char * render_ascii_cells_sve ( const image_t * ,
const char * ,
ascii_color_t ,
bool  )

References height, and width.

Referenced by ascii_legacy_render(), and ascii_luminance_render().

◆ render_halfblocks_avx2()

char * render_halfblocks_avx2 ( const uint8_t * ,
int ,
int ,
int ,
ascii_color_t  )

◆ render_halfblocks_neon()

char * render_halfblocks_neon ( const uint8_t * ,
int ,
int ,
int ,
ascii_color_t  )

◆ render_halfblocks_sse2()

char * render_halfblocks_sse2 ( const uint8_t * ,
int ,
int ,
int ,
ascii_color_t  )

◆ render_halfblocks_ssse3()

char * render_halfblocks_ssse3 ( const uint8_t * ,
int ,
int ,
int ,
ascii_color_t  )

◆ render_halfblocks_sve()

char * render_halfblocks_sve ( const uint8_t * ,
int ,
int ,
int ,
ascii_color_t  )