ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
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_render_options_t

Typedefs

typedef struct image image_t

Enumerations

enum  ascii_color_t { ASCII_COLOR_NONE , ASCII_COLOR_16 , ASCII_COLOR_256 , ASCII_COLOR_TRUE }
enum  ascii_renderer_t { ASCII_RENDERER_LUMINANCE = 0 , ASCII_RENDERER_GLYPH = 1 }

Functions

char * ascii_render_image (const image_t *image, const ascii_render_options_t *options)
bool ascii_render_palette_supported (ascii_renderer_t kind, const char *palette)
char * ascii_render (const image_t *image, const char *palette, ascii_color_t color, bool background, bool dither, const simd_cells_t *ops)
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)
outbuf_t * ascii_render_set_output_target (outbuf_t *target)

Typedef Documentation

◆ image_t

typedef struct image image_t

Enumeration Type Documentation

◆ ascii_color_t

Shared terminal-cell contract for every ISA and scalar fallback. Palettes use complete UTF-8 glyphs from cache64. Runs never cross rows. Background mode sets a contrasting foreground. Colored rows finish with SGR reset before newline; monochrome rows carry no SGR or erase sequences. The returned buffer is owned by the caller and freed with SAFE_FREE. ISA entry points select their vector stages; callers must check CPU support.

Enumerator
ASCII_COLOR_NONE 
ASCII_COLOR_16 
ASCII_COLOR_256 
ASCII_COLOR_TRUE 

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

◆ ascii_renderer_t

Selects the character-matching algorithm.

Enumerator
ASCII_RENDERER_LUMINANCE 
ASCII_RENDERER_GLYPH 

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

Function Documentation

◆ 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}
#define SET_ERRNO(code,...)
@ ERROR_INVALID_PARAM
char * ascii_render_image(const image_t *image, const ascii_render_options_t *options)
SIMD-friendly storage for one RGB pixel. r, g, and b hold the color channels and padding keeps the la...

References ascii_render_image(), ASCII_RENDERER_LUMINANCE, ERROR_INVALID_PARAM, and SET_ERRNO.

◆ 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)

References ascii_render_glyph_match_mode().

◆ 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;
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
char cells[(512 *256 *2)]
Definition fft.c:22
#define GLYPH_MASK_MAX_SUM
unsigned int uint32_t
unsigned long long uint64_t
unsigned char uint8_t
@ ASCIICHAT_OK
Definition error_codes.h:50
#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
void emit_row_end(outbuf_t *out, bool colored, bool newline)
static bool size_t index
Definition queries.h:8
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
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).
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
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)

References ASCII_COLOR_16, ASCII_COLOR_256, ASCII_COLOR_NONE, ASCII_COLOR_TRUE, ascii_emit_prepared_row(), ascii_frame_emit_row, ascii_frame_finish, ascii_frame_init, ascii_glyph_palette_supported(), ascii_stabilizer_choose, ASCII_TEMPORAL_COLOR_TIME_NS, ASCII_TEMPORAL_MAX_INTERVAL_NS, ASCII_TEMPORAL_MOTION_LIMIT, ASCIICHAT_OK, rgb_pixel_t::b, ascii_temporal_cell_t::background, ascii_frame_t::bg, ascii_stabilizer_t::cells, cells, glyph_candidate_t::centered, ascii_stabilizer_t::elapsed_ns, emit_row_end(), glyph_candidate_t::energy, ERROR_INVALID_PARAM, ascii_frame_t::fg, rgb_pixel_t::g, ascii_frame_t::glyph, GLYPH_ASCII_COUNT, GLYPH_COUNT, GLYPH_MASK_MAX_SUM, simd_cells_t::glyph_shape_correlation, glyph_shape_correlation_scalar(), image::h, ascii_frame_t::index, index, glyph_candidate_t::key, ascii_frame_t::luma, glyph_candidate_t::mask, ascii_frame_t::output, ascii_frame_t::packed, image::pixels, simd_cells_t::quantize, rgb_pixel_t::r, ascii_temporal_cell_t::rgb_samples, ascii_frame_t::row, ascii_temporal_cell_t::samples, SET_ERRNO, ascii_frame_t::stabilizer, glyph_candidate_t::sum, ascii_frame_t::temporal_preselected, glyph_candidate_t::tone, ascii_temporal_cell_t::valid, image::w, and ascii_frame_t::width.

Referenced by ascii_render_glyph_match().

◆ 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}
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
char * ascii_luminance_halfblocks(const uint8_t *rgb, int width, int height, int stride, ascii_color_t mode, const simd_cells_t *ops)

References ascii_luminance_halfblocks(), height, and width.

◆ ascii_render_image()

char * ascii_render_image ( const image_t * image,
const ascii_render_options_t * options )

Render an image using the chosen renderer. The caller frees the returned text. Luminance maps one input pixel to one terminal cell. Glyph matching consumes an 8×8 pixel region per cell and requires both image dimensions to be positive multiples of eight. Callers select and scale the input to their desired cell dimensions before calling this function.

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

6 {
7 if (!options) {
8 SET_ERRNO(ERROR_INVALID_PARAM, "Missing ASCII rendering options");
9 return NULL;
10 }
11 const simd_cells_t *backend = options->backend ? options->backend : simd_cells_backend();
12 switch (options->kind) {
14 return ascii_luminance_render(image, options->palette, options->color, options->background, options->dither, backend);
16 return ascii_glyph_render(image, options->color, backend, options->palette, options->background);
17 }
18 SET_ERRNO(ERROR_INVALID_PARAM, "Unknown ASCII renderer");
19 return NULL;
20}
const simd_cells_t * simd_cells_backend(void)
Definition cells.c:61
char * ascii_glyph_render(const image_t *image, ascii_color_t mode, const simd_cells_t *ops, const char *palette, bool background)
Definition glyph.c:198
char * ascii_luminance_render(const image_t *image, const char *palette, ascii_color_t mode, bool background, bool dither, const simd_cells_t *ops)
The useful part of simd cells is in luminance: together they describe what is pending,...
Definition cells.h:14

References ascii_glyph_render(), ascii_luminance_render(), ASCII_RENDERER_GLYPH, ASCII_RENDERER_LUMINANCE, ascii_render_options_t::backend, ascii_render_options_t::background, ascii_render_options_t::color, ascii_render_options_t::dither, ERROR_INVALID_PARAM, ascii_render_options_t::kind, ascii_render_options_t::palette, SET_ERRNO, and simd_cells_backend().

Referenced by ascii_convert_with_buffers_stabilized(), and ascii_render().

◆ ascii_render_palette_supported()

bool ascii_render_palette_supported ( ascii_renderer_t kind,
const char * palette )

Check whether a renderer accepts a nonempty palette.

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

22 {
23 if (kind == ASCII_RENDERER_GLYPH)
25 return kind == ASCII_RENDERER_LUMINANCE && palette && *palette;
26}
bool ascii_glyph_palette_supported_impl(const char *palette)
Definition glyph.c:68

References ascii_glyph_palette_supported_impl(), ASCII_RENDERER_GLYPH, and ASCII_RENDERER_LUMINANCE.

Referenced by acip_render_settings_prepare(), ascii_convert_with_buffers_stabilized(), mirror_convert_frame(), and validate_glyph_palette().

◆ 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}
Dynamic output buffer (auto-expanding).